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 | ebdcc4153319521ae1ccae05f1004f23 | train_002.jsonl | 1471875000 | Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. | 256 megabytes | import java.util.Scanner;
public class MagicOddSquare
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[][]=new int[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]=0;
int i=0,j=n/2;
a[i][j]=1;
for(int x=2;x<=n*n;x++)
{
i=i-1;j=j+1;
if(i<0&&j>n-1)
{
i=i+2;j=j-1;
}
else if(i<0)
i=n-1;
else if(j>n-1)
j=0;
else{;}
if(a[i][j]==0)
a[i][j]=x;
else
{
i=i+2;j=j-1;
a[i][j]=x;
}
}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
System.out.print(a[k][l]+" ");
System.out.println();
}
}
} | Java | ["1", "3"] | 1 second | ["1", "2 1 4\n3 5 7\n6 9 8"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | a7da19d857ca09f052718cb69f2cea57 | The only line contains odd integer n (1 ≤ n ≤ 49). | 1,500 | Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd. | standard output | |
PASSED | 97efcac6621e2a5f0bc5318af2ecfebc | train_002.jsonl | 1471875000 | Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. | 256 megabytes | import java.io.*;
import java.util.*;
public class magicOddSquare
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner (System.in);
PrintWriter out = new PrintWriter(System.out);
{
int n = sc.nextInt();
int mid = (n+1)/2;
if (n == 1)
out.println(1);
else
{
Stack<Integer> even = new Stack<Integer>();
Stack<Integer> odd = new Stack<Integer>();
for (int numbers = 1; numbers <= n*n; numbers++)
{
if (numbers%2 == 0) even.push(numbers);
else odd.push(numbers);
}
int [][] grid = new int [n+1][n+1];
// fill odds
int toFill = 1;
// filling the upper half first
for (int row = 1; row<= mid-1; row++)
{
int fill = toFill;
int fillLeft = mid-1;
int fillRight = mid+1;
grid[row][mid] = odd.pop();
fill--;
while (fill != 0)
{
grid[row][fillLeft] = odd.pop();
grid[row][fillRight] = odd.pop();
fillLeft--;
fillRight++;
fill-=2;
}
toFill += 2;
}
// fill lower half
toFill = 1;
for (int row = n; row >= mid+1; row--)
{
int fill = toFill;
int fillLeft = mid-1;
int fillRight = mid+1;
grid[row][mid] = odd.pop();
fill--;
while (fill != 0)
{
grid[row][fillLeft] = odd.pop();
grid[row][fillRight] = odd.pop();
fillLeft--;
fillRight++;
fill-=2;
}
toFill += 2;
}
// fill the middle row
for (int i = 1; i<= n; i++)
grid[mid][i] = odd.pop();
/* for (int i = 1; i<= n; i++)
{
for (int j = 1; j<=n; j++)
System.out.print(grid[i][j]+" ");
System.out.println();
}*/
// fill the even s
for (int i = 1; i<=n; i++)
{
for (int j = 1; j<=n; j++)
{
if (grid[i][j] == 0)
grid[i][j] = even.pop();
}
}
int rowSum = 0;
int colSum = 0;
int mainDiag = 0;
int otherDiag = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{rowSum += grid[i][j]; colSum += grid[j][i]; mainDiag += grid[j][j]; otherDiag += grid[j][n+1-j];}
if (rowSum % 2 == 0 )
{out.println("Warning Row Sum "+i); break;}
if ( colSum % 2 == 0 )
{out.println("Warning Col Sum"); break;}
if ( mainDiag % 2 == 0 )
{out.println("Warning mainDiag Sum"); break;}
if ( otherDiag % 2 == 0)
{out.println("Warning otherDiag Sum"); break;}
rowSum = 0; colSum = 0; mainDiag = 0; otherDiag = 0;
}
for (int i = 1; i<= n; i++)
{
for (int j = 1; j<=n; j++)
out.print(grid[i][j]+" ");
out.println();
}
}
}
out.close();
}
}
| Java | ["1", "3"] | 1 second | ["1", "2 1 4\n3 5 7\n6 9 8"] | null | Java 8 | standard input | [
"constructive algorithms",
"math"
] | a7da19d857ca09f052718cb69f2cea57 | The only line contains odd integer n (1 ≤ n ≤ 49). | 1,500 | Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd. | standard output | |
PASSED | 508bee8ab964be4ada323ba38e8444d0 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.Scanner;
public class A {
private static String replace(String s, int i, char c) {
String suff = (i >= s.length() - 1)?"":s.substring(i);
return s.substring(0, i) + c + s.substring(i + 1);
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] a = new int[n];
scn.nextLine();
String str = scn.nextLine();
for (int i = 0; i < str.length(); i++) {
a[i] = str.charAt(i) - '0';
}
int[] f = new int[9];
for (int i = 0; i < 9; i++) {
f[i] = scn.nextInt();
}
scn.close();
boolean flag = false;
for (int i = 0; i < n; i++) {
if (a[i] < f[a[i] - 1] || (a[i] == f[a[i] - 1] && flag)) {
flag = true;
a[i] = f[a[i] - 1];
} else if (flag) {
break;
}
}
for (int i = 0; i < n; i++) System.out.print(a[i]);
System.out.println();
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 148de8dca52093c5ea600fc1189e8f8c | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;
public class A{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int mapping[]=new int[9];
String actual=sc.next();
StringBuffer answer=new StringBuffer("");
for(int i=0;i<9;i++)
{
mapping[i]=sc.nextInt();
}
boolean change=false;
for(int i=0;i<n;i++)
{
int temp=Integer.parseInt(actual.charAt(i)+"");
// if(mapping[temp-1]==temp)
// continue;
// if(mapping[temp-1]<temp)
// if(!change)
// continue;
// else
// break;
// else
// {
// actual=actual.substring(0,i)+mapping[temp-1]+actual.substring(i+1);
// change=true;
// }
if(mapping[temp-1]==temp)
answer=answer.append(mapping[temp-1]);
else if(mapping[temp-1]<temp)
{
if(!change)
{
answer=answer.append(actual.charAt(i));
}
else
{
answer=answer.append(actual.substring(i));
break;
}
}
else
{
answer=answer.append(mapping[temp-1]);
change=true;
}
}
System.out.println(answer);
}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 67ff0a2557f2ca6556779f1eac3be9f5 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes |
import java.util.*;
import java.math.*;
public class Animal {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int f[]=new int [10];
int n=scanner.nextInt();
char []s=scanner.next().toCharArray();
String ans=new String();
for(int i=1;i<=9;i++)
f[i]=scanner.nextInt();
int i=0;
while(i<n&&f[s[i]-'0']<=s[i]-'0')//
i++;
while(i<n&&f[s[i]-'0']>=s[i]-'0')
{
s[i]=(char)(f[s[i]-'0']+'0');i++;
}
//System.out.println(s);
System.out.println(new String(s));
}
public static class Fun{
public HashMap<Integer, Integer>h1=new HashMap<Integer, Integer>();
public void dfs(int x[]){
x[0]=x[0]+5;
}
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | bb5fbd57f7d875b55adad85029dfb375 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public void sort(long b[]){
Random rd = new Random();
for(int i=1;i<b.length;++i){
int c = rd.nextInt(i);
long v = b[c];
b[c] = b[i];
b[i] = v;
}
Arrays.sort(b);
}
public void sort(int b[]){
Random rd = new Random();
for(int i = 1;i<b.length;++i){
int c = rd.nextInt(i);
int v = b[c];
b[c]= b[i];
b[i] = v;
}
Arrays.sort(b);
}
static int groups = 0;
static int[] fa;
static int[] sz;
static void init(int n) {
groups = n;
fa = new int[n];
for (int i = 1; i < n; ++i) {
fa[i] = i;
}
sz = new int[n];
Arrays.fill(sz, 1);
}
static int root(int p) {
while (p != fa[p]) {
fa[p] = fa[fa[p]];
p = fa[p];
}
return p;
}
static void combine(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) {
return;
}
if (sz[i] < sz[j]) {
fa[i] = j;
sz[j] += sz[i];
} else {
fa[j] = i;
sz[i] += sz[j];
}
groups--;
}
class Pair {
long x;long y;
Pair(long xx,long yy){
x = xx;y= yy;
long g = gcd(Math.abs(x),Math.abs(y));
if(xx*yy>=0){
x = Math.abs(x)/g;
y = Math.abs(y)/g;
}else{
x = -Math.abs(x)/g;
y = Math.abs(y)/g;
}
}
public boolean equals(Object fk){
Pair p = (Pair)fk;
return x == p.x && y== p.y;
}
public int hashCode(){
return (int)(x*37+y);
}
}
void dfs(int g, List<Integer> go[],boolean vis[]){
vis[g] = true;
for(int u: go[g]){
int to = u;
if(!vis[to]) {
dfs(to, go, vis);
}
}
}
public static void main(String[] args) throws Exception {
new Main().run();}
TreeMap<Integer,Integer> tmp = new TreeMap<>();
void remove(int u){
int v = tmp.get(u);
if(v==1){
tmp.remove(u);
}else{
tmp.put(u,v-1);
}
}
void solve(){
int n =ni();
String s = ns();
int f[] = na(9);
boolean gd[] = new boolean[10];
for(int i=0;i<9;++i){
if(i+1<f[i]){
gd[i+1] = true;
}
}
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.length();++i){
int d = s.charAt(i)-'0';
if(gd[d]){
sb.append(s.substring(0,i));
for(;i<s.length();++i){
int dd =s.charAt(i)-'0';
if(gd[dd]||f[dd-1]==dd){
sb.append(f[dd-1]);
}else{
sb.append(s.substring(i));
println(sb);return;
}
}
println(sb);
return;
}
}
println(s);
// PriorityQueue<int[]> q= new PriorityQueue<>((x,y)->{return x[0]-y[0];});
//
// q.offer(new int[]{0,0});
// boolean inq[] = new boolean[n];
// while(q.size()>0){
// int c[]= q.poll();
// for(int i=h[c[1]];i!=-1;i=ne[i]){
// if(!inq[to[i]]) {
// q.offer(new int[]{ wt[i], to[i]});
// inq[to[i]] = true;
// }
// }
// }
}
int err = 0;
void go(int rt,int pt,int fa) {
stk[rt] = pt;
for(int i=h[rt];i!=-1;i=ne[i]) {
if(to[i]==pt||to[i]==fa) continue;
go(to[i],pt,rt);
}
}
int ct = 0;
void add(int u,int v,int w,int q){
to[ct] = v;
wt[ct] = w;
qt[ct] = q;
ne[ct] = h[u];
h[u] = ct++;
//
// to1[ct] = u;
// wt1[ct] = w;
// ne1[ct] = h1[v];
// h1[v] = ct++;
}
int []h,ne,to,wt,qt,stk;
long inv(long a, long MOD) {
//return fpow(a, MOD-2, MOD);
return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD;
}
void inverse(){
int MAXN = 10000;
long inv[] = new long[MAXN+1];
inv[1] = 1; //注意初始化
long mod = 1000000007;
for (int i=2; i<=MAXN; ++i) {
inv[i] = (mod - mod / i) * inv[(int) (mod % i)] % mod;
}
}
// 计算某个特别大的组合数
long C(long n,long m, long MOD) {
if(m+m>n)m=n-m;
long up=1,down=1;
for(long i=0;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+1)%MOD;
}
return up*inv(down, MOD)%MOD;
}
void solve2() {
int n = ni();
String s= ns();
int g[][] = new int[3][3];
for(int i = 0;i<n;++i){
char c = s.charAt(i);
int idx = 0;
if(c=='G'){
idx =1;
}else if(c=='B'){
idx =2;
}
g[c][i%3]++;
}
}
// int []h,to,ne,wt;
long t1[];
// long t2[];
void update(long[] t,int i,long v){
for(;i<t.length;i+=(i&-i)){
t[i] += v;
}
}
long get(long[] t,int i){
long s = 0;
for(;i>0;i-=(i&-i)){
s += t[i];
}
return s;
}
int equal_bigger(long t[],long v){
int s=0,p=0;
for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) {
if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){
v -= t[p+(1<<i)];
p |= 1<<i;
}
}
return p+1;
}
long gcd(long a,long b){ return b==0?a: gcd(b,a%b);}
InputStream is;PrintWriter out;
void run() throws Exception {is = System.in;
// is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in");
out = new PrintWriter(System.out);solve();out.flush();}
private byte[] inbuf = new byte[2];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {lenbuf = is.read(inbuf);} catch (IOException e) {throw new InputMismatchException();}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];}
private boolean isSpaceChar(int c) {return !(c >= 33 && c <= 126);}
private int skip() {int b;while((b = readByte()) != -1 && isSpaceChar(b));return b;}
private double nd() {return Double.parseDouble(ns());}
private char nc() {return (char) skip();}
private char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;}
private String ns() {int b = skip();StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[] ns(int n) {char[] buf = new char[n];int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b;b = readByte(); }
return n == p ? buf : Arrays.copyOf(buf, p);}
private String nline() {int b = skip();StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;}
private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;}
private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();return a;}
private int ni() { int num = 0, b; boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){};
if (b == '-') { minus = true; b = readByte(); }
while (true) {
if (b >= '0' && b <= '9') num = (num << 3) + (num << 1) + (b - '0');
else return minus ? -num : num;
b = readByte();}}
private long nl() { long num = 0; int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){};
if (b == '-') { minus = true; b = readByte(); }
while (true) {
if (b >= '0' && b <= '9') num = num * 10 + (b - '0');
else return minus ? -num : num;
b = readByte();}}
void print(Object obj){out.print(obj);}
void println(Object obj){out.println(obj);}
void println(){out.println();}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | a25cd9ff7b8e68eb74c2326cbba3ffca | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes |
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) throws java.lang.Exception {
//Reader pm =new Reader();
Scanner pm = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = 1;
while(t-- > 0){
int n = pm.nextInt();
String s = pm.next();
char[] c = s.toCharArray();
HashMap<Integer, Integer> hm = new HashMap<>();
for(int i=0;i<9;i++) {
int pl = pm.nextInt();
hm.put(i + 1, pl);
}
int last = -1;
int l = -1;
int r = -1;
for(int i=0;i<n;i++){
int cur = Character.getNumericValue(c[i]);
int exp = hm.get(cur);
if(exp > cur) {
l = i;
break;
}
// System.out.println(cur+" "+exp);
// if(exp == cur && i + 1 < n && Character.getNumericValue(c[i + 1]) <= hm.get(Character.getNumericValue(c[i]))) {
// last = i;
// }
// if(exp > cur && (last == -1 || last == (i-1))) {
// c[i] = (char) (exp + '0');
// last = i;
// }
}
if(l == -1) {
System.out.println(s);
return;
}
for(int i=l;i<n;i++) {
int cur = Character.getNumericValue(c[i]);
int exp = hm.get(cur);
if(exp < cur) {
r = i - 1;
break;
}
}
if(r == -1)
r = n-1;
for(int i=l;i<=r;i++) {
int cur = Character.getNumericValue(c[i]);
int exp = hm.get(cur);
c[i] = (char) (exp + '0');
}
System.out.println(new String(c));
}
//end of tests
}
//end of main class
static int countInRange(int arr[], int n, int x, int y) {
// initialize result
int count = 0;
count = upperIndex(arr, n, y) -
lowerIndex(arr, n, x) + 1;
return count;
}
static int lowerIndex(int arr[], int n, int x) {
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
// function to find last index <= y
static int upperIndex(int arr[], int n, int y) {
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
public static StringBuilder dec_to_bin(long n) {
// decimal to binary upto 30 binaries
if(n==0) {
StringBuilder str=new StringBuilder("");
for(int i=0;i<30;i++) {
str.append("0");
}
return str;
}
StringBuilder str=new StringBuilder("");
while(n!=0) {
str.append(n%2L);
n/=2L;
}
str=str.reverse();
StringBuilder tmp_str=new StringBuilder("");
int len=str.length();
while(len!=30) {
tmp_str.append("0");
len++;
}
tmp_str.append(str);
return tmp_str;
}
private static int binarySearchPM(int[] arr, int key){
int n = arr.length;
int mid = -1;
int begin = 0,end=n;
while(begin <= end){
mid = (begin+end) / 2;
if(mid == n){
return n;
}
if(key < arr[mid]){
end = mid-1;
} else if(key > arr[mid]){
begin = mid+1;
} else {
return mid;
}
}
//System.out.println(begin+" "+end);
return -begin; //expected Index
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | ba3eb7ebcf7fb27c31803ee22e82d0a5 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;public class S{public static void main(String[]g){Scanner s=new Scanner(System.in);int n=s.nextInt(),c=0,a;char[]b=s.next().toCharArray(),f=new char[58];for(int i=1;i<10;i++)f['0'+i]=((char)('0'+s.nextInt()));for(int i=0;i<n;i++)if(f[a=b[i]]>a|f[a]==a&c>0){c=1;b[i]=f[a];}else if(c>0)break;System.out.print(new String(b));}} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 18b2b34d930fef8eaca66fad9fc57dd3 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;public class S{public static void main(String[]g){Scanner s=new Scanner(System.in);int n=s.nextInt(),c=0,a;StringBuilder b=new StringBuilder(s.next());char[]f=new char[256];for(int i=1;i<10;i++)f['0'+i]=((char)('0'+s.nextInt()));for(int i=0;i<n;i++)if(f[a=b.charAt(i)]>a|f[a]==a&c>0){c=1;b.setCharAt(i,f[a]);}else if(c>0)break;System.out.print(b);}} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | b516f4e5b04c4827a410d286e87a7af5 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;public class S{public static void main(String[]g){Scanner s=new Scanner(System.in);int n=s.nextInt(),c=0,a;StringBuilder b=new StringBuilder(s.next());int[]f=new int[10];for(int i=1;i<10;i++)f[i]=s.nextInt();for(int i=0;i<n;i++)if(f[a=Character.getNumericValue(b.charAt(i))]>a||f[a]==a&&c>0){c=1;b.replace(i,i+1,Integer.toString(f[a]));}else if(c>0)break;System.out.print(b);}} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | fc083b6319615d02515c10bd8208594f | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt(), c = 1;
String t = s.next();
int[] f = new int[10];
for (int i = 1; i < 10; i++)
f[i] = s.nextInt();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
int a = Character.getNumericValue(t.charAt(i));
if (f[a] > a && c > 0 || f[a] == a && c > 1) {
c++;
sb.append(f[a]);
} else {
if (c > 1)
c = 0;
sb.append(a);
}
}
System.out.println(sb);
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 3147b1780e8e8b750e0dad235f90054a | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class atharva {
InputStream is;
PrintWriter out;
long mod = (long)(1e9 + 7), inf = (long)(1e18);
class pair {
long A, B;
pair(long a, long b) {
A = a; B = b;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void solve() {
int n=ni();
char ch[]=ns().toCharArray();
int map[]=new int[10];
for(int i=1;i<=9;i++)
map[i]=ni();
StringBuilder sb=new StringBuilder();int flg=0;int ctr=0;
for(int i=0;i<n;i++)
{
int curr=(ch[i]-48);
if(curr<=map[curr]&&ctr==0)
{
sb.append(map[curr]);
if(curr<map[curr])
flg=1;
}
else
{
sb.append(ch[i]);
if(flg==1)
{
ctr=1;
}
}
}
out.println(sb);
}
static long lbound(LinkedList<Long> l,long ele)
{
int low=0;
int high=l.size()-1;
int ans=0;
while(low<high)
{
int mid=(low+high)/2;
if(l.get(mid)==ele)
{ans=mid;break;}
else if(l.get(mid)>ele)
{high=mid-1;}
else
{ans=mid;low=mid;}
}
return l.get(ans);
}
//---------- I/O Template ----------
public static void main(String[] args) { new atharva().run(); }
void run() {
is = System.in;
out = new PrintWriter(System.out);
solve();
out.flush();
}
byte input[] = new byte[1024];
int len = 0, ptr = 0;
int readByte() {
if(ptr >= len) { ptr = 0;
try { len = is.read(input); }
catch(IOException e) { throw new InputMismatchException(); }
if(len <= 0) { return -1; }
} return input[ptr++];
}
boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
int skip() {
int b = readByte();
while(b != -1 && isSpaceChar(b)) { b = readByte(); }
return b;
}
char nc() { return (char)skip(); }
String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
String nLine() {
int b = skip();
StringBuilder sb = new StringBuilder();
while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
int ni() {
int n = 0, b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
if(b == -1) { return -1; } //no input
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
long nl() {
long n = 0L; int b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
double nd() { return Double.parseDouble(ns()); }
float nf() { return Float.parseFloat(ns()); }
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) { a[i] = ni(); }
return a;
}
char[] ns(int n) {
char c[] = new char[n];
int i, b = skip();
for(i = 0; i < n; i++) {
if(isSpaceChar(b)) { break; }
c[i] = (char)b; b = readByte();
} return i == n ? c : Arrays.copyOf(c,i);
}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 8bba0620717fc3ad2936b68250d1b56b | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter pw;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
int poo=sc.nextInt();
char[] s = sc.next().toCharArray();
int []a=new int[10];
for (int i = 1; i <10 ; i++) {
a[i]=sc.nextInt();
}
StringBuilder ans=new StringBuilder();
for (int i = 0; i < s.length; i++) {
int e =s[i]-'0';
if (e<a[e]){
int y = e;
while (i<s.length){
y = s[i]-'0';
if (a[y]>=y){
ans =ans.append(a[y]);
}
else {
break;
}
i++;
}
for (int j = i; j < s.length; j++) {
ans = ans .append(s[j]-'0');
}
pw.print(ans);
pw.close();
return;
}
else {
ans=ans.append(e);
}
}
pw.print(ans);
pw.close();
}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 31a40ddb97e643c84d662b5940ab179e | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.BigInteger;
public class MapLargestNumber
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long digitsOfA = Long.parseLong(sc.next());
String strNum = sc.next();
//System.out.println("input test: " + strNum/Math.pow(10, digitsOfA-1));
HashMap<Integer, Long> mapOfDigits = new HashMap<>();
int arrLength = (int) digitsOfA;
int[] arrayOfNumbers = new int[arrLength];
char[] numberCharArr;
numberCharArr = strNum.toCharArray();
//System.out.println("Length Test: " + numberCharArr.length);
for(int i = 0; i < arrayOfNumbers.length; i++){
arrayOfNumbers[i] = Character.getNumericValue(numberCharArr[i]);
}
int mapCount = 1;
while(sc.hasNext()){
mapOfDigits.put(mapCount, Long.parseLong(sc.next()));
mapCount++;
}
getMaxNumber(arrayOfNumbers, mapOfDigits);
}
public static void getMaxNumber(int[] arrayOfNumbers, HashMap<Integer,Long> mapOfDigits){
boolean start = false;
for(int i = 0; i < arrayOfNumbers.length; i++){
if(mapOfDigits.get(arrayOfNumbers[i]).intValue() > arrayOfNumbers[i]){
//get corresponding key's value
arrayOfNumbers[i] = mapOfDigits.get(arrayOfNumbers[i]).intValue();
start = true;
} else if(arrayOfNumbers[i] > mapOfDigits.get(arrayOfNumbers[i]) && start == true){
//discontinue because it's no longer contiguous.
start = false;
break;
} else if(arrayOfNumbers[i] >= mapOfDigits.get(arrayOfNumbers[i]) && start == false){
continue;
}
else{
//System.out.println("continue");
continue;
}
}
//create finalValue by iterating over arrayOfNumbers
for(int x : arrayOfNumbers){
System.out.print(x);
}
}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 4ca48be0dffe38e75639755a61d215fa | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class cf1557b {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String s = br.readLine();
String[] t = br.readLine().split(" ");
int[] f = new int[10];
for (int i = 1; i < 10; i++) {
f[i] = Integer.parseInt(t[i - 1] + "");
}
List<Integer> diff = new ArrayList<>();
for (int i = 0; i < n; i++) {
int tmp = Integer.parseInt(s.charAt(i) + "");
diff.add(f[tmp] - tmp);
}
String[] tmpArr = s.split("");
for (int i = 0; i < n; i++) {
if (diff.get(i) > 0) {
while (i < n && diff.get(i) >= 0) {
tmpArr[i] = f[Integer.parseInt(tmpArr[i])]+"";
i++;
}
break;
}
}
System.out.println(String.join("", tmpArr));
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 2095ecbea03c55e58b0932bcc8e4f07f | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;
public class longNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String str=s.next();
int fun[]=new int[10];
for(int i=1;i<10;i++)
fun[i]=s.nextInt();
int first=-1,last=-1;
for(int i=0;i<str.length();i++) {
if(fun[(str.charAt(i)-'0')]>str.charAt(i)-'0')
{
if(first==-1)
first=i;
while(i<str.length() && fun[(str.charAt(i)-'0')]>=str.charAt(i)-'0')
i++;
last=i-1;
break;
}
}
for(int i=0;i<str.length();i++) {
if(i>=first && i <=last)
System.out.print(fun[(str.charAt(i)-'0')]);
else
System.out.print(str.charAt(i)-'0');
}
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 0c03fece0286b4c12a2dc3d970df97f0 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.io.*;
import java.util.*;
public class Task {
static int[] f;
public static void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
StringBuilder s = new StringBuilder(in.next());
StringBuilder ans = new StringBuilder();
f = new int[10];
for (int i = 1; i <= 9; ++i) f[i] = in.nextInt();
boolean started = false, ended = false;
for (int i = 0; i < s.length(); ++i) {
if (!started && !ended) {
if (f[s.charAt(i) - '0'] > s.charAt(i) - '0') {
started = true;
ans.append(f[s.charAt(i) - '0']);
} else {
ans.append(s.charAt(i));
}
} else if (started && !ended) {
if (f[s.charAt(i) - '0'] >= s.charAt(i) - '0') {
ans.append(f[s.charAt(i)-'0']);
}else{
ended=true;
ans.append(s.charAt(i));
}
}else{
ans.append(s.charAt(i));
}
}
out.println(ans.toString());
}
public static void main(String[] args) {
FastScanner ind = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out, true);
Scanner in = new Scanner(System.in);
solve(0, in, out);
}
}
class Pair {
int end, start;
public Pair(int start, int end) {
this.end = end;
this.start = start;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream inputStream) {
br = new BufferedReader(new
InputStreamReader(inputStream));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 09f34ce76311d8d18b984790c6c15e14 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;
public class Class{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
String x=sc.nextLine();
String y=(sc.nextLine().replaceAll(" ",""));
StringBuilder ans=new StringBuilder();
boolean flag=true;
for(int i=0;ans.length()!=x.length()&&i<x.length();i++){
if(flag&&x.charAt(i)<y.charAt(Integer.parseInt(""+x.charAt(i))-1)){
flag=false;
ans.append(y.charAt(Integer.parseInt(""+x.charAt(i))-1));
for(int j=i+1;j<x.length();j++){
if(x.charAt(j)<=y.charAt(Integer.parseInt(""+x.charAt(j))-1))
ans.append(y.charAt(Integer.parseInt(""+x.charAt(j))-1));
else {
i=j-1;
break;
}
}
}
else ans.append(x.charAt(i));
}
System.out.print(ans);
}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 03bc0dcd764ca197b37098455f0c1f6c | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;
public class Class {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
String s=sc.nextLine();
String fx=sc.nextLine().replaceAll(" ","");
StringBuilder ans=new StringBuilder();
boolean flag=false;
for(int i=0;i<s.length();i++){
int x=Integer.parseInt(""+s.charAt(i));
if(flag||s.charAt(i)>=fx.charAt(x-1))ans.append(s.charAt(i));
else {
ans.append(fx.charAt(x-1));
while(++i<s.length()){
x=Integer.parseInt(""+s.charAt(i));
if(s.charAt(i)<=fx.charAt(x-1))ans.append(fx.charAt(x-1));
else {
i--;
flag=true;
break;
}
}
}
}
System.out.println(ans);
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | 7e22b3af072d14805ab44b455328ecd4 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.io.PrintWriter;
import java.util.*;
/**
* Created by Andrey on 17.02.2019.
*/
public class TaskD1 {
static final double PI = 3.14159265359;
void solve(Scanner in, PrintWriter out) {
int n = in.nextInt();
in.nextLine();
String st = in.nextLine();
char[] f = new char[9];
for (int i = 0; i < 9; ++i) {
f[i] = in.next().charAt(0);
}
char[] arr = st.toCharArray();
boolean flag = false;
for (int i = 0; i < n; ++i) {
char num = arr[i];
if (f[num - '1'] > num) {
flag = true;
arr[i] = f[num - '1'];
} else if(f[num - '1'] < num){
if (flag) break;
}
}
out.println(new String(arr));
}
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
void run() {
try (Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out)) {
solve(in, out);
}
}
public static void main(String[] args) {
new TaskD1().run();
}
} | Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | f0561fcb30b1eb49207dbc3c18f1fd46 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
*
* @author Haya
*/
public class LongNumber {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
in.readLine();
char[] num = in.readLine().toCharArray();
char[] vals = in.readLine().replace(" ", "").toCharArray();
boolean flag = false;
for (int i = 0; i < num.length; i++) {
int n = convert(num[i]);
if (n > convert(vals[n - 1]) && flag) {
break;
} else if (convert(vals[n - 1]) > n) {
num[i] = vals[n - 1];
flag = true;
}
}
System.out.println(new String(num));
}
public static int convert(char c) {
return c - '0';
}
public 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 string = "";
try {
string = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | c0a51184ad21aac48b575bde87934ef2 | train_002.jsonl | 1556289300 | You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once? | 256 megabytes | import java.util.*;
public class Num2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
char[] c = scan.next().toCharArray();
int[] f = new int[10];
for (int i = 1; i <= 9; i++)
f[i] = scan.nextInt();
int i = 0;
while (i < n && c[i]-'0' >= f[c[i]-'0']) {
i++;
}
while (i < n && c[i]-'0' <= f[c[i]-'0']) {
c[i] = (char) (f[c[i]-'0'] + '0');
i++;
}
System.out.println(new String(c));
}
}
| Java | ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"] | 2 seconds | ["1557", "99999", "33"] | null | Java 8 | standard input | [
"greedy"
] | 378a9ab7ad891d60f23645106d24f314 | The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \le f(i) \le 9$$$). | 1,300 | Print the maximum number you can get after applying the operation described in the statement no more than once. | standard output | |
PASSED | c20822873bc146dc3512b4a4dbbbe8b9 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
StringBuilder ans=new StringBuilder("");
int test=input.scanInt();
for(int tt=1;tt<=test;tt++) {
int n=input.scanInt();
int m=input.scanInt();
int arr[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
arr[i][j]=input.scanInt();
}
}
long fin=0;
boolean taken[][]=new boolean[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
ArrayList<Integer> arrli=new ArrayList<>();
if(!taken[i][j]) {
arrli.add(arr[i][j]);
taken[i][j]=true;
}
if(!taken[n-i-1][j]) {
arrli.add(arr[n-i-1][j]);
taken[n-i-1][j]=true;
}
if(!taken[i][m-j-1]) {
arrli.add(arr[i][m-j-1]);
taken[i][m-j-1]=true;
}
if(!taken[n-i-1][m-j-1]) {
arrli.add(arr[n-i-1][m-j-1]);
taken[n-i-1][m-j-1]=true;
}
fin+=get(arrli);
}
}
ans.append(fin+"\n");
}
System.out.println(ans);
}
public static long get(ArrayList<Integer> arrli) {
if(arrli.size()==0) {
return 0;
}
long min=Long.MAX_VALUE;
for(int i=0;i<arrli.size();i++) {
long tmp=0;
for(int j=0;j<arrli.size();j++) {
tmp+=Math.abs(arrli.get(i)-arrli.get(j));
}
min=Math.min(min,tmp);
}
return min;
}
}
| Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 989fd26a0bfff1069aef4f31a9b8dddc | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt(),m=sc.nextInt();
int a[][]=new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
long ans=0,sum=0;
int b[]=new int[3];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
//System.out.println(i+" "+j);
b[0]=a[i][j];b[1]=a[i][m-j-1];b[2]=a[n-i-1][j];
Arrays.sort(b);
ans+=Math.abs(b[1]-b[0])+Math.abs(b[1]-b[2]);
a[i][j]=b[1];
a[n-i-1][j]=b[1];
a[i][m-j-1]=b[1];
}
}
System.out.println(ans);
}
//System.out.println("XXXXXXXX");
}
}
| Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 5c6bf252048c1924a87268dcab64088a | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | //package dataStructures;
import java.util.*;
import java.lang.*;
import java.io.*;
public class test1
{
static long mod = 1000000007;
public static void main (String[] args) throws IOException
{
FastReader sc = new FastReader();
int t = sc.nextInt();
StringBuffer ans = new StringBuffer("");
while(t-->0) {
int n=sc.nextInt(),m=sc.nextInt(),arr[][] = new int[n][m];
long sum=0;
for(int i=0;i<n;i++)for(int j=0;j<m;j++)arr[i][j]=sc.nextInt();
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
int[] x = {arr[i][j],arr[n-1-i][j],arr[i][m-1-j],arr[n-1-i][m-1-j]};
Arrays.sort(x);
sum+=x[2]+x[3]-x[0]-x[1];
}
}
sum/=4;
ans.append(sum+"\n");
}
System.out.println(ans);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 107351219ae72fda463a813f556cefee | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | /*input
2
4 2
4 2
2 4
4 2
2 4
3 4
1 2 3 4
5 6 7 8
9 10 11 18
3
1 4
1 2 9 2
4 1
2
2
3
3
2 2
2 2
10 2
4 2
4 2
2 4
4 2
2 4
3 4
1 2 3 4
5 6 7 8
9 10 11 18
*/
import java.util.*;
public class Easy3{
public static <Type> void print(Type val) { System.out.println(val); }
public static <Type> String printArr(Type val[]){
StringBuilder ans = new StringBuilder("");
for(Type v:val)
ans.append(v+" ");
ans.append("\n");
return String.valueOf(ans);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(),i,n,k,rows,cols,begc,endc;
long arr[][];
while(t>0)
{
t--;
rows = sc.nextInt();cols=sc.nextInt();
arr = new long[rows][cols];
for(i=0;i<rows;i++){
for(int j=0;j<cols;j++)
arr[i][j] = sc.nextLong();
}
long ans = 0;
int begr = 0, endr = rows-1;
while(begr<=endr){
begc = 0;
endc = cols - 1;
while(begc<endc){
long curr[],div;
if(begr==endr){
curr = new long[2];
curr[0] = arr[begr][begc];
curr[1] = arr[begr][endc];
div=2;
}
else{
curr = new long[4];
curr[0] = arr[begr][begc];
curr[1] = arr[begr][endc];
curr[2] = arr[endr][begc];
curr[3] = arr[endr][endc];
div = 4;
}
long avg = 0;//arr[begr][begc] + arr[begr][endc] + arr[endr][begc] + arr[endr][endc];
for(long ele:curr){
avg+=ele;
}
avg/=div;
// print(avg);
long op1 = 0,op2=0;
for(long ele:curr){
op1+=Math.abs(ele-avg);
}
avg++;
for(long ele:curr){
op2+=Math.abs(ele-avg);
}
long ansc = Math.min(op1,op2);
for(long ele:curr){
op2 = 0;
for(long ele2:curr){
op2+=Math.abs(ele2-ele);
}
ansc = Math.min(ansc,op2);
}
ans+=ansc;
// System.out.println("begr = " + begr + " BEGC = "+ begc + " ans="+ans);
begc++;
endc--;
}
if(begc==endc ){
ans+=Math.abs(arr[begr][begc]-arr[endr][begc]);
}
// System.out.println("begr = " + begr + " ans="+ans);
begr++;
endr--;
}
print(ans);
}
sc.close();
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 020a20ee53a79bf7553f89709af36140 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.io.*;
import java.util.*;
import static java.util.Collections.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf {
private static final int MOD = (int) (1e9 + 7), MOD_FFT = 998244353;
private static final Reader r = new Reader();
private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
private static final boolean thread = false;
static final boolean HAS_TEST_CASES = true;
static final int SCAN_LINE_LENGTH = 1000002;
static int n, a[], e[][];
private static Vector<Integer> adj[], v = new Vector<>();
private static Vector<pi> g[];
static void solve() throws Exception {
int n = ni(), m = ni();
e = new int[n][m];
for (int i = 0; i < e.length; i++) {
e[i] = na(m);
}
long ans = 0;
for (int i = 0; i <= (e.length - 1) / 2; i++) {
for (int j = 0; j <= (e[0].length - 1) / 2; j++) {
HashSet<pi> ind = new HashSet<>();
ind.add(new pi(i, j));
ind.add(new pi(n - i - 1, j));
ind.add(new pi(i, m - j - 1));
ind.add(new pi(n - i - 1, m - j - 1));
pi a[] = new pi[ind.size()];
Iterator<pi> it = ind.iterator();
int k = 0;
while (it.hasNext()) {
a[k++] = it.next();
}
long avg = Long.MAX_VALUE;
for (int k2 = 0; k2 < a.length; k2++) {
long t = 0;
for (int l = 0; l < a.length; l++) {
if (k2 == l)
continue;
t += abs(e[a[l].fir][a[l].snd] - e[a[k2].fir][a[k2].snd]);
}
avg = min(avg, t);
}
ans += avg;
}
}
pn(ans);
}
public static void main(final String[] args) throws Exception {
if (!thread) {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// pni(i + " t");
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final Exception e) {
// pni("idk Exception");
// e.printStackTrace(System.err);
// System.exit(0);
throw e;
}
}
out.flush();
}
r.close();
out.close();
}
private static void swap(final int i, final int j) {
final int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static boolean isMulOverflow(final long A, final long B) {
if (A == 0 || B == 0)
return false;
final long result = A * B;
if (A == result / B)
return true;
return false;
}
private static int gcd(final int a, final int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static class Pair<T, E> implements Comparable<Pair<T, E>> {
T fir;
E snd;
Pair() {
}
Pair(final T x, final E y) {
this.fir = x;
this.snd = y;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(final Pair<T, E> o) {
final int c = ((Comparable<T>) fir).compareTo(o.fir);
return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd);
}
}
private static class pi implements Comparable<pi> {
int fir, snd;
pi() {
}
pi(final int a, final int b) {
fir = a;
snd = b;
}
public int compareTo(final pi o) {
return fir == o.fir ? snd - o.snd : fir - o.fir;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + fir;
result = prime * result + snd;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
pi other = (pi) obj;
if (fir != other.fir)
return false;
if (snd != other.snd)
return false;
return true;
}
}
private static <T> void checkV(final Vector<T> adj[], final int i) {
adj[i] = adj[i] == null ? new Vector<>() : adj[i];
}
private static int[] na(final int n) throws Exception {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private static int[] na1(final int n) throws Exception {
final int[] a = new int[n + 1];
for (int i = 1; i < a.length; i++) {
a[i] = ni();
}
return a;
}
private static String n() throws IOException {
return r.readToken();
}
private static String nln() throws IOException {
return r.readLine();
}
private static int ni() throws IOException {
return r.nextInt();
}
private static long nl() throws IOException {
return r.nextLong();
}
private static double nd() throws IOException {
return r.nextDouble();
}
private static void p(final Object o) {
out.print(o);
}
private static void pn(final Object o) {
out.println(o);
}
private static void pn() {
out.println("");
}
private static void pi(final Object o) {
out.print(o);
out.flush();
}
private static void pni() {
out.println("");
out.flush();
}
private static void pni(final Object o) {
out.println(o);
out.flush();
}
private static class Reader {
private final int BUFFER_SIZE = 1 << 17;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
// private StringTokenizer st;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(final String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
final byte[] buf = new byte[SCAN_LINE_LENGTH];
public String readLine() throws IOException {
int cnt = 0;
int c;
o: while ((c = read()) != -1) {
if (c == '\n')
if (cnt == 0)
continue o;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String readToken() throws IOException {
int cnt = 0;
int c;
o: while ((c = read()) != -1) {
if (!(c >= 33 && c <= 126))
if (cnt == 0)
continue o;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static {
if (thread)
new Thread(null, new Runnable() {
@Override
public void run() {
try {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final ArrayIndexOutOfBoundsException e) {
e.printStackTrace(System.err);
System.exit(-1);
} catch (final Exception e) {
pni("idk Exception in solve");
e.printStackTrace(System.err);
System.exit(-1);
}
}
out.flush();
} catch (final Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
}, "rec", (1L << 28)).start();
}
@SuppressWarnings({ "unchecked", })
private static <T> T deepCopy(final T old) {
try {
return (T) deepCopyObject(old);
} catch (final IOException | ClassNotFoundException e) {
e.printStackTrace(System.err);
System.exit(-1);
}
return null;
}
private static Object deepCopyObject(final Object oldObj) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
} catch (final ClassNotFoundException e) {
pni("Exception in ObjectCloner = " + e);
throw (e);
} finally {
oos.close();
ois.close();
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 0b80a2736eb2cd21995d7c17b6a02d56 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
static final int INF = (int) (1e9 + 10);
static final int MOD = (int) (1e9 + 7);
// static final int N = (int) (4e5 + 5);
// static ArrayList<Integer>[] graph;
// static boolean visited[];
// static long size[];
public static void main(String[] args) throws NumberFormatException, IOException {
FastReader sc = new FastReader();
PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
long [][] mat = new long[n][m];
for(int i =0 ; i < n ; i++){
for(int j =0 ; j < m ; j++){
mat[i][j] = sc.nextLong();
}
}
long ans =0;
for(int i = 0 ; i < n ; i++){
for(int j =0 ; j < m ; j++){
long [] arr = {mat[i][j] , mat[i][m-j-1] , mat[n-i-1][j] , mat[n-i-1][m-j-1]};
Arrays.sort(arr);
ans += Math.abs(mat[i][j] - arr[1]);
}
}
pr.println(ans);
// pr.println(count);
}
pr.flush();
pr.close();
}
public static boolean pal(long [][] mat , int n , int m) {
for(int i =0 ; i < n ; i++){
int l =0;
int r = m-1;
while(l < r){
if(mat[i][l] != mat[i][r]) return false;
l++;
r--;
}
}
for(int i =0 ; i < m ; i++){
int l = 0;
int r = n-1;
while(l < r){
if(mat[l][i] != mat[r][i])return false;
l++;
r--;
}
}
return true;
}
public static void dfsf(boolean [] vis , int u , ArrayList<ArrayList<Integer>> g){
vis[u] = true;
//yaha par har componet k element ka outut le sakte hai;
for(int i = 0 ; i < g.get(u).size() ; i++){
if(!vis[g.get(u).get(i)]){
dfsf(vis , g.get(u).get(i) , g );
}
}
}
public static void dfsft(int i , boolean [] vis , Stack<Integer> stk , ArrayList<ArrayList<Integer>> adj){
vis[i] = true;
for(int j = 0 ; j < adj.get(i).size() ; j++){
if(!vis[adj.get(i).get(j)]){
dfsft(adj.get(i).get(j) , vis ,stk , adj);
}
}
stk.push(i);
}
public static ArrayList<ArrayList<Integer>> transpose(ArrayList<ArrayList<Integer>> adj , int n){
ArrayList<ArrayList<Integer>> g = new ArrayList<>();
for(int i = 0 ; i < n ; i++){
g.add(new ArrayList<Integer>() );
}
for(int i = 0 ; i < n ; i++ ){
for(int j = 0 ; j < adj.get(i).size() ; j++){
g.get(adj.get(i).get(j)).add(i);
}
}
return g;
}
/*
* Template From Here
*/
private static boolean possible(long[] arr, int[] f, long mid, long k) {
long c = 0;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] * f[f.length - i - 1]) > mid) {
c += (arr[i] - (mid / f[f.length - i - 1]));
}
}
// System.out.println(mid+" "+c);
if (c <= k)
return true;
return false;
}
public static int lowerBound(ArrayList<Integer> array, int length, long value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value <= array.get(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
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 long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static void sortbyColumn(int[][] att, int col) {
// Using built-in sort function Arrays.sort
Arrays.sort(att, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1, final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
// if (entry1[col] > entry2[col])
// return 1;
// else //(entry1[col] >= entry2[col])
// return -1;
return new Integer(entry1[col]).compareTo(entry2[col]);
// return entry1[col].
}
}); // End of function call sort().
}
static class pair {
int V, I;
pair(int v, int i) {
V = v;
I = i;
}
}
public static int[] swap(int data[], int left, int right) {
int temp = data[left];
data[left] = data[right];
data[right] = temp;
return data;
}
public static int[] reverse(int data[], int left, int right) {
while (left < right) {
int temp = data[left];
data[left++] = data[right];
data[right--] = temp;
}
return data;
}
public static boolean findNextPermutation(int data[]) {
if (data.length <= 1)
return false;
int last = data.length - 2;
while (last >= 0) {
if (data[last] < data[last + 1]) {
break;
}
last--;
}
if (last < 0)
return false;
int nextGreater = data.length - 1;
for (int i = data.length - 1; i > last; i--) {
if (data[i] > data[last]) {
nextGreater = i;
break;
}
}
data = swap(data, nextGreater, last);
data = reverse(data, last + 1, data.length - 1);
return true;
}
static long nCr(long n, long r) {
if (n == r)
return 1;
return fact(n) / (fact(r) * fact(n - r));
}
static long fact(long n) {
long res = 1;
for (long i = 2; i <= n; i++)
res = res * i;
return res;
}
/*
* static boolean prime[] = new boolean[1000007];
*
* public static void sieveOfEratosthenes(int n) {
*
* for (int i = 0; i < n; i++) prime[i] = true; * for (int p = 2; p * p <=
* n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p]
* == true) { // Update all multiples of p for (int i = p * p; i <= n; i +=
* p) prime[i] = false; } }
*
* // Print all prime numbers // for(int i = 2; i <= n; i++) // { //
* if(prime[i] == true) // System.out.print(i + " "); // } }
*/
static long power(long fac2, int y, int p) {
long res = 1;
fac2 = fac2 % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * fac2) % p;
fac2 = (fac2 * fac2) % p;
}
return res;
}
static long modInverse(long fac2, int p) {
return power(fac2, p - 2, p);
}
static boolean isPrime(int n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
public static BigInteger lcmm(String a, String b) {
BigInteger s = new BigInteger(a);
BigInteger s1 = new BigInteger(b);
BigInteger mul = s.multiply(s1);
BigInteger gcd = s.gcd(s1);
BigInteger lcm = mul.divide(gcd);
return lcm;
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 336bbe0d39a4cefd140a3934fbd4e9b9 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces{
public static void main(String args[]){
codeforces s=new codeforces();
Scanner sc=new Scanner(System.in);
for (long t = sc.nextInt(), sum=0; t-- > 0; sum=0) {
int n = sc.nextInt(), m = sc.nextInt(), a[][] = new int[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = sc.nextInt();
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
int p[]= {a[i][j],a[i][m-1-j],a[n-1-i][j],a[n-1-i][m-1-j]};
Arrays.sort(p);
sum+=p[2]+p[3]-p[0]-p[1];
}
System.out.println(sum/4);
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 2056561de5c83c85ad8f1e6e97db3593 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
public class nice {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-->0){
int N = sc.nextInt();
int M = sc.nextInt();
int[][] grid = new int[N][M];
int[][] original = new int[N][M];
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
grid[i][j] = sc.nextInt();
original[i][j] = grid[i][j];
}
}
long ans = 0;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
int[] arr = new int[4];
arr[0] = grid[i][j];
arr[1] = grid[N-i-1][j];
arr[2] = grid[i][M-j-1];
arr[3] = grid[N-i-1][M-j-1];
Arrays.sort(arr);
int val = (arr[1] + arr[2])/2;
grid[i][j] = val;
grid[N-i-1][j] = val;
grid[i][M-j-1] = val;
grid[N-i-1][M-j-1] = val;
}
}
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
ans += Math.abs(grid[i][j] - original[i][j]);
//System.out.print(grid[i][j] + " ");
}
//System.out.println();
}
System.out.println(ans);
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | f743654ee69e85645ec719d935121a12 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
public class SolutionB extends Thread {
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;
}
}
private static final FastReader scanner = new FastReader();
private static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
new Thread(null, new SolutionB(), "Main", 1 << 26).start();
}
public void run() {
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
solve();
}
out.close();
}
static class Pos {
int row;
int col;
public Pos(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Pos)) {
return false;
}
Pos pos = (Pos) o;
if (row != pos.row) {
return false;
}
return col == pos.col;
}
@Override
public int hashCode() {
int result = row;
result = 31 * result + col;
return result;
}
}
private static void solve() {
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = scanner.nextInt();
}
}
long ops = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
List<Pos> mirrorPoints = new ArrayList<>();
mirrorPoints.add(new Pos(i, j));
mirrorPoints.add(new Pos(i, m - 1 - j));
mirrorPoints.add(new Pos(n - 1 - i, j));
mirrorPoints.add(new Pos(n - 1 - i, m - 1 - j));
long minOps = Long.MAX_VALUE;
int minValue = 0;
for (Pos p: mirrorPoints) {
long tmpOps = 0;
int value = a[p.row][p.col];
for (Pos q: mirrorPoints.stream().distinct().collect(Collectors.toList())) {
tmpOps += Math.abs(value - a[q.row][q.col]);
}
if (tmpOps < minOps) {
minOps = tmpOps;
minValue = value;
}
}
ops += minOps;
for (Pos p: mirrorPoints) {
a[p.row][p.col] = minValue;
}
}
}
System.out.println(ops);
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 3f8574c6828a342d384a1f508aa5034f | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int tc = s.nextInt();
for(int t = 0;t < tc;t++) {
int n = s.nextInt();
int m = s.nextInt();
int[][] input = new int[n][m];
for(int i = 0;i < n;i++) {
for(int j = 0;j < m;j++) {
input[i][j] = s.nextInt();
}
}
long ans = 0;
for(int i = 0;i < n;i++) {
for(int j = 0;j < m;j++) {
int[] arr = new int[3];
arr[0] = input[i][j];
arr[1] = input[n - i - 1][j];
arr[2] = input[i][m - j - 1];
for(int i1 = 0;i1 < 3;i1++) {
for(int j1 = 0;j1 < 3 - i1 - 1;j1++) {
if(arr[j1] > arr[j1 + 1]) {
int temp = arr[j1];
arr[j1] = arr[j1 + 1];
arr[j1 + 1] = temp;
}
}
}
input[i][j] = input[n - i - 1][j] = input[i][m - j - 1] = arr[1];
ans += (arr[1] - arr[0]) + (arr[2] - arr[1]);
}
}
System.out.println(ans);
}
}
}
| Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 204212951f7d05ba165efe305680fe0f | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
outer: while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int arr[][] = new int[n][m];
// int end[] = new int[n];
// long fac[] = new long[n + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = sc.nextInt();
}
// end[i] = sc.nextInt()
// int end[] = new int[n];
// long fac[] = new long[n + 1];;
}
long ans=0;
for (int i = 0; i <= (arr.length - 1) / 2; i++) {
for (int j = 0; j <= (arr[0].length - 1) / 2; j++) {
HashSet<pi> hash = new HashSet<>();
hash.add(new pi(i, j));
// int end[] = new int[n];
// long fac[] = new long[n + 1];
hash.add(new pi(n - i - 1, j));
hash.add(new pi(i, m - j - 1));
hash.add(new pi(n - i - 1, m - j - 1));
pi a[] = new pi[hash.size()];
Iterator<pi> it = hash.iterator();
// int end[] = new int[n];
// long fac[] = new long[n + 1];
int k = 0;
while (it.hasNext()) {
a[k++] = it.next();
}
long averagecount = Long.MAX_VALUE;
for (int k2 = 0; k2 < a.length; k2++) {
// int end[] = new int[n];
// long fac[] = new long[n + 1];
long ttt = 0;
for (int l = 0; l < a.length; l++) {
if (k2 == l)
// int end[] = new int[n];
// long fac[] = new long[n + 1];
continue;
ttt += Math.abs(arr[a[l].fir][a[l].snd] - arr[a[k2].fir][a[k2].snd]);
}
averagecount = Math.min(averagecount, ttt);
// int end[] = new int[n];
// long fac[] = new long[n + 1];
}
ans += averagecount;
}
}
System.out.println(ans);
}
}
static class pi {
int fir, snd;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + fir;
result = prime * result + snd;
return result;
// int end[] = new int[n];
// long fac[] = new long[n + 1];
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
pi other = (pi) obj;
if (fir != other.fir)
// int end[] = new int[n];
// long fac[] = new long[n + 1];
return false;
if (snd != other.snd)
return false;
return true;
// int end[] = new int[n];
// long fac[] = new long[n + 1];
}
public pi(int fir, int snd) {
this.fir = fir;
// int end[] = new int[n];
// long fac[] = new long[n + 1];
this.snd = snd;
}
}
}
| Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | ef16b3a347d1ecf33ac06f535d352cb3 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces{
public static void main(String args[]){
//codeforces s=new codeforces();
Scanner sc=new Scanner(System.in);
for (long t = sc.nextInt(), sum=0; t-- > 0; sum=0) {
int n = sc.nextInt(), m = sc.nextInt(), a[][] = new int[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = sc.nextInt();
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
int p[]= {a[i][j],a[i][m-1-j],a[n-1-i][j],a[n-1-i][m-1-j]};
Arrays.sort(p);
sum+=p[2]+p[3]-p[0]-p[1];
}
System.out.println(sum/4);
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 3a15fbf9859cff2f3cfa1bd13b5b3229 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces{
public static void main(String args[]){
codeforces s=new codeforces();
Scanner sc=new Scanner(System.in);
for (long t = sc.nextInt(), sum=0; t-- > 0; sum=0) {
int n = sc.nextInt(), m = sc.nextInt(), a[][] = new int[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = sc.nextInt();
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
int p[]= {a[i][j],a[i][m-1-j],a[n-1-i][j],a[n-1-i][m-1-j]};
Arrays.sort(p);
sum+=p[2]+p[3]-p[0]-p[1];
}
System.out.println(sum/4);
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 8da2f6f315b4f702276074c4e708aca2 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | //package Codeforces.Round675Div2;
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
Soumit sc = new Soumit();
int t = sc.nextInt();
while (t-->0)
{
int n = sc.nextInt();
int m = sc.nextInt();
long[][] mat = new long[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
mat[i][j] = sc.nextInt();
}
}
long answer = 0;
for(int i=0;i<=(n-1)/2;i++)
{
for(int j=0;j<=(m-1)/2;j++)
{
ArrayList<Long> arr = new ArrayList<>();
arr.add(mat[i][j]);
if(m%2!=1 || j!=(m-1)/2) {
arr.add(mat[i][m - j - 1]);
}
if(n%2!=1 || i!=(n-1)/2)
{
arr.add(mat[n-i-1][j]);
}
if((n%2!=1 || i!=(n-1)/2) && (m%2!=1 || j!=(m-1)/2))
{
arr.add(mat[n-i-1][m-j-1]);
}
Collections.sort(arr);
long min = Long.MAX_VALUE;
for(long l: arr)
{
long ans = 0;
for(long l1: arr)
{
ans += Math.abs(l-l1);
}
min = Math.min(ans , min);
}
answer += min;
}
}
System.out.println(answer);
}
sc.close();
}
static class Soumit {
final private int BUFFER_SIZE = 1 << 18;
final private DataInputStream din;
final private byte[] buffer;
private PrintWriter pw;
private int bufferPointer, bytesRead;
StringTokenizer st;
public Soumit() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Soumit(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public void streamOutput(String file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
}
public void println(String a) {
pw.println(a);
}
public void print(String a) {
pw.print(a);
}
public String readLine() throws IOException {
byte[] buf = new byte[100064]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
/*if (din == null)
return;*/
if (din != null) din.close();
if (pw != null) pw.close();
}
}
}
| Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 395a15227a1cd6259a62599fedd29001 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | // package oct2020;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class cf_subs {
static FastReader scn = new FastReader();
static OutputStream out = new BufferedOutputStream(System.out);
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int tc = scn.nextInt();
while(tc-->0) {
int n =scn.nextInt(),m= scn.nextInt();
int[][] g = new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
g[i][j]=scn.nextInt();
}
}
long ans=0;
boolean ok[][] =new boolean[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(!ok[i][j]) {
// ok[n-i-1][j]=ok[i][m-j-1]=ok[n-i-1][m-j-1]=true;
int[] xd = {i,i,n-i-1,n-i-1},yd = {j,m-j-1,j,m-j-1};
ArrayList<Integer> parts = new ArrayList<>();
for(int k=0;k<4;k++) {
int x = xd[k],y=yd[k];
if(!ok[x][y]) {
parts.add(g[x][y]);
ok[x][y]=true;
}
}
Collections.sort(parts);
int split= parts.get(parts.size()/2);
for(int p:parts)ans+=Math.abs(p-split);
}
}
}
out.write((ans+"\n").getBytes());
}
out.close();
}
static class Pair<S extends Comparable<S>,T extends Comparable<T>> implements Comparable<Pair<S,T>>{
S first;
T second;
public Pair() {
this.first=null;
this.second =null;
}
public Pair(S first) {
this.first = first;
this.second =null;
}
public Pair(S first, T second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair<S,T> o) {
if(this.first.compareTo(o.first)==0) {
return this.second.compareTo(o.second);
}
return this.first.compareTo(o.first);
}
public String toString() {
return "{"+this.first.toString()+","+this.second.toString()+"}";
}
}
static int log(long N) {
// TODO Auto-generated method stub
return 63- Long.numberOfLeadingZeros(N);
}
static int lowerBound(int[] a, int x,int lf,int rg) {
int l = lf-1, r = rg+1;
while (r - l > 1) {
int c = (l + r) / 2;
if (a[c] < x) {
//if (a[c] > x) {
l = c;
} else {
r = c;
}
}
return r;
}
static int upperBound(int[] a, int x,int lf,int rg) {
int l = lf-1, r = rg+1;
while (r - l > 1) {
int c = (l + r) / 2;
if (a[c] <= x) {
l = c;
} else {
r = c;
}
}
return r;
}
static int lowerBound(long[] a, long x) {
int l = -1, r = a.length;
while (r - l > 1) {
int c = (l + r) / 2;
if (a[c] < x) {
l = c;
} else {
r = c;
}
}
return r;
}
static int upperBound(long[] a, long x) {
int l = -1, r = a.length;
while (r - l > 1) {
int c = (l + r) / 2;
if (a[c] <= x) {
l = c;
} else {
r = c;
}
}
return r;
}
static int lowerBound(double[] a, double x) {
int l = -1, r = a.length;
while (r - l > 1) {
int c = (l + r) / 2;
if (a[c] < x) {
l = c;
} else {
r = c;
}
}
return r;
}
static int upperBound(double[] a, double x) {
int l = -1, r = a.length;
while (r - l > 1) {
int c = (l + r) / 2;
if (a[c] <= x) {
l = c;
} else {
r = c;
}
}
return r;
}
static <T> int lowerBound(List<T> ls, T x) throws RuntimeException {
if (ls.size() == 0)
return -1;
if (ls.get(0) instanceof Integer) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) >= 0 ? 1 : -1);
} else if (ls.get(0) instanceof Long) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) >= 0 ? 1 : -1);
} else if (ls.get(0) instanceof Double) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) >= 0 ? 1 : -1);
} else {
System.err.println(
String.format("%s:Arey Maa chudi padi hai", Thread.currentThread().getStackTrace()[1].getMethodName()));
throw new RuntimeException();
}
}
static <T> int upperBound(List<T> ls, T x) throws RuntimeException {
if (ls.size() == 0)
return -1;
if (ls.get(0) instanceof Integer) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) > 0 ? 1 : -1);
} else if (ls.get(0) instanceof Long) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) > 0 ? 1 : -1);
} else if (ls.get(0) instanceof Double) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) > 0 ? 1 : -1);
} else {
System.err.println(
String.format("%s:Arey Maa chudi padi hai", Thread.currentThread().getStackTrace()[1].getMethodName()));
throw new RuntimeException();
}
}
static <T> int rupperBound(List<T> ls, T x) throws RuntimeException {
if (ls.size() == 0)
return -1;
if (ls.get(0) instanceof Integer) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) < 0 ? 1 : -1);
} else if (ls.get(0) instanceof Long) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) < 0 ? 1 : -1);
} else if (ls.get(0) instanceof Double) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) < 0 ? 1 : -1);
} else {
System.err.println(
String.format("%s:Arey maa chudi padi hai", Thread.currentThread().getStackTrace()[1].getMethodName()));
throw new RuntimeException();
}
}
static <T> int rlowerBound(List<T> ls, T x) {
if (ls.size() == 0)
return -1;
if (ls.get(0) instanceof Integer) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) <= 0 ? 1 : -1);
} else if (ls.get(0) instanceof Long) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) <= 0 ? 1 : -1);
} else if (ls.get(0) instanceof Double) {
return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) <= 0 ? 1 : -1);
} else {
System.err.println(
String.format("%s:Arey maa chudi padi hai", Thread.currentThread().getStackTrace()[1].getMethodName()));
throw new RuntimeException();
}
}
public static int[] merge(int[] one,int[] two){
int[] res = new int[one.length+two.length];
int i = 0;
int j = 0;
int k = 0;
while(i<one.length&&j<two.length){
if(one[i]<two[j]){
res[k] = one[i];
i++;
k++;
}
else{
res[k] = two[j];
j++;
k++;
}
}
if(i==one.length){
while(j<two.length){
res[k] = two[j];
j++;
k++;
}
}
else{
while(i<one.length){
res[k] = one[i];
k++;
i++;
}
}
return res;
}
public static int[] mergesort(int[] arr, int l, int r){
if(l==r){
int[] br = new int[1];
br[0] = arr[l];
return br;
}
int mid = (l+r)/2;
int[] fh = mergesort(arr,l,mid);
int[] sh = mergesort(arr,mid+1,r);
return merge(fh,sh);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 50ccc161514ecbc2e9ac140458a96fa5 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.io.*;
import java.util.*;
public class noob {
InputReader in;
final long mod=1000000007;
StringBuilder sb;
public static void main(String[] args) throws java.lang.Exception {
new noob().run();
}
void run() throws Exception {
in=new InputReader(System.in);
sb = new StringBuilder();
int t=i();
for(int i=1;i<=t;i++)
solve();
System.out.print(sb);
}
void solve() {
int i,j;
int n=i(), m=i();
long a[][]=new long[n][m];
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
a[i][j]=l();
}
if(n==1 && m==1) {
sb.append(0+"\n");
return;
}
long p=0,q=0;
long ans=0;
for(i=0;i<n/2;i++) {
for(j=0;j<m/2;j++) {
long ar[]=new long[4];
ar[0]=a[i][j];
ar[1]=a[n-i-1][j];
ar[2]=a[i][m-j-1];
ar[3]=a[n-i-1][m-j-1];
Arrays.sort(ar);
ans+=(long)Math.abs(ar[2]-ar[0]) + (long)Math.abs(ar[2]-ar[1]) + (long)Math.abs(ar[2]-ar[3]);
}
}
if(n%2==1) {
i=n/2;
for(j=0;j<m/2;j++) {
p=a[i][j];
q=a[i][m-j-1];
ans+=(long)Math.abs(p-q);
}
}
if(m%2==1) {
j=m/2;
for(i=0;i<n/2;i++) {
p=a[i][j];
q=a[n-i-1][j];
ans+=(long)Math.abs(p-q);
}
}
sb.append(ans+"\n");
}
String s(){return in.next();}
int i(){return in.nextInt();}
long l(){return in.nextLong();}
double d(){return in.nextDouble();}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public void skip(int x) {
while (x-->0)
read();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextString() {
return next();
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean hasNext() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value != -1;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 171a43d0d69289eac78192ab54f4374d | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public final class NiceMatrix{
private long med(long[] m){
assert m.length == 4;
Arrays.sort(m);
return (m[1] + m[2])/2;
}
private long median(long[][] m, int rb, int re, int cb, int ce){
{
long _median = (rb == re ?
(cb == ce ? m[rb][cb] : (m[rb][cb] + m[rb][ce]) / 2) :
(cb == ce ?
(m[rb][cb] + m[re][cb]) / 2 :
med(new long[] {m[rb][cb], m[rb][ce], m[re][cb], m[re][ce]})));
//absolute diffs' sum
return (rb == re ?
(cb == ce ? 0 : Math.abs(_median - m[rb][cb]) + Math.abs(_median - m[rb][ce])):
(cb == ce ?
(Math.abs(_median - m[rb][cb]) + Math.abs(_median - m[re][cb])) :
(Math.abs(_median - m[rb][cb]) + Math.abs(_median - m[rb][ce]) + Math.abs(_median - m[re][cb])
+ Math.abs(_median - m[re][ce]))));
}
}
public long niceMatrix(long[][] m){
if(m == null)
return 0;
int rb= 0, re = m.length - 1;
int cb= 0, ce = m[0].length - 1;
long mmoves = 0; //minimum moves: one move is +1/-1 a matrix element.
while(rb <= re){
while(cb <= ce){
mmoves += median(m, rb, re, cb, ce);
if(cb == ce)
break;
cb++; ce--;
}
if(rb == re)
break;
rb++; re--;
//reset cb and ce to beginning and end columns.
cb= 0; ce = m[0].length - 1;
}
return mmoves;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int r = sc.nextInt();
int c = sc.nextInt();
long[][] m = new long[r][c];
int i = 0, j = 0;
while(i < r){
j = 0;
while(j < c){
m[i][j] = sc.nextInt();
j++;
}
i++;
}
NiceMatrix nm = new NiceMatrix();
System.out.println(nm.niceMatrix(m));
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 410ab4e1b5d3d510b9e074e25cd522de | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
long mod1 = (long) 1e9 + 7;
int mod2 = 998244353;
public void solve() throws Exception {
int t = sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int m=sc.nextInt();
long arr[][]=new long[n][m];
boolean brr[][]=new boolean[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
arr[i][j]=sc.nextLong();
}
}
long count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(brr[i][j] || brr[i][m-j-1] || brr[n-i-1][j] || brr[n-i-1][m-j-1])
continue;
if(j==m-j-1 && i==n-i-1)
{
continue;
}
else if(i==n-1-i)
{
long first=arr[i][j];
long second=arr[i][m-j-1];
count+=Math.abs(second-first);
}
else if(j==m-j-1)
{
long first=arr[i][j];
long second=arr[n-i-1][j];
count+=Math.abs(second-first);
}
else
{
long first=arr[i][j];
long second=arr[i][m-j-1];
long third=arr[n-i-1][j];
long fourth=arr[n-i-1][m-1-j];
ArrayList<Long> a=new ArrayList<>();
a.add(first);
a.add(second);
a.add(third);
a.add(fourth);
Collections.sort(a);
long temp=a.get(1);
count+=Math.abs(first-temp)+Math.abs(second-temp)+Math.abs(third-temp)+Math.abs(fourth-temp);
}
brr[i][j]=true;
brr[i][m-j-1]=true;
brr[n-1-i][j]=true;
brr[n-1-i][m-j-1]=true;
//out.println(count);
}
}
out.println(count);
}
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long ncr(int n, int r, long p) {
if (r > n)
return 0l;
if (r > n - r)
r = n - r;
long C[] = new long[r + 1];
C[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r] % p;
}
public long power(long x, long y, long p) {
long res = 1;
// out.println(x+" "+y);
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static Throwable uncaught;
BufferedReader in;
FastScanner sc;
PrintWriter out;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable uncaught) {
Solution.uncaught = uncaught;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
if (Solution.uncaught != null) {
throw Solution.uncaught;
}
}
}
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 int[] readArray(int n) throws Exception {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | c29379ab54c3d3a82f0f0e76b0bb04b8 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static class Point{
int x, y, z, i;
Point(int nx, int ny){x = nx; y = ny; }
}
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int q = pint(in);
for(; q > 0; q--) {
StringTokenizer st = new StringTokenizer(in.readLine());
int n = pint(st), m = pint(st);
long[][] a = new long[n + 1][m + 1];
for(int i = 1; i <= n; i++) {
st = new StringTokenizer(in.readLine());
for(int j = 1; j <= m; j++) {
a[i][j] = pint(st);
}
}
long t = 0;
for(int i = 1; i <= (n + 1) / 2; i++) {
for(int j = 1; j <= (m + 1) / 2; j++) {
long w = a[i][j], x = a[n - i + 1][m - j + 1],
y = a[n - i + 1][j], z = a[i][m - j + 1];
if((i - 1) * 2 + 1 == n) {
if((j - 1) * 2 + 1 == m) {
t += 0;
}else {
t += Math.abs(w - z);
}
}else {
if((j - 1) * 2 + 1 == m) {
t += Math.abs(w - y);;
}else {
long d1 = Math.abs(w - w) + Math.abs(x - w) + Math.abs(y - w) + Math.abs(z - w);
long d2 = Math.abs(w - x) + Math.abs(x - x) + Math.abs(y - x) + Math.abs(z - x);
long d3 = Math.abs(w - y) + Math.abs(x - y) + Math.abs(y - y) + Math.abs(z - y);
long d4 = Math.abs(w - z) + Math.abs(x - z) + Math.abs(y - z) + Math.abs(z - z);
t += Math.min(Math.min(d1, d2), Math.min(d3, d4));
}
}
}
}
System.out.println(t);
}
}
static int pint(BufferedReader in) throws IOException {return Integer.parseInt(in.readLine());}
static int pint(StringTokenizer st) {return Integer.parseInt(st.nextToken());}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | b857d3883c3aa42a91f481ff2849377e | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
public class Solution{
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
int t = sc.nextInt();
while(t-->0){
int m = sc.nextInt(),n = sc.nextInt();
int mat[][] = new int[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j] = sc.nextInt();
long minops = 0;
for(int i=0;i<=(m/2-1);i++){
for(int j=0;j<=(n/2-1);j++){
int a = mat[i][j];
int b = mat[i][n-1-j];
int c = mat[m-1-i][j];
int d = mat[m-1-i][n-1-j];
ArrayList<Integer>arr = new ArrayList<Integer>();
arr.add(a);
arr.add(b);
arr.add(c);
arr.add(d);
Collections.sort(arr);
int mid = (int)Math.ceil(arr.size()/2.0)-1;
minops += (long)Math.abs(a-arr.get(mid))+Math.abs(b-arr.get(mid))+Math.abs(c-arr.get(mid))+Math.abs(d-arr.get(mid));
mat[i][j] = arr.get(mid);
mat[i][n-1-j] = arr.get(mid);
mat[m-1-i][j] = arr.get(mid);
mat[m-1-i][n-1-j] = arr.get(mid);
}
}
if(m%2!=0){
int i = m/2;
for(int j=0;j<=(n/2-1);j++){
minops+=Math.abs(mat[i][j]-mat[i][n-1-j]);
mat[i][j] = Math.min(mat[i][j],mat[i][n-1-j]);
mat[i][n-1-j] = Math.min(mat[i][j],mat[i][n-1-j]);
}
}
if(n%2!=0){
int i = n/2;
for(int j=0;j<=(m/2-1);j++){
minops+=Math.abs(mat[j][i]-mat[m-1-j][i]);
mat[j][i] = Math.min(mat[j][i],mat[m-1-j][i]);
mat[m-1-j][i] = Math.min(mat[j][i],mat[m-1-j][i]);
}
}
/*for(int i=0;i<m;i++){
for(int j=0;j<n;j++)
System.out.print(mat[i][j]+" ");
System.out.println();
} */
System.out.println(minops);
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | e7e4f07d1801cf20badaa64541b6ed57 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
public class Solution{
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
int t = sc.nextInt();
while(t-->0){
int m = sc.nextInt(),n = sc.nextInt();
int mat[][] = new int[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j] = sc.nextInt();
long minops = 0;
for(int i=0;i<=(m/2-1);i++){
for(int j=0;j<=(n/2-1);j++){
int a = mat[i][j];
int b = mat[i][n-1-j];
int c = mat[m-1-i][j];
int d = mat[m-1-i][n-1-j];
ArrayList<Integer>arr = new ArrayList<Integer>();
arr.add(a);
arr.add(b);
arr.add(c);
arr.add(d);
Collections.sort(arr);
int mid = (int)Math.ceil(arr.size()/2.0)-1;
minops += (long)(int)arr.get(mid)+arr.get(2)+arr.get(3)-((long)2*arr.get(mid)+arr.get(0));
mat[i][j] = arr.get(mid);
mat[i][n-1-j] = arr.get(mid);
mat[m-1-i][j] = arr.get(mid);
mat[m-1-i][n-1-j] = arr.get(mid);
}
}
if(m%2!=0){
int i = m/2;
for(int j=0;j<=(n/2-1);j++){
minops+=Math.abs(mat[i][j]-mat[i][n-1-j]);
mat[i][j] = Math.min(mat[i][j],mat[i][n-1-j]);
mat[i][n-1-j] = Math.min(mat[i][j],mat[i][n-1-j]);
}
}
if(n%2!=0){
int i = n/2;
for(int j=0;j<=(m/2-1);j++){
minops+=Math.abs(mat[j][i]-mat[m-1-j][i]);
mat[j][i] = Math.min(mat[j][i],mat[m-1-j][i]);
mat[m-1-j][i] = Math.min(mat[j][i],mat[m-1-j][i]);
}
}
/*for(int i=0;i<m;i++){
for(int j=0;j<n;j++)
System.out.print(mat[i][j]+" ");
System.out.println();
} */
System.out.println(minops);
}
}
} | Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 951652e735e99a98d413cdc2ca2cda79 | train_002.jsonl | 1601827500 | A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him! | 256 megabytes | import java.util.*;
public class Code {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
int arr[][]=new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
arr[i][j]=sc.nextInt();
}
}
long ans=0;
ans=rec(arr);
if(n%2==1)
{
ans+=palli(arr[(n/2)]);
}
if(m%2==1)
{
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=arr[i][m/2];
}
ans+=palli(a);
}
System.out.println(ans);
}
}
private static long palli(int[] arr) {
// TODO Auto-generated method stub
int i=0;
int j=arr.length-1;
long res=0;
while(i<j)
{
res+=Math.max(arr[j],arr[i])-Math.min(arr[j], arr[i]);
i++;
j--;
}
return res;
}
private static long rec(int[][] arr) {
// TODO Auto-generated method stub
int m=arr[0].length;
int n=arr.length;
int l1=0;
int l2=n-1;
int l3=0;
int l4=m-1;
long res=0;
while(l1<l2)
{
l3=0;
l4=m-1;
while(l3<l4)
{
int a[]=new int[4];
a[0]=arr[l1][l3];
a[1]=arr[l1][l4];
a[2]=arr[l2][l3];
a[3]=arr[l2][l4];
int avg=0;
for(int i=0;i<4;i++)
{
avg+=a[i];
}
avg/=4;
long temp1=0;
long temp2=0;
long temp3=0;
long temp4=0;
for(int i=0;i<4;i++)
{
temp1+=Math.abs(a[i]-a[0]);
temp2+=Math.abs(a[i]-a[1]);
temp3+=Math.abs(a[i]-a[2]);
temp4+=Math.abs(a[i]-a[3]);
}
res+=Math.min(temp4,Math.min(temp3,Math.min(temp1, temp2)));
l3++;
l4--;
}
l1++;
l2--;
}
return res;
}
}
| Java | ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"] | 1 second | ["8\n42"] | NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5 | Java 11 | standard input | [
"implementation",
"greedy",
"math"
] | 5aa709f292f266799f177b174c8bc14b | The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix. | 1,300 | For each test output the smallest number of operations required to make the matrix nice. | standard output | |
PASSED | 0de65f2deffb463bfc515c329264ec2c | train_002.jsonl | 1267117200 | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. | 64 megabytes | /**
* ******* Created on 17/9/19 9:29 PM*******
*/
import java.io.*;
import java.util.*;
public class ACon2 {
public static void main(String[] args) throws IOException {
final class Pair{
String a;
int b;
public Pair(String a , int b){
this.a =a;
this.b =b;
}
}
try (Input input = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
int n = input.nextInt();
Map<String, Integer>map = new HashMap<>();
Map<String, Integer> check = new HashMap<>();
int max = Integer.MIN_VALUE;
Pair[] inputs = new Pair[n];
for(int i=0;i<n;i++){
String s = input.next();
int score = input.nextInt();
inputs[i]= new Pair(s, score);
if(map.containsKey(s)){
int temp = map.get(s)+score;
map.put(s,temp);
}else{
map.put(s,score);
}
}
for(String key: map.keySet()){
if(map.get(key) > max){
max = map.get(key);
}
}
int i;
for(i=0;i<n;i++){
String s = inputs[i].a;
int score = inputs[i].b;
if(check.containsKey(s)){
int temp = check.get(s)+score;
check.put(s,temp);
}else{
check.put(s,score);
}
if(check.get(s)>=max && map.get(s)==max)break;
}
System.out.println(inputs[i].a );
}
}
interface Input extends Closeable {
String next() throws IOException;
default int nextInt() throws IOException {
return Integer.parseInt(next());
}
default long nextLong() throws IOException {
return Long.parseLong(next());
}
default double nextDouble() throws IOException {
return Double.parseDouble(next());
}
default int[] readIntArray() throws IOException {
return readIntArray(nextInt());
}
default int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextInt();
}
return array;
}
default long[] readLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextLong();
}
return array;
}
}
private static class StandardInput implements Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer stringTokenizer;
@Override
public void close() throws IOException {
reader.close();
}
@Override
public String next() throws IOException {
if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
}
}
| Java | ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"] | 1 second | ["andrew", "andrew"] | null | Java 8 | standard input | [
"implementation",
"hashing"
] | c9e9b82185481951911db3af72fd04e7 | The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. | 1,500 | Print the name of the winner. | standard output | |
PASSED | 3dc6976bdb36682181411711fd9ea8fc | train_002.jsonl | 1267117200 | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. | 64 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args)
{
int[] ar = new int[1010];
String[] st = new String[1010];
HashMap<String,Integer> map = new HashMap<String,Integer>();
HashMap<String,Integer> hash = new HashMap<String,Integer>();
int n;
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
int max = Integer.MIN_VALUE;
for(int i=0;i<n;i++)
{
st[i] = scan.next();
ar[i] = scan.nextInt();
map.put(st[i],map.getOrDefault(st[i], 0)+ar[i]);
}
for(int i=0;i<n;i++) max = Math.max(map.get(st[i]), max);
for(int i=0;i<n;i++)
{
hash.put(st[i], hash.getOrDefault(st[i],0)+ar[i]);
if(hash.get(st[i])>=max && map.get(st[i])>=max)
{
System.out.println(st[i]);
break;
}
}
}
} | Java | ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"] | 1 second | ["andrew", "andrew"] | null | Java 8 | standard input | [
"implementation",
"hashing"
] | c9e9b82185481951911db3af72fd04e7 | The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. | 1,500 | Print the name of the winner. | standard output | |
PASSED | cc22005dcff48007afd2ea5b5edc5f97 | train_002.jsonl | 1267117200 | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. | 64 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args)
{
HashMap<String,Integer> map = new HashMap<String,Integer>();
LinkedHashMap<Integer,String> hash = new LinkedHashMap<Integer,String>();
int use = Integer.MIN_VALUE;
int max = Integer.MIN_VALUE;
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i=0;i<n;i++)
{
String name = scan.next();
int score = scan.nextInt();
map.put(name, score + map.getOrDefault(name,0));
if(hash.get(map.get(name))==null) hash.put(map.get(name), name);
}
for(Map.Entry<String, Integer> en:map.entrySet()) max = Math.max(max, en.getValue());
//System.out.println(hash);
for(Map.Entry<Integer, String> en:hash.entrySet())
{
if(map.get(en.getValue())==max && en.getKey()>=max)
{
System.out.println(en.getValue());
break;
}
}
}
} | Java | ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"] | 1 second | ["andrew", "andrew"] | null | Java 8 | standard input | [
"implementation",
"hashing"
] | c9e9b82185481951911db3af72fd04e7 | The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. | 1,500 | Print the name of the winner. | standard output | |
PASSED | 6d0cea2d031211fa9ff017a981cf27e8 | train_002.jsonl | 1267117200 | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. | 64 megabytes |
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Winner {
public static class Player
{
public int score;
public ArrayList<ArrayList<Integer>> record;
public Player()
{
record = new ArrayList<ArrayList<Integer>>();
score = 0;
}
public void addScore(int s, int r)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(s);
temp.add(r);
record.add(temp);
score += s;
}
}
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
ArrayList<String> names = new ArrayList<String>();
Map<String, Player> players = new HashMap<String, Player>();
int n = console.nextInt();
for(int i=0; i<n; i++)
{
String tempName = console.next();
if(!players.containsKey(tempName))
{
players.put(tempName, new Player());
names.add(tempName);
}
players.get(tempName).addScore(console.nextInt(), i);
}
String topPlayer = names.get(0);
int topScore = players.get(topPlayer).score;
ArrayList<String> tiedPlayers = new ArrayList<String>();
for(int i=1; i<names.size(); i++)
{
if(players.get(names.get(i)).score > topScore)
{
topPlayer = names.get(i);
topScore = players.get(topPlayer).score;
}
}
for(int i=0; i<names.size(); i++)
{
if(players.get(names.get(i)).score == topScore)
{
tiedPlayers.add(names.get(i));
}
}
if(tiedPlayers.size() > 0)
{
int lowestRound = n;
for(int i=0; i<tiedPlayers.size(); i++)
{
int addScore = 0;
for(ArrayList<Integer> temp : players.get(tiedPlayers.get(i)).record)
{
addScore += temp.get(0);
if(addScore >= topScore && temp.get(1) < lowestRound)
{
topPlayer = tiedPlayers.get(i);
lowestRound = temp.get(1);
break;
}
}
}
}
System.out.println(topPlayer);
console.close();
}
} | Java | ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"] | 1 second | ["andrew", "andrew"] | null | Java 8 | standard input | [
"implementation",
"hashing"
] | c9e9b82185481951911db3af72fd04e7 | The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. | 1,500 | Print the name of the winner. | standard output | |
PASSED | ffd841e043919cb1639b200ca21f889a | train_002.jsonl | 1267117200 | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. | 64 megabytes |
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Winner {
public static class Player
{
public int score;
public ArrayList<ArrayList<Integer>> record;
public Player()
{
record = new ArrayList<ArrayList<Integer>>();
score = 0;
}
public void addScore(int s, int r)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(s);
temp.add(r);
record.add(temp);
score += s;
}
}
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
ArrayList<String> names = new ArrayList<String>();
Map<String, Player> players = new HashMap<String, Player>();
int n = console.nextInt();
for(int i=0; i<n; i++)
{
String tempName = console.next();
if(!players.containsKey(tempName))
{
players.put(tempName, new Player());
names.add(tempName);
}
players.get(tempName).addScore(console.nextInt(), i);
}
String topPlayer = names.get(0);
int topScore = players.get(topPlayer).score;
ArrayList<String> tiedPlayers = new ArrayList<String>();
for(int i=1; i<names.size(); i++)
{
if(players.get(names.get(i)).score > topScore)
{
topPlayer = names.get(i);
topScore = players.get(topPlayer).score;
}
}
for(int i=0; i<names.size(); i++)
{
if(players.get(names.get(i)).score == topScore)
{
tiedPlayers.add(names.get(i));
}
}
if(tiedPlayers.size() > 0)
{
int lowestRound = n;
for(int i=0; i<tiedPlayers.size(); i++)
{
int addScore = 0;
for(ArrayList<Integer> temp : players.get(tiedPlayers.get(i)).record)
{
addScore += temp.get(0);
if(addScore >= topScore && temp.get(1) < lowestRound)
{
topPlayer = tiedPlayers.get(i);
lowestRound = temp.get(1);
}
}
}
}
System.out.println(topPlayer);
console.close();
}
}
| Java | ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"] | 1 second | ["andrew", "andrew"] | null | Java 8 | standard input | [
"implementation",
"hashing"
] | c9e9b82185481951911db3af72fd04e7 | The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. | 1,500 | Print the name of the winner. | standard output | |
PASSED | 1c9e6c7d5d39deca38de95523b9ec7cb | train_002.jsonl | 1267117200 | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. | 64 megabytes |
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Winner {
public static class Player
{
public int score;
public ArrayList<ArrayList<Integer>> record;
public Player()
{
record = new ArrayList<ArrayList<Integer>>();
score = 0;
}
public void addScore(int s, int r)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(s);
temp.add(r);
record.add(temp);
score += s;
}
}
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
ArrayList<String> names = new ArrayList<String>();
Map<String, Player> players = new HashMap<String, Player>();
int n = console.nextInt();
for(int i=0; i<n; i++)
{
String tempName = console.next();
if(!players.containsKey(tempName))
{
players.put(tempName, new Player());
names.add(tempName);
}
players.get(tempName).addScore(console.nextInt(), i);
}
String topPlayer = names.get(0);
int topScore = players.get(topPlayer).score;
ArrayList<String> tiedPlayers = new ArrayList<String>();
for(int i=1; i<names.size(); i++)
{
if(players.get(names.get(i)).score > topScore)
{
topPlayer = names.get(i);
topScore = players.get(topPlayer).score;
}
}
for(int i=0; i<names.size(); i++)
{
if(players.get(names.get(i)).score == topScore)
{
tiedPlayers.add(names.get(i));
}
}
if(tiedPlayers.size() > 0)
{
int lowestRound = n;
for(int i=0; i<tiedPlayers.size(); i++)
{
int addScore = 0;
for(ArrayList<Integer> temp : players.get(tiedPlayers.get(i)).record)
{
addScore += temp.get(0);
if(addScore >= topScore && temp.get(1) < lowestRound)
{
topPlayer = tiedPlayers.get(i);
lowestRound = temp.get(1);
}
}
}
}
System.out.println(topPlayer);
console.close();
}
} | Java | ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"] | 1 second | ["andrew", "andrew"] | null | Java 8 | standard input | [
"implementation",
"hashing"
] | c9e9b82185481951911db3af72fd04e7 | The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. | 1,500 | Print the name of the winner. | standard output | |
PASSED | 39aac80c171abba6e1b4561235f2b8cb | train_002.jsonl | 1267117200 | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. | 64 megabytes | import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner scan=new Scanner(System.in); HashMap<String,Integer> map=new HashMap<>();
int n=scan.nextInt();
int max=Integer.MIN_VALUE;
String ans="";
String [] name1=new String [n];
int[] score1=new int[n];
for(int i=0;i<n;i++) {
String name=scan.next();
int score=scan.nextInt();
name1[i]=name;
score1[i]=score; if(map.containsKey(name)) { map.put(name,map.get(name)+score); } else map.put(name,score);
}
// int max=Integer.MAX_VALUE;
// String ans="";
for(String s: map.keySet()){
if(map.get(s)>max)
max=map.get(s);
}
HashMap<String,Integer> map1=new HashMap<>();
for(int i=0;i<n;i++){
if(map1.containsKey(name1[i])){
map1.put(name1[i],map1.get(name1[i])+score1[i]);
}
else
map1.put(name1[i], score1[i]);
if(map1.get(name1[i])>=max&&map.get(name1[i])==max)
{ ans=name1[i];break;}
}
System.out.println(ans);
}
}
| Java | ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"] | 1 second | ["andrew", "andrew"] | null | Java 8 | standard input | [
"implementation",
"hashing"
] | c9e9b82185481951911db3af72fd04e7 | The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. | 1,500 | Print the name of the winner. | standard output | |
PASSED | c7d2c178465a0d65ee6dec223985706c | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] trees = new int[n];
for (int i = 0; i < n; i++) {
trees[i] = scan.nextInt();
}
int currentHeight = 0;
long ans = 0;
for (int i = 0; i < n - 1; i++) {
ans += trees[i] - currentHeight + 1;
currentHeight = trees[i];
if (currentHeight > trees[i + 1]) {
ans += currentHeight - trees[i + 1];
currentHeight = trees[i + 1];
}
}
ans += trees[n - 1] - currentHeight + n;
System.out.println(ans);
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 5d09d40694945777b1a5d64f89a0b08c | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = s.nextInt();
int cur_h = 0;
int answer = 0;
for (int i = 0; i < n; i++) {
answer += arr[i] - cur_h;
answer++;
if (i < n - 1) {
if (arr[i] > arr[i + 1]) {
answer += arr[i] - arr[i + 1];
answer++;
cur_h = arr[i + 1];
} else {
answer++;
cur_h = arr[i];
}
}
}
System.out.println(answer);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | b8b3670c2db17bb60d3e12d332f54193 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sb=new StringTokenizer(br.readLine());
int n= Integer.parseInt(sb.nextToken());
int[] h=new int[n];
for(int i=0;i<n;i++) {
sb=new StringTokenizer(br.readLine());
h[i]=Integer.parseInt(sb.nextToken());
}
int result=0;
int currentHeight=0;
for(int i=0;i<n-1;i++) {
if(h[i]<=h[i+1]) {
result+=(h[i]-currentHeight);
result+=1;
result+=1;
currentHeight=h[i];
} else {
result+=(h[i]-currentHeight);
result+=1;
result+=(h[i]-h[i+1]);
result+=1;
currentHeight=h[i+1];
}
}
result+=(h[n-1]-currentHeight+1);
System.out.println(result);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | fb33e5b6cb4385ada5a9863a7bdfa2a0 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
/**
* Write a description of class b here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class b
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int total = 0;
int curTree = input.nextInt();
int prev;
total += 2*num-1;
total += curTree;
for(int i = 1; i < num; i++)
{
prev = curTree;
curTree = input.nextInt();
total += Math.abs(curTree - prev);
}
System.out.println(total);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | a25604f7df4e3889a445d44a63f04d9f | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine().trim());
int[] heights = new int[N];
for(int i = 0; i < N; i++){
int height = Integer.parseInt(br.readLine().trim());
heights[i] = height;
}
long time = 0;
int currHeight = 0;
for(int i = 0; i < N; i++){
if(i > 0){
time += 1;
}
time += 1;
time += Math.abs(heights[i] - currHeight);
currHeight = heights[i];
}
System.out.println(time);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 0f823ab5b52c703e006787e27750c13d | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.InputStreamReader;
import java.util.Scanner;
public class Round_162B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner r = new Scanner(new InputStreamReader(System.in));
int n = r.nextInt();
int[] h = new int[n];
for (int i = 0; i < n; i++) {
h[i] = r.nextInt();
}
int count = 0;
for (int i = 0; i < h.length; i++) {
if (i == 0) {
count += h[i] + 1;
} else if (h[i - 1] < h[i]) {
count += h[i] - h[i - 1] + 2;
} else {
count += h[i - 1] - h[i] + 2;
}
}
System.out.println(count);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 8f4549c832a7cff1e7cd55b56bcbd895 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
public class cf265b {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] v = new int[n];
for(int i=0; i<n; i++)
v[i] = in.nextInt();
int ans = n+n-1+v[0];
for(int i=1; i<n; i++)
ans += Math.abs(v[i]-v[i-1]);
System.out.println(ans);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 8fe0ebdbf574bfbfe4845b46c6109323 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = t_int(br);
int[] t = new int[n];
for (int i = 0; i < n; i++) {
t[i] = t_int(br);
}
int ops = t[0] + 1;
for (int i = 1; i < n; i++) {
ops++;
ops += Math.abs(t[i] - t[i - 1]);
ops += 1;
}
System.out.println(ops);
br.close();
}
public static int t_int(BufferedReader br) throws Exception {
return Integer.parseInt(br.readLine());
}
public static StringTokenizer t_st(BufferedReader br) throws Exception {
return new StringTokenizer(br.readLine());
}
public static int[] t_int_a(BufferedReader br, int n) throws Exception {
StringTokenizer st = t_st(br);
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
return a;
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 72aeee56371a206609a673b3b04ee199 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class Solution265B{
public Solution265B() {
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int height = 0;
int pos = 0;
int total = 0;
for(int i = 0 ; i < n ; i++) {
if(pos != 0)
total += 1;
height = sc.nextInt();
total += Math.abs(pos - height) + 1;
pos = height;
}
System.out.println(total);
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | a62232b8ca4924b508d3ed287d2155b8 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes |
import java.util.Scanner;
public class P265B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] h = new int[n];
for (int i = 0; i < n; i++) {
h[i] = scanner.nextInt();
}
long cost = 2 * n - 1;
int ch = 0;
for (int i = 0; i < n - 1; i++) {
cost += h[i] - ch;
ch = h[i];
int down = Math.max(0, h[i] - h[i + 1]);
ch = h[i] - down;
cost += down;
}
cost += h[n - 1] - ch;
System.out.println(cost);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 8cfefcb5479ececaa7a4be234488aa08 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
*/
public class Main {
private static BufferedReader br;
private static StringTokenizer st;
private static PrintWriter pw;
static int[] dx = new int[]{-1,0,0,0,1};
static int[] dy = new int[]{0,-1,0,1,0};
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int qq = 1;
//int qq = Integer.MAX_VALUE;
//int qq = readInt();
for(int casenum = 1; casenum <= qq; casenum++) {
int n = readInt();
long ret = 0;
int[] list = new int[n];
for(int i = 0; i < n; i++) {
list[i] = readInt();
}
ret += list[0];
for(int i = 1; i < n; i++) {
ret += Math.abs(list[i-1] - list[i]);
}
pw.println(ret + 2*n-1);
}
pw.close();
}
public static boolean good(int curr) {
String ret = String.valueOf(curr);
Set<Character> set = new HashSet<Character>();
for(int i = 0; i < ret.length(); i++) {
set.add(ret.charAt(i));
}
return set.size() == ret.length();
}
private static long readLong() throws IOException {
return Long.parseLong(nextToken());
}
private static double readDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private static int readInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) {
if(!br.ready()) {
pw.close();
System.exit(0);
}
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 25c61fea854bad05f32de8533d409180 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class taskA {
Scanner in;
PrintWriter out;
void solve() {
int n = in.nextInt();
int time = 0;
int pred = in.nextInt();
time += pred;
time++;
for (int i = 1; i < n; i++) {
int sled = in.nextInt();
if(pred <= sled){
time += 2 + (sled - pred);
}else{
time +=pred - sled + 2;
}
pred = sled;
}
out.println(time);
}
void run() {
in = new Scanner(System.in);
out = new PrintWriter(System.out);
try {
solve();
} finally {
out.close();
}
}
public static void main(String[] args) {
new taskA().run();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | f86f46515c453ec5b906342b10dbe34a | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes |
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class b {
Scanner cin = new Scanner(System.in);
int a[] = new int [111111];
void run(){
int n;
while(cin.hasNext()){
n = cin.nextInt();
int i;
for(i=0;i<n;i++){
a[i] = cin.nextInt();
}
long ans = a[0] + 1;
for(i=1;i<n;i++){
if(a[i-1] > a[i])
ans += a[i-1]-a[i]+2;
else
ans += a[i]-a[i-1]+2;
}
System.out.println(ans);
}
}
public static void main(String[] args) {
new b().run();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 8f2ac44e365a3b206d5d92fdf455f084 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
import java.io.*;
public class second162 {
public static void main(String[] args)throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
//Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(rd.readLine());
StringTokenizer tk;
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
tk = new StringTokenizer(rd.readLine());
arr[i] = Integer.parseInt(tk.nextToken());
}
int sum = 2*n - 1;
sum += arr[0];
for (int i = 1; i < arr.length; i++) {
sum += Math.abs(arr[i] - arr[i - 1]);
}
System.out.println(sum);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | fde037e428332e1907d8494c0756fb90 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B {
static StringTokenizer st;
static BufferedReader in;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt();
int[]h = new int[n+1];
for (int i = 1; i <= n; i++) {
h[i] = nextInt();
}
long ans = n;
int cur_h = h[1];
ans += h[1];
for (int i = 2; i <= n; i++) {
ans++;
if (h[i] >= cur_h) {
ans += h[i]-cur_h;
}
else {
ans += cur_h-h[i];
}
cur_h = h[i];
}
System.out.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(in.readLine());
}
return st.nextToken();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 72c2ad54abe64ea19ef170ebf73a46e9 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class New {
public static void main(String[] args) {
Scanner bahy = new Scanner(System.in);
int num = bahy.nextInt();
int[] hight = new int [num];
for(int n=0;n<num;n++)
{
hight[n]=bahy.nextInt();
}
int sum= hight[0]+1;
for(int y=1;y<num;y++)
{
sum+=Math.abs(hight[y-1]-hight[y])+1+1;
}
System.out.println(sum);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 3d29a8cbfafc6cbff163713adde5ddf0 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class D2B {
public void solve(){
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int ans = 0;
int current = 0;
for(int i = 0; i < N; i++){
int height = sc.nextInt();
ans += Math.abs(current-height);
current = height;
}
System.out.println(ans+N*2-1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new D2B().solve();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | d4a2cb31771da2af94e19f5e4925f90a | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
import java.math.*;
public class solve{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
Integer n = scanner.nextInt();
Integer[] mas = new Integer[n];
mas[0] = scanner.nextInt();
Long seconds = new Long(0);
for(int i = 1; i < n; i++){
mas[i] = scanner.nextInt();
seconds += Math.abs(mas[i] - mas[i-1]);
}
//eat
seconds += n;
//jump
seconds += n - 1;
//first
seconds += mas[0];
System.out.print(seconds);
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 673172bffdf837a2c65bc14b481b91e9 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes |
import java.io.BufferedInputStream;
import java.util.*;
import static java.lang.Math.*;
public class C264B {
public void solve() throws Exception {
int n = nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
int res = n + n - 1;
int akt = 0;
for (int i = 0; i < arr.length - 1; i++) {
res += arr[i] - akt;
akt = arr[i];
if (arr[i + 1] < arr[i]) {
res += arr[i] - arr[i + 1];
akt = arr[i + 1];
}
}
res += arr[n - 1] - akt;
println(res);
}
// ------------------------------------------------------
void debug(Object... os) {
System.err.println(Arrays.deepToString(os));
}
void print(Object... os) {
if (os != null && os.length > 0)
System.out.print(os[0].toString());
for (int i = 1; i < os.length; ++i)
System.out.print(" " + os[i].toString());
}
void println(Object... os) {
print(os);
System.out.println();
}
BufferedInputStream bis = new BufferedInputStream(System.in);
String nextWord() throws Exception {
char c = (char) bis.read();
while (c <= ' ')
c = (char) bis.read();
StringBuilder sb = new StringBuilder();
while (c > ' ') {
sb.append(c);
c = (char) bis.read();
}
return new String(sb);
}
String nextLine() throws Exception {
char c = (char) bis.read();
while (c <= ' ')
c = (char) bis.read();
StringBuilder sb = new StringBuilder();
while (c != '\n' && c != '\r') {
sb.append(c);
c = (char) bis.read();
}
return new String(sb);
}
int nextInt() throws Exception {
return Integer.parseInt(nextWord());
}
long nextLong() throws Exception {
return Long.parseLong(nextWord());
}
public static void main(String[] args) throws Exception {
new C264B().solve();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 38777ce1d5f9e0ca963048bdef0a1a19 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | /**
* Created with IntelliJ IDEA.
* User: yuantian
* Date: 3/8/13
* Time: 1:04 AM
* To change this template use File | Settings | File Templates.
*/
import java.util.*;
public class RoadsideTreesSimplifiedEdition {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int total = -1;
int cur = 0;
for(int i = 0; i < n; i++) {
int h = in.nextInt();
total += 2 + Math.abs(h-cur);
cur = h;
}
System.out.println(total);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 9ec9bbee757ffe8b7c9c29b0b0be7773 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes |
import java.util.Scanner;
public class RoadsideTrees {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numberOfTrees = sc.nextInt();
int[] trees = new int[numberOfTrees];
for (int i = 0; i < trees.length; i++) {
trees[i] = sc.nextInt();
}
int sec = numberOfTrees;
int prev = 0;
for (int i = 0; i < trees.length; i++) {
sec += Math.abs(prev - trees[i]) + 1;
prev = trees[i];
}
System.out.println(sec-1);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 2d537c158321e1c31e228aab15e0f77a | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
int h = 0;
int t = 0;
for (int i = 0; i < n; i++) {
int hi = Integer.parseInt(br.readLine());
if (h > hi) { t += h - hi; h = hi; }
t++;
if (h < hi) { t += hi - h; h = hi; }
t++;
}
pw.println(t-1);
pw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 179d691abe3acfe13626541cbdf0851e | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.*;
public class B {
public static void main (String[]ar)throws Exception{
BufferedReader read = new BufferedReader (new InputStreamReader(System.in));
int trees = Integer.parseInt(read.readLine());
int[] t = new int[trees];
for(int i=0;i<trees;i++)
t[i] = Integer.parseInt(read.readLine());
int sec = 0,pos = 0;
boolean jump = false;
for(int i=0;i<trees;i++){
if(!jump){
sec += t[i];
}else{
sec += t[i] - pos + 1;
}
if(i < trees - 1 && t[i+1] < t[i]){
sec += t[i] - t[i+1];
jump = true;
pos = t[i+1];
}else{
pos = t[i];
jump = true;
}
sec++;
}
System.out.println(sec);
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | e7b8c9afbd64fe9f9145e7129ccffc0b | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int arr[]=new int[n];
int time=0;
int h=0;
for (int i = 0; i < arr.length; i++) {
arr[i]=scan.nextInt();
if(i==0){
time+=1+arr[0];
h=arr[0];
}else{
time++;
if(arr[i]<h){
time+=(arr[i-1]-arr[i]);
h-=(arr[i-1]-arr[i]);
time++;
}else{
time+=(arr[i]-arr[i-1]);
time++;
h+=arr[i]-arr[i-1];
}
}
}
System.out.println(time);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 0eb1642b01db896ec9a0720ab130cc65 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
public class nut {
public static void main(String[] args) {
Scanner k=new Scanner(System.in);
int tree=k.nextInt();
int[] array=new int[tree];
int second=0;
int before=0;
for (int i = 0; i < tree; i++) {
array[i]=k.nextInt();
if (i==0) {
second+=Math.abs(before-array[i])+1;
}else{
second+=Math.abs(before-array[i])+2;
}
before=array[i];
}
System.out.println(second);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | ac4258c12aa016f0f50af1faf4ad642b | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
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();
int[] hs = IOUtils.readIntArray(in, n);
int h = hs[0];
long res = h + 1;
for (int i = 0; i < n - 1; ++i) {
int nh = hs[i + 1];
if (nh > h) {
res += 1 + nh - h + 1;
} else if (nh < h) {
res += 1 + h - nh + 1;
} else {
res += 1 + 1;
}
h = nh;
}
out.println(res);
}
}
class IOUtils {
public static int[] readIntArray(Scanner in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.nextInt();
return array;
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 0a0549eda6361b580038525885da0f30 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.*;
public class CF162B{
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String line = stdin.readLine();
int n = Integer.parseInt(line);
int[] h = new int[n];
for(int i=0; i<n; i++){
line = stdin.readLine();
h[i] = Integer.parseInt(line);
}
int cnt = 0;
int curH = 0;
for(int i=0; i<n-1; i++){
cnt += (h[i] - curH);
cnt++;
if(h[i] > h[i+1]){
cnt += (h[i] - h[i+1]);
curH = h[i+1];
}else{
curH = h[i];
}
cnt++;
}
cnt += (h[n-1] - curH);
cnt++;
System.out.println(cnt);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 80cca67cdc13177e1db3e841add60044 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int h[] = new int[n];
for (int i = 0; i < n; i++)
h[i] = in.nextInt();
int steps = 0;
steps = steps + h[0] + 1; // climb + eat
int curr = h[0];
for (int i = 1; i < n; i++) {
if (h[i] >= h[i - 1]) {
steps = steps + 1; // jump
int remain = h[i] - curr;
steps = steps + remain; // climb
curr = h[i];
steps = steps + 1; // eat
} else {
int remain = curr - h[i];
steps = steps + remain; // descend
steps = steps + 1; // jump
curr = h[i];
steps = steps + 1; // eat
}
}
System.out.println(steps);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 49711e7ba6a10c9a45dca47e7d624283 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.ArrayList;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
import java.util.PriorityQueue;
import java.util.HashMap;
import java.util.Stack;
public class div2
{
public static void main(String []args)throws IOException{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter outt = new OutputWriter(outputStream);
StringBuilder out=new StringBuilder();
int n=in.readInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{ a[i]=in.readInt();
}
long tot=0;tot=a[0]+1;
for(int i=1;i<n;i++){
if(a[i-1]<=a[i]){
tot=tot+1+(a[i]-a[i-1])+1;
}
else{
tot=tot+a[i-1]-a[i]+1+1;
}
}
outt.printLine(tot);
outt.close();
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 24a73b2ee110d24a25a7bc0dc41dc982 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | /*
PROG: b
LANG: JAVA
*/
import java.util.*;
import java.io.*;
public class b {
private void solve() throws Exception {
//BufferedReader br = new BufferedReader(new FileReader("b.in"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] a = new int[n];
for(int i = 0; i < n; ++i)
a[i] = Integer.parseInt(br.readLine());
int r = a[0] + 1, pe = a[0];
for(int i = 1; i < n; ++i) {
r += Math.abs(a[i] - pe) + 2;
pe = a[i];
}
System.out.println(r);
}
public static void main(String[] args) throws Exception {
new b().solve();
}
static void debug(Object...o) {
System.err.println(Arrays.deepToString(o));
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | eb0a86abcb5255dfcef0218ca50a8d94 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class CF199A {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int pos = 0;
long total = N+N-1;
for(int a=0;a<N;a++){
int cur = sc.nextInt();
total+=Math.abs(cur-pos);
pos=cur;
}
System.out.println(total);
}
private static long LCM(long a, long b) {
return a*b /gcd(a,b);
}
private static long gcd(long a, long b) {
if(b==0)return a;
return gcd(b,a%b);
}
private static long[][] Pow(long[][] array, int n) {
if(n==1)return array;
int cur = n/2;
long[][] temp = Pow(array,cur);
long[][] thing = Mul(temp,temp);
if((n&1)==1) thing = Mul(thing,array);
return thing;
}
private static long[][] Mul(long[][] A, long[][] B) {
long[][] res = new long[4][4];
for(int i =0 ;i< 4;i++){
for(int j= 0; j<4;j++){
for(int k=0;k<4;k++){
res[i][j] += A[i][k]*B[k][j];
res[i][j]%=1000000007;
}
}
}
return res;
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 855338f9232e2fa8fe6c113b4c49fbe1 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import static java.lang.Math.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class one implements Runnable {
public void run() {
int n = nextInt();
int[] h = new int[n];
for (int i = 0; i < n; i++) {
h[i] = nextInt();
}
long ans = h[0] + 1;
for (int i = 1; i < n; i++) {
ans += abs(h[i] - h[i - 1]) + 1 + 1;
}
out.print(ans);
// --------------------------------------------------------------------------------------------
out.close();
System.exit(0);
}
private static boolean fileIOMode = false;
private static String problemName = "yyy";
private static BufferedReader in;
private static PrintWriter out;
private static StringTokenizer tokenizer;
public static void main(String[] args) throws Exception {
if (fileIOMode) {
in = new BufferedReader(new FileReader(problemName + ".in"));
out = new PrintWriter(problemName + ".out");
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
tokenizer = new StringTokenizer("");
new Thread(new one()).start();
}
private static String nextLine() {
try {
return in.readLine();
} catch (IOException e) {
return null;
}
}
private static String nextToken() {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
private static int nextInt() {
return Integer.parseInt(nextToken());
}
private static long nextLong() {
return Long.parseLong(nextToken());
}
private static double nextDouble() {
return Double.parseDouble(nextToken());
}
private static BigInteger nextBigInteger() {
return new BigInteger(nextToken());
}
private static void print(Object o) {
if (fileIOMode) {
System.out.print(o);
}
out.print(o);
}
private static void println(Object o) {
if (fileIOMode) {
System.out.println(o);
}
out.println(o);
}
private static void printf(String s, Object... o) {
if (fileIOMode) {
System.out.printf(s, o);
}
out.printf(s, o);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | e6766255b0220e5e633c8f3f0924b597 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by IntelliJ IDEA.
* User: prasoon.m
* Date: 1/20/13
* Time: 7:05 PM
* To change this template use File | Settings | File Templates.
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
String line = reader.readLine();
int noi = Integer.parseInt( line );
int prev = 0, curr;
long total = 0;
for( int i=0; i< noi; i++ ) {
line = reader.readLine();
curr = Integer.parseInt( line );
if( i == 0 ) {
total = curr+1;
}
else {
total++;
total += Math.abs( prev - curr );
total++;
}
prev = curr;
}
System.out.println(total);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 9a2d738d95191ea1cf6024e66e26ee37 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public final class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int num=in.nextInt();
int[] arr= new int[num];
for(int i=0;i<num;i++){
arr[i]=in.nextInt();
}
int ans=arr[0]+1;
int j=1;
//for(int j=1;j<=num-1;j++){//
if(num>1){
int diff=arr[1]-arr[0];
do{
if(diff>=0)
ans=ans+diff+2;
else
ans=ans+Math.abs(diff)+2;
//System.out.println(ans);
if(j<num-1)
diff=arr[j+1]-arr[j];
j++;
}while(j!=num);}
System.out.println(ans);
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 83b858db78bdc76bd06c169d1b0ab892 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
public class Main{
void solve(){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] h = new int[n];
for(int i=0; i<n; i++) h[i] = sc.nextInt();
int height = h[0];
int cnt = 1+h[0];
for(int i=1; i<n; i++){
if(h[i]!=height){
cnt += (2+Math.abs(height-h[i]));
height = h[i];
}else{
cnt += 2;
}
}
System.out.println(cnt);
}
public static void main(String[] args){
new Main().solve();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | dddebb38b3f5265357efeb26b3b416c2 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Ziklon
*/
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();
int A[] = new int[n];
for (int i = 0; i < n; i++) A[i] = in.nextInt();
long ans = A[0] + 1;
int curH = A[0];
for (int i = 0; i + 1 < n; i++) ans += Math.abs(A[i] - A[i + 1]) + 2;
out.println(ans);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | eee49b891c7731244bdb8c8a0e29ac48 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class TreesAlongTheRoad {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int prev = sc.nextInt();
int sum = prev + 1;
for (int i = 1; i < n; i++) {
int cur = sc.nextInt();
sum += Math.abs(cur - prev) + 2;
prev = cur;
}
System.out.println(sum);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | ff21545d9a5f2b6ab6a62cfcabf7024c | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class nuts
{
public static void main(String[] args) throws IOException
{
BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(cin.readLine());
int f=n;
int ar[]=new int[n];
int i=0;
while(n-->0)
{
ar[i]=Integer.parseInt(cin.readLine());
i++;
}
int sum=ar[0]+1;
for(int j=1;j<f;j++)
{
if(ar[j]<ar[j-1])
{
int sure=ar[j-1]-ar[j];
sum=sum+sure+2;
}
else if(ar[j]==ar[j-1])
{
sum=sum+2;
}
else
{
int sure=ar[j]-ar[j-1];
sum=sum+sure+2;
}
}
System.out.println(sum);
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 4818cd62e5466174977dff06f9ea1fc2 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int currentHeight = 0;
int time = 0;
for(int i = 0; i < n; i++) {
int height = scan.nextInt();
time += Math.abs(height - currentHeight); // забраться на вершину
time++; // съесть орех
time++; // перепрыгнуть дальше
currentHeight = height;
}
System.out.print(time - 1); // -1 потому что первый раз белка не прыгала
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 759239805c002448d226218e50a8a2c4 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes |
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class B162 {
public void solve() throws IOException {
Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i = 0, j = 0;
int n = sc.nextInt();
int a[] = new int[n];
for (i = 0; i < n; ++i) {
a[i] = sc.nextInt();
}
int sum = a[0] + 1 + 1;//h+eat+jump
if(n==1){
sum--;
}
for (i = 1; i < n; ++i) {
if (i == n - 1) {
if (a[i] < a[i - 1]) {
sum += 1+a[i-1]-a[i];
} else {
sum += (1 + a[i] - a[i - 1]);
}
} else {
if (a[i] <= a[i - 1]) {
sum += 2+a[i-1]-a[i];
} else {
sum += (2 + a[i] - a[i - 1]);
}
}
}
System.out.println(sum);
}
public static void main(String[] args) throws IOException {
new B162().solve();
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 76a2b2ab7e6214f7ddf918bec69a3d09 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
public class b2 {
public static void main(String[] args) throws IOException
{
input.init(System.in);
int n = input.nextInt();
int[] data = new int[n];
for(int i =0; i<n; i++) data[i] = input.nextInt();
long res = data[0];
for(int i = 1; i<n; i++) res += Math.abs(data[i]-data[i-1]);
res += 2*n-1;
System.out.println(res);
}
public static class input {
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 | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 40ae82551f6680dbd00218f2c935d941 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.*;
/**
*
* @author DHA
*/
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int n,h = 0;
BufferedReader r = new BufferedReader (new InputStreamReader (System.in));
n=Integer.parseInt(r.readLine());
int[] he=new int[n];
for(int i=0;i< n;i++)
he[i]=Integer.parseInt(r.readLine());
int t = 0;
for(int i=0;i< n;i++)
{
if(he[i]>=h && i!=0)
{
t=t+he[i]-h;
t=t+1;
}
else
{
if(i!=0)
{t=t+he[i-1]-he[i];
t=t+1;
}
else
t=he[i];
}
t=t+1;
h=he[i];
}
System.out.println(t);
// s= r.readLine().toCharArray();
// t= r.readLine().toCharArray();
// int si=0;
//
// for(int i=0;i<t.length;i++)
// {
// if(si==s.length)
// break;
// if(t[i]==s[si])
// si++;
// }
//
// if(si==s.length)
// System.out.println(si);
// else
// System.out.println(si+1);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 2aead2ab6bc838db4e0fb2110bbe1c8a | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.BigInteger;
public class Main{
//for(Map.Entry<Object, Object> e: map.entrySet()){ ...
public static void main(String[] args){
try{
//-*-*- tp start -*-*-
final int INF = Integer.MAX_VALUE, MINF = Integer.MIN_VALUE;
Scanner s = new Scanner(System.in);
//-*-*- tp end -*-*-
//write a program...
int n = s.nextInt();
int[] trees = new int[n];
for(int i = 0; i < n; i++){
trees[i] = s.nextInt();
}
int ans = 0, curh = 0;
for(int i = 0; i < n-1; i++){
ans += trees[i]-curh;
ans++;
curh = trees[i];
if(trees[i+1]<trees[i]){
ans += trees[i]-trees[i+1];
curh = trees[i+1];
}
ans++;
}
ans += trees[n-1]-curh+1;
debugl(ans);
//-*-*- tp start -*-*-
}catch(Exception e){
e.printStackTrace();
}
}
//-*-*- tp end -*-*-
//functions
private static String rep(String x, int n){
String str = "";
for(int i = 0; i < n; i++){
str += x;
}
return str;
}
//-*-*- tp start -*-*-
private static void debugl(Object obj){
System.out.println(obj);
}
private static void debug(Object obj){
System.out.print(obj);
}
private static String toStrmd(Object[] obj){
return Arrays.deepToString(obj);
}
}
//-*-*- tp end -*-*- | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | f2828400b800256080f2d3f225d1feaa | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class JavaApplication16{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n+2];
for(int k=1;k<=n;k++){
a[k]=sc.nextInt();
}
int q=0;
for(int k=1;k<n;k++){
if(k==1){
q=a[k]+1;
if(a[k]>a[k+1]){
q=q+a[k]-a[k+1]+2;
}
if(a[k]<=a[k+1]){
q=q+a[k+1]-a[k]+2;
}
}
else if(k<n&&k!=1){
if(a[k]>a[k+1]){
q=q+a[k]-a[k+1]+2;
}
if(a[k]<=a[k+1]){
q=q+a[k+1]-a[k]+2;
}
}}
if(n==1){
q=q+a[1]+1;
}
System.out.println(q);
}} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | cf27ed6de5b7cc6b74723eab80b72ef7 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class B implements Runnable {
private void solve() throws IOException {
int n = nextInt();
long last = nextInt();
long res = last + 1;
long cur = last;
for (int i = 1; i < n; i++) {
long h = nextInt();
if (last > h) {
res += (last - h);
res++;
cur = h;
} else {
res++;
}
res += (h - cur);
res++;
cur = h;
last = h;
}
pl(res);
}
public static void main(String[] args) {
new B().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new BufferedReader(
new InputStreamReader(System.in)));
writer = new PrintWriter(System.out);
tokenizer = null;
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
BigInteger nextBigInteger() throws IOException {
return new BigInteger(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
void p(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.flush();
writer.print(objects[i]);
writer.flush();
}
}
void pl(Object... objects) {
p(objects);
writer.flush();
writer.println();
writer.flush();
}
int cc;
void pf() {
writer.printf("Case #%d: ", ++cc);
writer.flush();
}
} | Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 18512edbad884462d306c779f041fa9c | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
public class d2_162_B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
// int a[]=new int[n];
// int b[]=new int[n];
// for(int i=0;i<n;i++)
// a[i]=sc.nextInt();
// int cost=a[0]+1;
// for(int i=0 ;i<n-1;i++)
// {
// if(a[i]>a[i+1])
// {
// b[i]=a[i]-a[i+1];
// }
// else if(a[i]<a[i+1])
// {
// b[i]=a[i];
// b[i+1]=a[i+1]-a[i];
// }
// else
// b[i]=a[i];
// }
// if(a[0]>a[1])
// {
// for(int i=0;i<n;i++)
// {
// cost+=b[i]+2;
// }
// }
// else
// {
// cost+=1;
// for(int i=1;i<n;i++)
// {
// cost+=b[i]+2;
// }
// }
// System.out.println(cost);
int curht=0,treeht,steps=0;
for(int i=0;i<n;i++)
{
treeht=sc.nextInt();
if(curht<treeht)
{
steps+=treeht-curht+1;
steps++;
}
else
{
steps+=curht-treeht+1;
steps++;
}
curht=treeht;
}
System.out.println(steps-1);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | eba723e1c6146dc6d92728dec3395c5f | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
public class RoadsideTrees {
public static void main(String[] args) {
int n;
int count = 0;
int tmp;
int curHeight = 0;
Scanner input = new Scanner(System.in);
n = Integer.parseInt(input.nextLine());
for (int i = 0; i < n; i++) {
tmp = Integer.parseInt(input.nextLine());
if(i == 0){
count = tmp + 1;
curHeight = tmp;
}else{
if(curHeight <= tmp){
count += (tmp - curHeight + 1);
curHeight = tmp;
}else{
count += (curHeight - tmp + 1);
curHeight = tmp;
}
count++;
}
}
System.out.println(count);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | d37479ac4048802907778cf3865c72e2 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.*;
public class problem2 {
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[]h=new int[n];
for(int i=0;i<n;i++)
{
h[i]=sc.nextInt();
}
int s=n+h[0];
for(int j=1;j<n;j++)
{
s+=Math.abs(h[j]-h[j-1])+1;
}
System.out.print(s);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output | |
PASSED | 8afff128bbe99dbccd4709c4d5bd4583 | train_002.jsonl | 1358686800 | Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts. | 256 megabytes | import java.util.Scanner;
public class CodeforcesRound162B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner kde=new Scanner(System.in);
int n=kde.nextInt();
int[] h= new int[n];
for(int i=0; i<n; i++ )
{
h[i]=kde.nextInt();
}
int sum=0;
sum=h[0]+1;
int visota=h[0];
for(int i=1; i<n; i++ )
{
if(visota<=h[i])
{
sum=sum+h[i]-visota+2;
visota=h[i];
}
else
{
sum=sum+(visota-h[i])+2;
visota=h[i];
}
}
System.out.println(sum);
}
}
| Java | ["2\n1\n2", "5\n2\n1\n2\n1\n1"] | 2 seconds | ["5", "14"] | null | Java 6 | standard input | [
"implementation",
"greedy"
] | a20ca4b053ba71f6b2dc05749287e0a4 | The first line contains an integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i. | 1,000 | Print a single integer — the minimal time required to eat all nuts in seconds. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.