exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | b7af39b985a3987d8f0315b80955fc7c | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
*
* @author RezaM
*/
public class B2 {
public static void main(String[] args) throws IOException {
Scanner scanner=new Scanner(System.in);
int n = scanner.nextInt();
int v = scanner.nextInt();
int ary[] = new int[3001];
for (int i = 0; i < n; i++) {
int day = scanner.nextInt();
int num = scanner.nextInt();
ary[day] += num;
}
int sum = 0;
int lastday = 0;
for (int i = 1; i <= 3000; i++) {
if (lastday - v >= 0) {
sum += v;
lastday = ary[i];
} else {
sum += lastday;
int bv = v - lastday;
if (ary[i] - bv >= 0) {
lastday = ary[i] - bv;
sum += bv;
} else {
lastday = 0;
sum += ary[i];
}
}
}
if (lastday >= v) {
sum += v;
} else {
sum += lastday;
}
System.out.println(sum);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | a239e9e818210e0abfe743451821bf94 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
/**
*
* @author RezaM
*/
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int ary[] = new int[3001];
for (int i = 0; i < n; i++) {
StringTokenizer str = new StringTokenizer(br.readLine());
int day = Integer.parseInt(str.nextToken());
int num = Integer.parseInt(str.nextToken());
ary[day] += num;
}
int sum = 0;
int lastday = 0;
for (int i = 1; i <= 3000; i++) {
if (lastday - v >= 0) {
sum += v;
lastday = ary[i];
} else {
sum += lastday;
int bv = v - lastday;
if (ary[i] - bv >= 0) {
lastday = ary[i] - bv;
sum += bv;
} else {
lastday = 0;
sum += ary[i];
}
}
}
if (lastday >= v) {
sum += v;
} else {
sum += lastday;
}
System.out.println(sum);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 1544a10c3a2f9cd4b448caf7c50cd84f | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.util.Set;
import java.util.Map;
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.TreeMap;
import java.util.SortedMap;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Dumbear (dumbear@163.com)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt(), m = in.nextInt();
SortedMap<Integer, Integer> fruits = new TreeMap<Integer, Integer>();
for (int i = 0; i < n; ++i) {
int a = in.nextInt(), b = in.nextInt();
if (!fruits.containsKey(a)) {
fruits.put(a, b);
} else {
fruits.put(a, fruits.get(a) + b);
}
}
int res = 0;
int current = m;
for (int day : fruits.keySet()) {
int num = fruits.get(day);
if (num <= current) {
res += num;
current = m;
} else {
num = Math.min(m, num - current);
res += current + num;
current = (fruits.containsKey(day + 1) ? m - num : m);
}
}
out.println(res);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | f1212501e6843f0ed56470a128f68e2a | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
final boolean isFileIO = false;
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
String delim = " ";
public static void main(String[] args) throws IOException {
Main m = new Main();
m.initIO();
m.solve();
m.in.close();
m.out.close();
}
public void initIO() throws IOException {
if(!isFileIO) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("A-large-practice.in"));
out = new PrintWriter("output.txt");
}
}
String nextToken() throws IOException {
if(!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken(delim);
}
String readLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
void solve() throws IOException {
int n, v;
n = nextInt(); v = nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i = 0; i < n; i++) {
a[i] = nextInt(); b[i] = nextInt();
}
int ans = 0;
for(int i = 1; i <= 3002; i++) {
int cur = 0;
int need = 0;
for(int j = 0; j < n; j++) {
if(a[j] == i - 1) {
need = v - cur;
cur += Math.min(need, b[j]);
b[j] -= Math.min(need, b[j]);
}
}
for(int j = 0; j < n; j++) {
if(a[j] == i) {
need = v - cur;
cur += Math.min(need, b[j]);
b[j] -= Math.min(need, b[j]);
}
}
ans += cur;
}
out.println(ans);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 597af8f4cc59905c92384b640e14b759 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
/**
*
* @author sousnake
*/
public class B {
public static void main(String a[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int v = Integer.parseInt(s[1]);
//intervals[] ob = new intervals[n];
ArrayList<intervals> ob = new ArrayList<intervals>();
for(int i=0;i<n;i++){
s = br.readLine().split(" ");
long x = Long.parseLong(s[0]);
long y = Long.parseLong(s[1]);
intervals o = new intervals(x, y);
ob.add(o);
//ob[i] = new intervals(x, y);
}
Collections.sort(ob,new comps());
//Arrays.sort(ob,new comps());
/*for(int i=0;i<n;i++){
System.out.println(ob[i].day+" "+ob[i].no);
}*/
long ans=0;
long curans=0;
for(int i=1;i<=3001;i++){
curans=0;
for(int j=0;j<n;j++){
if(ob.get(j).day==i||ob.get(j).day==i-1){
if(curans<v){
long tmp = v-curans;
if(ob.get(j).no<tmp){
curans=curans+ob.get(j).no;
//curans = curans+ob[j].no;
ob.get(j).no=0;
//ob[j].no=0;
}
else{
curans=curans+tmp;
ob.get(j).no=ob.get(j).no-tmp;
//ob[j].no= ob[j].no-tmp;
}
}
}
}
ans=ans+curans;
}
System.out.println(ans);
}
}
class intervals{
long day;
long no;
public intervals(long a,long b) {
this.day=a;
this.no=b;
}
}
class comps implements Comparator{
public int compare(Object o1, Object o2) {
intervals a = (intervals)o1;
intervals b = (intervals)o2;
if(a.day>b.day)
return +1;
else return -1;
}
} | Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | b0682b889ba45f2cfadb4610734a9b91 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author @thnkndblv
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int v = in.nextInt();
int[] day = new int[ 3003 ];
for (int i = 0; i < n; i++)
{
int a = in.nextInt();
int b = in.nextInt();
day[ a + 1 ] += b;
}
int[] rem = new int[ 3003 ];
int ans = 0;
for (int i = 3001; i > 0; i--)
{
int collected = Math.min( v, rem[ i ] );
rem[ i - 1 ] = Math.max( 0, day[ i ] - ( v - collected ) );
collected += Math.min( day[i], v - collected );
ans += collected;
}
out.println( 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 int nextInt() {
return Integer.parseInt(next());
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | b6e746db0d79c9bf83fc1363d34ef440 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 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 {
public static void sortArray(Integer myArray[][]) {
Arrays.sort(myArray, new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return Integer.valueOf(o1[0]).compareTo(Integer.valueOf(o2[0]));
}
});
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String y[] = in.readLine().split(" ");
int n = Integer.parseInt(y[0]);
int max = Integer.parseInt(y[1]);
Integer[][] trees = new Integer[n][2];
for (int i = 0; i < n; i++) {
y = in.readLine().split(" ");
trees[i][0] = Integer.parseInt(y[0]);
trees[i][1] = Integer.parseInt(y[1]);
}
sortArray(trees);
Integer[] t = new Integer[trees[n-1][0]+1];
for (int i = 0; i < t.length; i++) {
t[i]=0;
}
int input;
int index;
int rest;
for (int i = 0; i < n; i++) {
index = trees[i][0]-1;
input = trees[i][1];
// move all to next
if (t[index] == max) {
if (input + t[index + 1] > max) {
t[index + 1] = max;
} else {
t[index + 1] += input;
}
// space exists
} else {
if (input + t[index] > max) {
rest=input + t[index] - max;
t[index] = max;
if (rest + t[index + 1] > max) {
t[index + 1] = max;
} else {
t[index+ 1] +=rest;
}
} else {
t[index] += input;
}
}
}
int fruits = 0;
for (int i = 0; i <t.length; i++) {
fruits += t[i];
}
System.out.println(fruits);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | cdcea95c7e8eeb255c11374998480fec | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.util.Scanner;
public class PB {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int v = in.nextInt();
int[] f = new int[3002];
while(n-->0){
int a = in.nextInt();
int b = in.nextInt();
f[a] += b;
}
int s = 0;
int rem = 0;
for(int i=1;i<=3001;i++){
int round = 0;
if(f[i]+rem<=v){
s+=f[i]+rem;
rem=0;
}else{
if(rem<v){
round+=rem;
}else{
rem = f[i];
s+=v;
continue;
}
s+=v;
rem=f[i]-(v-round);
}
}
System.out.println(s);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 84addd5680ee51e0fecb141c25fb3151 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.awt.Point;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
/**
* @author Jens Staahl
*/
public class B {
// some local config
// static boolean test = false ;
private boolean test = System.getProperty("ONLINE_JUDGE") == null;
static String testDataFile = "testdata.txt";
// static String testDataFile = "testdata.txt";
private static String ENDL = "\n";
// Just solves the acutal kattis-problem
ZKattio io;
private void solve() throws Throwable {
io = new ZKattio(stream);
int n = io.getInt(), v = io.getInt();
List<Point> trees = new ArrayList<>();
for (int i = 0; i < n; i++) {
trees.add(new Point(io.getInt(), io.getInt()));
}
Collections.sort(trees, new Comparator<Point>() {
@Override
public int compare(Point a, Point b) {
return Integer.compare(a.x, b.x);
}
});
int[] picked = new int[n+4000];
for (int i = 0; i < trees.size(); i++) {
Point t = trees.get(i);
int d1 = t.x;
int can = Math.min(t.y, v-picked[d1]);
picked[d1] += can;
t.y -= can;
int d2 = d1+1;
can = Math.min(t.y, v-picked[d2]);
picked[d2] += can;
t.y -= can;
}
int sum =0;
for (int i = 0; i < picked.length; i++) {
sum += picked
[i];
}
System.out.println(sum);
}
public static void main(String[] args) throws Throwable {
new B().solve();
}
public B() throws Throwable {
if (test) {
stream = new FileInputStream(testDataFile);
}
}
InputStream stream = System.in;
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));// outStream
// =
public class ZKattio extends PrintWriter {
public ZKattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public ZKattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
public double getDouble() {
return Double.parseDouble(nextToken());
}
public long getLong() {
return Long.parseLong(nextToken());
}
public String getWord() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) {
}
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
// System.out;
} | Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | b920b63bf92b1826314de006352daa63 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.io.*;
import java.util.*;
public final class wing_mates
{
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws Exception
{
int n=sc.nextInt(),v=sc.nextInt();
Fruit[] a=new Fruit[n];
int md=0;
for(int i=0;i<n;i++)
{
a[i]=new Fruit(sc.nextInt(),sc.nextInt());
if(a[i].day>md)
{
md=a[i].day;
}
}
Arrays.sort(a);
long ans=0;
for(int i=1;i<=(md+1);i++)
{
int max=v;
for(int j=0;j<n && max>0;j++)
{
if(a[j].day==i-1 || a[j].day==i)
{
while(a[j].val>0 && max>0)
{
a[j].val--;
max--;
ans++;
}
}
else if(a[j].day>i)
{
break;
}
}
}
out.println(ans);
out.close();
}
}
class Fruit implements Comparable<Fruit>
{
int day,val;
public Fruit(int day,int val)
{
this.day=day;
this.val=val;
}
public int compareTo(Fruit x)
{
return Integer.compare(this.day,x.day);
}
} | Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 5238ceab7b64f0b0fac81c408a7172b4 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.util.Scanner;
public class PrB{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int v = scan.nextInt();
int c = 0;
int k = v;
int b;
int t[] = new int[3002];
for(int i = 0; i < n; i++){
b = scan.nextInt();
t[b] += scan.nextInt();
}
for(int i = 1; i < 3002; i++){
v = k;
if(t[i-1] > 0){
if(t[i-1] >= v){
c += v;
t[i-1] -= v;
v = 0;
}
else{
c += t[i-1];
v -= t[i-1];
t[i-1] = 0;
}
}
if(t[i] > 0){
if(t[i] >= v){
c += v;
t[i] -= v;
v = 0;
}
else{
c += t[i];
v -= t[i];
t[i] = 0;
}
}
}
System.out.println(c);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 795216ef3e1162763e474158640fe61f | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.util.Scanner;
public class Fruit {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int v = in.nextInt();
int[] daily = new int[3 * 1000 + 2];
for (int i = 0; i < n; i++) {
int a = in.nextInt();
int b = in.nextInt();
daily[a] += b;
}
int carry = 0;
int t = 0;
for (int i = 0; i < daily.length; i++) {
if (daily[i] + carry >= v) {
t += v;
carry = daily[i] - (carry >= v ? 0 : v - carry);
} else {
t += daily[i] + carry;
carry = 0;
}
}
System.out.println(t);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | a3987ceb44c8ff0296af5ba29187e8f6 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.util.*;
import java.io.*;
/**
* Created by user on 28.06.2014.
*/
public class Main {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), v = sc.nextInt();
int[] fid = new int[ 5000 ];
for ( int i = 0; i < n; ++ i){
int temp = sc.nextInt(),
add = sc.nextInt();
fid[temp] += add;
}
int pred_fr = 0, ppred_fr = 0, ans = 0;
for ( int i = 0; i < 5000; ++ i){
int norm = v;
ppred_fr = pred_fr;
pred_fr = fid[i];
int temp = Math.min( norm, ppred_fr );
ans += temp;
norm -= temp;
ppred_fr -= temp;
temp = Math.min( norm, pred_fr );
ans += temp;
norm -= temp;
pred_fr -= temp;
}
System.out.println(ans);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | e514ad65afba5c8b1ccac5739c740d07 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public Scanner sc;
public void run() {
int n = sc.nextInt();
int v = sc.nextInt();
int[] as = new int[3001];
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
as[a] += b;
}
int ans = 0;
for (int day = 1; day <= 3001; day++) {
int vv = v;
if (vv > as[day-1]) {
vv -= as[day-1];
ans += as[day-1];
if (day <= 3000) {
if (vv > as[day]) {
ans += as[day];
as[day] = 0;
} else {
as[day] -= vv;
ans += vv;
}
}
} else {
ans += vv;
}
}
System.out.println(ans);
}
public static void main(String[] args) {
Main main = new Main();
try { main.sc = new Scanner(new FileInputStream("test.in")); }
catch (FileNotFoundException e) { main.sc = new Scanner(System.in); }
main.run();
}
} | Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 4f258ae75150a9f092a81e4358cbf6bd | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes |
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
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();
}
}
class TaskD {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int v = in.nextInt();
int count =0;
int[] ans = new int[3005];
int[] tmp = new int[3005];
for(int i=1;i<3005;i++)
{ans[i]=0;
tmp[i] = 0;
}
for (int i = 1; i <= n; ++i) {
int d = in.nextInt();
int x = in.nextInt();
ans[d]+=x;
}
int pr = 0;
for(int i=1;i<3002;i++){
int curr = ans[i] + pr;
int diff = 0;
if(curr>v){
count+=v;
diff = curr-v;
if(diff>ans[i])diff = ans[i];
}
else count+=curr;
pr = diff;
}
System.out.println(count);
}
}
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 | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 17802e6e9ad5e4b4dff9483c6b070a2e | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/*
* 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.
*/
/**
*
* @author DELL
*/
public class main {
public static void main(String[] args) throws IOException {
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String s = x.readLine();
String[] s1 = s.split(" ");
int n = Integer.parseInt(s1[0]);
int v = Integer.parseInt(s1[1]);
int [][] a=new int [n][2];
int [] val=new int[3002];
for (int i = 0; i < n ; i++) {
s = x.readLine();
String[] s2 = s.split(" ");
a[i][0]= Integer.parseInt(s2[0]);
a[i][1]= Integer.parseInt(s2[1]);
}
int res=0;
merge_sort(a);
for(int i=0;i<n;i++){
int aa=a[i][0];
int bb=a[i][1];
int temp=val[aa];
int diff=v-temp;
if(diff>bb){
val[aa]+=bb;
}
else {
val[aa]=v;
val[aa+1]+=bb-diff;
if(val[aa+1]>v){
val[aa+1]=v;
}
}
}
for(int i=0;i<val.length;i++){
res+=val[i];
}
System.out.println(res);
}
public static void merge_sort(int [][] b){
int l=b.length;
merge_sort(b,0,l-1);
}
public static void merge_sort(int [][] b,int p,int r){
if(p<r){
int q=(p+r)/2;
merge_sort(b, p, q);
merge_sort(b, q+1, r);
merge(b,p,q,r);
}
}
public static void merge(int [][] c,int p,int q,int r){
int [][] a=new int[q-p+2][2];
int [][] b=new int[r-q+1][2];
for(int i=0;i<a.length-1;i++){
a[i][0]=c[p+i][0];
a[i][1]=c[p+i][1];
}
for(int i=0;i<b.length-1;i++){
b[i][0]=c[q+1+i][0];
b[i][1]=c[q+1+i][1];
}
int i=0;
int j=0;
a[a.length-1][0]=Integer.MAX_VALUE;
b[b.length-1][0]=Integer.MAX_VALUE;
for(int k=p;k<r+1;k++){
if(a[i][0]<=b[j][0]){
c[k][0]=a[i][0];
c[k][1]=a[i][1];
i++;
}
else{
c[k][0]=b[j][0];
c[k][1]=b[j][1];
j++;
}
}
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | cd6cae9a394c3a5bd594b43c5971cb4f | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | //package codeforces;
import java.util.Scanner;
/**
* Created by nitin.s on 13/03/16.
*/
public class ValeraAndFruits {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int v = in.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i = 0; i < n; ++i) {
a[i] = in.nextInt();
b[i] = in.nextInt();
}
int fromLastDays = 0;
int ans = 0;
for(int day = 1; day <= 3001; ++day) {
int curDay = 0;
for(int i = 0; i < n; ++i) {
if(a[i] == day) {
curDay += b[i];
}
}
if(curDay + fromLastDays <= v) {
ans +=fromLastDays + curDay;
fromLastDays = 0;
} else {
ans += v;
int tv = v - fromLastDays;
if(tv < 0) {
tv = 0;
}
fromLastDays = curDay - tv;
}
}
System.out.println(ans);
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | d5522d3fb9a54ef8cfb0e53ad71ffe4b | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int trees = sc.nextInt();
int can = sc.nextInt();
int lastday = 0;
long[] days = new long[3000];
Arrays.fill(days, 0);
for (int i = 0; i < trees; i++) {
int day = sc.nextInt();
int fruits = sc.nextInt();
days[day - 1] += fruits;
lastday = Math.max(lastday, day-1);
}
long sum = 0;
if(days[0] > can) {
sum += can;
days[0] -= can;
}
else {
sum += days[0];
days[0] = 0;
}
for (int i = 1; i <= lastday; i++) {
long today = 0;
if(days[i-1] > 0) {
if(can > days[i-1]) {
sum += days[i-1];
today = days[i-1];
days[i-1] = 0;
}
else {
sum += can;
today = can;
days[i-1] -= can;
}
}
if(today < can) {
if(can-today > days[i]) {
sum += days[i];
days[i] = 0;
}
else {
sum += can-today;
days[i] -= (can-today);
}
}
}
sum += Math.min(can, days[lastday]);
System.out.println(sum);
}
} | Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | c26f930e15c781b083f6bc1d951dce82 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.StringTokenizer;
public class B_252 {
static StringTokenizer st;
static BufferedReader br;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
int n = nextInt(), v = nextInt(), m = 0;
long ans = 0;
int[] a = new int[3005];
for (int i = 1; i <= n; i++) {
int k = nextInt();
a[k] += nextInt();
m = Math.max(m, k);
}
for (int i = 1; i <= m + 1; i++) {
//System.out.println(i + " " + a[i] + " " + a[i + 1]);
if (a[i - 1] > 0) {
if (a[i - 1] >= v)
ans += v;
else {
if (a[i] + a[i - 1] > v) {
ans += v;
a[i] = a[i] + a[i - 1] - v;
} else {
ans += a[i] + a[i - 1];
a[i] = 0;
}
}
a[i - 1] = 0;
} else {
if (a[i] >= v) {
ans += v;
a[i] -= v;
} else {
ans += a[i];
a[i] = 0;
}
}
}
pw.println(ans);
pw.close();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
private static long nextLong() throws IOException {
return Long.parseLong(next());
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 6c4d52ca8ce26ea0a74010b25e1c9d07 | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.io.*;
import java.util.*;
public class SolutionB {
public void solve(){
int n = nextInt(), perDay = nextInt();
Tree[] ar = new Tree[n];
for (int i = 0; i < ar.length; i++) {
ar[i] = new Tree(nextInt(), nextInt());
}
Arrays.sort(ar);
int sum = 0;
for(int day = 1 ; day <= 3001 ; day++){
int pSum = 0;
for(int i = 0 ; i < n && pSum < perDay; i++){
if(ar[i].day + 1 == day){
if(ar[i].fruits == 0)
continue;
if(ar[i].fruits > perDay - pSum){
int t = perDay - pSum;
pSum += t;
ar[i].fruits -= t;
}else{
pSum += ar[i].fruits;
ar[i].fruits = 0;
}
}
}
for(int i = 0 ; i < n && pSum < perDay; i++){
if(ar[i].day == day){
if(ar[i].fruits == 0)
continue;
if(ar[i].fruits > perDay - pSum){
int t = perDay - pSum;
pSum += t;
ar[i].fruits -= t;
}else{
pSum += ar[i].fruits;
ar[i].fruits = 0;
}
}
}
sum+=pSum;
}
out.println(sum);
}
public void run(){
solve();
out.close();
}
class Tree implements Comparable<Tree>{
int day, fruits;
public Tree(int i, int j){
day = i;
fruits = j;
}
@Override
public int compareTo(Tree o) {
// TODO Auto-generated method stub
return day - o.day;
}
}
public static void main(String args[]){
new SolutionB().run();
}
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
String line;
StringTokenizer st;
public String nextLine(){
try {
line = bf.readLine();
st = new StringTokenizer(line);
} catch (IOException e) {
return null;
}
return line;
}
public String nextString(){
while (st == null || !st.hasMoreElements()) {
try {
line = bf.readLine();
st = new StringTokenizer(line);
} catch (IOException e) {
return null;
}
}
return st.nextToken();
}
public int nextInt(){
return Integer.parseInt(nextString());
}
public long nextLong(){
return Long.parseLong(nextString());
}
public double nextDouble(){
return Double.parseDouble(nextString());
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 9d2f0042f3de011335680410c1b1c56d | train_004.jsonl | 1402241400 | Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.Character.Subset;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
public class marathon
{
public static ArrayList<Integer> strToInts(String str, String token)
{
ArrayList<Integer> result = new ArrayList<>();
String[] strs = str.split(token);
for (String string : strs)
result.add(Integer.parseInt(string));
return result;
}
public static ArrayList<Long> strToLong(String str, String token)
{
ArrayList<Long> result = new ArrayList<>();
ArrayList<String> strs = new ArrayList<>(Arrays.asList(str.split(token)));
for (String string : strs)
{
result.add(Long.parseLong(string));
}
return result;
}
public static void main(String[] args) throws IOException
{
// The input and output streams
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // system
//BufferedReader in = new BufferedReader(new FileReader("input.txt"));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
// Take input
ArrayList<Integer> params = strToInts(in.readLine(), " ");
int nTress = params.get(0);
int dayMax = params.get(1);
int[] daysFruits = new int[3003];
for (int i = 0; i < nTress; i++)
{
ArrayList<Integer> line = strToInts(in.readLine(), " ");
daysFruits[line.get(0)] += line.get(1);
}
System.out.println(solve(dayMax, daysFruits));
out.flush();
}
private static String solve(int dayMax, int[] daysFruits)
{
int count = 0;
for (int day = 1; day < daysFruits.length; day++)
{
int todayRem = dayMax;
// each stuff from yesterday
int eatenFromYesterday = Math.min(todayRem, daysFruits[day - 1]);
todayRem -= eatenFromYesterday;
daysFruits[day-1] -= eatenFromYesterday;
// each stuff from today
int eatenToday = Math.min(todayRem, daysFruits[day]);
todayRem -= eatenToday;
daysFruits[day] -= eatenToday;
count += eatenFromYesterday + eatenToday;
}
return "" + count;
}
}
| Java | ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"] | 1 second | ["8", "60"] | NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | Java 7 | standard input | [
"implementation",
"greedy"
] | 848ead2b878f9fd8547e1d442e2f85ff | The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. | 1,400 | Print a single integer — the maximum number of fruit that Valera can collect. | standard output | |
PASSED | 3dfeee8bd4b56bc8d936724d49e41a09 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
Integer n = in.nextInt();
Long k = in.nextLong(),min = (long) 0;
Long[] aux = new Long[26];
Arrays.fill(aux,(long)0);
String str = in.next();
for(int i = 0; i < n; i++)
{
aux[(int)(str.charAt(i)-'A')]++;
}
Arrays.sort(aux);
long ans = 0;
for(int i = 25; i >= 0; i--){
if(k <= 0 || aux[i] <= 0)break;
min = Math.min(aux[i], k);
ans += min*min;
k -= min;
}
out.println(ans);
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public Integer nextInt() {
return Integer.parseInt(next());
}
public Long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public Float nextFloat() {
return Float.parseFloat(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public Integer[] nextIntegerArray(int n) {
Integer[] a = new Integer[n];
for(Integer i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Long[] nextLongArray(int n) {
Long[] a = new Long[n];
for(Integer i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public String[] nextStringArray(int n){
String[] s = new String[n];
for(Integer i = 0; i < n; i++)
s[i] = next();
return s;
}
public String[] nextLineStringArray(int n){
String[] s = new String[n];
for(Integer i = 0; i < n; i++)
s[i] = next();
return s;
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | a013c02df83aaaf6aa4132278ca90c0c | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long c = sc.nextLong();
char[] ch = sc.next().toCharArray();
long[] a = new long[26];
for(int i=0;i<n;i++){
a[ch[i]-65]++;
}
Arrays.sort(a);
long s = 0;
for(int i=a.length-1;i>-1;i--){
if(c>a[i]){
s += a[i] * a[i];
c -= a[i];
}else{
s += c*c;
break;
}
}
System.out.print(s);
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 1f73ede32176c32f4c52d179c8cd38dd | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.InputMismatchException;
public class Main
{
public static void main(String args[])
{
InputStream input= System.in;
InputReader in= new InputReader(input);
OutputStream output= System.out;
try (PrintWriter out = new PrintWriter(output)) {
int n=in.readInt();
int k=in.readInt();
String s=in.readString();
int arr[]=new int[26];
for(int i=0;i<26;i++)arr[i]=0;
for(int i=s.length()-1;i>=0;i--)
{
int c=s.charAt(i)-65;
arr[c]++;}
long sum=0;
for(int i=0;i<25;i++)
{
for(int j=0;j<25-i;j++)
if(arr[j]<arr[j+1]){
int t=arr[j];arr[j]=arr[j+1];arr[j+1]=t;}}
for(int i=0;i<26;i++){
k-=arr[i];
if(k>=0)sum+=((long)(arr[i])*arr[i]);
else sum+=(long)(k+arr[i])*(k+arr[i]);
if(k<=0)break;}
out.println(sum);
} }
}
class InputReader
{
private InputStream stream;
private byte[] buffer = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream)
{
this.stream = stream;
}
private int read()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try {
numChars = stream.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buffer[curChar++];
}
public int readInt()
{
int c = read();
while (isWhiteSpace(c))
c = read();
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isWhiteSpace(c));
return res * sign;
}
public String readString()
{
int c = read();
while (isWhiteSpace(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isWhiteSpace(c));
return res.toString();
}
public String next()
{
return readString();
}
private boolean isWhiteSpace(int c)
{
return c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == -1;
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 42895af28adbd91cbb729026e4b055ba | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.*;
import java.util.*;
public class BCards {
static class IO {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
public String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(reader.readLine());
}
return tok.nextToken();
}
public int readInt() throws IOException {
return Integer.parseInt(readString());
}
public long readLong() throws IOException {
return Long.parseLong(readString());
}
public double readDouble() throws IOException {
return Double.parseDouble(readString());
}
public int[] readIntArray(int n) throws IOException {
int[] ar = new int[n];
for (int i = 0; i < ar.length; i++) {
ar[i] = readInt();
}
return ar;
}
public long[] readLongArray(int n) throws IOException {
long[] ar = new long[n];
for (int i = 0; i < ar.length; i++) {
ar[i] = readLong();
}
return ar;
}
public PrintWriter getWriter() throws IOException {
return pw;
}
public void close() throws IOException {
pw.flush();
pw.close();
reader.close();
}
}
public static void main(String[] args) throws Exception {
Task t = new Task();
t._solve();
}
static class Task {
IO io;
Task() {
this.io = new IO();
}
void _solve() throws Exception {
try {
solve();
} finally {
io.close();
}
}
int N, K;
char[] letters;
class CharCount implements Comparable<CharCount> {
char ch;
int count;
@Override
public int compareTo(CharCount o) {
return this.count - o.count;
}
public String toString() {
return ch + ":" + count;
}
}
void solve() throws Exception {
N = io.readInt();
K = io.readInt();
letters = io.readString().toCharArray();
List<CharCount> lc = new ArrayList<>();
Arrays.sort(letters);
int g = 0;
char gc = '-';
for (char ch : letters) {
if (ch != gc) {
// new group.
// store old one.
if (gc != '-') {
CharCount cc = new CharCount();
cc.ch = gc;
cc.count = g;
lc.add(cc);
}
// new stuff
gc = ch;
g = 1;
} else {
g++;
}
}
CharCount ccc = new CharCount();
ccc.ch = gc;
ccc.count = g;
lc.add(ccc);
Collections.sort(lc, Collections.reverseOrder());
long take = 0;
long sum = 0;
for (CharCount cc : lc) {
if (cc.count + take > K) {
sum += 1L * (K-take) * (K-take);
break;
} else {
take += cc.count;
sum += 1L * cc.count * cc.count;
}
}
io.getWriter().println(Long.toString(sum));
}
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 32c893963edfc354d62f1a2d8ef7e57f | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
/*
http://codeforces.com/problemset/problem/462/B
Codeforces #263 (Div.2)
B. Appleman and Card Game
*/
public class ApplemanCardGame {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
String cardsStr = in.next();
char[] cards = new char[n];
int[] cardsNum = new int[26];
for (int i = 0; i < n; i++) {
cards[i] = cardsStr.charAt(i);
cardsNum[Character.getNumericValue(cards[i])-10]++; //subtract 10 so 'A' is at 0 and 'Z' is at 25
}
long sum= 0;
long coins = 0;
int position = -1;
while(sum < k){
long max = 0;
for (int i = 0; i < cardsNum.length; i++) {
if(cardsNum[i] > max){
max = cardsNum[i];
position = i;
}
}
cardsNum[position] = 0;
long diff = (sum + max) - k; // how much the max puts the sum over k
if (diff <= 0){
coins += max*max;
sum += max;
}
else{
long part = k - sum;
coins += part * part;
sum += part;
}
}
System.out.println(coins);
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | e7adce5ee76fd395117fd06e764d878d | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class PashmakandGarden {
public static long sol(int[] num1, int num2[]) {
if (num1.length == 1) {
return num1[0];
}
if (num2.length == 1) {
return num2[0];
}
long sum = 0;
sum += sol(Arrays.copyOf(num1, num1.length / 2),
Arrays.copyOf(num1, num1.length - num1.length / 2))
+ sol(Arrays.copyOf(num2, num2.length / 2),
Arrays.copyOf(num2, num2.length - num2.length / 2));
return sum;
}
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// int n = Integer.parseInt(in.readLine());
String a[] = in.readLine().split(" ");
// int num[] = new int[n];
// long sum = 0;
// for (int i = 0; i < a.length; i++) {
// num[i] = Integer.parseInt(a[i]);
// sum += num[i];
// }
// sum += sol(Arrays.copyOf(num, num.length / 2),
// Arrays.copyOf(num, num.length - num.length / 2));
// out.println(sum);
int n = Integer.parseInt(a[0]);
int k = Integer.parseInt(a[1]);
String s = in.readLine();
long num[] = new long[28];
for (int i = 0; i < s.length(); i++) {
num[s.charAt(i) - 'A']++;
}
Arrays.sort(num);
long count = 0;
for (int i = num.length - 1; i >= 0 && k > 0; i--) {
if (num[i] >= k) {
count += (long)k * (long)k;
break;
} else {
count += num[i] * num[i];
k = k - (int)num[i];
}
}
out.println(count);
out.close();
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 0bc31a99cabfc484288b9a94e2a2983b | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class SolveContest {
PrintWriter out;
BufferedReader input;
long begin;
SolveContest(String infile, String outfile) {
try {
input = new BufferedReader(new FileReader(infile));
out = new PrintWriter(new FileWriter(outfile), true);
solver();
} catch (Exception ex) {
ex.printStackTrace();
input = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out, true);
solver();
} finally {
out.close();
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void startTime() {begin = System.currentTimeMillis();}
long getTime() {return System.currentTimeMillis()-begin;}
StringTokenizer st = new StringTokenizer("");
private String nextToken() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(input.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
private int nextInt() {
return Integer.parseInt(nextToken());
}
private long nextLong() {
return Long.parseLong(nextToken());
}
private double nextDouble() {
return Double.parseDouble(nextToken());
}
void solver() {
int n = nextInt();
int k = nextInt();
String s = nextToken();
Pair[] ps = new Pair[26];
for (int i = 0; i < ps.length; i++) {
ps[i] = new Pair('a',0);
}
for (int i = 0; i < s.length(); i++) {
if (ps[s.charAt(i)-'A'].w == 'a')
ps[s.charAt(i)-'A'] = new Pair(s.charAt(i),1);
else
ps[s.charAt(i)-'A'].c++;
}
Arrays.sort(ps);
// System.out.println(ps[0].w + " " + ps[0].c);
long res = 0;
for (int i = 0; i < 26; i++) {
if (ps[i].w != 'a' && k > 0) {
if (ps[i].c > k) {
res += 1L*k*k;
k = 0;
} else {
res += 1L*ps[i].c*ps[i].c;
k -= ps[i].c;
}
}
}
out.print(res);
}
public static void main(String[] args) {
new SolveContest("input.txt","output.txt");
}
}
class Pair implements Comparable<Pair>{
char w;
int c;
Pair(char w, int c) {
this.w = w;
this.c = c;
}
@Override
public int compareTo(Pair o) {
return o.c-c;
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | bd41c2fa3e759264d8228b50fb37af41 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
final int MOD = 1000000007;
int[] dx = { 1, 0, 0, -1 };
int[] dy = { 0, 1, -1, 0 };
void run() {
long n = sc.nextLong();
long k = sc.nextLong();
String s = sc.next();
long[] cnt = new long[26];
for (int i = 0; i < n; i++) {
cnt[s.charAt(i) - 'A']++;
}
Arrays.sort(cnt);
long ans = 0;
for (int i = cnt.length - 1; i >= 0; i--) {
if (k > cnt[i]) {
ans += cnt[i] * cnt[i];
k -= cnt[i];
} else {
ans += k * k;
break;
}
}
System.out.println(ans);
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (a[i] >= border)
int lower_bound(int a[], int border) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (border <= a[mid]) {
r = mid;
} else {
l = mid;
}
}
// r = l + 1
return r;
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | d2dd05d01a5222c133689e0ded432f94 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
final int MOD = 1000000007;
int[] dx = { 1, 0, 0, -1 };
int[] dy = { 0, 1, -1, 0 };
void run() {
long n = sc.nextLong();
long k = sc.nextLong();
char[] c = sc.next().toCharArray();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < n; i++) {
map.put(c[i], map.containsKey(c[i]) ? map.get(c[i]) + 1 : 1);
}
long[] a = new long[map.size()];
int index = 0;
for (char key : map.keySet()) {
a[index++] = map.get(key);
}
Arrays.sort(a);
long cnt = 0;
for (int i = a.length - 1; i >= 0 && k > 0; i--) {
if (k >= a[i]) {
cnt += a[i] * a[i];
k -= a[i];
} else {
cnt += k * k;
k = 0;
}
}
System.out.println(cnt);
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (a[i] >= border)
int lower_bound(int a[], int border) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (border <= a[mid]) {
r = mid;
} else {
l = mid;
}
}
// r = l + 1
return r;
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 33d04444cbb12d0bb784b078045a164b | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
void run() {
long n = sc2.nextLong();
long k = sc2.nextLong();
String card = sc2.next();
HashMap<Character, Long> map = new HashMap<Character, Long>();
long score = 0;
for (int i = 0; i < n; i++) {
char c = card.charAt(i);
if (!map.containsKey(c)) {
map.put(c, (long) 1);
} else {
long cnt = map.get(c);
map.put(c, cnt + 1);
}
}
while (k > 0 && n > 0) {
long max = 0;
char maxKey = '0';
for (char key : map.keySet()) {
if (max < map.get(key)) {
max = map.get(key);
maxKey = key;
}
}
if (k >= max) {
score += max * max;
k -= max;
map.put(maxKey, (long) 0);
} else {
score += k * k;
k -= k;
}
}
System.out.println(score);
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 9ac6c772f59124dbb9ea9ca93aaac716 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main_origin {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
long start = System.currentTimeMillis();
long fin = System.currentTimeMillis();
final int MOD = 1000000007;
int[] dx = { 1, 0, 0, -1 };
int[] dy = { 0, 1, -1, 0 };
void run() {
int n = sc.nextInt();
long k = sc.nextLong();
String s = sc.next();
long max = 0;
long[] cnt = new long[26];
for (int i = 0; i < n; i++) {
cnt[s.charAt(i) - 'A']++;
}
Arrays.sort(cnt);
for (int i = 25; i >= 0; i--) {
long num = cnt[i];
if (num <= k) {
max += num * num;
k -= num;
} else {
max += k * k;
break;
}
}
System.out.println(max);
}
public static void main(String[] args) {
new Main_origin().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(char[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (a[i] >= border)
int lower_bound(int a[], int border) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (border <= a[mid]) {
r = mid;
} else {
l = mid;
}
}
// r = l + 1
return r;
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 36c3031c9cd1bc82bb489dcc85c282bf | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.*;
import java.io.*;
public class CF462B {
public static void main(String... args) {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
long k = sc.nextLong();
char[] cards = sc.readNextLine().toCharArray();
HashMap<String, Long> map = new HashMap<String, Long>();
for (int i = 0; i < n; i++) {
String s = String.valueOf(cards[i]);
if (map.get(s) != null) {
map.put(s, map.get(s) + 1);
} else {
map.put(s, 1L);
}
}
List<Long> list = new ArrayList<Long>(map.values());
Collections.sort(list, Collections.reverseOrder());
long sum = 0;
int i = 0;
while (k > 0) {
long num = list.get(i);
if (num > k) {
sum += k * k;
break;
} else {
sum += num * num;
k -= num;
}
i++;
}
System.out.println(sum);
System.exit(0);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(Reader in) {
br = new BufferedReader(in);
}
public MyScanner() { 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()); }
// Slightly different from java.util.Scanner.nextLine(),
// which returns any remaining characters in current line,
// if any.
String readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 9bab4e6d6d0bffdd100eead874348153 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.BufferedInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class Main {
//public static final int C = 1000000007;
//static BigDecimal map[][];
//static int N;
//static int M;
public static void main(String[] args) {
//StringBuilder sb = new StringBuilder();
BufferedInputStream bs = new BufferedInputStream(System.in);
Scanner sc = new Scanner(bs);
int n = sc.nextInt();
int k = sc.nextInt();
//int m = sc.nextInt();
//ArrayList<Integer> al = new ArrayList<Integer>();
char a[];
String str = sc.next();
a = str.toCharArray();
int sum[] = new int[26];
//int m = sc.nextInt();
//HashMap<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
long ans = 0;
for (int i=0; i < n; i++) {
sum[a[i] - 'A']++;
}
Arrays.sort(sum);
for (int i=0; i < 26; i++) {
if (k - sum[25 - i] >= 0) {
k -= sum[25 - i];
ans += (long)sum[25 - i]*sum[25 - i];
}else {
ans += (long)k*k;
break;
}
}
System.out.println(ans);
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 4520d261d7e9bed2d4018b19f222a624 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes |
import java.util.*;
public class SolutionB {
public static void main(String[] args) {
System.out.println(solution());
}
private static long solution(){
//read input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextInt();
String cards = sc.next();
sc.close();
int i = 0;
Long[] size = new Long[26];
for(i=0;i<size.length;++i)
size[i] = 0l;
for(i=0;i<cards.length();++i){
size[cards.charAt(i) - 'A']++;
}
Arrays.sort(size, Collections.reverseOrder());
long sum = 0;
for(i=0;i<size.length;++i){
if(k > size[i]){
sum += size[i] * size[i];
k -= size[i];
}else{
sum += k * k;
break;
}
}
return sum;
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 94d34734f98ef6a2d9eab46f4089f21e | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long c = sc.nextLong();
char[] ch = sc.next().toCharArray();
long[] a = new long[26];
for(int i=0;i<n;i++){
a[ch[i]-65]++;
}
Arrays.sort(a);
long s = 0;
for(int i=a.length-1;i>-1;i--){
if(c>a[i]){
s += a[i] * a[i];
c -= a[i];
}else{
s += c*c;
break;
}
}
System.out.print(s);
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | e384cafe17e4d97a65852b6519cd702b | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class A {
static int max = 100000;
static boolean[] isPrime;
// compare class //
static class compare {
int a;
int b;
public compare(int a, int b) {
this.a = a;
this.b = b;
}
}
// comparator class //
static Comparator<compare> cmp = new Comparator<compare>() {
public int compare(compare a, compare b) {
if (a.a > b.b)
return 1;
else if (a.a < b.b)
return -1;
else
return 0;
}
};
static int getSum(long a) {
int result = 0;
while (a > 0) {
result += a % 10;
a /= 10;
}
return result;
}
static long pow(long n, int p) {
if (p == 0)
return 1;
if ((p & 1) == 0)
return pow(n * n, p >> 1);
else
return n * pow(n * n, p >> 1);
}
static int sumArray(int[] A) {
long ans = 0;
HashMap<Long, Integer> dp = new HashMap<Long, Integer>();
dp.put(0L, 1);
long curSum = 0;
for (int i = 0; i < A.length; i++) {
curSum += (long) A[i];
if (dp.containsKey(curSum))
ans += (long) dp.get(curSum);
if (dp.containsKey(curSum))
dp.put(curSum, dp.get(curSum) + 1);
else
dp.put(curSum, 1);
}
if (ans > 1000000000)
ans = -1;
return (int) ans;
}
static void Sieve(int N) {
isPrime = new boolean[N + 1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= N; i++) {
if (isPrime[i]) {
for (int j = i; i * j <= N; j++) {
isPrime[i * j] = false;
}
}
}
}
static char[][] arr;
static int isValid(int i, int j) {
if (i < arr.length && j < arr[0].length && i >= 0 && j >= 0
&& arr[i][j] == 'o')
return 1;
else
return 0;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String[] str = s.nextLine().split(" ");
long n = Integer.parseInt(str[0]);
long k = Integer.parseInt(str[1]);
String a = s.nextLine();
long[] countAlpha = new long[128];
int countNew = 0;
for (int i = 0; i < a.length(); i++) {
if (countAlpha[a.charAt(i)] == 0)
countNew++;
countAlpha[a.charAt(i)]++;
}
long[] maxArr = new long[countNew];
int countMax = 0;
for (int i = 0; i < countAlpha.length; i++)
if (countAlpha[i] > 0)
maxArr[countMax++] = countAlpha[i];
Arrays.sort(maxArr);
long res = 0;
for (int i = maxArr.length - 1; i >= 0; i--) {
if (maxArr[i] >= k) {
res += (k * k);
break;
} else {
res += (maxArr[i] * maxArr[i]);
k -= maxArr[i];
}
}
System.out.println(res);
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 66f1dba1443971dd97006259c71b051a | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.*;
import java.io.*;
public class C462B {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.valueOf(st.nextToken()) ,k = Integer.valueOf(st.nextToken()) , i , j;
long a[] = new long[26];
char ch[] = br.readLine().toCharArray();
for(i = 0;i < n;i ++)
a[ch[i] - 65] ++;
Arrays.sort(a);
//for(i = 0;i < 26;i ++)
//System.out.print(a[i] + " "); System.out.println();
/*if(a[25] > k){
System.out.println((long)k*k);
return;
}*/
int s = 0;
long sum = 0;
i = 25;
while(s < k){
if(s + a[i] <= k){
s += a[i]; sum += a[i]*a[i];
}else
break;
i --;
}
if(s < k){
sum += (long)(k - s)*(k - s);
}
System.out.println(sum );
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | f7e55a696ea5fcaa49f3fc8f22417afd | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class CardGame {
public static void main(String[] args) {
new CardGame();
}
public CardGame(){
Scanner sc = new Scanner(System.in);
sc.nextInt();
int k = sc.nextInt();
String cards = sc.next();
int count[] = new int [256];
for (int i=0;i<cards.length();i++){
count[(int)cards.charAt(i)]++;
}
Arrays.sort(count);
for (int i=255;i>=0;i--){
}
long sum = 0;
int c = 255;
while(k>0){
long number;
if (k-count[c]<0){
number = k;
}else number = count[c];
sum += number*number;
k-=number;
c--;
}
System.out.println(sum);
sc.close();
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 7bfa6f039ae334db0a6e31fe544a9f7a | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes |
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import sun.security.util.BigInt;
/*
* 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.
*/
/**
*
* @author Houssem
*/
public class Codeforces263div1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
long k = sc.nextInt();
String ch=sc.next();
//System.out.print((int)ch.charAt(0));
long [] tocc=new long[26];
for(int i=0;i<ch.length();i++)
{
tocc[((int)ch.charAt(i))-65]++;
}
long argent=0;
while(k!=0)
{
int imax=0;
for(int i=1;i<26;i++)
{
if(tocc[i]>tocc[imax])
{
imax=i;
}
}
if(tocc[imax]>=k)
{
argent+=k*k;
k=0;
}
else
{
argent+= tocc[imax]*tocc[imax];
k-=tocc[imax];
tocc[imax]=0;
}
}
//affichage du tableau des occurences des lettres
// for(int i=0;i<26;i++)
// {
// System.out.print(tocc[i]+" ");
// }
System.out.print(argent);
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | da0bf44d51a654431dbca0f1f90120ab | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B462 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
long k=sc.nextInt();
String str=sc.next();
long[] a=new long[26];
for(int i=0;i<n;i++){
a[str.charAt(i)-'A']++;
}
Arrays.sort(a);
long output=0;
for (int i = a.length-1; i >=0; i--) {
if(a[i]<=k && k>0)
output+=a[i]*a[i];
else if(k>0)
output+=k*k;
else
break;
k=k-a[i];
}
System.out.println(output);
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 756ec64e67d84f60ba7e1ead8876222a | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.*;
public class CF_263_B_ApplemanAndCardGame {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int numLetters = in.nextInt();
int k = in.nextInt();
String line = in.next();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
ArrayList<Integer> values;
char currChar;
for (int i = 0; i < numLetters; i++) {
currChar = line.charAt(i);
if (map.containsKey(currChar)) {
int val = map.get(currChar);
map.put(currChar, val + 1);
}
else {
map.put(currChar, 1);
}
}
values = new ArrayList<Integer>(map.values());
Collections.sort(values);
Collections.reverse(values);
long total = 0;
int i = 0;
long currVal = 0;
long numChosen = 0;
while (true) {
currVal = values.get(i);
if (numChosen + currVal >= k) {
total += (k - numChosen) * (k - numChosen);
System.out.println(total);
return;
}
else {
total += currVal * currVal;
numChosen += currVal;
i++;
}
}
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 7d7126c22756fca965d5ee2b9501de0c | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Reader.init(System.in);
long n = Reader.nextLong() ;
long k = Reader.nextLong () ;
long sum = 0 ;
String s = Reader.next() ;
long [] a = new long [26] ;
for (int i=0 ; i<s.length(); i++) {
a[s.charAt(i)-'A']++ ;
}
Arrays.sort(a);
out:for (int j=a.length-1 ; j>=0 ; j--) {
if (a[j]<=k&&k>0&&a[j]>0) {
sum+=a[j]*a[j] ;
k-=a[j] ;
} else {
if (k==0)
break out ;
else if (a[j]>k){
sum+=k*k;
break out ;
}
}
}
System.out.println(sum);
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | f0efb6a845a2779b219b60d506c6318c | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String x = br.readLine();
String[] y = x.split(" ");
// int length = Integer.parseInt(y[0]) ;
long sel_l = Integer.parseInt(y[1]);
String z = br.readLine();
String[] m = z.split("");
String[] dis_l = new String[m.length];
int dis_l_l = 0;
int[] count_l = new int[m.length];
for (int uu = 0; uu < dis_l.length; uu++) {
dis_l[uu] = "";
}
for (int k = 0; k < m.length; k++) {
int it = 0;
boolean found = false;
while (!dis_l[it].equals(m[k])) {
it++;
if (it >= dis_l.length) {
break;
}
}
if (it >= dis_l.length) {
dis_l[dis_l_l] = m[k];
count_l[dis_l_l] = 1;
dis_l_l++;
} else {
count_l[it]++;
}
}
long points = 0;
while (sel_l != 0) {
long max = 0;
int max_index = -1;
for (int i = 0; i < count_l.length; i++) {
if (count_l[i] >= max) {
max = count_l[i];
max_index = i;
}
}
if (sel_l >= max) {
sel_l = sel_l - max;
points = points + max * max;
count_l[max_index] = 0;
} else {
points = points + sel_l * sel_l;
sel_l = 0;
}
}
System.out.println(points);
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 69ac9e0179ab602ba51e6f06d351dd32 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.Arrays;
import java.util.Hashtable;
import java.util.Scanner;
public class Solution{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
long[] c = new long[26];
String s = sc.next();
long r = 0;
for(int i=0;i<n;i++){
c[s.charAt(i)-'A']++;
}
Arrays.sort(c);
int i = 25;
while(true){
if(k>c[i]){
k -= c[i];
r += c[i]*c[i];
}else{
r += k*k;
System.out.println(r);
return;
}
i--;
}
}
} | Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | b7d5d25d32bf47c93ee90890791da8ee | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes | import java.util.*;
import java.lang.*;
public class B {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
String cards = input.next();
int[] counter = new int[26];
for(int i = 0; i < cards.length(); ++i) {
++counter[cards.charAt(i) - 'A'];
}
Arrays.sort(counter);
int index = 25;
long score = 0;
while(k > 0) {
long min = Math.min(k, counter[index]);
score += min * min;
k -= min;
--index;
}
System.out.println(score);
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 9f8506bf7e9d885970a01aa829f52197 | train_004.jsonl | 1409061600 | Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Locale;
import java.util.TreeSet;
/**
*
* @author edemairy
*/
public class Main {
private int nbTC;
private StringBuilder result = new StringBuilder();
private static class EndException extends RuntimeException {
}
public void run() throws IOException {
// Scanner scanner = new Scanner(System.in);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
nbTC = 1;//readInt(reader);
// nbTC = Integer.MAX_VALUE;
// scanner.nextLine();
try {
for (int tc = 1; tc <= nbTC; ++tc) {
result.append(oneTestCase(reader));
result.append('\n');
}
} catch (EndException e) {
}
System.out.print(result);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Main main = new Main();
main.run();
}
private class Letter implements Comparable<Letter> {
public char c;
public long nb;
public Letter(char c, int nb) {
this.c = c;
this.nb = nb;
}
// will give descending order.
@Override
public int compareTo(Letter o) {
if (o.nb > this.nb) {
return 1;
} else if (o.nb < this.nb) {
return -1;
} else {
return o.c - this.c;
}
}
}
private StringBuilder oneTestCase(BufferedReader reader) throws IOException {
Formatter formatter = new Formatter(Locale.ENGLISH);
StringBuilder output = new StringBuilder();
// for (int i = 0; i < 5; ++i) {
// formatter.format("%3d", n[i]);
// }
String[] parts = reader.readLine().split(" ");
int n = Integer.parseInt(parts[0]);
long k = Integer.parseInt(parts[1]);
String cards = reader.readLine();
int[] nb = new int[26];
for (int i=0; i<n; i++) {
nb[cards.charAt(i)-'A']++;
}
TreeSet<Letter> alphabet = new TreeSet<Letter>();
for (int i=0; i<26; i++) {
alphabet.add(new Letter((char)('A'+i), nb[i]));
}
long r = 0;
while (k>0) {
Letter l = alphabet.pollFirst();
if (l.nb <= k) {
k -= l.nb;
r += l.nb*l.nb;
} else {
r += k*k;
k = 0;
}
}
formatter.format("%d", r);
output.append(formatter.out());
return output;
}
private int readInt(BufferedReader reader) throws IOException {
int r = 0;
boolean positive = true;
char currentChar = (char) reader.read();
while ((currentChar == ' ') || (currentChar == '\n')) {
currentChar = (char) reader.read();
}
if (currentChar == (char) -1) {
throw new IOException("end of stream");
}
if (currentChar == '-') {
positive = false;
currentChar = (char) reader.read();
}
while ((currentChar >= '0') && (currentChar <= '9')) {
r = r * 10 + currentChar - '0';
currentChar = (char) reader.read();
}
if (positive) {
return r;
} else {
return -r;
}
}
private long readLong(BufferedReader reader) throws IOException {
long r = 0;
boolean positive = true;
char currentChar = (char) reader.read();
while ((currentChar == ' ') || (currentChar == '\n')) {
currentChar = (char) reader.read();
}
if (currentChar == (char) -1) {
throw new IOException("end of stream");
}
if (currentChar == '-') {
positive = false;
currentChar = (char) reader.read();
}
while ((currentChar >= '0') && (currentChar <= '9')) {
r = r * 10 + currentChar - '0';
currentChar = (char) reader.read();
}
if (positive) {
return r;
} else {
return -r;
}
}
private char readChar(BufferedReader reader) throws IOException {
return (char) reader.read();
}
}
| Java | ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"] | 1 second | ["82", "4"] | NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | Java 7 | standard input | [
"greedy"
] | 480defc596ee5bc800ea569fd76dc584 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman. | 1,300 | Print a single integer – the answer to the problem. | standard output | |
PASSED | 102efc5ed19bf60b00e4f479839aa0ed | train_004.jsonl | 1565188500 | You have a string $$$s$$$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right. Let $$$Grid(s)$$$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $$$s$$$. For example, if $$$s = \text{DSAWWAW}$$$ then $$$Grid(s)$$$ is the $$$4 \times 3$$$ grid: you can place the robot in the cell $$$(3, 2)$$$; the robot performs the command 'D' and moves to $$$(3, 3)$$$; the robot performs the command 'S' and moves to $$$(4, 3)$$$; the robot performs the command 'A' and moves to $$$(4, 2)$$$; the robot performs the command 'W' and moves to $$$(3, 2)$$$; the robot performs the command 'W' and moves to $$$(2, 2)$$$; the robot performs the command 'A' and moves to $$$(2, 1)$$$; the robot performs the command 'W' and moves to $$$(1, 1)$$$. You have $$$4$$$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $$$s$$$ to minimize the area of $$$Grid(s)$$$.What is the minimum area of $$$Grid(s)$$$ you can achieve? | 256 megabytes | import java.io.*;
public class WASDstring {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
String wasd = br.readLine();
int maxV = 0, minV = 0, maxH = 0, minH = 0;
int firstMaxV = 0, firstMinV = 0, lastMaxV = 0, lastMinV = 0, firstMaxH = 0, firstMinH = 0, lastMaxH = 0, lastMinH = 0;
int currH = 0, currV = 0;
for (int j = 0; j < wasd.length(); j++) {
if (wasd.charAt(j) == 'W') {
currV++;
if (currV > maxV) {
maxV = currV;
firstMaxV = j;
lastMaxV = j;
}
if (currV == maxV) {
lastMaxV = j;
}
}
if (wasd.charAt(j) == 'A') {
currH--;
if (currH < minH) {
minH = currH;
firstMinH = j;
lastMinH = j;
}
if (currH == minH) {
lastMinH = j;
}
}
if (wasd.charAt(j) == 'S') {
currV--;
if (currV < minV) {
minV = currV;
firstMinV = j;
lastMinV = j;
}
if (currV == minV) {
lastMinV = j;
}
}
if (wasd.charAt(j) == 'D') {
currH++;
if (currH > maxH) {
maxH = currH;
firstMaxH = j;
lastMaxH = j;
}
if (currH == maxH) {
lastMaxH = j;
}
}
}
long vertD = 1 + maxV - minV, horD = 1 + maxH - minH;
long area = vertD * horD;
if ((lastMinV < firstMaxV && (maxV > 1 || minV != 0))|| (lastMaxV < firstMinV && (minV < -1 || maxV != 0))) {
area = (vertD - 1) * horD;
}
if ((lastMinH < firstMaxH && (maxH > 1 || minH != 0)) || (lastMaxH < firstMinH && (minH < -1 || maxH != 0))) {
area = Long.min(area, vertD * (horD - 1));
}
System.out.println(area);
}
}
}
| Java | ["3\nDSAWWAW\nD\nWA"] | 2 seconds | ["8\n2\n4"] | NoteIn the first query you have to get string $$$\text{DSAWW}\underline{D}\text{AW}$$$.In second and third queries you can not decrease the area of $$$Grid(s)$$$. | Java 8 | standard input | [
"dp",
"greedy",
"math",
"implementation",
"data structures",
"brute force",
"strings"
] | a4f183775262fdc42dc5fc621c196ec9 | The first line contains one integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of queries. Next $$$T$$$ lines contain queries: one per line. This line contains single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$, $$$s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$$$) — the sequence of commands. It's guaranteed that the total length of $$$s$$$ over all queries doesn't exceed $$$2 \cdot 10^5$$$. | 2,100 | Print $$$T$$$ integers: one per query. For each query print the minimum area of $$$Grid(s)$$$ you can achieve. | standard output | |
PASSED | 707be69307d35952f98305c16ac7acbf | train_004.jsonl | 1565188500 | You have a string $$$s$$$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right. Let $$$Grid(s)$$$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $$$s$$$. For example, if $$$s = \text{DSAWWAW}$$$ then $$$Grid(s)$$$ is the $$$4 \times 3$$$ grid: you can place the robot in the cell $$$(3, 2)$$$; the robot performs the command 'D' and moves to $$$(3, 3)$$$; the robot performs the command 'S' and moves to $$$(4, 3)$$$; the robot performs the command 'A' and moves to $$$(4, 2)$$$; the robot performs the command 'W' and moves to $$$(3, 2)$$$; the robot performs the command 'W' and moves to $$$(2, 2)$$$; the robot performs the command 'A' and moves to $$$(2, 1)$$$; the robot performs the command 'W' and moves to $$$(1, 1)$$$. You have $$$4$$$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $$$s$$$ to minimize the area of $$$Grid(s)$$$.What is the minimum area of $$$Grid(s)$$$ you can achieve? | 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 sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, sc, out);
out.close();
}
static class Task {
public static final char up='W';
public static final char down='S';
public static final char left='A';
public static final char right='D';
public void solve(int testNumber, InputReader sc, PrintWriter out) {
int T=sc.nextInt();
while(T-->0) {
char[] res=(" "+sc.next()).toCharArray();
int x=0;
int y=0;
int n=res.length-1;
long[] preX=new long[res.length];
long[] preY=new long[res.length];
long max_x=0;
long min_x=0;
long max_y=0;
long min_y=0;
for(int i=1;i<=n;i++) {
int dx=0;
int dy=0;
if(res[i]==down||res[i]==up)
dx=res[i]==up?1:-1;
else
dy=res[i]==right?1:-1;
preX[i]=preX[i-1]+dx;
preY[i]=preY[i-1]+dy;
max_x=Math.max(max_x, preX[i]);
min_x=Math.min(min_x, preX[i]);
max_y=Math.max(max_y, preY[i]);
min_y=Math.min(min_y, preY[i]);
}
long ans=(max_x-min_x+1)*(max_y-min_y+1);
if(max_x-min_x+1>2) {
Node firstMin=new Node(0,0);
Node lastMax=new Node(0,0);
Node firstMax=new Node(0,0);
Node lastMin=new Node(0,0);
for(int i=1;i<=n;i++) {
if(preX[i]<firstMin.val) {
firstMin.val=preX[i];
firstMin.index=i;
}
if(preX[i]>=lastMax.val) {
lastMax.val=preX[i];
lastMax.index=i;
}
if(preX[i]<=lastMin.val) {
lastMin.val=preX[i];
lastMin.index=i;
}
if(preX[i]>firstMax.val) {
firstMax.val=preX[i];
firstMax.index=i;
}
}
if(firstMin.index>lastMax.index) { //+1
ans=Math.min(ans, (max_x-min_x)*(max_y-min_y+1));
}
if(firstMax.index>lastMin.index) { //-1
ans=Math.min(ans, (max_x-min_x)*(max_y-min_y+1));
}
}
if(max_y-min_y+1>2) {
Node firstMin=new Node(0,0);
Node lastMax=new Node(0,0);
Node firstMax=new Node(0,0);
Node lastMin=new Node(0,0);
for(int i=1;i<=n;i++) {
if(preY[i]<firstMin.val) {
firstMin.val=preY[i];
firstMin.index=i;
}
if(preY[i]>=lastMax.val) {
lastMax.val=preY[i];
lastMax.index=i;
}
if(preY[i]<=lastMin.val) {
lastMin.val=preY[i];
lastMin.index=i;
}
if(preY[i]>firstMax.val) {
firstMax.val=preY[i];
firstMax.index=i;
}
}
if(firstMin.index>lastMax.index) { //+1
ans=Math.min(ans, (max_x-min_x+1)*(max_y-min_y));
}
if(firstMax.index>lastMin.index) { //-1
ans=Math.min(ans, (max_x-min_x+1)*(max_y-min_y));
}
}
out.println(ans);
}
}
}
static class Node{
int index;
long val;
public Node(int index,int val) {
this.index=index;
this.val=val;
}
}
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\nDSAWWAW\nD\nWA"] | 2 seconds | ["8\n2\n4"] | NoteIn the first query you have to get string $$$\text{DSAWW}\underline{D}\text{AW}$$$.In second and third queries you can not decrease the area of $$$Grid(s)$$$. | Java 8 | standard input | [
"dp",
"greedy",
"math",
"implementation",
"data structures",
"brute force",
"strings"
] | a4f183775262fdc42dc5fc621c196ec9 | The first line contains one integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of queries. Next $$$T$$$ lines contain queries: one per line. This line contains single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$, $$$s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$$$) — the sequence of commands. It's guaranteed that the total length of $$$s$$$ over all queries doesn't exceed $$$2 \cdot 10^5$$$. | 2,100 | Print $$$T$$$ integers: one per query. For each query print the minimum area of $$$Grid(s)$$$ you can achieve. | standard output | |
PASSED | 51d8b26ba75c18b9047b2bef0f4657ed | train_004.jsonl | 1565188500 | You have a string $$$s$$$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right. Let $$$Grid(s)$$$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $$$s$$$. For example, if $$$s = \text{DSAWWAW}$$$ then $$$Grid(s)$$$ is the $$$4 \times 3$$$ grid: you can place the robot in the cell $$$(3, 2)$$$; the robot performs the command 'D' and moves to $$$(3, 3)$$$; the robot performs the command 'S' and moves to $$$(4, 3)$$$; the robot performs the command 'A' and moves to $$$(4, 2)$$$; the robot performs the command 'W' and moves to $$$(3, 2)$$$; the robot performs the command 'W' and moves to $$$(2, 2)$$$; the robot performs the command 'A' and moves to $$$(2, 1)$$$; the robot performs the command 'W' and moves to $$$(1, 1)$$$. You have $$$4$$$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $$$s$$$ to minimize the area of $$$Grid(s)$$$.What is the minimum area of $$$Grid(s)$$$ you can achieve? | 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 sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, sc, out);
out.close();
}
static class Task {
public static final char up='W';
public static final char down='S';
public static final char left='A';
public static final char right='D';
public void solve(int testNumber, InputReader sc, PrintWriter out) {
int T=sc.nextInt();
while(T-->0) {
char[] res=(" "+sc.next()).toCharArray();
int x=0;
int y=0;
int n=res.length-1;
long[] preX=new long[res.length];
long[] preY=new long[res.length];
long max_x=0;
long min_x=0;
long max_y=0;
long min_y=0;
for(int i=1;i<=n;i++) {
int dx=0;
int dy=0;
if(res[i]==down||res[i]==up)
dx=res[i]==up?1:-1;
else
dy=res[i]==right?1:-1;
preX[i]=preX[i-1]+dx;
preY[i]=preY[i-1]+dy;
max_x=Math.max(max_x, preX[i]);
min_x=Math.min(min_x, preX[i]);
max_y=Math.max(max_y, preY[i]);
min_y=Math.min(min_y, preY[i]);
}
long ans=(max_x-min_x+1)*(max_y-min_y+1);
if(max_x-min_x+1>2) {
Node firstMin=new Node(0,0);
Node lastMax=new Node(0,0);
for(int i=1;i<=n;i++) {
if(preX[i]<firstMin.val) {
firstMin.val=preX[i];
firstMin.index=i;
}
if(preX[i]>=lastMax.val) {
lastMax.val=preX[i];
lastMax.index=i;
}
}
if(firstMin.index>lastMax.index) { //+1
ans=Math.min(ans, (max_x-min_x)*(max_y-min_y+1));
}
for(int i=1;i<=n;i++)
preX[i]=-preX[i];
firstMin=new Node(0,0);
lastMax=new Node(0,0);
for(int i=1;i<=n;i++) {
if(preX[i]<firstMin.val) {
firstMin.val=preX[i];
firstMin.index=i;
}
if(preX[i]>=lastMax.val) {
lastMax.val=preX[i];
lastMax.index=i;
}
}
if(firstMin.index>lastMax.index) { //-1
ans=Math.min(ans, (max_x-min_x)*(max_y-min_y+1));
}
}
if(max_y-min_y+1>2) {
Node firstMin=new Node(0,0);
Node lastMax=new Node(0,0);
for(int i=1;i<=n;i++) {
if(preY[i]<firstMin.val) {
firstMin.val=preY[i];
firstMin.index=i;
}
if(preY[i]>=lastMax.val) {
lastMax.val=preY[i];
lastMax.index=i;
}
}
if(firstMin.index>lastMax.index) { //+1
ans=Math.min(ans, (max_x-min_x+1)*(max_y-min_y));
}
for(int i=1;i<=n;i++)
preY[i]=-preY[i];
firstMin=new Node(0,0);
lastMax=new Node(0,0);
for(int i=1;i<=n;i++) {
if(preY[i]<firstMin.val) {
firstMin.val=preY[i];
firstMin.index=i;
}
if(preY[i]>=lastMax.val) {
lastMax.val=preY[i];
lastMax.index=i;
}
}
if(firstMin.index>lastMax.index) { //-1
ans=Math.min(ans, (max_x-min_x+1)*(max_y-min_y));
}
}
out.println(ans);
}
}
}
static class Node{
int index;
long val;
public Node(int index,int val) {
this.index=index;
this.val=val;
}
}
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\nDSAWWAW\nD\nWA"] | 2 seconds | ["8\n2\n4"] | NoteIn the first query you have to get string $$$\text{DSAWW}\underline{D}\text{AW}$$$.In second and third queries you can not decrease the area of $$$Grid(s)$$$. | Java 8 | standard input | [
"dp",
"greedy",
"math",
"implementation",
"data structures",
"brute force",
"strings"
] | a4f183775262fdc42dc5fc621c196ec9 | The first line contains one integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of queries. Next $$$T$$$ lines contain queries: one per line. This line contains single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$, $$$s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$$$) — the sequence of commands. It's guaranteed that the total length of $$$s$$$ over all queries doesn't exceed $$$2 \cdot 10^5$$$. | 2,100 | Print $$$T$$$ integers: one per query. For each query print the minimum area of $$$Grid(s)$$$ you can achieve. | standard output | |
PASSED | 95db471704609c962137115936167863 | train_004.jsonl | 1565188500 | You have a string $$$s$$$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right. Let $$$Grid(s)$$$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $$$s$$$. For example, if $$$s = \text{DSAWWAW}$$$ then $$$Grid(s)$$$ is the $$$4 \times 3$$$ grid: you can place the robot in the cell $$$(3, 2)$$$; the robot performs the command 'D' and moves to $$$(3, 3)$$$; the robot performs the command 'S' and moves to $$$(4, 3)$$$; the robot performs the command 'A' and moves to $$$(4, 2)$$$; the robot performs the command 'W' and moves to $$$(3, 2)$$$; the robot performs the command 'W' and moves to $$$(2, 2)$$$; the robot performs the command 'A' and moves to $$$(2, 1)$$$; the robot performs the command 'W' and moves to $$$(1, 1)$$$. You have $$$4$$$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $$$s$$$ to minimize the area of $$$Grid(s)$$$.What is the minimum area of $$$Grid(s)$$$ you can achieve? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/*
* 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.
*/
/**
*
* @author Andy Phan
*/
public class c {
public static void main(String[] args) {
FS in = new FS(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
int[] map = new int['Z'];
map['W'] = 0;
map['S'] = 1;
map['A'] = 2;
map['D'] = 3;
while(T-->0) {
String s = in.next();
int n = s.length();
long[][] bounds = new long[4][n+1]; //0 = W, 1 = S, 2 = A, 3 = D
for(int i = 0; i < n; i++) {
int type = map[s.charAt(i)];
bounds[type][i+1] = Math.max(0, bounds[type][i]-1);
bounds[type^1][i+1] = bounds[type^1][i]+1;
bounds[type^2][i+1] = bounds[type^2][i];
bounds[type^3][i+1] = bounds[type^3][i];
}
long[][] bounds2 = new long[4][n+1]; //0 = W, 1 = S, 2 = A, 3 = D
for(int i = n-1; i >= 0; i--) {
int type = map[s.charAt(i)]^1;
bounds2[type][i] = Math.max(0, bounds2[type][i+1]-1);
bounds2[type^1][i] = bounds2[type^1][i+1]+1;
bounds2[type^2][i] = bounds2[type^2][i+1];
bounds2[type^3][i] = bounds2[type^3][i+1];
}
long min = (bounds2[0][0]+bounds2[1][0]+1)*(bounds2[2][0]+bounds2[3][0]+1);
for(int i = 0; i < n; i++) {
for(int j = 0; j < 4; j++) {
long[] bound = new long[4];
for(int k = 0; k < 4; k++) bound[k] = bounds[k][i];
bound[j] = Math.max(bound[j], Math.max(0, bounds2[j][i]-1));
bound[j^1] = Math.max(bound[j^1], bounds2[j^1][i]+1);
bound[j^2] =Math.max(bound[j^2], bounds2[j^2][i]);
bound[j^3] = Math.max(bound[j^3], bounds2[j^3][i]);
long size = (bound[0]+bound[1]+1)*(bound[2]+bound[3]+1);
min = Math.min(min, size);
}
}
System.out.println(min);
}
out.close();
}
static class FS {
BufferedReader in;
StringTokenizer token;
public FS(InputStream str) {
in = new BufferedReader(new InputStreamReader(str));
}
public String next() {
if (token == null || !token.hasMoreElements()) {
try {
token = new StringTokenizer(in.readLine());
} catch (IOException ex) {
}
return next();
}
return token.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["3\nDSAWWAW\nD\nWA"] | 2 seconds | ["8\n2\n4"] | NoteIn the first query you have to get string $$$\text{DSAWW}\underline{D}\text{AW}$$$.In second and third queries you can not decrease the area of $$$Grid(s)$$$. | Java 8 | standard input | [
"dp",
"greedy",
"math",
"implementation",
"data structures",
"brute force",
"strings"
] | a4f183775262fdc42dc5fc621c196ec9 | The first line contains one integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of queries. Next $$$T$$$ lines contain queries: one per line. This line contains single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$, $$$s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$$$) — the sequence of commands. It's guaranteed that the total length of $$$s$$$ over all queries doesn't exceed $$$2 \cdot 10^5$$$. | 2,100 | Print $$$T$$$ integers: one per query. For each query print the minimum area of $$$Grid(s)$$$ you can achieve. | standard output | |
PASSED | 01148c25133f83786a966e5c5ad1ba78 | train_004.jsonl | 1565188500 | You have a string $$$s$$$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right. Let $$$Grid(s)$$$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $$$s$$$. For example, if $$$s = \text{DSAWWAW}$$$ then $$$Grid(s)$$$ is the $$$4 \times 3$$$ grid: you can place the robot in the cell $$$(3, 2)$$$; the robot performs the command 'D' and moves to $$$(3, 3)$$$; the robot performs the command 'S' and moves to $$$(4, 3)$$$; the robot performs the command 'A' and moves to $$$(4, 2)$$$; the robot performs the command 'W' and moves to $$$(3, 2)$$$; the robot performs the command 'W' and moves to $$$(2, 2)$$$; the robot performs the command 'A' and moves to $$$(2, 1)$$$; the robot performs the command 'W' and moves to $$$(1, 1)$$$. You have $$$4$$$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $$$s$$$ to minimize the area of $$$Grid(s)$$$.What is the minimum area of $$$Grid(s)$$$ you can achieve? | 256 megabytes | //package com.company;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Solution_C {
static class Point {
public long x, y;
public Point(long x, long y) {
this.x = x;
this.y = y;
}
}
static class Rectangle {
public Point downLeft;
public Point topRight;
public Rectangle(Point a, Point b) {
downLeft = a;
topRight = b;
}
public Rectangle(Rectangle r) {
downLeft = r.downLeft;
topRight = r.topRight;
}
public long getArea() {
return (topRight.x - downLeft.x + 1) * (topRight.y - downLeft.y + 1);
}
}
public static long calcArea(Rectangle a, Rectangle b) {
long minX = Math.min(Math.min(a.downLeft.x, a.topRight.x), Math.min(b.downLeft.x, b.topRight.x));
long maxX = Math.max(Math.max(a.downLeft.x, a.topRight.x), Math.max(b.downLeft.x, b.topRight.x));
long minY = Math.min(Math.min(a.downLeft.y, a.topRight.y), Math.min(b.downLeft.y, b.topRight.y));
long maxY = Math.max(Math.max(a.downLeft.y, a.topRight.y), Math.max(b.downLeft.y, b.topRight.y));
Rectangle r = new Rectangle(new Point(minX, minY), new Point(maxX, maxY));
return r.getArea();
}
public static long solve(String s) {
List<Point> positions = new ArrayList<>();
List<Rectangle> rectangles = new ArrayList<>();
long currX = 0, currY = 0;
long minX = 0, maxX = 0;
long minY = 0, maxY = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'W') {
currY++;
}
else if(s.charAt(i) == 'S') {
currY--;
}
else if(s.charAt(i) == 'D') {
currX++;
}
else if(s.charAt(i) == 'A') {
currX--;
}
positions.add(new Point(currX, currY));
minX = Math.min(minX, currX);
maxX = Math.max(maxX, currX);
minY = Math.min(minY, currY);
maxY = Math.max(maxY, currY);
rectangles.add(new Rectangle(new Point(minX, minY), new Point(maxX, maxY)));
// System.out.println("currArea: " + rectangles.get(rectangles.size()-1).getArea());
}
minX = positions.get(positions.size() -1 ).x; maxX = positions.get(positions.size() -1 ).x;
minY = positions.get(positions.size() -1 ).y; maxY = positions.get(positions.size() -1 ).y;
long result = rectangles.get(rectangles.size() - 1).getArea();
for(int i = positions.size() - 1; i >= 0; i--) {
currX = positions.get(i).x;
currY = positions.get(i).y;
minX = Math.min(minX, currX);
maxX = Math.max(maxX, currX);
minY = Math.min(minY, currY);
maxY = Math.max(maxY, currY);
Rectangle currR = new Rectangle(new Point(minX, minY+1), new Point(maxX, maxY+1));
result = Math.min(result, calcArea(rectangles.get(i), currR));
currR = new Rectangle(new Point(minX, minY-1), new Point(maxX, maxY-1));
result = Math.min(result, calcArea(rectangles.get(i), currR));
currR = new Rectangle(new Point(minX+1, minY), new Point(maxX+1, maxY));
result = Math.min(result, calcArea(rectangles.get(i), currR));
currR = new Rectangle(new Point(minX-1, minY), new Point(maxX-1, maxY));
result = Math.min(result, calcArea(rectangles.get(i), currR));
}
return result;
}
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
FastScanner sc = new FastScanner(in);
PrintWriter out = new PrintWriter(System.out);
int T = sc.nextInt();
for(int i = 0; i < T; i++) {
String s = sc.nextToken();
out.println(solve(s));
}
// Rectangle r1 = new Rectangle(new Point(0 , 0), new Point(0, 1));
// Rectangle r2 = new Rectangle(new Point(1 , 0), new Point(1, 1));
// Rectangle r3 = new Rectangle(new Point(0 , 0), new Point(1, 1));
// System.out.println(calcArea(r1, r2));
// System.out.println(r3.getArea());
out.close();
}
}
/* Class that simulates Scanner - but Faster?(taken from user on CodeForces) */
class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}
| Java | ["3\nDSAWWAW\nD\nWA"] | 2 seconds | ["8\n2\n4"] | NoteIn the first query you have to get string $$$\text{DSAWW}\underline{D}\text{AW}$$$.In second and third queries you can not decrease the area of $$$Grid(s)$$$. | Java 8 | standard input | [
"dp",
"greedy",
"math",
"implementation",
"data structures",
"brute force",
"strings"
] | a4f183775262fdc42dc5fc621c196ec9 | The first line contains one integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of queries. Next $$$T$$$ lines contain queries: one per line. This line contains single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$, $$$s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$$$) — the sequence of commands. It's guaranteed that the total length of $$$s$$$ over all queries doesn't exceed $$$2 \cdot 10^5$$$. | 2,100 | Print $$$T$$$ integers: one per query. For each query print the minimum area of $$$Grid(s)$$$ you can achieve. | standard output | |
PASSED | 7abaa54215e8233b550832eb8da6226a | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class E
{
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static MyScanner sc = new MyScanner(System.in);
static StringBuilder result = new StringBuilder();
public static void main(String... args)
{
int n = sc.nextInt();
int[][] matrix = new int[n][n];
boolean[][] c = new boolean[n][n];
boolean[][] r = new boolean[n][n];
int num = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j)
{
while (matrix[i][j] == 0)
{
// fill
if (!r[i][num] && !c[j][num])
{
r[i][num] = true;
c[j][num] = true;
matrix[i][j] = num;
r[j][num] = true;
c[i][num] = true;
matrix[j][i] = num;
}
num++;
if (num == n)
{
num = 1;
}
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
result.append(matrix[i][j]);
result.append(" ");
}
result.append("\n");
}
System.out.println(result);
}
/**
* Flatfoot's Scanner with slight modifications.
* @author <a href="http://codeforces.com/profile/Flatfoot">Flatfoot</a>
* @see <a href="http://codeforces.com/blog/entry/7018">Source</a>
*/
private static class MyScanner
{
BufferedReader br;
StringTokenizer st;
MyScanner(InputStream in)
{
this.br = new BufferedReader(new InputStreamReader(in));
}
String next()
{
while (this.st == null || !this.st.hasMoreElements())
{
try
{
this.st = new StringTokenizer(this.br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return this.st.nextToken();
}
int nextInt()
{
return Integer.parseInt(this.next());
}
long nextLong()
{
return Long.parseLong(this.next());
}
double nextDouble()
{
return Double.parseDouble(this.next());
}
String nextLine()
{
String str = "";
try
{
str = this.br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 8 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | bc9df390cddd0f4c98b6ac79cc61f0f9 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CodeForces {
public static void main(String[] args) {
FastReader reader = new FastReader();
int n = reader.nextInt();
long start = System.currentTimeMillis();
int[][] m = new int[n][n];
for (int col = 1; col < n; col++) {
for (int i = 0, j = col; j >= 0; i++, j--) {
if (i != j) {
m[i][j] = col;
}
}
}
for (int col = 2; col < n - 2; col++) {
for (int i = n - 2, j = col; i >= col; i--, j++) {
if (i != j) {
m[i][j] = col - 1;
}
}
}
int even = 2;
for (int j = 1; j < n / 2; j++) {
m[m.length - 1][j] = even;
m[j][m.length - 1] = even;
even += 2;
}
int odd = 1;
for (int j = n / 2; j < n - 1; j++) {
m[m.length - 1][j] = odd;
m[j][m.length - 1] = odd;
odd += 2;
}
printMatrix(m);
}
private static void printMatrix(int[][] m) {
StringBuilder sb = new StringBuilder();
for (int[] row : m) {
for (int e : row) {
sb.append(e);
sb.append(" ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}
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() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
} | Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 8 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 5b2835a3c1205147bfd647cc39c02fa6 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.*;
import java.io.*;
import java.text.*;
import java.math.*;
import static java.lang.Integer.*;
import static java.lang.Double.*;
import java.lang.Math.*;
public class start_of_the_season {
public static void main(String[] args) throws Exception {
new start_of_the_season().run();
}
public void run() throws Exception {
FastIO file = new FastIO();
PrintWriter out = new PrintWriter(System.out);
int n = file.nextInt() - 1;
int[][] a = new int[n + 1][n + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = (i + j) % (n) + 1;
}
}
for (int i = 0; i < n; i++) {
a[i][n] = a[i][i];
a[n][i] = a[i][i];
}
for (int i = 0; i < n + 1; i++) a[i][i] = 0;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n + 1; j++) {
out.print(a[i][j]);
out.print(" ");
}
out.println();
}
out.flush();
}
public static class FastIO {
BufferedReader br;
StringTokenizer st;
public FastIO() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long pow(long n, long p, long mod) {
if (p == 0)
return 1;
if (p == 1)
return n % mod;
if (p % 2 == 0) {
long temp = pow(n, p / 2, mod);
return (temp * temp) % mod;
} else {
long temp = pow(n, (p - 1) / 2, mod);
temp = (temp * temp) % mod;
return (temp * n) % mod;
}
}
public static long pow(long n, long p) {
if (p == 0)
return 1;
if (p == 1)
return n;
if (p % 2 == 0) {
long temp = pow(n, p / 2);
return (temp * temp);
} else {
long temp = pow(n, (p - 1) / 2);
temp = (temp * temp);
return (temp * n);
}
}
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 8 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 762473441261c4e2f6379597ea1fced3 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.Scanner;
public class R012E {
int n;
public R012E() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt() - 1;
}
private void process() {
int[][] array = new int[n+1][n+1];
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
array[i][j] = 1 + (i + j) % n;
}
}
for(int i=0; i<n; i++) {
array[i][n] = array[n][i] = array[i][i];
array[i][i] = 0;
}
for(int i=0; i<=n; i++) {
for(int j=0; j<=n; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
new R012E().process();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | b1a51f0bc2646faa83544f14d53ea3db | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main implements Runnable {
public void solution() throws IOException {
int n = in.nextInt();
int[][] a = new int[n][n];
boolean[][] r = new boolean[n][n];
boolean[][] c = new boolean[n][n];
for (int i = 0; i < n; ++i) {
r[i][0] = true;
c[i][0] = true;
}
for (int i = 0; i < a.length; ++i) {
int cur = i;
for (int j = 0; j < n; ++j) {
if (i == j) {
if (j + 1 < n) {
cur = next(i, j + 1, cur, r, c);
}
} else {
a[i][j] = cur;
r[i][cur] = true;
c[j][cur] = true;
if (j + 1 < n) {
cur = next(i, j + 1, cur, r, c);
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 0) {
out.print(' ');
}
out.print(a[i][j]);
}
out.println();
}
}
private int next(int i, int j, int cur, boolean[][] r, boolean[][] c) {
j %= r.length;
cur = (cur + 1) % r.length;
for (int k = 0; k < 2 * r.length; ++k) {
if (r[i][cur] || c[j][cur]) {
cur = (cur + 1) % r.length;
} else {
break;
}
}
return cur;
}
public void run() {
try {
solution();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws IOException {
new Thread(null, new Main(), "", 1 << 28).start();
}
private class Scanner {
BufferedReader reader;
StringTokenizer tokenizer;
public Scanner(BufferedReader reader) {
this.reader = reader;
this.tokenizer = new StringTokenizer("");
}
public boolean hasNext() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String next = reader.readLine();
if (next == null) {
return false;
}
tokenizer = new StringTokenizer(next);
}
return true;
}
public String next() throws IOException {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
} | Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | c04fac32048aa1dca4f41493088da8f2 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class E {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
System.in)));
while (sc.hasNextInt()) {
int n = sc.nextInt();
char[][] number = new char[n][];
for (int i = 0; i < n; i++) {
number[i] = Integer.toString(i).toCharArray();
}
int[][] a = new int[n][n];
for (int i = 0; i < n - 1; i++) {
a[0][i] = i;
}
a[0][0] = n - 1;
for (int i = 1; i < n - 1; i++) {
for (int j = 0; j < n - 2; j++) {
a[i][j] = a[i - 1][j + 1];
}
a[i][n - 2] = a[i - 1][0];
}
for (int i = 0; i < n - 1; i++) {
a[i][n - 1] = a[i][i];
a[i][i] = 0;
}
for (int i = 0; i < n - 1; i++) {
a[n - 1][i] = a[i][n - 1];
}
a[n - 1][n - 1] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j > 0)
System.out.write(' ');
for (int k = 0; k < number[a[i][j]].length; k++)
System.out.write(number[a[i][j]][k]);
}
System.out.write('\n');
}
System.out.flush();
}
System.out.close();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 1be8cbb30c5c8bfd277c8e7f87465952 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
int a[][];
int n;
public void run() throws Exception{
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new int[n+1][n+1];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j] = (i+j)%n;
for(int i=1;i<n;i++) doIt(i);
for(int i=1;i<n;i++){
a[i][n-1] = a[i][i];
a[i][i] = 0;
}
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
a[i][j] = a[j][i];
PrintWriter out = new PrintWriter(System.out);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
out.print(a[i][j]+" ");
out.println();
}
out.close();
}
private void doIt(int x){
for(int i=0;i<n;i++)
if (a[x][i] == 0){
for(int j=i;j<n;j++)
a[x][j] = a[x][j+1];
break;
}
}
public static void main(String args[]) throws Exception{
new Main().run();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 245d657405b03013d4aa6499fe2d9e6c | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int N = kb.nextInt();
int i,j,k,a,b,c;
int tb[][] = new int[1005][1005];
for(i=0;i<N;i++)
{
for(a=i,b=0;a>=0;a--,b++)
tb[a][b]=i;
}
for(i=2;i<N-1;i++)
{
for(a=N-2,b=i;b<N-1;a--,b++)
tb[a][b]=i-1;
}
for(i=1;i<N/2;i++)
{
tb[i][N-1]=2*i;
tb[N-1][i]=2*i;
}
for(i=0;i<N/2;i++)
{
tb[N/2+i][N-1]=2*i+1;
tb[N-1][N/2+i]=2*i+1;
}
for(i=0;i<N;i++)
tb[i][i]=0;
for(i=0;i<N;i++)
{
System.out.print(tb[i][0]+" ");
for(j=1;j<N;j++)
System.out.print(tb[i][j]+" ");
System.out.println("");
}
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 77e900315cd08314f2395d4ed4ed6052 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
int n = r.nextInt();
int[][] a = new int[n][n];
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-1; j++)
a[i][j] = 1 + (i+j)%(n-1);
for(int j = 0; j < n-1; j++)
a[n-1][j] = a[j][n-1] = a[j][j];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)
if(i == j)System.out.print("0 ");
else System.out.print(a[i][j] + " ");
System.out.println();
}
// for(int i = 0; i < n; i++){
// for(int j = 0; j < n; j++)
// if(i != j)System.out.print(1 + (n-1+a[i][j])%(n-1)+" ");
// else System.out.print(0+" ");
// System.out.println();
// }
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 4858d1abc641df7d5b546141fd7fec56 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
input = new BufferedReader(new InputStreamReader(System.in));
// input = new BufferedReader(new FileReader("input.txt"));
output = new PrintWriter(System.out);
tokenizer = new StreamTokenizer(input);
int n = nextInt() - 1;
int[][] array = new int[n + 1][n + 1];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
array[i][j] = 1 + (i + j) % n;
for(int i = 0; i < n; i++) {
array[i][n] = array[n][i] = array[i][i];
array[i][i] = 0;
}
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
output.print(array[i][j]);
output.print(' ');
}
output.println();
}
input.close();
output.close();
}
static BufferedReader input;
static PrintWriter output;
static StreamTokenizer tokenizer;
static int nextInt() throws IOException {
tokenizer.nextToken();
return (int)tokenizer.nval;
}
static double nextDouble() throws IOException {
tokenizer.nextToken();
return tokenizer.nval;
}
static String nextWord() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
static String nextLine() throws IOException {
return input.readLine();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 58764f49c8b750104b10d1c0c46ad4e9 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes |
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author madi
*/
public class SeasonStart {
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
matrix[i][j] = 0;
} else if (i + j < n) {
matrix[i][j] = i + j;
} else if (i == n - 1 || j == n - 1) {
if (i < n - 1) {
if (i < n / 2) {
matrix[i][j] = i * 2;
} else {
matrix[i][j] = (i - n / 2) * 2 + 1;
}
} else {
if (j < n / 2) {
matrix[i][j] = j * 2;
} else {
matrix[i][j] = (j - n / 2) * 2 + 1;
}
}
} else {
matrix[i][j] = (i + j) - (n - 1);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 06bdd20c25a2fa5a55454434ddd268c5 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.Scanner;
public class C12E {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m[][]=new int[n][n];
if(n==2){
m[0][0]=0;
m[0][1]=1;
m[1][0]=1;
m[1][1]=0;
}
else{
int ind=0;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1;j++){
if(i!=j){
if((i+j)<=n-1)
m[i][j]=i+j;
else
m[i][j]=i+j-(n-1);
}
}
}
m[n-1][0]=n-1;
m[0][n-1]=n-1;
for(int i=1;i<n-1;i++){
if(i<=(n-1)/2){
m[n-1][i]=2*i;
m[i][n-1]=2*i;
}
else{
m[n-1][i]=2*(i-((n-1)/2))-1;
m[i][n-1]=2*(i-((n-1)/2))-1;
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 1bec7a68828621eda86453167acdfb35 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int N = kb.nextInt();
int i, j, a, b;
int tb[][] = new int[1005][1005];
for (i = 0; i < N; i++) {
for (a = i, b = 0; a >= 0; a--, b++)
tb[a][b] = i;
}
for (i = 2; i < N - 1; i++) {
for (a = N - 2, b = i; b < N - 1; a--, b++)
tb[a][b] = i - 1;
}
for (i = 1; i < N / 2; i++) {
tb[i][N - 1] = 2 * i;
tb[N - 1][i] = 2 * i;
}
for (i = 0; i < N / 2; i++) {
tb[N / 2 + i][N - 1] = 2 * i + 1;
tb[N - 1][N / 2 + i] = 2 * i + 1;
}
for (i = 0; i < N; i++)
tb[i][i] = 0;
for (i = 0; i < N; i++) {
System.out.print(tb[i][0] + " ");
for (j = 1; j < N; j++)
System.out.print(tb[i][j] + " ");
System.out.println("");
}
}
} | Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 4d0d746bc4edef08170759e4d992517d | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes |
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author madi
*/
public class SeasonStart {
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
matrix[i][j] = 0;
} else if (i + j < n) {
matrix[i][j] = i + j;
} else if (i == n - 1 || j == n - 1) {
if (i < n - 1) {
if (i < n / 2) {
matrix[i][j] = i * 2;
} else {
matrix[i][j] = (i - n / 2) * 2 + 1;
}
} else {
if (j < n / 2) {
matrix[i][j] = j * 2;
} else {
matrix[i][j] = (j - n / 2) * 2 + 1;
}
}
} else {
matrix[i][j] = (i + j) - (n - 1);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
} | Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 3a1a49f487fce58d4f648ccfdafff2e3 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;
public class Main implements Runnable {
/////////////////////////////////////////////////////////////////
// Solution
private void solve() throws Throwable {
int n = nextInt();
int[][] a = new int[n][n];
boolean[][] hor = new boolean[n][n], ver = new boolean[n][n];
for (int k = 1; k < n; k++) {
for (int i = 1; i <= k; i++) {
int j = k - i;
if (i == j)
continue;
a[i][j] = a[j][i] = k;
hor[i][k] = hor[j][k] = ver[i][k] = ver[j][k] = true;
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i][j] != 0)
continue;
for (int k = 1; k < n; k++) {
if (!hor[i][k] && !ver[j][k]) {
a[i][j] = a[j][i] = k;
hor[i][k] = hor[j][k] = ver[i][k] = ver[j][k] = true;
break;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
pw.print(a[i][j]);
pw.print(' ');
}
pw.println();
}
}
/////////////////////////////////////////////////////////////////
// Utility functions
PrintWriter pw;
BufferedReader in;
StringTokenizer st;
void initStreams() throws FileNotFoundException,
UnsupportedEncodingException {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.setIn(new FileInputStream("1"));
}
pw = new PrintWriter(System.out);
in = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-9"));
}
String nextString() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextString());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextString());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextString());
}
static Throwable sError;
public static void main(String[] args) throws Throwable {
Thread t = new Thread(new Main());
t.start();
t.join();
if (sError != null)
throw sError;
}
public void run() {
try {
initStreams();
solve();
} catch (Throwable e) {
sError = e;
} finally {
if (pw != null)
pw.close();
}
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | bb93e9a8a1338edc7611ef24618fa262 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.util.Scanner;
public class E {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[][] res = new int[n][n];
for (int i=0; i<n-1; ++i)
for (int j=0; j<n-1; ++j)
res[i][j] = 1 + (i+j) % (n-1);
for (int i=0; i<n-1; ++i) {
res[i][n-1] = res[n-1][i] = res[i][i];
res[i][i] = 0;
}
for (int i=0; i<n; ++i) {
for (int j=0; j<n-1; ++j)
System.out.print(res[i][j] + " ");
System.out.println(res[i][n-1]);
}
}
} | Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | f75eb601fd5fd4ad80f70f9015b921a1 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class _12_d2_E {
boolean showDebug = true;
public void solve() throws Exception {
int n = nextInt();
int[][] m = new int[n][n];
for (int i=1; i<n; i++)
for (int j=0; j<i; j++)
if (i==n-1) m[i][j]=m[j][i] = 1+(i+2*j-1)%(n-1);
else m[i][j]=m[j][i] = 1+(i+j-1)%(n-1);
for (int[] p:m)
printlnArr(p);
}
////////////////////////////////////////////////////////////////////////////
double EPS = 1e-7;
int INF = Integer.MAX_VALUE;
long INFL = Long.MAX_VALUE;
double INFD = Double.MAX_VALUE;
int[] dx = {0,1,0,-1};
int[] dy = {-1,0,1,0};
int[] dx8 = {0,1,1,1,0,-1,-1,-1};
int[] dy8 = {-1,-1,0,1,1,1,0,-1};
int[] knightMovesX = {1,2,2,1,-1,-2,-2,-1};
int[] knightMovesY = {-2,-1,1,2,2,1,-1,-2};
@SuppressWarnings("serial")
class IncMap extends HashMap<Object, Integer> {
boolean add(Object key, int amount) {
Integer i = get(key);
if (i!=null) {
put(key, i+amount);
return false;
} else {
put(key, amount);
return true;
}
}
boolean add(Object key) {
return add(key, 1);
}
}
int min(int... nums) {
int r = INF;
for (int i: nums)
if (i<r) r=i;
return r;
}
int max(int... nums) {
int r = -INF;
for (int i: nums)
if (i>r) r=i;
return r;
}
long minL(long... nums) {
long r = INFL;
for (long i: nums)
if (i<r) r=i;
return r;
}
long maxL(long... nums) {
long r = -INFL;
for (long i: nums)
if (i>r) r=i;
return r;
}
double minD(double... nums) {
double r = INFD;
for (double i: nums)
if (i<r) r=i;
return r;
}
double maxD(double... nums) {
double r = -INFD;
for (double i: nums)
if (i>r) r=i;
return r;
}
long sumArr(int[] arr) {
long res = 0;
for (int i: arr)
res+=i;
return res;
}
long sumArr(long[] arr) {
long res = 0;
for (long i: arr)
res+=i;
return res;
}
double sumArr(double[] arr) {
double res = 0;
for (double i: arr)
res+=i;
return res;
}
long partsFitCnt(long partSize, long wholeSize) {
return (partSize+wholeSize-1)/partSize;
}
int digitSum(long i) {
i = abs(i);
int r = 0;
while (i>0) {
r+=i%10;
i/=10;
}
return r;
}
long digitProd(long i) {
if (i==0) return 0;
i = abs(i);
long r = 1;
while (i>0) {
r*=i%10;
i/=10;
}
return r;
}
long gcd (long a, long b) {
while (b>0) {
a%=b;
long tmp=a; a=b; b=tmp;
}
return a;
}
long lcm(long a, long b) {
return (a*b)/gcd(a,b);
}
double log_2 = log(2);
double log2(double i) {
if (i<=0) return -INFD;
return log(i)/log_2;
}
long binpow(int x, int n) {
long r = 1;
while (n>0) {
if ((n&1)!=0) r*=x;
x*=x;
n>>=1;
}
return r;
}
long fac(int i) {
if (i>20) throw new IllegalArgumentException();
return i<=1 ? 1:fac(i-1)*i;
}
double dist(double x, double y, double xx, double yy) {
return sqrt((xx-x)*(xx-x)+(yy-y)*(yy-y));
}
boolean isPalindrome(String s) {
for (int i=0; i<s.length()/2; i++)
if (s.charAt(i)!=s.charAt(s.length()-1-i)) return false;
return true;
}
int occurenciesCnt(String s, String pattern) {
int res = 0;
for (int i=0; i<s.length()-pattern.length()+1; i++)
if (s.substring(i, i+pattern.length()).equals(pattern)) res++;
return res;
}
int occurenciesCnt(String s, char pattern) {
int res = 0;
for (int i=0; i<s.length(); i++)
if (s.charAt(i)==pattern) res++;
return res;
}
int[] months = {0,31,28,31,30,31,30,31,31,30,31,30,31};
boolean isLeapYear(int y) {
return y%4==0 && (y%400==0 || y%100!=0);
}
boolean isValidDate(int d, int m, int y) {
if (isLeapYear(y) && m==2 && d==29) return true;
return m>0 && m<=12 && d>0 && d<=months[d];
}
int[] nextDay(int d, int m, int y) {
if (d>=months[m])
if (m==2 && d==28 && isLeapYear(y)) d++;
else {d=1; m++;}
else
d++;
if (m==13) {d=1; m=1; y++;}
return new int[] {d,m,y};
}
String str(Object o) {
return o.toString();
}
long timer = System.currentTimeMillis();
void startTimer() {
timer = System.currentTimeMillis();
}
void stopTimer() {
System.err.println("time: "+(System.currentTimeMillis()-timer)/1000.0);
}
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
String nextLine() throws IOException {
return in.readLine();
}
String nextWord() throws IOException {
StringBuilder sb = new StringBuilder();
int c = 0;
while (c<=' ') c=in.read();
while (c>' ') {
sb.append((char)c);
c = in.read();
}
return sb.toString();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextWord());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextWord());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextWord());
}
int[] nextArr(int size) throws NumberFormatException, IOException {
int[] arr = new int[size];
for (int i=0; i<size; i++)
arr[i] = nextInt();
return arr;
}
long[] nextArrL(int size) throws NumberFormatException, IOException {
long[] arr = new long[size];
for (int i=0; i<size; i++)
arr[i] = nextLong();
return arr;
}
double[] nextArrD(int size) throws NumberFormatException, IOException {
double[] arr = new double[size];
for (int i=0; i<size; i++)
arr[i] = nextDouble();
return arr;
}
String[] nextArrS(int size) throws NumberFormatException, IOException {
String[] arr = new String[size];
for (int i=0; i<size; i++)
arr[i] = nextWord();
return arr;
}
void print(Object o) throws IOException {
out.write(o.toString());
}
void println(Object o) throws IOException {
out.write(o.toString());
out.newLine();
}
void print(Object... o) throws IOException {
for (int i=0; i<o.length; i++) {
if (i!=0) out.write(' ');
out.write(o[i].toString());
}
}
void println(Object... o) throws IOException {
print(o);
out.newLine();
}
void printArr(int[] arr) throws IOException {
for (int i=0; i<arr.length; i++) {
if (i!=0) out.write(' ');
out.write(Integer.toString(arr[i]));
}
}
void printArr(long[] arr) throws IOException {
for (int i=0; i<arr.length; i++) {
if (i!=0) out.write(' ');
out.write(Long.toString(arr[i]));
}
}
void printArr(double[] arr) throws IOException {
for (int i=0; i<arr.length; i++) {
if (i!=0) out.write(' ');
out.write(Double.toString(arr[i]));
}
}
void printlnArr(int[] arr) throws IOException {
printArr(arr);
out.newLine();
}
void printlnArr(long[] arr) throws IOException {
printArr(arr);
out.newLine();
}
void printlnArr(double[] arr) throws IOException {
printArr(arr);
out.newLine();
}
void debug(Object... o) {
if (showDebug) System.err.println(Arrays.deepToString(o));
}
public static void main(String[] args) throws Exception {
Locale.setDefault(Locale.US);
new _12_d2_E().solve();
out.flush();
out.close(); in.close();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | c7e2bbbf4ec6b061a2c2bd185efe8e44 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class SeasonStart {
static BufferedReader reader;
static StringTokenizer tokenizer;
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
int n = NextInt();
int a[][] = new int[n][n];
for(int i = 0 ;i < n; ++i)
a[i][i] = 0;
for(int i = 0; i < n; ++i)
a[0][i] = i;
for (int i = 1; i < n - 1; ++i) {
int last = a[i - 1][i] + 1;
if (last == n)
last = 1;
for (int j = i + 1; j < n - 1; ++j) {
a[i][j] = last + j - i;
if(a[i][j] > n - 1)
a[i][j] %= (n - 1);
}
a[i][n - 1] = last;
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < i; ++j)
a[i][j] = a[j][i];
for (int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j)
System.out.print(Integer.toString(a[i][j]) + " ");
System.out.println();
}
}
static int NextInt() throws NumberFormatException, IOException {
return Integer.parseInt(NextToken());
}
static double NextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(NextToken());
}
static long NextLong() throws NumberFormatException, IOException {
return Long.parseLong(NextToken());
}
static String NextToken() throws IOException {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | b78c3a95560bdfb3194772697a6ea665 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProblemE {
public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int N = Integer.valueOf(s.readLine());
int[][] mat = new int[N][N];
for (int i = 0 ; i < N - 1 ; i++) {
if (i == 0) {
for (int j = 1 ; j < N ; j++) {
mat[i][j] = mat[j][i] = j;
}
continue;
}
boolean[] disable = new boolean[N];
for (int j = 0 ; j < i ; j++) {
disable[mat[i][j]] = true;
}
if (i == N-2) {
for (int j = 1 ; j <= N-1 ; j++) {
if (!disable[j]) {
mat[i][N-1] = mat[N-1][i] = j;
break;
}
}
continue;
}
int nonp = -1;
for (int j = i+1 ; j < N - 1 ; j++) {
if (!disable[mat[i-1][j+1]]) {
int p = mat[i-1][j+1];
mat[i][j] = mat[j][i] = p;
disable[p] = true;
} else {
nonp = j;
}
}
mat[i][N-1] = mat[N-1][i] = mat[i-1][i+1];
disable[mat[i][N-1]] = true;
if (nonp != -1) {
for (int d = 1 ; d <= N-1 ; d++) {
if (!disable[d]) {
mat[i][nonp] = mat[nonp][i] = d;
}
}
}
}
for (int i = 0 ; i < N ; i++) {
StringBuffer b = new StringBuffer();
for (int j = 0 ; j < N ; j++) {
b.append(" ").append(mat[i][j]);
}
out.println(b.substring(1));
}
out.flush();
}
public static void debug(Object... os){
System.err.println(Arrays.deepToString(os));
}
} | Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 5efb989161811da8a2a46032f7e4d48d | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashMap;
public class Main {
private static StreamTokenizer in;
private static PrintWriter out;
private static int nextInt() throws Exception {
in.nextToken();
return (int)in.nval;
}
private static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws Exception {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
int n = nextInt(), m = n/2;
int[][] a = new int[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (i==j) continue;
a[i][j]=i+j;
}
}
for (int j=1; j<n-1; j++) {
a[n-1][j] = (a[n-1][j-1] + 2) % (n-1);
}
for (int i=2; i<m; i++) {
a[n-i][i] = 1;
for (int j=i+1; j<n-i; j++) {
a[n-i][j] = (a[n-i][j-1] + 1) % (n-i+1);
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (i>=j)
out.print(j==0 ? a[i][j] : " " + a[i][j]);
else
out.print(j==0 ? a[j][i] : " " + a[j][i]);
}
out.println();
}
out.flush();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | bc8e90917294b25e2b638a98384f8283 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.*;
import java.util.*;
public class StartOfTheSeason
{
public Scanner in;
public PrintWriter out;
StartOfTheSeason()
{
in = new Scanner(System.in);
out = new PrintWriter(System.out);
}
StartOfTheSeason(String i, String o) throws FileNotFoundException
{
in = new Scanner(new File(i));
out = new PrintWriter(new File(o));
}
public void finalize()
{
out.flush();
in.close();
out.close();
}
public void solve()
{
int SZ = 1001;
int n = in.nextInt();
int[][] mat = new int[SZ][SZ];
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
mat[i][j] = 1 + (i + j) % (n - 1);
for(int i = 0; i < n; mat[i][i] = 0, ++i)
mat[i][n - 1] = mat[n - 1][i] = mat[i][i];
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
System.out.print(mat[i][j] + " ");
System.out.printf("\n");
}
}
public static void main(String[] args)
{
StartOfTheSeason t = new StartOfTheSeason();
t.solve();
t.finalize();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 28d0128dfef07fb2f1effff5dcd98a8d | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.*;
import java.util.*;
public class StartOfTheSeason
{
public Scanner in;
public PrintWriter out;
StartOfTheSeason()
{
in = new Scanner(System.in);
out = new PrintWriter(System.out);
}
StartOfTheSeason(String i, String o) throws FileNotFoundException
{
in = new Scanner(new File(i));
out = new PrintWriter(new File(o));
}
public void finalize()
{
out.flush();
in.close();
out.close();
}
public void solve()
{
int SZ = 1001;
int n = in.nextInt();
int[][] mat = new int[SZ][SZ];
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
mat[i][j] = 1 + (i + j) % (n - 1);
for(int i = 0; i < n; mat[i][i] = 0, ++i)
mat[i][n - 1] = mat[n - 1][i] = mat[i][i];
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
System.out.print(mat[i][j] + " ");
System.out.printf("\n");
}
}
public static void main(String[] args)
{
StartOfTheSeason t = new StartOfTheSeason();
t.solve();
t.finalize();
}
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 74fc747c66051ef6de0278cbdb2a01cf | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
private static StreamTokenizer in;
private static PrintWriter out;
private static BufferedReader inB;
private static int nextInt() throws Exception{
in.nextToken();
return (int)in.nval;
}
private static String nextString() throws Exception{
in.nextToken();
return in.sval;
}
static{
inB = new BufferedReader(new InputStreamReader(System.in));
in = new StreamTokenizer(inB);
out = new PrintWriter(System.out);
}
public static void main(String[] args)throws Exception {
int n = nextInt();
int[][] mas = new int[n][n];
for(int i = 0; i<n; i++) {
for(int j = 0; j<n; j++) {
if(i == j)continue;
if(i == 0) {
mas[i][j] = mas[j][i] = j == n-1 ? 1 : (j+1);
} else if(j == 0) {
mas[i][j] = mas[j][i] = i == n-1 ? 1 : (i+1);
} else if(i == n-1) {
mas[i][j] = (mas[i][j-1] + 2) % (n-1);
if(mas[i][j] == 0)mas[i][j] = n-1;
} else if(j == n-1) {
mas[i][j] = (mas[i-1][j] + 2) % (n-1);
if(mas[i][j] == 0)mas[i][j] = n-1;
} else {
int cur = mas[i-1][j] == 0 ? (mas[i][j-1]+1) : (mas[i-1][j]+1);
mas[i][j] = cur%(n-1) == 0 ? (n-1) : cur%(n-1);
}
}
}
for(int i = 0; i<n; i++) {
for(int j = 0; j<n; j++) {
out.print(mas[i][j] + " ");
}
out.println();
}
out.flush();
}
/////////////////////////////////////////////////
private static void println(Object o) throws Exception {
System.out.println(o);
}
private static void exit(Object o) throws Exception {
println(o);
exit();
}
private static void exit() {
System.exit(0);
}
/////////////////////////////////
}
| Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 07b222ce1b68e76d7a7f34d1abaa0855 | train_004.jsonl | 1272538800 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public void solution() throws IOException {
int n = in.nextInt();
int[][] res = new int[n][n];
boolean[][] row = new boolean[n][n];
boolean[][] col = new boolean[n][n];
for (int i = 0; i < n; ++i) {
row[i][0] = true;
col[i][0] = true;
}
for (int i = 0; i < n; ++i) {
int next = i;
int need = 0;
for (int j = 0; j < n; ++j) {
if (i != j) {
for (int it = 0; it < 2 * n; ++it) {
if (row[i][next] || col[j][next]) {
++next;
if (next == n) {
next = 0;
}
} else {
break;
}
}
row[i][next] = true;
col[j][next] = true;
res[i][j] = next;
} else {
}
next = (next + 1) % n;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 0) {
out.print(" ");
}
out.print(res[i][j]);
}
out.println();
}
out.flush();
}
public static void main(String[] args) throws IOException {
new Main().solution();
}
private class Scanner {
BufferedReader reader;
StringTokenizer tokenizer;
public Scanner(BufferedReader reader) {
this.reader = reader;
this.tokenizer = new StringTokenizer("");
}
public boolean hasNext() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String next = reader.readLine();
if (next == null) {
return false;
}
tokenizer = new StringTokenizer(next);
}
return true;
}
public String next() throws IOException {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
} | Java | ["2", "4"] | 2 seconds | ["0 1\n1 0", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"] | null | Java 6 | standard input | [
"constructive algorithms"
] | f5f892203b62dae7eba86f59b13f5a38 | The first line contains one integer n (2 ≤ n ≤ 1000), n is even. | 2,100 | Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. | standard output | |
PASSED | 9ed64e600fa5505521e84246d0e59692 | train_004.jsonl | 1335016800 | The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series.There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot.The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set.Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on.Help the Beaver to implement the algorithm for selecting the desired set. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main implements Runnable {
class Edge implements Comparable<Edge>{
int from, to, cost;
Edge(int from, int to, int cost){
this.from = from; this.to = to; this.cost = cost;
}
public int compareTo(Edge b){
return cost - b.cost;
}
}
ArrayList<Edge> graph;
int N, K, T;
public void solve() throws IOException {
N = nextInt();
K = nextInt();
T = nextInt();
graph = new ArrayList<>();
for(int i = 0; i < K; i++){
int from = nextInt() - 1; //boy
int to = nextInt() - 1; //girl
int cost = nextInt();
graph.add(new Edge(from, to, cost));
}
Collections.sort(graph);
//Binary search
int left = -1, right = 0;
for(int i = 0; i < K; i++) right += graph.get(i).cost;
while((right - left) > 1){
int mid = (left + right) / 2;
if(cnt(mid) >= T)
right = mid;
else
left = mid;
}
System.out.println(right);
}
static int counter;
public int cnt(int limit){
counter = 0;
doit(0, limit, (1<<N)-1, (1<<N)-1);
return counter;
}
public void doit(int x, int limit, int boyMask, int girlMask){
++counter;
if(counter > T) return;
for(int i = x; i < K && graph.get(i).cost <= limit; i++){
if( ((boyMask >> graph.get(i).from) & 1) > 0 && ((girlMask >> graph.get(i).to) & 1) > 0){
doit(i+1, limit - graph.get(i).cost, boyMask - (1 << graph.get(i).from), girlMask - (1 << graph.get(i).to));
}
}
}
//-----------------------------------------------------------
public static void main(String[] args) {
new Main().run();
}
public void print1Int(int[] a){
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
public void print2Int(int[][] a){
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
tok = null;
solve();
in.close();
} catch (IOException e) {
System.exit(0);
}
}
public String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
BufferedReader in;
StringTokenizer tok;
} | Java | ["2 4 3\n1 1 1\n1 2 2\n2 1 3\n2 2 7", "2 4 7\n1 1 1\n1 2 2\n2 1 3\n2 2 7"] | 2 seconds | ["2", "8"] | NoteThe figure shows 7 acceptable sets of marriages that exist in the first sample. | Java 7 | standard input | [] | 7348b5644f232bf377a4834eded42e4b | The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: 1 ≤ n ≤ 5 The input limitations for getting 100 points are: 1 ≤ n ≤ 20 | 2,300 | Print a single number — the value of the t-th acceptable variant. | standard output | |
PASSED | 05bb775ab8cc92b6087fef5160cd56a5 | train_004.jsonl | 1493391900 | The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:There are space-separated non-empty words of lowercase and uppercase Latin letters.There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.You should write a program that will find minimal width of the ad. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main3
{
static class Reader
{
private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;}
public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];}
public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;}
public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();}
public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;}
public int i(){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 double d() throws IOException {return Double.parseDouble(s()) ;}
public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
}
///////////////////////////////////////////////////////////////////////////////////////////
// RRRRRRRRR AAA HHH HHH IIIIIIIIIIIII LLL //
// RR RRR AAAAA HHH HHH IIIIIIIIIII LLL //
// RR RRR AAAAAAA HHH HHH III LLL //
// RR RRR AAA AAA HHHHHHHHHHH III LLL //
// RRRRRR AAA AAA HHHHHHHHHHH III LLL //
// RR RRR AAAAAAAAAAAAA HHH HHH III LLL //
// RR RRR AAA AAA HHH HHH IIIIIIIIIII LLLLLLLLLLLL //
// RR RRR AAA AAA HHH HHH IIIIIIIIIIIII LLLLLLLLLLLL //
///////////////////////////////////////////////////////////////////////////////////////////
static int k;
static int store[]=new int[2000000];
static int counter=0;
public static boolean check(int length)
{
int sum=0;
int lines=1;
for(int i=0;i<counter;i++)
{
if(i!=counter-1)
{
if(store[i]+1>length)
return false;
}
else if(store[i]>length)
return false;
}
for(int i=0;i<counter;i++)
{
if(i==counter-1)
if(sum+store[i]<=length)
{
sum+=store[i];
}
else
{
sum=store[i];
lines++;
}
else
if(sum+store[i]+1<=length)
{
sum+=store[i]+1;
}
else
{
sum=store[i]+1;
lines++;
}
}
if(lines<=k)
return true;
return false;
}
public static void main(String[] args)throws IOException
{
PrintStream out= new PrintStream(System.out);
Scanner sc=new Scanner(System.in);
k=sc.nextInt();
String gg=sc.nextLine();
String s=sc.nextLine();
s=s.replace(' ','-');
StringTokenizer st = new StringTokenizer(s,"-");
while(st.hasMoreTokens())
store[counter++]=st.nextToken().length();
int low=0;
int high=1000000;
while(low<high)
{
int mid=(low+high)/2;
if(check(mid))
high=mid;
else
low=mid+1;
}
out.println(low);
out.flush();
}
} | Java | ["4\ngarage for sa-le", "4\nEdu-ca-tion-al Ro-unds are so fun"] | 1 second | ["7", "10"] | NoteHere all spaces are replaced with dots.In the first example one of possible results after all word wraps looks like this:garage.for.sa-leThe second example:Edu-ca-tion-al.Ro-unds.are.so.fun | Java 8 | standard input | [
"binary search",
"greedy"
] | 3cc766aabb6f5f7b687a62a0205ca94c | The first line contains number k (1 ≤ k ≤ 105). The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters. | 1,900 | Output minimal width of the ad. | standard output | |
PASSED | 599f19c039cc28044fa2e597b159344f | train_004.jsonl | 1493391900 | The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:There are space-separated non-empty words of lowercase and uppercase Latin letters.There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.You should write a program that will find minimal width of the ad. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class MagazineAd {
static boolean p(ArrayList<Integer> a, int maxWidth, int lines) {
int width = 0;
for (int x : a) {
if (x > maxWidth)
return false;
if (width + x <= maxWidth)
width += x;
else {
width = x;
lines--;
}
}
if (width > 0)
lines--;
return lines >= 0;
}
public static void main(String[] args) throws IOException {
MyScanner sc = new MyScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int lines = sc.nextInt();
String s = sc.nextLine();
int n = s.length();
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0, prev = 0; i < n; i++) {
if (s.charAt(i) == ' ' || s.charAt(i) == '-') {
a.add(i - prev + 1);
prev = i + 1;
}
if (i == n - 1)
a.add(s.length() - prev);
}
System.err.println(a);
int low = 0, high = (int) 1e6 + 1, ans = 0;
while (low <= high) {
int mid = (low + high) >> 1;
if (p(a, mid, lines)) {
high = mid - 1;
ans = mid;
}
else
low = mid + 1;
}
out.println(ans);
out.flush();
out.close();
}
static class MyScanner {
StringTokenizer st;
BufferedReader br;
public MyScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public MyScanner(String file) throws IOException {
br = new BufferedReader(new FileReader(new File(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 boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["4\ngarage for sa-le", "4\nEdu-ca-tion-al Ro-unds are so fun"] | 1 second | ["7", "10"] | NoteHere all spaces are replaced with dots.In the first example one of possible results after all word wraps looks like this:garage.for.sa-leThe second example:Edu-ca-tion-al.Ro-unds.are.so.fun | Java 8 | standard input | [
"binary search",
"greedy"
] | 3cc766aabb6f5f7b687a62a0205ca94c | The first line contains number k (1 ≤ k ≤ 105). The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters. | 1,900 | Output minimal width of the ad. | standard output | |
PASSED | 27c094596a683934856515405449183f | train_004.jsonl | 1493391900 | The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:There are space-separated non-empty words of lowercase and uppercase Latin letters.There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.You should write a program that will find minimal width of the ad. | 256 megabytes | import java.io.*;
import java.util.*;
public class p8{
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 boolean check(int[] arr, int mid, int n, int k){
int pi = 0, i=0, cur = mid, count = 0;
while(i<n){
if(cur==0){
count++;
if(pi==0) return false;
cur = mid - (i-pi) + 1;
pi = 0;
}
if(arr[i]==1){
pi = i;
}
cur--;
i++;
}
return ( (cur==0?count:count+1) <= k);
}
static int search(int[] arr, int l, int r, int k){
int ans = r;
while(l<=r){
int mid = l + (r-l)/2;
if(check(arr, mid, arr.length, k)){
ans = mid;
r = mid-1;
}else{
l = mid+1;
}
}
return ans;
}
public static void main(String[] args)
{
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int k = sc.nextInt();
String s = sc.nextLine();
int n = s.length();
int[] arr = new int[n];
for(int i=0;i<n;i++)
arr[i] = (s.charAt(i)==' ' || s.charAt(i)=='-')?1:0;
out.println(search(arr, 1, n, k));
out.close();
}
}
| Java | ["4\ngarage for sa-le", "4\nEdu-ca-tion-al Ro-unds are so fun"] | 1 second | ["7", "10"] | NoteHere all spaces are replaced with dots.In the first example one of possible results after all word wraps looks like this:garage.for.sa-leThe second example:Edu-ca-tion-al.Ro-unds.are.so.fun | Java 8 | standard input | [
"binary search",
"greedy"
] | 3cc766aabb6f5f7b687a62a0205ca94c | The first line contains number k (1 ≤ k ≤ 105). The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters. | 1,900 | Output minimal width of the ad. | standard output | |
PASSED | 6f0c578db6a4ed0ff4994839a24dd567 | train_004.jsonl | 1493391900 | The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:There are space-separated non-empty words of lowercase and uppercase Latin letters.There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.You should write a program that will find minimal width of the ad. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Abood2D {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int k = sc.nextInt();
StringTokenizer st = new StringTokenizer(sc.nextLine());
ArrayList<String> S = new ArrayList<>();
while(st.hasMoreTokens()){
S.add(st.nextToken());
}
int l = 0;
int h = (int) 1e6;
int ans = -1;
while(l <= h) {
int mid = (l + h) / 2;
int w = 0;
int c = 0;
int line = 1;
int cur = 0;
while(line <= k && w < S.size()) {
if(cur + S.get(w).length() + 1 - c <= mid || w == S.size() - 1 && cur + S.get(w).length() - c <= mid) {
if(w < S.size() - 1)
cur += S.get(w).length() + 1 - c;
else
cur += S.get(w).length() - c;
w++;
c = 0;
continue;
}
boolean f = false;
for (int i = S.get(w).length() - 1; i >= c; i--)
if(S.get(w).charAt(i) == '-' && i - c + 1 + cur <= mid){
cur += i - c + 1;
c = i + 1;
f = true;
break;
}
if(f)
continue;
line++;
cur = 0;
}
if(line <= k) {
ans = mid;
h = mid - 1;
}else {
l = mid + 1;
}
}
out.println(ans);
out.flush();
out.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream System){br = new BufferedReader(new InputStreamReader(System));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine()throws IOException{return br.readLine();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public char nextChar()throws IOException{return next().charAt(0);}
public Long nextLong()throws IOException{return Long.parseLong(next());}
public boolean ready() throws IOException{return br.ready();}
public void waitForInput(){for(long i = 0; i < 3e9; i++);}
}
} | Java | ["4\ngarage for sa-le", "4\nEdu-ca-tion-al Ro-unds are so fun"] | 1 second | ["7", "10"] | NoteHere all spaces are replaced with dots.In the first example one of possible results after all word wraps looks like this:garage.for.sa-leThe second example:Edu-ca-tion-al.Ro-unds.are.so.fun | Java 8 | standard input | [
"binary search",
"greedy"
] | 3cc766aabb6f5f7b687a62a0205ca94c | The first line contains number k (1 ≤ k ≤ 105). The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters. | 1,900 | Output minimal width of the ad. | standard output | |
PASSED | 92e0bcd57e1829328663c6267260e1ef | train_004.jsonl | 1493391900 | The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:There are space-separated non-empty words of lowercase and uppercase Latin letters.There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.You should write a program that will find minimal width of the ad. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
import java.text.*;
public class A{
//public static PrintWriter pw;
public static PrintWriter pw=new PrintWriter(System.out);
public static void solve() throws IOException{
// pw=new PrintWriter(new FileWriter("C:\\Users\\shree\\Downloads\\small_output_in"));
FastReader sc=new FastReader();
int k=sc.I();
char s[]=sc.nextLine().toCharArray();
ArrayList<Integer> v=new ArrayList<>();
int c=0;
for(int i=0;i<s.length;i++) { c++;
if(s[i]==' ' || s[i]=='-') {
v.add(c);
c=0;
} else if(s[i]=='\n') c--;
}
v.add(c);
int l=0,h=(int)1e7;
int ans=Integer.MAX_VALUE;
while(l<h) {
int w=(l+h)/2;
boolean is=false;
int cnt=1,curr=0;
for(int u : v) {
if(curr+u<=w) {
curr+=u;
}else {
curr=u;
cnt++;
}
if(u>w) {
is=true;
break;
}
}
if(cnt<=k && !is) { h=w;
ans=Math.min(ans, w);
}
else l=w+1;
}
pw.println(ans);
pw.close();
}
public static void main(String[] args) {
new Thread(null ,new Runnable(){
public void run(){
try{
solve();
} catch(Exception e){
e.printStackTrace();
}
}
},"1",1<<26).start();
}
static long M=(long)Math.pow(10,9)+7;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() throws FileNotFoundException{
//br=new BufferedReader(new FileReader("C:\\Users\\shree\\Downloads\\B-small-practice.in"));
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 I(){ return Integer.parseInt(next()); }
long L(){ return Long.parseLong(next()); }
double D() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\ngarage for sa-le", "4\nEdu-ca-tion-al Ro-unds are so fun"] | 1 second | ["7", "10"] | NoteHere all spaces are replaced with dots.In the first example one of possible results after all word wraps looks like this:garage.for.sa-leThe second example:Edu-ca-tion-al.Ro-unds.are.so.fun | Java 8 | standard input | [
"binary search",
"greedy"
] | 3cc766aabb6f5f7b687a62a0205ca94c | The first line contains number k (1 ≤ k ≤ 105). The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters. | 1,900 | Output minimal width of the ad. | standard output | |
PASSED | 9a9a77a302c15385cc05fe34b2bace60 | train_004.jsonl | 1493391900 | The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:There are space-separated non-empty words of lowercase and uppercase Latin letters.There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.You should write a program that will find minimal width of the ad. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw =
new BufferedWriter(new OutputStreamWriter(System.out));
static int K, L, i, j;
static int[] SL = new int[1000001];
public static void main(String args[]) throws IOException{
Scanner sc = new Scanner(System.in);
K = Integer.parseInt(br.readLine());
char[] str = br.readLine().toCharArray();
L = str.length;
for(i = 0, j = 0; i < L; i++){
if(str[i] == '-' || str[i] == ' ') SL[j++]++;
else SL[j]++;
}
bw.write(sol(1, 1000000)+"");
bw.close();
}
static int sol(int s, int e){
if(s == e) return s;
int m = (s+e)/2, line = 1, jj = 0;
for(int ii = 0; ii <= j; ii++){
if(SL[ii] > m) return sol(m+1, e);
if(jj+SL[ii] <= m) jj += SL[ii];
else{
line++;
jj = SL[ii];
}
}
if(line <= K) return sol(s, m);
else return sol(m+1, e);
}
} | Java | ["4\ngarage for sa-le", "4\nEdu-ca-tion-al Ro-unds are so fun"] | 1 second | ["7", "10"] | NoteHere all spaces are replaced with dots.In the first example one of possible results after all word wraps looks like this:garage.for.sa-leThe second example:Edu-ca-tion-al.Ro-unds.are.so.fun | Java 8 | standard input | [
"binary search",
"greedy"
] | 3cc766aabb6f5f7b687a62a0205ca94c | The first line contains number k (1 ≤ k ≤ 105). The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters. | 1,900 | Output minimal width of the ad. | standard output | |
PASSED | 043ee47070a07168023353a5e610365a | train_004.jsonl | 1599230100 | You are given two integers $$$a$$$ and $$$b$$$.In one move, you can choose some integer $$$k$$$ from $$$1$$$ to $$$10$$$ and add it to $$$a$$$ or subtract it from $$$a$$$. In other words, you choose an integer $$$k \in [1; 10]$$$ and perform $$$a := a + k$$$ or $$$a := a - k$$$. You may use different values of $$$k$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class CF1409A{
public static void main(String a[])
{
Scanner input = new Scanner(System.in);
int test_cases = input.nextInt();
while(test_cases --> 0)
{
int num1 = input.nextInt();
int num2 = input.nextInt();
System.out.println((Math.abs(num1-num2)+9)/10);
}
input.close();
}
} | Java | ["6\n5 5\n13 42\n18 4\n1337 420\n123456789 1000000000\n100500 9000"] | 1 second | ["0\n3\n2\n92\n87654322\n9150"] | NoteIn the first test case of the example, you don't need to do anything.In the second test case of the example, the following sequence of moves can be applied: $$$13 \rightarrow 23 \rightarrow 32 \rightarrow 42$$$ (add $$$10$$$, add $$$9$$$, add $$$10$$$).In the third test case of the example, the following sequence of moves can be applied: $$$18 \rightarrow 10 \rightarrow 4$$$ (subtract $$$8$$$, subtract $$$6$$$). | Java 11 | standard input | [
"greedy",
"math"
] | d67a97a3b69d599b03d3fce988980646 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$). | 800 | For each test case, print the answer: the minimum number of moves required to obtain $$$b$$$ from $$$a$$$. | standard output | |
PASSED | 6d60fe15d374a88b9d691bf8b387803f | train_004.jsonl | 1599230100 | You are given two integers $$$a$$$ and $$$b$$$.In one move, you can choose some integer $$$k$$$ from $$$1$$$ to $$$10$$$ and add it to $$$a$$$ or subtract it from $$$a$$$. In other words, you choose an integer $$$k \in [1; 10]$$$ and perform $$$a := a + k$$$ or $$$a := a - k$$$. You may use different values of $$$k$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class TwoInts{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int num = input.nextInt();
for(int i = 0; i < num; i++){
int a = input.nextInt();
int b = input.nextInt();
int diff = Math.abs(a-b);
int count = diff/10;
if(diff%10 != 0){
count++;
}
System.out.println(count);
}
}
} | Java | ["6\n5 5\n13 42\n18 4\n1337 420\n123456789 1000000000\n100500 9000"] | 1 second | ["0\n3\n2\n92\n87654322\n9150"] | NoteIn the first test case of the example, you don't need to do anything.In the second test case of the example, the following sequence of moves can be applied: $$$13 \rightarrow 23 \rightarrow 32 \rightarrow 42$$$ (add $$$10$$$, add $$$9$$$, add $$$10$$$).In the third test case of the example, the following sequence of moves can be applied: $$$18 \rightarrow 10 \rightarrow 4$$$ (subtract $$$8$$$, subtract $$$6$$$). | Java 11 | standard input | [
"greedy",
"math"
] | d67a97a3b69d599b03d3fce988980646 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$). | 800 | For each test case, print the answer: the minimum number of moves required to obtain $$$b$$$ from $$$a$$$. | standard output | |
PASSED | fb8a7444383a439a37ce063fb08b385a | train_004.jsonl | 1599230100 | You are given two integers $$$a$$$ and $$$b$$$.In one move, you can choose some integer $$$k$$$ from $$$1$$$ to $$$10$$$ and add it to $$$a$$$ or subtract it from $$$a$$$. In other words, you choose an integer $$$k \in [1; 10]$$$ and perform $$$a := a + k$$$ or $$$a := a - k$$$. You may use different values of $$$k$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Abcc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
int[] q=new int[t];
for (int i = 0; i < t ; i++) {
int a=sc.nextInt();
int b=sc.nextInt();
sc.nextLine();
int count=0;
if (a==b){
count=0;
}
else if (b-a>0){
if (b - a > 10) {
int k = b - a;
if (k%10!=0) {
count = k / 10 + 1;
} else{
count=k/10;
}
} else{
count=1;
}
} else if (b-a<0 ){
if (a-b>10){
int k=a-b;
if (k%10!=0) {
count = k / 10 + 1;
} else{
count=k/10;
}
} else{
count=1;
}
}
q[i]=count;
}
for (int i = 0; i < t ; i++) {
System.out.println(q[i]);
}
}
}
| Java | ["6\n5 5\n13 42\n18 4\n1337 420\n123456789 1000000000\n100500 9000"] | 1 second | ["0\n3\n2\n92\n87654322\n9150"] | NoteIn the first test case of the example, you don't need to do anything.In the second test case of the example, the following sequence of moves can be applied: $$$13 \rightarrow 23 \rightarrow 32 \rightarrow 42$$$ (add $$$10$$$, add $$$9$$$, add $$$10$$$).In the third test case of the example, the following sequence of moves can be applied: $$$18 \rightarrow 10 \rightarrow 4$$$ (subtract $$$8$$$, subtract $$$6$$$). | Java 11 | standard input | [
"greedy",
"math"
] | d67a97a3b69d599b03d3fce988980646 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$). | 800 | For each test case, print the answer: the minimum number of moves required to obtain $$$b$$$ from $$$a$$$. | standard output | |
PASSED | cebb3a03a3e43cba08f94e591c6e465a | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.*;
public class cp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
int cntodd = 0, cnteven = 0, count = 0;
for(int i=0;i<n;i++)
{
if(arr[i] % 2 == 0)
{
cnteven++;
}
else if(arr[i] % 2 != 0)
{
cntodd++;
}
}
if(cnteven > 0)
{
System.out.println("1");
for(int i = 0; i < n; i++)
{
if(arr[i] % 2 == 0)
{
System.out.println(i+1);
break;
}
}
}
else if(cntodd > 1)
{
System.out.println("2");
for(int i=0;i<n;i++)
{
if(arr[i]%2 != 0)
{
System.out.print(i+1 + " ");
count++;
}
if(count == 2)
{
System.out.println();
break;
}
}
}
else
{
System.out.println("-1");
}
t--;
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | ee21fd02fff445913e92608e95599aa4 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int k = scanner.nextInt();
int toPrint = -1;
for (int j = 0; j < k; j++) {
int a = scanner.nextInt();
if (toPrint != -1)
continue;
if (a % 2 == 0)
toPrint = j;
}
if (toPrint == -1 && k >= 2)
{
System.out.println(2);
System.out.println(1 + " " + 2);
}
else if (toPrint == -1)
{
System.out.println(toPrint);
}
else
{
System.out.println(1);
System.out.println(toPrint + 1);
}
}
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 0f42b63d3fffc8e7920d8da8939a7ea0 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.*;
import java.util.Scanner;
public class Main{
public static void main(String Args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
int count=0,n,flag=0;
n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
if(arr[i]%2==0) {
count=i+1;
}
}
if(count>0) {
System.out.println("1");
System.out.println(count);
}else {
if(n==1) {
System.out.println("-1");
}else {
System.out.println("2");
System.out.println("1 2");
}
}
t--;
}
}}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 1dc05123a48f8f7c945bff26e0f07c32 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int first = scanner.nextInt();
if(first % 2 == 0) {
System.out.println(1);
System.out.println(1);
}
else if (n == 1) {
System.out.println(-1);
}
else {
int second = scanner.nextInt();
if(second % 2 == 0) {
System.out.println(1);
System.out.println(2);
}
else {
System.out.println(2);
System.out.println(1 + " " + 2);
}
}
scanner.nextLine();
}
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 5f752f6f6bbdddaf7268b9de1d5050ec | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.Scanner;
public class Even_Subset_Sum_Problem {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner t = new Scanner(System.in);
int test = t.nextInt();
while (test-- > 0) {
int n = t.nextInt();
int[] a = new int[n];
int even = 0, idx = 0;
for (int i = 0; i < n; i++) {
a[i] = t.nextInt();
if (a[i] % 2 == 0) {
even++;
idx = i + 1;
}
}
if (even > 0) {
System.out.println(1);
System.out.println(idx);
} else if (n > 1) {
System.out.println(2);
System.out.println(1 + " " + 2);
} else {
System.out.println(-1);
}
}
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | da7b53e43b320b99d37be6f6c4c9773a | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes |
import java.io.PrintWriter;
import java.util.Scanner;
public class EvenSubsetSumProblemA extends PrintWriter {
//this trick improves performances
EvenSubsetSumProblemA() {
super(System.out);
}
public static void main(String[] $) {
EvenSubsetSumProblemA o = new EvenSubsetSumProblemA();
o.main();
o.flush();
}
void main() {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
//use this if just a single test
//int count = 1;
main:
while (count-- > 0) {
int n = sc.nextInt();
int firstPos=0;
int[] array = new int[n+1];
for (int i = 1; i <= n; i++) {
array[i]=sc.nextInt();
}
for (int i = 1; i <= n; i++) {
if(array[i]%2==0){
println(1);
println(i);
continue main;
}
if(firstPos==0){
firstPos=i;
}else{
println(2);
println(firstPos+" "+i);
continue main;
}
}
println(-1);
}
sc.close();
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | a465e1f1a8ef62d2406cd714670a3567 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes |
import java.io.PrintWriter;
import java.util.Scanner;
public class EvenSubsetSumProblemA extends PrintWriter {
//this trick improves performances
EvenSubsetSumProblemA() {
super(System.out);
}
public static void main(String[] $) {
EvenSubsetSumProblemA o = new EvenSubsetSumProblemA();
o.main();
o.flush();
}
void main() {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
//use this if just a single test
//int count = 1;
main:
while (count-- > 0) {
int n = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i]=sc.nextInt();
}
for (int i = 0; i < n; i++) {
int sum=0;
for (int j = i; j <n ; j++) {
sum +=array[j];
if(sum%2==0){
println(j-i+1);
for (int k = i; k <=j ; k++) {
print((k+1)+" ");
}
println();
continue main;
}
}
}
println(-1);
}
sc.close();
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | cf7f481fab923d1deef51efa90212a14 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | // import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
// import java.lang.Math;
// import java.util.Arrays;
// import java.util.HashSet;
// import java.util.HashMap;
// import java.util.Collections;
// import java.math.BigInteger;
public class Try {
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){
int n = Integer.parseInt(br.readLine());
String[] sr = br.readLine().split(" ");
if(n==1 && Integer.parseInt(sr[0])%2!=0){
System.out.println(-1);
continue;
}
if(Integer.parseInt(sr[0])%2==0){
System.out.println(1);
System.out.println(1);
continue;
}
if(Integer.parseInt(sr[1])%2==0){
System.out.println(1);
System.out.println(2);
continue;
}
System.out.println(2);
System.out.println(1 + " " + 2);
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | b035340a3f501ee2f80888f5be731909 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | // import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
// import java.lang.Math;
// import java.util.Arrays;
// import java.util.HashSet;
// import java.util.HashMap;
// import java.util.Collections;
// import java.math.BigInteger;
public class Try {
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){
int n = Integer.parseInt(br.readLine());
String[] sr = br.readLine().split(" ");
if(n==1 && Integer.parseInt(sr[0])%2!=0){
System.out.println(-1);
continue;
}
else if(Integer.parseInt(sr[0])%2==0){
System.out.println(1);
System.out.println(1);
continue;
}
else if(Integer.parseInt(sr[1])%2==0){
System.out.println(1);
System.out.println(2);
continue;
}
System.out.println(2);
System.out.println(1 + " " + 2);
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | ba5e4a9b3efa2afd0012eb32b1375ad7 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t!=0)
{
int n=s.nextInt();
int o=0,e=0;
int od[]=new int[2];
int ei=0,j=0;
for(int i=0;i<n;i++)
{
int x=s.nextInt();
if(x%2==0)
{
e++;
ei=i+1;
}
else
{
o++;
if(j<=1)
{
od[j]=i+1;
j++;
}
}
}
if(e==0)
{
if(o>=2)
{
System.out.println(2);
System.out.println(od[0]+" "+od[1]);
}
else
{
System.out.println(-1);
}
}
else{
System.out.println(1);
System.out.println(ei);
}
t--;
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 483f4d345af4dc0a66f38c66ee898dbb | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.Scanner;
public class Evensub2 {
public static void main(String []args) {
Scanner sc=new Scanner (System.in);
int a=sc.nextInt();
int sum=0,k=0;
for(int l=0;l<a;l++)
{
int s=sc.nextInt();
int ar[]=new int[s];
for(int i=0;i<s;i++)
{
ar[i]=sc.nextInt();
}
for(int i=0;i<ar.length;i++) {
if(ar[i]%2==0) {
System.out.println(1);
System.out.println(i+1);
k=100;break;
}
else {
if(i!=ar.length-1) {
if((ar[i]+ar[i+1])%2==0) {k=100;
System.out.println(2);
System.out.println((i+1)+" "+(i+2));
break;}}
}
}
if(k==0)System.out.println(-1);
k=0;
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 78dfa62490c15eebf4c72a4be0f3ed49 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
while(cases-- > 0) {
int noOfcases = sc.nextInt(), sum = 0, count = 0, check = 0;
int arr[] = new int[noOfcases];
int ind[] = new int[noOfcases];
for(int i = 0, l =0; i < noOfcases; i++) {
arr[i] = sc.nextInt();
}
for(int i = 0, l =0; i < noOfcases; i++) {
if(arr[i] % 2 == 0) {
System.out.println(1);
System.out.println(i+1);
check = 1;
break;
}
else{
sum += arr[i];
ind[l++] = i+1;
count +=1;
if(sum % 2 == 0) {
System.out.println(count);
for(int j = 0; j < l; j++)
System.out.print(ind[j] + " ");
check = 1;
break;
}
}
}
if(check != 1)
System.out.println(-1);
}
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 343d66a8ca7ea26dac7e5c222604175d | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
int t=sc.nextInt();
while (t-->=1){
int n=sc.nextInt();
int a[]=sc.readArray(n);
ArrayList<Integer> oddNumber= new ArrayList<>();
int holder=0;
for (int i=0;i<n;i++){
if (a[i]%2==0){
holder=i+1;
}
else{
oddNumber.add(i+1);
}
}
if (holder!=0){
System.out.println(1+"\n"+holder);
}
else if (oddNumber.size()>1){
System.out.println(2+"\n"+oddNumber.get(0)+" "+oddNumber.get(1));
}
else System.out.println(-1);
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 9398d5eb17458fc128a370e5bf7e84d1 | train_004.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.Scanner;
import java.util.Arrays;
import java.util.*;
public class subset
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int t,n,m,i,c=1,sum=0,j;
t=s.nextInt();
for(i=0;i<t;i++)
{ sum=0;c=1;
n=s.nextInt();
ArrayList<Integer> a=new ArrayList<Integer>(n);
for(j=0;j<n;j++)
{
a.add(s.nextInt());
sum+=a.get(j);
}
if(sum%2==0)
{
System.out.println(n);
for(j=0;j<n;j++)
{
System.out.print((j+1)+" ");
}
System.out.println();
}
else
{
if(n==1)
System.out.println("-1");
else
{
System.out.println(n-1);
for(j=0;j<n;j++)
{
if(c==1)
{
if(a.get(j)%2!=0){
c=0;
continue;}
else
System.out.print((j+1)+" ");
}
else
System.out.print((j+1)+" ");
}
System.out.println();
}
}
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.