content
stringlengths 219
31.2k
| complexity
stringclasses 5
values | file_name
stringlengths 6
9
| complexity_ranked
float64 0.1
0.9
|
---|---|---|---|
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;
public class A1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long size = scan.nextLong();
int numberOfSpecial = scan.nextInt();
long pageSize = scan.nextLong();
long[] specialItemsArray = new long[numberOfSpecial];
for (int i = 0; i < numberOfSpecial; i++) {
specialItemsArray[i] = scan.nextLong();
}
int totalRemoved = 0;
int step = 0;
long currentPageIndex = BigDecimal.valueOf(specialItemsArray[0]).divide(BigDecimal.valueOf(pageSize),2, RoundingMode.UP).setScale(0, RoundingMode.CEILING).longValue();
int specialItemArrayIndex = 1;
while (specialItemArrayIndex < numberOfSpecial) {
long pageIndex = BigDecimal.valueOf(specialItemsArray[specialItemArrayIndex] - totalRemoved).divide(BigDecimal.valueOf(pageSize),2,RoundingMode.UP).setScale(0, RoundingMode.CEILING).longValue();
if (currentPageIndex != pageIndex) {
step++;
totalRemoved = specialItemArrayIndex;
currentPageIndex = BigDecimal.valueOf(specialItemsArray[specialItemArrayIndex] - totalRemoved).divide(BigDecimal.valueOf(pageSize),2,RoundingMode.UP).setScale(0, RoundingMode.CEILING).longValue();
}
specialItemArrayIndex++;
}
System.out.println(step + 1);
}
}
|
n
|
633.java
| 0.5 |
import java.util.*;
import java.io.*;
public class C{
static PrintWriter out;
static InputReader in;
public static void main(String args[]){
out = new PrintWriter(System.out);
in = new InputReader();
new C();
out.flush(); out.close();
}
C(){
int w = solve();
out.print(w == 0 ? "sjfnb" : "cslnb");
}
int n;
long a[];
int solve(){
n = in.nextInt(); a = new long[n];
long sum = 0;
for(int i = 0; i < n; i++)sum += a[i] = in.nextLong();
if(sum == 0){
return 1;
}
Arrays.sort(a);
int c = 0, c0 = 0; long p = -1, max = 0;
int f = 0;
long t = -1; int pp = -1;
for(int i = 0; i < n; i++){
if(a[i] == p){
c++;
}else{
if(p == 0)c0 = c;
if(c >= 2){f++; t = p; pp = i - 2;}
max = Math.max(max, c);
p = a[i];
c = 1;
}
}
max = Math.max(max, c);
sum = 0;
if(c >= 2){f++; t = p; pp = n - 2;}
if(max > 2 || c0 > 1 || f > 1)return 1;
if(f == 1){
long v = Arrays.binarySearch(a, t - 1);
if(v >= 0)return 1;
a[pp]--; sum = 1;
}
p = -1;
for(int i = 0; i < n; i++){
sum += a[i] - (p + 1);
a[i] = p + 1;
p = a[i];
}
return 1 - (int)(sum % 2);
}
public static class InputReader{
BufferedReader br;
StringTokenizer st;
InputReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public String next(){
while(st == null || !st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
}catch(IOException e){}
}
return st.nextToken();
}
}
}
|
n
|
636.java
| 0.5 |
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);
//boolean
int t=1,c=0;
for(int i=1;i<n;i++){
if(a[i]==a[i-1])
{
if(i-2>=0&&a[i-2]==a[i-1]-1){
System.out.println("cslnb");
return;
}
c++;
}
if(a[i]==a[i-1]&&a[i]==0){
System.out.println("cslnb");
return;
}
}
if(c>1)
{
System.out.println("cslnb");
return;
}
for(int i=0;i<n;i++){
if((a[i]-i)%2!=0)
t=t^1;
}
if(t==1)
System.out.println("cslnb");
else
System.out.println("sjfnb");
}
}
|
n
|
637.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
744444444747477777774
44444447474747777777
*/
/**
*
* @author Andy Phan
*/
public class b {
public static void main(String[] args) {
FS in = new FS(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
Integer[] arr = new Integer[n];
int numZ = 0;
for(int i = 0; i < n; i++) {
arr[i] = in.nextInt();
if(arr[i] == 0) numZ++;
}
Arrays.sort(arr);
if(numZ > 1) {
System.out.println("cslnb");
return;
}
int numDup = 0;
int[] arr2 = new int[n];
for(int i = 0; i < n; i++) {
arr2[i] = arr[i];
if(i != 0) {
if(arr2[i] == arr2[i-1]) {
arr2[i-1]--;
numDup++;
}
}
}
if(numDup > 1) {
System.out.println("cslnb");
return;
}
for(int i = 0; i < n; i++) {
if(i != 0) {
if(arr2[i] == arr2[i-1]) {
System.out.println("cslnb");
return;
}
}
}
long num = 0;
if(numDup == 1) num++;
for(int i = 0; i < n; i++) {
num += arr2[i]-i;
}
if(num%2 == 0) {
System.out.println("cslnb");
} else {
System.out.println("sjfnb");
}
out.close();
}
static class FS {
BufferedReader in;
StringTokenizer token;
public FS(InputStream str) {
in = new BufferedReader(new InputStreamReader(str));
}
public String next() {
if (token == null || !token.hasMoreElements()) {
try {
token = new StringTokenizer(in.readLine());
} catch (IOException ex) {
}
return next();
}
return token.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
n
|
638.java
| 0.5 |
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class B {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
Integer[] a = new Integer[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
Arrays.sort(a);
int[] b = new int[n];
for(int i=0; i<n; i++) b[i] = a[i].intValue();
boolean diff = false;
boolean diff2 = false;
Set<Integer> vals = new HashSet<Integer>();
vals.add(b[0]);
int valval = 0;
for(int i=1; i<n; i++) {
vals.add(b[i]);
if(b[i] == b[i-1]) {
if(!diff) {
diff = true;
valval = b[i];
}
else diff2 = true;
}
}
long sum = 0;
for(int i : b) sum += i;
sum -= 1L*n*(n-1)/2;
if(diff && !diff2) {
if(!vals.contains((valval-1)) && (valval > 0)) {
if(sum%2 == 0) out.println("cslnb"); else out.println("sjfnb");
}
else out.println("cslnb");
}
else if(diff2) out.println("cslnb");
else if(sum%2 == 0) out.println("cslnb"); else out.println("sjfnb");
// int n = Integer.parseInt(st.nextToken());
out.close(); System.exit(0);
}
}
|
n
|
639.java
| 0.5 |
// Java implementation of brute
// force solution.
import
java.util.LinkedHashSet;
class
GFG
{
// Function to check if the given
// number has repeated digit or not
static
int
repeated_digit(
int
n)
{
LinkedHashSet<Integer> s =
new
LinkedHashSet<>();
// Traversing through each digit
while
(n !=
0
)
{
int
d = n %
10
;
// if the digit is present
// more than once in the
// number
if
(s.contains(d))
{
// return 0 if the number
// has repeated digit
return
0
;
}
s.add(d);
n = n /
10
;
}
// return 1 if the number has
// no repeated digit
return
1
;
}
// Function to find total number
// in the given range which has
// no repeated digit
static
int
calculate(
int
L,
int
R)
{
int
answer =
0
;
// Traversing through the range
for
(
int
i = L; i < R +
1
; ++i)
{
// Add 1 to the answer if i has
// no repeated digit else 0
answer = answer + repeated_digit(i);
}
return
answer;
}
// Driver Code
public
static
void
main(String[] args)
{
int
L =
1
, R =
100
;
// Calling the calculate
System.out.println(calculate(L, R));
}
}
// This code is contributed by RAJPUT-JI
|
n
|
64.java
| 0.5 |
import java.util.*;
import java.io.*;
public class C{
static PrintWriter out;
static InputReader in;
public static void main(String args[]){
out = new PrintWriter(System.out);
in = new InputReader();
new C();
out.flush(); out.close();
}
C(){
int a = solve();
out.print(a == 0 ? "tokitsukaze" : a == 1 ? "quailty" : "once again");
}
int n, k;
char ch[]; int a[], c0 = 0, c1 = 0;
TreeSet<Integer> ts[] = new TreeSet[2];
boolean check(){
int min = 0, max = n;
if(!ts[0].isEmpty()){
min = ts[0].first(); max = ts[0].last();
if(max - min + 1 > k)return true;
}
if(!ts[1].isEmpty()){
min = ts[1].first(); max = ts[1].last();
if(max - min + 1 > k)return true;
}
return false;
}
int solve(){
n = in.nextInt(); k = in.nextInt();
ch = in.next().trim().toCharArray(); a = new int[n];
for(int i = 0; i < n; i++)c1 += a[i] = ch[i] - '0';
c0 = n - c1;
for(int i = 0; i < k; i++){
if(a[i] == 0)c0--; else c1--;
}
if(c0 == 0 || c1 == 0)return 0;
for(int i = k; i < n; i++){
if(a[i] == 0)c0--; else c1--;
if(a[i - k] == 0)c0++; else c1++;
if(c0 == 0 || c1 == 0)return 0;
}
for(int i = 0; i < 2; i++)ts[i] = new TreeSet<>();
for(int i = 0; i < n; i++){
ts[a[i]].add(i);
}
for(int i = 0; i < k; i++){
ts[a[i]].remove(i);
}
if(check())return 2;
for(int i = k; i < n; i++){
ts[a[i]].remove(i); ts[a[i - k]].add(i - k);
if(check())return 2;
}
return 1;
}
public static class InputReader{
BufferedReader br;
StringTokenizer st;
InputReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public String next(){
while(st == null || !st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
}catch(IOException e){}
}
return st.nextToken();
}
}
}
|
n
|
640.java
| 0.5 |
import java.io.*;
import java.util.*;
public class Solution {
static boolean canWinFromOneMove(char []s,int k) {
int prefix=0;
int n=s.length;
for(int i=0;i<n && s[i]==s[0];i++)
prefix++;
int suffix=0;
for(int i=n-1;i>=0 && s[i]==s[n-1];i--)
suffix++;
return s[0]==s[n-1] && prefix+suffix+k>=n || Math.max(prefix, suffix)+k>=n;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt(),k=sc.nextInt();
char []s=sc.next().toCharArray();
if(canWinFromOneMove(s, k)) {
System.out.println("tokitsukaze");
return;
}
int []suff=new int [n+1];
suff[n-1]=1;
for(int i=n-2;i>=0;i--) {
suff[i]=1+(s[i+1]==s[i]?suff[i+1]:0);
}
for(int i=n-2;i>=0;i--)
suff[i]=Math.max(suff[i], suff[i+1]);
int max=0,curr=0;
boolean draw=false;
int ones=0;
for(int i=0;i+k<=n;i++) {
// one
int prefix=ones==i?k+ones:max;
int suffix=i+k==n?k:s[i+k]=='1' && suff[i+k]==n-(i+k)?k+suff[i+k]:suff[i+k];
char first=i==0?'1':s[0],last=i+k==n?'1':s[n-1];
boolean zero=first==last && prefix+suffix+k>=n || Math.max(prefix, suffix)+k>=n;
// zero
prefix=ones==0?k+ones:max;
suffix=i+k==n?k:s[i+k]=='0' && suff[i+k]==n-(i+k)?k+suff[i+k]:suff[i+k];
first=i==0?'0':s[0];
last=i+k==n?'0':s[n-1];
boolean one=first==last && prefix+suffix+k>=n || Math.max(prefix, suffix)+k>=n;
if(!zero || !one) {
// System.err.println(i+1);
draw=true;
}
if(s[i]=='1')
ones++;
if(i>0 && s[i]==s[i-1] )
curr++;
else
curr=1;
max=Math.max(max, curr);
}
out.println(draw?"once again":"quailty");
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Scanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
boolean ready() throws IOException {
return br.ready();
}
}
}
|
n
|
641.java
| 0.5 |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.SortedSet;
import java.util.Set;
import java.util.NavigableSet;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Niyaz Nigmatullin
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int n = in.nextInt();
int k = in.nextInt();
char[] c = in.next().toCharArray();
NavigableSet<Integer> ones = new TreeSet<>();
NavigableSet<Integer> zeros = new TreeSet<>();
for (int i = 0; i < n; i++) {
if (c[i] == '0') zeros.add(i);
else ones.add(i);
}
if (ones.isEmpty() || zeros.isEmpty() || ones.last() - ones.first() + 1 <= k || zeros.last() - zeros.first() + 1 <= k) {
out.println("tokitsukaze");
return;
}
if (check(ones, n, k) && check(zeros, n, k)) {
out.println("quailty");
return;
}
out.println("once again");
}
private boolean check(NavigableSet<Integer> ones, int n, int k) {
for (int i = 0; i + k <= n; i++) {
int left = ones.first();
int right = ones.last();
if (left >= i) {
left = ones.higher(i + k - 1);
}
if (right < i + k) {
right = ones.lower(i);
}
if (right - left + 1 > k) {
return false;
}
}
return true;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
}
static class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
public String next() {
StringBuilder sb = new StringBuilder();
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
if (c < 0) {
return null;
}
while (c >= 0 && !isWhiteSpace(c)) {
sb.appendCodePoint(c);
c = read();
}
return sb.toString();
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
}
}
|
n
|
642.java
| 0.5 |
// Java code to demonstrate Difference Array
class
GFG {
// Creates a diff array D[] for A[] and returns
// it after filling initial values.
static
void
initializeDiffArray(
int
A[],
int
D[])
{
int
n = A.length;
D[
0
] = A[
0
];
D[n] =
0
;
for
(
int
i =
1
; i < n; i++)
D[i] = A[i] - A[i -
1
];
}
// Does range update
static
void
update(
int
D[],
int
l,
int
r,
int
x)
{
D[l] += x;
D[r +
1
] -= x;
}
// Prints updated Array
static
int
printArray(
int
A[],
int
D[])
{
for
(
int
i =
0
; i < A.length; i++) {
if
(i ==
0
)
A[i] = D[i];
// Note that A[0] or D[0] decides
// values of rest of the elements.
else
A[i] = D[i] + A[i -
1
];
System.out.print(A[i] +
" "
);
}
System.out.println();
return
0
;
}
// Driver Code
public
static
void
main(String[] args)
{
// Array to be updated
int
A[] = {
10
,
5
,
20
,
40
};
int
n = A.length;
// Create and fill difference Array
// We use one extra space because
// update(l, r, x) updates D[r+1]
int
D[] =
new
int
[n +
1
];
initializeDiffArray(A, D);
// After below update(l, r, x), the
// elements should become 20, 15, 20, 40
update(D,
0
,
1
,
10
);
printArray(A, D);
// After below updates, the
// array should become 30, 35, 70, 60
update(D,
1
,
3
,
20
);
update(D,
2
,
2
,
30
);
printArray(A, D);
}
}
// This code is contributed by Anant Agarwal.
|
n
|
65.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class LightItUp {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int previous = 0;
int array[] = new int[n+1];
int answer = 0;
StringTokenizer st1 = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i++){
array[i] = Integer.parseInt(st1.nextToken());
if(i % 2 == 0){
answer += (array[i] - previous);
}
previous = array[i];
}
if(n % 2 == 0){
answer += (m - previous);
}
previous = m;
int max = Integer.MAX_VALUE;
while(n-- != 0){
int temp = array[n];
if(n%2 == 0){
array[n] = array[n+1] - (previous - array[n]);
}
else{
array[n] = array[n+1] + (previous - array[n]);
}
previous = temp;
max = Math.min(max, array[n]);
}
if(max>=-1){
System.out.println(answer);
}
else{
System.out.println(answer - (max+1));
}
}
}
|
n
|
650.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Cf1003A {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer stk = new StringTokenizer(br.readLine());
int[] arr = new int[100];
int max = 0;
int tmp;
for (int i = 0; i < n; i++) {
tmp = Integer.parseInt(stk.nextToken()) - 1;
max = max < ++arr[tmp] ? arr[tmp] : max;
}
System.out.println(max);
}
}
|
n
|
653.java
| 0.5 |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Priya
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
contest1 solver = new contest1();
solver.solve(1, in, out);
out.close();
}
static class contest1 {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int a = in.nextInt();
int b = in.nextInt();
int x = in.nextInt();
String s = "";
int count = 0;
int copya = a;
int copyb = b;
int change = 0;
if (x == 1) {
if (copya > copyb) {
while (a > 0) {
s += '0';
a--;
}
while (b > 0) {
s += '1';
b--;
}
} else {
while (b > 0) {
s += '1';
b--;
}
while (a > 0) {
s += '0';
a--;
}
}
} else {
// out.println("got in");
if (copya > copyb) {
while (change <= x - 1) {
if (change % 2 == 0) {
s += '0';
change++;
a--;
} else {
s += '1';
change++;
b--;
}
}
} else if (copyb >= copya) {
//out.println("got in in ");
while (change <= x - 1) {
//out.println(change+" hmm");
if (change % 2 == 0) {
s += '1';
change++;
b--;
} else {
s += '0';
change++;
a--;
}
}
}
char z = s.charAt(s.length() - 1);
//out.println(z+" hm");
if (z == '0') {
while (a > 0) {
s += '0';
a--;
}
while (b > 0) {
s += '1';
b--;
}
} else {
while (b > 0) {
s += '1';
b--;
}
while (a > 0) {
s += '0';
a--;
}
}
}
out.println(s);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
n
|
654.java
| 0.5 |
import java.util.*;
import java.io.*;
public class programA {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int arr[] = new int[n];
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(st.nextToken());
}
int s= 2;
for(int i=0;i<n-1;i++){
long dis = (long)2*d;
long dis2 = Math.abs(arr[i]-arr[i+1]);
if(dis2 == dis)s++;
else if (dis2 > dis)s+=2;
}
System.out.println(s);
}
}
|
n
|
657.java
| 0.5 |
import java.util.*;
import java.io.*;
public class SonyaExhibition {
static BufferedReader br;
static StringTokenizer tokenizer;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
int[] arr = {0,1};
for(int i = 0; i < n; i++) {
System.out.print(arr[i % 2]);
}
System.out.println();
}
public static String next() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = br.readLine();
if (line == null)
throw new IOException();
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
|
n
|
658.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Cf1005A {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int numberOfStairs = 0;
StringBuilder result = new StringBuilder("");
StringTokenizer stk = new StringTokenizer(br.readLine());
int previousNum = 0;
int tmp = 0;
for (int i = 0; i < n; i++) {
tmp = Integer.parseInt(stk.nextToken());
if (tmp == 1){
++numberOfStairs;
if (previousNum != 0)
result.append(previousNum + " ");
}
previousNum = tmp;
}
result.append(previousNum);
System.out.println(numberOfStairs);
System.out.println(result);
}
}
|
n
|
659.java
| 0.5 |
class
Profit
{
// Returns maximum profit with two transactions on a given
// list of stock prices, price[0..n-1]
static
int
maxProfit(
int
price[],
int
n)
{
// Create profit array and initialize it as 0
int
profit[] =
new
int
[n];
for
(
int
i=
0
; i<n; i++)
profit[i] =
0
;
/* Get the maximum profit with only one transaction
allowed. After this loop, profit[i] contains maximum
profit from price[i..n-1] using at most one trans. */
int
max_price = price[n-
1
];
for
(
int
i=n-
2
;i>=
0
;i--)
{
// max_price has maximum of price[i..n-1]
if
(price[i] > max_price)
max_price = price[i];
// we can get profit[i] by taking maximum of:
// a) previous maximum, i.e., profit[i+1]
// b) profit by buying at price[i] and selling at
// max_price
profit[i] = Math.max(profit[i+
1
], max_price-price[i]);
}
/* Get the maximum profit with two transactions allowed
After this loop, profit[n-1] contains the result */
int
min_price = price[
0
];
for
(
int
i=
1
; i<n; i++)
{
// min_price is minimum price in price[0..i]
if
(price[i] < min_price)
min_price = price[i];
// Maximum profit is maximum of:
// a) previous maximum, i.e., profit[i-1]
// b) (Buy, Sell) at (min_price, price[i]) and add
// profit of other trans. stored in profit[i]
profit[i] = Math.max(profit[i-
1
], profit[i] +
(price[i]-min_price) );
}
int
result = profit[n-
1
];
return
result;
}
public
static
void
main(String args[])
{
int
price[] = {
2
,
30
,
15
,
10
,
8
,
25
,
80
};
int
n = price.length;
System.out.println(
"Maximum Profit = "
+ maxProfit(price, n));
}
}
/* This code is contributed by Rajat Mishra */
|
n
|
66.java
| 0.5 |
import java.util.*;
import java.lang.*;
public class CodeForce {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
String t=sc.next();
int count=s.length()+t.length();
for(int i=0;i<Math.min(s.length(), t.length());i++){
if(s.charAt(s.length()-1-i)==t.charAt(t.length()-1-i))count=count-2;
else break;
}
System.out.println(count);
}
}
|
n
|
660.java
| 0.5 |
import java.util.*;
//201920181
public class Polycarp{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
int rem[] = new int[3];
Arrays.fill(rem,-1);
rem[0] = 0;
char ch[] = s.next().toCharArray();
int n = ch.length;
long dp[] = new long[n];
int sum = 0;
for(int i=0;i<ch.length;i++){
sum = sum + (ch[i]-48);
if(rem[sum%3] != -1)
if(i>0){
dp[i] = Math.max(dp[i-1],dp[rem[sum%3]]+1);}
else
dp[i] = 1;
else
if(i>0)
dp[i] = dp[i-1];
rem[sum%3] = i;
sum = sum%3;
}
System.out.println(dp[n-1]);
}
}
|
n
|
662.java
| 0.5 |
import java.util.*;
import java.io.*;
import java.util.Map.Entry;
public class Codeforces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
for(int i = 0; i < n; i++){
if(arr[i] % 2 == 0){
arr[i]--;
}
}
for(int i = 0; i < n; i++){
pw.print(arr[i] + " ");
}
pw.flush();
pw.close();
}
}
|
n
|
665.java
| 0.5 |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author zodiacLeo
*/
public class Main
{
public static void main(String[] args)
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC
{
public void solve(int testNumber, FastScanner in, FastPrinter out)
{
int n = in.nextInt();
long[] a = new long[n];
long[] b = new long[n];
for (int i = 0; i < n; i++)
{
b[i] = a[i] = in.nextInt();
if (i > 0)
{
a[i] += a[i - 1];
}
}
for (int i = n - 2; i >= 0; i--)
{
b[i] += b[i + 1];
}
long sum1 = 0;
long sum3 = 0;
long result = 0;
for (int i = 0, j = n - 1; i < j; )
{
sum1 = a[i];
sum3 = b[j];
if (sum1 == sum3)
{
result = Math.max(result, sum1);
i++;
j--;
}
else if (sum1 < sum3)
{
i++;
}
else
{
j--;
}
}
out.println(result);
}
}
static class FastScanner
{
public BufferedReader br;
public StringTokenizer st;
public FastScanner(InputStream is)
{
br = new BufferedReader(new InputStreamReader(is));
}
public FastScanner(File f)
{
try
{
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
public String next()
{
while (st == null || !st.hasMoreElements())
{
String s = null;
try
{
s = br.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
if (s == null)
{
return null;
}
st = new StringTokenizer(s);
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
}
static class FastPrinter extends PrintWriter
{
public FastPrinter(OutputStream out)
{
super(out);
}
public FastPrinter(Writer out)
{
super(out);
}
}
}
|
n
|
667.java
| 0.5 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,ans=0;
String sa,sb;
char[] a,b;
n=sc.nextInt();
sa=sc.next();
sb=sc.next();
a=sa.toCharArray();
b=sb.toCharArray();
ans=0;
for(int i=0;i<n;i++)
{
if(i==n/2)
{
if(a[i]!=b[i])
ans++;
continue;
}
int[] vis=new int[28];
vis[a[i]-'a']++;
vis[b[i]-'a']++;
vis[a[n-1-i]-'a']++;
vis[b[n-1-i]-'a']++;
int num=0,ans1=0,ans2=0;
for(int j=0;j<26;j++)
if(vis[j]!=0)
{
num++;
if(num==1)
ans1=vis[j];
else
ans2=vis[j];
}
if(num==2)
{
if(ans1==1||ans2==1)
ans++;
}
else if(num==3)
{
if(a[i]==a[n-i-1])
ans+=2;
else
ans++;
}
else if(num==4)
ans+=2;
a[i]=b[i];
a[n-1-i]=b[n-1-i];
}
System.out.println(ans);
}
}
|
n
|
668.java
| 0.5 |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author caoash
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
EMilitaryProblem solver = new EMilitaryProblem();
solver.solve(1, in, out);
out.close();
}
static class EMilitaryProblem {
ArrayList<Integer>[] adj;
int[] sz;
int[] preorder;
int curPos;
int[] pos;
public void solve(int testNumber, FastScanner br, PrintWriter pw) {
int n = br.nextInt();
int q = br.nextInt();
preorder = new int[n];
sz = new int[n];
pos = new int[n];
curPos = 0;
adj = new ArrayList[n];
for (int i = 0; i < n; i++) adj[i] = new ArrayList<Integer>();
for (int i = 1; i < n; i++) {
int u = br.nextInt();
u--;
adj[u].add(i);
}
dfs(0);
for (int i = 0; i < n; i++) {
pos[preorder[i]] = i;
}
for (int i = 0; i < q; i++) {
int u = br.nextInt();
int k = br.nextInt();
u--;
if (sz[u] < k) {
pw.println("-1");
} else {
pw.println(preorder[pos[u] + k - 1] + 1);
}
}
pw.close();
}
public int dfs(int u) {
preorder[curPos] = u;
curPos++;
sz[u]++;
for (int e : adj[u]) {
sz[u] += dfs(e);
}
return sz[u];
}
}
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastScanner.SpaceCharFilter filter;
public FastScanner(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
n
|
669.java
| 0.5 |
// A Simple Java program to find
// minimum average subarray
class
Test {
static
int
arr[] =
new
int
[] {
3
,
7
,
90
,
20
,
10
,
50
,
40
};
// Prints beginning and ending indexes of subarray
// of size k with minimum average
static
void
findMinAvgSubarray(
int
n,
int
k)
{
// k must be smaller than or equal to n
if
(n < k)
return
;
// Initialize beginning index of result
int
res_index =
0
;
// Compute sum of first subarray of size k
int
curr_sum =
0
;
for
(
int
i =
0
; i < k; i++)
curr_sum += arr[i];
// Initialize minimum sum as current sum
int
min_sum = curr_sum;
// Traverse from (k+1)'th element to n'th element
for
(
int
i = k; i < n; i++)
{
// Add current item and remove first
// item of previous subarray
curr_sum += arr[i] - arr[i - k];
// Update result if needed
if
(curr_sum < min_sum) {
min_sum = curr_sum;
res_index = (i - k +
1
);
}
}
System.out.println(
"Subarray between ["
+
res_index +
", "
+ (res_index + k -
1
) +
"] has minimum average"
);
}
// Driver method to test the above function
public
static
void
main(String[] args)
{
int
k =
3
;
// Subarray size
findMinAvgSubarray(arr.length, k);
}
}
|
n
|
67.java
| 0.5 |
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
int[] game=new int[n];
int[] bill=new int[m];
for (int i = 0; i <n ; i++) {
game[i]=scan.nextInt();
}
for (int i = 0; i <m ; i++) {
bill[i]=scan.nextInt();
}
int i=0;
int j=0;
int ans=0;
while (i<m){
boolean f=true;
for (int k = j; k <n ; k++) {
if (bill[i]>=game[k]){
ans++;
i++;
j=k+1;
f=false;
break;
}
}
if (f){
break;
}
}
System.out.println(ans);
}
}
|
n
|
670.java
| 0.5 |
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
String str=in.next();
int cnt=0;
for(int i=0;i<str.length();++i) {
if(str.charAt(i)=='1') {
++cnt;
}
}
int i=0;
for(;i<str.length();++i) {
if(str.charAt(i)=='0') {
System.out.print("0");
}
else if(str.charAt(i)=='2') {
while(cnt-->0) {//
System.out.print("1");
}
System.out.print("2");
}
}
while(cnt-->0) {
System.out.print("1");
}
in.close();
}
}
|
n
|
671.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class AnnoyingPresent {
//UPSOLVED
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
long n = Long.parseLong(st.nextToken()) , m = Long.parseLong(st.nextToken());
long sum = 0;
for(int i=0;i<m;i++){
StringTokenizer st1 = new StringTokenizer(br.readLine());
sum+= n* Long.parseLong(st1.nextToken());
Long a= Long.parseLong(st1.nextToken());
if(a < 0){
if(n % 2 == 0)
sum += n*n / 4*a;
else{
sum += (n/2) * (n/2+1) * a;
}
}
else
sum += (a*(n) * (n-1) / 2);
}
System.out.println((double)sum/n);
}
}
|
n
|
672.java
| 0.5 |
import java.io.*;
import java.util.*;
public class CF1009E {
static final int MD = 998244353;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] aa = new int[1 + n];
for (int i = 1, a = 0; i <= n; i++)
aa[i] = a = (a + Integer.parseInt(st.nextToken())) % MD;
int[] pp = new int[n];
pp[0] = 1;
for (int i = 1, p = 1; i < n; i++) {
pp[i] = p;
p = p * 2 % MD;
}
int d = 0;
long ans = 0;
for (int i = n - 1; i >= 0; i--) { // rest at i
d = (d * 2 % MD + aa[n - 1 - i]) % MD; // rest again before n
ans = (ans + (long) (d + aa[n - i]) * pp[i]) % MD;
}
System.out.println(ans);
}
}
|
n
|
675.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SFly {
public static void main(String[] args) throws IOException {
BufferedReader lector = new BufferedReader(new InputStreamReader(System.in));
int planet = Integer.parseInt(lector.readLine());
int ini = Integer.parseInt(lector.readLine());
double peso = ini;
int[] desp = new int[planet];
int[] ater = new int[planet];
String[] temp = lector.readLine().split(" ");
for(int i=0; i<planet; i++) {
desp[i] = Integer.parseInt(temp[i]);
if(desp[i] == 1) {
System.out.println(-1);
lector.close();
return;
}
}
temp = lector.readLine().split(" ");
for(int i=0; i<planet; i++) {
ater[i] = Integer.parseInt(temp[i]);
if(ater[i] == 1) {
System.out.println(-1);
lector.close();
return;
}
}
temp = null;
int i=planet-1;
peso = (peso*ater[0])/(ater[0]-1);
while(i>0) {
peso = (peso*desp[i])/(desp[i]-1);
peso = (peso*ater[i])/(ater[i]-1);
i--;
}
peso = (peso*desp[0])/(desp[0]-1);
peso = peso - ini;
System.out.println(peso);
lector.close();
}
}
|
n
|
676.java
| 0.5 |
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
/* spar5h */
public class cf1 implements Runnable{
public void run() {
InputReader s = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int t = 1;
while(t-- > 0) {
int n = s.nextInt(), m = s.nextInt();
int[] a = new int[n + 1];
for(int i = 1; i <= n; i++)
a[i] = s.nextInt();
int[] b = new int[n + 1];
for(int i = 1; i <= n; i++)
b[i] = s.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(a[1]);
for(int i = 2; i <= n; i++) {
list.add(b[i]); list.add(a[i]);
}
list.add(b[1]);
double wt = m;
boolean check = true;
for(int i = list.size() - 1; i >= 0; i--) {
if(list.get(i) <= 1) {
check = false; break;
}
double x = wt / (list.get(i) - 1);
wt += x;
}
if(check)
w.println(wt - m);
else
w.println(-1);
}
w.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new cf1(),"cf1",1<<26).start();
}
}
|
n
|
679.java
| 0.5 |
import java.util.*;
public class helloWorld
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
String str = in.next();
boolean[] exist = new boolean[200];
int dn[][] = new int[200][m+1];
for(int i = 0; i < n; i++) {
int a = str.charAt(i);
exist[a] = true;
dn[a][1] = a - 'a' + 1;
}
for(int k = 2; k <= m; k++)
for(int i = 'a'; i <= 'z'; i++)
if(exist[i]) {
int a = 0;
for(int j = i+2; j <= 'z'; j++)
if(dn[j][k-1] > 0 && (a == 0 || (a > dn[j][k-1]) ) )
a = dn[j][k-1];
if(a > 0)
dn[i][k] = a + i - 'a' + 1;
}
int ans = -1;
for(int i = 'a'; i <= 'z'; i++)
if(dn[i][m] > 0 && (ans == -1 || ans > dn[i][m]) )
ans = dn[i][m];
System.out.println(ans);
in.close();
}
}
|
n
|
683.java
| 0.5 |
import java.io.*;
public class First {
StreamTokenizer in;
PrintWriter out;
int nextInt() throws IOException {
in.nextToken();
return (int)in.nval;
}
long nextLong() throws IOException {
in.nextToken();
return (long) in.nval;
}
String nextString() throws IOException {
in.nextToken();
return in.sval;
}
void run() throws IOException {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
solve();
out.flush();
}
void solve() throws IOException {
int n = nextInt(), k = nextInt(), sum = 0, count = 0;
String str = nextString();
char[] arr = str.toCharArray();
boolean[] bool = new boolean[26];
for(char ch: arr){
bool[((int)ch)-97] = true;
}
for(int i = 0; i < 26; i++){
if(bool[i]){
sum += i+1;
count++;
i += 1;
}
if(count == k) break;
}
if(count == k) out.println(sum);
else out.println(-1);
}
public static void main(String[] args) throws IOException {
new First().run();
}
}
|
n
|
685.java
| 0.5 |
import java.util.*;
import java.io.*;
public class Solution
{
public static void main(String [] args) throws IOException
{
PrintWriter pw=new PrintWriter(System.out);//use pw.println() not pw.write();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
/*
inputCopy
5 3
xyabd
outputCopy
29
inputCopy
7 4
problem
outputCopy
34
inputCopy
2 2
ab
outputCopy
-1
inputCopy
12 1
abaabbaaabbb
outputCopy
1
*/
int n=Integer.parseInt(st.nextToken());
int k=Integer.parseInt(st.nextToken());
st=new StringTokenizer(br.readLine());
String str=st.nextToken();
char [] arr=str.toCharArray();
Arrays.sort(arr);
int weight=arr[0]-96;
char a=arr[0];
int included=1;
for(int i=1;i<arr.length;++i)
{
if(included==k)
break;
char c=arr[i];
if(c-a<2)
continue;
weight+=arr[i]-96;
++included;
a=arr[i];
}
if(included==k)
pw.println(weight);
else
pw.println(-1);
pw.close();//Do not forget to write it after every program return statement !!
}
}
/*
→Judgement Protocol
Test: #1, time: 77 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
5 3
xyabd
Output
29
Answer
29
Checker Log
ok 1 number(s): "29"
Test: #2, time: 78 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
7 4
problem
Output
34
Answer
34
Checker Log
ok 1 number(s): "34"
Test: #3, time: 139 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 2
ab
Output
-1
Answer
-1
Checker Log
ok 1 number(s): "-1"
Test: #4, time: 124 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
12 1
abaabbaaabbb
Output
1
Answer
1
Checker Log
ok 1 number(s): "1"
Test: #5, time: 124 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
50 13
qwertyuiopasdfghjklzxcvbnmaaaaaaaaaaaaaaaaaaaaaaaa
Output
169
Answer
169
Checker Log
ok 1 number(s): "169"
Test: #6, time: 108 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
50 14
qwertyuiopasdfghjklzxcvbnmaaaaaaaaaaaaaaaaaaaaaaaa
Output
-1
Answer
-1
Checker Log
ok 1 number(s): "-1"
Test: #7, time: 93 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1 1
a
Output
1
Answer
1
Checker Log
ok 1 number(s): "1"
Test: #8, time: 108 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
50 1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Output
1
Answer
1
Checker Log
ok 1 number(s): "1"
Test: #9, time: 77 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
50 2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Output
-1
Answer
-1
Checker Log
ok 1 number(s): "-1"
Test: #10, time: 92 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
13 13
uwgmkyqeiaocs
Output
169
Answer
169
Checker Log
ok 1 number(s): "169"
Test: #11, time: 124 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
13 13
hzdxpbfvrltnj
Output
182
Answer
182
Checker Log
ok 1 number(s): "182"
Test: #12, time: 93 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1 1
n
Output
14
Answer
14
Checker Log
ok 1 number(s): "14"
Test: #13, time: 92 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
10 8
smzeblyjqw
Output
113
Answer
113
Checker Log
ok 1 number(s): "113"
Test: #14, time: 78 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
20 20
tzmvhskkyugkuuxpvtbh
Output
-1
Answer
-1
Checker Log
ok 1 number(s): "-1"
Test: #15, time: 109 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
30 15
wjzolzzkfulwgioksfxmcxmnnjtoav
Output
-1
Answer
-1
Checker Log
ok 1 number(s): "-1"
Test: #16, time: 93 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
40 30
xumfrflllrrgswehqtsskefixhcxjrxbjmrpsshv
Output
-1
Answer
-1
Checker Log
ok 1 number(s): "-1"
Test: #17, time: 124 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
50 31
ahbyyoxltryqdmvenemaqnbakglgqolxnaifnqtoclnnqiabpz
Output
-1
Answer
-1
Checker Log
ok 1 number(s): "-1"
Test: #18, time: 124 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
10 7
iuiukrxcml
Output
99
Answer
99
Checker Log
ok 1 number(s): "99"
Test: #19, time: 109 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
38 2
vjzarfykmrsrvwbwfwldsulhxtykmjbnwmdufa
Output
5
Answer
5
Checker Log
ok 1 number(s): "5"
Test: #20, time: 77 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
12 6
fwseyrarkwcd
Output
61
Answer
61
Checker Log
ok 1 number(s): "61"
Test: #21, time: 109 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 2
ac
Output
4
Answer
4
Checker Log
ok 1 number(s): "4"
Test: #22, time: 108 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1 1
c
Output
3
Answer
3
Checker Log
ok 1 number(s): "3"
Test: #23, time: 124 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 2
ad
Output
5
Answer
5
Checker Log
ok 1 number(s): "5"
Test: #24, time: 77 ms., memory: 0 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER
Input
2 1
ac
Output
-1
Answer
1
Checker Log
wrong answer 1st number
*/
|
n
|
687.java
| 0.5 |
class
MinimumDistance
{
int
minDist(
int
arr[],
int
n,
int
x,
int
y)
{
int
i =
0
;
int
min_dist = Integer.MAX_VALUE;
int
prev=
0
;
// Find the first occurence of any of the two numbers (x or y)
// and store the index of this occurence in prev
for
(i =
0
; i < n; i++)
{
if
(arr[i] == x || arr[i] == y)
{
prev = i;
break
;
}
}
// Traverse after the first occurence
for
(; i < n; i++)
{
if
(arr[i] == x || arr[i] == y)
{
// If the current element matches with any of the two then
// check if current element and prev element are different
// Also check if this value is smaller than minimum distance
// so far
if
(arr[prev] != arr[i] && (i - prev) < min_dist)
{
min_dist = i - prev;
prev = i;
}
else
prev = i;
}
}
return
min_dist;
}
/* Driver program to test above functions */
public
static
void
main(String[] args) {
MinimumDistance min =
new
MinimumDistance();
int
arr[] = {
3
,
5
,
4
,
2
,
6
,
3
,
0
,
0
,
5
,
4
,
8
,
3
};
int
n = arr.length;
int
x =
3
;
int
y =
6
;
System.out.println(
"Minimum distance between "
+ x +
" and "
+ y
+
" is "
+ min.minDist(arr, n, x, y));
}
}
|
n
|
69.java
| 0.5 |
import java.util.*;
public class helloWorld
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int cnt = 0;
String ans = "Yes";
for(int i = 0; i < n; i++)
cnt += in.nextInt();
for(int i = 0; i < n; i++)
cnt -= in.nextInt();
if(cnt < 0)
ans = "No";
System.out.println(ans);
in.close();
}
}
|
n
|
693.java
| 0.5 |
import java.util.*;
import java.io.*;
public class Piles {
static int summation(int arr[]) {
int k, sum=0;
for(k=0;k<arr.length;k++) {
sum = sum + arr[k];
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n>=1 && n<=50) {
int x[] = new int[n];
int y[] = new int[n];
for(int i=0;i<n;i++) {
x[i] = sc.nextInt();
}
for(int j=0;j<n;j++) {
y[j] = sc.nextInt();
}
int xsum = summation(x);
int ysum = summation(y);
if(xsum>=ysum) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
}
|
n
|
694.java
| 0.5 |
import java.util.Scanner;
public class Stones {
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int s1=0;
int s2=0;
for (int i=0;i<n;++i)
s1+=input.nextInt();
for (int i=0;i<n;++i)
s2+=input.nextInt();
if (s1 >= s2)
System.out.println("Yes");
else
System.out.println("No");
}
}
|
n
|
695.java
| 0.5 |
import java.util.Scanner;
public class Piles {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] a = new int[2]; int x = scan.nextInt();
for(int i = 0; i < 2; i++) for(int j = 0; j < x; j++) a[i] += scan.nextInt();
System.out.println(a[1] <= a[0] ? "Yes" : "No");
}
}
|
n
|
696.java
| 0.5 |
import java.util.*;
import static java.lang.Math.*;
import java.io.*;
public class SolutionB {
public static void main(String args[])throws IOException{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
int a[] = new int[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
if(!set1.contains(a[i])){
set1.add(a[i]);
}else{
System.out.println(0);
return;
}
}
for(int i = 0; i < n; i++){
int b = a[i] & k;
if(b != a[i] && set1.contains(b)){
System.out.println(1);
return;
}
//if(!set2.contains(b)){
//set2.add(b);
//}else{
// System.out.println(2);
// return;
//}
}
for(int i = 0; i < n; i++){
int b = a[i] & k;
if(b != a[i] && set2.contains(b)){
System.out.println(2);
return;
}else{
set2.add(b);
}
}
System.out.println(-1);
}
}
|
n
|
698.java
| 0.5 |
//package contese_476;
import java.util.*;
public class q1
{
int m=(int)1e9+7;
public class Node
{
int a;
int b;
public void Node(int a,int b)
{
this.a=a;
this.b=b;
}
}
public int mul(int a ,int b)
{
a=a%m;
b=b%m;
return((a*b)%m);
}
public int pow(int a,int b)
{
int x=1;
while(b>0)
{
if(b%2!=0)
x=mul(x,a);
a=mul(a,a);
b=b/2;
}
return x;
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
HashMap<Integer,Integer> h=new HashMap();
//HashMap<Integer,Integer> h1=new HashMap();
int[] a=new int[n];
int x=sc.nextInt();
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(h.get(a[i])==null)
{
h.put(a[i], 1);
//h1.put(a[i],i);
}
else
{
System.out.print(0);
System.exit(0);
}
}
for(int i=0;i<n;i++)
{
int num=a[i]&x;
if(num==a[i])
continue;
if(h.get(num)==null)
continue;
else
{
System.out.print(1);
System.exit(0);
}
}
for(int i=0;i<n;i++)
{
int num=a[i]&x;
if(num==a[i])
continue;
if(h.get(num)==null)
h.put(num, 1);
else
{
System.out.print(2);
System.exit(0);
}
}
System.out.print(-1);
}
}
|
n
|
699.java
| 0.5 |
/**
* Created by Baelish on 7/30/2018.
*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class B {
public static void main(String[] args)throws Exception {
FastReader in = new FastReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int ans = -1;
int f[] = new int[(int)2e5+50];
int g[] = new int[(int)2e5+50];
int n = in.nextInt(), x = in.nextInt();
int arr[] = new int[n+1];
for (int i = 1; i <= n && ans == -1; i++) {
int a = in.nextInt();
if(f[a] > 0){
ans = 0; break;
}
f[a]++;
arr[i] = a;
}
for (int i = 1; i <= n && ans == -1; i++) {
int a = arr[i] & x;
if( (a == arr[i] && f[a] > 1) || (a != arr[i] && f[a] > 0)){
ans = 1; break;
}
g[a]++;
}
for (int i = 1; i <= n && ans == -1; i++) {
int a = arr[i] & x;
if(g[a] > 1){
ans = 2; break;
}
//g[a]++;
}
pw.println(ans);
pw.close();
}
static void debug(Object...obj) {
System.err.println(Arrays.deepToString(obj));
}
static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
static final int ints[] = new int[128];
public FastReader(InputStream is){
for(int i='0';i<='9';i++) ints[i]=i-'0';
this.is = is;
}
public 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++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while((b = readByte()) != -1 && isSpaceChar(b));
return b;
}
public String next(){
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt(){
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) + ints[b];
}else{
return minus ? -num : num;
}
b = readByte();
}
}
public long nextLong() {
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<<3) + (num<<1) + ints[b];
}else{
return minus ? -num : num;
}
b = readByte();
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
/* public char nextChar() {
return (char)skip();
}*/
public char[] next(int n){
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
/*private char buff[] = new char[1005];
public char[] nextCharArray(){
int b = skip(), p = 0;
while(!(isSpaceChar(b))){
buff[p++] = (char)b;
b = readByte();
}
return Arrays.copyOf(buff, p);
}*/
}
}
|
n
|
704.java
| 0.5 |
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;
public class Main {
public static void main(String[] args) throws IOException {
FastReader in = new FastReader(System.in);
PrintWriter out = new PrintWriter(System.out);
long n = in.nextLong();
long k = in.nextLong();
long s = in.nextLong();
if (k > s || k * (n - 1) < s) {
out.println("NO");
} else {
out.println("YES");
long cur = 1;
while (k > 0) {
long x = Math.min(n - 1, s - k + 1);
cur = ( cur - x > 0 ? cur - x : cur + x);
out.print(cur + " ");
s -= x;
k--;
}
}
out.flush();
out.close();
}
static class FastReader {
StringTokenizer st;
BufferedReader br;
public FastReader(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public FastReader(FileReader fileReader) {
br = new BufferedReader(fileReader);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
|
n
|
708.java
| 0.5 |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class DeathNote {
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static StringTokenizer st;
public static void main(String[] args) throws IOException {
int n = nextInt();
int m = nextInt();
int[] a = intArray(n);
StringBuffer sb = new StringBuffer();
int x = 0;
for (int i = 0; i < n; i++) {
x += a[i];
sb.append(" " + (x/m));
x %= m;
}
System.out.println(sb.toString().substring(1));
}
public static String nextLine() throws IOException {
return in.readLine();
}
public static String nextString() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextString());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextString());
}
public static int[] intArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public static int[][] intArray(int n, int m) throws IOException {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = nextInt();
return a;
}
public static long[] longArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
|
n
|
712.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Cf1017A {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int result = 1;
int thomasSum = 0;
StringTokenizer stk;
stk = new StringTokenizer(br.readLine());
int first = Integer.parseInt(stk.nextToken());
int second = Integer.parseInt(stk.nextToken());
int third = Integer.parseInt(stk.nextToken());
int fourth = Integer.parseInt(stk.nextToken());
thomasSum = first + second + third + fourth;
int tmp;
for (int i = 1; i < n; i++) {
stk = new StringTokenizer(br.readLine());
first = Integer.parseInt(stk.nextToken());
second = Integer.parseInt(stk.nextToken());
third = Integer.parseInt(stk.nextToken());
fourth = Integer.parseInt(stk.nextToken());
tmp = first + second + third + fourth;
if (tmp > thomasSum)
result++;
}
System.out.println(result);
}
}
|
n
|
713.java
| 0.5 |
import java.util.*;
public class Test { public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int x= (int)Math.sqrt(n) ;
int a[] = new int[n+5];
for(int i=1,o=n,j;i<=n;i+=x)
for(j=(int)Math.min(i+x-1,n);j>=i;a[j--]=o--);
for(int i=1;i<=n;i++)System.out.print(a[i]+" ");
System.out.println();
}
}
|
n
|
714.java
| 0.5 |
import java.util.Scanner;
//import java.util.Scanner;
public class SingleWildcard {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
char[] s1 =new char[a];
s1 = input.next().toCharArray();
char[] s2 = new char[b];
s2 = input.next().toCharArray();
boolean condition = false;
for(int i=0; i<a;i++){
if(s1[i]=='*'){
condition= true;
break;
}
}
if(!condition){
if(match(s1,s2)){
System.out.println("YES");
}
else
System.out.println("NO");
return;
}
else{
int i=0;
if(s1.length-1>s2.length){
System.out.println("NO");
return;
}
while(i<s1.length && i<s2.length && s1[i]==s2[i]){
i++;
}
int j=s2.length-1;
int k = s1.length-1;
while(j>=0 && k>=0 && s1[k]==s2[j] && i<=j){
j--;
k--;
}
//System.out.println(i);
if(i==k && i>=0 && i<s1.length && s1[i]=='*' ){
System.out.println("YES");
return;
}
System.out.println("NO");
}
}
static boolean match(char[] s1,char[] s2){
if(s1.length!=s2.length)return false;
for(int i=0; i<s1.length;i++){
if(s1[i]!=s2[i])return false;
}
return true;
}
}
|
n
|
717.java
| 0.5 |
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
String s = sc.next();
StringBuilder ans = new StringBuilder();
int count = 0;
int open = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
ans.append("(");
count++;
open++;
} else {
ans.append(")");
open--;
}
if (count == k / 2) {
break;
}
}
while (open > 0) {
ans.append(")");
open--;
}
System.out.println(ans.toString());
}
}
|
n
|
719.java
| 0.5 |
import java.io.*;
import java.util.Arrays;
import java.util.StringJoiner;
import java.util.StringTokenizer;
import java.util.function.Function;
public class Main {
static String S;
public static void main(String[] args) {
FastScanner sc = new FastScanner(System.in);
S = sc.next();
System.out.println(solve());
}
static int solve() {
int ans = -1;
int time = 1;
int n = S.length();
for (int i = 1; i < n*2; i++) {
if( S.charAt((i-1)%n) != S.charAt(i%n) ) {
time++;
} else {
ans = Math.max(time, ans);
time = 1;
}
}
ans = Math.max(time, ans);
if( ans == n*2 ) {
return n;
} else {
return ans;
}
}
@SuppressWarnings("unused")
static class FastScanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
long nextLong() {
return Long.parseLong(next());
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[] nextIntArray(int n, int delta) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt() + delta;
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
static <A> void writeLines(A[] as, Function<A, String> f) {
PrintWriter pw = new PrintWriter(System.out);
for (A a : as) {
pw.println(f.apply(a));
}
pw.flush();
}
static void writeLines(int[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (int a : as) pw.println(a);
pw.flush();
}
static void writeLines(long[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (long a : as) pw.println(a);
pw.flush();
}
static int max(int... as) {
int max = Integer.MIN_VALUE;
for (int a : as) max = Math.max(a, max);
return max;
}
static int min(int... as) {
int min = Integer.MAX_VALUE;
for (int a : as) min = Math.min(a, min);
return min;
}
static void debug(Object... args) {
StringJoiner j = new StringJoiner(" ");
for (Object arg : args) {
if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
else j.add(arg.toString());
}
System.err.println(j.toString());
}
}
|
n
|
721.java
| 0.5 |
import java.io.*;
import java.util.*;
public class CF1027D {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] cc = new int[n];
for (int i = 0; i < n; i++)
cc[i] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] aa = new int[n];
for (int i = 0; i < n; i++)
aa[i] = Integer.parseInt(st.nextToken()) - 1;
int[] used = new int[n];
int ans = 0;
for (int i = 0; i < n; i++) {
if (used[i] == 2)
continue;
int j = i;
while (used[j] == 0) {
used[j] = 1;
j = aa[j];
}
if (used[j] == 1) {
int c = cc[j];
while (used[j] == 1) {
used[j] = 2;
c = Math.min(c, cc[j]);
j = aa[j];
}
ans += c;
}
j = i;
while (used[j] == 1) {
used[j] = 2;
j = aa[j];
}
}
System.out.println(ans);
}
}
|
n
|
728.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Cheast {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] l = new int[n];
l[0] = sc.nextInt();
int x=1;
int c=1;
for (int i=1;i<n;i++) {
l[i] = sc.nextInt();
if (l[i] <= l[i-1]*2) {
c++;
x = Math.max(x,c);
}
else
c = 1;
}
System.out.println(x);
}
}
|
n
|
735.java
| 0.5 |
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
public class Main
{
static Graph graph[];
public static void add_edge(int u,int v)
{
graph[u].adj.add(graph[v]);
graph[v].adj.add(graph[u]);
}
public static void dfs(int index)
{
Graph z=graph[index];
z.vis=1;Graph v;
for( int i=0;i<z.adj.size();i++)
{
v=z.adj.get(i);
if(v.vis==0)
{
v.dist=z.dist+1;
v.parent=z.val;
dfs(v.val);
}
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
int n=sc.nextInt();
Pair arr[]=new Pair[n];
Pair pref[]=new Pair[n];
Pair suff[]=new Pair[n];
for( int i=0;i<n;i++)
{
long u=sc.nextLong();
long v=sc.nextLong();
arr[i]=new Pair(u,v);
pref[i]=new Pair(0,0);
suff[i]=new Pair(0,0);
}
pref[0].x=arr[0].x;
pref[0].y=arr[0].y;
for( int i=1;i<n;i++)
{
pref[i].x=(long)Math.max(pref[i-1].x,arr[i].x);
pref[i].y=(long)Math.min(pref[i-1].y,arr[i].y);
}
suff[n-1].x=arr[n-1].x;
suff[n-1].y=arr[n-1].y;
for( int i=n-2;i>=0;i--)
{
suff[i].x=(long)Math.max(suff[i+1].x,arr[i].x);
suff[i].y=(long)Math.min(suff[i+1].y,arr[i].y);
}
long max=Long.MIN_VALUE;
long ans=0;
for( int i=0;i<n;i++)
{
long val=Long.MAX_VALUE;
long val1=Long.MAX_VALUE;
if(i!=0&&i!=n-1)
{
val=(long)Math.min(pref[i-1].y,suff[i+1].y)-(long)Math.max(pref[i-1].x,suff[i+1].x);
}
else if(i!=n-1)
{
val=suff[i+1].y-suff[i+1].x;
}
else
val=pref[i-1].y-pref[i-1].x;
ans=val;
if(ans<0)
ans=0;
max=(long)Math.max(ans,max);
}
System.out.println(max);
}
}
class mycomparator implements Comparator<Graph>
{
public int compare(Graph a, Graph b)
{
return b.dist-a.dist;
}
}
class Graph
{
int vis,col,val;int parent;int deg;int dist;
ArrayList<Graph> adj;
Graph(int val)
{
vis=0;
col=-1;
adj=new ArrayList<>();
parent=-1;
this.val=val;
deg=0;
dist=-1;
}
}
class Pair
{
long x,y;
Pair( long x, long y)
{
this.x=x;
this.y=y;
}
}
|
n
|
736.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class CF_1029E_Tree_with_Small_Distances {
static ArrayList<Integer> adj[];
static int dist[];
static boolean visitParent[];
static int ans=0;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
adj=new ArrayList[n+1];
dist = new int[n+1];
visitParent = new boolean[n+1];
for(int i=0;i<=n;i++) adj[i]=new ArrayList<Integer>();
int max=0;
//first contribution
for(int i=1;i<n;i++){
int u = sc.nextInt(),v=sc.nextInt();
adj[u].add(v);
adj[v].add(u);
}
dist[1]=0;
dfs(1,1);
System.out.println(ans);
}
private static void dfs(int i , int j) {
// TODO Auto-generated method stub
boolean f = false;
for(int k=0;k<adj[i].size();k++){
int x = adj[i].get(k);
if(x!=j){
dist[x]=dist[i]+1;
dfs(x,i);
if(visitParent[x])
f=true;
}
}
//System.out.println(Arrays.toString(dist));
if(dist[i]>2&&!visitParent[j]&&!f&&!visitParent[i]){
visitParent[j]=true;
ans++;
for(int v=0;v<adj[i].size();v++){
}
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
|
n
|
738.java
| 0.5 |
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class PlayingPiano {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> as = new LinkedList<>();
int[] as2 = new int[n];
for (int i = 0; i < n; i++) {
int a = scanner.nextInt();
as.add(a);
as2[i] = a;
}
//System.out.println(solve(as));
System.out.println(solve2(as2));
scanner.close();
}
public static String solve(List<Integer> as) {
List<Integer> fingers = new LinkedList<>();
fingers.add(1);
fingers.add(2);
fingers.add(3);
fingers.add(4);
fingers.add(5);
List<Integer> solution = assign(as, fingers, fingers);
if (solution == null) {
return "-1";
} else {
StringBuilder sb = new StringBuilder();
for (int b : solution) {
sb.append(b);
sb.append(" ");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
}
private static List<Integer> assign(List<Integer> as, List<Integer> fingers, List<Integer> allFingers) {
// if fingers is empty return null
if (fingers.isEmpty()) {
return null;
}
// if as size is one then return first element in fingers
if (as.size() == 1) {
List<Integer> ret = new LinkedList<>();
ret.add(fingers.get(0));
return ret;
}
// get sublist
List<Integer> subList = as.subList(1, as.size());
for (int i = 0; i < fingers.size(); i++) {
// recursively call with sublist and limited list of fingers
List<Integer> subFingers = new LinkedList<>();
final int j = i;
if (as.get(0) < as.get(1)) {
subFingers = allFingers.stream()
.filter(p -> p > fingers.get(j)).collect(Collectors.toList());
} else if (as.get(0) > as.get(1)) {
subFingers = allFingers.stream()
.filter(p -> p < fingers.get(j)).collect(Collectors.toList());
} else {
subFingers = allFingers.stream()
.filter(p -> p != fingers.get(j)).collect(Collectors.toList());
}
List<Integer> ret = assign(subList, subFingers, allFingers);
if (ret != null) {
List<Integer> solution = new LinkedList<>();
solution.add(fingers.get(i));
solution.addAll(ret);
return solution;
}
// if return is null, then return null, else return an array
}
return null;
}
public static String solve2(int[] as) {
int[] ret = new int[as.length];
if (as.length == 1) return "1";
if (as[0] < as[1]) ret[0] = 1;
else if (as[0] == as[1]) ret[0] = 3;
else ret[0] = 5;
for (int i = 1; i < as.length - 1; i++) {
if (as[i-1] < as[i] && ret[i-1] == 5) return "-1";
if (as[i-1] > as[i] && ret[i-1] == 1) return "-1";
if (as[i-1] < as[i] && as[i] < as[i+1]) {
ret[i] = ret[i-1] + 1;
} else if (as[i-1] == as[i] && as[i] < as[i+1]) {
ret[i] = ret[i-1] == 1 ? 2 : 1;
} else if (as[i-1] > as[i] && as[i] < as[i+1]) {
ret[i] = 1;
} else if (as[i-1] < as[i] && as[i] == as[i+1]) {
ret[i] = ret[i-1] + 1;
} else if (as[i-1] == as[i] && as[i] == as[i+1]) {
ret[i] = ret[i-1] == 4 ? 2 : 4;
} else if (as[i-1] > as[i] && as[i] == as[i+1]) {
ret[i] = ret[i-1] == 2 ? 1 : 2;
} else if (as[i-1] < as[i] && as[i] > as[i+1]) {
ret[i] = 5;
} else if (as[i-1] == as[i] && as[i] > as[i+1]) {
ret[i] = ret[i-1] == 5 ? 4 : 5;
} else if (as[i-1] > as[i] && as[i] > as[i+1]) {
ret[i] = ret[i-1] - 1;
}
}
if (as.length > 1) {
if (as[as.length - 1] > as[as.length - 2]) {
if (ret[as.length - 2] == 5)
return "-1";
ret[as.length - 1] = 5;
} else if (as[as.length - 1] == as[as.length - 2]) {
ret[as.length - 1] = ret[as.length - 2] == 5 ? 4 : 5;
} else {
if (ret[as.length - 2] == 1)
return "-1";
ret[as.length - 1] = 1;
}
}
StringBuilder sb = new StringBuilder();
for (int b : ret) {
sb.append(b);
sb.append(" ");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
}
|
n
|
742.java
| 0.5 |
import java.util.*;
import java.io.*;
public class _1036_B_DiagonalWalkingV2 {
public static void main(String[] args) throws IOException {
int Q = readInt();
while(Q-- > 0) {
long n = readLong(), m = readLong(), k = readLong();
if(Math.max(n, m) > k) println(-1);
else {
long ans = k;
if(n%2 != k%2) ans--;
if(m%2 != k%2) ans--;
println(ans);
}
}
exit();
}
final private static int BUFFER_SIZE = 1 << 16;
private static DataInputStream din = new DataInputStream(System.in);
private static byte[] buffer = new byte[BUFFER_SIZE];
private static int bufferPointer = 0, bytesRead = 0;
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static 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 static String read() throws IOException {
byte[] ret = new byte[1024];
int idx = 0;
byte c = Read();
while (c <= ' ') {
c = Read();
}
do {
ret[idx++] = c;
c = Read();
} while (c != -1 && c != ' ' && c != '\n' && c != '\r');
return new String(ret, 0, idx);
}
public static int readInt() 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 static long readLong() 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 static double readDouble() 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 static void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private static byte Read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
static void print(Object o) {
pr.print(o);
}
static void println(Object o) {
pr.println(o);
}
static void flush() {
pr.flush();
}
static void println() {
pr.println();
}
static void exit() throws IOException {
din.close();
pr.close();
System.exit(0);
}
}
|
n
|
746.java
| 0.5 |
import java.util.*;
import java.io.*;
public class vasyaarray{
public static void main(String[] args)throws IOException {
FastReader in=new FastReader(System.in);
int n=in.nextInt();
long arr1[]=new long[n];
long pre1[]=new long[n];
int i,j;
arr1[0]=in.nextLong();
pre1[0]=arr1[0];
for(i=1;i<n;i++)
{
arr1[i]=in.nextLong();
pre1[i]=pre1[i-1]+arr1[i];
}
int m=in.nextInt();
long arr2[]=new long[m];
long pre2[]=new long[m];
arr2[0]=in.nextInt();
pre2[0]=arr2[0];
for(i=1;i<m;i++)
{
arr2[i]=in.nextInt();
pre2[i]=pre2[i-1]+arr2[i];
}
//System.out.println(Arrays.toString(pre1));
//System.out.println(Arrays.toString(pre2));
if(pre1[n-1]!=pre2[m-1])
System.out.println("-1");
else
{
long s1=0,s2=0;
i=j=0;
int k=0;
while(true)
{
if(s1==s2)
{
if(s1==0)
{
s1=arr1[i++];
s2=arr2[j++];
// System.out.println(s1+" "+s2+" "+i+" "+j);
}
else
{
k++;
s1=s2=0;
//System.out.println(k+" "+i+" "+j);
if(i==n && j==m)
break;
}
}
else if(s1>s2)
{
s2+=arr2[j++];
//System.out.println("1"+" "+s1+" "+s2+" "+i+" "+j);
}
else
{
s1+=arr1[i++];
//System.out.println("2 "+s1+" "+s2+" "+i+" "+j);
}
}
System.out.println(k);
}
}
}
class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
String nextLine() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c !=10; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
char nextChar() throws IOException{
int c;
for (c = scan(); c <= 32; c = scan());
return (char)c;
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
|
n
|
747.java
| 0.5 |
import java.util.*;
import java.io.*;
public class X
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String s1 = br.readLine();
String s2 = br.readLine();
int i=0;
char c1,c2;
int cost = 0;
while(i<n)
{
c1 = s1.charAt(i);
c2 = s2.charAt(i);
if(c1 != c2)
{
if((i+1)<n && s1.charAt(i+1) != s2.charAt(i+1) && s1.charAt(i) != s1.charAt(i+1))
{
cost +=1;
i++;
}
else
{
cost +=1;
}
}
i++;
}
System.out.println(cost);
}
}
|
n
|
750.java
| 0.5 |
/**
* BaZ :D
*/
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main
{
static Reader scan;
static PrintWriter pw;
static int n,k,left[],right[],arr[];
static long MOD = 1000000007,count[],dp[];
public static void main(String[] args) {
new Thread(null,null,"BaZ",1<<25)
{
public void run()
{
try
{
solve();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static void solve() throws IOException
{
scan = new Reader();
pw = new PrintWriter(System.out,true);
StringBuilder sb = new StringBuilder();
n = ni();
k = ni();
int stack[] = new int[1000001];
int top = -1;
arr = new int[n];
left = new int[n];
right = new int[n];
for(int i=0;i<n;++i) {
arr[i] = ni();
while(top>=0 && arr[stack[top]]<=arr[i])
top--;
if(top==-1)
left[i] = 0;
else left[i] = stack[top]+1;
stack[++top] = i;
}
top = -1;
for(int i=n-1;i>=0;--i) {
while(top>=0 && arr[stack[top]]<arr[i])
top--;
if(top==-1)
right[i] = n-1;
else right[i] = stack[top]-1;
stack[++top] = i;
}
//pa("left", left);
//pa("right", right);
dp = new long[n+1];
for(int i=0;i<=n;++i) {
if(i<k)
continue;
dp[i] = dp[i-k+1] + (i-k+1);
}
count = new long[n];
long ans = 0;
for(int i=0;i<n;++i) {
int len = right[i]-left[i]+1;
int lef = i-left[i];
int rig = right[i]-i;
long count = dp[len] - dp[lef] - dp[rig];
if(count>=MOD)
count%=MOD;
ans += count*arr[i];
if(ans>=MOD)
ans%=MOD;
}
pl(ans);
pw.flush();
pw.close();
}
static int ni() throws IOException
{
return scan.nextInt();
}
static long nl() throws IOException
{
return scan.nextLong();
}
static double nd() throws IOException
{
return scan.nextDouble();
}
static void pl()
{
pw.println();
}
static void p(Object o)
{
pw.print(o+" ");
}
static void pl(Object o)
{
pw.println(o);
}
static void psb(StringBuilder sb)
{
pw.print(sb);
}
static void pa(String arrayName, Object arr[])
{
pl(arrayName+" : ");
for(Object o : arr)
p(o);
pl();
}
static void pa(String arrayName, int arr[])
{
pl(arrayName+" : ");
for(int o : arr)
p(o);
pl();
}
static void pa(String arrayName, long arr[])
{
pl(arrayName+" : ");
for(long o : arr)
p(o);
pl();
}
static void pa(String arrayName, double arr[])
{
pl(arrayName+" : ");
for(double o : arr)
p(o);
pl();
}
static void pa(String arrayName, char arr[])
{
pl(arrayName+" : ");
for(char o : arr)
p(o);
pl();
}
static void pa(String listName, List list)
{
pl(listName+" : ");
for(Object o : list)
p(o);
pl();
}
static void pa(String arrayName, Object[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(Object o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, int[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(int o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, long[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(long o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, char[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(char o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, double[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(double o : arr[i])
p(o);
pl();
}
}
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];
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();
}
}
}
|
n
|
752.java
| 0.5 |
import java.util.*;
public class helloWorld
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[] ar = new int[200];
String str = in.next();
for(int i = 0; i < str.length(); i++)
ar[ str.charAt(i) ]++;
int ans = 100000;
for(int i = 'A'; i < 'A' + m; i++)
ans = Math.min(ans, ar[i]);
ans *= m;
System.out.println(ans);
in.close();
}
}
|
n
|
753.java
| 0.5 |
import java.util.*;
public class helloWorld
{
static ArrayList<Integer> ar;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
StringBuilder ans = new StringBuilder();
if(n <= 2)
ans.append("No");
else if(n == 4) {
ans.append("Yes\n");
ans.append("1 2\n");
ans.append("3 1 3 4\n");
}
else {
ans.append("Yes\n");
if(n % 2 == 1) {
int a = (n+1)/2;
ans.append("1 ");
ans.append(a);
ans.append("\n");
ans.append(n-1);
for(int i = 1; i < a; i++)
ans.append(" " + i);
for(int i = a+1; i <= n; i++)
ans.append(" " + i);
}
else {
int a = n/2;
ans.append("2 ");
ans.append(a + " ");
ans.append(a+1 + " \n");
ans.append(n-2);
for(int i = 1; i < a; i++)
ans.append(" " + i);
for(int i = a+2; i <= n; i++)
ans.append(" " + i);
}
}
System.out.println(ans);
in.close();
}
}
|
n
|
754.java
| 0.5 |
// LM10: The next Ballon d'or
import java.util.*;
import java.io.*;
import java.math.*;
import javafx.util.Pair;
public class Main
{
static class FastReader
{
private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public FastReader() { this(System.in); }public FastReader(InputStream is) { mIs = is;}
public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];}
public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;}
public String next(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();}
public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;}
public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;}
public double d() throws IOException {return Double.parseDouble(next()) ;}
public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
public void scanIntArr(int [] arr){ for(int li=0;li<arr.length;++li){ arr[li]=i();}}
public void scanLongArr(long [] arr){for (int i=0;i<arr.length;++i){arr[i]=l();}}
public void shuffle(int [] arr){ for(int i=arr.length;i>0;--i) { int r=(int)(Math.random()*i); int temp=arr[i-1]; arr[i-1]=arr[r]; arr[r]=temp; } }
}
public static void main(String[] args)throws IOException {
/*
inputCopy
4
2 1 2 1
outputCopy
4
inputCopy
5
0 -1 -1 -1 -1
outputCopy
4
*/
PrintWriter pw = new PrintWriter(System.out);
FastReader fr = new FastReader();
int n=fr.i();
int [] arr=new int[n];
fr.scanIntArr(arr);
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
long sum=0;
if(n==1)
{
pw.println(arr[0]);
pw.flush();
pw.close();
return;
}
for(int i=0;i<n;++i)
{
if(arr[i]<min)
min=arr[i];
if(arr[i]>max)
max=arr[i];
sum+=Math.abs(arr[i]);
}
if(min>0)
{
sum-=2*min;
}
if(max<0)
{
sum+=2*max;
}
pw.println(sum);
pw.flush();
pw.close();
}
}
|
n
|
756.java
| 0.5 |
import java.util.*;
import java.io.*;
public class FirstClass {
public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
int arr[] = new int [n];
StringTokenizer st1 = new StringTokenizer(br.readLine());
for(int i = 0 ; i < n ; i++)
{
arr[i] = Integer.parseInt(st1.nextToken());
}
int max = -1;
boolean flag = true;
for(int i = 0 ; i < n ; i++)
{
if(arr[i] > max+1)
{
flag = false;
out.println(i+1);
break;
}
else
{
max = Math.max(max, arr[i]);
}
}
if(flag)
out.println(-1);
out.flush();
out.close();
}
}
|
n
|
758.java
| 0.5 |
// O(n) solution for finding smallest subarray with sum
// greater than x
class
SmallestSubArraySum
{
// Returns length of smallest subarray with sum greater than x.
// If there is no subarray with given sum, then returns n+1
static
int
smallestSubWithSum(
int
arr[],
int
n,
int
x)
{
// Initialize current sum and minimum length
int
curr_sum =
0
, min_len = n +
1
;
// Initialize starting and ending indexes
int
start =
0
, end =
0
;
while
(end < n)
{
// Keep adding array elements while current sum
// is smaller than x
while
(curr_sum <= x && end < n)
curr_sum += arr[end++];
// If current sum becomes greater than x.
while
(curr_sum > x && start < n)
{
// Update minimum length if needed
if
(end - start < min_len)
min_len = end - start;
// remove starting elements
curr_sum -= arr[start++];
}
}
return
min_len;
}
// Driver program to test above functions
public
static
void
main(String[] args)
{
int
arr1[] = {
1
,
4
,
45
,
6
,
10
,
19
};
int
x =
51
;
int
n1 = arr1.length;
int
res1 = smallestSubWithSum(arr1, n1, x);
if
(res1 == n1+
1
)
System.out.println(
"Not Possible"
);
else
System.out.println(res1);
int
arr2[] = {
1
,
10
,
5
,
2
,
7
};
int
n2 = arr2.length;
x =
9
;
int
res2 = smallestSubWithSum(arr2, n2, x);
if
(res2 == n2+
1
)
System.out.println(
"Not Possible"
);
else
System.out.println(res2);
int
arr3[] = {
1
,
11
,
100
,
1
,
0
,
200
,
3
,
2
,
1
,
250
};
int
n3 = arr3.length;
x =
280
;
int
res3 = smallestSubWithSum(arr3, n3, x);
if
(res3 == n3+
1
)
System.out.println(
"Not Possible"
);
else
System.out.println(res3);
}
}
// This code has been contributed by Mayank Jaiswal
|
n
|
76.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;
public class B {
static ArrayList<Integer> [] adj;
static int [] num;
static int dfs(int u, int p){
int cnt = 0;
for(int v:adj[u]){
if(v != p)
cnt += dfs(v, u);
}
if(adj[u].size() == 1 && u != 0 || u == 0 && adj[0].size() == 0)
cnt++;
num[cnt]++;
return cnt;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
adj = new ArrayList[n];
for (int i = 0; i < adj.length; ++i) {
adj[i] = new ArrayList<>();
}
for (int i = 1; i < n; ++i) {
int p = sc.nextInt()-1;
adj[p].add(i);
adj[i].add(p);
}
num = new int[n+1];
dfs(0, -1);
for (int i = 1; i < num.length; ++i) {
num[i] += num[i-1];
}
int cur = 1;
for (int i = 0; i < num.length; ++i) {
while(cur <= num[i]){
out.print(i + " ");
++cur;
}
}
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
}
}
|
n
|
761.java
| 0.5 |
/*
Roses are red
Memes are neat
All my test cases time out
Lmao yeet
*/
import java.util.*;
import java.io.*;
public class A
{
public static void main(String args[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int L = Integer.parseInt(st.nextToken());
int A = Integer.parseInt(st.nextToken());
int[] arr1 = new int[N];
int[] arr2 = new int[N];
for(int i=0; i < N; i++)
{
st = new StringTokenizer(infile.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken())+x;
arr1[i] = x;
arr2[i] = y;
}
int res = 0;
for(int i=1; i < N; i++)
res += (arr1[i]-arr2[i-1])/A;
if(N > 0)
res += (L-arr2[N-1])/A+arr1[0]/A;
else
res += L/A;
System.out.println(res);
}
}
|
n
|
763.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class SequenceTransformation {
void solve() {
int p = 1, n = in.nextInt();
while (n > 0) {
if (n == 1) {
out.print(p + " ");
break;
}
if (n == 2) {
out.print(p + " ");
out.print(2 * p + " ");
break;
}
if (n == 3) {
out.print(p + " ");
out.print(p + " ");
out.print(3 * p + " ");
break;
}
for (int i = 0; i < (n + 1) / 2; i++) {
out.print(p + " ");
}
p *= 2;
n /= 2;
}
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new SequenceTransformation().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
|
n
|
765.java
| 0.5 |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in Actual solution is at the top
*
* @author MaxHeap
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CBanhMi solver = new CBanhMi();
solver.solve(1, in, out);
out.close();
}
static class CBanhMi {
long mod = (long) (1e9 + 7);
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int q = in.nextInt();
long[] two = new long[n + 1];
two[0] = 1;
for (int i = 1; i <= n; ++i) {
two[i] = (two[i - 1] * 2L);
two[i] %= mod;
}
char[] s = in.nextCharArray();
int[] acc = new int[n + 1];
for (int i = 1; i <= n; ++i) {
acc[i] = s[i - 1] == '0' ? 0 : 1;
acc[i] += acc[i - 1];
}
// 0 0 1 1 | 1: 1 1 2| 2: 2 3| 4: 5| 9
// 0 1 1 1| 1: 1 2 2| 2: 3 3| 5: 6| 11
// 0 1 1 wwqwq| 1: 1 2 2| 3: 5 3| 8: 8| 16
// 0 0 1 1| 1: 1 1 2| 3: 3 3| 6: 6| 12
// 0 0 0 1| 1: 1 1 1| 2: 2 2| 4: 4| 8
while (q-- > 0) {
int f = in.nextInt();
int t = in.nextInt();
int ones = acc[t] - acc[f - 1];
int zeros = (t - f + 1) - ones;
if (ones == 0) {
out.println(0);
} else {
long ans = two[t - f + 1] - (zeros > 0 ? two[zeros] : 0);
if (zeros == 0) {
--ans;
}
ans = (ans + mod) % mod;
out.println(ans);
}
}
}
}
static class InputReader implements FastIO {
private InputStream stream;
private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
private static final int EOF = -1;
private byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (this.numChars == EOF) {
throw new UnknownError();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException ex) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return EOF;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public String next() {
int c;
while (isSpaceChar(c = this.read())) {
}
StringBuilder result = new StringBuilder();
result.appendCodePoint(c);
while (!isSpaceChar(c = this.read())) {
result.appendCodePoint(c);
}
return result.toString();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF;
}
public char[] nextCharArray() {
return next().toCharArray();
}
}
static interface FastIO {
}
}
|
n
|
770.java
| 0.5 |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
static final long mod=(int)1e9+7;
public static void main(String[] args) throws Exception
{
FastReader in=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int n=in.nextInt();
long ans=0;
for(int i=2;2*i<=n;i++)
{
ans+=i*(n/i-1);
}
ans*=4;
pw.print(ans);
pw.flush();
}
}
class pair
{
int f,s;
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException
{
if(st==null || !st.hasMoreElements())
{
st=new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(next());
}
public long nextLong() throws IOException
{
return Long.parseLong(next());
}
}
|
n
|
771.java
| 0.5 |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.List;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* 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;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, FastReader in, PrintWriter out) {
Debug debug = new Debug();
int n = in.nextInt();
int K = in.nextInt();
List<Integer>[] g = GraphUtils.nextU(in, n, n - 1, true);
int[] center = GraphUtils.getCenter(g);
if (center.length == 2) {
out.println("No");
return;
}
int[][] pars = GraphUtils.parents3(g, center[0]);
int[] par = pars[0], ord = pars[1], depth = pars[2];
int[] deg = new int[n];
for (int i = 0; i < n; ++i) deg[i] = g[i].size();
if (deg[center[0]] < 3) {
out.println("No");
return;
}
// all leaves depth = K
for (int i = 0; i < n; ++i) {
if (deg[i] == 1) {
if (depth[i] != K) {
out.println("No");
return;
}
} else if (i != center[0]) {
if (deg[i] < 4) {
out.println("No");
return;
}
}
}
out.println("Yes");
}
}
static class GraphUtils {
public static List<Integer>[] nextU(FastReader in, int n, int m, boolean oneIndexed) {
int diff = oneIndexed ? 1 : 0;
List<Integer>[] g = new List[n];
for (int i = 0; i < n; ++i) g[i] = new ArrayList<>();
for (int i = 0; i < m; ++i) {
int u = in.nextInt() - diff;
int v = in.nextInt() - diff;
g[u].add(v);
g[v].add(u);
}
return g;
}
public static int[][] parents3(List<Integer>[] g, int root) {
int n = g.length;
int[] par = new int[n];
ArrayUtils.fill(par, -1);
int[] depth = new int[n];
depth[0] = 0;
int[] q = new int[n];
q[0] = root;
for (int p = 0, r = 1; p < r; p++) {
int cur = q[p];
for (int nex : g[cur]) {
if (par[cur] != nex) {
q[r++] = nex;
par[nex] = cur;
depth[nex] = depth[cur] + 1;
}
}
}
return new int[][]{par, q, depth};
}
public static int[] getCenter(List<Integer>[] g) {
int n = g.length;
int[] q = new int[n];
int[] deg = new int[n];
int p = 0;
for (int i = 0; i < n; i++) {
deg[i] = g[i].size();
if (g[i].size() <= 1) { // < for n=1
q[p++] = i;
}
}
int bound = p == n ? 0 : p;
for (int z = 0; z < p; z++) {
if (bound == z && p < n) bound = p;
int cur = q[z];
deg[cur]--;
for (int e : g[cur]) {
if (--deg[e] == 1) q[p++] = e;
}
}
assert p == n;
assert bound >= n - 2 && bound < n;
if (bound == n - 2) {
return new int[]{q[n - 2], q[n - 1]};
} else {
return new int[]{q[n - 1]};
}
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int pnumChars;
public FastReader(InputStream stream) {
this.stream = stream;
}
private int pread() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = pread();
while (isSpaceChar(c))
c = pread();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = pread();
}
int res = 0;
do {
if (c == ',') {
c = pread();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = pread();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
static class ArrayUtils {
public static void fill(int[] array, int value) {
Arrays.fill(array, value);
}
}
static class Debug {
PrintWriter out;
boolean oj;
boolean system;
long timeBegin;
Runtime runtime;
public Debug(PrintWriter out) {
oj = System.getProperty("ONLINE_JUDGE") != null;
this.out = out;
this.timeBegin = System.currentTimeMillis();
this.runtime = Runtime.getRuntime();
}
public Debug() {
system = true;
oj = System.getProperty("ONLINE_JUDGE") != null;
OutputStream outputStream = System.out;
this.out = new PrintWriter(outputStream);
this.timeBegin = System.currentTimeMillis();
this.runtime = Runtime.getRuntime();
}
}
}
|
n
|
772.java
| 0.5 |
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class C {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bf.readLine());
int counter = 0;
for(int i=0; i<2*n/3; i++) System.out.println("0 " + i);
for(int i=0; i<n-2*n/3; i++) System.out.println("3 " + (2*i+1));
}
}
|
n
|
773.java
| 0.5 |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Stream;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author lewin
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
ERandomForestRank solver = new ERandomForestRank();
solver.solve(1, in, out);
out.close();
}
static class ERandomForestRank {
int n;
List<Integer>[] tree;
int mod = 998244353;
long[][] dp;
public void solve(int testNumber, InputReader in, OutputWriter out) {
n = in.nextInt();
tree = LUtils.genArrayList(n);
for (int i = 0; i < n - 1; i++) {
int a = in.nextInt() - 1, b = in.nextInt() - 1;
tree[a].add(b);
tree[b].add(a);
}
dp = new long[n][];
out.println((dfs(0, -1)[2] * 2) % mod);
}
long[] dfs(int node, int par) {
long tot = 1;
long alltaken = 1;
for (int next : tree[node]) {
if (next == par) continue;
long[] r = dfs(next, node);
tot = tot * (r[0] + r[1] + r[0] + r[1]) % mod;
alltaken = alltaken * (r[1] + r[1] + r[0]) % mod;
}
long free = alltaken;
long notfree = (tot - alltaken + mod) % mod;
long sum = notfree;
for (int next : tree[node]) {
if (next == par) continue;
long mult = tot * Utils.inv((dp[next][0] + dp[next][1] + dp[next][0] + dp[next][1]) % mod, mod) % mod;
sum = (sum + 2 * mult * dp[next][2]) % mod;
}
return dp[node] = new long[]{free, notfree, sum};
}
}
static class Utils {
public static long inv(long N, long M) {
long x = 0, lastx = 1, y = 1, lasty = 0, q, t, a = N, b = M;
while (b != 0) {
q = a / b;
t = a % b;
a = b;
b = t;
t = x;
x = lastx - q * x;
lastx = t;
t = y;
y = lasty - q * y;
lasty = t;
}
return (lastx + M) % M;
}
}
static class LUtils {
public static <E> List<E>[] genArrayList(int size) {
return Stream.generate(ArrayList::new).limit(size).toArray(List[]::new);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 20];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
;
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
}
|
n
|
774.java
| 0.5 |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class CodeForces
{
public static void main(String[] args)
{
Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = input.nextInt();
String s = input.next();
boolean done = false;
for (int i = 0; i < s.length() - 1; i++)
{
if (s.charAt(i) != s.charAt(i + 1))
{
System.out.println("YES");
System.out.println(s.charAt(i) + "" + s.charAt(i + 1));
done = true;
break;
}
}
if (!done)
{
System.out.println("NO");
}
}
}
|
n
|
776.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class pr1073B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
int[] a = new int[n];
int[] b = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(st.nextToken());
}
solve(n, a, b, out);
out.flush();
out.close();
}
private static void solve(int n, int[] a, int[] b, PrintWriter out) {
boolean[] book = new boolean[n+1];
boolean f;
int j1 = 0, j2 = 0;
for (int i = 0; i < n; i++) {
f = false;
int num = b[i];
if(!book[num]) {
f = true;
j1 = j2;
for (;j2 < n; j2++) {
book[a[j2]] = true;
if (a[j2] == num) {
j2++;
break;
}
}
}
out.print(f ? j2-j1 + " ": 0 + " ");
}
}
}
|
n
|
777.java
| 0.5 |
// Java program to find maximum average
// subarray of given length.
import
java .io.*;
class
GFG {
// Returns beginning index
// of maximum average
// subarray of length 'k'
static
int
findMaxAverage(
int
[]arr,
int
n,
int
k)
{
// Check if 'k' is valid
if
(k > n)
return
-
1
;
// Create and fill array
// to store cumulative
// sum. csum[i] stores
// sum of arr[0] to arr[i]
int
[]csum =
new
int
[n];
csum[
0
] = arr[
0
];
for
(
int
i =
1
; i < n; i++)
csum[i] = csum[i -
1
] + arr[i];
// Initialize max_sm as
// sum of first subarray
int
max_sum = csum[k -
1
],
max_end = k -
1
;
// Find sum of other
// subarrays and update
// max_sum if required.
for
(
int
i = k; i < n; i++)
{
int
curr_sum = csum[i] -
csum[i - k];
if
(curr_sum > max_sum)
{
max_sum = curr_sum;
max_end = i;
}
}
// To avoid memory leak
//delete [] csum;
// Return starting index
return
max_end - k +
1
;
}
// Driver Code
static
public
void
main (String[] args)
{
int
[]arr = {
1
,
12
, -
5
, -
6
,
50
,
3
};
int
k =
4
;
int
n = arr.length;
System.out.println(
"The maximum "
+
"average subarray of length "
+ k +
" begins at index "
+ findMaxAverage(arr, n, k));
}
}
// This code is contributed by anuj_67.
|
n
|
78.java
| 0.5 |
import java.io.*;
import java.util.*;
public class CF1082D {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] aa = new int[n];
int[] i1 = new int[n];
int[] i2 = new int[n];
int n1 = 0, n2 = 0, m2 = 0;
for (int i = 0; i < n; i++) {
int a = Integer.parseInt(st.nextToken());
aa[i] = a;
if (a == 1)
i1[n1++] = i;
else {
i2[n2++] = i;
m2 += a;
}
}
if (m2 < (n2 - 1) * 2 + n1) {
System.out.println("NO");
return;
}
int m = n2 - 1 + n1;
int d = n2 - 1 + Math.min(n1, 2);
PrintWriter pw = new PrintWriter(System.out);
pw.println("YES " + d);
pw.println(m);
for (int i = 0; i + 1 < n2; i++) {
pw.println((i2[i] + 1) + " " + (i2[i + 1] + 1));
aa[i2[i]]--; aa[i2[i + 1]]--;
}
if (n1 > 0) {
while (n2 > 0 && aa[i2[n2 - 1]] == 0)
n2--;
pw.println((i2[n2 - 1] + 1) + " " + (i1[n1 - 1] + 1));
aa[i2[n2 - 1]]--;
n1--;
}
for (int i = 0, j = 0; j < n1; j++) {
while (aa[i2[i]] == 0)
i++;
pw.println((i2[i] + 1) + " " + (i1[j] + 1));
aa[i2[i]]--;
}
pw.close();
}
}
|
n
|
785.java
| 0.5 |
/**
* Created by Aminul on 3/14/2019.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import static java.lang.Math.max;
public class E_2 {
public static void main(String[] args) throws Exception {
FastReader in = new FastReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt(), k = in.nextInt(), N = (int) 5e6 + 1;
int left = 0, right = 0;
int a[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
if (a[i] == k) left++;
}
int f[] = new int[N + 1];
int ans = 0;
for (int i = n; i >= 1; i--) {
if (a[i] == k) left--;
f[a[i]]++;
f[a[i]] = max(f[a[i]], 1 + right);
ans = max(ans, f[a[i]] + left);
if (a[i] == k) right++;
}
pw.println(ans);
pw.close();
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
public FastReader(InputStream is) {
this.is = is;
}
public 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++];
}
public int nextInt() {
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();
}
}
}
}
|
n
|
786.java
| 0.5 |
import java.util.*;
import java.io.*;
public class MinimumDiameterTree{
public static void main(String[] args) {
InputReader in = new InputReader (System.in);
PrintWriter out = new PrintWriter (System.out);
int n = in.nextInt();
int s = in.nextInt();
int deg[] = new int [n];
for (int i = 1; i < n; ++i) {
deg[in.nextInt() - 1] ++;
deg[in.nextInt() - 1] ++;
}
int l = 0;
for (int i = 0; i < n; ++i)
if (deg[i] == 1) l ++;
out.println((double) 2 * s / l);
out.close();
}
public static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
n
|
789.java
| 0.5 |
// Java program to find maximum average subarray
// of given length.
import
java.io.*;
class
GFG {
// Returns beginning index of maximum average
// subarray of length 'k'
static
int
findMaxAverage(
int
arr[],
int
n,
int
k)
{
// Check if 'k' is valid
if
(k > n)
return
-
1
;
// Compute sum of first 'k' elements
int
sum = arr[
0
];
for
(
int
i =
1
; i < k; i++)
sum += arr[i];
int
max_sum = sum, max_end = k-
1
;
// Compute sum of remaining subarrays
for
(
int
i = k; i < n; i++)
{
sum = sum + arr[i] - arr[i-k];
if
(sum > max_sum)
{
max_sum = sum;
max_end = i;
}
}
// Return starting index
return
max_end - k +
1
;
}
// Driver program
public
static
void
main (String[] args)
{
int
arr[] = {
1
,
12
, -
5
, -
6
,
50
,
3
};
int
k =
4
;
int
n = arr.length;
System.out.println(
"The maximum average"
+
" subarray of length "
+ k
+
" begins at index "
+ findMaxAverage(arr, n, k));
}
}
// This code is contributed by anuj_67.
|
n
|
79.java
| 0.5 |
import java.util.*;
public class ehab3 {
public static void main( String[] args ) {
Scanner in = new Scanner( System.in );
int n = in.nextInt();
int[] a = new int[n];
for ( int i = 0; i < n; i++ )
a[i] = in.nextInt();
System.out.println( ( n + 1 ) );
int c = 0;
for ( int i = n - 1; i >= 0; i-- ) {
int cd = n + i - ( ( a[i] + c ) % n );
System.out.println( "1 " + ( i + 1 ) + " " + cd );
c += cd;
}
System.out.println( "2 " + n + " " + n );
}
}
|
n
|
793.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class EhabAndAComponentChoosingProblem {
long INF = (long) 1e18;
int n;
int[] a;
int[][] G;
void solve() {
n = in.nextInt();
a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
int[] fr = new int[n - 1], to = new int[n - 1];
for (int i = 0; i < n - 1; i++) {
fr[i] = in.nextInt() - 1;
to[i] = in.nextInt() - 1;
}
G = build_graph(n, fr, to);
int[][] ret = bfs(G, 0);
int[] par = ret[0], ord = ret[2];
long best = -INF;
long[] dp = new long[n];
for (int i = n - 1; i >= 0; i--) {
int u = ord[i];
dp[u] = a[u];
for (int v : G[u]) {
if (v != par[u]) {
if (dp[v] > 0) dp[u] += dp[v];
}
}
best = Math.max(best, dp[u]);
}
int k = 0;
for (int i = n - 1; i >= 0; i--) {
int u = ord[i];
dp[u] = a[u];
for (int v : G[u]) {
if (v != par[u]) {
if (dp[v] > 0) dp[u] += dp[v];
}
}
if (dp[u] == best) {
dp[u] = -INF;
k++;
}
}
out.printf("%d %d%n", best * k, k);
}
int[][] bfs(int[][] G, int root) {
int n = G.length;
int[] par = new int[n];
Arrays.fill(par, -1);
int[] dep = new int[n];
dep[root] = 0;
int[] qu = new int[n];
qu[0] = root;
for (int l = 0, r = 1; l < r; l++) {
int u = qu[l];
for (int v : G[u]) {
if (v != par[u]) {
qu[r++] = v;
par[v] = u;
dep[v] = dep[u] + 1;
}
}
}
return new int[][]{par, dep, qu};
}
int[][] build_graph(int n, int[] from, int[] to) {
int[][] G = new int[n][];
int[] cnt = new int[n];
for (int i = 0; i < from.length; i++) {
cnt[from[i]]++;
cnt[to[i]]++;
}
for (int i = 0; i < n; i++) G[i] = new int[cnt[i]];
for (int i = 0; i < from.length; i++) {
G[from[i]][--cnt[from[i]]] = to[i];
G[to[i]][--cnt[to[i]]] = from[i];
}
return G;
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new EhabAndAComponentChoosingProblem().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
|
n
|
795.java
| 0.5 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
long sum1=0,sum2=0;
for(int i=0;i<num*2;i++){
sum1+=sc.nextInt();
sum2+=sc.nextInt();
}
System.out.println(sum1/num+" "+sum2/num);
}
}
|
n
|
796.java
| 0.5 |
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.min;
import static java.lang.System.exit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class F {
static void solve() throws Exception {
int n = scanInt();
long l[] = new long[n];
for (int i = 0; i < n; i++) {
l[i] = scanLong();
}
long e1 = 0, e2 = 0, ans = 0;
boolean water = false;
String types = scanString();
for (int i = 0; i < n; i++) {
long li = l[i], cur;
switch (types.charAt(i)) {
case 'G':
cur = min(e1, li);
e1 -= cur;
li -= cur;
e2 += 2 * cur;
ans += 2 * cur;
e2 += li;
ans += 3 * li;
break;
case 'W':
water = true;
e1 += li;
ans += 2 * li;
break;
case 'L':
cur = min(e1, li);
e1 -= cur;
li -= cur;
ans += 2 * cur;
cur = min(e2, li);
e2 -= cur;
li -= cur;
ans += 3 * cur;
ans += (water ? 4 : 6) * li;
break;
default:
throw new AssertionError();
}
}
out.print(ans);
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static String scanString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
}
|
n
|
798.java
| 0.5 |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class UniformString {
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static StringTokenizer st;
public static void main(String[] args) throws IOException {
int t = nextInt();
for (int i = 0; i < t; i++) {
int n = nextInt();
int k = nextInt();
String s = "";
for (int j = 0; j < n; j++)
s += (char)('a' + (j%k));
System.out.println(s);
}
}
public static String nextLine() throws IOException {
return in.readLine();
}
public static String nextString() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextString());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextString());
}
public static int[] intArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public static int[][] intArray(int n, int m) throws IOException {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = nextInt();
return a;
}
public static long[] longArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
|
n
|
799.java
| 0.5 |
// JAVA implementation of left rotation
// of an array K number of times
import
java.util.*;
import
java.lang.*;
import
java.io.*;
class
arr_rot
{
// Function to leftRotate array multiple
// times
static
void
leftRotate(
int
arr[],
int
n,
int
k)
{
/* To get the starting point of
rotated array */
int
mod = k % n;
// Prints the rotated array from
// start position
for
(
int
i =
0
; i < n; ++i)
System.out.print(arr[(i + mod) % n]
+
" "
);
System.out.println();
}
// Driver program
public
static
void
main (String[] args)
{
int
arr[] = {
1
,
3
,
5
,
7
,
9
};
int
n = arr.length;
int
k =
2
;
leftRotate(arr, n, k);
k =
3
;
leftRotate(arr, n, k);
k =
4
;
leftRotate(arr, n, k);
}
}
// This code is contributed by Sanjal
|
n
|
8.java
| 0.5 |
// Java program to find number of operations
// to make an array palindrome
class
GFG
{
// Returns minimum number of count operations
// required to make arr[] palindrome
static
int
findMinOps(
int
[] arr,
int
n)
{
int
ans =
0
;
// Initialize result
// Start from two corners
for
(
int
i=
0
,j=n-
1
; i<=j;)
{
// If corner elements are same,
// problem reduces arr[i+1..j-1]
if
(arr[i] == arr[j])
{
i++;
j--;
}
// If left element is greater, then
// we merge right two elements
else
if
(arr[i] > arr[j])
{
// need to merge from tail.
j--;
arr[j] += arr[j+
1
] ;
ans++;
}
// Else we merge left two elements
else
{
i++;
arr[i] += arr[i-
1
];
ans++;
}
}
return
ans;
}
// Driver method to test the above function
public
static
void
main(String[] args)
{
int
arr[] =
new
int
[]{
1
,
4
,
5
,
9
,
1
} ;
System.out.println(
"Count of minimum operations is "
+
findMinOps(arr, arr.length));
}
}
|
n
|
80.java
| 0.5 |
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Stack;
public class D527A2 {
public static void main(String[] args) {
FastScanner in = new FastScanner(System.in);
int N = in.nextInt();
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < N; i++) {
int num = in.nextInt() % 2;
if(stack.size() >= 1 && stack.lastElement() == num)
stack.pop();
else
stack.add(num);
}
System.out.println(stack.size() <= 1 ? "YES" : "NO");
}
/**
* Source: Matt Fontaine
*/
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int chars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (chars == -1)
throw new InputMismatchException();
if (curChar >= chars) {
curChar = 0;
try {
chars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (chars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
/*
5
2 1 1 2 5
outputCopy
YES
inputCopy
3
4 5 3
outputCopy
YES
inputCopy
2
10 10
outputCopy
YES
inputCopy
3
1 2 3
outputCopy
NO
5
2 3 2 2 3
YES
*/
|
n
|
800.java
| 0.5 |
import java.io.*;
import java.util.*;
public class tr {
static int[][] ad;
static boolean []vis;
static int []goods;
static int[][]sol;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt();
int []a=new int [n];
int max=0;
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Stack<Integer> s=new Stack<>();
boolean f=true;
for(int i=0;i<n;i++) {
max=Math.max(max,a[i]);
if(!s.isEmpty() && a[i]>s.peek())
f=false;
s.push(a[i]);
while(!s.isEmpty()) {
int high=s.pop();
if(s.isEmpty() || s.peek()!=high) {
s.push(high);
break;
}
s.pop();
}
// System.out.println(s+" "+max);
}
//System.out.println(f+" "+max);
if(f && s.size()==0)
out.println("YES");
else if(f && s.size()==1 && s.peek()==max)
out.println("YES");
else
out.println("NO");
out.flush();
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static class pair implements Comparable<pair> {
int a;
int b;
pair(int a, int b) {
this.a = a;
this.b = b;
}
public String toString() {
return a + " " + b;
}
@Override
public int compareTo(pair o) {
return o.a-a ;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
n
|
801.java
| 0.5 |
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner s= new Scanner(System.in);
int n=s.nextInt();StringBuilder sb=new StringBuilder();
long[] a=new long[n/2];
for(int i=0;i<n/2;i++){
a[i]=s.nextLong();
}int j=0;long[] a2=new long[n/2];long[] a1=new long[n/2];
a1[j]=a[a.length-1]/2;
a2[j]=a[a.length-1]-a[a.length-1]/2;
for(int i=(n-1)/2-1;i>=0;i--){
// a1[j]=a[i]/2;a2[j++]=a[i]-a[i]/2;
long n1=a1[j];
if((a[i]-n1)<a2[j]){
a2[j+1]=a2[j++];a1[j]=a[i]-a2[j];
}else{a1[++j]=n1;a2[j]=a[i]-n1;}
}int k=0;//int[] ans=new int[2*n];
for(int i=(n-1)/2;i>=0;i--)
sb.append(a1[i]+" ");
for(int i=0;i<n/2;i++)
sb.append(a2[i]+" ");
System.out.println(sb.toString());
}
}
|
n
|
803.java
| 0.5 |
import java.io.*;
public class GennadyAndACardGame {
public static void main(String[] args) throws IOException {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
String line =null;
String[] n={};
String s="NO";
while((line=bufr.readLine())!=null) {
if("over".equals(line))
break;
n=bufr.readLine().split(" ");
for(int i=0;i<n.length;i++){
if(n[i].contains(line.substring(0,1)) || n[i].contains(line.substring(1))){
s="YES";
}
}
System.out.println(s);
s="NO";
bufw.flush();
}
bufw.close(); //关闭
}
}
|
n
|
806.java
| 0.5 |
// Java program to find the smallest positive value that cannot be
// represented as sum of subsets of a given sorted array
class
FindSmallestInteger
{
// Returns the smallest number that cannot be represented as sum
// of subset of elements from set represented by sorted array arr[0..n-1]
int
findSmallest(
int
arr[],
int
n)
{
int
res =
1
;
// Initialize result
// Traverse the array and increment 'res' if arr[i] is
// smaller than or equal to 'res'.
for
(
int
i =
0
; i < n && arr[i] <= res; i++)
res = res + arr[i];
return
res;
}
// Driver program to test above functions
public
static
void
main(String[] args)
{
FindSmallestInteger small =
new
FindSmallestInteger();
int
arr1[] = {
1
,
3
,
4
,
5
};
int
n1 = arr1.length;
System.out.println(small.findSmallest(arr1, n1));
int
arr2[] = {
1
,
2
,
6
,
10
,
11
,
15
};
int
n2 = arr2.length;
System.out.println(small.findSmallest(arr2, n2));
int
arr3[] = {
1
,
1
,
1
,
1
};
int
n3 = arr3.length;
System.out.println(small.findSmallest(arr3, n3));
int
arr4[] = {
1
,
1
,
3
,
4
};
int
n4 = arr4.length;
System.out.println(small.findSmallest(arr4, n4));
}
}
// This code has been contributed by Mayank Jaiswal(mayank_24)
|
n
|
81.java
| 0.5 |
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class CodeForces {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int d = in.nextInt(), sum = in.nextInt();
List<Data> data = new ArrayList<>();
for (int i = 0; i < d; ++i) {
int min = in.nextInt(), max = in.nextInt();
int centre = (min + max) / 2;
data.add(new Data(centre - min, max - centre, centre));
sum -= centre;
}
Iterator<Data> it = data.iterator();
while (sum != 0) {
if (!it.hasNext()) {
System.out.println("NO");
return;
}
Data da = it.next();
if (sum > 0) {
da.centre += da.above;
sum -= da.above;
} else {
da.centre -= da.below;
sum += da.below;
}
}
System.out.println("YES");
for (Data da : data) {
System.out.print(da.centre + " ");
}
}
}
class Data {
int below, above, centre;
Data(int below, int above, int centre) {
this.below = below;
this.above = above;
this.centre = centre;
}
}
|
n
|
814.java
| 0.5 |
// practice with rainboy
import java.io.*;
import java.util.*;
public class CF903F {
static final int INF = 0x3f3f3f3f;
static void fill(int[][][][] aa, int a) {
for (int h0 = 0; h0 <= 4; h0++)
for (int h1 = 0; h1 <= 4; h1++)
for (int h2 = 0; h2 <= 4; h2++)
for (int h3 = 0; h3 <= 4; h3++)
aa[h0][h1][h2][h3] = a;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int a1 = Integer.parseInt(st.nextToken());
int a2 = Integer.parseInt(st.nextToken());
int a3 = Integer.parseInt(st.nextToken());
int a4 = Integer.parseInt(st.nextToken());
int[] aa = new int[10];
aa[0] = aa[1] = aa[2] = aa[3] = a1;
aa[4] = aa[5] = aa[6] = a2;
aa[7] = aa[8] = a3;
aa[9] = a4;
int[][] ww = new int[10][4];
ww[0][0] = 1;
ww[1][1] = 1;
ww[2][2] = 1;
ww[3][3] = 1;
ww[4][0] = ww[4][1] = 2;
ww[5][1] = ww[5][2] = 2;
ww[6][2] = ww[6][3] = 2;
ww[7][0] = ww[7][1] = ww[7][2] = 3;
ww[8][1] = ww[8][2] = ww[8][3] = 3;
ww[9][0] = ww[9][1] = ww[9][2] = ww[9][3] = 4;
char[][] cc = new char[4][n + 8];
for (int k = 0; k < 4; k++) {
char[] c_ = cc[k];
br.readLine().getChars(0, n, c_, 4);
c_[0] = c_[1] = c_[2] = c_[3]
= c_[n + 4] = c_[n + 5] = c_[n + 6] = c_[n + 7] = '.';
}
int[][][][] dp = new int[5][5][5][5];
int[][][][] dq = new int[5][5][5][5];
fill(dp, INF);
dp[4][4][4][4] = 0;
int[] hh = new int[4];
for (int i = 0; i < n + 4; i++) {
for (int h0 = 0; h0 <= 4; h0++)
for (int h1 = 0; h1 <= 4; h1++)
for (int h2 = 0; h2 <= 4; h2++)
for (int h3 = 0; h3 <= 4; h3++)
for (int s = 0; s < 10; s++) {
hh[0] = h0;
hh[1] = h1;
hh[2] = h2;
hh[3] = h3;
for (int k = 0; k < 4; k++) {
int h = ww[s][k];
if (hh[k] < h) {
while (h < 4 && cc[k][i + h] == '.')
h++;
hh[k] = h;
}
}
int x = dp[h0][h1][h2][h3] + aa[s];
if (dp[hh[0]][hh[1]][hh[2]][hh[3]] > x)
dp[hh[0]][hh[1]][hh[2]][hh[3]] = x;
}
fill(dq, INF);
for (int h0 = 1; h0 <= 4; h0++) {
hh[0] = h0 < 4 || cc[0][i + 4] == '*' ? h0 - 1 : 4;
for (int h1 = 1; h1 <= 4; h1++) {
hh[1] = h1 < 4 || cc[1][i + 4] == '*' ? h1 - 1 : 4;
for (int h2 = 1; h2 <= 4; h2++) {
hh[2] = h2 < 4 || cc[2][i + 4] == '*' ? h2 - 1 : 4;
for (int h3 = 1; h3 <= 4; h3++) {
hh[3] = h3 < 4 || cc[3][i + 4] == '*' ? h3 - 1 : 4;
int x = dp[h0][h1][h2][h3];
if (dq[hh[0]][hh[1]][hh[2]][hh[3]] > x)
dq[hh[0]][hh[1]][hh[2]][hh[3]] = x;
}
}
}
}
int[][][][] tmp = dp; dp = dq; dq = tmp;
}
System.out.println(dp[4][4][4][4]);
}
}
|
n
|
820.java
| 0.5 |
import java.util.Scanner;
public class GenerateLogin {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
char last = b.charAt(0);
String ans = ""+a.charAt(0);
for(int i = 1;i<a.length();i++){
if(a.charAt(i)>=last)break;
ans+=a.charAt(i);
}
ans+=last;
System.out.println(ans);
}
}
|
n
|
824.java
| 0.5 |
import java.util.*;
import java.io.*;
public class A{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans = 0;
for(int i = 1; i <= n; i++){
ans += ((i*2) <= n) ? i : n-i+1;
}
System.out.println(ans);
}
}
|
n
|
825.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class D909 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] line = br.readLine().toCharArray();
int n = line.length;
int l = 0;
ArrayList<Node> groups = new ArrayList<>();
Node node = new Node(line[0], 1);
groups.add(node);
for (int i = 1; i < n; i++) {
if (line[i] == groups.get(l).letter) {
groups.get(l).count++;
} else {
node = new Node(line[i], 1);
groups.add(node);
l++;
}
}
int moves = 0;
ArrayList<Node> temp = new ArrayList<>();
while (groups.size() > 1) {
moves++;
l = groups.size();
groups.get(0).count--;
groups.get(l - 1).count--;
for (int i = 1; i < l - 1; i++) {
groups.get(i).count -= 2;
}
int p;
for (p = 0; p < l; p++) {
if (groups.get(p).count > 0) {
temp.add(groups.get(p));
break;
}
}
int lTemp = temp.size();
for (p++; p < l; p++) {
if (groups.get(p).count <= 0) {
continue;
}
if (groups.get(p).letter == temp.get(lTemp - 1).letter) {
temp.get(lTemp - 1).count += groups.get(p).count;
} else {
temp.add(groups.get(p));
lTemp++;
}
}
groups.clear();
groups.addAll(temp);
temp.clear();
}
System.out.println(moves);
}
private static class Node {
char letter;
int count;
Node(char letter, int count) {
this.letter = letter;
this.count = count;
}
}
}
|
n
|
827.java
| 0.5 |
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int n = in.nextInt(), l[] = new int[n],
p = -1000000, d_min = 1000000, min = 1000000000;
for(int i = 0; i < n; i++) {
l[i] = in.nextInt();
min = Math.min(min, l[i]);
}
for(int i = 0; i < n; i++)
if(l[i] == min) {
d_min = Math.min(d_min, i - p);
p = i;
}
out.print(d_min);
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public String next() throws IOException {
if(!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
while(!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
String r = st.nextToken("\n");
st = new StringTokenizer(br.readLine(), " ");
return r;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
|
n
|
828.java
| 0.5 |
import java.io.*;
import java.util.*;
import java.util.Collections;
import java.util.Arrays;
public class Codechef {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=sc.nextInt();
int b=sc.nextInt();
int res=0;
for(int i=1;i<n;i++)
res=Math.max(res, Math.min(a/i, b/(n-i) ) );
System.out.println(res);
}
}
|
n
|
829.java
| 0.5 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] P = new int[n];
int[] check=new int[n];
for (int i = 1; i < n; i++) {
P[i] = scanner.nextInt();
P[i]--;
check[P[i]]++;
}
int[] leaves = new int[n];
for (int i=0;i<n;i++) {
if(check[i]==0){
leaves[P[i]]++;
}
}
for (int i = 0; i < n; i++) {
if (check[i]>0&&leaves[i]<3) {
System.out.println("No");
return;
}
}
System.out.println("Yes");
}
}
|
n
|
836.java
| 0.5 |
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import static java.lang.Math.ceil;
public class practise {
static final int N = 31;
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out,true);
static long c[] = new long[N];
static int w[] = new int[N];
public static void main(String[] args) {
w[0] = 1;
for(int i=1;i<N;i++) w[i] = w[i-1]*2;
int n = sc.nextInt(); int l = sc.nextInt();
for(int i=0;i<N;i++) c[i] = Long.MAX_VALUE;
for(int i=0;i<n;i++) c[i] = sc.nextInt();
for(int i=1;i<N;i++) {
c[i] = min(c[i-1]*2,c[i]);
}
long ans = 0;
for(int i=0;i<N;i++) {
if( (l&w[i])!=0) {
ans += c[i];
}
}
long m = 0;
for(int i=N - 1;i>=0;i--) {
if((l&w[i])!=0) {
m += c[i];
} else {
ans = min(ans,m+c[i]);
}
}
out.println(ans);
}
static long min(long a,long b) {
return a<b?a:b;
}
}
|
n
|
837.java
| 0.5 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C {
public static int mod = 1000000000 + 7;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String n = br.readLine();
int k = Integer.parseInt(br.readLine());
int l = n.length();
if(k == 0) {
System.out.println(1);
}else {
int max = 1000;
if (l <= 10) {
max = Integer.min(1000, Integer.parseInt(n, 2));
}
int[] steps = new int[max + 1];
for (int i = 2; i <= max; i++) {
int ones = numberOfOnes(i);
steps[i] = 1 + steps[ones];
}
if (l <= 10) {
int ans = 0;
for (int i = 1; i <= max; i++) {
if (steps[i] == k) {
ans++;
}
}
System.out.println(ans);
} else {
int[][] C = binomial(max);
int ans = 0;
int count = 0;
for (int i = 0; i < l; i++) {
if (n.charAt(i) == '1') {
for (int j = count; j < max; j++) {
if (steps[j] == k - 1) {
ans = (ans + C[l - i - 1][j - count]) % mod;
if (i == 0 && k == 1) {
ans = (ans + mod - 1) % mod;
}
}
}
count++;
}
}
int ones = 0;
for (int i = 0; i < l; i++) {
if (n.charAt(i) == '1') {
ones++;
}
}
if (steps[ones] == k-1) {
ans = (ans + 1) % mod;
}
System.out.println(ans);
}
}
}
public static int numberOfOnes(int x) {
char[] s = Integer.toBinaryString(x).toCharArray();
int count = 0;
for (char c : s) {
if (c == '1') {
count++;
}
}
return count;
}
public static int[][] binomial(int n) {
int[][] C = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) {
C[i][j] = ((C[i - 1][j - 1] % mod) + (C[i - 1][j] % mod)) % mod;
}
}
return C;
}
}
|
n
|
841.java
| 0.5 |
import java.util.*;
public class Main{
public static void main(String [] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
char c[]=new char[n];
Arrays.fill(c,'o');
int x=1,y=1,z;
while(y<=n)
{
c[y-1]='O';
z=x+y;
x=y;
y=z;
}
String ans=new String(c);
System.out.println(ans);
}
}
|
n
|
846.java
| 0.5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.