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
sequencelengths 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 | 261c8f8ac28b9772e2a26adbbb00c773 | train_000.jsonl | 1397749200 | One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once.The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table. | 256 megabytes | import java.io.*;
import java.util.*;
public class Task{
public static void main(String[] args) throws IOException{
new Task().run();
}
StreamTokenizer in;
Scanner ins;
PrintWriter out;
int nextInt() throws IOException{
in.nextToken();
return (int)in.nval;
}
long nextLong() throws IOException{
in.nextToken();
return (long)in.nval;
}
double nextDouble() throws IOException{
in.nextToken();
return in.nval;
}
char nextChar() throws IOException{
in.nextToken();
return (char)in.ttype;
}
boolean isNextChar() throws IOException{
in.nextToken();
return in.ttype != StreamTokenizer.TT_NUMBER;
}
int getReadedInt(){
return (int) in.nval;
}
String nextString() throws IOException{
in.nextToken();
return in.sval;
}
private static final String INPUT = "g.in";
Stack<Integer>[] tabels;
void run() throws IOException{
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
ins = new Scanner(System.in);
out = new PrintWriter(System.out);
try{
if(System.getProperty("xDx") != null){
in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
ins = new Scanner(new FileReader("input.txt"));
out = new PrintWriter(new FileWriter("output.txt"));
}else{
// in = new StreamTokenizer(new BufferedReader(new FileReader(INPUT)));
// ins = new Scanner(new FileReader(INPUT));
// out = new PrintWriter(System.out);
}
}catch(Exception e){
// in = new StreamTokenizer(new BufferedReader(new FileReader(INPUT)));
// ins = new Scanner(new FileReader(INPUT));
// out = new PrintWriter(System.out);
}
int n = nextInt(), k = nextInt();
if(k*n*2 > n*(n - 1)){
out.print(-1);
}else{
resolve(n, k);
}
out.close();
}
private void resolve(int n, int k) {
out.println(n*k);
for(int i = 0; i < n; i++){
for(int j = i + 1; j < i + 1 + k; ++j){
// out.printf("%d %d\n", i + 1, j%n + 1);
out.println((i + 1) +" " + (j%n + 1));
}
}
}
} | Java | ["3 1"] | 1 second | ["3\n1 2\n2 3\n3 1"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"graphs"
] | 14570079152bbf6c439bfceef9816f7e | The first line contains two integers — n and k (1 ≤ n, k ≤ 1000). | 1,400 | In the first line print an integer m — number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi). The numbers ai and bi mean, that in the i-th match the team with number ai won against the team with number bi. You can assume, that the teams are numbered from 1 to n. If a tournir that meets the conditions of the problem does not exist, then print -1. | standard output | |
PASSED | c224dc3315d863fb4cd53577367fbe7c | train_000.jsonl | 1397749200 | One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once.The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
public void run() {
try {
int n = reader.nextInt();
int k = reader.nextInt();
if ((n - 1) / 2 < k) {
writer.println(-1);
} else {
writer.println(n * k);
for (int i = 0; i < n; ++ i) {
for (int j = 1; j <= k; ++ j) {
writer.println("" + (i + 1) + " " + ((i + j) % n + 1));
}
}
}
// main
} catch (IOException ex) {
}
writer.close();
}
Main() {
reader = new InputReader(System.in);
writer = new PrintWriter(System.out);
}
public static void main(String[] args) {
new Main().run();
}
private void debug(Object...os) {
System.err.println(Arrays.deepToString(os));
}
private InputReader reader;
private PrintWriter writer;
}
class InputReader {
InputReader(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = new StringTokenizer("");
}
private String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public Integer nextInt() throws IOException {
return Integer.parseInt(next());
}
private BufferedReader reader;
private StringTokenizer tokenizer;
}
| Java | ["3 1"] | 1 second | ["3\n1 2\n2 3\n3 1"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"graphs"
] | 14570079152bbf6c439bfceef9816f7e | The first line contains two integers — n and k (1 ≤ n, k ≤ 1000). | 1,400 | In the first line print an integer m — number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi). The numbers ai and bi mean, that in the i-th match the team with number ai won against the team with number bi. You can assume, that the teams are numbered from 1 to n. If a tournir that meets the conditions of the problem does not exist, then print -1. | standard output | |
PASSED | 7981404b29141e8fc28c8f8b775ed9f8 | train_000.jsonl | 1397749200 | One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once.The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table. | 256 megabytes | import java.io.*;
import java.util.*;
public class example {
FastScanner in;
PrintWriter out;
class Point {
int x, y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
void solve() {
int n = in.nextInt();
int k = in.nextInt();
if (n < 2 * k + 1) {
out.println(-1);
return;
}
out.println(k * n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
int next = (i + j + 1) % n;
out.println((i + 1) + " " + (next + 1));
}
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
void run() {
try {
in = new FastScanner(new File("test.in"));
out = new PrintWriter(new File("test.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new example().runIO();
}
}
| Java | ["3 1"] | 1 second | ["3\n1 2\n2 3\n3 1"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"graphs"
] | 14570079152bbf6c439bfceef9816f7e | The first line contains two integers — n and k (1 ≤ n, k ≤ 1000). | 1,400 | In the first line print an integer m — number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi). The numbers ai and bi mean, that in the i-th match the team with number ai won against the team with number bi. You can assume, that the teams are numbered from 1 to n. If a tournir that meets the conditions of the problem does not exist, then print -1. | standard output | |
PASSED | 7c5fe9dc237c7c59071c2d5de52c230d | train_000.jsonl | 1397749200 | One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once.The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
public class Main {
private static StringTokenizer st;
private static BufferedReader br;
public static long MOD = 1000000007;
private static List<List<Integer>>[] ways;
public static void print(Object x) {
System.out.println(x + "");
}
public static void printArr(long[] x) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < x.length; i++) {
s.append(x[i] + " ");
}
print(s);
}
public static void printArr(int[] x) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < x.length; i++) {
s.append(x[i] + " ");
}
print(s);
}
public static String join(Collection<?> x, String space) {
if (x.size() == 0) return "";
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Object elt : x) {
if (first) first = false;
else sb.append(space);
sb.append(elt);
}
return sb.toString();
}
public static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
st = new StringTokenizer(line.trim());
}
return st.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static List<Integer> nextInts(int N) throws IOException {
List<Integer> ret = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
ret.add(nextInt());
}
return ret;
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
int k = nextInt();
if (2 * k > n - 1) {
print(-1);
} else {
print(n * k);
StringBuilder out = new StringBuilder();
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++) {
int a = i + 1;
int b = ((i + j) % n) + 1;
out.append(a + " " + b + "\n");
}
}
print(out);
}
}
}
| Java | ["3 1"] | 1 second | ["3\n1 2\n2 3\n3 1"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"graphs"
] | 14570079152bbf6c439bfceef9816f7e | The first line contains two integers — n and k (1 ≤ n, k ≤ 1000). | 1,400 | In the first line print an integer m — number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi). The numbers ai and bi mean, that in the i-th match the team with number ai won against the team with number bi. You can assume, that the teams are numbered from 1 to n. If a tournir that meets the conditions of the problem does not exist, then print -1. | standard output | |
PASSED | a5ff1f0c751864c30ef7dbb49f95123a | train_000.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 AverageSuperheroGangPowerB {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int maxPerSuperHeroe = scanner.nextInt();
int totOperations = scanner.nextInt();
double sum = 0;
int[] powers = new int[n+1];
powers[0] = -1;
for (int i = 1; i <=n ; i++) {
powers[i] = scanner.nextInt();
sum += powers[i];
}
Arrays.sort(powers);
double mx = (double)(sum+Math.min(totOperations, (long)n*maxPerSuperHeroe))/(double)(n);
double increase;
for (int i = 1; i<=Math.min(n-1, totOperations) ; i++) {
sum -= powers[i];
increase = sum + Math.min(totOperations-i, (long)(n-i)*maxPerSuperHeroe);
mx = Math.max(mx, (double)increase/(n-i));
}
System.out.println(mx);
}
}
| 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 | 4d2cea39b2f11375c4d70845d2dad58b | train_000.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.Arrays;
public class ProbB {
public static int n, k, m;
public static int[] arr;
public static long[] prefixSum;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] tokens = br.readLine().split(" ");
n = Integer.parseInt(tokens[0]);
k = Integer.parseInt(tokens[1]);
m = Integer.parseInt(tokens[2]);
arr = new int[n];
tokens = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(tokens[i]);
}
Arrays.sort(arr); // Sort array
prefixSum = new long[n];
prefixSum[0] = (long) arr[0];
for (int i = 1; i < n; i++) {
prefixSum[i] = prefixSum[i-1] + (long) arr[i];
}
int maxRemovalsPossible = Math.min(m, n-1); // Cant remove all (n) superheroes nor exceed the number of ops (m)
double maxAvg = 0;
for (int i = 0; i <= maxRemovalsPossible; i++) {
long currM = m - i;
if (currM < 0) break;
long currHeroes = n - i;
long currSum = f(i);
// or we add k per each hero or we add all that's left of m
currSum += Math.min(currHeroes * k, currM);
double currAvg = currSum / (double) currHeroes;
maxAvg = Math.max(maxAvg, currAvg);
}
System.out.println(maxAvg);
}
// Sum by removing first n-th elements
public static long f(int i) {
if (i == 0) return prefixSum[n-1];
return prefixSum[n-1] - prefixSum[i-1];
}
}
| 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 | 6f6aac390fbd2eda39c76cfcf94c4358 | train_000.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.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class B1111 {
public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new FileReader("F:/books/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s1 = br.readLine().split(" ");
int n = Integer.parseInt(s1[0]);
int k = Integer.parseInt(s1[1]);
int m = Integer.parseInt(s1[2]);
long[] a = new long[n];
String[] s2 = br.readLine().split(" ");
for(int i=0;i<n;i++) {
a[i] = Long.parseLong(s2[i]);
}
Arrays.sort(a);
for(int i=1;i<n;i++) {
a[i] += a[i-1];
}
Double max = Double.MIN_VALUE;
for(int i=0;i<n;i++) {
max = Math.max(max, calc(a,i,k,m));
}
System.out.println(max);
}
private static Double calc(long[] a, int x, long k, long m) {
Double ret = Double.MIN_VALUE;
int l = a.length;
if(x>m) return ret;
long sum = a[l-1];
sum -= (x>0)?a[x-1]:0;
long r = l-x;
long inc = Math.min(r*k, m-x);
sum += inc;
ret = ((double)sum/(double)r);
return ret;
}
}
| 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 | 0bc5c7737b3c7961c05f3ffe5809cabb | train_000.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.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1[]=br.readLine().split(" ");
String s2[]=br.readLine().split(" ");
int n=Integer.parseInt(s1[0]);
int k=Integer.parseInt(s1[1]);
int m=Integer.parseInt(s1[2]);
long a[]=new long[n];
long S=0;
for(int i=0;i<n;i++)
{ a[i]=Long.parseLong(s2[i]); S+=a[i]; }
Arrays.sort(a);
double max=(double)(S+Math.min(m,(long)n*k))/(double)n;
for(int i=0;i<Math.min(n-1,m);i++)
{
long mm=m-i-1;
long nn=n;
long kk=k;
long j=i;
S-=a[i];
max=Math.max(max,(double)(S+Math.min(mm,(nn-j-1)*kk))/((double)(n-i-1)));
}
System.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 | 91f9bfa42c6075c79af3e68f0edfa2cc | train_000.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 static java.lang.Math.*;
import static java.lang.Math.min;
import static java.util.Arrays.*;
import java.util.*;
public class B {
public Object solve () {
int N = sc.nextInt(), K = sc.nextInt(), M = sc.nextInt();
int [] A = sc.nextInts();
sort(A, Collections.reverseOrder());
double [] S = new double [N+1];
for (int i : rep(N))
S[i+1] = S[i] + A[i];
double res = 0;
for (int i : req(1, N)) {
// Keep the top i, remove the rest
long Z = M - (N - i);
if (Z >= 0) {
Z = min(Z, 1L * K * i);
res = max(res, (S[i] + Z) / i);
}
}
return res;
}
private static final boolean ONE_TEST_CASE = true;
private static final boolean CASE_PREFIX = false;
private static void init () {
}
private static Integer [] boxed (int ... A) { return stream(A).mapToObj(x -> x).toArray(Integer[]::new); }
private static int [] rep (int N) { return rep(0, N); }
private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
private static int [] req (int S, int T) { return rep(S, T+1); }
private static int [] sort (int [] A, Comparator<Integer> C) { Integer [] AA = boxed(A); Arrays.sort(AA, C); return unboxed(AA, A); }
private static int [] unboxed (Integer [] A) { return stream(A).mapToInt(x -> x).toArray(); }
private static int [] unboxed (Integer [] A, int [] a) { System.arraycopy(unboxed(A), 0, a, 0, A.length); return a; }
//////////////////////////////////////////////////////////////////////////////////// OFF
private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static class IOUtils {
public static class MyScanner {
public String next () { newLine(); return line[index++]; }
public int nextInt () { return Integer.parseInt(next()); }
public String nextLine () { line = null; return readLine(); }
public String [] nextStrings () { return split(nextLine()); }
public int[] nextInts () { return nextStream().mapToInt(Integer::parseInt).toArray(); }
//////////////////////////////////////////////
private boolean eol () { return index == line.length; }
private String readLine () {
try {
return r.readLine();
} catch (Exception e) {
throw new Error (e);
}
}
private final java.io.BufferedReader r;
private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
private MyScanner (java.io.BufferedReader r) {
try {
this.r = r;
while (!r.ready())
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine () {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private java.util.stream.Stream<String> nextStream () { return java.util.Arrays.stream(nextStrings()); }
private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim (String delim, Object o, Object ... A) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : A)
append(b, p, delim);
return b.substring(delim.length());
}
//////////////////////////////////////////////////////////////////////////////////
private static final java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
private static void start () { if (t == 0) t = millis(); }
private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
f.accept(java.lang.reflect.Array.get(o, i));
}
else if (o instanceof Iterable<?>)
((Iterable<?>)o).forEach(f::accept);
else
g.accept(o instanceof Double ? formatter.format(o) : o);
}
private static void append (final StringBuilder b, Object o, final String delim) {
append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o);
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void err (Object o, Object ... A) { System.err.println(build(o, A)); }
private static boolean CMD, DEBUG;
private static void write (Object o) {
if (CMD)
err(o, '(', time(), ')');
pw.println(o);
if (DEBUG) {
pw.flush();
System.out.flush();
}
}
private static void exit () {
IOUtils.pw.close();
System.out.flush();
if (DEBUG) {
err("------------------");
err(time());
}
System.exit(0);
}
private static long t;
private static long millis () { return System.currentTimeMillis(); }
private static String time () { return "Time: " + (millis() - t) / 1000.0; }
private static void run (int N) {
try {
CMD = System.getProperties().containsKey("CMD");
DEBUG = System.getProperties().containsKey("DEBUG");
}
catch (Throwable t) {}
for (int n = 1; n <= N; ++n) {
Object res = new B().solve();
if (res != null)
write((CASE_PREFIX ? "Case #" + n + ": " : "") + build(res));
}
exit();
}
}
////////////////////////////////////////////////////////////////////////////////////
public static void main (String[] args) {
init();
int N = ONE_TEST_CASE ? 1 : sc.nextInt();
IOUtils.run(N);
}
}
| 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 | b713e917779eeb18c8d35982cd06d9a4 | train_000.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 static java.lang.Math.*;
import static java.lang.Math.min;
import static java.util.Arrays.*;
import java.util.*;
public class B {
public Object solve () {
int N = sc.nextInt(), K = sc.nextInt(), M = sc.nextInt();
int [] A = sc.nextInts();
sort(A, Collections.reverseOrder());
double [] S = new double [N+1];
for (int i : rep(N))
S[i+1] = S[i] + A[i];
double res = 0;
for (int i : req(1, N)) {
// Keep the top i, remove the rest
int Z = M - (N - i);
if (Z >= 0) {
Z = (int) min(Z, 1L * K * i);
res = max(res, (S[i] + Z) / i);
}
}
return print(res);
}
private static final boolean ONE_TEST_CASE = true;
private static void init () {
}
private static Integer [] boxed (int ... A) { return stream(A).mapToObj(x -> x).toArray(Integer[]::new); }
private static int [] rep (int N) { return rep(0, N); }
private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
private static int [] req (int S, int T) { return rep(S, T+1); }
private static int [] sort (int [] A, Comparator<Integer> C) { Integer [] AA = boxed(A); Arrays.sort(AA, C); return unboxed(AA, A); }
private static int [] unboxed (Integer [] A) { return stream(A).mapToInt(x -> x).toArray(); }
private static int [] unboxed (Integer [] A, int [] a) { System.arraycopy(unboxed(A), 0, a, 0, A.length); return a; }
//////////////////////////////////////////////////////////////////////////////////// OFF
private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static Object print (Object o, Object ... A) { IOUtils.print(o, A); return null; }
private static class IOUtils {
public static class MyScanner {
public String next () { newLine(); return line[index++]; }
public int nextInt () { return Integer.parseInt(next()); }
public String nextLine () { line = null; return readLine(); }
public String [] nextStrings () { return split(nextLine()); }
public int[] nextInts () { return nextStream().mapToInt(Integer::parseInt).toArray(); }
//////////////////////////////////////////////
private boolean eol () { return index == line.length; }
private String readLine () {
try {
return r.readLine();
} catch (Exception e) {
throw new Error (e);
}
}
private final java.io.BufferedReader r;
private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
private MyScanner (java.io.BufferedReader r) {
try {
this.r = r;
while (!r.ready())
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine () {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private java.util.stream.Stream<String> nextStream () { return java.util.Arrays.stream(nextStrings()); }
private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim (String delim, Object o, Object ... A) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : A)
append(b, p, delim);
return b.substring(delim.length());
}
//////////////////////////////////////////////////////////////////////////////////
private static final java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
private static void start () { if (t == 0) t = millis(); }
private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
f.accept(java.lang.reflect.Array.get(o, i));
}
else if (o instanceof Iterable<?>)
((Iterable<?>)o).forEach(f::accept);
else
g.accept(o instanceof Double ? formatter.format(o) : o);
}
private static void append (final StringBuilder b, Object o, final String delim) {
append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o);
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void print (Object o, Object ... A) {
pw.println(build(o, A));
if (DEBUG)
pw.flush();
}
private static void err (Object o, Object ... A) { System.err.println(build(o, A)); }
private static boolean DEBUG;
private static void write (Object o) {
err(o, '(', time(), ')');
if (!DEBUG)
pw.println(o);
}
private static void exit () {
IOUtils.pw.close();
System.out.flush();
err("------------------");
err(time());
System.exit(0);
}
private static long t;
private static long millis () { return System.currentTimeMillis(); }
private static String time () { return "Time: " + (millis() - t) / 1000.0; }
private static void run (int N) {
try { DEBUG = System.getProperties().containsKey("DEBUG"); }
catch (Throwable t) {}
for (int n = 1; n <= N; ++n) {
Object res = new B().solve();
if (res != null)
write("Case #" + n + ": " + build(res));
}
exit();
}
}
////////////////////////////////////////////////////////////////////////////////////
public static void main (String[] args) {
init();
int N = ONE_TEST_CASE ? 1 : sc.nextInt();
IOUtils.run(N);
}
}
| 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 | 63998021894c04cb1b7e7301c8366c45 | train_000.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.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter wr = new PrintWriter(System.out);
long n=sc.nextInt();
long k=sc.nextInt();
long m=sc.nextInt();
ArrayList<Integer>arr=new ArrayList<>();
double sum=0;
for(int i=0;i<n;i++) {
int a=sc.nextInt();
sum+=a;
arr.add(a);
}
double ans=(sum+(Math.min(m, k*n)))/n;
Collections.sort(arr);
for(int i=0;i<Math.min(n-1, m);i++) {
sum-=arr.get(i);
//System.out.println(sum+" "+i+" "+(n-i-1)+" "+k*(n-i-1)+" "+(m-i-1));
ans=Math.max(ans, (sum+Math.min(k*(n-i-1),m-i-1))/(n-i-1));
}
System.out.println(ans);
}
}
class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
| Java | ["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 | e6e9032dcb9481341133da1bb430fa88 | train_000.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.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int m=sc.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
long sum[]=new long[n];
sum[n-1]=arr[n-1];
for(int i=n-2;i>=0;i--){
sum[i]=arr[i]+sum[i+1];
}
double max=0;
long use=0;
for(int i=0;i<n;i++){
long lm=m-i;
if(lm<0)continue;
use=Math.min((long)k*(n-i),lm);
max = Math.max(max,(double)( (sum[i] + use )/(double)(n-i)));
}
System.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 | a76c418860da8121ad41c49f0ee63795 | train_000.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.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
Reader in = new Reader();
int n = in.nextInt();
long k = in.nextInt();
int m = in.nextInt();
int[] a = in.na(n);
Arrays.sort(a);
double sum = 0;
for(int i = 0; i<n; i++) sum+=a[i];
double ans =(sum+Double.min(k*n, m))/n;
int l = Integer.min(n-1, m);
for(int used = 1; used<=l; used++) {
sum-=a[used-1];
double pos = (sum+Double.min(k*(n-used), m-used))/(n-used);
ans = Double.max(ans, pos);
}
System.out.println(ans);
}
static class Reader {
static BufferedReader br;
static StringTokenizer st;
private int charIdx = 0;
private String s;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public char nextChar() {
if (s == null || charIdx >= s.length()) {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
s = st.nextToken();
charIdx = 0;
}
return s.charAt(charIdx++);
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] nS(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
public int nextInt() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Long.parseLong(st.nextToken());
}
public String next() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return st.nextToken();
}
public static void readLine() {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
}
}
} | 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 | fc281f472c63021266b8d781d06e4d2e | train_000.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 TaskB {
static class Solution {
int n, k, m;
int mx;
long sum;
double ans;
int a[] = new int[100001];
int c[] = new int[1000001];
void input() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
k = scanner.nextInt();
m = scanner.nextInt();
for (int i = 0; i < n; ++i) {
a[i] = scanner.nextInt();
mx = Math.max(mx, a[i]);
sum += a[i];
++c[a[i]];
}
}
void run() {
input();
Arrays.parallelSort(a, 0, n);
int d, h;
for (int i = 0; i < Math.min(n, m + 1); ++i) {
h = (m - i) / (n - i);
d = (m - i) % (n - i);
if (h < k)
ans = Math.max(ans, h + (double)(sum + d) / (n - i));
else
ans = Math.max(ans, (double)sum / (n - i) + k);
sum -= a[i];
}
System.out.println(ans + 1e-9);
}
}
public static void main(String[] args) {
new Solution().run();
}
}
| 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 | 51fb30ff5094255a9ffefc00ec54b376 | train_000.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 javax.lang.model.util.ElementScanner6;
import java.io.*;
public class Main{
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader inp = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(inp, out);
out.close();
}
static class Solver {
class Node implements Comparable<Node>{
int l;
int r;
Node(int l,int r)
{
this.l = l;
this.r =r;
}
public int compareTo(Node n)
{
return Long.compare(this.l, n.l);
}
}
public boolean done(int[] sp,int[] par)
{
int root;
root=findSet(sp[0],par);
for(int i=1;i<sp.length;i++)
{
if(root!=findSet(sp[i], par))
return false;
}
return true;
}
public int findSet(int i,int[] par)
{
int x =i;
boolean flag =false;
while(par[i]>=0)
{
flag = true;
i=par[i];
}
if(flag)
par[x]=i;
return i;
}
public void unionSet(int i,int j,int[] par)
{
int x = findSet(i, par);
int y = findSet(j, par);
if(x<y)
{
par[y]=x;
}
else
{
par[x]=y;
}
}
public long pow(long a, long b, long MOD) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2, MOD);
if (b % 2 == 0) {
return val * val % MOD;
} else {
return val * (val * a % MOD) % MOD;
}
}
public void minPrimeFactor(int n,int[] s)
{
boolean prime[] = new boolean[n+1];
Arrays.fill(prime, true);
s[1]=1;
s[2]=2;
for(int i=4;i<=n;i+=2)
{
prime[i]=false;
s[i]=2;
}
for(int i=3;i<=n;i+=2)
{
if(prime[i])
{
s[i]=i;
for(int j=2*i;j<=n;j+=i)
{
prime[j]=false;
s[j]=i;
}
}
}
}
// public void findAllPrime(int n,ArrayList<Node> al,int s[])
// {
// int curr = s[n];
// int cnt = 1;
// while(n>1)
// {
// n/=s[n];
// if(curr==s[n])
// {
// cnt++;
// continue;
// }
// Node n1 = new Node(curr,cnt);
// al.add(n1);
// curr=s[n];
// cnt=1;
// }
// }
public int binarySearch(int n,int k)
{
int left=1;
int right=100000000+5;
int ans=0;
while(left<=right)
{
int mid = (left+right)/2;
if(n/mid>=k)
{
left = mid+1;
ans=mid;
}
else
{
right=mid-1;
}
}
return ans;
}
public boolean checkPallindrom(String s)
{
char ch[] = s.toCharArray();
for(int i=0;i<s.length()/2;i++)
{
if(ch[i]!=ch[s.length()-1-i])
return false;
}
return true;
}
public void dfs_util(ArrayList<Integer>[] al,boolean vis[],int x,int cnt[],int sts[],int fts[],long sv[])
{
vis[x] = true;
long min=Long.MAX_VALUE;
sts[cnt[0]]=x+1;
for(int i=0;i<al[x].size();i++)
{
if(!vis[al[x].get(i)])
{
cnt[0]++;
dfs_util(al, vis, al[x].get(i),cnt,sts,fts,sv);
if(sv[al[x].get(i)]<min&&sv[al[x].get(i)]!=-1)
{
min=sv[al[x].get(i)];
}
}
}
if(sv[x]==-1)
{
if(min==Long.MAX_VALUE)
{
min=-1;
}
sv[x]=min;
}
return ;
}
public void dfs(ArrayList[] al,int sts[],int fts[],long sv[])
{
boolean vis[] = new boolean[al.length];
int cnt[]= new int[2*al.length+2];
for(int i=0;i<al.length;i++)
{
if(!vis[i])
{
dfs_util(al,vis,i,cnt,sts,fts,sv);
}
}
}
public void remove(ArrayList<Integer>[] al,int x)
{
for(int i=0;i<al.length;i++)
{
for(int j=0;j<al[i].size();j++)
{
if(al[i].get(j)==x)
al[i].remove(j);
}
}
}
public int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public void printDivisors(int n,ArrayList<Integer> al)
{
// Note that this loop runs till square root
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
{
al.add(i);
}
else // Otherwise print both
{
al.add(i);
al.add(n/i);
}
}
}
}
public static long constructSegment(long seg[],long arr[], int low,int high,int pos)
{
if(low==high)
{
seg[pos] = arr[low];
return seg[pos];
}
int mid = (low+high)/2;
long t1=constructSegment(seg, arr,low , mid,(2*pos)+1);
long t2=constructSegment(seg,arr,mid+1,high,(2*pos)+2);
seg[pos] = t1+t2;
return seg[pos];
}
public static long querySegment(long seg[],int low,int high,int qlow, int qhigh,int pos)
{
if(qlow<=low&&qhigh>=high)
{
return seg[pos];
}
else if(qlow>high||qhigh<low )
{
return 0;
}
else{
long ans=0;
int mid=(low+high)/2;
ans+=querySegment(seg,low , mid, qlow, qhigh, (2*pos)+1);
ans+=querySegment(seg, mid+1, high, qlow, qhigh, (2*pos)+2);
return ans;
}
}
public static int lcs( char[] X, char[] Y, int m, int n )
{
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return Integer.max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}
private void solve(InputReader inp, PrintWriter out1) {
int n = inp.nextInt();
int k =inp.nextInt();
int m = inp.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++)
{
arr[i] = inp.nextLong();
}
Arrays.sort(arr);
long sum[] = new long[n];
sum[0]=arr[0];
for(int i=1;i<n;i++)
{
sum[i]=sum[i-1]+arr[i];
}
double ans=((double)sum[n-1]+ (double)Long.min((long)m,(long)k*n))/(double)(n);
for(int i=1;i<=m;i++)
{
if(i>=n)
{
break;
}
double temp = ((double)sum[n-1]-sum[i-1]+ (double)Long.min((long)m-i,(long)k*(n-i)))/(double)(n-i);
ans = Double.max(ans,temp);
}
out1.println(ans);
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
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());
}
}
}
class ele{
long value;
long i;
boolean flag;
public ele(long value,long i)
{
this.value = value;
this.i=i;
this.flag = false;
}
} | 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 | c76b8ed85a223f224317a23028499e9a | train_000.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;
import static java.lang.Double.isNaN;
public class Main {
public static double fun(int n, int m, int k, double sum, int[] v){
double max = 0;
if( n >= (m/k)){
max = (sum + k*(int)(m/k))/n;
// System.out.println(max + " /111");
if( n >= (m/k+1)) {
max = (sum + m) / n;
}
} else {
max = sum;
int yy = 0;
if((m-n*k > 0) && (m-n*k >=n)) {
for (int i = m - n * k; i < n; i++) {
// System.out.println("***");
max -= v[i];
yy++;
}
max += ((n - yy) * k);
max /= (n - yy);
// System.out.println(max + " //222");
}
else{
max = 0;
}
}
return max;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int m = in.nextInt();
int[] v = new int[n];
double sum = 0;
for(int i = 0; i < n; i++) {
v[i] = in.nextInt();
sum += v[i];
}
double max = 0;
Arrays.sort(v);
max = fun(n, m, k, sum, v);
int m_2 = m;
int n_2 = n;
double may = sum;
int i = 0;
while(m_2 !=0 && n_2 != 1){
may = may - v[i];
m_2 = m_2 - 1;
n_2 = n_2 - 1;
++i;
double res = fun(n_2, m_2, k, may, v);
if(!isNaN(res)) {
max = Math.max(max, res);
}
}
System.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 | cd011a1c2ed9c8a1c4bf1fee7a98ec92 | train_000.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 CodeForces_practice;
import java.util.Arrays;
import java.util.Scanner;
public class B1111 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
long t=s.nextInt();
long k=s.nextInt();
long m=s.nextInt();
long [] arr=new long [(int)t];
double sum=0;
for(int i=0;i<t;i++)
{
arr[i]=s.nextLong();
sum+=arr[i];
}
Arrays.sort(arr);
double ans = 0;
for(int i=0;i<t&&i<=m;i++)
{
double x = (sum + Math.min((t-i)*k, m-i))/(double)(t-i);
ans=Math.max(ans, x);
sum-=arr[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 | e4809f8f6d26bbbad26b341c142c91af | train_000.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.Arrays;
import java.util.StringTokenizer;
public class Main {
static final int UNCALC = -1;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
int m = sc.nextInt();
Integer[] a = sc.nextIntegerArray(n);
Arrays.sort(a);
long sum = 0;
for (int x : a)
sum += x;
double max = 1.0 * (sum + Math.min(1l * n * k, m)) / n;
if (m >= n - 1) {
max = Math.max(max, (a[n - 1] + Math.min(m - (n - 1), k)));
}
for (int rem = 1; rem <= m && rem < n; rem++) {
int left = n - rem;
sum -= a[rem - 1];
long add = Math.min(1l * left * k, m - rem);
max = Math.max(max, 1.0 * (add + sum) / left);
}
out.println(max);
out.flush();
out.close();
}
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);
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] ans = new double[n];
for (int i = 0; i < n; i++)
ans[i] = nextDouble();
return 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 | 6850b4a396c491cd8fb9b43c9fbf198e | train_000.jsonl | 1353079800 | One day Petya got a set of wooden cubes as a present from his mom. Petya immediately built a whole city from these cubes.The base of the city is an n × n square, divided into unit squares. The square's sides are parallel to the coordinate axes, the square's opposite corners have coordinates (0, 0) and (n, n). On each of the unit squares Petya built a tower of wooden cubes. The side of a wooden cube also has a unit length.After that Petya went an infinitely large distance away from his masterpiece and looked at it in the direction of vector v = (vx, vy, 0). Petya wonders, how many distinct cubes are visible from this position. Help him, find this number.Each cube includes the border. We think that a cube is visible if there is a ray emanating from some point p, belonging to the cube, in the direction of vector - v, that doesn't contain any points, belonging to other cubes. | 256 megabytes | import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Egor Kulikov ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int count = in.readInt();
int vx = in.readInt();
int vy = in.readInt();
int[][] height = IOUtils.readIntTable(in, count, count);
final int[][] distance = new int[count][count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++)
distance[i][j] = Math.min(Math.min(vx * i + vy * j, vx * i + vy * (j + 1)), Math.min(vx * (i + 1) + vy * j, vx * (i + 1) + vy * (j + 1)));
}
int[] positions = new int[(count + 1) * (count + 1)];
for (int i = 0; i <= count; i++) {
for (int j = 0; j <= count; j++)
positions[i * (count + 1) + j] = vy * i - vx * j;
}
ArrayUtils.sort(positions, IntComparator.DEFAULT);
positions = ArrayUtils.unique(positions);
IntervalTree tree = new IntervalTree(positions.length - 1);
int[] order = new int[count * count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++)
order[i * count + j] = (i << 10) + j;
}
ArrayUtils.sort(order, new IntComparator() {
public int compare(int first, int second) {
return distance[first >> 10][first & 1023] - distance[second >> 10][second & 1023];
}
});
long answer = 0;
for (int i : order) {
int x = i >> 10;
int y = i & 1023;
int from = Math.min(Math.min(vy * x - vx * y, vy * x - vx * (y + 1)), Math.min(vy * (x + 1) - vx * y, vy * (x + 1) - vx * (y + 1)));
int to = Math.max(Math.max(vy * x - vx * y, vy * x - vx * (y + 1)), Math.max(vy * (x + 1) - vx * y, vy * (x + 1) - vx * (y + 1)));
from = Arrays.binarySearch(positions, from);
to = Arrays.binarySearch(positions, to);
long min = tree.query(from, to - 1);
answer += Math.max(0, height[x][y] - min);
tree.update(from, to - 1, height[x][y]);
}
out.printLine(answer);
}
static class IntervalTree {
protected int size;
protected int[] value;
protected int[] delta;
protected IntervalTree(int size) {
this.size = size;
int nodeCount = Math.max(1, Integer.highestOneBit(size) << 2);
value = new int[nodeCount];
delta = new int[nodeCount];
}
public void update(int from, int to, int delta) {
update(0, 0, size - 1, from, to, delta);
}
private void update(int root, int left, int right, int from, int to, int delta) {
if (left > to || right < from)
return;
if (left >= from && right <= to) {
this.delta[root] = Math.max(this.delta[root], delta);
value[root] = Math.max(value[root], delta);
return;
}
this.delta[2 * root + 1] = Math.max(this.delta[2 * root + 1], this.delta[root]);
this.delta[2 * root + 2] = Math.max(this.delta[2 * root + 2], this.delta[root]);
int middle = (left + right) >> 1;
value[2 * root + 1] = Math.max(value[2 * root + 1], this.delta[root]);
value[2 * root + 2] = Math.max(value[2 * root + 2], this.delta[root]);
this.delta[root] = 0;
update(2 * root + 1, left, middle, from, to, delta);
update(2 * root + 2, middle + 1, right, from, to, delta);
value[root] = Math.min(value[2 * root + 1], value[2 * root + 2]);
}
public int query(int from, int to) {
return query(0, 0, size - 1, from, to);
}
private int query(int root, int left, int right, int from, int to) {
if (left > to || right < from)
return Integer.MAX_VALUE;
if (left >= from && right <= to)
return value[root];
delta[2 * root + 1] = Math.max(delta[2 * root + 1], delta[root]);
delta[2 * root + 2] = Math.max(delta[2 * root + 2], delta[root]);
int middle = (left + right) >> 1;
value[2 * root + 1] = Math.max(value[2 * root + 1], delta[root]);
value[2 * root + 2] = Math.max(value[2 * root + 2], delta[root]);
this.delta[root] = 0;
return Math.min(query(2 * root + 1, left, middle, from, to), query(2 * root + 2, middle + 1, right, from, to));
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
public static int[][] readIntTable(InputReader in, int rowCount, int columnCount) {
int[][] table = new int[rowCount][];
for (int i = 0; i < rowCount; i++)
table[i] = readIntArray(in, columnCount);
return table;
}
}
class ArrayUtils {
private static int[] tempInt = new int[0];
public static int[] sort(int[] array, IntComparator comparator) {
return sort(array, 0, array.length, comparator);
}
public static int[] sort(int[] array, int from, int to, IntComparator comparator) {
ensureCapacityInt(to - from);
System.arraycopy(array, from, tempInt, 0, to - from);
sortImpl(array, from, to, tempInt, 0, to - from, comparator);
return array;
}
private static void ensureCapacityInt(int size) {
if (tempInt.length >= size)
return;
size = Math.max(size, tempInt.length << 1);
tempInt = new int[size];
}
private static void sortImpl(int[] array, int from, int to, int[] temp, int fromTemp, int toTemp, IntComparator comparator) {
if (to - from <= 1)
return;
int middle = (to - from) >> 1;
int tempMiddle = fromTemp + middle;
sortImpl(temp, fromTemp, tempMiddle, array, from, from + middle, comparator);
sortImpl(temp, tempMiddle, toTemp, array, from + middle, to, comparator);
int index = from;
int index1 = fromTemp;
int index2 = tempMiddle;
while (index1 < tempMiddle && index2 < toTemp) {
if (comparator.compare(temp[index1], temp[index2]) <= 0)
array[index++] = temp[index1++];
else
array[index++] = temp[index2++];
}
if (index1 != tempMiddle)
System.arraycopy(temp, index1, array, index, tempMiddle - index1);
if (index2 != toTemp)
System.arraycopy(temp, index2, array, index, toTemp - index2);
}
public static int[] unique(int[] array) {
return unique(array, 0, array.length);
}
public static int[] unique(int[] array, int from, int to) {
if (from == to)
return new int[0];
int count = 1;
for (int i = from + 1; i < to; i++) {
if (array[i] != array[i - 1])
count++;
}
int[] result = new int[count];
result[0] = array[from];
int index = 1;
for (int i = from + 1; i < to; i++) {
if (array[i] != array[i - 1])
result[index++] = array[i];
}
return result;
}
}
interface IntComparator {
public static final IntComparator DEFAULT = new IntComparator() {
public int compare(int first, int second) {
if (first < second)
return -1;
if (first > second)
return 1;
return 0;
}
};
public int compare(int first, int second);
}
| Java | ["5 -1 2\n5 0 0 0 1\n0 0 0 0 2\n0 0 0 1 2\n0 0 0 0 2\n2 2 2 2 3", "5 1 -2\n5 0 0 0 1\n0 0 0 0 2\n0 0 0 1 2\n0 0 0 0 2\n2 2 2 2 3"] | 5 seconds | ["20", "15"] | null | Java 6 | standard input | [
"dp",
"two pointers",
"geometry",
"data structures"
] | de37d373a7b93b9380317ef3813761f6 | The first line contains three integers n, vx and vy (1 ≤ n ≤ 103, |vx|, |vy| ≤ |104|, |vx| + |vy| > 0). Next n lines contain n integers each: the j-th integer in the i-th line aij (0 ≤ aij ≤ 109, 1 ≤ i, j ≤ n) represents the height of the cube tower that stands on the unit square with opposite corners at points (i - 1, j - 1) and (i, j). | 2,700 | Print a single integer — the number of visible cubes. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 4c42de655a93ee6e526e32fedf6bd105 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes |
// Problem : D. Ehab and another another xor problem
// Contest : Codeforces Round #525 (Div. 2)
// URL : https://codeforces.com/contest/1088/problem/D
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
import java.io.*;
import java.util.*;
public class a implements Runnable{
public static void main(String[] args) {
new Thread(null, new a(), "process", 1<<26).start();
}
public void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out),true);
//PrintWriter out = new PrintWriter("file.out");
Task solver = new Task();
//int t = scan.nextInt();
int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
static final int inf = Integer.MAX_VALUE;
public void solve(int testNumber, FastReader sc, PrintWriter pw) {
//CHECK FOR QUICKSORT TLE
//***********************//
//CHECK FOR INT OVERFLOW
//***********************//
int c = 0;
int d = 0;
pw.println("? 0 0");
int smt = sc.nextInt();
for(int i = 29; i >= 0; i--){
int test1 = c << (i + 1);
int test2 = d << (i + 1);
pw.println("? " + (test1 + (1 << i)) + " " + test2);
int ret = sc.nextInt();
pw.println("? " + test1 + " " + (test2 + (1 << i)));
int ret2 = sc.nextInt();
if(ret == ret2){
c <<= 1;
if(smt > 0){
c++;
}
d <<= 1;
if(smt < 0){
d++;
}
smt = ret;
}
else if(ret == -1 || ret2 == 1){
c <<= 1;
c++;
d <<= 1;
d++;
}
else{
c <<= 1;
d <<= 1;
}
//pw.println(i);
}
pw.println("! "+ c + " " + d);
}
}
static long binpow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
if ((b & 1) == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
static void sort(int[] x){
shuffle(x);
Arrays.sort(x);
}
static void sort(long[] x){
shuffle(x);
Arrays.sort(x);
}
static class tup implements Comparable<tup>{
int a, b;
tup(int a,int b){
this.a=a;
this.b=b;
}
@Override
public int compareTo(tup o){
return Integer.compare(o.b,b);
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
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 | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 41d7dbec0f1fc082758a00bb23142267 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.util.*;
public class ContestD {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int c=0,d=0;
int x = 29;
boolean f = true;
int r1=0,r2=0;
while(x>=0) {
if(f) {
System.out.println("? "+c+" "+d);
System.out.flush();
r1 = in.nextInt();
}
c ^= 1<<x;
d ^= 1<<x;
System.out.println("? "+c+" "+d);
System.out.flush();
r2 = in.nextInt();
f = true;
if(r1>r2) {
d ^= 1<<x;
}else if(r1<r2) {
c ^= 1<<x;
}else {
d ^= 1<<x;
System.out.println("? "+c+" "+d);
System.out.flush();
int r3 = in.nextInt();
if(r3<0) {
d^=1<<x;
}else {
c^=1<<x;
}
f = false;
}
x--;
}
in.close();
System.out.println("! "+c+" "+d);
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 1564f47fd84b0e3d3ef1e0d8e00e3bc7 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = 0;
int b = 0;
System.out.println("? 0 0");
System.out.flush();
int flag = sc.nextInt();
for (int i = 29; i >= 0; i--) {
System.out.println(String.format("? %d %d", 1 << i ^ a, 1 << i ^ b));
System.out.flush();
int temp = sc.nextInt();
if (flag != 0 && flag + temp == 0) {
if (flag > 0) {
a |= 1 << i;
} else {
b |= 1 << i;
}
System.out.println(String.format("? %d %d", a, b));
System.out.flush();
flag = sc.nextInt();
}
else {
System.out.println(String.format("? %d %d", 1 << i ^ a, b));
System.out.flush();
temp = sc.nextInt();
if (temp < 0) {
a |= 1 << i;
b |= 1 << i;
} else {
}
}
}
System.out.println(String.format("! %d %d", a, b));
System.out.flush();
sc.close();
}
} | Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | e30eb74d77c1a9831578422b7428c590 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import javax.swing.plaf.synth.SynthSpinnerUI;
import org.omg.Messaging.SyncScopeHelper;
public class A {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
long a=0;
long b=0;
boolean A; //true if a is bigger
System.out.println("? 0 0");
A=sc.nextInt()==1;
for(int i=29;i>=0;i--) {
System.out.println("? "+(a|(1<<i))+" "+b);
int f=sc.nextInt();
System.out.println("? "+a+" "+(b|(1<<i)));
int s=sc.nextInt();
if(f!=s) {
if(f==-1) {
a|=1<<i;
b|=1<<i;
}
}else {
if(A) {
a|=1<<i;
}else {
b|=1<<i;
}
A=f==1;
}
}
System.out.println("! "+a+" "+b);
}
}
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 int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public String nextLine() throws IOException{
return br.readLine();
}
public Double nextDouble() throws IOException{
return Double.parseDouble(next());
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | dbeed0e54ee38fd022cb01813e98b765 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.util.*;
public class EhabandXor{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int c = 0;
int d = 0;
int x = 29;
boolean f = true;
int r1 = 0;
int r2 = 0;
int r3 = 0;
while(x >= 0) {
if(f) {
System.out.println("? "+c+" "+d);
System.out.flush();
r1 = in.nextInt();
}
c ^= 1 << x;
d ^= 1 << x;
System.out.println("? "+c+" "+d);
System.out.flush();
r2 = in.nextInt();
f = true;
if(r1 > r2) {
d ^= 1<<x;
}else if(r1 < r2) {
c ^= 1<<x;
}else {
d ^= 1<<x;
System.out.println("? "+c+" "+d);
System.out.flush();
r3 = in.nextInt();
if(r3 < 0) {
d ^= 1 << x;
}else {
c ^= 1 << x;
}
f = false;
}
x--;
}
in.close();
System.out.println("! "+c+" "+d);
}
} | Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | c62d00429b47a0e780fbbda3a182a585 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Task{
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.flush();out.close();
}
static class TaskE {
final int MAX=29;
int query(int c,int d,InputReader in){
System.out.println("? "+c+" "+d);
return in.nextInt();
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int c=0,d=0;
int t=query(c,d,in);
for(int i=MAX;i>=0;i--){
int p2=(1<<i);
int c1=c^p2,d1=d^p2;
if(t==1){
int val=query(c1,d1,in);
if(val==-1){
c=c1;
t=query(c,d,in);
}else{
val=query(c1,d,in);
if(val==-1){
c=c1;d=d1;
}
}
}else if(t==-1){
int val=query(c1,d1,in);
if(val==1){
d=d1;
t=query(c,d,in);
}else{
val=query(c,d1,in);
if(val==1){
c=c1;d=d1;
}
}
}else{
int val=query(c1,d,in);
if(val==-1){
c=c1;d=d1;
}
}
}
System.out.println("! "+c+" "+d);
}
// pair ja[][];long w[];int from[],to[],c[];
// void make(int n,int m,InputReader in){
// ja=new pair[n+1][];w=new long[m];from=new int[m];to=new int[m];c=new int[n+1];
// for(int i=0;i<m;i++){
// int u=in.nextInt(),v=in.nextInt();long wt=in.nextLong();
// c[u]++;c[v]++;from[i]=u;to[i]=v;w[i]=wt;
// }
// for(int i=1;i<=n;i++){
// ja[i]=new pair[c[i]];c[i]=0;
// }
// for(int i=0;i<m;i++){
// ja[from[i]][c[from[i]]++]=new pair(to[i],w[i]);
// ja[to[i]][c[to[i]]++]=new pair(from[i],w[i]);
// }
// }
// int[] radixSort(int[] f){ return radixSort(f, f.length); }
// int[] radixSort(int[] f, int n)
// {
// int[] to = new int[n];
// {
// int[] b = new int[65537];
// for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++;
// for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
// for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i];
// int[] d = f; f = to;to = d;
// }
// {
// int[] b = new int[65537];
// for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++;
// for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
// for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i];
// int[] d = f; f = to;to = d;
// }
// return f;
// }
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 4cb20e69b8692941d3eebd8bba8d6359 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;
public class EhabAnotherXOR {
public static void main(String[] args) {
FastScanner scanner = new FastScanner();
int curA = 0;
int curB = 0;
System.out.println("? 0 0");
System.out.flush();
int greater = scanner.nextInt();
for(int i = 29; i >= 0; i--) {
int curLoc = 1<<i;
int outB = curLoc ^ curB;
int outA = curLoc ^ curA;
System.out.println("? " + outA + " " + curB);
System.out.flush();
int res1 = scanner.nextInt();
if (res1 == -2) break;
System.out.println("? " + curA + " " + outB);
System.out.flush();
int res2 = scanner.nextInt();
if (res1 == res2) {
if (greater == 1) curA ^= curLoc;
else {
curB ^= curLoc;
}
greater = res1;
}
else if (res1 == -1) {
curA ^= curLoc;
curB ^= curLoc;
}
}
System.out.println("! " + curA + " " + curB);
System.out.flush();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader in) {
br = new BufferedReader(in);
}
public FastScanner() {
this(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 readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | b75f62723a94c5bf3f15e1904c38ad31 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int g = 0;
int[] cur = new int[2];
if (query(0, 0, out, in) >= 0) {
g = 0;
} else {
g = 1;
}
for (int i = 29; i >= 0; i--) {
int[] q = new int[]{query(cur[0] + (1 << i), cur[1], out, in), query(cur[0], cur[1] + (1 << i), out, in)};
if (q[0] != q[1]) {
if (q[1] == 1) {
cur[0] += (1 << i);
cur[1] += (1 << i);
}
} else {
cur[g] += (1 << i);
if (q[0] == 1) {
g = 0;
} else {
g = 1;
}
}
}
out.println("! " + cur[0] + " " + cur[1]);
}
int query(int c, int d, PrintWriter out, InputReader in) {
out.println("? " + c + " " + d);
out.flush();
return (in.nextInt());
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 9974d3344624911d3756914a2c7dd5e7 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class EhabAndAnotherAnotherXorProblem2 {
void solve() {
int a = 0, b = 0;
int res = ask(0, 0);
for (int i = 29; i >= 0; i--) {
int x = ask(a | 1 << i, b);
int y = ask(a, b | 1 << i);
if (x == y) {
if (res > 0) {
a |= 1 << i;
} else {
b |= 1 << i;
}
res = x;
} else {
if (x < 0) {
a |= 1 << i;
b |= 1 << i;
}
}
}
out.printf("! %d %d%n", a, b);
}
int ask(int c, int d) {
out.printf("? %d %d%n", c, d);
out.flush();
return in.nextInt();
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new EhabAndAnotherAnotherXorProblem2().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 41b7869641b044f1a0556b9dbb7f27cf | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class EhabAndAnotherAnotherXorProblem {
void solve() {
int a = 0, b = 0;
boolean[] vis = new boolean[30];
for (int i = 29; i >= 0; ) {
int exp = ask(a, b);
if (exp == 0) break;
int c = a, d = b;
int j = i;
while (j >= 0) {
c |= 1 << j;
d |= 1 << j;
int cur = ask(c, d);
if (cur == exp) {
j--;
} else {
break;
}
}
if (exp > 0) {
a |= 1 << j;
} else {
b |= 1 << j;
}
vis[j] = true;
i = j - 1;
}
for (int i = 29; i >= 0; i--) {
if (!vis[i]) {
if (ask(a | 1 << i, b) > 0) continue;
a |= 1 << i;
b |= 1 << i;
}
}
printAns(a, b);
}
int ask(int c, int d) {
out.printf("? %d %d%n", c, d);
out.flush();
return in.nextInt();
}
void printAns(int a, int b) {
out.printf("! %d %d%n", a, b);
out.flush();
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new EhabAndAnotherAnotherXorProblem().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 781697b970be5ae03f63f1a39dfbb304 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author llamaoo7
*/
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);
DEhabAndAnotherAnotherXorProblem solver = new DEhabAndAnotherAnotherXorProblem();
solver.solve(1, in, out);
out.close();
}
static class DEhabAndAnotherAnotherXorProblem {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int a = 0;
int b = 0;
int ra;
int rb;
int cur;
out.println("? 0 0");
out.flush();
boolean prev = in.nextInt() == 1;
int n = 30;
for (int i = 0; i < n; i++) {
cur = (1 << (n - i - 1));
out.println("? " + (a ^ cur) + " " + b);
out.flush();
ra = in.nextInt();
if (ra == -2) return;
out.println("? " + a + " " + (b ^ cur));
out.flush();
rb = in.nextInt();
if (rb == -2) return;
if (ra == rb) {
if (prev) a |= cur;
else b |= cur;
prev = ra == 1;
} else {
if (rb == 1) {
a |= cur;
b |= cur;
}
}
}
out.println("! " + a + " " + b);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 12c52f3911ce3c4b8961957ef62aa23f | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
private InputReader in = new InputReader(System.in);
public static void main(String[] args) {
Solution solution = new Solution();
solution.solve();
}
static class InputReader {
private BufferedReader reader;
private 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());
}
}
private boolean debug = false;
private void solve() {
int a = 0, b = 0;
int result = ask(0, 0);
boolean greater = result > 0;
int left = 0, right = 0;
List<Integer> indexList = new ArrayList<>();
for (int i = 29; i >= 0; --i) {
int r = ask(left | (1 << i), right | (1 << i));
if (r == 0) {
for (int j = 0; j <= i; ++j) indexList.add(j);
break;
}
if (r < 0 && greater) {
a |= 1 << i;
left |= 1 << i;
greater = ask(left, right) > 0;
}
else if (r > 0 && !greater) {
b |= 1 << i;
right |= 1 << i;
greater = ask(left, right) > 0;
}
else indexList.add(i);
if (debug) System.out.println("a = " + a + " - b = " + b);
}
int x = equal(30, left, right, indexList);
a = a | x;
b = b | x;
if (debug) System.out.println("x = " + x);
System.out.println("! " + a + " " + b);
}
private int equal(int n, int left, int right, List<Integer> indexList) { // n: numbers left
if (debug) System.out.println("Starting equal " + n);
int a = 0;
for (int i : indexList) {
int result = ask(left | (1 << i), right);
if (result < 0) a |= (1 << i);
}
return a;
}
private int count = 0;
private int ask(int c, int d) {
System.out.println("? " + c + " " + d);
System.out.flush();
if (debug) {
System.out.println("Times: " + (++count));
int a = 15, b = 10;
int k = Integer.compare(a ^ c, b ^ d);
System.out.println(k);
return k;
}
else return in.nextInt();
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 325ccaee38d92dc9964a670b74715e97 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
private InputReader in = new InputReader(System.in);
public static void main(String[] args) {
Solution solution = new Solution();
solution.solve();
}
static class InputReader {
private BufferedReader reader;
private 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());
}
}
private boolean debug = false;
private void solve() {
int a = 0, b = 0;
int result = ask(0, 0);
boolean greater = result > 0;
int left = 0, right = 0;
List<Integer> indexList = new ArrayList<>();
for (int i = 29; i >= 0; --i) {
int r = ask(left | (1 << i), right | (1 << i));
if (r == 0) {
for (int j = 0; j <= i; ++j) indexList.add(j);
break;
}
if (r < 0 && greater) {
a |= 1 << i;
left |= 1 << i;
greater = ask(left, right) > 0;
}
else if (r > 0 && !greater) {
b |= 1 << i;
right |= 1 << i;
greater = ask(left, right) > 0;
}
else indexList.add(i);
}
int x = equal(30, left, right, indexList);
a = a | x;
b = b | x;
System.out.println("! " + a + " " + b);
}
private int equal(int n, int left, int right, List<Integer> indexList) { // n: numbers left
int a = 0;
for (int i : indexList) {
int result = ask(left | (1 << i), right);
if (result < 0) a |= (1 << i);
}
return a;
}
private int ask(int c, int d) {
System.out.println("? " + c + " " + d);
System.out.flush();
return in.nextInt();
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 75b562d06ce533bbfc4e4aa6880619e1 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import static java.lang.System.exit;
public class Solve implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
try {
// in = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter("output.txt");
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} catch (Exception e) {
String filename = "";
if (!filename.isEmpty()) {
in = new BufferedReader(new FileReader(filename + ".in"));
out = new PrintWriter(filename + ".out");
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = readInt();
}
return res;
}
long[] readLongArray(int size) throws IOException {
long[] res = new long[size];
for (int i = 0; i < size; i++) {
res[i] = readInt();
}
return res;
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
<T> List<T>[] createGraphList(int size) {
List<T>[] list = new List[size];
for (int i = 0; i < size; i++) {
list[i] = new ArrayList<>();
}
return list;
}
public static void main(String[] args) {
new Solve().run();
// new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
}
long timeBegin, timeEnd;
void time() {
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
long memoryTotal, memoryFree;
void memory() {
memoryFree = Runtime.getRuntime().freeMemory();
System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
+ " KB");
}
public void run() {
try {
timeBegin = System.currentTimeMillis();
memoryTotal = Runtime.getRuntime().freeMemory();
init();
solve();
out.close();
if (System.getProperty("ONLINE_JUDGE") == null) {
time();
memory();
}
} catch (Exception e) {
e.printStackTrace();
exit(-1);
}
}
int query(int a, int b) throws IOException {
out.println("? " + a + " " + b);
out.flush();
return readInt();
}
private void solve() throws IOException {
int bigger = query(0, 0);
int a = 0;
int b = 0;
for (int i = 29; i >= 0; i--) {
int f = query(a | (1 << i), b);
int s = query(a, b | (1 << i));
if (f == 1 && s == -1) {
continue;
}
if (f == -1 && s == 1) {
a |= (1 << i);
b |= (1 << i);
continue;
}
if (f == s) {
if (bigger == 1) {
a |= (1 << i);
} else {
b |= (1 << i);
}
bigger = f;
}
}
out.println("! " + a + " " + b);
}
} | Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 36184e357190b2ff1eecf6d030556333 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Vadim
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
D solver = new D();
solver.solve(1, in, out);
out.close();
}
static class D {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int ap = 0, bp = 0;
int a = 0, b = 0;
out.println("? " + ap + " " + bp);
out.flush();
int r = in.ni();
for (int i = 29; i >= 0; i--) {
int ab = 0, bb = 0;
if (r != 0) { //a <> b
out.println("? " + (ap | (1 << i)) + " " + (bp | (1 << i)));
out.flush();
int ans = in.ni();
if (ans != r) {//1 0
ab = (r == 1) ? 1 : 0;
bb = (r == 1) ? 0 : 1;
ap |= (ab << i);
bp |= (bb << i);
out.println("? " + ap + " " + bp);
out.flush();
r = in.ni();
} else {
out.println("? " + (ap | (1 << i)) + " " + bp);
out.flush();
ans = in.ni();
if (ans == -1) { // 1 1
ap |= (1 << i);
bp |= (1 << i);
}
}
} else {
out.println("? " + (ap | (1 << i)) + " " + bp);
out.flush();
int ans = in.ni();
if (ans == -1) { // 1 1
ap |= (1 << i);
bp |= (1 << i);
}
}
}
out.println("! " + ap + " " + bp);
out.flush();
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String ns() {
while (st == null || !st.hasMoreTokens()) {
try {
String rl = in.readLine();
if (rl == null) {
return null;
}
st = new StringTokenizer(rl);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(ns());
}
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | ded0e595bacfbb88164ac8853af504c9 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 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;
public class _1088D {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(in, out);
out.close();
}
private static class Solver {
private void solve(InputReader in, PrintWriter out) {
int a = 0;
int b = 0;
System.out.println(" ? 0 0");
System.out.flush();
int nextBigger = in.nextInt();
for (int i = 29; i >= 0; i--) {
System.out.println(" ? " + (a + (1 << i)) + " " + b);
System.out.flush();
int tmp1 = in.nextInt();
System.out.println(" ? " + a + " " + (b + (1 << i)));
System.out.flush();
int tmp2 = in.nextInt();
if (tmp1 != tmp2) {
if (tmp1 == -1) {
a += (1 << i);
b += (1 << i);
}
} else {
if (nextBigger == 1) {
a += (1 << i);
} else {
b += (1 << i);
}
nextBigger = tmp1;
}
}
System.out.println("! " + a + " " + b);
}
}
private static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
private InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private int nextInt() {
return Integer.parseInt(next());
}
private long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 47f27ad7006dedba8d58e0a7cd3cbd28 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 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;
public class _1088D {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(in, out);
out.close();
}
private static class Solver {
private void solve(InputReader in, PrintWriter out) {
int a = 0;
int b = 0;
out.println(" ? 0 0");
out.flush();
int nextBigger = in.nextInt();
for (int i = 29; i >= 0; i--) {
out.println(" ? " + (a + (1 << i)) + " " + b);
out.flush();
int tmp1 = in.nextInt();
out.println(" ? " + a + " " + (b + (1 << i)));
out.flush();
int tmp2 = in.nextInt();
if (tmp1 != tmp2) {
if (tmp1 == -1) {
a += (1 << i);
b += (1 << i);
}
} else {
if (nextBigger == 1) {
a += (1 << i);
} else {
b += (1 << i);
}
nextBigger = tmp1;
}
}
out.println("! " + a + " " + b);
}
}
private static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
private InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private int nextInt() {
return Integer.parseInt(next());
}
private long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 2216ca2358b2ba78214339590f063eb0 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes |
import java.util.Scanner;
public class D {
public static void main(String[] aaaa) {
Scanner scanner = new Scanner(System.in);
System.out.println("? 0 0");
System.out.flush();
int res = scanner.nextInt();
// System.out.println("res " + res);
int a = 0;
int b = 0;
for (int i = 29; i >= 0; i--) {
int c = 0;
int d = (a ^ b) + (1 << i);
System.out.println("? " + c + " " + d);
System.out.flush();
int res1 = scanner.nextInt();
//int res2 = scanner.nextInt();
c = (a ^ b) + (1 << i);
d = 0;
System.out.println("? " + c + " " + d);
System.out.flush();
int res2 = scanner.nextInt();
//int res4 = scanner.nextInt();
int newres = 0;
if (res >= 0 && res1 >=0 && res2 >= 0) {
a += (1 << i);
//b += (1 << i);
newres = 1;
}
if (res >= 0 && res1 >=0 && res2 < 0) {
a += (1 << i);
b += (1 << i);
newres = 1;
}
if (res >= 0 && res1 < 0 && res2 >= 0) {
//a += (1 << i);
//b += (1 << i);
newres = 1;
}
if (res >= 0 && res1 < 0 && res2 < 0) {
a += (1 << i);
//b += (1 << i);
newres = -1;
}
if (res < 0 && res1 >=0 && res2 >= 0) {
//a += (1 << i);
b += (1 << i);
newres = 1;
}
if (res < 0 && res1 >=0 && res2 < 0) {
a += (1 << i);
b += (1 << i);
newres = -1;
}
if (res < 0 && res1 < 0 && res2 >= 0) {
//a += (1 << i);
//b += (1 << i);
newres = -1;
}
if (res < 0 && res1 < 0 && res2 < 0) {
//a += (1 << i);
b += (1 << i);
newres = -1;
}
// System.out.println("res " + res + " a " + a + " b " + b);
res = newres;
//System.out.println("? 0 0");
}
System.out.println("! " + a + " " + b);
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 8afcf09b2ea544475214d874798fc7b1 | train_000.jsonl | 1543934100 | This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \oplus c>b \oplus d$$$. 0 if $$$a \oplus c=b \oplus d$$$. -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Abhas Jain
*/
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);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long a = 0, b = 0;
long g = 0;
out.println("? " + a + " " + b);
out.flush();
g = in.nl();
long curr;
for (long pos = 29; pos >= 0; --pos) {
out.println("? " + (a + (1L << pos)) + " " + (b + (1L << pos)));
out.flush();
curr = in.nl();
if (curr == g) {
out.println("? " + (a + (1 << pos)) + " " + b);
out.flush();
long c = in.nl();
if (c == -1L) {
a += (1L << pos);
b += (1L << pos);
}
} else {
if (g == 1L && curr == -1L) {
a += (1 << pos);
} else b += (1 << pos);
out.println("? " + a + " " + b);
out.flush();
g = in.nl();
}
}
out.println("! " + a + " " + b);
out.flush();
}
}
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 long nl() {
return Long.parseLong(next());
}
}
}
| Java | ["1\n-1\n0"] | 1 second | ["? 2 1\n? 1 2\n? 2 0\n! 3 1"] | NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer. | Java 8 | standard input | [
"constructive algorithms",
"implementation",
"bitmasks",
"interactive"
] | 7dc1137dd1f0c645cc7ec6dfdb92f5df | See the interaction section. | 2,000 | To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. | standard output | |
PASSED | 4e5794545917aa3b0155273c89eda074 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
public class code_ {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String args[] ) throws Exception {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int j=0;j<t;j++){
String s1=s.next();
// System.out.println(s1);
int c=0,max=0;
boolean f=false;
for(int i=0;i<s1.length();i++){
if(s1.charAt(i)=='1'&&!f)
f=true;
else if(s1.charAt(i)=='0'&&f)
c++;
else if(s1.charAt(i)=='1'&&f)
{ //f=false;
max+=c;
c=0;
}
}
System.out.println(max);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 543a506b3c8cdc6c0c8166fc216adbe2 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.Scanner;
public class train {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String s = "";int c =0;
while (t != 0) {
s = in.next();c=0;
int a = s.indexOf("1");
int b = s.lastIndexOf("1") + 1;
if (a != -1 && b != -1 ) {
s = s.substring(a, b);
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '0')c++;
}
System.out.println(c);
}
else System.out.println("0");
t--;
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | a847f4b4916f566408f7549a8085352f | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
import java.math.*;
public class test{
public static void main(String []a){
int size;
Scanner obj = new Scanner(System.in);
size=obj.nextInt();
String sd = obj.nextLine();
for(int i=0;i<size;i++){
int count0=0;
String s =obj.nextLine();
int findi1=0;
int k=0;
int findb1=0;
// System.out.println("www");
while(k<s.length() && s.charAt(k) != '1' ){
k++;
}
if( k<s.length() && s.charAt(k) == '1')
{
findi1=k;
}
else{
System.out.println(0);
continue;
}
for(int j=s.length()-1;j>k;j--){
if(s.charAt(j) == '1')
{findb1=j;break;}
}
if(findb1==0 )
{System.out.println(0);
continue;
}
for(int g=findi1+1;g<findb1;g++)
{
if(s.charAt(g)=='0')
count0++;
}
System.out.println(count0);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | df50570c4437bf549590215db58e778b | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br ;
public static void main(String[] args) throws Exception{
// write your code here
input input = new input();
int tc= input.in();
while (tc-->0){
String s = input.sin();
int len = s.length();
int zero[] = new int[len];
if(len==1|| len==2){
System.out.println(0);
continue;
}
int f=-1;
int l=-1;
for (int k =0;k<len;k++){
char ch = s.charAt(k);
int last= k!=0?zero[k-1]:0;
if (ch == '0') {
zero[k]+=last+1;
} else{
zero[k]= last;
if(f!=-1){
l=k;
} else{
f=k;
l=k;
}
}
}
if(f!=-1)
System.out.println(zero[l]-zero[f]);
else
System.out.println(0);
}
}
}
class input{
BufferedReader br ;
public input(){
br = new BufferedReader(new InputStreamReader(System.in));
}
int in() throws Exception{
return Integer.parseInt(br.readLine());
}
long Lin() throws Exception{
return Long.parseLong(br.readLine());
}
long Lin(String num) throws Exception{
return Long.parseLong(num);
}
String sin() throws Exception{
return br.readLine();
}
String [] sarrin() throws Exception{
return br.readLine().split(" ");
}
String [] regexSplit() throws Exception{
return br.readLine().split(" ?(?<!\\G)((?<=[^\\p{Punct}])(?=\\p{Punct})|\\b) ?");
}
int in(String num) throws Exception{
return Integer.parseInt(num);
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | a399272073a430e7dde9bcd8b2d35def | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.Scanner;
public class scratch_36 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int j=0;j<t;j++){
String n=sc.next();
int prev=0;
int count=0;
int g=n.indexOf('1');
if(g==-1){
System.out.println(0);
}
else {
n=n.substring(g);
for(int i=0;i<n.length();i++){
if(n.charAt(i)=='0'){
}
else {
if(i-prev-1>=1){
count+=i-prev-1;
}
prev=i;
}
}
System.out.println(count);
}
}}} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | dda9c6842a7ad787ecade749dd776dd6 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
String str = scanner.next();
int start = 0;
while (start < str.length() && str.charAt(start) == '0') {
start++;
}
int end = str.length()-1;
while (end >= 0 && str.charAt(end) == '0') {
end--;
}
int answer = 0;
for (int i = start; i <= end; i++) {
if (str.charAt(i) == '0')
answer++;
}
System.out.println(answer);
}
scanner.close();
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | f988d9a1d146b21754afcc6cac5be2c4 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class CodeForce{
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
String str=br.readLine();
int temp=0,count=0,max=-9,flag=0;
for(int i=0;i<str.length();i++){
if(temp==1 && str.charAt(i)=='1'){
temp=0;
flag=0;
}
if(temp==0 && str.charAt(i)=='1'){
temp=1;
// System.out.println("1");
}
if(temp==1 && str.charAt(i)=='0'){
count++;
flag++;
//System.out.println("11");
}
}
System.out.println(count-flag);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | ce821769177df0368f8d567c27aba036 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i1=0;i1<t;i1++)
{
String s=sc.next();
char[] ch=s.toCharArray();
int x=s.indexOf('1');
int y=s.lastIndexOf('1');
int m=0;int count=0;
boolean flag=true;
for(int i=x;i<y;i++)
{
if(ch[i]=='0')
{
m++;
}
}
System.out.println(m);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 0f201ba69d9aeb9ddc478aca35e2a3b4 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class hello2
{static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static String sum (String s)
{
String s1 = "";
if(s.contains("a"))
s1+="a";
if(s.contains("e"))
s1+="e";
if(s.contains("i"))
s1+="i";
if(s.contains("o"))
s1+="o";
if(s.contains("u"))
s1+="u";
return s1;
}
public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
public static void main(String args[])
{
FastReader sc = new FastReader();
int T = sc.nextInt();
while(T-->0)
{
String p = sc.next();
int ans=0;
int c=0;
for(int i=0;i<p.length();i++)
{
if(p.charAt(i)=='1')
{c=i;
break;
}
}
int last=0;
for(int i=p.length()-1;i>=0;i--)
{
if(p.charAt(i)=='1')
{
last=i;
break;
}
}
for(int i=c;i<last;i++)
if(p.charAt(i)=='0')
{
ans++;
}
System.out.println(ans);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 708b777b307cf60b737e385da338578b | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | //package codeforce;
import java.util.*;
public class class5 {
public static void main(String []args){
Scanner scan= new Scanner(System.in);
int test;
String s;
test=scan.nextInt();
while(test-- >0){
int count=0;
s=scan.next();
int f=s.indexOf('1');
int l=s.lastIndexOf('1');
for(int i=f;i<l;i++){
if(s.charAt(i)=='0'){
count=count+1;
}
}
System.out.println(count);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | d7b89dd170d0dc07efed3179b95fbbec | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
import java.io.*;
public class ErasingZeroes
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T-->0)
{
String str = br.readLine();
int first=-1,last=-1,zero=0,one=0;
for(int i=0;i<str.length();i++)
{
char ch = str.charAt(i);
if(first==-1 && ch=='1')
{
first=i;
one++;
}
if(ch=='1')
{
last=i;
}
if(ch=='0')
zero++;
}
if(zero==0 || one==0)
{
System.out.println(0);
continue;
}
int count = 0;
for(int i=first;i<=last;i++)
{
char ch = str.charAt(i);
if(ch=='0')
count++;
}
System.out.println(count);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 7d1f2280d1bb093c89483d23fe44b13e | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes |
import java.util.Scanner;
public class ErasingZeroes {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
for(int i=0;i<n;i++) {
String s=scan.next();
int t=0;
if(s.length()==1 || s.contains("1")==false) {
System.out.println(t);
continue;
}
int j=0,k=s.length()-1;
while(s.charAt(j)!='1') {
j++;continue;
}
while(s.charAt(k)!='1') {
k--;continue;
}
for(int x=j;x<k;x++) {
if(s.charAt(x)=='0') {
s.replaceAll("0", "");
t++;
}
}
System.out.println(t);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 16abd4944877ea99871fe11da925537e | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
public class ErasingZ{
public static int minimum(String s){
int i = 0;
int j = s.length() - 1;
while(i < j
&& (s.charAt(i) == '0' || s.charAt(j) == '0')){
if(s.charAt(i) == '0'){
i = i + 1;
}
if(s.charAt(j) == '0'){
j = j - 1;
}
}
int ans = 0;
while(i < j){
if(s.charAt(i++) == '0'){
ans++;
}
}
return ans;
}
public static void main(String args[]){
Scanner sca = new Scanner(System.in);
int t = sca.nextInt();
while(t-- > 0){
String s = sca.next();
System.out.println(minimum(s));
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 40ea817c4f97c6a9076d6f6966822e42 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | //dman @16-07-2020 @QuarantineVIbes
import java.util.*;
public class Game{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int t=s.nextInt();
s.nextLine();
while(t-->0){
String s1=s.nextLine();
int count=0,z=0,m=0,temp=0;
for(int i=0;i<s1.length()-1;i++){
for(int j=i+1;j<s1.length();j++){
if(s1.charAt(i)=='1' && s1.charAt(j)=='0'){
m++;
count++;
temp=m;
}else{
i=j-1;
m=0;
break;
}
}
}
if(s1.charAt(s1.length()-1)=='0'){
System.out.println(count-temp);
}
else
System.out.println(count);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 07285c30f71fc3c0a6ab59f3a06baa3c | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader var = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(var.readLine());
String line = null;
int n = -1;
boolean oneFlag = false;
int cntZeroesTemp = 0;
int cntZeroes = 0;
while (t > 0) {
line = var.readLine();
n = line.length();
if (n == 1)
System.out.println("0");
else {
for (int i = 0; i < n; i++) {
if (line.charAt(i) == '1') {
oneFlag = true;
cntZeroes += cntZeroesTemp;
cntZeroesTemp = 0;
} else if (line.charAt(i) == '0' && oneFlag) {
cntZeroesTemp++;
}
}
System.out.println(cntZeroes);
}
cntZeroes = 0;
cntZeroesTemp = 0;
oneFlag = false;
t--;
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 7f1f424269891cbf2999e707d751ab28 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
public class Solution {
public static void main(String [] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0) {
String st = br.readLine();
if(st.length()==0) {
System.out.println(0);
}else {
st = trimFromStart(st);
if(st.length()==0) {
System.out.println(0);
}else {
st = trimFromEnd(st);
if(st.length()==0) {
System.out.println(0);
}else {
int n = st.length();
int ans = 0 ;
for(int i=0;i<n;i++) {
if(st.charAt(i)=='0') {
ans++;
}
}
System.out.println(ans);
}
}
}
}
}
private static String trimFromStart(String s) {
int n = s.length();
for(int i=0;i<n;i++) {
if(s.charAt(i)!='0') {
return s.substring(i);
}
}
return "";
}
private static String trimFromEnd(String s) {
int n = s.length();
for(int i=n-1;i>=0;i--) {
if(s.charAt(i)!='0') {
return s.substring(0,i+1);
}
}
return "";
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 1840388d44de8582cf835fa76022a395 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
public class MS {
public static void main (String args[]){
Scanner sc =new Scanner (System.in);
int n;
String str="";
n=sc.nextInt();
for(int i=0;i<n;i++){
str=sc.next();
int flag_for_one=0;
int nextOne=0;
int count=0;
int tempcount=0;
for(int j=0;j<str.length();j++){
if(str.charAt(j)=='1'){
flag_for_one=1;
count=count+tempcount;
nextOne=0;
tempcount=0;
}
if(str.charAt(j)=='0'&&flag_for_one==1){
tempcount++;
}
}
System.out.println(count);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 424a46011b7e79c048cffcd7507647fb | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes |
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int tc = scanner.nextInt();
for (int i = 0; i < tc; i++) {
String seq = scanner.next();
System.out.println(zeroesErasing(seq));
}
}
public static int zeroesErasing (String seq) {
char[] seqChars = seq.toCharArray();
int count = -1;
if (seq.contains("01")) {
if (seq.contains("10")) {
count = 0;
int returnedcount = count;
boolean backToOne = false;
for (int i = seq.indexOf("10")+1; i < seq.length(); i++) {
if (seqChars[i] == '0') {
count++;
} else {
returnedcount = count;
}
}
return returnedcount;
} else {
return 0;
}
} else {
return 0;
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 2d619ddd45fad187e464a074bebc189d | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
public class JavaApplication78 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int a[]=new int[h];
while(h--!=0){
int temp=0;
boolean t=true;
int count=0,count1=0;
String s=sc.next();
for(int i=0;i<s.length();i++)
{
if(t==true && s.charAt(i)=='1')
{
temp=i;
t=false;
break;
}
}
for(int i=temp;i<s.length();i++)
{
if(s.charAt(i)=='0')
count++;
if(s.charAt(i)=='1')
{
count1=count;
}
}
System.out.println(count1);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 8ab39fab5f34cfe4ae21b1a5b110b8a7 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
public class Solution
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
String str=s.next();
int pos1=-1;
int n=str.length();
int pos2=str.length();
for(int i=0;i<n;i++)
{
if(str.charAt(i)=='1')
{
pos1=i;
break;
}
}
for(int i=n-1;i>=0;i--)
{
if(str.charAt(i)=='1')
{
pos2=i;
break;
}
}
if(pos1==-1&&pos2==str.length())
{
System.out.println(0);
}
else{
int cunt=0;
for(int i=pos1;i<=pos2;i++)
{
if(str.charAt(i)=='0')
{
cunt++;
}
}
System.out.println(cunt);
}
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | c7ff5c4a2d1934821852da7d7ceec6b5 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
public class Solurion
{
public static void main(String ar[])
{
Scanner scan=new Scanner(System.in);
int test=scan.nextInt();
while(test-->0)
{
String s=scan.next();
int frst=-1,last=-1;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='1')
{
if(frst==-1)
{
frst=i;last=i;
}
last=i;
}
}
int count=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='0'&&i>=frst&&i<=last)
count++;
}
System.out.println(count);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | d4521cee4ad75cb7631b5c11a064e133 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
public class july30 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for(int i=0; i<t; i++) {
String num = scan.next();
int left = num.indexOf('1');
int right = num.lastIndexOf('1');
int count0=0;
for(int j=left;j<right;j++){
if(num.charAt(j)==48){
count0++;
}
}
System.out.println(count0);
}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 2562700803171135802edb72aa1de515 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.Scanner;
public class ErasingZeroes {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
int[] result = new int[t];
boolean flag;
String s;
for (int j = 0; j < t; j++) {
flag = false;
s = scan.next();
int num = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
flag = true;
result[j] += num;
num = 0;
} else {
if (flag)
num++;
}
}
}
for (int o : result) {
System.out.println(o);
}
scan.close();
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | f140f899178786adbe16b96857217b6d | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.*;
import java.lang.*;
public class erase_zeros {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int i;
for(i=0;i<t;i++)
{
String s=sc.next();
int count=0;
int j=1;
while(j<s.length())
{
if(s.charAt(j)=='0' && s.charAt(j-1)=='1')
{
int temp=0;
while(s.charAt(j)!='1')
{
count++;
j++;
temp++;
if(j==s.length())
{
count=count-temp;
break;
}
}
}
j++;
}
System.out.println(count);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 5f1d412fd2f9b767dd9ee04e3b1b7ab2 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class P1303A_ErasingZeroes {
public static void main(String subhani[]) throws IOException, NumberFormatException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String str = br.readLine();
solution(str);
}
}
public static void solution(String str) {
int min = 0;
ArrayList<Integer> onePosition = new ArrayList<Integer>();
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == '1')
onePosition.add(i);
}
if (onePosition.size() == 0 || onePosition.size() == 1) {
min = 0;
} else {
for (int i = 0; i < onePosition.size() - 1; ++i) {
int diff = onePosition.get(i + 1) - onePosition.get(i) - 1;
if (diff >= 1) {
min += diff;
}
// System.out.println(min);
}
}
System.out.println(min);
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | af6df681653922a055627d1a22225156 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int steps = sc.nextInt();
for (int i = 0; i < steps; i++) {
String line = sc.next();
if (line.length() <= 1) {
System.out.println(0);
continue;
}
System.out.println(skipZeros(line));
}
}
private static int skipZeros(String s) {
int zerosCount = 0;
char[] chars = s.toCharArray();
if (!s.contains("1")) {
return 0;
}
for (int i = s.indexOf("1"); i < s.lastIndexOf("1"); i++) {
if (chars[i] == '0') {
zerosCount++;
}
}
return zerosCount;
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 21b4519c6c9efe8c282b99e05c45e67a | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.Scanner;
public class main{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int tc=s.nextInt();
while(tc--!=0){
String a=s.next();
int f = a.indexOf("1");
int l = a.lastIndexOf("1");
int c= 0;
if(f==-1) System.out.println(0);
else{
for(int i =f;i<=l;i++) if(a.charAt(i)=='0') c++;
System.out.println(c);}}
}
} | Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 38a76a12585be606a8aecf4b606e583f | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Rb_wahid
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
AErasingZeroes solver = new AErasingZeroes();
solver.solve(1, in, out);
out.close();
}
static class AErasingZeroes {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int t = in.nextInt();
while (t-- > 0) {
String string = in.next();
if (string.length() < 2)
out.println(0);
else {
char[] arr = string.toCharArray();
int count = 0;
int first1 = -1;
int last1 = arr.length - 1;
for (char ch : arr) {
first1++;
if (ch == '1')
break;
}
for (; last1 >= 0; last1--) {
if (arr[last1] == '1') {
break;
}
}
for (int i = first1 + 1; i < last1; i++) {
char xchar = arr[i];
if (xchar == '0') {
count++;
}
}
out.println(count);
}
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return nextString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 120381fa43002a1f28755beaa6249218 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.Scanner;
public class ErasingZeroes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int j = 0; j < t; j++) {
String s = sc.next();
int ones = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') ones++;
}
int counter = 0;
int removedOnes = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1' && ones - removedOnes > 0) {
removedOnes++;
} else if (!(s.charAt(i) == '0' && ones - removedOnes == 0 || ones - removedOnes == ones)) {
counter++;
}
}
System.out.println(counter);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 6e0db6c2bc3cdc58a53fd308f815e35d | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes | import java.util.Scanner;
public class ErasingZeroes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int j = 0; j < t; j++) {
String s = sc.next();
int ones = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') ones++;
}
int counter = 0;
int removedOnes = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1' && ones - removedOnes > 0) {
removedOnes++;
} else if (s.charAt(i) == '0' && ones - removedOnes == 0 || ones - removedOnes == ones) {
} else {
counter++;
}
}
System.out.println(counter);
}
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 53e683a898e2f99983ce70c4fced3b32 | train_000.jsonl | 1581518100 | You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader fastReader = new FastReader();
StringBuilder out = new StringBuilder();
int testCaeses = fastReader.nextInt();
while (testCaeses-- > 0) {
char [] array = fastReader.next().toCharArray();
int left = 0, right = array.length - 1;
while(left < right && array[left] == '0') {
++left;
}
while(left < right && array[right] == '0') {
--right;
}
if(left > right) {
out.append("0\n");
}else {
int counter = 0;
for(;left <right;left++) {
if(array[left] =='0') {
++counter;
}
}
out.append(counter+"\n");
}
}
System.out.println(out);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["3\n010011\n0\n1111000"] | 1 second | ["2\n0\n0"] | NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | Java 8 | standard input | [
"implementation",
"strings"
] | 5de66fbb594bb317654366fd2290c4d3 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1. | 800 | Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). | standard output | |
PASSED | 010120ece23c88b5f94dc3c5967eb5c8 | train_000.jsonl | 1560955500 | After a hard-working week Polycarp prefers to have fun. Polycarp's favorite entertainment is drawing snakes. He takes a rectangular checkered sheet of paper of size $$$n \times m$$$ (where $$$n$$$ is the number of rows, $$$m$$$ is the number of columns) and starts to draw snakes in cells.Polycarp draws snakes with lowercase Latin letters. He always draws the first snake with the symbol 'a', the second snake with the symbol 'b', the third snake with the symbol 'c' and so on. All snakes have their own unique symbol. There are only $$$26$$$ letters in the Latin alphabet, Polycarp is very tired and he doesn't want to invent new symbols, so the total number of drawn snakes doesn't exceed $$$26$$$.Since by the end of the week Polycarp is very tired, he draws snakes as straight lines without bends. So each snake is positioned either vertically or horizontally. Width of any snake equals $$$1$$$, i.e. each snake has size either $$$1 \times l$$$ or $$$l \times 1$$$, where $$$l$$$ is snake's length. Note that snakes can't bend.When Polycarp draws a new snake, he can use already occupied cells for drawing the snake. In this situation, he draws the snake "over the top" and overwrites the previous value in the cell.Recently when Polycarp was at work he found a checkered sheet of paper with Latin letters. He wants to know if it is possible to get this sheet of paper from an empty sheet by drawing some snakes according to the rules described above. If it is possible, he is interested in a way to draw snakes. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1185e {
public static void main(String[] args) throws IOException {
int t = ri();
next: while (t --> 0) {
int n = rni(), m = ni(), pos[][] = new int[26][4], bounds[][] = new int[26][4];
for (int i = 0; i < 26; ++i) {
fill(pos[i], -1);
bounds[i][0] = n;
bounds[i][1] = m;
}
char[][] map = new char[n][m];
for (int i = 0; i < n; ++i) {
char[] row = rcha();
for (int j = 0; j < m; ++j) {
map[i][j] = row[j];
if (row[j] >= 'a') {
int ind = row[j] - 'a';
if (pos[ind][0] == -1) {
pos[ind][0] = i;
pos[ind][1] = j;
}
pos[ind][2] = i;
pos[ind][3] = j;
bounds[ind][0] = min(bounds[ind][0], i);
bounds[ind][1] = min(bounds[ind][1], j);
bounds[ind][2] = max(bounds[ind][2], i);
bounds[ind][3] = max(bounds[ind][3], j);
}
}
}
// prln(pos[0]);
int ans = 26;
for (int i = 25; i >= 0; --i) {
if (pos[i][0] == -1 && (i == 25 || pos[i + 1][0] == -1)) {
--ans;
continue;
}
if (pos[i][0] == -1) {
pos[i][0] = pos[i][2] = pos[i + 1][0];
pos[i][1] = pos[i][3] = pos[i + 1][1];
continue;
}
if (bounds[i][0] != bounds[i][2] && bounds[i][1] != bounds[i][3]) {
// prln(bounds[i]);
prN();
continue next;
}
if (pos[i][0] == pos[i][2]) {
for (int j = pos[i][1] + 1; j < pos[i][3]; ++j) {
if (map[pos[i][0]][j] - 'a' < i) {
// prln(i, pos[i][0], j);
prN();
continue next;
}
}
} else {
for (int j = pos[i][0] + 1; j < pos[i][2]; ++j) {
if (map[j][pos[i][1]] - 'a' < i) {
// prln(i, j, pos[i][1]);
prN();
continue next;
}
}
}
}
prY();
prln(ans);
for (int i = 0; i < ans; ++i) {
prln(pos[i][0] + 1, pos[i][1] + 1, pos[i][2] + 1, pos[i][3] + 1);
}
}
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}
} | Java | ["1\n5 6\n...a..\n..bbb.\n...a..\n.cccc.\n...a..", "3\n3 3\n...\n...\n...\n4 4\n..c.\nadda\nbbcb\n....\n3 5\n..b..\naaaaa\n..b..", "2\n3 3\n...\n.a.\n...\n2 2\nbb\ncc"] | 4 seconds | ["YES\n3\n1 4 5 4\n2 3 2 5\n4 2 4 5", "YES\n0\nYES\n4\n2 1 2 4\n3 1 3 4\n1 3 3 3\n2 2 2 3\nNO", "YES\n1\n2 2 2 2\nYES\n3\n1 1 1 2\n1 1 1 2\n2 1 2 2"] | null | Java 11 | standard input | [
"implementation",
"brute force"
] | f34d21b9568c758cacc1d00af1316c86 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases to solve. Then $$$t$$$ test cases follow. The first line of the test case description contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n,m \le 2000$$$) — length and width of the checkered sheet of paper respectively. Next $$$n$$$ lines of test case description contain $$$m$$$ symbols, which are responsible for the content of the corresponding cell on the sheet. It can be either lowercase Latin letter or symbol dot ('.'), which stands for an empty cell. It is guaranteed that the total area of all sheets in one test doesn't exceed $$$4\cdot10^6$$$. | 2,000 | Print the answer for each test case in the input. In the first line of the output for a test case print YES if it is possible to draw snakes, so that you can get a sheet of paper from the input. If it is impossible, print NO. If the answer to this question is positive, then print the way to draw snakes in the following format. In the next line print one integer $$$k$$$ ($$$0 \le k \le 26$$$) — number of snakes. Then print $$$k$$$ lines, in each line print four integers $$$r_{1,i}$$$, $$$c_{1,i}$$$, $$$r_{2,i}$$$ and $$$c_{2,i}$$$ — coordinates of extreme cells for the $$$i$$$-th snake ($$$1 \le r_{1,i}, r_{2,i} \le n$$$, $$$1 \le c_{1,i}, c_{2,i} \le m$$$). Snakes should be printed in order of their drawing. If there are multiple solutions, you are allowed to print any of them. Note that Polycarp starts drawing of snakes with an empty sheet of paper. | standard output | |
PASSED | 2e5c6c2efcf43888b9a6dccf10f4d7ac | train_000.jsonl | 1601903100 | In the Kingdom of Wakanda, the 2020 economic crisis has made a great impact on each city and its surrounding area. Cities have made a plan to build a fast train rail between them to boost the economy, but because of the insufficient funds, each city can only build a rail with one other city, and they want to do it together.Cities which are paired up in the plan will share the cost of building the rail between them, and one city might need to pay more than the other. Each city knows the estimated cost of building their part of the rail to every other city. One city can not have the same cost of building the rail with two different cities.If in a plan, there are two cities that are not connected, but the cost to create a rail between them is lower for each of them than the cost to build the rail with their current pairs, then that plan is not acceptable and the collaboration won't go on. Your task is to create a suitable plan for the cities (pairing of the cities) or say that such plan doesn't exist. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
static final int NONE = -1;
static class Irving {
int n;
int[][] preferences;
int[][] rank;
int[] left;
int[] right;
int[] second;
boolean exists;
int[] matching;
public Irving(int[][] preferences) {
this.n = preferences.length;
this.preferences = new int[n][n];
// Append artificial last entries to preference lists
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
this.preferences[i][j] = preferences[i][j];
}
this.preferences[i][n - 1] = i;
}
// Construct ranking from preference lists
this.rank = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
rank[i][this.preferences[i][j]] = j;
}
}
this.left = new int[n];
this.right = new int[n];
Arrays.fill(this.right, n - 1);
this.second = new int[n];
this.exists = true;
// Run algorithm
proposalPhase();
if (exists) {
for (int i = 0; i < n; i++) second[i] = left[i] + 1;
rotationPhase();
this.matching = new int[n];
for (int i = 0; i < n; i++) {
matching[i] = preferences[i][left[i]];
}
}
}
/**
* Phase 1 of the algorithm
*/
private void proposalPhase() {
boolean[] proposedTo = new boolean[n];
for (int i = 0; i < n; i++) {
int proposer = i;
int next;
do {
next = preferences[proposer][left[proposer]];
int current = preferences[next][right[next]];
while (rank[next][proposer] > rank[next][current]) {
left[proposer]++;
next = preferences[proposer][left[proposer]];
current = preferences[next][right[next]];
}
if (proposer == next) {
exists = false;
return;
}
right[next] = rank[next][proposer];
proposer = current;
} while (proposedTo[next]);
proposedTo[next] = true;
}
}
/**
* Phase 2 of the algorithm
*/
private void rotationPhase() {
while (true) {
int unmatched = NONE;
for (int i = 0; i < n; i++) {
if (left[i] > right[i]) {
exists = false;
return;
}
if (left[i] < right[i] && unmatched == NONE) unmatched = i;
}
if (unmatched == NONE) return;
reduce(cycle(unmatched));
}
}
/**
* Finds an all-or-nothing cycle
*/
private int[] cycle(int unmatched) {
int[] p = new int[n];
p[0] = unmatched;
int[] q = new int[n];
int[] index = new int[n];
Arrays.fill(index, NONE);
index[unmatched] = 0;
for (int i = 0; i < n - 1; i++) {
updateSecond(p[i]);
q[i] = preferences[p[i]][second[p[i]]];
p[i + 1] = preferences[q[i]][right[q[i]]];
if (index[p[i + 1]] != NONE) {
int s = index[p[i + 1]];
int[] a = new int[i + 1 - s];
for (int j = s; j < i + 1; j++) {
a[j - s] = p[j];
}
return a;
}
index[p[i + 1]] = i + 1;
}
return null;
}
/**
* Updates the index of the second guy in person's reduced preference
* list
*/
private void updateSecond(int person) {
int next = preferences[person][second[person]];
while (rank[next][person] > right[next]) {
second[person]++;
next = preferences[person][second[person]];
}
}
/**
* Phase 2 reduction of the preference lists, given a cycle
*/
private void reduce(int[] cycle) {
for (int proposer : cycle) {
int next = preferences[proposer][second[proposer]];
right[next] = rank[next][proposer];
left[proposer] = second[proposer];
second[proposer]++;
}
}
}
static class Cost {
int from;
int to;
int value;
public Cost(int from, int to, int value) {
this.from = from;
this.to = to;
this.value = value;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Cost[][] A = new Cost[N][N - 1];
int[][] preferences = new int[N][N - 1];
for (int i = 0; i < N; i++) {
String[] line = br.readLine().split(" ");
for (int j = 0; j < N - 1; j++) {
A[i][j] = new Cost(i, j < i ? j : j + 1, Integer.parseInt(line[j]));
}
Arrays.sort(A[i], Comparator.comparingInt(o -> o.value));
for (int j = 0; j < N - 1; j++) {
preferences[i][j] = A[i][j].to;
}
}
if (N % 2 != 0) {
System.out.println(-1);
return;
}
Irving irving = new Irving(preferences);
if (irving.exists) {
for (int i = 0; i < N; i++) {
System.out.println(irving.matching[i] + 1);
}
} else {
System.out.println(-1);
}
}
}
| Java | ["4\n35 19 20\n76 14 75\n23 43 78\n14 76 98", "4\n2 5 8\n7 1 12\n4 6 7\n8 4 5"] | 1 second | ["3\n4\n1\n2", "-1"] | null | Java 11 | standard input | [] | 82add5e87e7c801d8a840f09292e7a1b | First line contains one integer $$$N \;(2 \leq N \leq 10^3)\, $$$ — the number of cities. Each of the next $$$N$$$ lines contains $$$N-1$$$ integers $$$A_{i,1}, A_{i,2}, ..., A_{i,i-1}, A_{i,i+1}, ..., A_{i,N-1}\; (1 \leq A_{i,j} \leq 10^9)\, $$$ — where $$$A_{i,j}$$$ represents the cost for city $$$i$$$ to build the rail to city $$$j$$$. Note that in each line $$$A_{i,i}$$$ is skipped. | 3,500 | Output should contain $$$N$$$ integers $$$O_{1}, O_{2}, ..., O_N$$$, where $$$O_i$$$ represents the city with which city $$$i$$$ should build the rail with, or $$$-1$$$ if it is not possible to find the stable pairing. | standard output | |
PASSED | dfa35030716cb9e42c3958b06d824855 | train_000.jsonl | 1601903100 | The Bubble Cup hypothesis stood unsolved for $$$130$$$ years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem:Given a number $$$m$$$, how many polynomials $$$P$$$ with coefficients in set $$${\{0,1,2,3,4,5,6,7\}}$$$ have: $$$P(2)=m$$$?Help Jerry Mao solve the long standing problem! | 256 megabytes | import java.io.*;
import java.util.*;
public class E {
static int MOD = (int) 1e9 + 7, inv = (MOD + 1) / 2;
static long sum(long x) {
x %= MOD;
return x * (x + 1) % MOD * inv % MOD;
}
static long sum(long l, long r) {
long ans = sum(r) - sum(l - 1);
if (ans < 0)
ans += MOD;
return ans;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int tc = sc.nextInt();
while (tc-- > 0) {
long n = sc.nextLong();
if (n == 1) {
out.println(1);
continue;
}
n >>= 1;
long x = n - 1;
long ans = 2 * sum(2, x / 2 + 1) % MOD;
if (x % 2 == 1)
ans += n / 2 + 1;
ans %= MOD;
ans += 2;
ans %= MOD;
out.println(ans);
}
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Scanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
boolean ready() throws IOException {
return br.ready() || st.hasMoreTokens();
}
int[] nxtArr(int n) throws IOException {
int[] ans = new int[n];
for (int i = 0; i < n; i++)
ans[i] = nextInt();
return ans;
}
}
static void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
static void shuffle(int[] a) {
int n = a.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
int tmpIdx = rand.nextInt(n);
int tmp = a[i];
a[i] = a[tmpIdx];
a[tmpIdx] = tmp;
}
}
} | Java | ["2\n2 4"] | 1 second | ["2\n4"] | NoteIn first case, for $$$m=2$$$, polynomials that satisfy the constraint are $$$x$$$ and $$$2$$$.In second case, for $$$m=4$$$, polynomials that satisfy the constraint are $$$x^2$$$, $$$x + 2$$$, $$$2x$$$ and $$$4$$$. | Java 11 | standard input | [
"dp",
"constructive algorithms",
"bitmasks",
"math"
] | 24f4bd10ae714f957920afd47ac0c558 | The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 5\cdot 10^5)$$$ - number of test cases. On next line there are $$$t$$$ numbers, $$$m_i$$$ $$$(1 \leq m_i \leq 10^{18})$$$ - meaning that in case $$$i$$$ you should solve for number $$$m_i$$$. | 2,400 | For each test case $$$i$$$, print the answer on separate lines: number of polynomials $$$P$$$ as described in statement such that $$$P(2)=m_i$$$, modulo $$$10^9 + 7$$$. | standard output | |
PASSED | 922db010402c5f515036f15a0e414979 | train_000.jsonl | 1601903100 | The Bubble Cup hypothesis stood unsolved for $$$130$$$ years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem:Given a number $$$m$$$, how many polynomials $$$P$$$ with coefficients in set $$${\{0,1,2,3,4,5,6,7\}}$$$ have: $$$P(2)=m$$$?Help Jerry Mao solve the long standing problem! | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.InputMismatchException;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
static class TaskAdapter implements Runnable {
@Override
public void run() {
long startTime = System.currentTimeMillis();
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
Output out = new Output(outputStream);
JBubbleCupHypothesis solver = new JBubbleCupHypothesis();
int testCount = in.nextInt();
for(int i = 1; i<=testCount; i++)
solver.solve(i, in, out);
out.close();
System.err.println(System.currentTimeMillis()-startTime+"ms");
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1<<28);
thread.start();
thread.join();
}
static class JBubbleCupHypothesis {
private final int mod = (int) (1e9+7);
BigInteger four = BigInteger.valueOf(4);
BigInteger modd = BigInteger.valueOf(mod);
public JBubbleCupHypothesis() {
}
public void solve(int kase, InputReader in, Output pw) {
BigInteger x = new BigInteger(in.next()).divide(BigInteger.TWO).add(BigInteger.TWO);
pw.println(x.multiply(x).divide(four).mod(modd));
}
}
static class Output implements Closeable, Flushable {
public StringBuilder sb;
public OutputStream os;
public int BUFFER_SIZE;
public String lineSeparator;
public Output(OutputStream os) {
this(os, 1<<16);
}
public Output(OutputStream os, int bs) {
BUFFER_SIZE = bs;
sb = new StringBuilder(BUFFER_SIZE);
this.os = new BufferedOutputStream(os, 1<<17);
lineSeparator = System.lineSeparator();
}
public void print(String s) {
sb.append(s);
if(sb.length()>BUFFER_SIZE >> 1) {
flushToBuffer();
}
}
public void println(Object... o) {
for(int i = 0; i<o.length; i++) {
if(i!=0) {
print(" ");
}
print(String.valueOf(o[i]));
}
println();
}
public void println() {
sb.append(lineSeparator);
}
private void flushToBuffer() {
try {
os.write(sb.toString().getBytes());
}catch(IOException e) {
e.printStackTrace();
}
sb = new StringBuilder(BUFFER_SIZE);
}
public void flush() {
try {
flushToBuffer();
os.flush();
}catch(IOException e) {
e.printStackTrace();
}
}
public void close() {
flush();
try {
os.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
static interface InputReader {
String next();
}
static class FastReader implements InputReader {
final private int BUFFER_SIZE = 1<<16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer;
private int bytesRead;
public FastReader(InputStream is) {
din = new DataInputStream(is);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() {
StringBuilder ret = new StringBuilder(64);
byte c = skip();
while(c!=-1&&!isSpaceChar(c)) {
ret.appendCodePoint(c);
c = read();
}
return ret.toString();
}
public int nextInt() {
int ret = 0;
byte c = skipToDigit();
boolean neg = (c=='-');
if(neg) {
c = read();
}
do {
ret = ret*10+c-'0';
} while((c = read())>='0'&&c<='9');
if(neg) {
return -ret;
}
return ret;
}
private boolean isSpaceChar(byte b) {
return b==' '||b=='\r'||b=='\n'||b=='\t'||b=='\f';
}
private byte skip() {
byte ret;
while(isSpaceChar((ret = read()))) ;
return ret;
}
private boolean isDigit(byte b) {
return b>='0'&&b<='9';
}
private byte skipToDigit() {
byte ret;
while(!isDigit(ret = read())&&ret!='-') ;
return ret;
}
private void fillBuffer() {
try {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
}catch(IOException e) {
e.printStackTrace();
throw new InputMismatchException();
}
if(bytesRead==-1) {
buffer[0] = -1;
}
}
private byte read() {
if(bytesRead==-1) {
throw new InputMismatchException();
}else if(bufferPointer==bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
}
}
| Java | ["2\n2 4"] | 1 second | ["2\n4"] | NoteIn first case, for $$$m=2$$$, polynomials that satisfy the constraint are $$$x$$$ and $$$2$$$.In second case, for $$$m=4$$$, polynomials that satisfy the constraint are $$$x^2$$$, $$$x + 2$$$, $$$2x$$$ and $$$4$$$. | Java 11 | standard input | [
"dp",
"constructive algorithms",
"bitmasks",
"math"
] | 24f4bd10ae714f957920afd47ac0c558 | The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 5\cdot 10^5)$$$ - number of test cases. On next line there are $$$t$$$ numbers, $$$m_i$$$ $$$(1 \leq m_i \leq 10^{18})$$$ - meaning that in case $$$i$$$ you should solve for number $$$m_i$$$. | 2,400 | For each test case $$$i$$$, print the answer on separate lines: number of polynomials $$$P$$$ as described in statement such that $$$P(2)=m_i$$$, modulo $$$10^9 + 7$$$. | standard output | |
PASSED | 9162350d68a844fe2b32d382a44fb6d1 | train_000.jsonl | 1601903100 | The Bubble Cup hypothesis stood unsolved for $$$130$$$ years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem:Given a number $$$m$$$, how many polynomials $$$P$$$ with coefficients in set $$${\{0,1,2,3,4,5,6,7\}}$$$ have: $$$P(2)=m$$$?Help Jerry Mao solve the long standing problem! | 256 megabytes | //package bubblecup13.f;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class J {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
final int mod = 1000000007;
for(int T = ni();T > 0;T--){
long m = nl();
out.println(((m/4+1)%mod)*(((m/2+1)/2+1)%mod) % mod);
// // 8 4 2 1 = 15
// long[] dp = new long[9];
// dp[0] = 1;
// for(int i = 60;i >= 0;i--){
// long[] ndp = new long[9];
// int nj = (int) (m >>> i & 1);
// int njl = nj - 7;
// for(int j = 0;j < 8;j++) {
// ndp[Math.max(0, njl)] += dp[j];
// if(nj+1 < 9)ndp[nj+1] += mod - dp[j];
////
//// for (int k = 0; k <= 7; k++) {
//// int nj = (j * 2 + (int) (m >>> i & 1)) - k;
//// if (nj < 8 && nj >= 0) {
//// ndp[nj] += dp[j];
//// if (ndp[nj] >= mod) ndp[nj] -= mod;
//// }
//// }
// nj += 2; njl += 2;
// }
// for(int j = 1;j < 9;j++){
// ndp[j] += ndp[j-1];
// }
// for(int j = 0;j < 9;j++){
// ndp[j] %= mod;
// }
// dp = ndp;
// }
// long ex = ((m/4+1)%mod)*(((m/2+1)/2+1)%mod) % mod;
//// out.println(dp[0]);
// assert dp[0] == ex;
}
}
void run() throws Exception
{
// int n = 500000, m = 99999;
// Random gen = new Random();
// StringBuilder sb = new StringBuilder();
// sb.append(n + " ");
// for (int i = 0; i < n; i++) {
// sb.append(Math.abs(gen.nextLong() % (long)1e18) + " ");
// }
// INPUT = sb.toString();
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new J().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["2\n2 4"] | 1 second | ["2\n4"] | NoteIn first case, for $$$m=2$$$, polynomials that satisfy the constraint are $$$x$$$ and $$$2$$$.In second case, for $$$m=4$$$, polynomials that satisfy the constraint are $$$x^2$$$, $$$x + 2$$$, $$$2x$$$ and $$$4$$$. | Java 11 | standard input | [
"dp",
"constructive algorithms",
"bitmasks",
"math"
] | 24f4bd10ae714f957920afd47ac0c558 | The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 5\cdot 10^5)$$$ - number of test cases. On next line there are $$$t$$$ numbers, $$$m_i$$$ $$$(1 \leq m_i \leq 10^{18})$$$ - meaning that in case $$$i$$$ you should solve for number $$$m_i$$$. | 2,400 | For each test case $$$i$$$, print the answer on separate lines: number of polynomials $$$P$$$ as described in statement such that $$$P(2)=m_i$$$, modulo $$$10^9 + 7$$$. | standard output | |
PASSED | 8895331911e35d7fe8cae9b79316c016 | train_000.jsonl | 1426610700 | Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'.Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.Let's write down integer k ≥ 0 — the error threshold. We will say that string T occurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
D528 solver = new D528();
solver.solve(1, in, out);
out.close();
}
static class D528 {
int SS;
int TT;
int k;
char[] dna;
char[] pattern;
long[] A;
long[] a;
long[] C;
long[] c;
long[] G;
long[] g;
long[] T;
long[] t;
void access(long[] arr, char let) {
int[] cs = new int[SS + 1];
for (int i = 1; i <= SS; i++) {
cs[i] = cs[i - 1] + (dna[i - 1] == let ? 1 : 0);
}
for (int i = 0; i < SS; i++) {
if (cs[Math.min(SS, i + 1 + k)] - cs[Math.max(i - k, 0)] != 0)
arr[i] = 1;
}
}
void place(long[] arr, char let) {
for (int i = 0; i < TT; i++)
if (pattern[i] == let) arr[i] = 1;
}
void precomp() {
A = new long[SS];
access(A, 'A');
C = new long[SS];
access(C, 'C');
G = new long[SS];
access(G, 'G');
T = new long[SS];
access(T, 'T');
a = new long[TT];
place(a, 'A');
c = new long[TT];
place(c, 'C');
g = new long[TT];
place(g, 'G');
t = new long[TT];
place(t, 'T');
}
public void solve(int testNumber, FastScanner s, PrintWriter out) {
SS = s.nextInt();
TT = s.nextInt();
k = s.nextInt();
dna = s.next().toCharArray();
pattern = new StringBuilder(s.next()).reverse().toString().toCharArray();
precomp();
long[][] mults = new long[4][];
mults[0] = FFT.multiply(A, a);
mults[1] = FFT.multiply(C, c);
mults[2] = FFT.multiply(G, g);
mults[3] = FFT.multiply(T, t);
int ways = 0;
for (int k = TT - 1; k < TT + SS - 1; k++) {
long sum = 0;
for (long[] row : mults)
sum += row[k];
if (sum == TT) ways++;
}
out.println(ways);
}
}
static class FFT {
public static void fft(double[] a, double[] b, boolean invert) {
int n = a.length;
int shift = 32 - Integer.numberOfTrailingZeros(n);
for (int i = 1; i < n; i++) {
int j = Integer.reverse(i << shift);
if (i < j) {
double temp = a[i];
a[i] = a[j];
a[j] = temp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
for (int len = 2; len <= n; len <<= 1) {
int halfLen = len >> 1;
double angle = 2 * Math.PI / len * (invert ? -1 : 1);
double wLenA = Math.cos(angle);
double wLenB = Math.sin(angle);
for (int i = 0; i < n; i += len) {
double wA = 1;
double wB = 0;
for (int j = 0; j < halfLen; j++) {
double uA = a[i + j];
double uB = b[i + j];
double vA = a[i + j + halfLen] * wA - b[i + j + halfLen] * wB;
double vB = a[i + j + halfLen] * wB + b[i + j + halfLen] * wA;
a[i + j] = uA + vA;
b[i + j] = uB + vB;
a[i + j + halfLen] = uA - vA;
b[i + j + halfLen] = uB - vB;
double nextWA = wA * wLenA - wB * wLenB;
wB = wA * wLenB + wB * wLenA;
wA = nextWA;
}
}
}
if (invert) {
for (int i = 0; i < n; i++) {
a[i] /= n;
b[i] /= n;
}
}
}
public static long[] multiply(long[] a, long[] b) {
int resultSize = Integer.highestOneBit(Math.max(a.length, b.length) - 1) << 2;
resultSize = Math.max(resultSize, 2);
double[] aReal = new double[resultSize];
double[] aImaginary = new double[resultSize];
double[] bReal = new double[resultSize];
double[] bImaginary = new double[resultSize];
for (int i = 0; i < a.length; i++)
aReal[i] = a[i];
for (int i = 0; i < b.length; i++)
bReal[i] = b[i];
fft(aReal, aImaginary, false);
fft(bReal, bImaginary, false);
for (int i = 0; i < resultSize; i++) {
double real = aReal[i] * bReal[i] - aImaginary[i] * bImaginary[i];
aImaginary[i] = aImaginary[i] * bReal[i] + bImaginary[i] * aReal[i];
aReal[i] = real;
}
fft(aReal, aImaginary, true);
long[] result = new long[resultSize];
long carry = 0;
for (int i = 0; i < resultSize; i++) {
result[i] = Math.round(aReal[i]) + carry;
// carry = result[i] / 10;
// result[i] %= 10;
}
return result;
}
}
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
}
}
| Java | ["10 4 1\nAGCAATTCAT\nACAT"] | 3 seconds | ["3"] | NoteIf you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously. | Java 8 | standard input | [
"bitmasks",
"brute force",
"fft"
] | 11870e3fb1aaad9bf15dc505aa9cd0f5 | The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold. The second line contains string S. The third line contains string T. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'. | 2,500 | Print a single number — the number of occurrences of T in S with the error threshold k by the given definition. | standard output | |
PASSED | d49cc928b95cc459455a41e3f2b20dfb | train_000.jsonl | 1426610700 | Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'.Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.Let's write down integer k ≥ 0 — the error threshold. We will say that string T occurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(in, out);
out.close();
}
}
class TaskE {
private final String ACTG = "ACTG";
public void solve(InputReader in, OutputWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
String word = in.next();
String pattern = in.next();
int wordArray[][] = new int[n+1][4];
for(int i=0;i<n;i++) {
wordArray[Math.max(0,i-k)][ACTG.indexOf(word.charAt(i))]++;
wordArray[Math.min(i+k+1, n)][ACTG.indexOf(word.charAt(i))]--;
}
BitSet[] wordBits = new BitSet[4];
for (int i = 0; i < 4; ++i) {
wordBits[i] = new BitSet(n);
}
BitSet result = calculateResultBitSet(n, wordArray, wordBits);
int index;
for (int i = 0; i < m; ++i) {
index = ACTG.indexOf(pattern.charAt(i));
result.and(wordBits[index].get(i, n));
}
out.printLine(result.cardinality());
}
private BitSet calculateResultBitSet(int n, int[][] wordArray, BitSet[] wordBits) {
BitSet ans = new BitSet(n);
for(int i=0;i<n;i++) {
for(int j=0;j<4;j++) {
if(wordArray[i][j] > 0) wordBits[j].set(i);
if(i+1 < n) {
wordArray[i+1][j] += wordArray[i][j];
}
}
ans.set(i);
}
return ans;
}
}
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 boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
String line = reader.readLine();
if ( line == null ) {
return false;
}
tokenizer = new StringTokenizer(line);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() { return Double.parseDouble(next());}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if ( i != 0 ) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["10 4 1\nAGCAATTCAT\nACAT"] | 3 seconds | ["3"] | NoteIf you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously. | Java 8 | standard input | [
"bitmasks",
"brute force",
"fft"
] | 11870e3fb1aaad9bf15dc505aa9cd0f5 | The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold. The second line contains string S. The third line contains string T. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'. | 2,500 | Print a single number — the number of occurrences of T in S with the error threshold k by the given definition. | standard output | |
PASSED | ef6af3a6f66cc85e86b43bf97f80bf57 | train_000.jsonl | 1426610700 | Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'.Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.Let's write down integer k ≥ 0 — the error threshold. We will say that string T occurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(in, out);
out.close();
}
}
class TaskE {
private final String ACTG = "ACTG";
public void solve(InputReader in, OutputWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
String word = in.next();
String pattern = in.next();
int wordArray[][] = new int[n+1][4];
for(int i=0;i<n;i++) {
wordArray[Math.max(0,i-k)][ACTG.indexOf(word.charAt(i))]++;
wordArray[Math.min(i+k+1, n)][ACTG.indexOf(word.charAt(i))]--;
}
BitSet[] wordBits = new BitSet[4];
for (int i = 0; i < 4; ++i) {
wordBits[i] = new BitSet(n);
}
BitSet result = calculateResultBitSet(n, wordArray, wordBits);
int index;
for (int i = 0; i < m; ++i) {
index = ACTG.indexOf(pattern.charAt(i));
result.and(wordBits[index].get(i, n));
}
out.printLine(result.cardinality());
}
private BitSet calculateResultBitSet(int n, int[][] wordArray, BitSet[] wordBits) {
BitSet ans = new BitSet(n);
for(int i=0;i<n;i++) {
for(int j=0;j<4;j++) {
if(wordArray[i][j] > 0) wordBits[j].set(i);
if(i+1 < n) {
wordArray[i+1][j] += wordArray[i][j];
}
}
ans.set(i);
}
return ans;
}
}
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 boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
String line = reader.readLine();
if ( line == null ) {
return false;
}
tokenizer = new StringTokenizer(line);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() { return Double.parseDouble(next());}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if ( i != 0 ) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
} | Java | ["10 4 1\nAGCAATTCAT\nACAT"] | 3 seconds | ["3"] | NoteIf you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously. | Java 8 | standard input | [
"bitmasks",
"brute force",
"fft"
] | 11870e3fb1aaad9bf15dc505aa9cd0f5 | The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold. The second line contains string S. The third line contains string T. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'. | 2,500 | Print a single number — the number of occurrences of T in S with the error threshold k by the given definition. | standard output | |
PASSED | eec6e78336d9ee655ad93a0e4b14cf35 | train_000.jsonl | 1426610700 | Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'.Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.Let's write down integer k ≥ 0 — the error threshold. We will say that string T occurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class FuzzySearch {
public static void fft(double[] a, double[] b, boolean invert) {
int count = a.length;
for (int i = 1, j = 0; i < count; i++) {
int bit = count >> 1;
for (; j >= bit; bit >>= 1) j -= bit;
j += bit;
if (i < j) {
double temp = a[i];
a[i] = a[j];
a[j] = temp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
for (int len = 2; len <= count; len <<= 1) {
int halfLen = len >> 1;
double angle = 2 * Math.PI / len;
if (invert) angle = -angle;
double wLenA = Math.cos(angle), wLenB = Math.sin(angle);
for (int i = 0; i < count; i += len) {
double wA = 1, wB = 0;
for (int j = 0; j < halfLen; j++) {
double uA = a[i + j], uB = b[i + j];
double vA = a[i + j + halfLen] * wA - b[i + j + halfLen] * wB;
double vB = a[i + j + halfLen] * wB + b[i + j + halfLen] * wA;
a[i + j] = uA + vA;
b[i + j] = uB + vB;
a[i + j + halfLen] = uA - vA;
b[i + j + halfLen] = uB - vB;
double nextWA = wA * wLenA - wB * wLenB;
wB = wA * wLenB + wB * wLenA;
wA = nextWA;
}
}
}
if (invert) {
for (int i = 0; i < count; i++) {
a[i] /= count;
b[i] /= count;
}
}
}
public static long[] multiply(long[] a, long[] b) {
int resultSize = Integer.highestOneBit(Math.max(a.length, b.length) - 1) << 2;
resultSize = Math.max(resultSize, 1);
double[] aReal = new double[resultSize], aImaginary = new double[resultSize];
double[] bReal = new double[resultSize], bImaginary = new double[resultSize];
for (int i = 0; i < a.length; i++) aReal[i] = a[i];
for (int i = 0; i < b.length; i++) bReal[i] = b[i];
fft(aReal, aImaginary, false);
if (a == b)
{
System.arraycopy(aReal, 0, bReal, 0, aReal.length);
System.arraycopy(aImaginary, 0, bImaginary, 0, aImaginary.length);
}
else fft(bReal, bImaginary, false);
for (int i = 0; i < resultSize; i++) {
double real = aReal[i] * bReal[i] - aImaginary[i] * bImaginary[i];
aImaginary[i] = aImaginary[i] * bReal[i] + bImaginary[i] * aReal[i];
aReal[i] = real;
}
fft(aReal, aImaginary, true);
long[] result = new long[resultSize];
for (int i = 0; i < resultSize; i++) result[i] = Math.round(aReal[i]);
return result;
}
static char[] letters = "ACGT".toCharArray();
static int[] letterIds = new int[256];
public static void main(String[] args) throws IOException {
for(int i=0;i<letters.length;i++) {
letterIds[letters[i]] = i;
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int SC = Integer.parseInt(st.nextToken());
int TC = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
String S = br.readLine(), T = br.readLine();
long[][] eq1s = new long[4][SC];
long[][] eq2s = new long[4][TC];
int[][] dels = new int[4][SC];
for(int i=0;i<SC;i++) {
char c = S.charAt(i);
int id = letterIds[c];
int start = Math.max(0, i-k);
int end = i+k+1;
dels[id][start] += 1;
if(end < SC)
dels[id][end] -= 1;
}
int[] curDels = new int[4];
for(int i=0;i<SC;i++) {
for(int j=0;j<4;j++) {
curDels[j] += dels[j][i];
if(curDels[j] > 0) {
eq1s[j][i] = 1;
}
}
}
for(int i=0;i<TC;i++) {
char c = T.charAt(i);
int id = letterIds[c];
eq2s[id][TC-1-i] = 1;
}
long[][] polys = new long[4][];
for(int i=0;i<4;i++) {
polys[i] = multiply(eq1s[i], eq2s[i]);
}
long ans = 0;
for(int i=0;i<polys[0].length;i++) {
long cnt = 0;
for(int j=0;j<4;j++)
cnt += polys[j][i];
if(cnt == TC) {
// System.out.println(i-(TC-1)+1);
ans++;
}
}
System.out.println(ans);
}
}
| Java | ["10 4 1\nAGCAATTCAT\nACAT"] | 3 seconds | ["3"] | NoteIf you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously. | Java 8 | standard input | [
"bitmasks",
"brute force",
"fft"
] | 11870e3fb1aaad9bf15dc505aa9cd0f5 | The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold. The second line contains string S. The third line contains string T. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'. | 2,500 | Print a single number — the number of occurrences of T in S with the error threshold k by the given definition. | standard output | |
PASSED | 0b32c689411a467b3d842616a4ac1417 | train_000.jsonl | 1426610700 | Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'.Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.Let's write down integer k ≥ 0 — the error threshold. We will say that string T occurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class FuzzySearch {
static char[] letters = "ACGT".toCharArray();
static int[] letterIds = new int[256];
public static void main(String[] args) throws IOException {
for(int i=0;i<letters.length;i++) {
letterIds[letters[i]] = i;
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int SC = Integer.parseInt(st.nextToken());
int TC = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
String S = br.readLine(), T = br.readLine();
long[][] eq1s = new long[4][SC];
long[][] eq2s = new long[4][TC];
int[][] dels = new int[4][SC];
for(int i=0;i<SC;i++) {
char c = S.charAt(i);
int id = letterIds[c];
int start = Math.max(0, i-k);
int end = i+k+1;
dels[id][start] += 1;
if(end < SC)
dels[id][end] -= 1;
}
int[] curDels = new int[4];
for(int i=0;i<SC;i++) {
for(int j=0;j<4;j++) {
curDels[j] += dels[j][i];
if(curDels[j] > 0) {
eq1s[j][i] = 1;
}
}
}
for(int i=0;i<TC;i++) {
char c = T.charAt(i);
int id = letterIds[c];
eq2s[id][TC-1-i] = 1;
}
long[][] polys = new long[4][];
for(int i=0;i<4;i++) {
polys[i] = FFT.multiply(eq1s[i], eq2s[i]);
}
long ans = 0;
for(int i=0;i<polys[0].length;i++) {
long cnt = 0;
for(int j=0;j<4;j++)
cnt += polys[j][i];
if(cnt == TC) {
ans++;
}
}
System.out.println(ans);
}
// SET maxk appropriately!!! ~log(maxn) //%
static class FFT {
static final int maxk = 19, maxn = (1 << maxk) + 1;
// Init: wR, wI, rR, rI, aR, aI to new double[maxn] !!!
//#
static double[] wR = new double[maxn],
wI = new double[maxn],
rR = new double[maxn],
rI = new double[maxn],
aR = new double[maxn],
aI = new double[maxn]; //$
static int n, k, lastk = -1, dp[] = new int[maxn];
static void fft(boolean inv) {
if (lastk != k) {
lastk = k; dp[0] = 0;
for (int i = 1, g = -1; i < n; i++) {
if ((i & (i - 1)) == 0) g++;
dp[i] = dp[i ^ (1 << g)] ^ (1 << (k - 1 - g));
}
wR[1] = 1;
wI[1] = 0;
for (int t = 0; t < k - 1; t++) {
double a = Math.PI / n * (1 << (k - 1 - t));
double curR = Math.cos(a), curI = Math.sin(a);
int p2 = (1 << t), p3 = p2 * 2;
for (int j = p2, k = j * 2; j < p3; j++, k += 2) {
wR[k] = wR[j];
wI[k] = wI[j];
wR[k + 1] = wR[j] * curR - wI[j] * curI;
wI[k + 1] = wR[j] * curI + wI[j] * curR;
}
}
}
for (int i = 0; i < n; i++) {
int d = dp[i];
if (i < d) {
double tmp = aR[i];
aR[i] = aR[d];
aR[d] = tmp;
tmp = aI[i];
aI[i] = aI[d];
aI[d] = tmp;
}
}
if (inv) for (int i = 0; i < n; i++) aI[i] = -aI[i];
for (int len = 1; len < n; len <<= 1) {
for (int i = 0; i < n; i += len) {
int wit = len;
for (int it = 0, j = i + len; it < len; it++, i++, j++) {
double tmpR = aR[j] * wR[wit] - aI[j] * wI[wit];
double tmpI = aR[j] * wI[wit] + aI[j] * wR[wit];
wit++;
aR[j] = aR[i] - tmpR;
aI[j] = aI[i] - tmpI;
aR[i] += tmpR;
aI[i] += tmpI;
}
}
}
}
static long[] multiply(long[] a, long[] b) {
int na = a.length, nb = b.length;
for (k = 0, n = 1; n < na + nb - 1; n <<= 1, k++) {}
for (int i = 0; i < n; ++i) {
aR[i] = i < na ? a[i] : 0;
aI[i] = i < nb ? b[i] : 0;
}
fft(false);
aR[n] = aR[0];
aI[n] = aI[0];
double q = -1.0 / n / 4.0;
for (int i = 0; i <= n - i; ++i) {
double tmpR = aR[i] * aR[i] - aI[i] * aI[i];
double tmpI = aR[i] * aI[i] * 2;
tmpR -= aR[n - i] * aR[n - i] - aI[n - i] * aI[n - i];
tmpI -= aR[n - i] * aI[n - i] * -2;
aR[i] = -tmpI * q;
aI[i] = tmpR * q;
aR[n - i] = aR[i];
aI[n - i] = -aI[i];
}
fft(true);
long[] ans = new long[n = na + nb - 1]; // ONLY MOD IF NEEDED
for (int i = 0; i < n; i++) ans[i] = Math.round(aR[i]);
return ans;
}
static void fft2(double[][] xr, double[][] xi, boolean inv) {
n = xr[0].length;
k = Integer.numberOfTrailingZeros(n);
for (int i = 0; i < xr.length; i++) {
for (int j = 0; j < n; j++) { aR[j] = xr[i][j]; aI[j] = xi[i][j]; }
fft(inv);
for (int j=0;j<n;j++){xr[i][j] = aR[j] / (inv ? n : 1); xi[i][j] = aI[j] / (inv ? -n : 1);}
}
n = xr.length;
k = Integer.numberOfTrailingZeros(n);
for (int j = 0; j < xr[0].length; j++) {
for (int i = 0; i < n; i++) { aR[i] = xr[i][j]; aI[i] = xi[i][j]; }
fft(inv);
for (int i=0;i<n;i++){xr[i][j] = aR[i] / (inv ? n : 1); xi[i][j] = aI[i] / (inv ? -n : 1);}
}
}
static long[][] multiply2(long[][] a, long[][] b) {
int n1, n2;
for (n1 = 1; n1 < a.length + b.length - 1; n1 <<= 1) {}
for (n2 = 1; n2 < a[0].length + b[0].length - 1; n2 <<= 1) {}
double[][] ar = new double[n1][n2], ai = new double[n1][n2];
double[][] br = new double[n1][n2], bi = new double[n1][n2];
for (int i = 0; i < a.length; i++) for(int j=0;j<a[i].length;j++) ar[i][j] = a[i][j];
for (int i = 0; i < b.length; i++) for(int j=0;j<b[i].length;j++) br[i][j] = b[i][j];
fft2(ar,ai,false); fft2(br,bi,false);
for (int i = 0; i < n1; i++) {
for(int j = 0; j < n2; j++) {
double r1 = ar[i][j], r2 = br[i][j];
double i1 = ai[i][j], i2 = bi[i][j];
double real = r1 * r2 - i1 * i2;
ai[i][j] = i1 * r2+ i2*r1;
ar[i][j] = real;
}
}
fft2(ar,ai,true); long[][] result = new long[n1=a.length+b.length-1][n2=a[0].length+b[0].length-1];
for (int i = 0; i < n1; i++)
for(int j = 0; j < n2; j++) result[i][j] = Math.round(ar[i][j]);
return result;
}
}
} | Java | ["10 4 1\nAGCAATTCAT\nACAT"] | 3 seconds | ["3"] | NoteIf you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously. | Java 8 | standard input | [
"bitmasks",
"brute force",
"fft"
] | 11870e3fb1aaad9bf15dc505aa9cd0f5 | The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold. The second line contains string S. The third line contains string T. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'. | 2,500 | Print a single number — the number of occurrences of T in S with the error threshold k by the given definition. | standard output | |
PASSED | a065e826095a541f7100cf5577736890 | train_000.jsonl | 1426610700 | Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'.Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.Let's write down integer k ≥ 0 — the error threshold. We will say that string T occurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class FuzzySearch {
static char[] letters = "ACGT".toCharArray();
static int[] letterIds = new int[256];
public static void main(String[] args) throws IOException {
for(int i=0;i<letters.length;i++) {
letterIds[letters[i]] = i;
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int SC = Integer.parseInt(st.nextToken());
int TC = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
String S = br.readLine(), T = br.readLine();
long[][] eq1s = new long[4][SC];
long[][] eq2s = new long[4][TC];
int[][] dels = new int[4][SC];
for(int i=0;i<SC;i++) {
char c = S.charAt(i);
int id = letterIds[c];
int start = Math.max(0, i-k);
int end = i+k+1;
dels[id][start] += 1;
if(end < SC)
dels[id][end] -= 1;
}
int[] curDels = new int[4];
for(int i=0;i<SC;i++) {
for(int j=0;j<4;j++) {
curDels[j] += dels[j][i];
if(curDels[j] > 0) {
eq1s[j][i] = 1;
}
}
}
for(int i=0;i<TC;i++) {
char c = T.charAt(i);
int id = letterIds[c];
eq2s[id][TC-1-i] = 1;
}
long[][] polys = new long[4][];
for(int i=0;i<4;i++) {
polys[i] = FFT_Precise.mult(eq1s[i], eq2s[i]);
}
long ans = 0;
for(int i=0;i<polys[0].length;i++) {
long cnt = 0;
for(int j=0;j<4;j++)
cnt += polys[j][i];
if(cnt == TC) {
// System.out.println(i-(TC-1)+1);
ans++;
}
}
System.out.println(ans);
}
static class FFT_Precise {
static final int maxk = 21, maxn = (1 << maxk) + 1; // maxk ~ log(maxn)
static double[] ws_r = new double[maxn];
static double[] ws_i = new double[maxn];
static int[] dp = new int[maxn];
static double[] rs_r = new double[maxn];
static double[] rs_i = new double[maxn];
static int n, k;
static int lastk = -1;
static void fft(boolean rev) {
if (lastk != k) {
lastk = k;
dp[0] = 0;
for (int i = 1, g = -1; i < n; ++i) {
if ((i & (i - 1)) == 0) {
++g;
}
dp[i] = dp[i ^ (1 << g)] ^ (1 << (k - 1 - g));
}
ws_r[1] = 1;
ws_i[1] = 0;
for (int two = 0; two < k - 1; ++two) {
double alf = Math.PI / n * (1 << (k - 1 - two));
double cur_r = Math.cos(alf), cur_i = Math.sin(alf);
int p2 = (1 << two), p3 = p2 * 2;
for (int j = p2; j < p3; ++j) {
ws_r[j * 2] = ws_r[j];
ws_i[j * 2] = ws_i[j];
ws_r[j * 2 + 1] = ws_r[j] * cur_r - ws_i[j] * cur_i;
ws_i[j * 2 + 1] = ws_r[j] * cur_i + ws_i[j] * cur_r;
}
}
}
for (int i = 0; i < n; ++i) {
if (i < dp[i]) {
double ar = a_r[i];
double ai = a_i[i];
a_r[i] = a_r[dp[i]];
a_i[i] = a_i[dp[i]];
a_r[dp[i]] = ar;
a_i[dp[i]] = ai;
}
}
if (rev) {
for (int i = 0; i < n; ++i) {
a_i[i] = -a_i[i];
}
}
for (int len = 1; len < n; len <<= 1) {
for (int i = 0; i < n; i += len) {
int wit = len;
for (int it = 0, j = i + len; it < len; ++it, ++i, ++j) {
double tmp_r = a_r[j] * ws_r[wit] - a_i[j] * ws_i[wit];
double tmp_i = a_r[j] * ws_i[wit] + a_i[j] * ws_r[wit];
wit++;
a_r[j] = a_r[i] - tmp_r;
a_i[j] = a_i[i] - tmp_i;
a_r[i] += tmp_r;
a_i[i] += tmp_i;
}
}
}
}
static double[] a_r = new double[maxn];
static double[] a_i = new double[maxn];
static long[] mult(long[] _a, long[] _b) {
int na = _a.length, nb = _b.length;
for (k = 0, n = 1; n < na + nb - 1; n <<= 1, ++k)
;
for (int i = 0; i < n; ++i) {
a_r[i] = i < na ? _a[i] : 0;
a_i[i] = i < nb ? _b[i] : 0;
}
fft(false);
a_r[n] = a_r[0];
a_i[n] = a_i[0];
double r_i = -1.0d / n / 4.0d;
for (int i = 0; i <= n - i; ++i) {
double tmp_r = a_r[i] * a_r[i] - a_i[i] * a_i[i];
double tmp_i = a_r[i] * a_i[i] * 2.0;
double tmp2_r = a_r[n - i] * a_r[n - i] - a_i[n - i] * a_i[n - i];
double tmp2_i = a_r[n - i] * a_i[n - i] * -2.0;
tmp_r = tmp_r - tmp2_r;
tmp_i = tmp_i - tmp2_i;
a_r[i] = -tmp_i * r_i;
a_i[i] = tmp_r * r_i;
a_r[n - i] = a_r[i];
a_i[n - i] = -a_i[i];
}
fft(true);
int res = 0;
long[] ans = new long[maxn];
for (int i = 0; i < n; ++i) {
long val = (long) Math.round(a_r[i]);
if (val != 0) {
while (res < i) {
ans[res++] = 0;
}
ans[res++] = val;
}
}
return ans;
}
}
} | Java | ["10 4 1\nAGCAATTCAT\nACAT"] | 3 seconds | ["3"] | NoteIf you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously. | Java 8 | standard input | [
"bitmasks",
"brute force",
"fft"
] | 11870e3fb1aaad9bf15dc505aa9cd0f5 | The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold. The second line contains string S. The third line contains string T. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'. | 2,500 | Print a single number — the number of occurrences of T in S with the error threshold k by the given definition. | standard output | |
PASSED | 280f237abbac77b1f6b432c659c27507 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long x=sc.nextLong();
long a[]=new long[2*n+1];
for(int i=1;i<=n;i++){a[i]=sc.nextLong();a[n+i]=a[i];}
long b[]=new long[2*n+1];
long c[]=new long[2*n+1];
for(int i=1;i<=2*n;i++)b[i]=b[i-1]+a[i];
for(int i=1;i<=2*n;i++)c[i]=c[i-1]+(a[i]*(a[i]+1))/2;
long ans=0l;
for(int i=2*n;i>=1;i--){
int box=bs(b,x,i,1);
//System.out.println(i+" "+box);
long cnt=0l;
if((b[i]-b[box-1])>=x){
long ans1=c[i]-c[box];
long val1=a[box]*(a[box]+1)/2;
long too=((b[i]-b[box-1])-x);
long val2=val1-too*(too+1)/2;
cnt=ans1+val2;
//System.out.println(ans1+" "+val1+" "+too+" "+val2+" "+cnt);
}
//System.out.println("cnt "+cnt);
ans=Math.max(ans,cnt);
}
System.out.println(ans);
}
public static int bs(long a[],long val,int st,int end){
int ans=1;
int idx=st;
while(st>=end){
int mid=(st+end)>>1;
if(a[idx]-a[mid-1]>=val){
ans=mid;
end=mid+1;
}else{
st=mid-1;
}
}
return ans;
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | af12845f0f8427470260237eeafba309 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef{
static long a[];
static long mv[];
static long cd[];
static long tdv[];
static int n;
static long k;
public static void main (String[] args)throws IOException {
try{
//BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
//int t=Integer.parseInt(br.readLine());
int t=1;
// int t=sc.nextInt();
while(t-->0){
//int n=Integer.parseInt(br.readLine())
//int m = sc.nextInt();
//long d=Long.parseLong(br.readLine());
//String[] s= br.readLine().trim().split(" ");
// Map<Integer,Integer> m = new TreeMap<Integer,Integer>();
//List<Character> f = new ArrayList<Character>();
// List<Integer> s = new ArrayList<Integer>();
//<Integer> v= new Vector<Integer>();
//Set<Integer> s= new HashSet<Integer>();
input();
long ans=0;
for(int i=2*n;i>=n+1;i--){
// System.out.println(i);
ans=Math.max(ans,bs(i));
}
System.out.println(ans);
}
}
catch(Exception e){
}
}
public static void input(){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextLong();
a = new long[n*2+1];
cd = new long[n*2+1];
tdv= new long[n*2+1];
mv = new long[n*2+1];
a[0]=0;
mv[0]=0;
for(int i=1;i<=n;i++){
a[i]=sc.nextLong();
mv[i]=a[i]*(a[i]+1)/2;
a[n+i]=a[i];
mv[n+i]=mv[i];
}
cd[0]=a[0];
tdv[0]=mv[0];
for(int i=1;i<=2*n;i++){
cd[i]=cd[i-1]+a[i];
tdv[i]=tdv[i-1]+mv[i];
}
}
public static long bs(int x){
int ans =x;
int lb=0,ub=x;
while(lb<=ub){
int mid=(lb+ub)/2;
if(cd[x]-cd[mid]<k){
ans = mid;
ub =mid-1;
}
else lb=mid+1;
}
long val = tdv[x]-tdv[ans-1];
long xtrad = cd[x]-cd[ans-1]-k;
val = val-(xtrad*(xtrad+1)/2);
return val;
}
}
class pair{
int x,y,ind;
pair(int n,int m,int id){
x=n;
y=m;
ind=id;
}
}
class comp1 implements Comparator<pair>{
public int compare(pair p1,pair p2){
if(p1.x>p2.x)return -1;
else return 1;
}
}
class comp2 implements Comparator<pair>{
public int compare(pair p1,pair p2){
if(p1.y<p2.y)return -1;
else return 1;
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | dca05b7f1b663eddcdc06fc2da30c124 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static int bs(long d, long[] arr) {
int start = 0, end = arr.length - 1, ret = -1;
while(start <= end) {
int mid = (start + end) >> 1;
if(d <= arr[mid]) {
ret = mid;
end = mid - 1;
}else {
start = mid + 1;
}
}
return ret;
}
static long sum(long x) {
return x * (x + 1) >> 1;
}
static long solve(int start, int end, long[] cost) {
if(start <= end) {
return cost[end] - cost[start];
}
return cost[end] + cost[cost.length - 1] - cost[start];
}
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
long d = sc.nextLong();
long arr[] = new long[n], cost[] = new long[n], days[] = new long[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
cost[i] = i == 0 ? sum(arr[i]) : sum(arr[i]) + cost[i - 1];
days[i] = i == 0 ? arr[i] : arr[i] + days[i - 1];
}
long ans = 0;
for(int i = 0; i < n; i++) {
long start = days[i] - d + 1;
start += start <= 0 ? days[n - 1] : 0;
int idx = bs(start, days);
long cur = sum(arr[idx]) - sum(arr[idx] - (days[idx] - start + 1));
cur += solve(idx,i,cost);
ans = Math.max(ans, cur);
}
out.println(ans);
out.close();
}
}
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 Long nextLong() throws IOException {
return Long.parseLong(next());
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 9ff03a81e722d35576bc974b8b6cf287 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Set;
public class D {
public void solve() {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int n = in.readInt();
long x = in.readLong();
long days[] = IOUtils.readLongArray(in, n);
long dayss[] = new long[2 * n + 5];
for (int i = 0; i < n; i++) {
dayss[i] = days[i];
}
for (int i = 0; i < n; i++) {
dayss[n + i] = days[i];
}
long ans = Long.MIN_VALUE;
long currDays = 0, hugs = 0;
int st = 0, stPoint = 0;
for (int i = 0; i < 2 * n; i++) {
if ( currDays + dayss[i] < x ) {
currDays += dayss[i];
hugs += findSum(0, dayss[i]);
} else if ( currDays + dayss[i] == x ) {
hugs += findSum(0, dayss[i]);
ans = Math.max(ans, hugs);
currDays += days[i];
} else {
long toBeDeducted = dayss[i] - (x - currDays);
currDays = x;
while (st < 2 * n && dayss[st] - stPoint < toBeDeducted) {
hugs -= findSum(stPoint, dayss[st]);
toBeDeducted -= dayss[st] - stPoint;
st++;
stPoint = 0;
}
if ( st < 2 * n && dayss[st] - stPoint >= toBeDeducted ) {
int prev = stPoint;
stPoint += toBeDeducted;
hugs -= findSum(prev, stPoint);
hugs += findSum(0, dayss[i]);
}
ans = Math.max(ans, hugs);
}
}
out.printLine(ans);
out.flush();
}
private long findSum(long x, long y) {
return (y * (y + 1)) / 2 - (x * (x + 1)) / 2;
}
public static void main(String[] args) {
D solver = new D();
solver.solve();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if ( numChars == -1 )
throw new InputMismatchException();
if ( curChar >= numChars ) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if ( numChars <= 0 )
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if ( c == '-' ) {
sgn = -1;
c = read();
}
int res = 0;
do {
if ( c < '0' || c > '9' )
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if ( c == '-' ) {
sgn = -1;
c = read();
}
long res = 0;
do {
if ( c < '0' || c > '9' )
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if ( i != 0 )
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static class IOUtils {
public static int[] readIntegerArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.readInt();
}
return array;
}
public static long[] readLongArray(InputReader in, int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = in.readLong();
}
return array;
}
public static List<Integer> readIntegerList(InputReader in, int size) {
List<Integer> set = new ArrayList<>();
for (int i = 0; i < size; i++) {
set.add(in.readInt());
}
return set;
}
public static Set<Integer> readIntegerSet(InputReader in, int size) {
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < size; i++) {
set.add(in.readInt());
}
return set;
}
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | fde71a3e15bd31f01d90e4131caa7ea5 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.util.*;
public class D {
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static MyScanner sc;
static {
try {
sc = new MyScanner();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
doTask();
out.flush();
}
public static void doTask(){
int n = sc.nextInt();
long x = sc.nextLong();
long[] d = new long[2*n];
long[] p = new long[2*n+1];
long[] sum = new long[n];
long[] psum = new long[2*n+1];
for (int i=0; i<n; i++) {
d[i] = d[i+n] = sc.nextInt();
p[i+1] = p[i]+d[i];
sum[i] = d[i]*(d[i]+1)/2;
psum[i+1] = psum[i] + sum[i];
}
for (int i=n; i<2*n; i++) {
p[i+1] = p[i]+d[i-n];
psum[i+1] = psum[i] + sum[i-n];
}
long max = 0;
for (int i=0; i<n; i++) {
long currentMax = 0;
if (d[i] >= x) {
long startFromInFirstMonth = d[i]-x+1;
currentMax = d[i] * (d[i] + 1) / 2 - startFromInFirstMonth * (startFromInFirstMonth - 1) / 2;
} else {
// from start
long before = p[i];
int l = i + 1, r = 2 * n;
while (l < r) {
int m = l + (r - l) / 2;
if (before+x>p[m-1] && before+x<=p[m]) {
l = m;
break;
}
if (before + x < p[m]) {
r = m - 1;
} else {
l = m + 1;
}
}
long leftInThisMonth = p[l] - p[i] - x;
long addDays = Math.min(leftInThisMonth, d[i] - 1);
long startFromInFirstMonth = 1 + addDays;
long daysInLastMonth = d[l - 1] - leftInThisMonth + addDays;
currentMax = psum[l - 1] - psum[i + 1] + (daysInLastMonth * (daysInLastMonth + 1) / 2) + (d[i] * (d[i] + 1) / 2 - startFromInFirstMonth * (startFromInFirstMonth - 1) / 2);
}
max = Math.max(currentMax, max);
}
out.println(max);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() throws FileNotFoundException {
br = System.getProperty("ONLINE_JUDGE") != null
? new BufferedReader(new InputStreamReader(System.in))
: new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 3ab9d08b5e529c22dbb21c8a1971fa68 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String s[] = (br.readLine()).split(" ");
int n = Integer.parseInt(s[0]);
long x = Long.parseLong(s[1]);
long z[] = new long[n + 1];
s = (br.readLine()).split(" ");
for(int i = 0; i < n; i++){
z[i + 1] = Long.parseLong(s[i]);
}
long a[] = new long[2*n + 1];
for(int i = 1; i <= n; i++){
a[i] = z[i];
}
for(int i = n + 1; i <= 2*n; i++){
a[i] = a[i - n];
}
long premonth[] = new long[2*n + 1];
premonth[1] = a[1];
for(int i = 2; i <= 2*n; i++){
premonth[i] = premonth[i - 1] + a[i];
}
long prehugs[] = new long[2*n + 1];
prehugs[1] = (a[1] * (a[1] + 1)) / 2;
for(int i = 2; i <= 2*n; i++){
prehugs[i] = prehugs[i - 1] + ((a[i] * (a[i] + 1)) / 2);
}
long ans = 0;
for(int i = 2*n; i >= 1; i--){
if(premonth[i] >= x){
int low = 1, high = i;
long temp = x;
while(low <= high){
int mid = (low + high) / 2;
if(premonth[high] - premonth[mid - 1] == temp) {high = mid; break;}
else if(premonth[high] - premonth[mid - 1] > temp) low = mid + 1;
else{
temp -= (premonth[high] - premonth[mid - 1]);
high = mid - 1;
}
}
long dif = premonth[i] - premonth[high - 1] - x;
dif = (dif * (dif + 1)) / 2;
long tempans = prehugs[i] - prehugs[high - 1] - dif;
if(ans < tempans) ans = tempans;
}
else break;
}
System.out.println(ans);
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 55506a85ece2a0d564dcabc6faac75a2 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | /*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class D
{
public static void main(String omkar[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
long X = Long.parseLong(st.nextToken());
int[] arr = new int[2*N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = arr[i+N] = Integer.parseInt(st.nextToken());
long res = 0L;
long days = 0L;
long hugs = 0L;
int right = 0;
for(int i=0; i < N; i++)
{
while(days <= X)
{
res = Math.max(res, hugs);
hugs += bp(arr[right]);
days += arr[right];
right++;
}
if(days-arr[i] <= X)
res = Math.max(res, hugs-bp(days-X));
hugs -= bp(arr[i]);
days -= arr[i];
}
System.out.println(res);
}
public static long bp(long x)
{
return x*(x+1)/2;
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 01811315ffd18a86afed5a2616ba6279 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | //package 第1358场;
/* 盗图小能手
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/
import java.io.IOException;
import java.util.Scanner;
/**
* 首先知道在右端点一定屁股上。
* sumDays前缀和可以找包含的区间,控制左右指针使得多出来的天数在1个月内
* sumHugs前缀和-多出来的区间和 => 更新ans。
* <p>
* 多出来的区间y=包含区间-x,
* 多出来的区间和 =(y+1)*y/2
* <p>
* 比赛的时候没写出来,真是可惜啊
*/
public class D {
public static void fun(long x, long[] array) {
long[] sumDays = new long[array.length + 1];
long[] sumHugs = new long[array.length + 1];
for (int i = 0; i < array.length; i++) {
sumDays[i + 1] = sumDays[i] + array[i];
sumHugs[i + 1] = sumHugs[i] + array[i] * (array[i] + 1) / 2;
}
long ans = 0;
// 从右往左枚举,也不知道是不是好写一点,反正我就这么写了
// 从左到右的话,可以把前缀和给省掉,换尺取法
int left = array.length;
int right = array.length;
while (left > 0) {
while (sumDays[right] - sumDays[left] < x && left > 0) {
left--;
}
if (sumDays[right] - sumDays[left] >= x) {
long dec = sumDays[right] - sumDays[left] - x;
long res = sumHugs[right] - sumHugs[left] - dec * (dec + 1) / 2;
ans = Math.max(ans, res);
}
right--;
}
System.out.println(ans);
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long x = sc.nextLong();
// 年复一年,用两倍哟,并且x限制小于1年天数啦了。
long[] array = new long[2 * n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
array[n + i] = array[i];
}
fun(x, array);
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 3bd58a863e0e8c8493fcc1e1bb021dc4 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
static int MOD = 1000000007;
public static void main(String[] args) throws IOException {
Main m = new Main();
m.solve();
m.close();
}
void close() throws IOException {
pw.flush();
pw.close();
br.close();
}
int ri() throws IOException {
return Integer.parseInt(br.readLine());
}
long rl() throws IOException {
return Long.parseLong(br.readLine());
}
int[] ril() throws IOException {
String[] tokens = br.readLine().split(" ");
int[] A = new int[tokens.length];
for (int i = 0; i < A.length; i++)
A[i] = Integer.parseInt(tokens[i]);
return A;
}
long[] rll() throws IOException {
String[] tokens = br.readLine().split(" ");
long[] A = new long[tokens.length];
for (int i = 0; i < A.length; i++)
A[i] = Long.parseLong(tokens[i]);
return A;
}
void solve() throws IOException {
long[] nx = rll();
int n = (int) nx[0];
long x = nx[1];
int[] d = ril();
// observation: optimal solution starts or ends on the start/end
// of a month
// allows queries for the number of hugs between days i and j
// of a single month
long[] prefix = new long[1000001];
for (int i = 1; i <= 1000000; i++) {
prefix[i] = prefix[i-1] + i;
}
// allows querying #days between months i and j, inclusive
// long[] dprefix = new long[n+1];
// for (int i = 1; i <= n; i++) {
// dprefix[i] = dprefix[i-1] + d[i-1];
// }
long ans = 0;
long curr = 1;
int L = 0;
int l = 1;
int R = 0;
int r = 1;
long days = 1;
while (days < x) {
if (days + d[R] <= x) {
days += d[R];
curr += query(prefix, 1, d[R]); // note the 1 is for the next month
R++;
} else {
int left = (int) (x - days);
days += left;
curr += query(prefix, 2, left + 1);
r += left;
}
}
ans = curr;
do {
int deltal = d[L] - l;
int deltar = d[R] - r;
if (deltal == 0 || deltar == 0) {
curr -= l;
if (deltal == 0) {
L = (L + 1) % n;
l = 1;
} else {
l++;
}
if (deltar == 0) {
R = (R + 1) % n;
r = 1;
} else {
r++;
}
curr += r;
} else {
int delta = Math.min(deltal, deltar);
curr -= query(prefix, l, l + delta - 1);
curr += query(prefix, r+1, r + delta);
l += delta;
r += delta;
}
ans = Math.max(ans, curr);
} while (!(L == 0 && l == 1));
pw.println(ans);
}
long query(long[] prefix, int i, int j) {
return prefix[j] - prefix[i-1];
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 7d0ceb46e6a940f7653f400ec3fa952b | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class d645 implements Runnable{
public static void main(String[] args) {
try{
new Thread(null, new d645(), "process", 1<<26).start();
}
catch(Exception e){
System.out.println(e);
}
}
public void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//PrintWriter out = new PrintWriter("file.out");
Task solver = new Task();
//int t = scan.nextInt();
int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
static final int inf = Integer.MAX_VALUE;
public void solve(int testNumber, FastReader sc, PrintWriter out) {
int N = sc.nextInt();
long X = sc.nextLong();
long[] hugsInMonth = new long[N*2];
int[] days = new int[N*2];
for(int i = 0; i < N; i++) {
days[i] = sc.nextInt();
days[i + N] = days[i];
hugsInMonth[i] = ((long)days[i] * (days[i]+1)) / 2;
hugsInMonth[i + N] = ((long)days[i] * (days[i]+1)) / 2;
}
long[] pSum = new long[N*2];
long[] pSumHugs = new long[N*2];
pSum[0] = days[0];
pSumHugs[0] = hugsInMonth[0];
for(int i = 1; i < pSum.length; i++) {
pSum[i] = pSum[i-1] + days[i];
pSumHugs[i] = pSumHugs[i-1] + hugsInMonth[i];
}
//System.out.println(Arrays.toString(hugsInMonth));
//System.out.println(Arrays.toString(pSumHugs));
long max = 0;
for(int i = N; i < pSum.length; i++) {
// binary search for the smallest possible month j in which the days passed between j and i is less than X
int lo = i - N + 1, hi = i;
while(lo < hi) {
int mid = (lo + hi) / 2;
if(pSum[i] - pSum[mid-1] < X) { // days passed between i and j
hi = mid-1;
} else {
lo = mid+1;
}
}
while(pSum[i] - pSum[lo] < X) {
lo--;
}
while(pSum[i] - pSum[lo] >= X) {
lo++;
}
//System.out.println("Start in month" + (lo) + " and end in month " + (i));
// sum the number of hugs between month lo + 1 and month i
long sum = pSumHugs[i] - pSumHugs[lo];
// find the value of the "leftover" days in previous month
long leftover = X - (pSum[i] - pSum[lo]);
long excl = days[lo] - leftover;
long val = hugsInMonth[lo] - (excl * (excl + 1) / 2);
//System.out.println(val + sum);
max = Math.max(max, val + sum);
}
out.println(max);
}
}
static long binpow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
if ((b & 1) == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
static void sort(int[] x){
shuffle(x);
Arrays.sort(x);
}
static void sort(long[] x){
shuffle(x);
Arrays.sort(x);
}
static class tup implements Comparable<tup>, Comparator<tup>{
int a, b;
tup(int a,int b){
this.a=a;
this.b=b;
}
public tup() {
}
@Override
public int compareTo(tup o){
return Integer.compare(b,o.b);
}
@Override
public int compare(tup o1, tup o2) {
return Integer.compare(o1.b, o2.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
tup other = (tup) obj;
return a==other.a && b==other.b;
}
@Override
public String toString() {
return a + " " + b;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 022b4ee458dfdc10aae9fc2ad78dd9a7 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;
public class D {
//Solution by Sathvik Kuthuru
public static void main(String[] args) {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
int t = 1;
for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out);
out.close();
}
static class Task {
public void solve(int testNumber, FastReader scan, PrintWriter out) {
int n = scan.nextInt();
long x = scan.nextLong();
long[] a = new long[2 * n], sum = new long[n * 2], hug = new long[n * 2];
for(int i = 0; i < 2 * n; i++) {
a[i] = i < n ? scan.nextLong() : a[i - n];
sum[i] = a[i];
hug[i] = a[i] * (1 + a[i]) / 2;
if(i > 0) {
sum[i] += sum[i - 1];
hug[i] += hug[i - 1];
}
}
long best = 0;
long subDay = 0, subHug = 0;
for(int i = 0; i < n; i++) {
int low = i, high = 2 * n - 1, ans = -1;
while(low <= high) {
int mid = (low + high) / 2;
if(sum[mid] - subDay <= x) {
ans = mid;
low = mid + 1;
}
else high = mid - 1;
}
if(ans == -1) {
best = Math.max(best, get(a[i]) - get(a[i] - x));
}
else {
long left = x - (sum[ans] - subDay);
long extra = ans == 2 * n - 1 ? 0 : Math.min(a[i] - 1, a[ans + 1] - left);
long curr = hug[ans] - subHug - get(extra) + (ans == 2 * n - 1 ? 0 : get(left + extra));
best = Math.max(best, curr);
}
subDay = sum[i];
subHug = hug[i];
}
out.println(best);
}
long stupid(long[] a, int v) {
ArrayList<Long> d = new ArrayList<>();
for(long x : a) {
for(long i = 1; i <= x; i++) d.add(i);
}
long best = 0;
for(int i = 0; i < d.size(); i++) {
int next = Math.min(d.size() - 1, i + v - 1);
long curr = 0;
for(int j = i; j <= next; j++) curr += d.get(j);
best = Math.max(best, curr);
}
return best;
}
long get(long val) {
return val * (val + 1) / 2;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | dc9feabf4986c8dc930f6ffacb825ca1 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
}
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
public static FastReader sc = new FastReader();
static int mod = (int) (1e9+7),MAX=(int) (2e5);
static List<Integer>[] edges;
public static void main(String[] args){
int n = sc.nextInt();
long x = sc.nextLong();
long[] d = new long[2*n+1];
long[] pre = new long[2*n+1];
long[] sum = new long[2*n+1];
for(int i=1;i<=n;++i) {
d[i+n] = d[i] = sc.nextLong();
}
for(int i=1;i<=2*n;++i) {
pre[i] = pre[i-1] + d[i];
sum[i] = sum[i-1] + get(d[i]);
}
int j = 1;
long max = 0;
for(int i=1;i<=n;++i) {
while(j <= 2*n && pre[j]-pre[i-1] < x) ++j;
long dis = pre[j]-pre[i-1];
long val = sum[j]-sum[i-1];
long suff = dis-x;
long pref = d[j] - suff;
val-=get(d[j])-get(pref);
max = Math.max(max, val);
long shift = Math.min(d[i]-1,suff);
max = Math.max(max,val+shift*pref);
}
out.print(max);
out.close();
}
private static long get(long l) {
return (l*(l+1))/2;
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | ca8f2d000850662dd0aa9906d48023b6 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
}
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
public static FastReader sc = new FastReader();
static int mod = (int) (1e9+7),MAX=(int) (2e5);
static List<Integer>[] edges;
public static void main(String[] args){
int n = sc.nextInt();
long x = sc.nextLong();
long[] d = new long[2*n];
long[] p = new long[2*n];
long[] sum = new long[2*n];
TreeMap<Long,Integer> map = new TreeMap<>();
for(int i=0;i<n;++i) {
d[i] = sc.nextInt();
d[i+n] = d[i];
}
p[0] = d[0];
sum[0] = (d[0]*(d[0]+1))/2;
map.put(p[0],0);
for(int i=1;i<p.length;++i) {
p[i] = p[i-1] + d[i];
sum[i] = sum[i-1] + (d[i]*(d[i]+1))/2;
map.put(p[i],i);
}
long total = 0;
long max = 0;
for(int i=0;i<n;++i) {
int r = map.ceilingEntry(total + x).getValue();
long dis = get(p,i,r);
long val = getsum(sum,i,r);
long diff = 0;
if(dis > x) {
diff = dis-x;
diff = d[r]-diff;
long extra = (d[r]*(d[r]+1))/2 - (diff*(diff+1))/2;
val-=extra;
max = Math.max(max,val);
}else if(dis == x) {
max = Math.max(max,val);
}
if(d[i] > 1 && dis > x) {
max = Math.max(max, val+diff*Math.min(d[i]-1,dis-x));
}
// out.println(val+diff);
total+=d[i];
}
out.println(max);
out.close();
}
private static long getsum(long[] sum, int i, int r) {
if(i == 0) return sum[r];
return sum[r] - sum[i-1];
}
private static long get(long[] p, int i, int r) {
if(i == 0) return p[r];
return p[r] - p[i-1];
}
static void add(Map<Long, Integer> map, long i) {
// TODO Auto-generated method stub
if(map.containsKey(i)) map.put(i, map.get(i)+1);
else map.put(i, 1);
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 3109816ca3571c04846a061cd3da72ab | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class D {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader s = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
private static int[] rai(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
return arr;
}
private static int[][] rai(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextInt();
}
}
return arr;
}
private static long[] ral(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
return arr;
}
private static long[][] ral(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextLong();
}
}
return arr;
}
private static int ri() {
return s.nextInt();
}
private static long rl() {
return s.nextLong();
}
private static String rs() {
return s.next();
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieveOfEratosthenes(int n)
{
boolean[] prime = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p])
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static int[] lowestPrimeFactors(int n) {
int tot = 0;
int[] lpf = new int[n + 1];
int u = n + 32;
double lu = Math.log(u);
int[] primes = new int[(int) (u / lu + u / lu / lu * 1.5)];
for (int i = 2; i <= n; i++)
lpf[i] = i;
for (int p = 2; p <= n; p++) {
if (lpf[p] == p)
primes[tot++] = p;
int tmp;
for (int i = 0; i < tot && primes[i] <= lpf[p] && (tmp = primes[i] * p) <= n; i++) {
lpf[tmp] = primes[i];
}
}
return lpf;
}
static int bsearch(List<Integer> list,int x)
{
int l=0,r=list.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(list.get(mid)>=x)
{
if(mid==0)
{
return mid;
}
else if(list.get(mid-1)<x)
{
return mid;
}
else {
r=mid-1;
}
}
else
{
l=mid+1;
}
}
return -1;
}
static long divCount(long n)
{
HashMap<Long,Integer> map=new HashMap<>();
while(n%2==0)
{
n/=2;
map.put(2L,map.getOrDefault(2L,0)+1);
}
for(long i=3;i<=Math.sqrt(n);i+=2)
{
while(n%i==0)
{
map.put(i,map.getOrDefault(i,0)+1);
n/=i;
}
}
if(n>2)
{
map.put(n,1);
}
long prod=1L;
for(long i:map.keySet())
{
// System.out.println(i+" "+map.get(i));
prod*=(map.get(i)+1);
}
return prod-2;
}
static int bsearch(long[] dp,int st,int e,long val,long x)
{
int l=st,r=e;
int ind=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(val-dp[mid]>=x)
{
if(mid==e)
{
return mid;
}
else {
ind=mid;
l=mid+1;
}
}
else
{
r=mid-1;
}
}
return ind;
}
public static void main(String[] args) {
StringBuilder ans = new StringBuilder();
// int t = ri();
int t= 1;
while (t-- > 0)
{
int n=ri();
long x=rl();
long[] st=ral(n);
long[] arr=new long[2*n];
for(int i=0;i<2*n;i++)
{
arr[i]=st[i%n];
}
long[] dp1=new long[2*n];
long[] dp2=new long[2*n];
dp1[0]=(arr[0]*(arr[0]+1))/2;
dp2[0]=arr[0];
for(int i=1;i<2*n;i++)
{
dp1[i]=(arr[i]*(arr[i]+1))/2;
dp1[i]+=dp1[i-1];
dp2[i]=dp2[i-1]+arr[i];
}
long res=Integer.MIN_VALUE;
for(int i=2*n-1;i>=n;i--)
{
int ind=bsearch(dp2,0,i-1,dp2[i],x);
if(ind!=-1)
{
long count=dp1[i]-dp1[ind];
long extra=dp2[i]-dp2[ind]-x;
extra=((extra+1)*extra)/2;
count-=extra;
res=Math.max(count,res);
}
}
ans.append(res).append("\n");
}
out.print(ans.toString());
out.flush();
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | c9438bf42754eb90a3ac5cde78a9e6b2 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.util.*;
public class cp1 {
static StringBuilder run(){
StringBuilder sb=new StringBuilder();
// code
int n=in.nextInt();
long x=in.nextLong();
long a[]=new long[2*n+1];
long pre1[]=new long[2*n+1];
long pre2[]=new long[2*n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextLong();
a[i+n]=a[i];
}
for(int i=1;i<=2*n;i++){
pre1[i]=pre1[i-1]+(a[i]*(a[i]+1))/2;
pre2[i]=pre2[i-1]+a[i];
}
long maxi=0;
for(int i=2*n;i>=n+1;i--){
int low=1;
int high=i;
int ans=i;
while(low<=high){
int mid=(low+high)/2;
if(pre2[i]-pre2[mid]<x){
ans=mid;
high=mid-1;
}
else
low=mid+1;
}
long tmp=pre2[i]-pre2[ans-1]-x;
long sum=pre1[i]-pre1[ans-1]-((tmp*(tmp+1))/2);
maxi=Math.max(maxi, sum);
}
sb.append(maxi);
return sb;
}
static StringBuilder solve() {
StringBuilder result=new StringBuilder();
int t=1;
// t=in.nextInt();
while(--t>=0)
result.append(run()+"\n");
return result;
}
// Sankcan
public static void main(String args[]) {
System.out.print(solve());
}
static Scanner in=new Scanner(System.in);
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | f9e82c6b94c61475d97807c0975da544 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 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.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
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);
solve(in, out);
out.close();
}
private static void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
long x = in.nextLong();
long[] sumHugs = new long[n + 1];
long[] sumArr = new long[n + 1];
for (int i = 1; i <= n; i++) {
long d = in.nextLong();
sumHugs[i] = sumHugs[i - 1] + d * 1L * (d + 1) / 2;
sumArr[i] = sumArr[i-1] + d;
}
int start = 2;
long max = 0;
boolean oneBack = true;
for (int end = 1; end <= n; end++) {
while (getSum(start - 1, end, oneBack, sumArr) >= x && isOk(start, end, oneBack)) {
if (start >= n) {
if (!oneBack) {
break;
}
start = 1;
oneBack = false;
} else start++;
}
int left = start - 1;
long remainingDays = x - (getSum(left, end, oneBack, sumArr));
long sum = getSum(left, end, oneBack, sumHugs) + getExcessDays(left == 0 ? n : left, remainingDays, sumArr);
// System.out.println(start + " " + end + " " + Arrays.toString(sumHugs) + " " + Arrays.toString(sumArr) + " " + sum + " " + oneBack + " " + remainingDays + " " + x);
max = Math.max(max, sum);
}
out.println(max);
}
private static boolean isOk(int start, int end, boolean oneBack) {
return oneBack || start <= end;
}
private static long getSum(int left, int end, boolean oneBack, long[] arr) {
return oneBack ? arr[end] + arr[arr.length - 1] - arr[left] : arr[end] - arr[left];
}
private static long getExcessDays(int index, long x, long[] sumArr) {
if (index == 0 || x == 0) {
return 0;
}
long days = sumArr[index] - sumArr[index - 1];
long totalHugs = days * (days + 1) / 2;
days -= x;
long removedHugs = days * (days + 1) / 2;
// System.out.println(x + " " + days + " " + index);
return totalHugs - removedHugs;
}
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 | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 148e815f8f9835ca20e9bcb896fd28a2 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CF1358D {
public static void main(String[] args) {
FastReader input = new FastReader();
int months = input.nextInt();
long vacation = input.nextLong();
long[] days = new long[2 * months];
for(int i = 0;i < months;i++){
days[i] = input.nextLong();
}
for(int i = months;i < 2 * months;i++){
days[i] = days[i - months];
}
long[] prefix_sumOfDays = new long[2 * months];
long[] prefix_days = new long[2 * months];
for(int i = 0;i < 2 * months;i++){
if(i == 0){
prefix_sumOfDays[i] = (days[i] * (days[i]+1))/2;
prefix_days[i] = days[i];
}
else{
prefix_sumOfDays[i] = ((days[i] * (days[i]+1))/2) + prefix_sumOfDays[i-1];
prefix_days[i] = days[i] + prefix_days[i-1];
}
}
// for(int i = 0;i < 2 * months;i++){
// System.out.print(days[i] + " ");
// }
// System.out.println();
// for(int i = 0;i < 2 * months;i++){
// System.out.print(prefix_sumOfDays[i] + " ");
// }
// System.out.println();
// for(int i = 0;i < 2 * months;i++){
// System.out.print(prefix_days[i] + " ");
// }
long maxHug = Integer.MIN_VALUE;
for(int position = 2 * months-1; position >= months;position--){
int low = 0;
int high = position;
int index = -1;
boolean found = false;
while (low <= high){
int mid = (low + high) / 2;
long segmentDays = prefix_days[position] - prefix_days[mid];
if(segmentDays == vacation){
maxHug = Math.max(maxHug,prefix_sumOfDays[position] - prefix_sumOfDays[mid]);
found = true;
break;
}
else if(segmentDays < vacation){
index = mid;
high = mid - 1;
}
else{
low = mid + 1;
}
}
if(!found){
long rightPart = prefix_sumOfDays[position] - prefix_sumOfDays[index-1];
long p = prefix_days[position] - prefix_days[index-1];
long extraDays = p - vacation;
rightPart -= (extraDays * (extraDays + 1)) / 2;
maxHug = Math.max(maxHug,rightPart);
}
}
System.out.println(maxHug);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | 6035d54f19bd06f102af9f801f40bdf9 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class Main12{
static public void main(String args[])throws IOException{
int tt=1;
StringBuilder sb=new StringBuilder();
for(int ttt=1;ttt<=tt;ttt++){
int n=i();
long x=l();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=i();
}
Queue<Integer> queue=new LinkedList<>();
long[] sum=new long[1000001];
for(int i=1;i<=1000000;i++){
sum[i]=sum[i-1]+i;
}
long[] pre=new long[n];
pre[n-1]=sum[a[n-1]];
for(int i=n-2;i>=0;i--){
pre[i]=pre[i+1]+sum[a[i]];
}
long[] norm=new long[n];
norm[n-1]=a[n-1];
for(int i=n-2;i>=0;i--){
norm[i]=norm[i+1]+a[i];
}
long max=0;
long x1=x;
for(int i=0;i<n;i++){
int num=i;
if(x<=a[num]){
// pln(sum[a[num]]+" "+sum[a[num]-(int)x]);
max=Math.max(sum[a[num]]-sum[a[num]-(int)x],max);
}else{
long ans=0;
int start=i;
long tot=norm[0];
if(i+1<n){
tot-=norm[i+1];
}
if(x>=tot){
ans=ans+pre[0];
if(i+1<n){
ans-=pre[i+1];
}
x=x-tot;
start=n-1;
}
if(x!=0){
// pln(x+" "+start+" "+"H"+" "+ans);
int low=0; // start
int high=start; //n-1
long tmp=0;
if(start+1<n){
tmp=norm[start+1];
}
int index=start;
int flag=0;
while(low<=high){
int mid=low+(high-low)/2;
if(norm[mid]-tmp>x){
index=mid;
low=mid+1;
}else if(norm[mid]-tmp<x){
high=mid-1;
}else{
index=mid;
flag=1;
break;
}
}
// pln(index+"");
if(flag==1)
{
ans+=pre[index];
if(start+1<n){
ans-=pre[start+1];
}
}else{
// pln(i+" "+index+" "+start);
if(index==start){
ans+=sum[(int)a[index]]-sum[a[index]-(int)x];
// pln(ans+"GGG");
}else{
ans+=pre[index+1];
if(start+1<n){
ans-=pre[start+1];
}
x=x-norm[index+1];
if(start+1<n){
x+=norm[start+1];
}
if(x>=0)
ans+=sum[(int)a[index]]-sum[a[index]-(int)x];
}
}
}
// pln(i+" "+ans);
max=Math.max(ans,max);
}
x=x1;
}
sb.append(max+"\n");
}
System.out.print(sb.toString());
}
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
static ArrayList<ArrayList<Integer>> graph;
static int mod=1000000007;
static class Pair{
int x;
int y;
Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
}
public static int[] sort(int[] a){
int n=a.length;
ArrayList<Integer> ar=new ArrayList<>();
for(int i=0;i<a.length;i++){
ar.add(a[i]);
}
Collections.sort(ar);
for(int i=0;i<n;i++){
a[i]=ar.get(i);
}
return a;
}
public static long pow(long a, long b){
long result=1;
while(b>0){
if (b % 2 != 0){
result=(result*a);
b--;
}
a=(a*a);
b /= 2;
}
return result;
}
public static long gcd(long a, long b){
if (a == 0){
return b;
}
return gcd(b%a, a);
}
public static long lcm(long a, long b){
return a*(b/gcd(a,b));
}
public static long l(){
String s=in.String();
return Long.parseLong(s);
}
public static void pln(String value){
System.out.println(value);
}
public static int i(){
return in.Int();
}
public static String s(){
return in.String();
}
}
class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars== -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
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 String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.Int();
return array;
}
} | Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | f6148d997266eb189d6705901957f64c | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
static class Pair implements Comparable<Pair>{
int a;
int b;
public Pair(int x,int y){a=Math.min(x, y);b=Math.max(x, y);}
public Pair(){}
public int compareTo(Pair p){
return p.a - a;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + b;
// out.println("hashcode = "+result);
return result;
}
@Override
public boolean equals(Object obj) {
Pair cur = (Pair)obj;
if((a==cur.a && b==cur.b) || (a==cur.b && b==cur.a))return true;
return false;
}
}
static class cell implements Comparable<cell>{
int a,b;
long dis;
public cell(int x,int y,long z) {a=x;b=y;dis=z;}
public int compareTo(cell c) {
return Long.compare(dis,c.dis);
}
}
static class TrieNode{
TrieNode[]child;
int w;
boolean term;
TrieNode(){
child = new TrieNode[26];
}
}
public static long gcd(long a,long b)
{
if(a<b)
return gcd(b,a);
if(b==0)
return a;
return gcd(b,a%b);
}
static long lcm(long a,long b) {
return a*b / gcd(a,b);
}
//static long ans = 0;
static long mod = (long)(1e9+7);///998244353;
public static void main(String[] args) throws Exception {
new Thread(null, null, "Anshum Gupta", 99999999) {
public void run() {
try {
solve();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static long pow(long x,long y){
if(y == 0)return 1;
if(y==1)return x;
long a = pow(x,y/2);
a = (a*a)%mod;
if(y%2==0){
return a;
}
return (a*x)%mod;
}
static long mxx;
static int mxN = (int)(3e5+5);
static long[]fact,inv_fact;
static long my_inv(long a) {
return pow(a,mod-2);
}
static long bin(int a,int b) {
if(a < b || a<0 || b<0)return 0;
return ((fact[a]*inv_fact[a-b])%mod * inv_fact[b])%mod;
}
//static ArrayList<ArrayList<Integer>>adj;
//static boolean[]vis;
//static Long[]p;
//static long val;
//
//static int[]bfs(int a){
// Queue<Integer> q = new LinkedList<Integer>();
//
// vis = new boolean[n+1];
// int[]dis = new int[n+1];
// q.add(a);
// dis[a] = 0;
// vis[a] = true;
// while(!q.isEmpty()) {
// int cur = q.poll();
// // vis[cur] = true;
// // if(cur == b)break;
// for(int x : adj.get(cur)) {
// if(vis[x])continue;
// q.add(x);
// dis[x] = dis[cur]+1;
// vis[x] = true;
// }
// }
// return dis;
//}
//static int n,m;
public static void solve() throws Exception {
// solve the problem here
MyScanner s = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out), true);
int tc = 1;//s.nextInt();
mxx = (long)(1e18+5);
// fact=new long[mxN];
// inv_fact = new long[mxN];
// fact[0]=inv_fact[0]=1L;
// for(int i=1;i<mxN;i++) {
// fact[i] = (i*fact[i-1])%mod;
// inv_fact[i] = my_inv(fact[i]);
// }
while(tc-->0){
int n = s.nextInt();
long x = s.nextLong();
long[]d=new long[n+1];
long mx = 0;
for(int i=1;i<=n;i++) {
d[i]=s.nextLong();
mx = Math.max(mx, d[i]);
}
if(mx >= x) {
long dif = mx - x ;
long ans = mx*(mx+1)/2 - dif*(dif+1)/2;
out.println(ans);
continue;
}
long[]dd = new long[2*n];
for(int i=0;i<n;i++)dd[i]=dd[i+n]=d[i+1];
int i = -1, j = 0;
long sum = 0;
long tot = 0;
long ans = 0;
while(i < n) {
if(i >= 0) {
sum -= dd[i];
tot -= dd[i]*(dd[i++]+1)/2;
}
else i++;
while(j < 2*n && sum < x ) {
sum += dd[j];
tot += dd[j]*(dd[j++]+1)/2;
}
long dif = sum - x;
ans = Math.max(ans, tot - dif*(dif+1)/2);
// out.println(ans);
}
out.println(ans);
}
out.flush();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output | |
PASSED | fd6f2fc25357117c4dc545c8b5c31ed1 | train_000.jsonl | 1590503700 | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
long mod = 998244353;
public static void main(String[] args){
Main main = new Main();
int t = 1;
while(t-->0){
main.solve();
}
out.flush();
}
void solve(){
int n = sc.nextInt();
long x = sc.nextLong();
long[] d = new long[2*n+1];
for(int i=1; i<=n; i++){
d[i] = sc.nextLong();
d[i+n] = d[i];
}
long[] totalDays = new long[2*n+1];
for(int i=1; i<=2*n; i++){
totalDays[i] = totalDays[i-1]+d[i];
}
long ans = 0, sum = 0;
int left = 0;
for(int end=1; end<=2*n; end++){
sum += (d[end]+1)*d[end]/2;
if(totalDays[end]<x) continue;
while(totalDays[end]-totalDays[left]>=x) {
sum -= d[left]*(d[left]+1)/2;
left++;
}
long tail = x-(totalDays[end]-totalDays[left]);
long front = d[left]-tail;
long temp = sum -front*(front+1)/2;
ans = Math.max(ans, temp);
}
out.println(ans);
}
}
| Java | ["3 2\n1 3 1", "3 6\n3 3 3", "5 6\n4 2 3 1 3"] | 2 seconds | ["5", "12", "15"] | NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | Java 8 | standard input | [
"greedy",
"two pointers",
"implementation",
"binary search",
"brute force"
] | 9ba374e20305d93ba42ef152e2cad5b5 | The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$. | 1,900 | Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.