filename_1
stringlengths 8
15
| filename_2
stringlengths 8
12
| code_1
stringlengths 591
24.4k
| code_2
stringlengths 591
35.2k
| labels
int64 0
1
| notes
stringclasses 2
values |
---|---|---|---|---|---|
51151974 | c23278ec | import java.io.*;
import java.util.*;
public class Solution{
public static void main (String[] args) throws java.lang.Exception {
FastReader sc = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int testCase = sc.nextInt();
while (testCase-->0){
int n = sc.nextInt();
String[] strArr = new String[n];
for(int i=0; i<n; i++) {
strArr[i]=sc.nextLine();
}
int[] total = new int[5];
ArrayList<int[]> al = new ArrayList<>();
for(int i=0; i<n; i++){
int[] arr= new int[5];
for(int j=0; j<strArr[i].length(); j++){
arr[strArr[i].charAt(j)-'a']++;
}
for(int j=0; j<5; j++){
total[j]+=arr[j];
}
al.add(arr);
}
int ans=0;
for(int i=0; i<5; i++){
ArrayList<Integer> all = new ArrayList<>();
for(int j=0; j<n; j++){
all.add(strArr[j].length()-2*al.get(j)[i]);
}
java.util.Collections.sort(all);
int c=0, d=0;
for(int j=0; j<n; j++){
c+=all.get(j);
if(c<0) d=j+1;
}
ans = Math.max(ans,d);
}
System.out.println(ans);
}
}
// Fast Reader Class
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; }
}
} | import java.io.*;
import java.sql.Array;
import java.util.*;
public class P1551C {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
static class Task {
public void solve(InputReader in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
String[] s = new String[n];
int[][] freq = new int[n][6];
for (int i = 0; i < n; i++) {
s[i] = in.nextLine();
for (char c : s[i].toCharArray()) {
freq[i][c - 'a']++;
freq[i][5]++;
}
}
int totmax = 0;
for (int i = 0; i < 5; i++) {
List<Integer> diffs = new ArrayList<>(n);
for (int j = 0; j < n; j++) {
diffs.add(2 * freq[j][i] - freq[j][5]);
}
Collections.sort(diffs);
Collections.reverse(diffs);
int curr = 0, cnt = 0;
while (cnt < diffs.size() && curr + diffs.get(cnt) > 0) {
curr += diffs.get(cnt++);
}
totmax = Math.max(totmax, cnt);
}
out.println(totmax);
}
}
}
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());
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
}
}
| 0 | Non-plagiarised |
13441e8f | 141effef | import java.util.*;
public class Armchairs {
public static int findMinTime(List<Integer> zeros, List<Integer> ones) {
if (ones.size() == 0)
return 0;
int oneSize = ones.size();
int zeroSize = zeros.size();
int [][] time = new int [oneSize + 1][zeroSize + 1];
for (int i=1; i<=oneSize; i++) {
time[i][i] = time[i - 1][i - 1] + Math.abs(ones.get(i - 1) - zeros.get(i - 1));
for (int j=i+1; j<=zeroSize; j++) {
time[i][j] = Math.min(time[i][j - 1], time[i - 1][j - 1] +
Math.abs(ones.get(i - 1) - zeros.get(j - 1)));
}
}
return time[oneSize][zeroSize];
}
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> zeros = new ArrayList<>();
List<Integer> ones = new ArrayList<>();
for (int i=0; i<n; i++) {
int number= sc.nextInt();
if (number == 1)
ones.add(i);
else
zeros.add(i);
}
System.out.println(findMinTime(zeros, ones));
}
} | // package codeforces;
import java.util.*;
public class ArmChairs {
static int[]arr;
static ArrayList<Integer>a;
static ArrayList<Integer>b;
static int dp[][];
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=scn.nextInt();
}
dp=new int[n+1][n+1];
a =new ArrayList<>();
b =new ArrayList<>();
for(int i=0;i<n;i++) {
if(arr[i]==0) {
a.add(i);
}else{
b.add(i);
}
}
System.out.println(solve(0,0));
}
public static int solve(int i,int j) {
if(i==b.size()) {
return 0;
}
if(j==a.size()) {
return 100000000;
}
if(dp[i][j]!=0) {
return dp[i][j];
}
int x=Math.abs(a.get(j)-b.get(i))+solve(i+1,j+1);
int y=solve(i,j+1);
return dp[i][j]=Math.min(x, y);
}
}
| 0 | Non-plagiarised |
115c99cb | 921b6e4a | import java.io.*;
import java.util.*;
public class D_Java {
public static final int MOD = 998244353;
public static int mul(int a, int b) {
return (int)((long)a * (long)b % MOD);
}
int[] f;
int[] rf;
public int C(int n, int k) {
return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k]));
}
public static int pow(int a, int n) {
int res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
static void shuffleArray(int[] a) {
Random rnd = new Random();
for (int i = a.length-1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
int tmp = a[index];
a[index] = a[i];
a[i] = tmp;
}
}
public static int inv(int a) {
return pow(a, MOD-2);
}
public void doIt() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer(in.readLine());
int n = Integer.parseInt(tok.nextToken());
int k = Integer.parseInt(tok.nextToken());
f = new int[n+42];
rf = new int[n+42];
f[0] = rf[0] = 1;
for (int i = 1; i < f.length; ++i) {
f[i] = mul(f[i-1], i);
rf[i] = mul(rf[i-1], inv(i));
}
int[] events = new int[2*n];
for (int i = 0; i < n; ++i) {
tok = new StringTokenizer(in.readLine());
int le = Integer.parseInt(tok.nextToken());
int ri = Integer.parseInt(tok.nextToken());
events[i] = le*2;
events[i + n] = ri*2 + 1;
}
shuffleArray(events);
Arrays.sort(events);
int ans = 0;
int balance = 0;
for (int r = 0; r < 2*n;) {
int l = r;
while (r < 2*n && events[l] == events[r]) {
++r;
}
int added = r - l;
if (events[l] % 2 == 0) {
// Open event
ans += C(balance + added, k);
if (ans >= MOD) ans -= MOD;
ans += MOD - C(balance, k);
if (ans >= MOD) ans -= MOD;
balance += added;
} else {
// Close event
balance -= added;
}
}
in.close();
System.out.println(ans);
}
public static void main(String[] args) throws IOException {
(new D_Java()).doIt();
}
} | import java.io.*;
import java.util.*;
public class D_Java {
public static final int MOD = 998244353;
public static int mul(int a, int b) {
return (int)((long)a * (long)b % MOD);
}
int[] f;
int[] rf;
public int C(int n, int k) {
return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k]));
}
public static int pow(int a, int n) {
int res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
static void shuffleArray(int[] a) {
Random rnd = new Random();
for (int i = a.length-1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
int tmp = a[index];
a[index] = a[i];
a[i] = tmp;
}
}
public static int inv(int a) {
return pow(a, MOD-2);
}
public void doIt() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer(in.readLine());
int n = Integer.parseInt(tok.nextToken());
int k = Integer.parseInt(tok.nextToken());
f = new int[n+42];
rf = new int[n+42];
f[0] = rf[0] = 1;
for (int i = 1; i < f.length; ++i) {
f[i] = mul(f[i-1], i);
rf[i] = mul(rf[i-1], inv(i));
}
int[] events = new int[2*n];
for (int i = 0; i < n; ++i) {
tok = new StringTokenizer(in.readLine());
int le = Integer.parseInt(tok.nextToken());
int ri = Integer.parseInt(tok.nextToken());
events[i] = le*2;
events[i + n] = ri*2 + 1;
}
shuffleArray(events);
Arrays.sort(events);
int ans = 0;
int balance = 0;
for (int r = 0; r < 2*n;) {
int l = r;
while (r < 2*n && events[l] == events[r]) {
++r;
}
int added = r - l;
if (events[l] % 2 == 0) {
// Open event
ans += C(balance + added, k);
if (ans >= MOD) ans -= MOD;
ans += MOD - C(balance, k);
if (ans >= MOD) ans -= MOD;
balance += added;
} else {
// Close event
balance -= added;
}
}
in.close();
System.out.println(ans);
}
public static void main(String[] args) throws IOException {
(new D_Java()).doIt();
}
} | 1 | Plagiarised |
cdb801a1 | e14d1ba0 | //package codeforces;
import java.io.*;
import java.util.*;
public class Solution {
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 ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
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) throws java.lang.Exception {
FastReader fr = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = fr.ni();
while(t-->0) {
int n = fr.ni();
long arr [] = new long[n];
for(int i = 0 ; i < n ; i++) arr[i]= fr.nl();
long even = arr[0];
long odd = arr[1];
long minEven = arr[0];
long minOdd = arr[1];
long ans = (minEven*n) + (minOdd*n);
for(int i = 2 ; i < n ; i++) {
if((i&1) == 0) {
even += arr[i];
minEven = Math.min(minEven, arr[i]);
long a = (i+2)/2;
long b = (i+1)-a;
long temp = (even + (minEven*(n - a)));
temp += (odd + (minOdd*(n - b)));
ans = Math.min(ans, temp);
}else {
odd += arr[i];
minOdd = Math.min(minOdd, arr[i]);
long a = (i+2)/2;
long b = (i+1)-a;
long temp = (even + (minEven*(n - a)));
temp += (odd + (minOdd*(n - b)));
ans = Math.min(ans, temp);
}
}
out.println(ans);
}
out.close();
}
} | import java.util.*;
public class ss
{
public static void main(String[]args)
{
Scanner in=new Scanner (System.in);
int t=in.nextInt();
for(int i1=0;i1<t;i1++)
{
int n=in.nextInt();
long[] ar=new long[n];
for(int i=0;i<n;i++)
{
ar[i]=in.nextLong();
}
long[] ans=new long[n];
ans[0]=ar[0]*n;
long m=ar[0];
long s1=ar[0];
for(int i=2;i<n;i+=2)
{
if(m>ar[i])
{
m=ar[i];
}
s1+=ar[i];
ans[i]=s1-m+m*(n-i/2);
}
ans[1]=ar[1]*n;
m=ar[1];
s1=ar[1];
for(int i=3;i<n;i+=2)
{
if(m>ar[i])
{
m=ar[i];
}
s1+=ar[i];
ans[i]=s1-m+m*(n-i/2);
}
long mini=ans[0]+ans[1];
for(int i=1;i<n-1;i++)
{
mini=Math.min(ans[i]+ans[i+1],mini);
}
System.out.println(mini);
}
}
}
| 0 | Non-plagiarised |
01b911ac | a195911e | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.TreeMap;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Stack;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.Iterator;
public class First {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int T = fs.nextInt();
for (int tt = 0; tt < T; tt++) {
solve(fs);
}
}
static void solve(FastScanner fs)
{
int n=fs.nextInt();
long[] times=takeLong(n, fs);
long[] damage=takeLong(n, fs);
long reqTime=times[n-1]-damage[n-1]+1;
long ans=0;
for(int i=n-1;i>=0;)
{
reqTime=times[i]-damage[i]+1;
long time=times[i];
i--;
while(i>=0 && times[i]>=reqTime)
{
long thisReqTime=times[i]-damage[i]+1;
reqTime=Math.min(reqTime, thisReqTime);
i--;
}
long x=time-reqTime+1;
// pn(x);
ans+=(x*(x+1)/2);
}
pn(ans);
}
static long MOD=(long)(1e9+7);
static boolean check(String x, String ans)
{
if(x.length()!=ans.length()) return x.length()>ans.length();
if(x.compareTo(ans) <= -1) return false;
else return true;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static void pn(Object o) { System.out.println(o); }
static void p(Object o) { System.out.print(o); }
static void flush() { System.out.flush(); }
static void debugInt(int[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugIntInt(int[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static void debugLong(long[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugLongLong(long[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static long[] takeLong(int n, FastScanner fs)
{
long[] arr=new long[n];
for(int i=0;i<n;++i)
arr[i]=fs.nextLong();
return arr;
}
static long[][] takeLongLong(int m, int n, FastScanner fs)
{
long[][] arr=new long[m][n];
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
arr[i][j]=fs.nextLong();
return arr;
}
static int[] takeInt(int n, FastScanner fs)
{
int[] arr=new int[n];
for(int i=0;i<n;++i)
arr[i]=fs.nextInt();
return arr;
}
static int[][] takeIntInt(int m, int n, FastScanner fs)
{
int[][] arr=new int[m][n];
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
arr[i][j]=fs.nextInt();
return arr;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static int getDecimalFromBinary(String s)
{
long no=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='1')
no++;
no=no<<1;
}
return (int)(no>>1);
}
static String getBinaryFromDecimal(int x)
{
StringBuilder sb=new StringBuilder();
for(int i=0;i<31;++i)
{
if((x&1)==0)
sb.append('0');
else
sb.append('1');
x=x>>1;
}
return sb.reverse().toString();
}
static boolean[] sieve()
{
int size=(int)(1e5+5);
boolean[] isPrime=new boolean[size];
Arrays.fill(isPrime, true);
for(int i=2;i<size;++i)
for(int j=2*i;isPrime[i]==true && j<size;j+=i)
isPrime[j]=false;
return isPrime;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
class SortComp implements Comparator<String>
{
public int compare(String s1, String s2)
{
if(s1.length()!=s2.length()) return s2.length()-s1.length();
if(s1.compareTo(s2) <= -1) return 1;
else return -1;
}
} | import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc=new FastReader();
static int dp[];
static boolean v[];
// static int mod=998244353;;
static int mod=1000000007;
static int max;
static int bit[];
//static long fact[];
// static long A[];
static HashMap<Integer,Integer> map;
//static StringBuffer sb=new StringBuffer("");
//static HashMap<Integer,Integer> map;
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
outer :while (ttt-- > 0)
{
int n=i();
long A[]=inputL(n);
long B[]=inputL(n);
long C[]=new long[n];
for(int i=0;i<n;i++) {
C[i]=A[i]-B[i]+1;
}
long min=C[n-1];
long ans=0;
long last=A[n-1];
for(int i=n-1;i>=0;i--) {
if(C[i]>min) {
continue;
}
if(A[i]<min) {
long y=last-min+1;
ans+=y*(y+1)/2;
last=A[i];
min=C[i];
continue;
}
min=C[i];
}
long y=last-min+1;
ans+=y*(y+1)/2;
System.out.println(ans);
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return A[a]=find(A, A[a]);
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return find(A, A[a]);
//}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
static void add(int v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(int v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
public static int upper(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void print(int A[]) {
for(int i : A) {
out.print(i+" ");
}
out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static HashMap<Long,Integer> hash(long A[]){
HashMap<Long,Integer> map=new HashMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
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;
}
}
}
| 0 | Non-plagiarised |
4dc0247e | 875ed4c8 | import java.io.*;
import java.util.*;
public class ComdeFormces {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int t=sc.nextInt();
while(t--!=0) {
int n=sc.nextInt();
// int m=sc.nextInt();
ArrayList<ArrayList<pair>> ar=new ArrayList<>();
ArrayList<pair> arr=new ArrayList<>();
int c[]=new int[n];
for(int i=0;i<=n;i++) {
ar.add(new ArrayList<>());
}
for(int i=0;i<n-1;i++) {
int a=sc.nextInt();
int b=sc.nextInt();
ar.get(a).add(new pair(b,i));
ar.get(b).add(new pair(a,i));
}
boolean ans=true;
for(int i=0;i<=n;i++) {
if(ar.get(i).size()>2) {
ans=false;
break;
}
}
if(ans) {
for(int i=1;i<=n;i++) {
if(ar.get(i).size()==1) {
boolean vis[]=new boolean[n+1];
dfs(ar,2,i,vis,c);
break;
}
}
for(int i=0;i<n;i++) {
if(c[i]!=0)log.write(c[i]+" ");
}
}
else log.write("-1");
log.write("\n");
log.flush();
}
}
static void dfs(ArrayList<ArrayList<pair>>ar ,int val,int src,boolean vis[],int c[]) {
if(vis[src])return;
vis[src]=true;
for(int i=0;i<ar.get(src).size();i++) {
if(!vis[ar.get(src).get(i).a]) {
c[ar.get(src).get(i).b]=val;
if(val==2) {
dfs(ar,3,ar.get(src).get(i).a,vis,c);
}
else {
dfs(ar,2,ar.get(src).get(i).a,vis,c);
}
}
}
}
static ArrayList<Integer> divisors(int n){
ArrayList<Integer> ar=new ArrayList<>();
for (int i=2; i<=Math.sqrt(n); i++){
if (n%i == 0){
if (n/i == i) {
ar.add(i);
}
else {
ar.add(i);
ar.add(n/i);
}
}
}
return ar;
}
static int primeDivisor(int n){
ArrayList<Integer> ar=new ArrayList<>();
int cnt=0;
boolean pr=false;
while(n%2==0) {
pr=true;
n/=2;
}
if(pr)ar.add(2);
for(int i=3;i*i<=n;i+=2) {
pr=false;
while(n%i==0) {
n/=i;
pr=true;
}
if(pr)ar.add(i);
}
if(n>2) ar.add(n);
return ar.size();
}
static int mdlg(int num, int base) {
int val=((int)(Math.ceil(Math.log(num)/Math.log(base))));
int val2=(int)Math.pow(base,val);
// System.out.println(num+" "+Math.pow(base,val));
if(num/val2==0)return val;
return val+1;
// return val;
}
static String rev(String s) {
char temp[]=s.toCharArray();
for(int i=0;i<temp.length/2;i++) {
char tp=temp[i];
temp[i]=temp[temp.length-1-i];
temp[temp.length-1-i]=tp;
}
return String.valueOf(temp);
}
static long bsv(long a[],long min,long max) {
long ans=-1;
while(min<=max) {
long mid=min+(max-min)/2;
long an=solvess(a,mid);
// System.out.println("an="+an+"mid="+mid);
if(an==mid) {
ans=Math.max(ans,an);
min=mid+1;
}
else if(an>mid)min=mid+1;
else if(an<mid)max=mid-1;
}
return ans;
}
static long solvess(long aa[],long el) {
long a[]=new long[aa.length];
long min=Long.MAX_VALUE;
for(int i=0;i<a.length;i++ ) {
a[i]=aa[i];
min=Math.min(min, aa[i]);
}
boolean ans=true;
long val[]=new long[a.length];
for(int i=a.length-1;i>=2;i--) {
if(a[i]+val[i]>el) {
if(val[i]>=el) {
long df=a[i];
a[i]-=df;
val[i-1]+=(df/3);
val[i-2]+=2*(df/3);
a[i]+=df%3;
}
else {
long df=a[i]+val[i]-el;
val[i-1]+=(df/3);
val[i-2]+=2*(df/3);
a[i]-=df;
a[i]+=df%3;
}
}
}
for(int i=0;i<a.length;i++) {
a[i]+=val[i];
}
long min2=Long.MAX_VALUE;
for(int i=0;i<a.length;i++) {
min2=Math.min(min2, a[i]);
}
return min2;
}
static int bsd(ArrayList<LinkedList<Integer>> arr, int val,int el) {
int s=0;
int e=arr.size()-1;
int ptr=0;
if(val==1)ptr=1;
while(s<=e) {
int m=s+(e-s)/2;
if(arr.get(m).get(ptr)==el)return m;
else if(arr.get(m).get(ptr)<el)s=m+1;
else e=m-1;
}
return -1;
}
static int bs(ArrayList<pair> arr,int el) {
int start=0;
int end=arr.size()-1;
while(start<=end) {
int mid=start+(end-start)/2;
if(arr.get(mid).a==el)return mid;
else if(arr.get(mid).a<el)start=mid+1;
else end=mid-1;
}
if(start>arr.size()-1)return -2;
return -1;
}
static String eval(String s, long bs, long be,long k) {
String ans="";
for(int i=s.length()-1;i>=0;i--) {
if(s.charAt(i)=='a') {
ans="a"+ans;
}
else if(be>0){
if(be>k) {
for(int j=0;j<k;j++)ans="b"+ans;
be-=k;
}
else {
for(int j=0;j<be;j++)ans="b"+ans;
be=0;
}
}
}
for(int i=0;i<bs;i++)ans="b"+ans;
return ans;
}
static int[][] slv() {
int r=200001;
int bits=(int)(Math.log(r)/Math.log(2))+1;
int cnt[][]=new int[r+1][bits];
for(int i=1;i<=r;i++) {
int temp=i;
int j=0;
int min=Integer.MAX_VALUE;
while(j<bits) {
int tval=1&temp;
temp>>=1;
if(tval==0)cnt[i][j]=cnt[i-1][j]+1;
else {
cnt[i][j]=cnt[i-1][j];
}
j++;
}
}
return cnt;
}
static long find(int s,long a[]) {
if(s>=a.length)return -1;
long num=a[s];
for(int i=s;i<a.length;i+=2) {
num=gcd(num,a[i]);
if(num==1 || num==0)return -1;
}
return num;
}
static long gcd(long a,long b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static long factmod(long n,long mod,long img) {
if(n==0)return 1;
long ans=1;
long temp=1;
while(n--!=0) {
if(temp!=img) {
ans=((ans%mod)*((temp)%mod))%mod;
}
temp++;
}
return ans%mod;
}
static long eval(ArrayList<Integer> a, int k) {
if(a.size()<1 )return 0;
int i=a.size()-1;
long ans=0;
while(i>=0) {
int temp=k;
int prev=0;
while(temp--!=0 && i>=0) {
ans+=Math.abs(a.get(i)-prev);
if(temp==0 || i==0)ans+=a.get(i);
prev=a.get(i);
i--;
}
}
return ans;
}
static int bs(long a[] ,long num) {
int start=0;
int end=a.length-1;
while(start<=end) {
int mid=start+(end-start)/2;
if(a[mid]==num) {
return mid;
}
else if(a[mid]<num)start=mid+1;
else end=mid-1;
}
return start;
}
//utils
static int ncr(int n, int r){
if(r>n-r)r=n-r;
int ans=1;
for(int i=0;i<r;i++){
ans*=(n-i);
ans/=(i+1);
}
return ans;
}
public static class trip{
int a,b,c;
public trip(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
public int compareTo(trip q) {
return this.c-q.c;
}
}
static int mergesort(int[] a,int start,int end) {
if(start>=end)return a[end];
int mid=start+(end-start)/2;
int min1=mergesort(a,start,mid);
int min2=mergesort(a,mid+1,end);
merge(a,start,mid,end);
return Math.min(min1, min2);
}
static void merge(int []a, int start,int mid,int end) {
int ptr1=start;
int ptr2=mid+1;
int b[]=new int[end-start+1];
int i=0;
while(ptr1<=mid && ptr2<=end) {
if(a[ptr1]<=a[ptr2]) {
b[i]=a[ptr1];
ptr1++;
i++;
}
else {
b[i]=a[ptr2];
ptr2++;
i++;
}
}
while(ptr1<=mid) {
b[i]=a[ptr1];
ptr1++;
i++;
}
while(ptr2<=end) {
b[i]=a[ptr2];
ptr2++;
i++;
}
for(int j=start;j<=end;j++) {
a[j]=b[j-start];
}
}
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
int a;
int b;
public pair(int a,int b) {
this.a=a;
this.b=b;
}
public int compareTo(pair b) {
return b.b-this.b;
}
public int compareToo(pair b) {
if(this.a!=b.a)return this.a-b.a;
else {
return b.b-this.b;
}
}
}
static long pow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
static int pow(int a, int pw) {
int temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
}
| import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0){
solve(sc);
}
}
public static void solve(FastReader sc){
int n = sc.nextInt();
ArrayList<ArrayList<Edge>> graph = new ArrayList<ArrayList<Edge>>();
for(int i = 0;i<n;++i){
graph.add(new ArrayList<>());
}
for(int i = 0;i<n-1;++i){
int u = sc.nextInt();
int v = sc.nextInt();
u--;
v--;
graph.get(u).add(new Edge(v, i));
graph.get(v).add(new Edge(u, i));
}
int start = 0;
for(int i = 0;i<n;++i){
if(graph.get(i).size()>2){
out.println(-1);out.flush();return;
}else if(graph.get(i).size()==1){
start=i;
}
}
int val=2;
int [] wgt = new int[n-1];
int curr = graph.get(start).get(0).node;
wgt[graph.get(start).get(0).idx] = val;
val=5;
while(true){
ArrayList<Edge> list = graph.get(curr);
if(list.size()==1){
break;
}else{
for(Edge el : list){
if(wgt[el.idx]==0){
wgt[el.idx] = val;
val = 7-val;
curr = el.node;
}
}
}
}
for(int el : wgt){
out.print(el + " ");
}
out.println();
out.flush();
}
static class Edge {
int node;
int idx;
Edge(int src, int nbr) {
this.node = src;
this.idx = nbr;
}
}
/*
int [] arr = new int[n];
for(int i = 0;i<n;++i){
arr[i] = sc.nextInt();
}
*/
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
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;
}
}
} | 0 | Non-plagiarised |
476e94d3 | 9310ad0c | import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class code2{
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
//@SuppressWarnings("unchecked")
public static void main(String[] arg) throws IOException{
//Reader in=new Reader();
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner(System.in);
int t=in.nextInt();
while(t-- > 0){
int n=in.nextInt();
long[] k=new long[n];
long[] h=new long[n];
for(int i=0;i<n;i++) k[i]=in.nextLong();
for(int i=0;i<n;i++) h[i]=in.nextLong();
long[] res=new long[n];
long[] power=new long[n];
long l=k[n-1]+1,r=-1;
for(int i=0;i<n;i++){
long lc=k[i]-h[i]+1;
//out.println("lc:"+lc);
long m=h[i];
if(lc<=l){
res[i]=(m*(m+1))/2;
power[i]=h[i];
}
else if(lc>r){
//out.println(m);
res[i]=res[i-1]+(m*(m+1))/2;
power[i]=h[i];
}
else{
res[i]=Long.MAX_VALUE;
for(int j=i-1;j>=0;j--){
if(power[j]+k[i]-k[j] >= h[i]){
long x=power[j]+k[i]-k[j];
if(x>=h[i]){
if(k[i]-h[i]+1>k[j]){
power[i]=h[i];
res[i]=res[j]+(m*(m+1))/2;
}
else{
power[i]=x;
long c=(x*(x+1))/2;
long d=(power[j]*(power[j]+1))/2;
res[i]=res[j]+c-d;
}
break;
}
}
}
}
l=Math.min(l,lc);
r=k[i];
}
out.println(res[n-1]);
}
out.flush();
}
}
| import java.io.*;
import java.util.*;
public class Main{
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) throws Exception {
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
long k[]=new long[n];
for(int i=0;i<n;i++)
{
k[i]=sc.nextLong();
}
long h[]=new long[n];
for(int i=0;i<n;i++)
{
h[i]=sc.nextLong();
}
ArrayList<Long> al=new ArrayList<>();
long csp=h[n-1],idx=k[n-1]-h[n-1];
for(int i=n-2;i>=0;i--)
{
if(idx<k[i])
{
if(k[i]-idx<h[i])
{
long diff=h[i]-(k[i]-idx);
csp+=diff;
idx-=diff;
}
}
else
{
al.add(csp);
csp=h[i];
idx=k[i]-csp;
}
}
long sum=0;
al.add(csp);
for(long i:al)
{
sum=sum+((i*(i+1))/2);
}
System.out.println(sum);
}
}
}
| 0 | Non-plagiarised |
1dab88fb | bac616ee | import java.util.*;
public class Main
{
static class Edge{
public int node;
public int index;
public Edge(int n, int i){
node=n;
index=i;
}
}
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int test=sc.nextInt();
while(test-->0){
solve();
}
}
static void solve(){
int n=sc.nextInt();
ArrayList<ArrayList<Edge>> graph= new ArrayList<ArrayList<Edge>>();
for(int i=0;i<n;i++){
graph.add(new ArrayList<>());
}
for (int i = 0; i < n - 1; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
u--; v--;
graph.get(u).add(new Edge(v, i));
graph.get(v).add(new Edge(u, i));
}
int start = 0;
for (int i = 0; i < n; i++) {
if (graph.get(i).size() > 2) {
System.out.println("-1");
return;
} else if (graph.get(i).size() == 1) {
start = i;
}
}
int[] weight = new int[n - 1];
int prevNode = -1;
int curNode = start;
int curWeight = 2;
while (true) {
ArrayList<Edge> edges = graph.get(curNode);
Edge next = edges.get(0);
if (next.node == prevNode) {
if (edges.size() == 1) {
break;
} else {
next = edges.get(1);
}
}
weight[next.index] = curWeight;
prevNode = curNode;
curNode = next.node;
curWeight = 5 - curWeight;
}
for (int i = 0; i < n - 1; i++) {
System.out.print(weight[i]);
System.out.print(" ");
}
System.out.println();
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class NotAssigning {
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 nextFloat() {
return Float.parseFloat(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextArray(int n) {
int[] a = new int[n];
for (int i=0; i<n; i++) {
a[i] = this.nextInt();
}
return a;
}
long[] nextArrayLong(int n) {
long[] a = new long[n];
for (int i=0; i<n; i++) {
a[i] = this.nextLong();
}
return a;
}
}
static class Pair {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
static boolean vis[];
public static void dfs(ArrayList<ArrayList<Pair>> t, int cur, boolean mode, int[] w) {
vis[cur] = true;
for (Pair p : t.get(cur)) {
if (!vis[p.a]) {
if (mode) {
w[p.b] = 3;
}
else {
w[p.b] = 2;
}
dfs(t, p.a, !mode, w);
}
}
}
public static void solve(int n, int[] u, int[] v) {
ArrayList<ArrayList<Pair>> t = new ArrayList<ArrayList<Pair>>(n);
for (int i=0; i<n; i++) {
t.add(new ArrayList<Pair>());
}
for (int i=0; i<n-1; i++) {
t.get(u[i]).add(new Pair(v[i], i));
t.get(v[i]).add(new Pair(u[i], i));
}
int start = 0;
for (int i=0; i<n; i++) {
if (t.get(i).size() > 2) {
System.out.println("-1");
return;
}
if (t.get(i).size() == 1) {
start = i;
}
}
vis = new boolean[n];
int[] w = new int[n-1];
dfs(t, start, false, w);
StringBuilder ans = new StringBuilder();
for (int i=0; i<n-1; i++) {
ans.append(w[i]).append(" ");
}
System.out.println(ans);
}
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int[] u = new int[n-1];
int[] v = new int[n-1];
for (int i=0; i<n-1; i++) {
u[i] = in.nextInt()-1;
v[i] = in.nextInt()-1;
}
solve(n, u, v);
}
}
}
| 0 | Non-plagiarised |
3368f340 | d8a171a3 | //package Codeforces;
import java.io.*;
import java.util.*;
public class Menorah {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-->0){
int n = sc.nextInt();
char[] a = sc.next().toCharArray();
char[] b = sc.next().toCharArray();
int a1=0, b1=0;
for(int i=0;i<n;i++){
if(a[i]=='1')
a1++;
if(b[i]=='1')
b1++;
}
int min = 100000000;
if(a1==b1){
int c = 0;
for(int i=0;i<n;i++){
if(a[i]!=b[i])
c++;
}
min = Math.min(min, c);
}
if(b1==(n-a1+1)){
int ind = -1;
for(int i=0;i<n;i++){
if(a[i]==b[i] && a[i]=='1'){
ind = i;
break;
}
}
int c = 0;
for(int i=0;i<n;i++){
if(i==ind)
continue;
if(a[i]==b[i])
c++;
}
min = Math.min(min, c + 1);
}
if(min == 100000000)
sb.append("-1\n");
else sb.append(min).append("\n");
}
System.out.println(sb);
sc.close();
}
}
| import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args)throws IOException {
FastScanner scan = new FastScanner();
PrintWriter output = new PrintWriter(System.out);
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt();
char initial[] = scan.next().toCharArray();
char desired[] = scan.next().toCharArray();
int lit1 = 0, lit2 = 0;
int ans = Integer.MAX_VALUE;
for(int i = 0;i<n;i++) {
if(initial[i]=='1') lit1++;
if(desired[i]=='1') lit2++;
}
if(lit1==lit2) {
int count = 0;
for(int i = 0;i<n;i++) if(initial[i]!=desired[i]) count++;
ans = Math.min(ans, count);
}
if(lit2==(n-lit1+1)) {
int count = 0;
for(int i = 0;i<n;i++) if(initial[i]==desired[i]) count++;
ans = Math.min(ans, count);
}
if(ans == Integer.MAX_VALUE) ans = -1;
output.println(ans);
}
output.flush();
}
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr) list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) arr[i] = list.get(i);
return arr;
}
public static int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b%a, a);
}
public static void printArray(int arr[]) {
for(int i:arr) System.out.print(i+" ");
System.out.println();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| 0 | Non-plagiarised |
51cc7026 | e81b2d16 |
import java.util.*;
import java.math.*;
import java.io.*;
import java.lang.*;
public class C_Balanced_Stone_Heaps {
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;
}
int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(next());
}
return a;
}
}
public static int t, n;
public static int H[];
private static long startTime = System.currentTimeMillis();
public static void main(String[] args) {
FastReader sc =new FastReader();
t = sc.nextInt();
while (t-->0) {
n = sc.nextInt();
H = new int[n];
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
H[i] = sc.nextInt();
max = Math.max(H[i], max);
min = Math.min(H[i], min);
}
int mid = 0;
while (min < max) {
mid = min + (max-min+1)/2;
if(check(mid)) {
min = mid;
}
else max = mid-1;
}
System.out.println(min);
}
}
public static boolean check(int x){
int S[] = Arrays.copyOf(H, H.length);
for (int i = n-1; i >= 2; i--) {
if(S[i]<x) return false;
int move = Math.min(S[i]-x, H[i])/3;
if(i>=2){
// S[i]-=move*3;
S[i-1]+=(move);
S[i-2]+=2*(move);
}
}
return S[0]>= x && S[1] >= x;
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class fastTemp {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int n;
static int arr[];
public static void main(String[] args)
{
FastReader fs = new FastReader();
int t = fs.nextInt();
while(t-- >0){
n = fs.nextInt();
arr = new int[n];
int max = Integer.MIN_VALUE;
for(int i=0;i<n;i++){
arr[i] = fs.nextInt();
if(max<arr[i]){
max =arr[i];
}
}
int l=1;
int r = max;
int ans = 0;
while(l<r){
int mid = l + (r-l+1)/2;
if(check(mid)){
l = mid;
}else{
r = mid-1;
}
}
System.out.println(l);
}
}
static int min = Integer.MAX_VALUE;
public static boolean check(int x){
int ar[] = new int[n];
for(int i=0;i<n;i++){
ar[i] = arr[i];
}
// Collections.copy(curr,arr);
for(int i=n-1;i>=2;i--){
if(ar[i]<x){
return false;
}
int d = (Math.min(arr[i],ar[i]-x))/3;
ar[i-1] += d;
ar[i-2] += 2*d;
}
return ar[0]>=x && ar[1]>=x;
}
} | 1 | Plagiarised |
0df4050e | d9199dfd | import java.io.*;
import java.util.*;
public class MainClass {
public static void main(String[] args) {
Reader in = new Reader(System.in);
int t = in.nextInt();
StringBuilder stringBuilder = new StringBuilder();
while (t-- > 0) {
ArrayList<Integer> reds = new ArrayList<>();
ArrayList<Integer> blue = new ArrayList<>();
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt() - 1;
}
char[] s = in.next().toCharArray();
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
reds.add(a[i]);
} else {
blue.add(a[i]);
}
}
Collections.sort(reds, Collections.reverseOrder());
Collections.sort(blue);
boolean ff = true;
int start = 0;
for (int i = 0; i < blue.size(); i++) {
if (blue.get(i) < start) {
ff = false;
break;
}
start++;
}
start = n - 1;
for (int i = 0; i < reds.size(); i++) {
if (reds.get(i) > start) {
ff = false;
break;
}
start--;
}
stringBuilder.append(ff?"YES":"NO").append("\n");
}
System.out.println(stringBuilder);
}
}
class Reader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Reader(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());
}
} | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class Simple{
public static void main(String args[]){
//System.out.println("Hello Java");
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t>0){
int n = s.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = s.nextInt();
}
String str = s.next();
//Arrays.sort(arr);
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int i=0;i<n;i++){
if(str.charAt(i)=='R'){
red.add(arr[i]);
}
else{
blue.add(arr[i]);
}
}
Collections.sort(red);
Collections.sort(blue);
int start =1;
boolean bool =true;
for(int i=0;i<blue.size();i++){
if(blue.get(i)<start){
bool = false;
break;
}
start++;
}
if(!bool){
System.out.println("NO");
}
else{
for(int i=0;i<red.size();i++){
if(red.get(i)>start){
bool = false;
break;
}
start++;
}
if(bool){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
t--;
}
s.close();
}
}
| 1 | Plagiarised |
df594a00 | fcbe7917 | import java.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
while(cases-- > 0) {
br.readLine();
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int[] a = new int[k];
int[] t = new int[k];
str = br.readLine().split(" ");
for(int i=0; i<k; i++) {
a[i] = Integer.parseInt(str[i]) - 1;
}
str = br.readLine().split(" ");
for(int i=0; i<k; i++) {
t[i] = Integer.parseInt(str[i]);
}
int[] temp = new int[n];
Arrays.fill(temp, Integer.MAX_VALUE);
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, Integer.MAX_VALUE);
Arrays.fill(right, Integer.MAX_VALUE);
int ind = 0;
for(int i=0; i<k; i++) {
left[a[i]] = t[i];
right[a[i]] = t[i];
}
int minleft = Integer.MAX_VALUE;
for(int i=0; i<n; i++) {
left[i] = Math.min(left[i], minleft);
minleft = left[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : left[i]+1;
}
int minright = Integer.MAX_VALUE;
for(int i=n-1; i>=0; i--) {
right[i] = Math.min(right[i], minright);
minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1;
}
for(int i=0; i<n; i++) {
temp[i] = Math.min(right[i], left[i]);
System.out.print(temp[i]+" ");
}
System.out.println();
}
}
} | import java.io.*;
import java.util.*;
public class E_Air_Conditioners{
public static void main(String Args[]) throws Exception{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
int t= Integer.parseInt(br.readLine());
StringTokenizer st;
while(t-->0){
String gap=br.readLine();
st=new StringTokenizer(br.readLine());
int n= Integer.parseInt(st.nextToken());
int k= Integer.parseInt(st.nextToken());
int pos[]=new int[k];
st=new StringTokenizer(br.readLine());
for(int i=0;i<k;i++){
pos[i]=Integer.parseInt(st.nextToken())-1;
}
int temp[]=new int[k];
st=new StringTokenizer(br.readLine());
int ans[]=new int[n];
int l[]=new int[n];
int r[]=new int[n];
Arrays.fill(ans,Integer.MAX_VALUE);
for(int i=0;i<k;i++){
temp[i]=Integer.parseInt(st.nextToken());
ans[pos[i]]=temp[i];
}
int min=Integer.MAX_VALUE;
for(int i=0;i<n;i++){
if(min==Integer.MAX_VALUE){
min=ans[i];
}
else{
min=Math.min(min+1,ans[i]);
}
l[i]=min;
}
min=Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
if(min==Integer.MAX_VALUE){
min=ans[i];
}
else{
min=Math.min(min+1,ans[i]);
}
r[i]=min;
}
for(int i=0;i<n;i++){
System.out.print(Math.min(l[i],r[i])+" ");
}
System.out.println();
}
}
}
| 0 | Non-plagiarised |
3ff60986 | d0b4b96c | import java.io.*;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int N = (int)2e5 +10;
static ArrayList<ArrayList<Integer>> adj= new ArrayList<>(N);
static
{
for(int i=0; i<N; i++)
{
adj.add(new ArrayList<>());
}
}
static int[][] a = new int[2][N];
static long[][] dp = new long[2][N];
static void dfs(int v, int p){
dp[0][v] = 0;
dp[1][v]=0;
for(int u: adj.get(v))
{
if(u!=p) {
dfs(u, v);
dp[0][v] += Math.max(Math.abs(a[0][v] - a[1][u]) + dp[1][u], Math.abs(a[0][v] - a[0][u]) + dp[0][u]);
dp[1][v] += Math.max(Math.abs(a[1][v] - a[1][u]) + dp[1][u], Math.abs(a[1][v] - a[0][u]) + dp[0][u]);
}
}
}
public static void main(String[] args) throws IOException{
// write your code here
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
for(int i=1; i<=n; i++)
{
a[0][i] = sc.nextInt();
a[1][i] = sc.nextInt();
adj.set(i, new ArrayList<>());
}
for(int i=1; i<n; i++)
{
int u = sc.nextInt();
int v = sc.nextInt();
adj.get(u).add(v);
adj.get(v).add(u);
}
dfs(1, 0);
System.out.println(Math.max(dp[0][1], dp[1][1]));
}
}
}
| import java.io.*;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int N = (int)2e5 +10;
static ArrayList<ArrayList<Integer>> adj= new ArrayList<>(N);
static
{
for(int i=0; i<N; i++)
{
adj.add(new ArrayList<>());
}
}
static int[][] a = new int[2][N];
static long[][] dp = new long[2][N];
static void dfs(int v, int p){
dp[0][v] = 0;
dp[1][v]=0;
for(int u: adj.get(v))
{
if(u!=p) {
dfs(u, v);
dp[0][v] += Math.max(Math.abs(a[0][v] - a[1][u]) + dp[1][u], Math.abs(a[0][v] - a[0][u]) + dp[0][u]);
dp[1][v] += Math.max(Math.abs(a[1][v] - a[1][u]) + dp[1][u], Math.abs(a[1][v] - a[0][u]) + dp[0][u]);
}
}
}
public static void main(String[] args) throws IOException{
// write your code here
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
for(int i=1; i<=n; i++)
{
a[0][i] = sc.nextInt();
a[1][i] = sc.nextInt();
adj.set(i, new ArrayList<>());
}
for(int i=1; i<n; i++)
{
int u = sc.nextInt();
int v = sc.nextInt();
adj.get(u).add(v);
adj.get(v).add(u);
}
dfs(1, 0);
System.out.println(Math.max(dp[0][1], dp[1][1]));
}
}
}
| 1 | Plagiarised |
1230c43e | ce0b2178 | //package com.company;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static boolean[] primecheck = new boolean[1000002];
public static void main(String[] args) throws IOException {
OutputStream outputStream = System.out;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t = 1;
t = in.nextInt();
for (int i = 0; i < t; i++) {
solver.solve(in, out);
}
out.close();
}
static class PROBLEM {
public void solve(FastReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.readArray(n);
char[] c = in.nextLine().toCharArray();
int cur = 1;
ArrayList<Pair> p = new ArrayList<>();
for (int i = 0; i < n; i++) {
p.add(new Pair(c[i], a[i]));
}
Collections.sort(p);
// for (int i = 0; i < n; i++) {
// out.println(p.get(i).x + " " + p.get(i).y);
// }
for (int i = 0; i < n; i++) {
if(p.get(i).x == 'B' && p.get(i).y < i+1){
out.println("NO");
return;
}
if(p.get(i).x == 'R' && p.get(i).y > i+1){
out.println("NO");
return;
}
}
out.println("YES");
// int n = in.nextInt(), m = in.nextInt();
// char[] s = in.nextLine().toCharArray();
//
// int rl = 0, ud = 0, r = 0 , l = 0, rlf = 0, udf = 0;
// int lmax = 0, rmax = 0, umax = 0, dmax = 0;
//
// for (int i = 0; i < s.length; i++) {
// if(s[i] == 'L'){
// if(rlf == 0) rlf = -1;
// rl--;
// l--;
// if(rl == 0) l = 0;
// if(rl < 0 && lmax+rmax <= m) lmax = Math.max(Math.abs(l), lmax);
// }
// if(s[i] == 'R'){
// if(rlf == 0) rlf = 1;
// rl++;
// r++;
//
// if(rl > 0 && lmax+rmax <= m) rmax = Math.max(r, rmax);
// }
// if(s[i] == 'U'){
// if(udf == 0) udf = 1;
// ud++;
// r = Math.max(Math.abs(ud), r);
// if(ud > 0 && umax+dmax <= n) umax = Math.max(Math.abs(ud), umax);
// }
// if(s[i] == 'D'){
// if(udf == 0) udf = -1;
// ud--;
// r = Math.max(Math.abs(ud), r);
// if(ud < 0 && umax+dmax <= n) dmax = Math.max(Math.abs(ud), dmax);
// }
// }
//
// int ansc = 0, ansr = 0;
//
// out.println(rlf + " lmx = " + lmax + " rmax" + rmax);
//
// if(rlf == 1) ansc = m-rmax;
// else if(rlf == -1) ansc = 1+lmax;
// else ansc = 1;
//
// if(udf == 1) ansr = 1+umax;
// else if(udf == -1) ansr = m-dmax;
// else ansr = 1;
//
// out.println(ansr + " " + ansc);
}
}
static HashMap<Integer, Integer> initializeMap(int n){
HashMap<Integer,Integer> hm = new HashMap<>();
for (int i = 0; i <= n; i++) {
hm.put(i, 0);
}
return hm;
}
static boolean isRegular(char[] c){
Stack<Character> s = new Stack<>();
for (char value : c) {
if (s.isEmpty() && value == ')') return false;
if (value == '(') s.push(value);
else s.pop();
}
return s.isEmpty();
}
static ArrayList<ArrayList<Integer>> createAdj(int n, int e){
FastReader in = new FastReader();
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n + 1; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < e; i++) {
int a = in.nextInt(), b = in.nextInt();
System.out.println(a);
adj.get(a).add(b);
adj.get(b).add(a);
}
return adj;
}
static int binarySearch(int[] a, int l, int r, int x){
if(r>=l){
int mid = l + (r-l)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) return binarySearch(a, l, mid-1, x);
else return binarySearch(a,mid+1, r, x);
}
return -1;
}
static boolean isPalindromeI(int n){
int d = 0;
int y = n;
while(y>0){
d++;
y/=10;
}
int[] a = new int[d];
for (int i = 0; i < d; i++) {
a[i] = n%10;
n/=10;
}
System.out.println(Arrays.toString(a));
for (int i = 0; i < d / 2; i++) {
if(a[i] != a[d-i-1]) return false;
}
return true;
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static boolean isSquare(double a) {
boolean isSq = false;
double b = Math.sqrt(a);
double c = Math.sqrt(a) - Math.floor(b);
if (c == 0) isSq = true;
return isSq;
}
static int exponentMod(int A, int B, int C) {
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (int) ((y + C) % C);
}
static class Pair implements Comparable<Pair>{
char x;
int y;
Pair(char x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
static class Tuple implements Comparable<Tuple>{
int x, y, id;
Tuple(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
public int compareTo(Tuple o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
// public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
// public U x;
// public V y;
//
// public Pair(U x, V y) {
// this.x = x;
// this.y = y;
// }
//
// public int hashCode() {
// return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
// }
//
// public boolean equals(Object o) {
// if (this == o)
// return true;
// if (o == null || getClass() != o.getClass())
// return false;
// Pair<U, V> p = (Pair<U, V>) o;
// return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
// }
//
// public int compareTo(Pair<U, V> b) {
// int cmpU = x.compareTo(b.x);
// return cmpU != 0 ? cmpU : y.compareTo(b.y);
// }
//
// public int compareToY(Pair<U, V> b) {
// int cmpU = y.compareTo(b.y);
// return cmpU != 0 ? cmpU : x.compareTo(b.x);
// }
//
// public String toString() {
// return String.format("(%s, %s)", x.toString(), y.toString());
// }
//
// }
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
boolean nextBoolean() {
return !(nextInt() == 0);
}
// boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] readArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = nextInt();
return array;
}
}
private static int[] mergeSort(int[] array) {
//array.length replaced with ctr
int ctr = array.length;
if (ctr <= 1) {
return array;
}
int midpoint = ctr / 2;
int[] left = new int[midpoint];
int[] right;
if (ctr % 2 == 0) {
right = new int[midpoint];
} else {
right = new int[midpoint + 1];
}
for (int i = 0; i < left.length; i++) {
left[i] = array[i];
}
for (int i = 0; i < right.length; i++) {
right[i] = array[i + midpoint];
}
int[] result = new int[array.length];
left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);
return result;
}
private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length || rightPointer < right.length) {
if (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
} else if (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
return result;
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
}
| import java.io.*;
import java.util.*;
public class D {
static class Pair implements Comparable<Pair>{
int a;
char b;
public Pair(int a, char b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair p) {
if(this.b == p.b)
return this.a - p.a;
return this.b - p.b;
}
}
public static void main(String[] args)throws IOException {
FastScanner scan = new FastScanner();
PrintWriter output = new PrintWriter(System.out);
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt();
int arr[] = scan.readArray(n);
char line[] = scan.next().toCharArray();
ArrayList<Pair> pairs = new ArrayList<Pair>();
for(int i = 0;i<n;i++) {
pairs.add(new Pair(arr[i], line[i]));
}
Collections.sort(pairs);
boolean possible = true;
for(int i = 1;i<=n;i++) {
if(pairs.get(i-1).a == i) {
continue;
}
else if(pairs.get(i-1).a < i && pairs.get(i-1).b == 'R') {
continue;
}
else if(pairs.get(i-1).a > i && pairs.get(i-1).b == 'B') {
continue;
}
else {
possible = false;
break;
}
}
output.println(possible == true ? "YES" : "NO");
}
output.flush();
}
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr) list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) arr[i] = list.get(i);
return arr;
}
public static int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b%a, a);
}
static boolean isPrime(int n) {
if (n <= 1) return false;
else if (n == 2) return true;
else if (n % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) if (n % i == 0) return false;
return true;
}
public static void printArray(int arr[]) {
for(int i:arr) System.out.print(i+" ");
System.out.println();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| 0 | Non-plagiarised |
9f354c5c | a6532df9 |
import java.util.*;
import java.io.*;
import java.math.*;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Solution {
private static class MyScanner {
private static final int BUF_SIZE = 2048;
BufferedReader br;
private MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
private boolean isSpace(char c) {
return c == '\n' || c == '\r' || c == ' ';
}
String next() {
try {
StringBuilder sb = new StringBuilder();
int r;
while ((r = br.read()) != -1 && isSpace((char)r));
if (r == -1) {
return null;
}
sb.append((char) r);
while ((r = br.read()) != -1 && !isSpace((char)r)) {
sb.append((char)r);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static class Reader{
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long mod = (long)(1e9 + 7);
static void sort(long[] arr ) {
ArrayList<Long> al = new ArrayList<>();
for(long e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static void sort(int[] arr ) {
ArrayList<Integer> al = new ArrayList<>();
for(int e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static void sort(char[] arr) {
ArrayList<Character> al = new ArrayList<Character>();
for(char cc:arr) al.add(cc);
Collections.sort(al);
for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i);
}
static long mod_mul( long... a) {
long ans = a[0]%mod;
for(int i = 1 ; i<a.length ; i++) {
ans = (ans * (a[i]%mod))%mod;
}
return ans;
}
static long mod_sum( long... a) {
long ans = 0;
for(long e:a) {
ans = (ans + e)%mod;
}
return ans;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] prime(int num) {
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
if(num >= 0) {
bool[0] = false;
bool[1] = false;
}
return bool;
}
static long modInverse(long a, long m)
{
long g = gcd(a, m);
return power(a, m - 2, m);
}
static long lcm(long a , long b) {
return (a*b)/gcd(a, b);
}
static int lcm(int a , int b) {
return (int)((a*b)/gcd(a, b));
}
static long power(long x, long y, long m){
if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m);
if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); }
static class Combinations{
private long[] z;
private long[] z1;
private long[] z2;
private long mod;
public Combinations(long N , long mod) {
this.mod = mod;
z = new long[(int)N+1];
z1 = new long[(int)N+1];
z[0] = 1;
for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod;
z2 = new long[(int)N+1];
z2[0] = z2[1] = 1;
for (int i = 2; i <= N; i++)
z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod;
z1[0] = z1[1] = 1;
for (int i = 2; i <= N; i++)
z1[i] = (z2[i] * z1[i - 1]) % mod;
}
long fac(long n) {
return z[(int)n];
}
long invrsNum(long n) {
return z2[(int)n];
}
long invrsFac(long n) {
return invrsFac((int)n);
}
long ncr(long N, long R)
{ if(R<0 || R>N ) return 0;
long ans = ((z[(int)N] * z1[(int)R])
% mod * z1[(int)(N - R)])
% mod;
return ans;
}
}
static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
void makeSet()
{
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int find(int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void union(int x, int y)
{
int xRoot = find(x), yRoot = find(y);
if (xRoot == yRoot)
return;
if (rank[xRoot] < rank[yRoot])
parent[xRoot] = yRoot;
else if (rank[yRoot] < rank[xRoot])
parent[yRoot] = xRoot;
else
{
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + 1;
}
}
}
static int max(int... a ) {
int max = a[0];
for(int e:a) max = Math.max(max, e);
return max;
}
static long max(long... a ) {
long max = a[0];
for(long e:a) max = Math.max(max, e);
return max;
}
static int min(int... a ) {
int min = a[0];
for(int e:a) min = Math.min(e, min);
return min;
}
static long min(long... a ) {
long min = a[0];
for(long e:a) min = Math.min(e, min);
return min;
}
static int[] KMP(String str) {
int n = str.length();
int[] kmp = new int[n];
for(int i = 1 ; i<n ; i++) {
int j = kmp[i-1];
while(j>0 && str.charAt(i) != str.charAt(j)) {
j = kmp[j-1];
}
if(str.charAt(i) == str.charAt(j)) j++;
kmp[i] = j;
}
return kmp;
}
/************************************************ Query **************************************************************************************/
/***************************************** Sparse Table ********************************************************/
static class SparseTable{
private long[][] st;
SparseTable(long[] arr){
int n = arr.length;
st = new long[n][25];
log = new int[n+2];
build_log(n+1);
build(arr);
}
private void build(long[] arr) {
int n = arr.length;
for(int i = n-1 ; i>=0 ; i--) {
for(int j = 0 ; j<25 ; j++) {
int r = i + (1<<j)-1;
if(r>=n) break;
if(j == 0 ) st[i][j] = arr[i];
else st[i][j] = Math.min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] );
}
}
}
public long gcd(long a ,long b) {
if(a == 0) return b;
return gcd(b%a , a);
}
public long query(int l ,int r) {
int w = r-l+1;
int power = log[w];
return Math.min(st[l][power],st[r - (1<<power) + 1][power]);
}
private int[] log;
void build_log(int n) {
log[1] = 0;
for(int i = 2 ; i<=n ; i++) {
log[i] = 1 + log[i/2];
}
}
}
/******************************************************** Segement Tree *****************************************************/
/**
static class SegmentTree{
long[] tree;
long[] arr;
int n;
SegmentTree(long[] arr){
this.n = arr.length;
tree = new long[4*n+1];
this.arr = arr;
buildTree(0, n-1, 1);
}
void buildTree(int s ,int e ,int index ) {
if(s == e) {
tree[index] = arr[s];
return;
}
int mid = (s+e)/2;
buildTree( s, mid, 2*index);
buildTree( mid+1, e, 2*index+1);
tree[index] = Math.min(tree[2*index] , tree[2*index+1]);
}
long query(int si ,int ei) {
return query(0 ,n-1 , si ,ei , 1 );
}
private long query( int ss ,int se ,int qs , int qe,int index) {
if(ss>=qs && se<=qe) return tree[index];
if(qe<ss || se<qs) return (long)(1e17);
int mid = (ss + se)/2;
long left = query( ss , mid , qs ,qe , 2*index);
long right= query(mid + 1 , se , qs ,qe , 2*index+1);
return Math.min(left, right);
}
public void update(int index , int val) {
arr[index] = val;
for(long e:arr) System.out.print(e+" ");
update(index , 0 , n-1 , 1);
}
private void update(int id ,int si , int ei , int index) {
if(id < si || id>ei) return;
if(si == ei ) {
tree[index] = arr[id];
return;
}
if(si > ei) return;
int mid = (ei + si)/2;
update( id, si, mid , 2*index);
update( id , mid+1, ei , 2*index+1);
tree[index] = Math.min(tree[2*index] ,tree[2*index+1]);
}
}
*/
/* ***************************************************************************************************************************************************/
// static MyScanner sc = new MyScanner(); // only in case of less memory
static Reader sc = new Reader();
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) throws IOException {
int tc = 1;
tc = sc.nextInt();
for(int i = 1 ; i<=tc ; i++) {
// sb.append("Case #" + i + ": " ); // During KickStart && HackerCup
TEST_CASE();
}
System.out.println(sb);
}
static void TEST_CASE() {
int n = sc.nextInt();
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i =0 ; i<n ; i++) adj.add(new ArrayList<>());
int[] U = new int[n-1] , V = new int[n-1];
for(int i = 0 ; i<n-1 ; i++) {
int u = sc.nextInt()-1 , v = sc.nextInt()-1;
U[i] = u; V[i] = v;
adj.get(u).add(v);
adj.get(v).add(u);
}
int ind = -1;
for(int i =0 ; i<n ; i++) {
if(adj.get(i).size()>2) {
sb.append("-1\n");
return;
}
if(adj.get(i).size() == 1) {
ind = i;
}
}
Map<String , Integer> map = new HashMap<>();
dfs(adj, ind, -1, true, map);
for(int i =0 ; i<n-1 ; i++) {
int u = U[i];
int v = V[i];
if(map.containsKey(u+" "+v)) sb.append(map.get(u+" "+v)+" ");
else sb.append(map.get(v+" "+u)+" ");
}
sb.append("\n");
}
static void dfs(ArrayList<ArrayList<Integer>> adj ,int u , int p ,boolean eve ,Map<String , Integer> map ) {
if(eve) {
map.put(u+" "+p, 2);
}else {
map.put(u+" "+p, 3);
}
for(int v:adj.get(u)) {
if(v == p) continue;
eve = !eve;
dfs(adj, v, u, eve, map);
}
}
}
/*******************************************************************************************************************************************************/
/**
1
*/
| import java.io.*;
import java.util.*;
public class new1{
public static void dfs(ArrayList<ArrayList<int[]> > aList1, int p, int u, int[] ans, int v) {
int[] aa = aList1.get(u).get(0);
if(aa[0] != p) {
ans[aa[1]] = v;
if(v == 2) dfs(aList1, u, aa[0], ans, 3);
else dfs(aList1, u, aa[0], ans, 2);
}
else if(aList1.get(u).size() > 1){
aa = aList1.get(u).get(1);
ans[aa[1]] = v;
if(v == 2) dfs(aList1, u, aa[0], ans, 3);
else dfs(aList1, u, aa[0], ans, 2);
}
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
ArrayList<ArrayList<int[]> > aList1 = new ArrayList<ArrayList<int[]> >(n + 1);
for (int j = 1; j <= n + 1; j++) {
ArrayList<int[]> list = new ArrayList<>();
aList1.add(list);
}
for(int j = 0; j < n - 1; j ++) {
int u = s.nextInt();
int v = s.nextInt();
int[] aa = {v, j};
int[] bb = {u, j};
aList1.get(u).add(aa);
aList1.get(v).add(bb);
}
int pos = 1; int ind = -1;
for(int i = 1; i <= n; i++) {
if(aList1.get(i).size() > 2) {
pos = 0;
break;
}
if(aList1.get(i).size() == 1) {
ind = i;
}
}
if(pos == 0) {
output.write(-1 + "\n");
}
else {
int[] ans = new int[n - 1];
dfs(aList1, -1, ind, ans, 2);
for(int i = 0; i < n - 1; i++) {
output.write(ans[i] + " ");
}
output.write("\n");
}
}
output.flush();
}
}
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();
}
public 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;
}} | 0 | Non-plagiarised |
8703709f | d2901569 | import java.io.*;
import java.util.*;
public class Main {
// static boolean[] prime = new boolean[10000000];
final static long mod = 1000000007;
public static void main(String[] args) {
// sieve();
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
Integer[] k = intInput(n, in), h = intInput(n, in);
long ans = 0;
int a = k[n - 1];
int current = k[n - 1];
for (int i = n - 1; i >= 0; i--) {
if (current > k[i]) {
ans += sum(a - current + 1);
a = k[i];
current = k[i]-h[i]+1;
}else {
current = Math.min(current, k[i] - h[i]+1);
}
}
ans += sum(a - current + 1);
out.println(ans);
}
out.flush();
}
static long sum(long a) {
return a * (a + 1) / 2;
}
static boolean prime(long k) {
for (int i = 2; i * i <= k; i++) {
if (k % i == 0) {
return false;
}
}
return true;
}
static long gcd(long a, long b) {
if (a % b == 0) {
return b;
} else {
return gcd(b, a % b);
}
}
static void reverseArray(Integer[] a) {
for (int i = 0; i < (a.length >> 1); i++) {
Integer temp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = temp;
}
}
static Integer[] intInput(int n, InputReader in) {
Integer[] a = new Integer[n];
for (int i = 0; i < a.length; i++)
a[i] = in.nextInt();
return a;
}
static Long[] longInput(int n, InputReader in) {
Long[] a = new Long[n];
for (int i = 0; i < a.length; i++)
a[i] = in.nextLong();
return a;
}
static String[] strInput(int n, InputReader in) {
String[] a = new String[n];
for (int i = 0; i < a.length; i++)
a[i] = in.next();
return a;
}
// static void sieve() {
// for (int i = 2; i * i < prime.length; i++) {
// if (prime[i])
// continue;
// for (int j = i * i; j < prime.length; j += i) {
// prime[j] = true;
// }
// }
// }
}
class Data {
int val;
int ind;
boolean positive;
Data(int val, int ind) {
this.val = Math.abs(val);
this.ind = ind;
this.positive = val >= 0;
}
}
class compareVal implements Comparator<Data> {
@Override
public int compare(Data o1, Data o2) {
// return (o1.val - o2.val == 0 ? o1.ind - o2.ind : o1.val - o2.val);
return (o1.val - o2.val);
}
}
class compareInd implements Comparator<Data> {
@Override
public int compare(Data o1, Data o2) {
return o1.ind - o2.ind == 0 ? o1.val - o2.val : o1.ind - o2.ind;
}
}
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());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | import java.util.*;
import java.util.Scanner;
public class Solution {
static int mod=1000000007;;
//
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println();
Scanner sc=new Scanner(System.in);
int tt=sc.nextInt();
//
//
while(tt-->0){
int n=sc.nextInt();
int k[]=new int[n];
int h[]=new int[n];
for(int i=0;i<n;i++) {
k[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
h[i]=sc.nextInt();
}
long ans=0;
int start=k[0]-h[0]-1;
int end=k[0];
int last=0;
for(int j=0;j<n;j++) {
start=k[j]-h[j]+1;
end=k[j];
last=j;
for(int i=j+1;i<n;i++) {
int temp=k[i]-h[i]+1;
if(temp<=end) {
start=Math.min(start, temp);
end=Math.max(end, k[i]);
last=i;
}
}
j=last;
long va=end-start+1;
ans+=(va*(va+1))/2;
}
System.out.println(ans);
}
}
}
| 0 | Non-plagiarised |
161b4a40 | 49e94e7e | import java.util.*;
import java.io.*;
public class Main{
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
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().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n){
int[] res=new int[n];
for(int i=0;i<n;i++)res[i]=nextInt();
return res;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=in.nextInt();
//int testCases=1;
while(testCases-- > 0){
solve(in);
}
out.close();
} catch (Exception e) {
return;
}
}
public static void solve( FastReader in){
int n=in.nextInt();
String s=in.next();
String t=in.next();
//int k=in.nextInt();
//long y=in.nextInt();
//long n=in.nextLong();
//int k=in.nextInt();
//long k=in.nextLong();
StringBuilder res=new StringBuilder();
char[] s1=s.toCharArray();
char[] t1=t.toCharArray();
int ans=n+2;
int[] cnt={0,0};
for(int i=0;i<n;i++){
if(s1[i]=='0' && t1[i]=='1'){
cnt[0]++;
}
if(s1[i]=='1' && t1[i]=='0'){
cnt[1]++;
}
}
if(cnt[0]==cnt[1])ans=Math.min(ans,cnt[0]+cnt[1]);
cnt[0]=cnt[1]=0;
for(int i=0;i<n;i++){
if(s1[i]=='0' && t1[i]=='0'){
cnt[0]++;
}
if(s1[i]=='1' && t1[i]=='1'){
cnt[1]++;
}
}
if(cnt[1]==cnt[0]+1){
ans=Math.min(ans,cnt[0]+cnt[1]);
}
if(ans>n){
res.append("-1");
}
else{
res.append(""+ans);
}
//int ans=x.size()+y.size();
//res.append(""+"Yes");
//res.append(""+"");
System.out.println(res.toString());
}
static int gcd(int a,int b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
static void sort(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void reversesort(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
Collections.reverse(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static void debug(String x){
System.out.println(x);
}
static < E > void print(E res)
{
System.out.println(res);
}
static String rString(String s){
StringBuilder sb=new StringBuilder();
sb.append(s);
return sb.reverse().toString();
}
} | import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
int first, second,third;
public Tuple(int first, int second, int third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long modPow(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return modPow(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Integer> l = new ArrayList<>();
for (int p = 2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i = p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p = 2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(long a[], long x)
{
int l=-1, r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int tt = sc.nextInt();
while(tt-- > 0)
{
int n = sc.nextInt();
char[] a = sc.next().toCharArray(), b = sc.next().toCharArray();
int c00 = 0, c01 = 0, c10 = 0, c11 = 0;
for(int i = 0;i<n;i++)
{
if(a[i] == '0' && b[i] == '0')
{
c00++;
}
else if(a[i] == '0' && b[i] == '1')
{
c01++;
}
else if(a[i] == '1' && b[i] == '0')
{
c10++;
}
else if(a[i] == '1' && b[i] == '1')
{
c11++;
}
}
int ans = mod;
if(c01 == c10) ans = min(ans, c01 + c10);
if(c11 == c00 + 1) ans = min(ans, c11 + c00);
fout.println((ans == mod) ? -1 : ans);
}
fout.close();
}
} | 0 | Non-plagiarised |
547ee69b | 5ce1fe32 | import java.util.Arrays;
import java.util.Scanner;
public class Main {
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(getMinimumTime(a, n));
}
public static int getMinimumTime(int[] a, int n) {
int[][] minTime = new int[n][n];
int[] posOfPerson = new int[n];
int totalOccupied = 0;
for(int i=0; i<n; i++) {
if(a[i] == 1) {
posOfPerson[totalOccupied] = i;
}
totalOccupied += a[i];
}
for(int i=0; i<n; i++) {
Arrays.fill(minTime[i], 1, n, Integer.MAX_VALUE);
}
if(a[0] == 0) {
minTime[0][1] = Math.abs(0-posOfPerson[0]);
}
int unoccupiedPlaces = 1-a[0];
for(int i=1; i<n; i++) {
if(a[i] == 0) {
unoccupiedPlaces++;
}
for(int j=1; j<=Math.min(totalOccupied, unoccupiedPlaces); j++) {
if(a[i] == 1) {
minTime[i][j] = minTime[i-1][j];
}
else if(j == unoccupiedPlaces) {
minTime[i][j] = minTime[i-1][j-1] + Math.abs(i-posOfPerson[j-1]);
}
else {
minTime[i][j] = Math.min(minTime[i-1][j], minTime[i-1][j-1] + Math.abs(i-posOfPerson[j-1]));
}
}
}
return minTime[n-1][totalOccupied];
}
}
| // Rohit Bohra | 16/05/2021 : 13:13:29
import java.io.*;
import java.util.*;
@SuppressWarnings("unchecked")
public class D{
static long[][] dp;
static List<Integer> seats,people;
static int n1,n2;
static long solve(int i,int j){
if(i==n1) return 0;
long ans = Integer.MAX_VALUE;
if(n1-i>n2-j) return ans;
if(dp[i][j]!=-1) return dp[i][j];
ans = min(solve(i,j+1),solve(i+1,j+1)+Math.abs(people.get(i)-seats.get(j)));
// out.printf("%d %d : %ld",i,j,ans);
return dp[i][j]=ans;
}
public static void main(String args[])throws IOException{
int n=sc.nextInt();
seats = new ArrayList<>(n/2);
people = new ArrayList<>(n/2);
for(int i=1;i<=n;i++){
if(sc.nextInt()==0){
seats.add(i);
}else{
people.add(i);
}
}
n1=people.size();
n2=seats.size();
// if(n2==n){
// out.println(0);
// }
dp = new long[n1][n2];
for(int i=0;i<n1;i++)
Arrays.fill(dp[i],-1);
long ans =solve(0,0);
out.println(ans);
out.close();
}
static int M=(int)Math.pow(10,9)+7;
public static void sort(int[] arr,boolean reverse){
ArrayList<Integer> list = new ArrayList<Integer>();
int n =arr.length;
for(int i=0;i<n;i++){
list.add(arr[i]);
}
if(reverse)
Collections.sort(list,Collections.reverseOrder());
else
Collections.sort(list);
for(int i=0;i<n;i++){
arr[i] = list.get(i);
}
}
public static double min(double ...a){
double min=Double.MAX_VALUE;
for(double i : a)
min =Math.min(i,min);
return min;
}
public static long min(long ...a){
long min=Long.MAX_VALUE;
for(long i : a)
min =Math.min(i,min);
return min;
}
public static int min(int ...a){
int min=Integer.MAX_VALUE;
for(int i : a)
min =Math.min(i,min);
return min;
}
public static double max(double ...a){
double max=Double.MIN_VALUE;
for(double i : a)
max =Math.max(i,max);
return max;
}
public static long max(long ...a){
long max=Long.MIN_VALUE;
for(long i : a)
max =Math.max(i,max);
return max;
}
public static int max(int ...a){
int max=Integer.MIN_VALUE;
for(int i : a)
max =Math.max(i,max);
return max;
}
static class Pair{
// Implementing equals() and hashCode()
// Map<Pair, V> map = //...
private final int x;
private final int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
return x == pair.x && y == pair.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static FastScanner sc = new FastScanner();
static PrintWriter out =new PrintWriter(System.out);
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
} | 0 | Non-plagiarised |
49e94e7e | fcc7e8fa | import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
int first, second,third;
public Tuple(int first, int second, int third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long modPow(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return modPow(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Integer> l = new ArrayList<>();
for (int p = 2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i = p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p = 2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(long a[], long x)
{
int l=-1, r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int tt = sc.nextInt();
while(tt-- > 0)
{
int n = sc.nextInt();
char[] a = sc.next().toCharArray(), b = sc.next().toCharArray();
int c00 = 0, c01 = 0, c10 = 0, c11 = 0;
for(int i = 0;i<n;i++)
{
if(a[i] == '0' && b[i] == '0')
{
c00++;
}
else if(a[i] == '0' && b[i] == '1')
{
c01++;
}
else if(a[i] == '1' && b[i] == '0')
{
c10++;
}
else if(a[i] == '1' && b[i] == '1')
{
c11++;
}
}
int ans = mod;
if(c01 == c10) ans = min(ans, c01 + c10);
if(c11 == c00 + 1) ans = min(ans, c11 + c00);
fout.println((ans == mod) ? -1 : ans);
}
fout.close();
}
} | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n, _00 = 0, _01 = 0, _11 = 0, _10 = 0;
n = in.nextInt();
char[] s = in.next().toCharArray();
char[] s1 = in.next().toCharArray();
for (int i = 0; i < n; i++) {
if(s[i]==s1[i]){
if(s[i]=='0'){
_00++;
}
else{
_11++;
}
}
else{
if(s[i]=='0'){
_01++;
}
else{
_10++;
}
}
}
int ans = Integer.MAX_VALUE;
if(_10 ==_01){
ans = 2*_01;
}
if(_11 == _00 + 1){
ans = Math.min(ans, 2*_00 + 1);
}
if(ans == Integer.MAX_VALUE){
out.println(-1);
}
else{
out.println(ans);
}
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a, b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return o.a - this.a;
}
}
static class arrayListClass {
ArrayList<Integer> arrayList2 ;
public arrayListClass(ArrayList<Integer> arrayList) {
this.arrayList2 = arrayList;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static 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());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | 0 | Non-plagiarised |
35f0c004 | efa38999 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=input.nextInt();
while(T-->0)
{
int n=input.nextInt();
String a=input.next();
String b=input.next();
int same1=0,same0=0,opp1=0,opp0=0;
for(int i=0;i<n;i++)
{
if(a.charAt(i)==b.charAt(i))
{
if(a.charAt(i)=='1') same1++;
else same0++;
}
else
{
if(a.charAt(i)=='1') opp1++;
else opp0++;
}
}
if(same0+same1==n)
{
out.println(0);
}
else
{
int x=same1+opp1;
int y=same1+opp0;
int z=same0+opp0;
if(x==y || (z+1)==y)
{
int min=Integer.MAX_VALUE;
if((same0+same1)%2!=0 && same0==(same0+same1)/2)
{
min=Math.min(min,same0+same1);
}
if((opp0+opp1)%2==0 && opp0==(opp0+opp1)/2)
{
min=Math.min(min,opp0+opp1);
}
out.println(min);
}
else
{
out.println(-1);
}
}
}
out.close();
}
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;
}
}
} | import java.util.*;
import java.io.*;
public class Main {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
try {
br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
} catch (Exception e) {
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;
}
}
// end of fast i/o code
public static void main(String[] args) {
FastReader reader = new FastReader();
StringBuilder sb = new StringBuilder("");
int t = reader.nextInt();
int ans = 0;
while (t-- > 0) {
int n = reader.nextInt();
String a = reader.nextLine();
String b = reader.nextLine();
ans = -1;
int even_demand = 0;
int odd_demand = 0;
int same_ones = 0, same_zeros = 0;
int diff_ones = 0, diff_zeros = 0;
for(int i=0; i<n; i++){
if(a.charAt(i)==b.charAt(i)){
even_demand++;
if(a.charAt(i)=='1'){
same_ones++;
}else{
same_zeros++;
}
}else{
odd_demand++;
if(a.charAt(i)=='1'){
diff_ones++;
}else{
diff_zeros++;
}
}
}
// System.out.println(even_demand+" "+same_ones+" "+same_zeros+" "+odd_demand+" "+diff_ones+" "+diff_zeros);
if(even_demand%2==1 && same_ones==same_zeros+1){
ans = even_demand;
}
if(odd_demand%2==0 && diff_ones==diff_zeros){
if(ans==-1){
ans = odd_demand;
}else{
ans = Math.min(ans, odd_demand);
}
}
sb.append(ans + "\n");
}
System.out.println(sb);
}
} | 0 | Non-plagiarised |
ac180326 | d1cd194e | //This code is written by प्रविण शंखपाळ
//package wizard;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.List;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import java.util.Map;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
public class Dobby {
public static void main(String[] args) {
try {
FastReader fr = new FastReader();
PrintWriter pt = new PrintWriter(System.out);
int t = fr.nextInt();
while (t > 0) {
int n = fr.nextInt(), m = fr.nextInt(), x = fr.nextInt();
ArrayList<Pair> pp = new ArrayList<>();
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = fr.nextInt();
Pair pr = new Pair(A[i], i);
pp.add(pr);
}
Collections.sort(pp);
Collections.reverse(pp);
int ps[] = new int[n];
int pk[] = new int[n];
Arrays.fill(ps, 0);
Arrays.fill(pk, 0);
int index = 0;
for (int i = 0; i < n; i++) {
if (pk[index] < x) {
pk[index] += pp.get(i).a;
}
ps[pp.get(i).b] = index + 1;
index++;
index = index == m ? 0 : index;
}
pt.println("YES");
for (int i = 0; i < n; i++) {
pt.print(ps[i] + " ");
}
pt.println();
t--;
}
pt.close();
} catch (
Exception e) {
return;
}
}
static void merge(long arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
static void sort(long arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static class Pair implements Comparable<Pair> {
int a, b;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if (this.a != o.a)
return Integer.compare(this.a, o.a);
else
return Integer.compare(this.b, o.b);
// return 0;
}
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair p = (Pair) o;
return p.a == a && p.b == b;
}
return false;
}
}
static int binarySearch(int arr[], int first, int last, int key) {
int mid = (first + last) / 2;
while (first <= last) {
if (arr[mid] < key) {
first = mid + 1;
} else if (arr[mid] == key) {
return mid;
} else {
last = mid - 1;
}
mid = (first + last) / 2;
}
return -1;
}
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;
}
}
}
| import java.util.*;
import java.lang.*;
import java.io.*;
public class Template {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null ||!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// static void solve(String s)
// {
//// Scanner sc = new Scanner(System.in);
//// String s = sc.next();
//
// int x[] = new int[2];
// x[0] = x[1] = -1;
//
// int ans = 0;
// int n = s.length();
// for(int i=0;i<n;i++)
// {
// int c = s.charAt(i) - '0';
// if(c == 1 || c == 0)
// {
// x[(i%2) ^ c] = i;
// }
// int min = Math.min(x[0], x[1]);
// ans += i - min;
// //System.out.println(ans);
// }
// System.out.println(ans);
// }
//
// public static void main(String args[])
// {
// FastReader sc = new FastReader();
// //solve();
// //Scanner sc = new Scanner(System.in)
// int testcases = sc.nextInt(); // nTest is the number of treasure hunts.
//
//// int testcases = 3;
// while(testcases-- > 0)
// {
// String s = sc.next();
// solve(s);
//
// }
//
// }
static class Pair implements Comparable<Pair>
{
int h;
int ind;
Pair(int h, int ind)
{
this.h = h;
this.ind = ind;
}
@Override
public int compareTo(Pair o) {
return this.h - o.h;
}
}
public static void main(String[] args) {
FastReader fs=new FastReader();
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
int n = fs.nextInt();
int m = fs.nextInt();
int x = fs.nextInt();
if(n < m)
{
System.out.println("NO");
continue;
}
Pair a[] = new Pair[n];
PriorityQueue<Pair> heap = new PriorityQueue<>();
for(int i=0;i<n;i++)
{
a[i] = new Pair(fs.nextInt(), i);
}
Arrays.sort(a);
for(int i=1;i<=m;i++)
{
heap.add(new Pair(0, i));
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int ans[] = new int[n];
int idx = 0;
while(!heap.isEmpty() && idx < n)
{
Pair curr = heap.poll();
curr.h += a[idx].h;
ans[a[idx].ind] = curr.ind;
heap.add(new Pair(curr.h, curr.ind));
idx++;
}
// int towers[] = new int[m+1];
// int tower = 1;
// boolean flag = false;
// boolean inc = true;
// for(int i=0;i<n;i++)
// {
// if(tower == m+1)
// {
// tower = m;
// inc = false;
// }
// if(tower == 0)
// {
// tower = 1;
// inc = true;
// }
// towers[tower] += a[i].h;
// System.out.println(a[i].h +" THis" + tower);
//// min = Math.min(min, towers[tower]);
//// max = Math.max(max, towers[tower]);
// ans[a[i].ind] = tower;
//// if(Math.abs(max - min) > x)
//// {
//// System.out.println("NO" + a[i].ind+" "+a[i].h +" "+min +" "+max);
//// flag = true;
//// break;
//// }
// if(inc)
// tower++;
// else
// tower--;
// }
// for(int i=1;i<=m;i++)
// {
// min = Math.min(min, towers[i]);
// max = Math.max(max, towers[i]);
// }
// if(Math.abs(max - min) > x)
// {
// System.out.println("NO" + max+" "+min);// + a[i].ind+" "+a[i].h +" "+min +" "+max);
// //flag = true;
// continue;
// }
// if(flag)
// continue;
System.out.println("YES");
for(int i:ans)
System.out.print(i+" ");
System.out.println();
}
}
}
| 0 | Non-plagiarised |
a7063d01 | ac4d0fc5 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
solve(in, out);
out.close();
}
static String reverse(String s) {
return (new StringBuilder(s)).reverse().toString();
}
static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar)
{
factors[1]=1;
int p;
for(p = 2; p*p <=n; p++)
{
if(factors[p] == 0)
{
ar.add(p);
factors[p]=p;
for(int i = p*p; i <= n; i += p)
if(factors[i]==0)
factors[i] = p;
}
}
for(;p<=n;p++){
if(factors[p] == 0)
{
factors[p] = p;
ar.add(p);
}
}
}
static void sort(int ar[]) {
int n = ar.length;
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort1(long ar[]) {
int n = ar.length;
ArrayList<Long> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort2(char ar[]) {
int n = ar.length;
ArrayList<Character> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static long ncr(long n, long r, long mod) {
if (r == 0)
return 1;
long val = ncr(n - 1, r - 1, mod);
val = (n * val) % mod;
val = (val * modInverse(r, mod)) % mod;
return val;
}
static class SegTree {
long tree[];
long lz[];
long r;
long combine(long a, long b){
return Math.min(a,b);
}
void build(long a[], int v, int tl, int tr) {
if (tl == tr) {
tree[v] = a[tl];
}
else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
tree[v] = combine(tree[2*v], tree[2*v+1]);
}
}
void pointUpdate(int v, int tl, int tr, int pos, long val) {
if(tl>pos||tr<pos)
return;
if(tl==pos&&tr==pos){
tree[v] = val;
return;
}
int tm = ((tl + tr) >> 1);
pointUpdate(v*2, tl, tm, pos, val);
pointUpdate(v*2+1, tm+1, tr, pos, val);
tree[v] = combine(tree[2*v],tree[2*v+1]);
}
// void push(int v, int tl, int tr){
// if(tl==tr){
// lz[v] = 0;
// return;
// }
// tree[2*v] += lz[v];
// tree[2*v+1] += lz[v];
// lz[2*v] += lz[v];
// lz[2*v+1] += lz[v];
// lz[v] = 0;
// }
// void rangeUpdate(int v, int tl, int tr, int l, int r, long val) {
// if(tl>r||tr<l)
// return;
// push(v, tl, tr);
// if(tl>=l&&tr<=r){
// tree[v] += val;
// lz[v] += val;
// return;
// }
// int tm = ((tl + tr) >> 1);
// rangeUpdate(v*2, tl, tm, l, r, val);
// rangeUpdate(v*2+1, tm+1, tr, l, r, val);
// tree[v] = combine(tree[2*v],tree[2*v+1]);
// }
long get(int v, int tl, int tr, int l, int r, long val) {
if(l>tr||r<tl||tree[v]>val){
return 0;
}
if (tl == tr) {
tree[v] = Integer.MAX_VALUE;
return 1;
}
int tm = ((tl + tr) >> 1);
long al = get(2*v, tl, tm, l, r, val);
long ar = get(2*v+1, tm+1, tr, l, r, val);
tree[v] = combine(tree[2*v],tree[2*v+1]);
return al+ar;
}
}
static class BIT{
int n;
long tree[];
long operate(long el, long val){
return el+val;
}
void update(int x, long val){
for(;x<n;x+=(x&(-x))){
tree[x] = operate(tree[x], val);
}
}
long get(int x){
long sum = 0;
for(;x>0;x-=(x&(-x))){
sum = operate(sum, tree[x]);
}
return sum;
}
}
static int parent[];
static int rank[];
static long m = 0;
static int find_set(int v) {
if (v == parent[v])
return v;
return find_set(parent[v]);
}
static void make_set(int v) {
parent[v] = v;
rank[v] = 1;
}
static void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (rank[a] < rank[b]){
int tmp = a;
a = b;
b = tmp;
}
parent[b] = a;
// if (rank[a] == rank[b])
// rank[a]++;
rank[a] += rank[b];
max1 = Math.max(max1,rank[a]);
}
}
static int parent1[];
static int rank1[];
static int find_set1(int v) {
if (v == parent1[v])
return v;
return find_set1(parent1[v]);
}
static void make_set1(int v) {
parent1[v] = v;
rank1[v] = 1;
}
static void union_sets1(int a, int b) {
a = find_set1(a);
b = find_set1(b);
if (a != b) {
if (rank1[a] < rank1[b]){
int tmp = a;
a = b;
b = tmp;
}
parent1[b] = a;
// if (rank1[a] == rank1[b])
// rank1[a]++;
rank1[a] += rank1[b];
}
}
static long max1 = 0;
static int count = 0;
static int count1 = 0;
static boolean possible;
public static void solve(InputReader sc, PrintWriter pw){
int i, j = 0;
// int t = 1;
long mod = 1000000007;
// int factors[] = new int[1000001];
// ArrayList<Integer> ar = new ArrayList<>();
// sieveOfEratosthenes(1000000, factors, ar);
// HashSet<Integer> set = new HashSet<>();
// for(int x:ar){
// set.add(x);
// }
int t = sc.nextInt();
u: while (t-- > 0) {
int n = sc.nextInt();
int e[][] = new int[n-1][2];
int x[] = new int[n];
int m = 0;
for(i=0;i<n-1;i++){
e[i][0] = sc.nextInt()-1;
e[i][1] = sc.nextInt()-1;
x[e[i][0]]++;
x[e[i][1]]++;
m = Math.max(x[e[i][0]],m);
m = Math.max(x[e[i][1]],m);
}
if(m>2)
pw.println(-1);
else{
if(n==2){
pw.println(2);
}
else if(n==3){
pw.println(2+" "+3);
}
else{
int d = 0;
int ans[] = new int[n-1];
ArrayList<Integer> ar[] = new ArrayList[n];
ArrayList<Integer> ar1[] = new ArrayList[n];
for(i=0;i<n;i++){
ar[i] = new ArrayList<>();
ar1[i] = new ArrayList<>();
}
for(i=0;i<n-1;i++){
int a = e[i][0];
int b = e[i][1];
ar[a].add(b);
ar1[a].add(i);
ar[b].add(a);
ar1[b].add(i);
if(x[a]==1)
d = a;
if(x[b]==1)
d = b;
}
visit(d,ar,ar1,ans,-1,2);
for(i=0;i<n-1;i++){
pw.print(ans[i]+" ");
}
pw.println();
}
}
}
}
static void visit(int d, ArrayList<Integer> ar[], ArrayList<Integer> ar1[], int ans[], int par, int v){
if(ar[d].get(0)!=par){
ans[ar1[d].get(0)] = v;
visit(ar[d].get(0), ar, ar1, ans, d, 5-v);
return;
}
if(ar[d].size()==1)
return;
ans[ar1[d].get(1)] = v;
visit(ar[d].get(1), ar, ar1, ans, d, 5-v);
}
static void KMPSearch(char pat[], char txt[], int pres[]){
int M = pat.length;
int N = txt.length;
int lps[] = new int[M];
int j = 0;
computeLPSArray(pat, M, lps);
int i = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
pres[i-1] = 1;
j = lps[j - 1];
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
static void computeLPSArray(char pat[], int M, int lps[])
{
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
static long[][] matrixMult(long a[][], long b[][], long mod){
int n = a.length;
int m = a[0].length;
int p = b[0].length;
long c[][] = new long[n][p];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=0;k<p;k++){
c[i][k] += a[i][j]*b[j][k];
c[i][k] %= mod;
}
}
}
return c;
}
static long[][] exp(long mat[][], long b, long mod){
if(b==0){
int n = mat.length;
long res[][] = new long[n][n];
for(int i=0;i<n;i++){
res[i][i] = 1;
}
return res;
}
long half[][] = exp(mat, b/2, mod);
long res[][] = matrixMult(half, half, mod);
if(b%2==1){
res = matrixMult(res, mat, mod);
}
return res;
}
static void countPrimeFactors(int num, int a[], HashMap<Integer,Integer> pos){
for(int i=2;i*i<num;i++){
if(num%i==0){
int y = pos.get(i);
while(num%i==0){
a[y]++;
num/=i;
}
}
}
if(num>1){
int y = pos.get(num);
a[y]++;
}
}
static void assignAnc(ArrayList<Integer> ar[], int depth[], int sz[], int par[][] ,int curr, int parent, int d){
depth[curr] = d;
sz[curr] = 1;
par[curr][0] = parent;
for(int v:ar[curr]){
if(parent==v)
continue;
assignAnc(ar, depth, sz, par, v, curr, d+1);
sz[curr] += sz[v];
}
}
static int findLCA(int a, int b, int par[][], int depth[]){
if(depth[a]>depth[b]){
a = a^b;
b = a^b;
a = a^b;
}
int diff = depth[b] - depth[a];
for(int i=19;i>=0;i--){
if((diff&(1<<i))>0){
b = par[b][i];
}
}
if(a==b)
return a;
for(int i=19;i>=0;i--){
if(par[b][i]!=par[a][i]){
b = par[b][i];
a = par[a][i];
}
}
return par[a][0];
}
static void formArrayForBinaryLifting(int n, int par[][]){
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1]==-1)
continue;
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
static long lcm(int a, int b){
return a*b/gcd(a,b);
}
static class CustomPair {
long a[];
CustomPair(long a[]) {
this.a = a;
}
}
static class Pair1 implements Comparable<Pair1> {
long a;
long b;
Pair1(long a, long b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair1 p) {
if(a!=p.a)
return (a<p.a?-1:1);
return (b<p.b?-1:1);
}
}
static class Pair implements Comparable<Pair> {
int a;
int b;
int c;
Pair(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int compareTo(Pair p) {
if(b!=p.b)
return (b-p.b);
return (a-p.a);
}
}
static class Pair2 implements Comparable<Pair2> {
int a;
int b;
int c;
Pair2(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int compareTo(Pair2 p) {
return a-p.a;
}
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long fast_pow(long base, long n, long M) {
if (n == 0)
return 1;
if (n == 1)
return base % M;
long halfn = fast_pow(base, n / 2, M);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
static long modInverse(long n, long M) {
return fast_pow(n, M - 2, M);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 9992768);
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());
}
}
} | import java.io.*;
import java.util.*;
public class Main {
static boolean[] ret;
static boolean[] updated;
static ArrayList<Integer>[] adjacencyList;
static Edge[] edgeList;
static class Edge {
int start, end, number;
public Edge (int _start, int _end, int _number) {
start = _start;
end = _end;
number = _number;
}
}
public static void dfs(int node) {
updated[node] = true;
for (int next : adjacencyList[edgeList[node].start]) {
if (!updated[next]) {
ret[next] = !ret[node];
dfs(next);
}
}
for (int next : adjacencyList[edgeList[node].end]) {
if (!updated[next]) {
ret[next] = !ret[node];
dfs(next);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int numCases = Integer.parseInt(br.readLine());
for (int i = 0; i < numCases; i++) {
int numVertices = Integer.parseInt(br.readLine());
int[] numEdges = new int[numVertices];
edgeList = new Edge[numVertices - 1];
adjacencyList = new ArrayList[numVertices];
for (int j = 0; j < numVertices; j++) {
adjacencyList[j] = new ArrayList<>();
}
for (int j = 0; j < numVertices - 1; j++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;
edgeList[j] = new Edge(a, b, j);
numEdges[a]++;
numEdges[b]++;
adjacencyList[a].add(j);
adjacencyList[b].add(j);
}
boolean good = true;
for (int j = 0; j < numVertices; j++) {
if (numEdges[j] > 2) {
good = false;
break;
}
}
if (!good) {
pw.println(-1);
} else {
ret = new boolean[numVertices - 1];
updated = new boolean[numVertices - 1];
dfs(0);
for (boolean b : ret) {
if (b)
pw.print(5 + " ");
else
pw.print(2 + " ");
}
pw.println();
}
}
br.close();
pw.close();
}
} | 0 | Non-plagiarised |
1162c08f | a4d6775d | import java.util.*;
public class CodeForces1525C{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>();
int n = sc.nextInt(),dp[][]=new int[n+1][n+1];
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++)
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
System.out.println(dp[o.size()][e.size()]);
}
}
| import java.io.*;
import java.util.*;
public class ArmChairs {
public static int solution(int n, int[] arr) {
ArrayList<Integer> one = new ArrayList<Integer>();
ArrayList<Integer> zero = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
if (arr[i] == 1) {
one.add(i);
} else {
zero.add(i);
}
}
int[][] dp = new int[one.size() + 1][zero.size() + 1];
for (int i = 1; i <= one.size(); i++) {
dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1));
for (int j = i + 1; j <= zero.size(); j++) {
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1)));
}
}
return dp[one.size()][zero.size()];
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(s[i]);
}
log.write(Integer.toString(solution(n, arr)) + "\n");
log.flush();
}
} | 1 | Plagiarised |
9fc811f7 | cff39394 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DTreeTag solver = new DTreeTag();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class DTreeTag {
int diam = 0;
public int dfs(ArrayList<Integer> g[], int x, int depth[], int p) {
int len = 0;
for (int y : g[x]) {
if (y != p) {
depth[y] = depth[x] + 1;
int cur = 1 + dfs(g, y, depth, x);
diam = Math.max(diam, cur + len);
len = Math.max(len, cur);
}
}
return len;
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int a = in.nextInt() - 1;
int b = in.nextInt() - 1;
int da = in.nextInt();
int db = in.nextInt();
int dis[] = new int[n];
ArrayList<Integer> g[] = new ArrayList[n];
for (int i = 0; i < n; i++) g[i] = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
int u = in.nextInt() - 1;
int v = in.nextInt() - 1;
g[u].add(v);
g[v].add(u);
}
diam = 0;
dfs(g, a, dis, -1);
int disb = dis[b];
if (2 * da >= Math.min(diam, db) || disb <= da) {
out.println("Alice");
} else {
out.println("Bob");
}
}
}
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());
}
}
}
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class D {
static int diam=0;
public static int dfs(int a, int p,int[] depth,ArrayList<Integer>[] list) {
int len=0;
for(int y:list[a]) {
if(y==p)
continue;
depth[y]=depth[a]+1;
int cur=1+dfs(y,a,depth,list);
diam=Math.max(cur+len,diam);
len=Math.max(cur, len);
}
return len;
}
public static void main(String[] args) {
MyScanner s = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
try {
int t=s.nextInt();
while(t-->0) {
int n=s.nextInt();
int a=s.nextInt();
int b=s.nextInt();
int da=s.nextInt();
int db=s.nextInt();
ArrayList<Integer>[] list=new ArrayList[n+1];
for(int i=0;i<=n;i++)
list[i]=new ArrayList<Integer>();
for(int i=0;i<n-1;i++) {
int u=s.nextInt();
int v=s.nextInt();
list[u].add(v);
list[v].add(u);
}
int[] depth=new int[n+1];
diam=0;
depth[a]=0;
int dia=dfs(a,0,depth,list);
if(2*da>=Math.min(diam, db) || depth[b]<=da) {
System.out.println("Alice");
}
else {
System.out.println("Bob");
}
}
out.close();
}catch(Exception e) {
System.out.println(e);
return;
}
}
public static PrintWriter out;
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| 1 | Plagiarised |
00af3420 | 86102d81 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
String[] s = new String[n];
for(int i=0; i<n; i++)
s[i] = sc.next();
int MAX = 0;
for(char c = 'a'; c <= 'e'; c++){
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top;
for(int i=0; i<n; ++i) {
int curChar = 0;
int otherChar = 0;
for(int j=0; j<s[i].length(); j++) {
if(s[i].charAt(j) == c)
curChar++;
else
otherChar++;
}
int diff = curChar - otherChar;
pq.add(diff);
}
int cur = 0;
int numberOfWords = 0;
while(!pq.isEmpty()){
if(cur + pq.peek() > 0){
cur += pq.poll();
numberOfWords++;
}else{
break;
}
}
MAX = Math.max(MAX, numberOfWords);
}
pw.println(MAX);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
|
import java.io.*;
import java.util.*;
public class Aqueous {
static MyScanner sc = new MyScanner();
public static void main(String[] args) {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
String s[] = new String[n];
for(int i = 0; i<n; i++) {
s[i] = sc.next();
}
int ans = Integer.MIN_VALUE;
for(char c = 'a'; c<='e'; c++) {
int ls[] = new int[n];
for(int i = 0; i<n; i++) {
String temp = s[i];
int delta = 0;
for(int j = 0; j<temp.length(); j++) {
if(temp.charAt(j)==c) {
delta++;
}
else {
delta--;
}
}
ls[i] = delta;
}
Arrays.sort(ls);
int cur = 0;
int score= 0;
for(int k = n-1; k>=0; k--) {
if(cur+ls[k]>=1) {
cur+=ls[k];
score++;
}
}
ans = Math.max(ans, score);
}
System.out.println(ans);
}
}
static void ruffleSort(int a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
int temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| 0 | Non-plagiarised |
1162c08f | 734a94be | import java.util.*;
public class CodeForces1525C{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>();
int n = sc.nextInt(),dp[][]=new int[n+1][n+1];
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++)
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
System.out.println(dp[o.size()][e.size()]);
}
}
|
import java.io.*;
import java.math.*;
import java.util.*;
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int solve(int dp[][] , ArrayList<Integer> one , ArrayList<Integer> zero , int i,int j)
{
int n=one.size();
int m=zero.size();
if(i>=n)
return 0;
else if(j>=m)
return Integer.MAX_VALUE;
else if(dp[i][j]!=-1)
return dp[i][j];
int val1 = Math.abs(one.get(i)-zero.get(j))+solve(dp, one ,zero ,i+1,j+1);
int val2 = solve(dp , one , zero , i,j+1);
dp[i][j]= Math.min(val1,val2);
return dp[i][j];
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuffer res = new StringBuffer();
int tc = 1;
while(tc-->0) {
int n = sc.nextInt();
ArrayList<Integer> one = new ArrayList<>();
ArrayList<Integer> zero = new ArrayList<>();
for(int i=0;i<n;i++) {
int x = sc.nextInt();
if(x==1) {
one.add(i);
}
else {
zero.add(i);
}
}
int dp[][] = new int[one.size()+1][zero.size()+1];
for(int i=1;i<=one.size();i++)
{
dp[i][i]=dp[i-1][i-1]+Math.abs(zero.get(i-1)-one.get(i-1));
for(int j=i+1;j<=zero.size();j++)
{
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(one.get(i-1)-zero.get(j-1)));
}
}
System.out.println(dp[one.size()][zero.size()]);
}
System.out.println(res);
}
}
| 1 | Plagiarised |
163d0dde | f87eb1b3 | // Working program with FastReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class aa
{
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 int findIndex(long arr[], long t)
{
// if array is Null
if (arr == null) {
return -1;
}
// find length of array
int len = arr.length;
int i = 0;
// traverse in the array
while (i < len) {
// if the i-th element is t
// then return the index
if (arr[i] == t) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
public static void main(String[] args)
{
FastReader d=new FastReader();
int t,i,j,c,z,k,l,n;
int mod = (int) 1e9 + 7;
int Inf=Integer.MAX_VALUE;
int negInf=Integer.MIN_VALUE;
t=d.nextInt();
//t=1;
//String s;
//char ch,ch1,ch2,ch3;
while(t-->0)
{
z=c=0;
n=d.nextInt();
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=d.nextInt();
//s=d.nextLine();//dont need extra d.nextLine()
long p=0;
long ans;
long x,y;
long e,o;
ans=Long.MAX_VALUE;
x=y=Integer.MAX_VALUE;
e=o=n;
for(i=0;i<n;i++) {
if(i%2==1) {
p+=a[i];
e--;
x=Long.min(x, a[i]);
ans=Long.min(ans,p+(o*y)+(e*x));
}
else {
p+=a[i];
o--;
y=Long.min(y, a[i]);
ans=Long.min(ans,p+(o*y)+(e*x));
}
}
System.out.println(ans);
}
}
} | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.PrintStream;
import java.io.PrintWriter;
public class A {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int aa, int bb)
{
first = aa; second = bb;
}
public int compareTo(Pair o)
{
if(this.second < o.second) return -1;
if(this.second > o.second) return +1;
return this.first - o.first;
}
}
static class Tuple implements Comparable<Tuple>{
long first, second,third;
public Tuple(long first, long second, long third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[n+1];
Arrays.fill(prime, true);
ArrayList<Integer> l = new ArrayList<>();
for (int p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i=p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p=2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long modPow(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return modPow(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n) {
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(int n, int k)
{
long ans = 1;
for(int i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
public static void Sort(int[] a)
{
List<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(int a[], int x)
{
int l=-1, r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0)
{
long INF = 1000000000000000007L;
int n = sc.nextInt();
long[] c = new long[n+1];
for(int i = 1;i<=n;i++) c[i] = sc.nextLong();
long ans = INF;
long mo = INF, so = 0, co = 0;
long me = INF, se = 0, ce = 0;
for(int i=1;i<=n;i++)
{
if(i%2 == 1)
{
mo = min(mo,c[i]);
so += c[i];
co++;
}
else
{
me = min(me,c[i]);
se += c[i];
ce++;
}
if(i>=2)
{
long x = so + (n - co) * mo + se + (n - ce) * me;
ans=min(ans,x);
}
}
fout.println(ans);
}
fout.close();
}
} | 0 | Non-plagiarised |
0922b7e7 | d1cd194e | import java.io.*;
import java.util.*;
public class PhoenixAndTowers {
public static void main(String[] args) throws IOException {
// BufferedReader in = new BufferedReader(new FileReader("PhoenixAndTowers.in"));
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("PhoenixAndTowers.out")));
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = Integer.parseInt(in.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(in.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int X = Integer.parseInt(st.nextToken());
PriorityQueue<Tower> towers = new PriorityQueue<Tower>();
for (int j = 1; j <= M; j++) towers.add(new Tower(j, 0));
out.println("YES");
st = new StringTokenizer(in.readLine());
for (int j = 0; j < N; j++) {
Tower t = towers.remove();
t.size += Integer.parseInt(st.nextToken());
towers.add(t);
out.print(t.idx + " ");
}
out.println();
}
out.close();
in.close();
}
public static class Tower implements Comparable<Tower> {
int idx, size;
public Tower(int idx, int size) {
this.idx = idx;
this.size = size;
}
@Override
public int compareTo(Tower o) {
return size - o.size;
}
}
} | import java.util.*;
import java.lang.*;
import java.io.*;
public class Template {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null ||!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// static void solve(String s)
// {
//// Scanner sc = new Scanner(System.in);
//// String s = sc.next();
//
// int x[] = new int[2];
// x[0] = x[1] = -1;
//
// int ans = 0;
// int n = s.length();
// for(int i=0;i<n;i++)
// {
// int c = s.charAt(i) - '0';
// if(c == 1 || c == 0)
// {
// x[(i%2) ^ c] = i;
// }
// int min = Math.min(x[0], x[1]);
// ans += i - min;
// //System.out.println(ans);
// }
// System.out.println(ans);
// }
//
// public static void main(String args[])
// {
// FastReader sc = new FastReader();
// //solve();
// //Scanner sc = new Scanner(System.in)
// int testcases = sc.nextInt(); // nTest is the number of treasure hunts.
//
//// int testcases = 3;
// while(testcases-- > 0)
// {
// String s = sc.next();
// solve(s);
//
// }
//
// }
static class Pair implements Comparable<Pair>
{
int h;
int ind;
Pair(int h, int ind)
{
this.h = h;
this.ind = ind;
}
@Override
public int compareTo(Pair o) {
return this.h - o.h;
}
}
public static void main(String[] args) {
FastReader fs=new FastReader();
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
int n = fs.nextInt();
int m = fs.nextInt();
int x = fs.nextInt();
if(n < m)
{
System.out.println("NO");
continue;
}
Pair a[] = new Pair[n];
PriorityQueue<Pair> heap = new PriorityQueue<>();
for(int i=0;i<n;i++)
{
a[i] = new Pair(fs.nextInt(), i);
}
Arrays.sort(a);
for(int i=1;i<=m;i++)
{
heap.add(new Pair(0, i));
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int ans[] = new int[n];
int idx = 0;
while(!heap.isEmpty() && idx < n)
{
Pair curr = heap.poll();
curr.h += a[idx].h;
ans[a[idx].ind] = curr.ind;
heap.add(new Pair(curr.h, curr.ind));
idx++;
}
// int towers[] = new int[m+1];
// int tower = 1;
// boolean flag = false;
// boolean inc = true;
// for(int i=0;i<n;i++)
// {
// if(tower == m+1)
// {
// tower = m;
// inc = false;
// }
// if(tower == 0)
// {
// tower = 1;
// inc = true;
// }
// towers[tower] += a[i].h;
// System.out.println(a[i].h +" THis" + tower);
//// min = Math.min(min, towers[tower]);
//// max = Math.max(max, towers[tower]);
// ans[a[i].ind] = tower;
//// if(Math.abs(max - min) > x)
//// {
//// System.out.println("NO" + a[i].ind+" "+a[i].h +" "+min +" "+max);
//// flag = true;
//// break;
//// }
// if(inc)
// tower++;
// else
// tower--;
// }
// for(int i=1;i<=m;i++)
// {
// min = Math.min(min, towers[i]);
// max = Math.max(max, towers[i]);
// }
// if(Math.abs(max - min) > x)
// {
// System.out.println("NO" + max+" "+min);// + a[i].ind+" "+a[i].h +" "+min +" "+max);
// //flag = true;
// continue;
// }
// if(flag)
// continue;
System.out.println("YES");
for(int i:ans)
System.out.print(i+" ");
System.out.println();
}
}
}
| 0 | Non-plagiarised |
4303de0d | c9a316ca | import java.io.*;
import java.util.*;
public class ParsasHumongousTree
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static class Output
{
private final PrintWriter writer;
public Output(OutputStream outputStream)
{
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public Output(Writer writer)
{
this.writer = new PrintWriter(writer);
}
public void print(Object...objects)
{
for (int i = 0; i < objects.length; i++)
{
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects)
{
print(objects);
writer.println();
}
public void close()
{
writer.close();
}
public void flush() {
writer.flush();
}
}
static class Pair
{
int a;
int b;
public Pair(int a, int b)
{
this.a=a;
this.b=b;
}
}
static HashMap<Integer,List<Integer>> edge=new HashMap();
static Pair a[];
static long dp[][];
static void dfs(int node, int parent)
{
for(int i:edge.get(node))
{
if(i!=parent)
{
dfs(i,node);
dp[node][0]+=Math.max(Math.abs(a[node].a-a[i].a)+dp[i][0],Math.abs(a[node].a-a[i].b)+dp[i][1]);
dp[node][1]+=Math.max(Math.abs(a[node].b-a[i].a)+dp[i][0],Math.abs(a[node].b-a[i].b)+dp[i][1]);
}
}
}
public static void main(String args[])
{
FastReader sc=new FastReader();
Output out=new Output(System.out);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
a=new Pair[n+1];
edge.clear();
for(int i=1;i<n+1;i++)
{
int l=sc.nextInt();
int r=sc.nextInt();
Pair p=new Pair(l,r);
a[i]=p;
}
for(int i=0;i<n-1;i++)
{
int u=sc.nextInt();
int v=sc.nextInt();
if(edge.containsKey(u))
{
List<Integer> list=edge.get(u);
list.add(v);
edge.put(u,list);
}
else
{
List<Integer> list=new ArrayList();
list.add(v);
edge.put(u,list);
}
if(edge.containsKey(v))
{
List<Integer> list=edge.get(v);
list.add(u);
edge.put(v,list);
}
else
{
List<Integer> list=new ArrayList();
list.add(u);
edge.put(v,list);
}
}
dp=new long[n+1][2];
dfs(1,0);
out.printLine(((long)Math.max(dp[1][0],dp[1][1])));
out.flush();
}
}
} |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static javax.swing.UIManager.get;
import static javax.swing.UIManager.getString;
public class Main {
static class Pair implements Comparable<Pair> {
int x = 0;
int y = 0;
public Pair(int x1, int y1) {
x = x1;
y = y1;
}
@Override
public int compareTo(Pair o) {
return this.x - o.x;
}
}
static boolean checkPallindrome(int n) {
ArrayList<Integer> list = new ArrayList<>();
while (n > 0) {
list.add(n % 10);
n /= 10;
}
int low = 0, high = list.size() - 1;
while (low <= high) {
if (list.get(low) != list.get(high))
return false;
low++;
high--;
}
return true;
}
static boolean check(String s) {
int low = 0, high = s.length() - 1;
while (low <= high) {
if (s.charAt(low) != s.charAt(high))
return false;
low++;
high--;
}
return true;
}
class Node implements Comparable<Node> {
int x = 0, y = 0, z = 0;
@Override
public int compareTo(Node o) {
return this.y - o.y;
}
}
static int min = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
//(1)very very important**(never take the first problem for granted, always check the test cases) take 5 minutes more and check the edge cases
// 5 minutes will not decreases rating as much as a wrong submission does it is easy u just think with an open mind and u will surely get the answer
//(2)let ur brain consume the problem don't just jump to the solution. after reading the problem take a pause 1 minute
//(3)go through the example test cases and also at least two of ur own test cases.Think of testcases which are difficult(edge cases).dry run ur concept
//(4) sometimes if else condition is not required but due to if else you miss some points and get wrong answer
int t = sc.nextInt();
while (t-- > 0) {
int n =sc.nextInt();
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for(int i=0;i<n;i++)
list.add(new ArrayList<Integer>());
ArrayList<Pair> list1 = new ArrayList<>();
for(int i=0;i<n;i++)
list1.add(new Pair(sc.nextInt(),sc.nextInt()));
for(int i=0;i<n-1;i++)
{
int a =sc.nextInt()-1,b=sc.nextInt()-1;
list.get(a).add(b);
list.get(b).add(a);
}
long[][] dp = new long[2][n];
dfs(0,-1,dp,list,list1);
System.out.println(Math.max(dp[0][0],dp[1][0]));
}
}
static void dfs(int u,int p,long[][] dp,ArrayList<ArrayList<Integer>> list,ArrayList<Pair> list1)
{
for(int v:list.get(u))
{ if(v==p)
continue;
dfs(v,u,dp,list,list1);
dp[1][u]+= Math.max(Math.abs(list1.get(u).y-list1.get(v).x)+dp[0][v],Math.abs(list1.get(u).y-list1.get(v).y)+dp[1][v]);
dp[0][u]+=Math.max(Math.abs(list1.get(u).x-list1.get(v).x)+dp[0][v],Math.abs(list1.get(u).x-list1.get(v).y)+dp[1][v]);
}
}
//static int lcs( int[] X, int[] Y, int m, int n )
//{
// int L[][] = new int[m+1][n+1];
//
// /* Following steps build L[m+1][n+1] in bottom up fashion. Note
// that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
// for (int i=0; i<=m; i++)
// {
// for (int j=0; j<=n; j++)
// {
// if (i == 0 || j == 0)
// L[i][j] = 0;
// else if (X[i-1] == Y[j-1])
// L[i][j] = L[i-1][j-1] + 1;
// else
// L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
// }
// }
// return L[m][n];
//}
// syntax of conditional operator y=(x==1)?1:0;
//Things to check when u r getting wrong answer
// 1- check the flow of the code
//2- If ur stuck read the problem once again
//3- before submitting always check the output format of ur code
//4- don't check standings until problem B is done
//5- if u r thinking ur concept is correct but still u r getting wrong answer try to implement it in another way
//6- By default, java interpret all numeral literals as 32-bit integer values.
// If you want to explicitely specify that this is something bigger then 32-bit integer you should use suffix L for long values. example long a = 600851475143L
//All the functions
long pow(long a,long b,long m)
{
a%=m;
long res=1L;
while(b>0)
{
long temp=b&1;
if(temp==1)
res=(res*a)%m;
a=(a*a)%m;
b>>=1;
}
return res;
}
static class DisjointUnionSets {
int[] rank, parent;
int n;
// Constructor
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
// Creates n sets with single item in each
void makeSet()
{
for (int i = 0; i < n; i++) {
// Initially, all elements are in
// their own set.
parent[i] = i;
}
}
// Returns representative of x's set
int find(int x)
{
// Finds the representative of the set
// that x is an element of
if (parent[x] != x) {
// if x is not the parent of itself
// Then x is not the representative of
// his set,
parent[x] = find(parent[x]);
// so we recursively call Find on its parent
// and move i's node directly under the
// representative of this set
}
return parent[x];
}
// Unites the set that includes x and the set
// that includes x
void union(int x, int y)
{
// Find representatives of two sets
int xRoot = find(x), yRoot = find(y);
// Elements are in the same set, no need
// to unite anything.
if (xRoot == yRoot)
return;
// If x's rank is less than y's rank
if (rank[xRoot] < rank[yRoot])
// Then move x under y so that depth
// of tree remains less
parent[xRoot] = yRoot;
// Else if y's rank is less than x's rank
else if (rank[yRoot] < rank[xRoot])
// Then move y under x so that depth of
// tree remains less
parent[yRoot] = xRoot;
else // if ranks are the same
{
// Then move y under x (doesn't matter
// which one goes where)
parent[yRoot] = xRoot;
// And increment the result tree's
// rank by 1
rank[xRoot] = rank[xRoot] + 1;
}
}
}
static void debug(String s)
{ System.out.println(s);
}
//collections.sort use merge sort instead of quick sort but arrays.sort use quicksort whose worst time complexity is O(n^2)
static int[] sort(int[] a)
{ ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<a.length;i++)
list.add(a[i]);
Collections.sort(list);
int ind=0;
for(int x:list)
a[ind++]=x;
return a;
}
//function to print an array for debugging
static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
static void printc(char[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
//normal gcd function, always put the greater number as a and the smaller number as b
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int lcm(int a,int b)
{
return (a*b)/gcd(a,b);
}
//to find gcd and lcm for numbers of long data type
static long gcdl(long a, long b) {
if (b == 0)
return a;
return gcdl(b, a % b);
}
static long lcml(long a,long b)
{
return (a*b)/gcdl(a,b);
}
//Input Reader to read faster input
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;
}
int[] readArray(int n)
{ int[] a = new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
}
}
| 0 | Non-plagiarised |
6f393cfe | d3a0b8d2 | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
try{
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int lst[][] = new int[n][5];
for(int i=0; i<n; i++){
String s = br.readLine();
for(int j=0; j<s.length(); j++){
lst[i][s.charAt(j)-'a']++;
}
}
int fans = Integer.MIN_VALUE;
for(int i=0; i<5; i++){
int val[] = new int[n];
for(int k=0; k<n; k++){
int sum = 0;
for(int j=0; j<5; j++){
if(i==j){
sum += lst[k][j];
}else{
sum -= lst[k][j];
}
}
val[k] = sum;
}
Arrays.sort(val);
int sum = 0;
int ans = 0;
for(int x = n-1; x>=0; x--){
sum+=val[x];
if(sum>0){
ans++;
}else{
break;
}
}
fans = Math.max(fans, ans);
}
bw.write(fans+"\n");
}
bw.flush();
}catch(Exception e){
return;
}
}
} | import java.io.File;
import java.io.IOException;
import java.util.*;
public class C {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0){
int n = in.nextInt();
int[][] cnt = new int[n][5];
int[] len = new int[n];
for (int i = 0; i < n; i++){
String s = in.next();
len[i] = s.length();
for (char c : s.toCharArray()) cnt[i][c-'a']++;
}
int max = 0;
for (int i = 0; i < 5; i++){
int[] diff = new int[n];
for (int j = 0; j < n; j++) diff[j] = cnt[j][i] - (len[j] - cnt[j][i]);
Arrays.sort(diff);
int j = n-2, sum = diff[n-1];
while (j>=0 && sum > 0){
max = Math.max(max, n - 1 - j);
sum += diff[j]; j--;
}
if (sum > 0) max = Math.max(max, n);
}
System.out.println(max);
}
}
}
| 0 | Non-plagiarised |
8f6421f3 | ea2fc2bc | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Main{
public static void main (String[] args){
FastReader s = new FastReader();
int t=1;t=s.ni();
for(int test=1;test<=t;test++){
int n=s.ni(),k=s.ni();
int position[]=s.readArray(k),temp[]=s.readArray(k);
int ans[]=new int[n];
Arrays.fill(ans,Integer.MAX_VALUE/2);
for(int i=0;i<k;i++){
ans[position[i]-1]=temp[i];
}
for(int i=1;i<n;i++){
ans[i]=Math.min(ans[i-1]+1,ans[i]);
}
for(int i=n-2;i>=0;i--){
ans[i]=Math.min(ans[i],ans[i+1]+1);
}
for(int i=0;i<n;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
int[] readArray(int n){
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] =Integer.parseInt(next());
return a;
}
String next(){
while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());}
catch (IOException e){e.printStackTrace();}}return st.nextToken();}
String nextLine(){String str = "";try {str = br.readLine();}catch (IOException e)
{e.printStackTrace();}return str;}
}
}
| //package Practise;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class AirConditioner {
public static void main(String args[]) {
FastScanner fs=new FastScanner();
int t=fs.nextInt();
for(int t1=0;t1<t;t1++) {
int n=fs.nextInt();
int k=fs.nextInt();
int []arr1=new int[k];
int []arr2=new int[k];
arr1=fs.readArray(k);
arr2=fs.readArray(k);
int []dp=new int[n];
//for(int i=0;i<k;i++)
//dp[arr1[i]-1]=arr2[i];
Arrays.fill(dp,Integer.MAX_VALUE/2);
for(int i=0;i<k;i++) {
dp[arr1[i]-1]=arr2[i];
}
for(int i=1;i<n;i++) {
dp[i]=Math.min(dp[i],dp[i-1]+1);
}
//Print(dp);
for(int i=n-2;i>=0;i--) {
dp[i]=Math.min(dp[i], dp[i+1]+1);
}
//Print(dp);
/*for(int i=0;i<n;i++) {
int min=Integer.MAX_VALUE;
for(int j=0;j<k;j++) {
min=Math.min(min, arr2[j]+Math.abs(i-arr1[j]+1));
}
dp[i]=min;
}*/
for(int val:dp) {
System.out.print(val+" ");
}
System.out.println();
}
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void Print(int []arr) {
for(int val:arr)
System.out.print(val+" ");
System.out.println();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| 1 | Plagiarised |
4d279121 | a3e272af | import java.util.*;
import java.io.*;
@SuppressWarnings("unchecked")
public class Main {
public static void main(String args[]) {
FastReader input = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = input.nextInt();
while(t-- > 0) {
int n = input.nextInt();
int m = input.nextInt();
int x = input.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++) arr[i] = input.nextInt();
PriorityQueue<Pair> queue = new PriorityQueue();
out.println("YES");
int res[] = new int[n];
for(int i = 1; i <= m; i++) {
queue.add(new Pair(0, i));
}
for(int i = 0; i < n; i++) {
Pair p = queue.remove();
out.print(p.second()+" ");
queue.add(new Pair(p.first()+arr[i], p.second()));
}
out.println();
out.flush();
}
}
}
class Pair implements Comparable<Pair> {
public int x;
public int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int first() {
return x;
}
public int second() {
return y;
}
public int compareTo(Pair p) {
if(this.first() == p.first()) {
return 0;
}else if(this.first() > p.first()) {
return 1;
}else {
return -1;
}
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}catch(Exception e) {
System.out.println(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String getString() {
return next();
}
} |
import java.io.*;
import java.util.*;
public class Asd {
static PrintWriter w = new PrintWriter(System.out);
static FastScanner s = new FastScanner();
static boolean sd = false;
public static void main(String[] args) {
int t = s.nextInt();
//int t=1;
while (t-- > 0) {
solve();
}
w.close();
}
public static class Student {
public int i1;
public int value;
// A parameterized student constructor
public Student(int i1,int i2) {
this.i1 = i1;
this.value=i2;
}
public int getkey() {
return i1;
}
public int getValue() {
return value;
}
}
static class StudentComparator implements Comparator<Student>{
// Overriding compare()method of Comparator
// for descending order of cgpa
@Override
public int compare(Student s1, Student s2) {
if (s1.i1 < s2.i1)
return -1;
else if (s1.i1 >s2.i1)
return 1;
return 0;
}
}
/* Function to print all the permutations of the string
static String swap(String str, int i, int j)
{
char ch;
char[] array = str.toCharArray();
ch = array[i];
array[i] = array[j];
array[j] = ch;
return String.valueOf(array);
}
static void permute(String str,int low,int high)
{
if(low == high)
list.add(Long.parseLong(str));
int i;
for(i = low; i<=high; i++){
str = swap(str,low,i);
permute(str, low+1,high);
str = swap(str,low,i);
}
}
use permute(str2,0,str2.length()-1); to perform combinations
*/
public static void solve() {
int n=s.nextInt();
int m=s.nextInt();
int x=s.nextInt();
int arr[]=new int[n];int res[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=s.nextInt();
PriorityQueue<Student> pq=new PriorityQueue<Student>(new StudentComparator());
for(int i=0;i<m;i++){
pq.add(new Student(arr[i],i));res[i]=i;}
for(int i=m;i<n;i++)
{
Student s1=pq.poll();
int k2=s1.getkey()+arr[i];
int v2=s1.getValue();res[i]=v2;
pq.add(new Student(k2,v2));
}
w.println("YES");
for(int i=0;i<n;i++)
w.print(res[i]+1+" ");
w.println();
}
static void call(ArrayList<Integer> t, ArrayList<Integer> m) {
if (t.size() == 0 && m.size() == 0) {
sd = true;
return;
}
t.remove(0);
t.remove(t.size() - 1);
call(t, new ArrayList<Integer>(m.subList(0, m.size() - 1)));
call(t, new ArrayList<Integer>(m.subList(1, m.size())));
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int noofdivisors(int n) {
//it counts no of divisors of every number upto number n
int arr[] = new int[n + 1];
for (int i = 1; i <= (n); i++) {
for (int j = i; j <= (n); j = j + i) {
arr[j]++;
}
}
return arr[0];
}
static char[] reverse(char arr[]) {
char[] b = new char[arr.length];
int j = arr.length;
for (int i = 0; i < arr.length; i++) {
b[j - 1] = arr[i];
j = j - 1;
}
return b;
}
static long factorial(int n) {
long su = 1;
for (int i = 1; i <= n; i++) {
su *= (long) i;
}
return su;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
long[] readArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| 0 | Non-plagiarised |
1162c08f | 6bcc5afd | import java.util.*;
public class CodeForces1525C{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>();
int n = sc.nextInt(),dp[][]=new int[n+1][n+1];
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++)
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
System.out.println(dp[o.size()][e.size()]);
}
}
| import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n];
ArrayList<Integer> lt1=new ArrayList<>();
ArrayList<Integer> lt0=new ArrayList<>();
for(int i=0;i<n;i++)
{
int l=s.nextInt();
if(l==0)
lt0.add(i+1);
else
lt1.add(i+1);
}
int dp[][]=new int[lt1.size()+1][lt0.size()+1];
for(int i=1;i<=lt1.size();i++)
{
dp[i][i]=dp[i-1][i-1]+Math.abs(lt0.get(i-1)-lt1.get(i-1));
for(int j=i+1;j<=lt0.size();j++)
{
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(lt1.get(i-1)-lt0.get(j-1)));
}
}
System.out.println(dp[lt1.size()][lt0.size()]);
}
} | 1 | Plagiarised |
d9199dfd | fb312dc6 | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class Simple{
public static void main(String args[]){
//System.out.println("Hello Java");
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t>0){
int n = s.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = s.nextInt();
}
String str = s.next();
//Arrays.sort(arr);
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int i=0;i<n;i++){
if(str.charAt(i)=='R'){
red.add(arr[i]);
}
else{
blue.add(arr[i]);
}
}
Collections.sort(red);
Collections.sort(blue);
int start =1;
boolean bool =true;
for(int i=0;i<blue.size();i++){
if(blue.get(i)<start){
bool = false;
break;
}
start++;
}
if(!bool){
System.out.println("NO");
}
else{
for(int i=0;i<red.size();i++){
if(red.get(i)>start){
bool = false;
break;
}
start++;
}
if(bool){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
t--;
}
s.close();
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class D {
static class Pair{
int val;
char c;
Pair(int a,char b){
this.val=a;
this.c=b;
}
}
public static void main(String[] args)
{
FastScanner sc=new FastScanner();
int t=sc.nextInt();
PrintWriter pw=new PrintWriter(System.out);
while(t-->0) {
int n=sc.nextInt();
int[] a=sc.readArray(n);
char[] s=sc.next().toCharArray();
boolean ok=true;
ArrayList<Integer> blues=new ArrayList<>();
ArrayList<Integer> reds=new ArrayList<>();
for(int i=0;i<n;i++){
if(s[i]=='B'){
blues.add(a[i]);
} else {
reds.add(a[i]);
}
}
Collections.sort(blues);
Collections.sort(reds);
for(int i=0;i<blues.size();i++){
if(blues.get(i)<(i+1)){
ok=false;
break;
}
}
int start=blues.size()+1;
for(int i=0;i<reds.size();i++){
if(reds.get(i)>(start++)){
ok=false;
break;
}
}
if(ok){
pw.println("YES");
} else {
pw.println("NO");
}
}
pw.flush();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| 1 | Plagiarised |
16a2a867 | 3951966f | import java.util.*;
import java.io.*;
public class Armchairs{
public static void main(String[] args) {
FastReader fr = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Scanner sc= new Scanner (System.in);
//Code From Here----
int t =fr.nextInt();
ArrayList <Integer> chairs= new ArrayList<>();
ArrayList <Integer> free= new ArrayList<>();
for (int i = 0; i < t; i++) {
int state =fr.nextInt();
if(state==1)
{
chairs.add(i);
}
else{
free.add(i);
}
}
int [][] dp=new int [t][t];
for (int[] is : dp) {
Arrays.fill(is, -1);
}
int ans=solve(chairs,free,0,0,chairs.size(),dp);
out.println(ans);
out.flush();
sc.close();
}
//This RadixSort() is for long method
private static int solve(ArrayList<Integer> chairs, ArrayList<Integer> free, int i, int j, int size,int [][] dp) {
if (dp[i][j]!=-1) {
return dp[i][j];
}
if (size==0) {
return 0;
}
if (j==free.size()) {
return 10000000;
}
int a=solve(chairs, free, i, j+1, size,dp);
int b=Math.abs(chairs.get(i)-free.get(j))+solve(chairs, free, i+1, j+1, size-1,dp);
dp[i][j]=Math.min(a, b);
return dp[i][j];
}
public static long[] radixSort(long[] f){ return radixSort(f, f.length); }
public static long[] radixSort(long[] f, int n)
{
long[] to = new long[n];
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i];
long[] d = f; f = to; to = d;
}
return f;
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int lowerBound(long a[], long x) { // x is the target value or key
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x) r = m;
else l = m;
}
return r;
}
static int upperBound(long a[], long x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1L;
if (a[m] <= x) l = m;
else r = m;
}
return l + 1;
}
static int upperBound(long[] arr, int start, int end, long k) {
int N = end;
while (start < end) {
int mid = start + (end - start) / 2;
if (k >= arr[mid]) {
start = mid + 1;
} else {
end = mid;
}
}
if (start < N && arr[start] <= k) {
start++;
}
return start;
}
static long lowerBound(long[] arr, int start, int end, long k) {
int N = end;
while (start < end) {
int mid = start + (end - start) / 2;
if (k <= arr[mid]) {
end = mid;
} else {
start = mid + 1;
}
}
if (start < N && arr[start] < k) {
start++;
}
return start;
}
// For Fast Input ----
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;
}
}
}
|
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class Armchairs {
static ArrayList<Integer> f;
static ArrayList<Integer> u;
static int dp[][];
static int fun(int i, int j){
if(i == f.size()) return 0;
if(j == u.size()) return 99999999;
if(dp[i][j] != -1) return dp[i][j];
int ans1 = fun(i, j+1);
int ans2 = fun(i+1, j+1) + Math.abs(f.get(i)-u.get(j));
return dp[i][j] = Math.min(ans1, ans2);
}
private static int solve(int n, int a[]) {
for (int i = 0; i < n; i++) {
if (a[i]==0)
u.add(i);
else
f.add(i);
}
return fun(0,0);
}
public static void main(String[] args)
throws IOException {
Scanner s = new Scanner();
int t = 1;
StringBuilder ans = new StringBuilder();
int count = 0;
while (t-- > 0) {
int n = s.nextInt();
int a[] = new int[n];
dp=new int[n][n];
for (int i = 0; i < n; i++) {
a[i]=s.nextInt();
}
f=new ArrayList<>();
u=new ArrayList<>();
for( int i=0; i<n; i++) Arrays.fill(dp[i],-1);
ans.append(solve(n, a)).append("\n");
}
System.out.println(ans.toString());
}
static class Scanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Scanner() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Scanner(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static long norm(long a, long MOD) {
return ((a % MOD) + MOD) % MOD;
}
public static long msub(long a, long b, long MOD) {
return norm(norm(a, MOD) - norm(b, MOD), MOD);
}
public static long madd(long a, long b, long MOD) {
return norm(norm(a, MOD) + norm(b, MOD), MOD);
}
public static long mMul(long a, long b, long MOD) {
return norm(norm(a, MOD) * norm(b, MOD), MOD);
}
public static long mDiv(long a, long b, long MOD) {
return norm(norm(a, MOD) / norm(b, MOD), MOD);
}
}
| 0 | Non-plagiarised |
3a12e509 | 6de04ee2 | import java.io.*;
import java.util.*;
public class Practice
{
// static final long mod=7420738134811L;
static int mod=1000000007;
static final int size=501;
static FastReader sc=new FastReader(System.in);
// static Reader sc=new Reader();
static PrintWriter out=new PrintWriter(System.out);
static long[] factorialNumInverse;
static long[] naturalNumInverse;
static int[] sp;
static long[] fact;
static ArrayList<Integer> pr;
public static void main(String[] args) throws IOException
{
// System.setIn(new FileInputStream("input.txt"));
// System.setOut(new PrintStream("output.txt"));
// factorial(mod);
// InverseofNumber(mod);
// InverseofFactorial(mod);
// make_seive();
int t=1;
t=sc.nextInt();
while(t-->0)
solve();
out.close();
out.flush();
}
static void solve() throws IOException
{
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
String s=sc.next();
ArrayList<Integer> blue=new ArrayList<Integer>();
ArrayList<Integer> red=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='B')
blue.add(arr[i]);
else
red.add(arr[i]);
}
Collections.sort(blue);
Collections.sort(red);
for(int i=0;i<blue.size();i++)
{
if(blue.get(i)<i+1)
{
out.println("NO");
return;
}
}
for(int i=0;i<red.size();i++)
{
if(red.get(i)>i+1+blue.size())
{
out.println("NO");
return;
}
}
out.println("YES");
}
static class Pair implements Cloneable, Comparable<Pair>
{
int x,y;
Pair(int a,int b)
{
this.x=a;
this.y=b;
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof Pair)
{
Pair p=(Pair)obj;
return p.x==this.x && p.y==this.y;
}
return false;
}
@Override
public int hashCode()
{
return Math.abs(x)+500*Math.abs(y);
}
@Override
public String toString()
{
return "("+x+" "+y+")";
}
@Override
protected Pair clone() throws CloneNotSupportedException {
return new Pair(this.x,this.y);
}
@Override
public int compareTo(Pair a)
{
int t= this.x-a.x;
if(t!=0)
return t;
else
return this.y-a.y;
}
public void swap()
{
this.y=this.y+this.x;
this.x=this.y-this.x;
this.y=this.y-this.x;
}
}
static class Tuple implements Cloneable, Comparable<Tuple>
{
int x,y,z;
Tuple(int a,int b,int c)
{
this.x=a;
this.y=b;
this.z=c;
}
public boolean equals(Object obj)
{
if(obj instanceof Tuple)
{
Tuple p=(Tuple)obj;
return p.x==this.x && p.y==this.y && p.z==this.z;
}
return false;
}
@Override
public int hashCode()
{
return (this.x+501*this.y);
}
@Override
public String toString()
{
return "("+x+","+y+","+z+")";
}
@Override
protected Tuple clone() throws CloneNotSupportedException {
return new Tuple(this.x,this.y,this.z);
}
@Override
public int compareTo(Tuple a)
{
int x=this.z-a.z;
if(x!=0)
return x;
int X= this.x-a.x;
if(X!=0)
return X;
return a.y-this.y;
}
}
static void arraySort(int arr[])
{
ArrayList<Integer> a=new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
a.add(arr[i]);
}
Collections.sort(a);
for (int i = 0; i < arr.length; i++) {
arr[i]=a.get(i);
}
}
static void arraySort(long arr[])
{
ArrayList<Long> a=new ArrayList<Long>();
for (int i = 0; i < arr.length; i++) {
a.add(arr[i]);
}
Collections.sort(a);
for (int i = 0; i < arr.length; i++) {
arr[i]=a.get(i);
}
}
static HashSet<Integer> primeFactors(int n)
{
HashSet<Integer> ans=new HashSet<Integer>();
if(n%2==0)
{
ans.add(2);
while((n&1)==0)
n=n>>1;
}
for(int i=3;i*i<=n;i+=2)
{
if(n%i==0)
{
ans.add(i);
while(n%i==0)
n=n/i;
}
}
if(n!=1)
ans.add(n);
return ans;
}
static void make_seive()
{
sp=new int[size];
pr=new ArrayList<Integer>();
for (int i=2; i<size; ++i) {
if (sp[i] == 0) {
sp[i] = i;
pr.add(i);
}
for (int j=0; j<(int)pr.size() && pr.get(j)<=sp[i] && i*pr.get(j)<size; ++j)
sp[i * pr.get(j)] = pr.get(j);
}
}
public static void InverseofNumber(int p)
{
naturalNumInverse=new long[size];
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for(int i = 2; i < size; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p;
}
// Function to precompute inverse of factorials
public static void InverseofFactorial(int p)
{
factorialNumInverse=new long[size];
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// pre-compute inverse of natural numbers
for(int i = 2; i < size; i++)
factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to 200001
public static void factorial(int p)
{
fact=new long[size];
fact[0] = 1;
for(int i = 1; i < size; i++)
fact[i] = (fact[i - 1] * (long)i) % p;
}
// Function to return nCr % p in O(1) time
public static long Binomial(int N, int R)
{
if(R<0)
return 1;
// n C r = n!*inverse(r!)*inverse((n-r)!)
long ans = ((fact[N] * factorialNumInverse[R]) % mod * factorialNumInverse[N - R]) % mod;
return ans;
}
static int findXOR(int x) //from 0 to x
{
if(x<0)
return 0;
if(x%4==0)
return x;
if(x%4==1)
return 1;
if(x%4==2)
return x+1;
return 0;
}
static boolean isPrime(long x)
{
if(x==1)
return false;
if(x<=3)
return true;
if(x%2==0 || x%3==0)
return false;
for(int i=5;i<=Math.sqrt(x);i+=2)
if(x%i==0)
return false;
return true;
}
static long gcd(long a,long b)
{
return (b==0)?a:gcd(b,a%b);
}
static int gcd(int a,int b)
{
return (b==0)?a:gcd(b,a%b);
}
static class Node
{
int vertex;
HashSet<Edge> adj;
int taxC,taxD;
Node(int ver)
{
vertex=ver;
taxD=0;
taxC=0;
adj=new HashSet<Edge>();
}
@Override
public String toString()
{
return vertex+" ";
}
}
static class Edge
{
Node to;
int cost;
Edge(Node t,int c)
{
this.to=t;
this.cost=c;
}
@Override
public String toString() {
return "("+to.vertex+","+cost+") ";
}
}
static long power(long x, long y)
{
if(x<=0)
return 1;
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1; // y = y/2
x = (x * x) % mod;
}
return res;
}
static long binomialCoeff(long n, long k)
{
if(n<k)
return 0;
long res = 1;
// if(k>n)
// return 0;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (long i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
static 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();
}
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;
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
public void printarray(int arr[])
{
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
}
| import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
char[] c=sc.next().toCharArray();
Vector<Integer> l=new Vector<>(), r=new Vector<>();
for(int i=0;i<n;i++)
(c[i] == 'B' ? l : r).add(a[i]);
Collections.sort(l);
Collections.sort(r,Collections.reverseOrder());
boolean ok = true;
for(int i=0;i<l.size();i++)
if (l.get(i) < i + 1)
ok = false;
for(int i=0;i<r.size();i++)
if (r.get(i) > n - i)
ok = false;
System.out.print((ok ? "YES" : "NO")+'\n');
}
}
} | 0 | Non-plagiarised |
b728ba1d | d04e5afb | 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.util.Objects;
import java.util.Collections;
import java.io.InputStream;
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);
CPhoenixAndTowers solver = new CPhoenixAndTowers();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CPhoenixAndTowers {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
ArrayList<Pair<Integer, Integer>> a = new ArrayList<>();
for (int i = 0; i < n; ++i) {
a.add(new Pair<>(in.nextInt(), i));
}
Collections.sort(a);
int[] ans = new int[n];
int[] sum = new int[m];
int j = 1;
for (int i = 0; i < n; ++i) {
ans[a.get(i).y] = j;
sum[j - 1] += a.get(i).x;
j++;
if (j == m + 1) j = 1;
}
for (int i = 1; i < m; ++i) {
if (Math.abs(sum[i - 1] - sum[i]) > k) {
out.println("NO");
}
}
out.println("YES");
for (int e : ans) {
out.print(e + " ");
}
out.println();
}
}
static class Pair<U, V> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) x).compareTo(o.x);
if (value != 0) return value;
return ((Comparable<V>) y).compareTo(o.y);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return x.equals(pair.x) && y.equals(pair.y);
}
public int hashCode() {
return Objects.hash(x, y);
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public String 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 boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| 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.util.Objects;
import java.util.Collections;
import java.io.InputStream;
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);
CPhoenixAndTowers solver = new CPhoenixAndTowers();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CPhoenixAndTowers {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
ArrayList<Pair<Integer, Integer>> a = new ArrayList<>();
for (int i = 0; i < n; ++i) {
a.add(new Pair<>(in.nextInt(), i));
}
Collections.sort(a);
int[] ans = new int[n];
int[] sum = new int[m];
int j = 1;
for (int i = 0; i < n; ++i) {
ans[a.get(i).y] = j;
sum[j - 1] += a.get(i).x;
j++;
if (j == m + 1) j = 1;
}
for (int i = 1; i < m; ++i) {
if (Math.abs(sum[i - 1] - sum[i]) > k) {
out.println("NO");
}
}
out.println("YES");
for (int e : ans) {
out.print(e + " ");
}
out.println();
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public String 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 boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class Pair<U, V> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) x).compareTo(o.x);
if (value != 0) return value;
return ((Comparable<V>) y).compareTo(o.y);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return x.equals(pair.x) && y.equals(pair.y);
}
public int hashCode() {
return Objects.hash(x, y);
}
}
}
| 1 | Plagiarised |
ee270b2a | fc80cbe0 | import java.util.*;
public class D{
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
int q = scanner.nextInt();
while(q-- > 0){
int n = scanner.nextInt(),
k = scanner.nextInt();
int[] a = new int[k];
for(int i=0;i<k;i++){
a[i] = scanner.nextInt();
}
int[] t = new int[k];
for(int j=0;j<k;j++){
t[j] = scanner.nextInt();
}
long[] L = new long[n];
long[] R = new long[n];
for(int i=0;i<n;i++){
L[i] = Integer.MAX_VALUE;
R[i] = Integer.MAX_VALUE;
}
for(int i=0;i<k;i++){
L[a[i]-1] = t[i];
R[a[i]-1] = t[i];
}
long min = Integer.MAX_VALUE;
for(int i=0;i<n;i++){
L[i] = Math.min(min+1,L[i]);
min = L[i];
}
min = Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
R[i] = Math.min(min+1,R[i]);
min = R[i];
}
for(int i=0;i<n;i++){
System.out.print(Math.min(L[i],R[i]) + " ");
}
System.out.println();
}
}
}
| import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;
public class AirConditioner {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int input = sc.nextInt();
sc.nextLine();
for(int i = 0; i < input; i++){
String[] length = sc.nextLine().split(" ");
String[] index = sc.nextLine().split(" ");
String[] temperature = sc.nextLine().split(" ");
long[] boxes = findTemp(length, index,temperature);
for(int j = 0; j < boxes.length; j++){
if(j == 0) System.out.print(boxes[j]);
else System.out.print(" "+boxes[j]);
}
System.out.println();
sc.nextLine();
}
}
public static long[] findTemp(String[] length, String[] index,String[] temperature) {
long n = Long.parseLong(length[0]);
HashSet<Integer> set = new HashSet<>();
long airCond = Long.parseLong(length[1]);
long[] boxes = new long[(int)n];
Arrays.fill(boxes,Integer.MAX_VALUE);
for(int i = 0 ; i < index.length; i++){
boxes[Integer.parseInt(index[i]) - 1] = Long.parseLong(temperature[i]);
set.add(Integer.parseInt(index[i]) - 1);
// System.out.println(index[i]);
}
int prev = -1;
for(int i = 0 ; i < boxes.length; i++) {
//System.out.println(i +" "+set.contains(i));
if(set.contains(i)){
if(prev != - 1) boxes[i] = Math.min((Math.abs(i - prev) ) + boxes[prev],boxes[i]);
// System.out.println(i + " "+prev);
prev = i;
}
if(prev == -1) continue;
if(set.contains(i)){
prev = i;
}
else{
boxes[i] = (Math.abs(i - prev) ) + boxes[prev];
}
}
prev = boxes.length;
for(int i = boxes.length - 1; i >= 0; i--) {
if(set.contains(i)){
// System.out.println(i);
if(prev != boxes.length) boxes[i] = Math.min((Math.abs(i - prev) ) + boxes[prev],boxes[i]);
prev = i;
}
if(prev == boxes.length) continue;
else{
boxes[i] = Math.min((Math.abs(i - prev) ) + boxes[prev],boxes[i]);
}
}
return boxes;
}
}
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| 0 | Non-plagiarised |
18e2441c | b728ba1d |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Map;
import java.util.HashMap;
public class cf1515 {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
static class Task {
public void solve(InputReader in, PrintWriter out) {
int t = in.nextInt();
while (t-- != 0) {
int n = in.nextInt();
int m = in.nextInt();
int x = in.nextInt();
TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
for (int i = 0; i < n; i++) {
int j = in.nextInt();
if (!map.containsKey(j)) {
map.put(j, new ArrayList<Integer>());
}
map.get(j).add(i);
}
out.println("YES");
int[] ans = new int[n];
int sta = 0;
for (int s : map.keySet()) {
for (int i = 0; i < map.get(s).size(); i++) {
ans[map.get(s).get(i)] = (sta++) % m + 1;
}
}
for(int i=0;i<n;i++) {
out.print(ans[i]+" ");
}
out.println();
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreElements()) {
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());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
| 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.util.Objects;
import java.util.Collections;
import java.io.InputStream;
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);
CPhoenixAndTowers solver = new CPhoenixAndTowers();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CPhoenixAndTowers {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
ArrayList<Pair<Integer, Integer>> a = new ArrayList<>();
for (int i = 0; i < n; ++i) {
a.add(new Pair<>(in.nextInt(), i));
}
Collections.sort(a);
int[] ans = new int[n];
int[] sum = new int[m];
int j = 1;
for (int i = 0; i < n; ++i) {
ans[a.get(i).y] = j;
sum[j - 1] += a.get(i).x;
j++;
if (j == m + 1) j = 1;
}
for (int i = 1; i < m; ++i) {
if (Math.abs(sum[i - 1] - sum[i]) > k) {
out.println("NO");
}
}
out.println("YES");
for (int e : ans) {
out.print(e + " ");
}
out.println();
}
}
static class Pair<U, V> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) x).compareTo(o.x);
if (value != 0) return value;
return ((Comparable<V>) y).compareTo(o.y);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return x.equals(pair.x) && y.equals(pair.y);
}
public int hashCode() {
return Objects.hash(x, y);
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public String 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 boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| 0 | Non-plagiarised |
722e318f | b790ef12 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class TaskB {
static long mod = 1000000007;
static FastScanner scanner;
static final StringBuilder result = new StringBuilder();
public static void main(String[] args) {
// 2 : 1000000000
scanner = new FastScanner();
int T = scanner.nextInt();
for (int t = 0; t < T; t++) {
solve(t + 1);
result.append("\n");
}
System.out.println(result);
}
static void solve(int t) {
int n = scanner.nextInt();
int[] a = scanner.nextIntArray(n);
String s = scanner.nextToken();
List<Integer> blue = new ArrayList<>();
List<Integer> red = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'B') {
blue.add(a[i]);
} else {
red.add(a[i]);
}
}
Collections.sort(blue);
Collections.sort(red);
for (int i = 0; i < blue.size(); i++) {
if (blue.get(i) < i + 1) {
result.append("NO");
return;
}
}
for (int i = 0; i < red.size(); i++) {
if (red.get(i) > i + 1 + blue.size()) {
result.append("NO");
return;
}
}
result.append("YES");
}
static class WithIdx implements Comparable<WithIdx> {
int val, idx;
public WithIdx(int val, int idx) {
this.val = val;
this.idx = idx;
}
@Override
public int compareTo(WithIdx o) {
if (val == o.val) {
return Integer.compare(idx, o.idx);
}
return Integer.compare(val, o.val);
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
String[] nextStringArray(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++) res[i] = nextToken();
return res;
}
}
static class PrefixSums {
long[] sums;
public PrefixSums(long[] sums) {
this.sums = sums;
}
public long sum(int fromInclusive, int toExclusive) {
if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
return sums[toExclusive] - sums[fromInclusive];
}
public static PrefixSums of(int[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
public static PrefixSums of(long[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
}
static class ADUtils {
static void sort(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
static void reverse(int[] arr) {
int last = arr.length / 2;
for (int i = 0; i < last; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
static void sort(long[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
}
static class MathUtils {
static long[] FIRST_PRIMES = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013,
1019, 1021, 1031, 1033, 1039, 1049, 1051};
static long[] primes(int to) {
long[] all = new long[to + 1];
long[] primes = new long[to + 1];
all[1] = 1;
int primesLength = 0;
for (int i = 2; i <= to; i++) {
if (all[i] == 0) {
primes[primesLength++] = i;
all[i] = i;
}
for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j];
j++) {
all[(int) (i * primes[j])] = primes[j];
}
}
return Arrays.copyOf(primes, primesLength);
}
static long modpow(long b, long e, long m) {
long result = 1;
while (e > 0) {
if ((e & 1) == 1) {
/* multiply in this bit's contribution while using modulus to keep
* result small */
result = (result * b) % m;
}
b = (b * b) % m;
e >>= 1;
}
return result;
}
static long submod(long x, long y, long m) {
return (x - y + m) % m;
}
static long modInverse(long a, long m) {
long g = gcdF(a, m);
if (g != 1) {
throw new IllegalArgumentException("Inverse doesn't exist");
} else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
return modpow(a, m - 2, m);
}
}
static public long gcdF(long a, long b) {
while (b != 0) {
long na = b;
long nb = a % b;
a = na;
b = nb;
}
return a;
}
}
}
/*
5
3 2 3 8 8
2 8 5 10 1
*/ | import java.io.*;
import java.util.*;
public class Main {
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 obj = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int l = obj.nextInt();
while (l-- != 0) {
int n = obj.nextInt();
int[] num = new int[n];
for (int i = 0; i < n; i++) num[i] = obj.nextInt();
Vector<Integer> red = new Vector<>();
Vector<Integer> blue = new Vector<>();
String s = obj.next();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') red.add(num[i]);
else blue.add(num[i]);
}
Collections.sort(blue);
Collections.sort(red);
int c = 1, f = 0;
for (int i = 0; i < blue.size(); i++) {
if (blue.get(i) < c) {
f = 1;
break;
}
c++;
}
for (int i = 0; i < red.size(); i++) {
if (red.get(i) > c) {
f = 1;
break;
}
c++;
}
if (f == 0) out.println("YES");
else out.println("NO");
}
out.flush();
}
}
| 1 | Plagiarised |
3e8ff074 | d9199dfd | import java.io.*;
import java.util.*;
public class blue_red_permutation {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while (T-- > 0) {
int N = Integer.parseInt(br.readLine());
String line1[] = br.readLine().split(" ");
char line2[] = br.readLine().toCharArray();
Range ranges[] = new Range[N];
for (int i = 0; i < N; i++) {
int cnt = Integer.parseInt(line1[i]);
int low = line2[i] == 'B' ? Math.min(1, cnt) : Math.max(1, cnt);
int high = line2[i] == 'R' ? Math.max(N, cnt) : Math.min(N, cnt);
ranges[i] = new Range(low, high);
}
Arrays.sort(ranges);
boolean doable = true;
for (int i = 0; i < N; i++) {
if (!ranges[i].contains(i + 1)) { doable = false; break; }
}
System.out.println(doable ? "YES" : "NO");
}
}
static class Range implements Comparable<Range> {
final int low, high;
Range(int low,int high) {
this.low = low;
this.high = high;
}
public boolean contains(int val) {
return val >= this.low && val <= this.high;
}
@Override
public int compareTo(Range range) {
if (this.low < range.low) { return -1; }
else if (this.low == range.low) { return this.high - range.high; }
else { return +1; }
}
}
} | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class Simple{
public static void main(String args[]){
//System.out.println("Hello Java");
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t>0){
int n = s.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = s.nextInt();
}
String str = s.next();
//Arrays.sort(arr);
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int i=0;i<n;i++){
if(str.charAt(i)=='R'){
red.add(arr[i]);
}
else{
blue.add(arr[i]);
}
}
Collections.sort(red);
Collections.sort(blue);
int start =1;
boolean bool =true;
for(int i=0;i<blue.size();i++){
if(blue.get(i)<start){
bool = false;
break;
}
start++;
}
if(!bool){
System.out.println("NO");
}
else{
for(int i=0;i<red.size();i++){
if(red.get(i)>start){
bool = false;
break;
}
start++;
}
if(bool){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
t--;
}
s.close();
}
}
| 0 | Non-plagiarised |
d2eb953e | dcf09f37 | import java.util.*;
// import java.lang.*;
import java.io.*;
// THIS TEMPLATE MADE BY AKSH BANSAL.
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static boolean[] isPrime;
private static void primes(){
int num = (int)1e6; // PRIMES FROM 1 TO NUM
isPrime = new boolean[num];
for (int i = 2; i< isPrime.length; i++) {
isPrime[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(isPrime[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
isPrime[j] = false;
}
}
}
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
static ArrayList<Integer>[] adj;
static void getAdj(int n,int q, FastReader sc){
adj = new ArrayList[n+1];
for(int i=1;i<=n;i++){
adj[i] = new ArrayList<>();
}
for(int i=0;i<q;i++){
int a = sc.nextInt();
int b = sc.nextInt();
adj[a].add(b);
adj[b].add(a);
}
}
static PrintWriter out;
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ________________________________
int t = sc.nextInt();
StringBuilder output = new StringBuilder();
while (t-- > 0) {
int n = sc.nextInt();
pair = new int[n+1][2];
dp = new long[n+1][2];
for(int i=1;i<=n;i++){
pair[i][0]=sc.nextInt();
pair[i][1]=sc.nextInt();
}
getAdj(n, n-1, sc);
output.append(solver(n)).append("\n");
}
out.println(output);
// _______________________________
// int n = sc.nextInt();
// out.println(solver());
// ________________________________
out.flush();
}
static int[][] pair;
static long[][] dp;
public static long solver(int n) {
dfs(1, 0);
return Math.max(dp[1][0], dp[1][1]);
}
static void dfs(int node, int parent){
for(Integer child: adj[node]){
if(child!=parent){
dfs(child, node);
long left1 = Math.abs(pair[node][0]-pair[child][0]) + dp[child][0];
long right1 = Math.abs(pair[node][0]-pair[child][1]) + dp[child][1];
long left2 = Math.abs(pair[node][1]-pair[child][0]) + dp[child][0];
long right2 = Math.abs(pair[node][1]-pair[child][1]) + dp[child][1];
dp[node][0] += Math.max(left1, right1);
dp[node][1] += Math.max(left2, right2);
}
}
}
} | /*
* akshaygupta26
*/
import java.io.*;
import java.util.*;
public class E
{
static long dp[][];
static long val[][];
static ArrayList<Integer> arr[];
public static void main(String[] args)
{
FastReader sc=new FastReader();
StringBuffer ans=new StringBuffer();
int test=sc.nextInt();
while(test-->0)
{
int n=sc.nextInt();
arr=new ArrayList[n+1];
val=new long[n+1][2];
dp=new long[n+1][2];
for(int i=0;i<=n;i++) Arrays.fill(dp[i], -1);
for(int i=1;i<=n;i++) {
val[i][0]=sc.nextLong();val[i][1]=sc.nextLong();
}
for(int i=0;i<=n;i++) arr[i]=new ArrayList<>();
for(int i=0;i<n-1;i++) {
int a=sc.nextInt(),b=sc.nextInt();
arr[a].add(b);arr[b].add(a);
}
long max1=0,max2=0;
for(int ch:arr[1]) {
max1+=solve(ch,0,1);
max2+=solve(ch,1,1);
}
ans.append(Math.max(max1, max2)+"\n");
}
System.out.print(ans);
}
static long solve(int nd,int prv,int par) {
if(dp[nd][prv] != -1) return dp[nd][prv];
long l=Math.abs(val[par][prv]-val[nd][0]);long r=Math.abs(val[par][prv]-val[nd][1]);
for(int ch:arr[nd]) {
if(ch != par) {
l+=solve(ch,0,nd);//Take left
r+=solve(ch,1,nd);//Take right
}
}
return dp[nd][prv] =Math.max(l, r);
}
static long _gcd(long a,long b) {
if(b == 0) return a;
return _gcd(b,a%b);
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
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;
}
}
}
| 0 | Non-plagiarised |
8a041ef5 | bdf7bfb2 | // package com.company;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.jar.JarEntry;
public class Main{
/* || श्री गणेशाय नमः ||
@𝐚𝐮𝐭𝐡𝐨𝐫 𝐉𝐢𝐠𝐚𝐫 𝐍𝐚𝐢𝐧𝐮𝐣𝐢
𝐒𝐕𝐍𝐈𝐓- 𝐒𝐮𝐫𝐚𝐭
*/
public static void main(String args[]){
InputReader in=new InputReader(System.in);
TASK solver = new TASK();
int t=1;
t = in.nextInt();
for(int i=1;i<=t;i++)
{
solver.solve(in,i);
}
}
static class TASK {
void solve(InputReader in, int testNumber) {
int n = in.nextInt();
int a[][] = new int[n][5];
for(int i=0;i<n;i++)
{
char[] s = in.next().toCharArray();
for(int j=0;j<s.length;j++)
{
a[i][s[j]-'a']++;
}
}
int max=0;
int x[] = new int[n];
for(int j=0;j<=4;j++)
{
for(int i=0;i<n;i++)
{
x[i]=2*a[i][j];
for(int k=0;k<5;k++)
{
x[i]-=a[i][k];
}
}
Arrays.sort(x);
int c=0,sum=0;
for(int i=n-1;i>=0;i--)
{
sum+=x[i];
if(sum<=0)
break;
c++;
}
max=Math.max(max,c);
}
System.out.println(max);
}
}
static class pair{
int x ;
int y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
}
static class Maths {
static long gcd(long a, long b) {
if (a < b)
return gcd(b,a);
if (b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
}
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);
}
}
}
| import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
String[] s = new String[n];
for(int i=0; i<n; i++)
s[i] = sc.next();
int MAX = 0;
for(char c = 'a'; c <= 'e'; c++){
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top;
for(int i=0; i<n; ++i) {
int curChar = 0;
int otherChar = 0;
for(int j=0; j<s[i].length(); j++) {
if(s[i].charAt(j) == c)
curChar++;
else
otherChar++;
}
int diff = curChar - otherChar;
pq.add(diff);
}
int cur = 0;
int numberOfWords = 0;
while(!pq.isEmpty()){
if(cur + pq.peek() > 0){
cur += pq.poll();
numberOfWords++;
}else{
break;
}
}
MAX = Math.max(MAX, numberOfWords);
}
pw.println(MAX);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
| 0 | Non-plagiarised |
298ee961 | 392218ef | import java.util.*;
import java.io.*;
public class Main {
static void print(String s) {
System.out.print(s);
}
static void printLine(String s) {
System.out.println(s);
}
static double parseDouble(String s) {
return Double.parseDouble(s.trim());
}
static int parseInt(String s) {
return Integer.parseInt(s.trim());
}
static String[] split(String s) {
return s.split("\\s+");
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
StringTokenizer st;
int test = parseInt(br.readLine());
for (int t = 0; t < test; t++) {
int n = parseInt(br.readLine());
ArrayList<Integer> blue = new ArrayList<Integer>();
ArrayList<Integer> red = new ArrayList<Integer>();
String[] toks = split(br.readLine());
String color = br.readLine();
for (int i = 0; i < n; i++) {
if (color.charAt(i) == 'B') {
blue.add(parseInt(toks[i]));
} else {
red.add(parseInt(toks[i]));
}
}
int[] blueArr = new int[blue.size()];
int[] redArr = new int[red.size()];
for (int i = 0; i < blue.size(); i++) {
blueArr[i] = blue.get(i);
}
for (int i = 0; i < red.size(); i++) {
redArr[i] = red.get(i);
}
Arrays.sort(blueArr);
Arrays.sort(redArr);
int blueP = 0; // pointer
int redP = 0; // pointer
// blue -> decrease, red -> increase
int start = 1;
boolean unable = false;
while (start <= n) {
if (blueP < blueArr.length) {
if (blueArr[blueP] < start) {
unable = true;
printLine("NO");
break;
} else {
blueP++;
start++;
}
} else {
if (redArr[redP] > start) {
unable = true;
printLine("NO");
break;
} else {
redP++;
start++;
}
}
}
if (!unable) {
printLine("YES");
}
}
}
}
| import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static void sort(int a[]){
ArrayList<Integer> arr=new ArrayList<>();
for(int i=0;i<a.length;i++)arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)a[i]=arr.get(i);
}
public static void main (String[] args) throws java.lang.Exception
{
// Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// int t=1;
//t=sc.nextInt();
int t=Integer.parseInt(br.readLine());
while(--t>=0){
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)a[i]=Integer.parseInt(st.nextToken());
String s=br.readLine();
ArrayList<Integer> inc=new ArrayList<>();
ArrayList<Integer> dec=new ArrayList<>();
for(int i=0;i<n;i++){
if(s.charAt(i)=='R')inc.add(a[i]);
else dec.add(a[i]);
}
Collections.sort(dec);
Collections.sort(inc,Collections.reverseOrder());
int p=n;
boolean flag=false;
for(int i=0;i<inc.size();i++){
if(inc.get(i)>p)flag=true;
p--;
}
p=1;
for(int i=0;i<dec.size();i++){
if(dec.get(i)<p)flag=true;
p++;
}
if(flag)System.out.println("NO");
else System.out.println("YES");
}
}
}
| 0 | Non-plagiarised |
4be01508 | b7de5c19 | import java.util.*;
import java.io.*;
public class q3{
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static List<List<Edge>> list;
public static void main(String[] args){
int tt = fs.nextInt();
for (int t=0;t<tt;t++){
solve();
}
pw.close();
}
static void solve(){
int n = fs.nextInt();
list = new ArrayList<>();
for (int i=0;i<n;i++) list.add(new ArrayList<>());
for (int i=0;i<n-1;i++){
int from = fs.nextInt()-1, to = fs.nextInt()-1;
list.get(from).add(new Edge(to, i));
list.get(to).add(new Edge(from, i));
}
int start = -1;
for (int i=0;i<n;i++){
if (list.get(i).size() > 2){
pw.println(-1);
return;
}
else if (list.get(i).size() == 1){
start = i;
}
}
int[] ans = new int[n-1];
int prev = -1;
int cur = start;
int curWeight = 2;
while (true){
Edge now = list.get(cur).get(0);
if (now.node == prev){
if (list.get(cur).size() == 1){
break;
}
now = list.get(cur).get(1);
}
ans[now.index] = 5 - curWeight;
curWeight = ans[now.index];
prev = cur;
cur = now.node;
}
for (int i : ans){
pw.printf("%d ", i);
}
pw.println();
}
static class Edge {
int node;
int index;
public Edge(int node, int index){
this.node = node;
this.index = index;
}
}
// ----------input function----------
static void sort(int[] a){
ArrayList<Integer> L = new ArrayList<>();
for (int i : a) L.add(i);
Collections.sort(L);
for (int i = 0; i < a.length; i++) a[i] = L.get(i);
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while (!st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
int[] readArray(int n){
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong(){
return Long.parseLong(next());
}
}
}
| import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
ArrayList<ArrayList<Pair>> graph = new ArrayList<>();
for (int j = 0; j < n; j++) {
graph.add(new ArrayList<>());
}
for (int j = 0; j < n - 1; j++) {
int u;
int v;
u = scan.nextInt();
v = scan.nextInt();
u--;
v--;
graph.get(u).add(new Pair(v, j));
graph.get(v).add(new Pair(u, j));
}
boolean soluble = true;
int curV = 0;
int prevV = -1;
int[] ans = new int[n];
int prime = 2;
for (int j = 0; j < n; j++) {
ArrayList<Pair> list = graph.get(j);
if (list.size() > 2) {
soluble = false;
} else if (list.size() == 1) {
curV = j;
}
}
if (soluble) {
for (int j = 0; j < n - 1; j++) {
ArrayList<Pair> list = graph.get(curV);
for (int z = 0; z < list.size(); z++) {
if (list.get(z).vertex != prevV) {
ans[list.get(z).numberOfEdge] = prime;
prime = changePrime(prime);
prevV = curV;
curV = list.get(z).vertex;
break;
}
}
}
for (int j = 0; j < n - 1; j++) {
System.out.print(ans[j] + " ");
}
System.out.println();
} else {
System.out.println(-1);
}
}
}
public static int changePrime(int prime) {
if (prime == 2) {
prime = 3;
} else {
prime = 2;
}
return prime;
}
}
class Pair {
int vertex;
int numberOfEdge;
public Pair(int vertex, int numberOfEdge) {
this.vertex = vertex;
this.numberOfEdge = numberOfEdge;
}
}
| 0 | Non-plagiarised |
80c384e4 | 967b67b0 | import java.util.*;
public class Solution {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int t = in.nextInt();
while(t-- > 0)
solve();
}
private static void solve() {
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[k+1];
for(int i=1; i<=k; i++)
a[i] = in.nextInt();
long l[] = new long[n+1];
long r[] = new long[n+1];
for(int i=1; i<=n; i++) {
l[i] = Integer.MAX_VALUE;
r[i] = Integer.MAX_VALUE;
}
for(int i=1; i<=k; i++) {
l[a[i]] = in.nextInt();
r[a[i]] = l[a[i]];
}
for(int i=2;i<=n;i++) {
l[i] = Math.min(l[i-1]+1, l[i]);
}
for(int i=n-1;i>=0;i--) {
r[i] = Math.min(r[i+1]+1, r[i]);
}
for(int i=1;i<=n;i++) {
System.out.print(Math.min(l[i], r[i])+" ");
}
System.out.println();
}
}
| import java.util.*;
public class Solution {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int t = in.nextInt();
while(t-- > 0)
solve();
}
private static void solve() {
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[k+1];
for(int i=1; i<=k; i++)
a[i] = in.nextInt();
long l[] = new long[n+1];
long r[] = new long[n+1];
for(int i=1; i<=n; i++) {
l[i] = Integer.MAX_VALUE;
r[i] = Integer.MAX_VALUE;
}
for(int i=1; i<=k; i++) {
l[a[i]] = in.nextInt();
r[a[i]] = l[a[i]];
}
for(int i=2;i<=n;i++) {
l[i] = Math.min(l[i-1]+1, l[i]);
}
for(int i=n-1;i>=0;i--) {
r[i] = Math.min(r[i+1]+1, r[i]);
}
for(int i=1;i<=n;i++) {
System.out.print(Math.min(l[i], r[i])+" ");
}
System.out.println();
}
} | 1 | Plagiarised |
6f02c6d9 | c9159d9c |
import java.io.*;
import java.util.*;
public class Main {
static long mod = 1000000007;
static long inv(long a, long b) {
return 1 < a ? b - inv(b % a, a) * b / a : 1;
}
static long mi(long a) {
return inv(a, mod);
}
static InputReader sc = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] A = new int[n];
for (int i = 0; i < A.length; i++) {
A[i] = sc.nextInt();
}
String word = sc.next();
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'R') {
red.add(A[i]);
} else {
blue.add(A[i]);
}
}
Collections.sort(blue);
Collections.sort(red);
boolean possible = true;
int a = 1;
for (int i = 0; i < blue.size(); i++, a++) {
if (blue.get(i) < a) {
possible = false;
break;
}
}
for (int i = 0; i < red.size(); i++, a++) {
if (red.get(i) > a) {
possible = false;
break;
}
}
if (possible) out.println("YES");
else out.println("NO");
}
out.flush();
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return str;
}
}
public static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
public static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
}
| import java.util.*;
public class SolutionB {
public static long gcd(long a, long b){
if(b==0){
return a;
}
return gcd(b, a%b);
}
public static long gcdSum(long b){
long a = 0;
long temp = b;
while(temp!=0){
a = a + temp%10;
temp = temp/10;
}
return gcd(a,b);
}
public static class Pair{
Long a;
Long b;
public Pair(Long a, Long b) {
this.a = a;
this.b = b;
}
}
public static long factorial (long n){
if(n==0)
return 1;
else if(n==1)
return n;
return n * factorial(n-1);
}
public static long lcm (long n){
if(n<3)
return n;
return lcmForBig(n,n-1);
}
private static long lcmForBig(long n, long l) {
if(l==1)
return n;
n = (n * l) / gcd(n, l);
return lcmForBig(n, l-1);
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i =0;i<t;i++) {
int n = s.nextInt();
int arr [] = new int[n];
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int j=0;j<n;j++){
int num = s.nextInt();
arr[j]=num;
}
String color = s.next();
for(int j=0;j<n;j++){
if(color.charAt(j)=='B'){
blue.add(arr[j]);
}
else{
red.add(arr[j]);
}
}
Collections.sort(blue);
String ans = "YES";
int counter = 0;
for(int j=0;j<blue.size();j++){
int current = blue.get(j);
if (current<1){
ans="NO";
break;
}
if(current>counter){
counter++;
}
else{
ans="NO";
break;
}
}
if(ans=="NO"){
System.out.println(ans);
}
else{
int tempCounter = n+1;
Collections.sort(red);
for(int j=red.size()-1;j>=0;j--){
int current = red.get(j);
if(current>=tempCounter){
ans="NO";
break;
}
else{
tempCounter--;
}
}
if(tempCounter-counter!=1)
System.out.println("NO");
else
System.out.println(ans);
}
}
return;
}
}
| 0 | Non-plagiarised |
00af3420 | 5449d33c | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
String[] s = new String[n];
for(int i=0; i<n; i++)
s[i] = sc.next();
int MAX = 0;
for(char c = 'a'; c <= 'e'; c++){
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top;
for(int i=0; i<n; ++i) {
int curChar = 0;
int otherChar = 0;
for(int j=0; j<s[i].length(); j++) {
if(s[i].charAt(j) == c)
curChar++;
else
otherChar++;
}
int diff = curChar - otherChar;
pq.add(diff);
}
int cur = 0;
int numberOfWords = 0;
while(!pq.isEmpty()){
if(cur + pq.peek() > 0){
cur += pq.poll();
numberOfWords++;
}else{
break;
}
}
MAX = Math.max(MAX, numberOfWords);
}
pw.println(MAX);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
| import java.io.*;
import java.util.*;
import java.lang.*;
public class C {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.Main(in, out);
out.close();
}
static class Solver {
public void Main(InputReader in, PrintWriter out) {
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int n = in.nextInt();
String[] A = new String[n];
for (int i = 0; i < n; i++) {
A[i] = in.next();
}
int ans = 0;
for (char c = 'a'; c <= 'e'; c++) {
int[] ls = new int[n];
for (int i = 0; i < n; i++) {
int delta = 0;
for (int j = 0; j < A[i].length(); j++) {
if (A[i].charAt(j) == c) {
delta += 1;
} else {
delta -= 1;
}
}
ls[i] = delta;
}
Arrays.sort(ls, 0, n);
int cur = 0;
int score = 0;
for (int i = n - 1; i >= 0; i--) {
if (cur + ls[i] >= 1) {
cur += ls[i];
score += 1;
}
}
ans = Math.max(ans, score);
}
out.println(ans);
}
}
}
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());
}
}
}
| 0 | Non-plagiarised |
2b2d3b84 | 9fd33d5a |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main
{
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 in = new FastReader();
int t=in.nextInt();
while(t-->0)
{
int n=in.nextInt();
int k=in.nextInt();
int a[]=new int[k];
int ans[]=new int[n];
int tem[]=new int[k];
for(int i=0;i<k;i++)
a[i]=in.nextInt();
for(int i=0;i<k;i++)
tem[i]=in.nextInt();
long c[]=new long[n];
long l[]=new long[n];
long r[]=new long[n];
Arrays.fill(c,Integer.MAX_VALUE);
Arrays.fill(l, Integer.MAX_VALUE);
Arrays.fill(r,Integer.MAX_VALUE);
long p=Integer.MAX_VALUE;
for(int i=0;i<k;i++)
c[a[i]-1]=tem[i];
for(int i=0;i<n;i++)
{
p=Math.min(p+1,c[i]);
l[i]=p;
}
p=Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--)
{
p=Math.min(p+1,c[i]);
r[i]=p;
}
for(int i=0;i<n;i++)
System.out.print(Math.min(l[i],r[i])+" ");
System.out.println();
}
}
}
| import java.util.*;
import java.io.*;
public class Solution {
private static ArrayList<Integer> prime = new ArrayList<>();
public static void main(String[] args) throws IOException {
Scanner in=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuffer out = new StringBuffer();
int T = in.nextInt();
OUTER:
while(T-->0) {
int n=in.nextInt(), k=in.nextInt();
int a[]=new int[k];
for(int i=0; i<k; i++) {
a[i]=in.nextInt()-1;
}
int t[]=new int[k];
long ans[]=new long[n];
for(int i=0; i<k; i++) {
t[i]=in.nextInt();
ans[a[i]]=t[i];
}
long temp=Integer.MAX_VALUE;
long left[]=new long[n];
for(int i=0; i<n; i++) {
if(ans[i]!=0) {
temp=Math.min(temp, ans[i]);
}
left[i]=temp;
temp+=1;
}
temp=Integer.MAX_VALUE;
long right[]=new long[n];
for(int i=n-1; i>=0; i--) {
if(ans[i]!=0) {
temp=Math.min(temp, ans[i]);
}
right[i]=temp;
temp+=1;
}
for(int i=0; i<n; i++) {
ans[i]=Math.min(left[i], right[i]);
out.append(ans[i]+" ");
}
out.append("\n");
}
System.out.print(out);
}
private static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
private static long gcd(long a, long b) {
if (a==0)
return b;
return gcd(b%a, a);
}
private static int toInt(String s) {
return Integer.parseInt(s);
}
private static long toLong(String s) {
return Long.parseLong(s);
}
} | 0 | Non-plagiarised |
4ea10951 | 7c11511f | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static String f1(String s, int index) {
StringBuffer sb = new StringBuffer();
for(int i =0;i<s.length();i++){
if( i == index) sb.append(s.charAt(i));
else if (s.charAt(i) == '0') sb.append('1');
else sb.append('0');
}
return sb.toString();
}
static long solve(String s1, String s2) {
if(s1.equals(s2)) return 0;
int o1 = 0, o2 = 0, diff = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == '1') o1++;
if (s2.charAt(i) == '1') o2++;
if (s1.charAt(i) != s2.charAt(i)) diff++;
}
return o1 == o2? diff : Integer.MAX_VALUE;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
String s1 = br.readLine();
String s2 = br.readLine();
long res = solve(s1, s2);
for(int i=0;i<s1.length();i++) {
if(s1.charAt(i) == '1' && s2.charAt(i) == '1') {
res = Math.min(res, 1 + solve(f1(s1, i), s2));
break;
}
}
for(int i=0;i<s1.length();i++) {
if(s1.charAt(i) == '1' && s2.charAt(i) == '0') {
res = Math.min(res,1 + solve(f1(s1, i), s2));
break;
}
}
if(res == Integer.MAX_VALUE) System.out.println("-1");
else System.out.println(res);
}
}
} | import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
int tc = io.nextInt();
for (int i = 0; i < tc; i++) {
solve();
}
io.close();
}
private static void solve() throws Exception {
int n = io.nextInt();
String a = io.next();
String b = io.next();
int zz = 0;
int zo = 0;
int oo = 0;
int oz = 0;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == '1' && b.charAt(i) == '0') {
oz++;
}
if (a.charAt(i) == '0' && b.charAt(i) == '0') {
zz++;
}
if (a.charAt(i) == '1' && b.charAt(i) == '1') {
oo++;
}
if (a.charAt(i) == '0' && b.charAt(i) == '1') {
zo++;
}
}
int ans = Integer.MAX_VALUE;
if (oz == zo) {
ans = Math.min(ans, oz + zo);
}
if (oo - 1 == zz) {
ans = Math.min(ans, oo + zz);
}
if (ans == Integer.MAX_VALUE) {
ans = -1;
}
io.println(ans);
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>(a.length);
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
//-----------PrintWriter for faster output---------------------------------
public static FastIO io = new FastIO();
//-----------MyScanner class for faster input----------
static class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public String nextLine() {
int c;
do {
c = nextByte();
} while (c < '\n');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > '\n');
return res.toString();
}
public int nextInt() {
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() {
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
//--------------------------------------------------------
} | 0 | Non-plagiarised |
6de04ee2 | d9199dfd | import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
char[] c=sc.next().toCharArray();
Vector<Integer> l=new Vector<>(), r=new Vector<>();
for(int i=0;i<n;i++)
(c[i] == 'B' ? l : r).add(a[i]);
Collections.sort(l);
Collections.sort(r,Collections.reverseOrder());
boolean ok = true;
for(int i=0;i<l.size();i++)
if (l.get(i) < i + 1)
ok = false;
for(int i=0;i<r.size();i++)
if (r.get(i) > n - i)
ok = false;
System.out.print((ok ? "YES" : "NO")+'\n');
}
}
} | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class Simple{
public static void main(String args[]){
//System.out.println("Hello Java");
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t>0){
int n = s.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = s.nextInt();
}
String str = s.next();
//Arrays.sort(arr);
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int i=0;i<n;i++){
if(str.charAt(i)=='R'){
red.add(arr[i]);
}
else{
blue.add(arr[i]);
}
}
Collections.sort(red);
Collections.sort(blue);
int start =1;
boolean bool =true;
for(int i=0;i<blue.size();i++){
if(blue.get(i)<start){
bool = false;
break;
}
start++;
}
if(!bool){
System.out.println("NO");
}
else{
for(int i=0;i<red.size();i++){
if(red.get(i)>start){
bool = false;
break;
}
start++;
}
if(bool){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
t--;
}
s.close();
}
}
| 0 | Non-plagiarised |
464a03b8 | c9159d9c | import java.util.*;
public class Soltion{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
Integer[] arr = new Integer[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
String s = sc.next();
List<Integer> blue = new ArrayList<>();
List<Integer> red = new ArrayList<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='B'){
blue.add(arr[i]);
}
else{
red.add(arr[i]);
}
}
Collections.sort(blue);
Collections.sort(red);
int p=1,q=n;
boolean flag = true;
for(int i=red.size()-1;i>=0;i--){
if(red.get(i)>q){
flag = false;
break;
}
q--;
}
for(int i=0;i<blue.size();i++){
if(blue.get(i)<p){
flag = false;
break;
}
p++;
}
System.out.println(flag? "Yes" : "No");
}
}
}
| import java.util.*;
public class SolutionB {
public static long gcd(long a, long b){
if(b==0){
return a;
}
return gcd(b, a%b);
}
public static long gcdSum(long b){
long a = 0;
long temp = b;
while(temp!=0){
a = a + temp%10;
temp = temp/10;
}
return gcd(a,b);
}
public static class Pair{
Long a;
Long b;
public Pair(Long a, Long b) {
this.a = a;
this.b = b;
}
}
public static long factorial (long n){
if(n==0)
return 1;
else if(n==1)
return n;
return n * factorial(n-1);
}
public static long lcm (long n){
if(n<3)
return n;
return lcmForBig(n,n-1);
}
private static long lcmForBig(long n, long l) {
if(l==1)
return n;
n = (n * l) / gcd(n, l);
return lcmForBig(n, l-1);
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i =0;i<t;i++) {
int n = s.nextInt();
int arr [] = new int[n];
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int j=0;j<n;j++){
int num = s.nextInt();
arr[j]=num;
}
String color = s.next();
for(int j=0;j<n;j++){
if(color.charAt(j)=='B'){
blue.add(arr[j]);
}
else{
red.add(arr[j]);
}
}
Collections.sort(blue);
String ans = "YES";
int counter = 0;
for(int j=0;j<blue.size();j++){
int current = blue.get(j);
if (current<1){
ans="NO";
break;
}
if(current>counter){
counter++;
}
else{
ans="NO";
break;
}
}
if(ans=="NO"){
System.out.println(ans);
}
else{
int tempCounter = n+1;
Collections.sort(red);
for(int j=red.size()-1;j>=0;j--){
int current = red.get(j);
if(current>=tempCounter){
ans="NO";
break;
}
else{
tempCounter--;
}
}
if(tempCounter-counter!=1)
System.out.println("NO");
else
System.out.println(ans);
}
}
return;
}
}
| 0 | Non-plagiarised |
8d6f1bf5 | e90a198b | //package Div2.C;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Menorah {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t > 0) {
int n = Integer.parseInt(br.readLine());
String source = br.readLine();
String destination = br.readLine();
int sameStatusOnes = 0;
int sameStatusZeros = 0;
int diffStatusOnes = 0;
int diffStatusZeros = 0;
for (int i = 0; i < n; i++) {
char c1 = source.charAt(i);
char c2 = destination.charAt(i);
if (c1 == c2) {
if (c1 == '0') {
sameStatusZeros += 1;
} else {
sameStatusOnes += 1;
}
} else {
if (c1 == '0') {
diffStatusZeros += 1;
} else {
diffStatusOnes += 1;
}
}
}
int sameStatus = sameStatusOnes + sameStatusZeros;
int diffStatus = diffStatusOnes + diffStatusZeros;
//first case
if (sameStatus == n) {
System.out.println(0);
} else if (diffStatus == n) {
//second case
if (diffStatus % 2 == 0 && diffStatusOnes == (n + 1) / 2)
System.out.println(n);
else
System.out.println(-1);
} else {
int op1 = -1;
int op2 = -1;
if (sameStatus % 2 != 0 && sameStatusOnes == (sameStatus + 1) / 2)
op1 = sameStatus;
if (diffStatus % 2 == 0 && diffStatusOnes == (diffStatus + 1) / 2)
op2 = diffStatus;
if (op1 != -1 && op2 != -1)
System.out.println(Integer.min(op1, op2));
else if (op1 != -1)
System.out.println(op1);
else if (op2 != -1)
System.out.println(op2);
else
System.out.println(-1);
}
t--;
}
}
}
| import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class Template {
static int mod = 1000000007;
public static void main(String[] args){
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int yo = sc.nextInt();
while (yo-- > 0) {
int n = sc.nextInt();
String s = sc.next();
String t = sc.next();
int op1 = cal(s,t,n);
int op2 = helper(s,t,n,'0');
int op3 = helper(s,t,n,'1');
int ans = min(min(op1,op2),op3);
if(ans == Integer.MAX_VALUE){
out.println("-1");
}
else {
out.println(ans);
}
}
out.close();
}
static int helper(String str1, String str2, int n, int ch){
char[] s = str1.toCharArray();
char[] t = str2.toCharArray();
int idx = -1;
for(int i = 0; i < n; i++){
if(s[i] == '1' && t[i] == ch){
idx = i;
break;
}
}
if(idx == -1){
return Integer.MAX_VALUE;
}
for(int i = 0; i < n; i++){
if(i == idx) continue;
if(s[i] == '1') s[i] = '0';
else s[i] = '1';
}
int ans = cal(String.valueOf(s),String.valueOf(t),n);
if(ans == Integer.MAX_VALUE) return ans;
return 1 + ans;
}
static int cal(String s, String t, int n){
int op01 = 0;
int op10 = 0;
for(int i = 0; i < n; i++){
if(s.charAt(i) != t.charAt(i)){
if(s.charAt(i) == '1') op10++;
else op01++;
}
}
if(op10 != op01){
return Integer.MAX_VALUE;
}
return op01 + op10;
}
/*
Source: hu_tao
Random stuff to try when stuck:
- use bruteforcer
- check for n = 1, n = 2, so on
-if it's 2C then it's dp
-for combo/probability problems, expand the given form we're interested in
-make everything the same then build an answer (constructive, make everything 0 then do something)
-something appears in parts of 2 --> model as graph
-assume a greedy then try to show why it works
-find way to simplify into one variable if multiple exist
-treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them)
-find lower and upper bounds on answer
-figure out what ur trying to find and isolate it
-see what observations you have and come up with more continuations
-work backwards (in constructive, go from the goal to the start)
-turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements)
-instead of solving for answer, try solving for complement (ex, find n-(min) instead of max)
-draw something
-simulate a process
-dont implement something unless if ur fairly confident its correct
-after 3 bad submissions move on to next problem if applicable
-do something instead of nothing and stay organized
-write stuff down
Random stuff to check when wa:
-if code is way too long/cancer then reassess
-switched N/M
-int overflow
-switched variables
-wrong MOD
-hardcoded edge case incorrectly
Random stuff to check when tle:
-continue instead of break
-condition in for/while loop bad
Random stuff to check when rte:
-switched N/M
-long to int/int overflow
-division by 0
-edge case for empty list/data structure/N=1
*/
public static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void sort(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
for (int i = 0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieve(int N) {
boolean[] sieve = new boolean[N + 1];
for (int i = 2; i <= N; i++)
sieve[i] = true;
for (int i = 2; i <= N; i++) {
if (sieve[i]) {
for (int j = 2 * i; j <= N; j += i) {
sieve[j] = false;
}
}
}
return sieve;
}
public static long power(long x, long y, long p) {
long res = 1L;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res;
}
public static void print(int[] arr, PrintWriter out) {
//for debugging only
for (int x : arr)
out.print(x + " ");
out.println();
}
public static int log2(int a){
return (int)(Math.log(a)/Math.log(2));
}
public static long ceil(long x, long y){
return (x + 0l + y - 1) / y;
}
static class FastScanner {
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
// For Input.txt and Output.txt
// FileInputStream in = new FileInputStream("input.txt");
// FileOutputStream out = new FileOutputStream("output.txt");
// PrintWriter pw = new PrintWriter(out);
// Scanner sc = new Scanner(in);
} | 0 | Non-plagiarised |
2e404969 | bc46480a | import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef {
static long fans[] = new long[200001];
static long inv[] = new long[200001];
static long mod = 1000000007;
static void init() {
fans[0] = 1;
inv[0] = 1;
fans[1] = 1;
inv[1] = 1;
for (int i = 2; i < 200001; i++) {
fans[i] = ((long) i * fans[i - 1]) % mod;
inv[i] = power(fans[i], mod - 2);
}
}
static long ncr(int n, int r) {
return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod;
}
public static void main(String[] args) throws java.lang.Exception {
FastReader in = new FastReader(System.in);
StringBuilder sb = new StringBuilder();
int t = 1;
t = in.nextInt();
while (t > 0) {
--t;
int n = in.nextInt();
long time[] = generateArray(in, n);
long hp[] = generateArray(in, n);
int s = 0;
long ans = 0;
while(s<time.length)
{
long l = time[s] - hp[s];
long r = time[s];
for(int i = s+1;i<n;i++)
{
if(time[i]-hp[i]<=l)
{
l = time[i]-hp[i];
r = time[i];
}
else if(time[i]-hp[i]<r)
{
r = time[i];
}
}
while(s<n && time[s]>=l && time[s]<=r)
{
++s;
}
long temp = r - l;
ans += (temp*(temp+1))/2;
}
sb.append(ans+"\n");
}
System.out.print(sb);
}
static long power(long x, long y) {
long res = 1; // Initialize result
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = ((res % mod) * (x % mod)) % mod;
// y must be even now
y = y >> 1; // y = y/2
x = ((x % mod) * (x % mod)) % mod; // Change x to x^2
}
return res;
}
static long[] generateArray(FastReader in, int n) throws IOException {
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = in.nextLong();
return arr;
}
static long[][] generatematrix(FastReader in, int n, int m) throws IOException {
long arr[][] = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = in.nextLong();
}
}
return arr;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
}
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 != 13; 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;
}
} | import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*/
public class Main {
static InputReader sc=new InputReader(System.in);
public static void main(String[] args) {
// Write your solution here
int t=sc.nextInt();
while(t-->0){
solve();
}
}
private static void solve() {
int n=sc.nextInt();
Node left[]=new Node[n];
int index=0;
Node ini[]=new Node[n];
int tmp[]=new int[n];
for(int i=0;i<n;i++){
tmp[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
ini[i]=new Node(tmp[i],tmp[i]-sc.nextInt()+1);
}
Arrays.sort(ini);
left[0]=ini[0];
for(int i=1;i<n;i++){
//System.out.println(ini[i].k+" "+ini[i].s);
if(ini[i].s<=left[index].k&&ini[i].k>left[index].k){
left[index].k=ini[i].k;
}else if(ini[i].s>left[index].k){
index++;
left[index]=ini[i];
}
}
long ans=0;
for(int i=0;i<=index;i++){
//System.out.println(left[i].k+" "+left[i].s);
ans+=(long)(left[i].k-left[i].s+2)*(left[i].k-left[i].s+1)/2;
}
System.out.println(ans);
}
}
class Node implements Comparable<Node>{
int k,s;
Node(int k,int s){
this.s=s;
this.k=k;
}
@Override
public int compareTo(Node node) {
return this.s-node.s;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
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 {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public 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 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 boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
| 0 | Non-plagiarised |
42fe7dd0 | c4ca2ff3 | import java.util.*;
public class Solution {
static Scanner sc=new Scanner(System.in);
public static void main(String args[]) {
int t=sc.nextInt();
outer:while(t-->0){
int n=sc.nextInt();
int[][] ct=new int[n][5];
int[] len=new int[n];
for (int i=0;i<n;i++) {
String s=sc.next();
len[i]=s.length();
for(char c:s.toCharArray()){
ct[i][c-'a']++;
}
}
int mx=0;
for (int i=0;i<5;i++) {
int[] diff=new int[n];
for (int j=0;j<n;j++) {
diff[j]=ct[j][i]-(len[j]-ct[j][i]);
}
Arrays.sort(diff);
int sum=0,inc=0;
for(int j=n-1;j>=0;j--){
sum+=diff[j];
if (sum>0) {
inc++;
}else {
break;
}
}
mx=Math.max(mx,inc);
}
System.out.println(mx);
}
}
}
| /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
PrintWriter out=new PrintWriter(System.out);
while(t-->0) {
int n=sc.nextInt();
int freq[][]=new int[n][5];
int rem[][]=new int[n][5];
for(int i=0;i<n;i++) {
String str=sc.next();
for(int j=0;j<str.length();j++) {
freq[i][str.charAt(j)-'a']++;
}
for(int k=0;k<5;k++) {
rem[i][k]=str.length()-freq[i][k];
}
}
int ans=0;
for(int i=0;i<5;i++) {
int arr[]=new int[n];
for(int j=0;j<n;j++)
arr[j]=freq[j][i]-rem[j][i];
Arrays.sort(arr);
int total=0;
int sum=0;
for(int k=n-1;k>=0;k--) {
if(sum+arr[k]>0) {
sum=sum+arr[k];
total++;
}
else {
break;
}
}
ans=Math.max(ans,total);
}
out.println(ans);
}
out.flush();
out.close();
}
}
| 1 | Plagiarised |
2eb89317 | 5f0a176e | import java.util.*;
import java.io.*;
////***************************************************************************
/* public class E_Gardener_and_Tree implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start();
}
public void run(){
WRITE YOUR CODE HERE!!!!
JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!!
}
}
*/
/////**************************************************************************
public class C_Menorah{
public static void main(String[] args) {
FastScanner s= new FastScanner();
//PrintWriter out=new PrintWriter(System.out);
//end of program
//out.println(answer);
//out.close();
StringBuilder res = new StringBuilder();
int t=s.nextInt();
int p=0;
while(p<t){
int n=s.nextInt();
String str1=s.nextToken();
String str2=s.nextToken();
if(str1.equals(str2)){
res.append("0 \n");
}
else{
long count1=0;
long count0=0;
for(int i=0;i<n;i++){
char ch=str1.charAt(i);
if(ch=='1'){
count1++;
}
}
count0=n-count1;
if(count1==0){
res.append("-1 \n");
}
else{
long nice1=0;
long nice0=0;
for(int i=0;i<n;i++){
char ch=str2.charAt(i);
if(ch=='1'){
nice1++;
}
}
nice0=(n-nice1);
int check1=0;
int check2=0;
if((count1==nice1)&&(count0==nice0)){
check1=1;
}
long yo1=(1+count0);
long yo0=(count1-1);
if((yo1==nice1)&&(yo0==nice0)){
check2=1;
}
if(check1==0 && check2==0){
res.append("-1 \n");
}
else{
//System.out.println("here");
long correct=0;
long wrong=0;
long correct1=0;
long correct0=0;
long wrong1=0;
long wrong0=0;
for(int i=0;i<n;i++){
char ch1=str1.charAt(i);
char ch2=str2.charAt(i);
if(ch1==ch2){
correct++;
if(ch1=='1'){
correct1++;
}
else{
correct0++;
}
}
else{
wrong++;
if(ch1=='1'){
wrong1++;
}
else{
wrong0++;
}
}
}
long ans1= solve(correct1,correct0,wrong1,wrong0,1);
long ans2= solve(correct1,correct0,wrong1,wrong0,0);
long ans=Math.min(ans1,ans2);
if(ans>=Integer.MAX_VALUE){
ans=-1;
}
res.append(ans+" \n");
}
}
}
p++;
}
System.out.println(res);
}
private static long solve( long correct1, long correct0, long wrong1, long wrong0,long a) {
long op1=Integer.MAX_VALUE;
long op2=Integer.MAX_VALUE;
if(wrong1==0 && wrong0==0){
return 0;
}
if(a==1){
{
// using correct1
if(correct1>0){
long newcorrect1=1+wrong0;
long newcorrect0=wrong1;
long newwrong1=correct0;
long newwrong0=correct1-1;
op1=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,0));
}
}
}
else{
{
//using wrong1
{
if(wrong1>0){
long newcorrect1=wrong0;
long newcorrect0=wrong1-1;
long newwrong1=1+correct0;
long newwrong0=correct1;
op2=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,1));
}
}
}
}
long ans=Math.min(op1,op2);
return ans;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
static long modpower(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// SIMPLE POWER FUNCTION=>
static long power(long x, long y)
{
long res = 1; // Initialize result
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = res * x;
// y must be even now
y = y >> 1; // y = y/2
x = x * x; // Change x to x^2
}
return res;
}
} |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class a729 {
public static void main(String[] args) throws IOException {
// try {
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter pt = new PrintWriter(System.out);
FastReader sc = new FastReader();
int t = sc.nextInt();
for(int o = 0 ; o<t;o++){
int n = sc.nextInt();
String s1 = sc.next();
String s2 = sc.next();
if(s1.equals(s2)) {
System.out.println(0);
continue;
}
int f = 0;
for(int i = 0 ; i<n;i++) {
if(s1.charAt(i) == '1') {
f = 1;
break;
}
}
if(f == 0) {
System.out.println(-1);
continue;
}
int c = 0;
int c1 = 0;
int c2 = 0;
int c3 = 0;
int c4 = 0;
for(int i = 0; i<n;i++) {
if(s1.charAt(i)!=s2.charAt(i)) {
c++;
if(s1.charAt(i) == '1') {
c1++;
}else {
c2++;
}
}else {
if(s1.charAt(i) == '1') {
c3++;
}else {
c4++;
}
}
}
int g = 0;
int h = 0;
int v1 = Integer.MAX_VALUE;
int v2 = Integer.MAX_VALUE;
if(c%2==1) {
g = 1;
}
int e = n-c;
if(e%2==0) {
h = 1;
}
//System.out.println(Math.min(v1, v2));
if(c2!=c/2){
g = 1;
}
if(c4!=e/2){
h = 1;
}
if(g == 0){
v1 = c;
}
if(h == 0){
v2 = e;
}
int ans = Math.min(v1, v2);
if(ans == Integer.MAX_VALUE) {
System.out.println(-1);
}else {
System.out.println(ans);
}
}
// }catch(Exception e) {
// return;
// }
}
//------------------------------------------------------------------------------------------------------------------------------------------------
public static boolean check(int[][] arr , int m , int n , int val){
HashSet<Integer>set = new HashSet<Integer>();
int f = 0;
int id = 0;
ArrayList<Integer> al = new ArrayList<Integer>();
for(int i = 0 ; i<m;i++) {
for(int j = 0 ; j<n;j++) {
if(arr[i][j]>=val) {
// System.out.println(arr[i][j] + " " + i + " " + j);
set.add(j);
al.add(arr[i][j]);
}
}
id = i;
if(set.size()>=2) {
f = 1;
break;
}else {
set.clear();
}
}
if(f == 0) {
return false;
}
// System.out.println(set);
// System.out.println(al);
// int g = 1;
for(int i = 0 ; i<n;i++) {
if(set.contains(i)) {
continue;
}
int h= 0;
for(int j = 0 ; j<m;j++) {
if(j == id) {
continue;
}
if(arr[j][i]>=val) {
h = 1;
break;
}
}
if(h == 0) {
return false;
}
}
return true;
}
static int cntDivisors(int n){
int cnt = 0;
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
cnt++;
else
cnt+=2;
}
}
return cnt;
}
public static long findgcd(long[] arr,int v) {
int n = arr.length;
long g = arr[v];
for(int i = v ; i<n;i+=2) {
g = gcd(g, arr[i]);
}
return g;
}
public static long ncr(long[] fac, int n , int r , long m) {
return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m;
}
public static void build(int [][] seg,char []arr,int idx, int lo , int hi) {
if(lo == hi) {
// seg[idx] = arr[lo];
seg[idx][(int)arr[lo]-'a'] = 1;
return;
}
int mid = (lo + hi)/2;
build(seg,arr,2*idx+1, lo, mid);
build(seg,arr,idx*2+2, mid +1, hi);
//seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
for(int i = 0 ; i<27;i++) {
seg[idx][i] = seg[2*idx+1][i] + seg[2*idx + 2][i];
}
}
//for f/inding minimum in range
public static void query(int[][]seg,int[]ans,int idx , int lo , int hi , int l , int r) {
if(lo>=l && hi<=r) {
for(int i = 0 ; i<27;i++) {
ans[i]+= seg[idx][i];
}
return ;
}
if(hi<l || lo>r) {
return;
}
int mid = (lo + hi)/2;
query(seg,ans,idx*2 +1, lo, mid, l, r);
query(seg,ans,idx*2 + 2, mid + 1, hi, l, r);
//return Math.min(left, right);
}
//// for sum
//
public static void update(int[][]seg,char[]arr,int idx, int lo , int hi , int node , char val) {
if(lo == hi) {
// seg[idx] += val;
seg[idx][val-'a']++;
seg[idx][arr[node]-'a']--;
}else {
int mid = (lo + hi )/2;
if(node<=mid && node>=lo) {
update(seg,arr, idx * 2 +1, lo, mid, node, val);
}else {
update(seg,arr, idx*2 + 2, mid + 1, hi, node, val);
}
//seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2];
for(int i = 0 ; i<27;i++) {
seg[idx][i] = seg[2*idx+1][i] + seg[2*idx + 2][i];
}
}
}
public static int lower_bound(int array[], int low, int high, int key){
// Base Case
if (low > high) {
return low;
}
// Find the middle index
int mid = low + (high - low) / 2;
// If key is lesser than or equal to
// array[mid] , then search
// in left subarray
if (key <= array[mid]) {
return lower_bound(array, low,
mid - 1, key);
}
// If key is greater than array[mid],
// then find in right subarray
return lower_bound(array, mid + 1, high,
key);
}
public static int upper_bound(int arr[], int low,
int high, int key){
// Base Case
if (low > high || low == arr.length)
return low;
// Find the value of middle index
int mid = low + (high - low) / 2;
// If key is greater than or equal
// to array[mid], then find in
// right subarray
if (key >= arr[mid]) {
return upper_bound(arr, mid + 1, high,
key);
}
// If key is less than array[mid],
// then find in left subarray
return upper_bound(arr, low, mid - 1,
key);
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
public static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public static long modInverse(long a, long m){
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
//-----------------------------------------------------------------------------------------------------------------------------------
//segment tree
//for finding minimum in range
// public static void build(int [] seg,int []arr,int idx, int lo , int hi) {
// if(lo == hi) {
// seg[idx] = arr[lo];
// return;
// }
// int mid = (lo + hi)/2;
// build(seg,arr,2*idx+1, lo, mid);
// build(seg,arr,idx*2+2, mid +1, hi);
// seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
// }
////for finding minimum in range
//public static int query(int[]seg,int idx , int lo , int hi , int l , int r) {
// if(lo>=l && hi<=r) {
// return seg[idx];
// }
// if(hi<l || lo>r) {
// return Integer.MAX_VALUE;
// }
// int mid = (lo + hi)/2;
// int left = query(seg,idx*2 +1, lo, mid, l, r);
// int right = query(seg,idx*2 + 2, mid + 1, hi, l, r);
// return Math.min(left, right);
//}
// // for sum
//
//public static void update(int[]seg,int idx, int lo , int hi , int node , int val) {
// if(lo == hi) {
// seg[idx] += val;
// }else {
//int mid = (lo + hi )/2;
//if(node<=mid && node>=lo) {
// update(seg, idx * 2 +1, lo, mid, node, val);
//}else {
// update(seg, idx*2 + 2, mid + 1, hi, node, val);
//}
//seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2];
//
//}
//}
//---------------------------------------------------------------------------------------------------------------------------------------
static void shuffleArray(long[] ar)
{
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
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;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
class SegmentTree{
int n;
public SegmentTree(int[] arr,int n) {
this.arr = arr;
this.n = n;
}
int[] arr = new int[n];
// int n = arr.length;
int[] seg = new int[4*n];
void build(int idx, int lo , int hi) {
if(lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi)/2;
build(2*idx+1, lo, mid);
build(idx*2+2, mid +1, hi);
seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
}
int query(int idx , int lo , int hi , int l , int r) {
if(lo<=l && hi>=r) {
return seg[idx];
}
if(hi<l || lo>r) {
return Integer.MAX_VALUE;
}
int mid = (lo + hi)/2;
int left = query(idx*2 +1, lo, mid, l, r);
int right = query(idx*2 + 2, mid + 1, hi, l, r);
return Math.min(left, right);
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
class coup{
char a;
int b;
public coup(char a , int b) {
this.a = a;
this.b = b;
}
}
class trip{
int a , b, c;
public trip(int a , int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | Non-plagiarised |
00db6701 | 0c1143f7 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int t=fs.nextInt();
while(t-->0)
{
int n=fs.nextInt();
int c[]=fs.readArray(n);
int i;
long mn0=c[0];
long mn1=c[1];
long s0=c[0];
long s1=c[1];
long ans;
ans=(mn0+mn1)*n*1L;
for(i=2;i<n;i++)
{
if(i%2==0)
{
s0+=c[i];
mn0=Math.min(mn0,c[i]);
}
else
{
s1+=c[i];
mn1=Math.min(mn1,c[i]);
}
ans=Math.min(ans,s0+mn0*(n-i/2-1)+s1+mn1*(n-(i+1)/2));
}
out.println(ans);
}
out.flush();
out.close();
}
static void sort(int a[]){
ArrayList<Integer> al=new ArrayList<>();
for(int x:a)al.add(x);
Collections.sort(al);
for(int i=0;i<a.length;i++) a[i]=al.get(i);
}
}
class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
String str="";
try {
str=br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
} | import java.io.*;
import java.util.*;
public class E {
public static void main(String[] args) {
// TODO Auto-generated method stub
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0 ; i< n ; i++) {
a[i] = sc.nextInt();
}
long oddMin = a[1];
long evenMin = a[0];
long res = (n * oddMin) + (n * evenMin);
long oddSum = a[1];
long evenSum = a[0];
for(int i = 2 ;i < n ; i++) {
if(i % 2 == 1) {
oddSum += a[i];
oddMin = Math.min(oddMin, a[i]);
}
else {
evenSum += a[i];
evenMin = Math.min(evenMin, a[i]);
}
int odd = (i + 1) / 2;
int even = (i / 2) + 1;
long minCostOdd = oddSum + oddMin*(n - odd);
long minCostEven = evenSum + evenMin*(n - even);
res = Math.min(res, minCostOdd + minCostEven);
}
System.out.println(res);
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
// Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2
// worst case since it uses a version of quicksort. Although this would never
// actually show up in the real world, in codeforces, people can hack, so
// this is needed.
static void ruffleSort(int[] a) {
//ruffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
}
| 0 | Non-plagiarised |
29cf2e70 | 2f4509a0 | import java.util.*;
import java.io.*;
public class D {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while(T-- > 0) {
int n = in.nextInt();
int[] a = new int[n];
for(int j=0;j<n;j++) a[j] = in.nextInt();
char[] s = in.next().toCharArray();
List<Integer> blue = new ArrayList<>();
List<Integer> red = new ArrayList<>();
for(int j=0;j<n;j++) {
if(s[j] == 'B') blue.add(a[j]);
else red.add(a[j]);
}
Collections.sort(blue);
Collections.sort(red);
boolean p = true;
int cur = 1;
for(int val : blue) {
if(val<cur) {
p = false;
break;
}
else cur++;
}
for(int val : red) {
if(val>cur) {
p = false;
break;
}
else cur++;
}
if(p) System.out.println("yes");
else System.out.println("no");
}
}
}
| //( ̄﹏ ̄;)
//(*  ̄︿ ̄)
import java.util.*;
import java.io.*;
public class Main {
private static FS sc = new FS();
private static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
private static class extra {
static int[] intArr(int size) {
int[] a = new int[size];
for(int i = 0; i < size; i++) a[i] = sc.nextInt();
return a;
}
static long[] longArr(int size) {
long[] a = new long[size];
for(int i = 0; i < size; i++) a[i] = sc.nextLong();
return a;
}
static long intSum(int[] a) {
long sum = 0;
for(int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
static long longSum(long[] a) {
long sum = 0;
for(int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
static LinkedList[] graphD(int vertices, int edges) {
LinkedList<Integer>[] temp = new LinkedList[vertices+1];
for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
for(int i = 0; i < edges; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
temp[x].add(y);
}
return temp;
}
static LinkedList[] graphUD(int vertices, int edges) {
LinkedList<Integer>[] temp = new LinkedList[vertices+1];
for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
for(int i = 0; i < edges; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
temp[x].add(y);
temp[y].add(x);
}
return temp;
}
static void printG(LinkedList[] temp) {
for(LinkedList<Integer> aa:temp) System.out.println(aa);
}
static long cal(long val, long pow, long mod) {
if(pow == 0) return 1;
long res = cal(val, pow/2, mod);
long ret = (res*res)%mod;
if(pow%2 == 0) return ret;
return (val*ret)%mod;
}
static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); }
}
static int mod = (int) 1e9 + 7;
static LinkedList<String>[] temp, temp2;
static int inf = (int) 1e9;
// static long inf = Long.MAX_VALUE;
public static void main(String[] args) {
int t = sc.nextInt();
// int t = 1;
StringBuilder ret = new StringBuilder();
while(t-- > 0) {
int n = sc.nextInt();
int[] a = extra.intArr(n);
String s = sc.next();
ArrayList<Integer> red = new ArrayList<>(), blue = new ArrayList<>();
for(int i = 0; i < n; i++) {
if(s.charAt(i) == 'R') red.add(a[i]);
else blue.add(a[i]);
}
Collections.sort(red);
Collections.sort(blue);
int start = 1, flag = 0;
for(int aa:blue) {
if(start > aa) {
flag = 1;
break;
}
start++;
}
for(int aa:red) {
if(start < aa) {
flag = 1;
break;
}
start++;
}
if(flag == 1) ret.append("NO\n");
else ret.append("YES\n");
}
System.out.println(ret);
}
}
| 1 | Plagiarised |
83b44c9c | e431de28 | import java.util.Scanner;
public class MinimumGridPath {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int test = scanner.nextInt();
StringBuilder sb = new StringBuilder();
for (int t = 0; t < test; t++) {
int n = scanner.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextLong();
}
long minx = arr[0];
long miny = arr[1];
long min = minx * n + miny * n;
long sumx = arr[0];
long sumy = arr[1];
for(int i=2; i<n; i++) {
int xc;
int yc;
if(i%2 == 0) {
xc = i/2 + 1;
yc = i/2;
sumx += arr[i];
minx = Math.min(minx, arr[i]);
} else {
xc = i/2 + 1;
yc = i/2 + 1;
sumy += arr[i];
miny = Math.min(miny, arr[i]);
}
min = Math.min(min, sumx + (n-xc) * minx + sumy + (n-yc) * miny);
}
sb.append(min).append(System.lineSeparator());
}
System.out.println(sb);
}
}
/*
// yehara
import java.io.PrintWriter;
import java.util.*;
public class C {
static Scanner sc;
static PrintWriter out;
public static void main(String[] args) throws Exception {
sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
for(int i=0; i<t; i++) {
solve();
}
out.flush();
}
static void solve() {
int n = sc.nextInt();
int[] c = new int[n];
for(int i=0; i<n; i++) {
c[i] = Integer.parseInt(sc.next());
}
long minx = c[0];
long miny = c[1];
long min = minx * n + miny * n;
long sumx = c[0];
long sumy = c[1];
for(int i=2; i<n; i++) {
int xc;
int yc;
if(i%2 == 0) {
xc = i/2 + 1;
yc = i/2;
sumx += c[i];
minx = Math.min(minx, c[i]);
} else {
xc = i/2 + 1;
yc = i/2 + 1;
sumy += c[i];
miny = Math.min(miny, c[i]);
}
min = Math.min(min, sumx + (n-xc) * minx + sumy + (n-yc) * miny);
}
out.println(min);
}
}
*/
| import java.io.*;
import java.lang.*;
import java.util.*;
public class MinGridPath {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0){
int n = s.nextInt();
long[] aa =new long[n];
for(int i=0;i<n;i++)
aa[i]=s.nextLong();
long minEven = aa[0];
long minOdd = aa[1];
long sum = aa[0]+aa[1];
long best = n*minEven + n*minOdd;
int numOdd = 1;
int numEven = 1;
for(int i=2; i<n; ++i) {
if(i%2 == 0) {
minEven = Math.min(aa[i], minEven);
numEven++;
}else {
minOdd = Math.min(aa[i], minOdd);
numOdd++;
}
sum += aa[i];
long score = sum;
score += minEven*(n-numEven);
score += minOdd*(n-numOdd);
best = Math.min(best, score);
}
System.out.println(best);
}
}
}
| 0 | Non-plagiarised |
a4c98ae1 | ee4f7b06 | import java.io.*;
import java.util.*;
public class Main {
static ArrayList<Integer> one=new ArrayList<>();
static ArrayList<Integer> zero=new ArrayList<>();
static long dp[][]= new long[5001][5001];
static long solve(int i,int j){
if (i==one.size())return 0;
if (j==zero.size())return Integer.MAX_VALUE;
if (dp[i][j]!=-1){
return dp[i][j];
}
return dp[i][j]=Math.min(solve(i+1,j+1)+Math.abs(one.get(i)-zero.get(j)),solve(i,j+1));
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
// int t=sc.nextInt();
// while (t-->=1){
int n=sc.nextInt();
int a[]=sc.readArray(n);
for (long i[]:dp){
Arrays.fill(i,-1);
}
for (int i=0;i<n;i++){
if (a[i]==1)one.add(i);
else zero.add(i);
}
Collections.sort(one);
Collections.sort(zero);
System.out.println(solve(0,0));
}
// out.flush();
//------------------------------------if-------------------------------------------------------------------------------------------------------------------------------------------------
//sieve
static void primeSieve(int a[]){
//mark all odd number as prime
for (int i=3;i<a.length;i+=2){
a[i]=1;
}
for (long i=3;i<a.length;i+=2){
//if the number is marked then it is prime
if (a[(int) i]==1){
//mark all muliple of the number as not prime
for (long j=i*i;j<a.length;j+=i){
a[(int)j]=0;
}
}
}
a[2]=1;
a[1]=a[0]=0;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static String sortString(String s) {
char temp[] = s.toCharArray();
Arrays.sort(temp);
return new String(temp);
}
static class Pair implements Comparable<Pair> {
String a;
int b;
public Pair(String a, int b) {
this.a = a;
this.b = b;
}
// to sort first part
// public int compareTo(Pair other) {
// if (this.a == other.a) return other.b > this.b ? -1 : 1;
// else if (this.a > other.a) return 1;
// else return -1;
// }
public int compareTo(Pair other) {
// if (this.b == other.b) return 0;
if (this.b > other.b) return 1;
else return -1;
}
//sort on the basis of first part only
// public int compareTo(Pair other) {
// if (this.a == other.a) return 0;
// else if (this.a > other.a) return 1;
// else return -1;
// }
}
static int[] frequency(String s){
int fre[]= new int[26];
for (int i=0;i<s.length();i++){
fre[s.charAt(i)-'a']++;
}
return fre;
}
static long LOWERBOUND(long a[],long k){
int s=0,e=a.length-1;
long ans=-1;
while (s<=e){
int mid=(s+e)/2;
if (a[mid]<=k){
ans=mid;
s=mid+1;
}
else {
e=mid-1;
}
}
return ans;
}
static long mod =(long)(1e9+7);
static long mod(long x) {
return ((x % mod + mod) % mod);
}
static long div(long a,long b){
return ((a/b)%mod+mod)%mod;
}
static long add(long x, long y) {
return mod(mod(x) + mod(y));
}
static long mul(long x, long y) {
return mod(mod(x) * mod(y));
}
//Fast Power(logN)
static int fastPower(long a,long n){
int ans=1;
while (n>0){
long lastBit=n&1;
if (lastBit==1){
ans=(int)mul(ans,a);
}
a=(int)mul(a,a);
n>>=1;
}
return ans;
}
static int[] find(int n, int start, int diff) {
int a[] = new int[n];
a[0] = start;
for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff;
return a;
}
static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}
static void printArray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
static boolean sorted(int a[]) {
int n = a.length;
boolean flag = true;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) flag = false;
}
if (flag) return true;
else return false;
}
public static int findlog(long n) {
if (n == 0)
return 0;
if (n == 1)
return 0;
if (n == 2)
return 1;
double num = Math.log(n);
double den = Math.log(2);
if (den == 0)
return 0;
return (int) (num / den);
}
public static long gcd(long a, long b) {
if (b % a == 0)
return a;
return gcd(b % a, a);
}
public static int gcdInt(int a, int b) {
if (b % a == 0)
return a;
return gcdInt(b % a, a);
}
static void sortReverse(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
// Collections.sort.(l);
Collections.sort(l, Collections.reverseOrder());
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(long n) {
long[] a = new long[(int) n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | import java.io.*;
import java.util.*;
import java.lang.Math;
public class Main {
static PrintWriter pw;
static Scanner sc;
static StringBuilder ans;
static long mod = 1000000000+7;
static void pn(final Object arg) {
pw.print(arg);
pw.flush();
}
/*-------------- for input in an value ---------------------*/
static int ni() { return sc.nextInt(); }
static long nl() { return sc.nextLong(); }
static double nd() { return sc.nextDouble(); }
static String ns() { return sc.next(); }
static void ap(int arg) { ans.append(arg); }
static void ap(long arg) { ans.append(arg); }
static void ap(String arg) { ans.append(arg); }
static void ap(StringBuilder arg) { ans.append(arg); }
/*-------------- for input in an array ---------------------*/
static void inputIntegerArray(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void inputLongArray(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void inputStringArray(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void inputDoubleArray(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*-------------- File vs Input ---------------------*/
static void runFile() throws Exception {
sc = new Scanner(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
}
static void runIo() throws Exception {
pw =new PrintWriter(System.out);
sc = new Scanner(System.in);
}
static void swap(int a, int b) {
int t = a;
a = b;
b = t;
}
static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);}
static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean sv[] = new boolean[1000002];
static void seive() {
//true -> not prime
// false->prime
sv[0] = sv[1] = true;
sv[2] = false;
for(int i = 0; i< sv.length; i++) {
if( !sv[i] && (long)i*(long)i < sv.length ) {
for ( int j = i*i; j<sv.length ; j += i ) {
sv[j] = true;
}
}
}
}
static long binpow( long a, long b) {
long res = 1;
while (b > 0) {
if ( (b & 1) > 0){
res = (res * a)%mod;
}
a = (a * a)%mod;
b >>= 1;
}
return res;
}
static long factorial(long n) {
long res = 1, i;
for (i = 2; i <= n; i++){
res = ((res%mod) * (i%mod))%mod;
}
return res;
}
static class Pair {
int idx;
int v;
Pair(int idx, int v){
this.idx = idx;
this.v = v;
}
}
public static void main(String[] args) throws Exception {
// runFile();
runIo();
int t;
t = 1;
// t = sc.nextInt();
ans = new StringBuilder();
while( t-- > 0 ) {
solve();
}
pn(ans+"");
}
static int N ;
static int M ;
static ArrayList<Integer> f;
static ArrayList<Integer> e;
static long dp[][];
static long find(int i, int j ) {
if( i == N ) return 0;
if( j == M ) return Integer.MAX_VALUE;
if (dp[i][j] != -1 )
return dp[i][j];
return dp[i][j] = Math.min( find(i, j+1), Math.abs(f.get(i)-e.get(j)) + find(i+1, j+1) );
}
public static void solve() {
int n = ni();
f = new ArrayList();
e = new ArrayList();
for(int i = 0; i<n; i++) {
int v = ni();
if( v == 0 ) {
e.add(i);
}
else
f.add(i);
}
N = f.size();
M = e.size();
dp = new long[N][M];
for(int i = 0; i<N; i++)
Arrays.fill(dp[i], -1);
ap(find(0, 0)+"\n");
}
}
// 0 1 2 3 4 5 6 7
// 1 1 0 0 0 1 0 0
// 1s -> { 0, 1, 5 }
// 0s -> { 2, 3, 6, 7 }
| 1 | Plagiarised |
3a318b43 | 90b71536 | import java.lang.reflect.Array;
import java.util.*;
import java.io.*;
import java.util.regex.Pattern;
public class B2 {
public static int getBit(int pos,int num){
int no = 1 << pos;
return no & num;
}
public static boolean palindrome(String s){
StringBuilder s1 = new StringBuilder();
for(int i=s.length();i>=0;i--){
s1.append(s.charAt(i));
}
String s2 = s1.toString();
if(s.equals(s2)){
return true;
}
return false;
}
public static void permutation(ArrayList<String> arr,int l, int r,String ans,HashMap<Character,Integer> map,char[] temp){
if(l>r){
//System.out.println(ans);
arr.add(ans);
return;
}
for(char c:temp) {
if (map.get(c) > 0) {
map.put(c, map.get(c) - 1);
permutation(arr, l + 1, r, ans + c, map,temp);
map.put(c, map.get(c) + 1);
}
}
}
public static long[] maxBeauty(HashMap[] arr, long[] lv,long[] rv,int start,int parent,boolean[] vis){
vis[start] = true;
long[] ans = new long[2];
// System.out.println(start);
ArrayList<Integer> list = new ArrayList<Integer>(arr[start].keySet());
for(int node:list){
if(vis[node]==false) {
long[] curr = maxBeauty(arr,lv,rv,node,start,vis);
ans[0] += Math.max(curr[0] + Math.abs(lv[start]-lv[node]),curr[1] + Math.abs(lv[start]-rv[node]));
ans[1] += Math.max(curr[0] + Math.abs(rv[start]-lv[node]),curr[1] + Math.abs(rv[start]-rv[node]));
}
}
return ans;
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int tc = Integer.parseInt(br.readLine());
while (tc > 0) {
int n = Integer.parseInt(br.readLine());
boolean[] vis = new boolean[n+1];
HashMap[] arr = new HashMap[n+1];
for(int i=0;i<=n;i++){
arr[i] = new HashMap<Integer,Long>();
}
long[] lv = new long[n+1];
long[] rv = new long[n+1];
for(int i=1;i<=n;i++){
String[] s1 = br.readLine().split(" ");
lv[i] = Long.valueOf(s1[0]);
rv[i] = Long.valueOf(s1[1]);
}
for(int i=0;i<n-1;i++){
String[] s1 = br.readLine().split(" ");
int u = Integer.valueOf(s1[0]);
int v = Integer.valueOf(s1[1]);
arr[u].put(v,1);
arr[v].put(u,1);
}
long[] ans = maxBeauty(arr,lv,rv,1,0,vis);
//System.out.println(maxBeauty(graph,lvrv,1,0,0));
//System.out.println(maxBeauty(graph,lvrv,1,1,0));
pw.println(Math.max(ans[1],ans[0]));
tc--;
}
pw.close();
}
} | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],int size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class MultiSet<U extends Comparable<U>> {
public int sz = 0;
public TreeMap<U, Integer> t;
public MultiSet() {
t = new TreeMap<>();
}
public void add(U x) {
t.put(x, t.getOrDefault(x, 0) + 1);
sz++;
}
public void remove(U x) {
if (t.get(x) == 1) t.remove(x);
else t.put(x, t.get(x) - 1);
sz--;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static long myceil(long a, long b){
return (a+b-1)/b;
}
static long C(long n,long r){
long count=0,temp=n;
long ans=1;
long num=n-r+1,div=1;
while(num<=n){
ans*=num;
//ans+=MOD;
ans%=MOD;
ans*=mypow(div,MOD-2);
//ans+=MOD;
ans%=MOD;
num++;
div++;
}
ans+=MOD;
return ans%MOD;
}
static long fact(long a){
long i,ans=1;
for(i=1;i<=a;i++){
ans*=i;
ans%=MOD;
}
return ans%=MOD;
}
static void sieve(int n){
is_sieve[0]=1;
is_sieve[1]=1;
int i,j,k;
for(i=2;i<n;i++){
if(is_sieve[i]==0){
sieve[i]=i;
tr.add(i);
for(j=i*i;j<n;j+=i){
if(j>n||j<0){
break;
}
is_sieve[j]=1;
sieve[j]=i;
}
}
}
}
// static void calc(int n){
// int i,j;
// dp[n-1]=0;
// if(n>1)
// dp[n-2]=1;
// for(i=n-3;i>=0;i--){
// long ind=n-i-1;
// dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD;
// }
// }
static long mypow(long x,long y){
long temp;
if( y == 0)
return 1;
temp = mypow(x, y/2);
if (y%2 == 0)
return (temp*temp)%MOD;
else
return ((x*temp)%MOD*(temp)%MOD)%MOD;
}
static long dist[],dp[][],left[],right[];
static int visited[],isit[];
static ArrayList<Pair<Integer,Pair<Long,Long>>> adj[],li;
//static int dp[][][];
static int MOD=1000000007;
static char ch[];
static int[] sieve,is_sieve;
static TreeSet<Integer> tr;
static long mat[][];
// static void bfs(int node,int par,Pair<Long,Long> p[],long taken){
// LinkedList<Integer> li=new LinkedList<>();
// li.add(node);
// while(!li.isEmpty()){
// int x=li.pollFirst();
// long lowNode=p[x-1].x;
// long highNode=p[x-1].y;
// int left=0,right=0;
// visited[x]=1;
// for(Pair<Integer,Pair<Long,Long>> pp:adj[x]){
// long max=0;
// if(selected[pp.x]==0){
// max=Math.max(Math.abs(lowNode-pp.y.y),Math.abs(highNode-pp.y.x));
// if(max==Math.abs(lowNode-pp.y.y)){
// left++;
// }else{
// right++;
// }
// }else{
// max=Math.max(Math.abs(lowNode-selected[pp.x]),Math.abs(highNode-selected[pp.x]));
// if(max==Math.abs(lowNode-selected[pp.x])){
// left++;
// }else{
// right++;
// }
// }
// if(visited[pp.x]==0)
// li.add(pp.x);
// }
// if(left>=right){
// selected[x]=lowNode;
// }else{
// selected[x]=highNode;
// }
// }
// }
static void dfs(int node,int par, Pair<Long,Long> p[]){
for(Pair<Integer,Pair<Long,Long>> pp:adj[node]){
if(pp.x!=par){
//sum+=Math.abs(selected[node]-selected[pp.x]);
dfs(pp.x,node,p);
//System.out.println(node+" "+pp.x);
long x=Math.abs(p[node].x-p[pp.x].x);
long y=Math.abs(p[node].x-p[pp.x].y);
long z=Math.abs(p[node].y-p[pp.x].x);
long w=Math.abs(p[node].y-p[pp.x].y);
left[node]+=Math.max(x+left[pp.x],y+right[pp.x]);
right[node]+=Math.max(z+left[pp.x],w+right[pp.x]);
}
}
}
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j,tno=0,tte;
t=in.nextInt();
//t=1;
//tte=t;
while(t-->0){
//sum=0;
int n=in.nextInt();
adj=new ArrayList[n+1];
left=new long[n+1];
right=new long[n+1];
visited=new int[n+1];
for(i=0;i<n+1;i++){
adj[i]=new ArrayList<>();
}
Pair<Long,Long> p[]=new Pair[n+1];
for(i=1;i<=n;i++){
p[i]=new Pair<>(in.nextLong(),in.nextLong());
}
for(i=0;i<n-1;i++){
int u,v;
u=in.nextInt();
v=in.nextInt();
adj[u].add(new Pair<>(v,p[v]));
adj[v].add(new Pair<>(u,p[u]));
}
//bfs(1,-1,p,Long.MAX_VALUE);
dfs(1,-1,p);
// for(i=0;i<n+1;i++){
// w.print(selected[i]+" ");
// }
// w.println();
w.println((long)Math.max(left[1],right[1]));
}
w.close();
}
} | 0 | Non-plagiarised |
12c1cc56 | c06c758d | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(bu.readLine());
String s[]=bu.readLine().split(" ");
ArrayList<Integer> z=new ArrayList<>(),o=new ArrayList<>();
long dp[][]=new long[n+1][n+1];
int i,j,a;
for(i=0;i<n;i++)
{
a=Integer.parseInt(s[i]);
if(a==0) z.add(i);
else o.add(i);
}
for(i=1;i<=o.size();i++)
{
long min=dp[i-1][i-1];
for(j=i;j<=z.size();j++)
{
dp[i][j]=min+Math.abs(z.get(j-1)-o.get(i-1));
min=Math.min(min,dp[i-1][j]);
}
}
long ans=Long.MAX_VALUE;
for(i=o.size();i<=z.size();i++)
ans=Math.min(ans,dp[o.size()][i]);
System.out.print(ans);
}
} | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(bu.readLine());
String s[]=bu.readLine().split(" ");
ArrayList<Integer> z=new ArrayList<>(),o=new ArrayList<>();
long dp[][]=new long[n+1][n+1];
int i,j,a;
for(i=0;i<n;i++)
{
a=Integer.parseInt(s[i]);
if(a==0) z.add(i);
else o.add(i);
}
for(i=1;i<=o.size();i++)
{
long min=dp[i-1][i-1];
for(j=i;j<=z.size();j++)
{
dp[i][j]=min+Math.abs(z.get(j-1)-o.get(i-1));
min=Math.min(min,dp[i-1][j]);
}
}
long ans=Long.MAX_VALUE;
for(i=o.size();i<=z.size();i++)
ans=Math.min(ans,dp[o.size()][i]);
System.out.print(ans);
}
} | 1 | Plagiarised |
06b9cf99 | fb69b3b4 | import java.util.*;
import java.io.*;
public class Solution {
public static void ruffleSort(long arr[])
{
ArrayList<Long> list = new ArrayList<>();
for(long i : arr)
list.add(i);
Collections.sort(list);
for(int i = 0; i < list.size(); i++)
arr[i] = list.get(i);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while (t-- > 0) {
// int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
st = new StringTokenizer(br.readLine());
long arr[] = new long[n];
long tot_sum = 0;
for(int i = 0; i < n; i++)
{
long e = Long.parseLong(st.nextToken());
arr[i] = e;
tot_sum+=e;
}
if(tot_sum <= k)
{
output.write("0\n");
continue;
}
ruffleSort(arr);
long psum = 0;
long res = tot_sum - k;
for(int p = 0; p < n - 1; p++)
{
int ind = n - p - 1;
psum+=arr[ind];
long sum = arr[0] + psum - tot_sum + k;
double d = sum / (double)(p + 2);
long x = (long)Math.floor(d);
// output.write("x = " + x + " sum = " + sum + " d = " + d + "\n");
long numsteps = Math.max(arr[0] - x, 0) + p + 1;
if(numsteps < 0)
continue;
// output.write("for p = " + p + " the number of steps req = " + numsteps + " and the x = " + x + "\n");
res = Math.min(res, numsteps);
}
output.write(res + "\n");
// int k = Integer.parseInt(st.nextToken());
// char arr[] = br.readLine().toCharArray();
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.flush();
}
} |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static long floor(long a, long b) {
long res = a / b;
while(res * b > a) res--;
return res;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
st = new StringTokenizer(br.readLine());
Long[] p = new Long[n];
for(int i = 0 ;i<n;i++) {
p[i] = Long.parseLong(st.nextToken());
}
Arrays.sort(p);
long[] sums = new long[n+1];
for(int i=0;i<n;i++) sums[i+1] = sums[i] + p[i];
long ans = Long.MAX_VALUE;
for(int y=0;y<n;y++) {
long x = p[0] - floor(k - sums[n-y] + p[0], y+1);
ans = Math.min(Math.max(x, 0) + y, ans);
}
System.out.println(ans);
}
}
} | 0 | Non-plagiarised |
21123500 | fbd26aa0 | import java.io.*;
import java.util.*;
public class Main {
static class Pair implements Comparable<Pair> {
int f, s;
Pair(int f, int s) {
this.f = f; this.s = s;
}
public int compareTo(Pair other) {
if (this.f != other.f) return this.f - other.f;
return this.s - other.s;
}
}
public static void main(String[] args) throws IOException {
PriorityQueue<Pair> pq = new PriorityQueue();
for (int t = readInt(); t > 0; --t) {
int n = readInt(), m = readInt(), x = readInt();
System.out.println("YES");
for (int i = 1; i <= m; ++i) {
pq.add(new Pair(readInt(), i));
System.out.print(i + " ");
}
for (int i = m + 1; i <= n; ++i) {
Pair p = pq.poll();
p.f += readInt();
pq.add(p);
System.out.print(p.s + " ");
}
System.out.println();
pq.clear();
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
}
| import java.io.*;
import java.util.*;
public class Main {
static class Pair implements Comparable<Pair> {
int f, s;
Pair(int f, int s) {
this.f = f; this.s = s;
}
public int compareTo(Pair other) {
if (this.f != other.f) return this.f - other.f;
return this.s - other.s;
}
}
public static void main(String[] args) throws IOException {
PriorityQueue<Pair> pq = new PriorityQueue();
for (int t = readInt(); t > 0; --t) {
int n = readInt(), m = readInt(), x = readInt();
System.out.println("YES");
for (int i = 1; i <= m; ++i) {
pq.add(new Pair(readInt(), i));
System.out.print(i + " ");
}
for (int i = m + 1; i <= n; ++i) {
Pair p = pq.poll();
p.f += readInt();
pq.add(p);
System.out.print(p.s + " ");
}
System.out.println();
pq.clear();
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
}
| 1 | Plagiarised |
734a94be | f6ca6fc8 |
import java.io.*;
import java.math.*;
import java.util.*;
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int solve(int dp[][] , ArrayList<Integer> one , ArrayList<Integer> zero , int i,int j)
{
int n=one.size();
int m=zero.size();
if(i>=n)
return 0;
else if(j>=m)
return Integer.MAX_VALUE;
else if(dp[i][j]!=-1)
return dp[i][j];
int val1 = Math.abs(one.get(i)-zero.get(j))+solve(dp, one ,zero ,i+1,j+1);
int val2 = solve(dp , one , zero , i,j+1);
dp[i][j]= Math.min(val1,val2);
return dp[i][j];
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuffer res = new StringBuffer();
int tc = 1;
while(tc-->0) {
int n = sc.nextInt();
ArrayList<Integer> one = new ArrayList<>();
ArrayList<Integer> zero = new ArrayList<>();
for(int i=0;i<n;i++) {
int x = sc.nextInt();
if(x==1) {
one.add(i);
}
else {
zero.add(i);
}
}
int dp[][] = new int[one.size()+1][zero.size()+1];
for(int i=1;i<=one.size();i++)
{
dp[i][i]=dp[i-1][i-1]+Math.abs(zero.get(i-1)-one.get(i-1));
for(int j=i+1;j<=zero.size();j++)
{
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(one.get(i-1)-zero.get(j-1)));
}
}
System.out.println(dp[one.size()][zero.size()]);
}
System.out.println(res);
}
}
| import java.util.*;
import java.io.*;
public class D {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> o=new ArrayList<Integer>();
ArrayList<Integer> e=new ArrayList<Integer>();
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
int dp[][]=new int[o.size()+1][e.size()+1];
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++){
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
}
System.out.println(dp[o.size()][e.size()]);
}
}
| 1 | Plagiarised |
07038b12 | a18cb2c1 | //Siddhartha Bose
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class h {
public static int r1=0;
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long[][] f=new long[501][501];
public static void main(String[] args) {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader s=new FastReader();
int t=s.nextInt();
while(t>0) {
int n=s.nextInt();
for(int i=1;i<=n;i++) {
vis[i]=0;
deg[i]=0;
ans[i]=0;
f1[i]=new ArrayList<>();
}
for(int i=1;i<=n-1;i++) {
int x=s.nextInt();
int y=s.nextInt();
f1[x].add(new pair1(y,i));
f1[y].add(new pair1(x,i));
deg[x]++;
deg[y]++;
}
// dfs(1);
int node=-1;
boolean p=false;
for(int i=1;i<=n;i++) {
if(deg[i]>2) {
p=true;
}
if(deg[i]==1) {
node=i;
}
}
if(p) {
out.println(-1);
}else {
dfs(node,2);
// System.out.println();
for(int i=1;i<=n-1;i++) {
out.print(ans[i]+" ");
}
out.println();
}
t--;
}
out.close();
}
static int[] ans=new int[100001];
static int[] deg=new int[100001];
static void dfs(int node,int v) {
vis[node]=1;
// deg[node]++;
for(int i=0;i<f1[node].size();i++) {
if(vis[f1[node].get(i).a]==0) {
int g=2;
if(v==2) {
g=5;
}
ans[f1[node].get(i).b]=v;
dfs(f1[node].get(i).a,g);
// deg[node]++;
}
}
}
// static long ans(int[] a,int[] b,long till,long[] post,int n,int k,int i) {
// if(k==0 || i==n) {
// return till+post[i];
// }
// if(f[i][k]!=-1) {
// return f[i][k];
// }
//
// }
// static class pair
static int[] vis=new int[100001];
static class pair1 {
private int a;
private int b;
pair1(int a,int b){
this.a=a;
this.b=b;
}
}
static class pair3 implements Comparable<pair3>{
private int a;
private int b;
private int d;
private int c;
pair3(int a,int b,int c,int d){
this.a=a;
this.d=d;
this.b=b;
this.c=c;
}
@Override
public int compareTo(pair3 c) {
if(c.b-c.a!=this.b-this.a) {
return Integer.compare(c.b-c.a, this.b-this.a);
}else {
return Integer.compare(this.c, c.c);
}
}
}
static class pair2 implements Comparable<pair2>{
private int a;
private int b;
private int d;
private int c;
pair2(int a,int b,int c,int d){
this.a=a;
this.d=d;
this.b=b;
this.c=c;
}
public int compareTo(pair2 c) {
if(c.b!=this.b) {
return Integer.compare(c.b, this.b);
}else {
if(c.c!=this.c) {
return Integer.compare(this.c, c.c);
}else {
return Integer.compare(this.a, c.a);
}
}
}
}
static boolean r(int k) {
for(int i=2;i<=k/2;i++) {
if(k%i==0) {
return false;
}
}
return true;
}
public static int[] is_prime=new int[100001];
public static ArrayList<Long> primes=new ArrayList<>();
public static void sieve() {
long maxN=100;
for(long i=1;i<=maxN;i++) {
is_prime[(int) i]=1;
}
is_prime[0]=0;
is_prime[1]=0;
for(long i=2;i*i<=maxN;i++) {
if(is_prime[(int) i]!=0) {
primes.add( i);
long c=1;
for(long j=i*i;j<=maxN;j+=i) {
is_prime[(int) j]++;
}
}
}
for(long i=2;i<=maxN;i++) {
is_prime[(int) i]=is_prime[(int) (i-1)]+is_prime[(int) i];
}
// int count=0;
// for(long i=1;i<=maxN;i++) {
// if(is_prime[(int) i]==1) {
// count++;
// }
// if(is_prime[count]==1) {
// arr[(int) i]=1;
// }else {
// arr[(int)i]=0;
// }
// }
}
public static long[] merge_sort(long[] A, int start, int end) {
if (end > start) {
int mid = (end + start) / 2;
long[] v = merge_sort(A, start, mid);
long[] o = merge_sort(A, mid + 1, end);
return (merge(v, o));
} else {
long[] y = new long[1];
y[0] = A[start];
return y;
}
}
static ArrayList<pair1>[] f1=new ArrayList[100001];
public static long[] merge(long a[], long b[]) {
// int count=0;
long[] temp = new long[a.length + b.length];
int m = a.length;
int n = b.length;
int i = 0;
int j = 0;
int c = 0;
while (i < m && j < n) {
if (a[i] < b[j]) {
temp[c++] = a[i++];
} else {
temp[c++] = b[j++];
}
}
while (i < m) {
temp[c++] = a[i++];
}
while (j < n) {
temp[c++] = b[j++];
}
return temp;
}
} | import java.util.*;
import java.io.*;
public class C1627 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[][] edges = new int[n - 1][2];
int[] deg = new int[n];
boolean valid = true;
ArrayList<Integer>[] adjList = new ArrayList[n];
for (int i = 0; i < n; i++) {
adjList[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
int u = sc.nextInt() - 1;
int v = sc.nextInt() - 1;
edges[i] = new int[]{u, v};
deg[u]++;
deg[v]++;
valid &= deg[u] <= 2 && deg[v] <= 2;
adjList[u].add(i);
adjList[v].add(i);
}
if (!valid) {
pw.println(-1);
continue;
}
int root = -1;
for (int i = 0; i < n; i++) {
if (adjList[i].size() == 1)
root = i;
}
int[] ans = new int[n - 1];
int curColor = 2;
int par = -1;
while (true) {
int nxt = -1;
for (int e : adjList[root]) {
int other = edges[e][0] ^ edges[e][1] ^ root;
if (other != par) {
ans[e] = curColor;
curColor = 5 - curColor;
nxt = other;
break;
}
}
if (nxt == -1)
break;
par = root;
root = nxt;
}
for (int x : ans) {
pw.print(x + " ");
}
pw.println();
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
| 0 | Non-plagiarised |
c9159d9c | f0ede32a | import java.util.*;
public class SolutionB {
public static long gcd(long a, long b){
if(b==0){
return a;
}
return gcd(b, a%b);
}
public static long gcdSum(long b){
long a = 0;
long temp = b;
while(temp!=0){
a = a + temp%10;
temp = temp/10;
}
return gcd(a,b);
}
public static class Pair{
Long a;
Long b;
public Pair(Long a, Long b) {
this.a = a;
this.b = b;
}
}
public static long factorial (long n){
if(n==0)
return 1;
else if(n==1)
return n;
return n * factorial(n-1);
}
public static long lcm (long n){
if(n<3)
return n;
return lcmForBig(n,n-1);
}
private static long lcmForBig(long n, long l) {
if(l==1)
return n;
n = (n * l) / gcd(n, l);
return lcmForBig(n, l-1);
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i =0;i<t;i++) {
int n = s.nextInt();
int arr [] = new int[n];
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int j=0;j<n;j++){
int num = s.nextInt();
arr[j]=num;
}
String color = s.next();
for(int j=0;j<n;j++){
if(color.charAt(j)=='B'){
blue.add(arr[j]);
}
else{
red.add(arr[j]);
}
}
Collections.sort(blue);
String ans = "YES";
int counter = 0;
for(int j=0;j<blue.size();j++){
int current = blue.get(j);
if (current<1){
ans="NO";
break;
}
if(current>counter){
counter++;
}
else{
ans="NO";
break;
}
}
if(ans=="NO"){
System.out.println(ans);
}
else{
int tempCounter = n+1;
Collections.sort(red);
for(int j=red.size()-1;j>=0;j--){
int current = red.get(j);
if(current>=tempCounter){
ans="NO";
break;
}
else{
tempCounter--;
}
}
if(tempCounter-counter!=1)
System.out.println("NO");
else
System.out.println(ans);
}
}
return;
}
}
| import java.util.*;
public class Soltion{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
Integer[] arr = new Integer[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
String s = sc.next();
List<Integer> blue = new ArrayList<>();
List<Integer> red = new ArrayList<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='B'){
blue.add(arr[i]);
}
else{
red.add(arr[i]);
}
}
Collections.sort(blue);
Collections.sort(red);
int p=1,q=n;
boolean flag = true;
for(int i=red.size()-1;i>=0;i--){
if(red.get(i)>q){
flag = false;
break;
}
q--;
}
for(int i=0;i<blue.size();i++){
if(blue.get(i)<p){
flag = false;
break;
}
p++;
}
System.out.println(flag? "Yes" : "No");
}
}
}
| 0 | Non-plagiarised |
584b0e9e | 722e318f |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class D_Round_753_Div3 {
public static int MOD = 1000000007;
static int[][] dp;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int T = in.nextInt();
for (int z = 0; z < T; z++) {
int n = in.nextInt();
int[] data = new int[n];
for (int i = 0; i < n; i++) {
data[i] = in.nextInt();
}
String line = in.next();
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (line.charAt(i) == 'B') {
blue.add(data[i]);
} else {
red.add(data[i]);
}
}
Collections.sort(blue);
Collections.sort(red);
int st = 1;
boolean ok = true;
for (int i : blue) {
if (i < st) {
ok = false;
break;
}
st++;
}
if (ok) {
for (int i : red) {
if (i > st) {
ok = false;
break;
}
st++;
}
}
out.println(ok ? "Yes" : "No");
}
out.close();
}
static int find(int v, int[] u) {
if (v == u[v]) {
return v;
}
return u[v] = find(u[v], u);
}
static int abs(int a) {
return a < 0 ? -a : a;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point {
int x;
int y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
public String toString() {
return x + " " + y;
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
data[index] %= MOD;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
result %= MOD;
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return (val * val);
} else {
return (val * ((val * a)));
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
} | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class TaskB {
static long mod = 1000000007;
static FastScanner scanner;
static final StringBuilder result = new StringBuilder();
public static void main(String[] args) {
// 2 : 1000000000
scanner = new FastScanner();
int T = scanner.nextInt();
for (int t = 0; t < T; t++) {
solve(t + 1);
result.append("\n");
}
System.out.println(result);
}
static void solve(int t) {
int n = scanner.nextInt();
int[] a = scanner.nextIntArray(n);
String s = scanner.nextToken();
List<Integer> blue = new ArrayList<>();
List<Integer> red = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'B') {
blue.add(a[i]);
} else {
red.add(a[i]);
}
}
Collections.sort(blue);
Collections.sort(red);
for (int i = 0; i < blue.size(); i++) {
if (blue.get(i) < i + 1) {
result.append("NO");
return;
}
}
for (int i = 0; i < red.size(); i++) {
if (red.get(i) > i + 1 + blue.size()) {
result.append("NO");
return;
}
}
result.append("YES");
}
static class WithIdx implements Comparable<WithIdx> {
int val, idx;
public WithIdx(int val, int idx) {
this.val = val;
this.idx = idx;
}
@Override
public int compareTo(WithIdx o) {
if (val == o.val) {
return Integer.compare(idx, o.idx);
}
return Integer.compare(val, o.val);
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
String[] nextStringArray(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++) res[i] = nextToken();
return res;
}
}
static class PrefixSums {
long[] sums;
public PrefixSums(long[] sums) {
this.sums = sums;
}
public long sum(int fromInclusive, int toExclusive) {
if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
return sums[toExclusive] - sums[fromInclusive];
}
public static PrefixSums of(int[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
public static PrefixSums of(long[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
}
static class ADUtils {
static void sort(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
static void reverse(int[] arr) {
int last = arr.length / 2;
for (int i = 0; i < last; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
static void sort(long[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
}
static class MathUtils {
static long[] FIRST_PRIMES = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013,
1019, 1021, 1031, 1033, 1039, 1049, 1051};
static long[] primes(int to) {
long[] all = new long[to + 1];
long[] primes = new long[to + 1];
all[1] = 1;
int primesLength = 0;
for (int i = 2; i <= to; i++) {
if (all[i] == 0) {
primes[primesLength++] = i;
all[i] = i;
}
for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j];
j++) {
all[(int) (i * primes[j])] = primes[j];
}
}
return Arrays.copyOf(primes, primesLength);
}
static long modpow(long b, long e, long m) {
long result = 1;
while (e > 0) {
if ((e & 1) == 1) {
/* multiply in this bit's contribution while using modulus to keep
* result small */
result = (result * b) % m;
}
b = (b * b) % m;
e >>= 1;
}
return result;
}
static long submod(long x, long y, long m) {
return (x - y + m) % m;
}
static long modInverse(long a, long m) {
long g = gcdF(a, m);
if (g != 1) {
throw new IllegalArgumentException("Inverse doesn't exist");
} else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
return modpow(a, m - 2, m);
}
}
static public long gcdF(long a, long b) {
while (b != 0) {
long na = b;
long nb = a % b;
a = na;
b = nb;
}
return a;
}
}
}
/*
5
3 2 3 8 8
2 8 5 10 1
*/ | 0 | Non-plagiarised |
313b42e6 | e45446bc | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Div2 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] s = reader.readLine().split(" ");
StringBuilder sb = new StringBuilder();
Div2 div2 = new Div2();
int t = Integer.parseInt(s[0]);
while (t-- > 0) {
s = reader.readLine().split(" ");
int n = Integer.parseInt(s[0]);
s = reader.readLine().split(" ");
long[] cs = new long[n];
for (int i = 0; i < n; i++) {
cs[i] = Long.parseLong(s[i]);
}
long min1 = cs[0];
long min2 = cs[1];
long cost = n * min1 + n * min2;
long sum = min1 + min2;
for (int i = 2; i < n; i++) {
sum += cs[i];
int count = i / 2;
if (i % 2 == 0) {
min1 = Math.min(min1, cs[i]);
cost = Math.min(cost, sum + (n - count - 1) * min1 + (n - count) * min2);
} else {
count++;
min2 = Math.min(min2, cs[i]);
cost = Math.min(cost, sum + (n - count) * min1 + (n - count) * min2);
}
}
sb.append(cost).append("\n");
}
System.out.println(sb.toString());
}
} |
import java.util.*;
import java.lang.*;
import java.io.*;
public class C {
// *** ++
// +=-==+ +++=-
// +-:---==+ *+=----=
// +-:------==+ ++=------==
// =-----------=++=========================
// +--:::::---:-----============-=======+++====
// +---:..:----::-===============-======+++++++++
// =---:...---:-===================---===++++++++++
// +----:...:-=======================--==+++++++++++
// +-:------====================++===---==++++===+++++
// +=-----======================+++++==---==+==-::=++**+
// +=-----================---=======++=========::.:-+*****
// +==::-====================--: --:-====++=+===:..-=+*****
// +=---=====================-... :=..:-=+++++++++===++*****
// +=---=====+=++++++++++++++++=-:::::-====+++++++++++++*****+
// +=======++++++++++++=+++++++============++++++=======+******
// +=====+++++++++++++++++++++++++==++++==++++++=:... . .+****
// ++====++++++++++++++++++++++++++++++++++++++++-. ..-+****
// +======++++++++++++++++++++++++++++++++===+====:. ..:=++++
// +===--=====+++++++++++++++++++++++++++=========-::....::-=++*
// ====--==========+++++++==+++===++++===========--:::....:=++*
// ====---===++++=====++++++==+++=======-::--===-:. ....:-+++
// ==--=--====++++++++==+++++++++++======--::::...::::::-=+++
// ===----===++++++++++++++++++++============--=-==----==+++
// =--------====++++++++++++++++=====================+++++++
// =---------=======++++++++====+++=================++++++++
// -----------========+++++++++++++++=================+++++++
// =----------==========++++++++++=====================++++++++
// =====------==============+++++++===================+++==+++++
// =======------==========================================++++++
// created by : Nitesh Gupta
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
String[] scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
long min;
long hor = arr[0], ver = arr[1];
long min1 = 0, min2 = 0;
min = (hor + ver) * n;
long x = 0, y = 0;
for (int i = 2; i < n; i++) {
if (i % 2 == 0) {
x += 1;
if (arr[i] >= hor) {
min1 += arr[i];
} else {
min1 += hor;
hor = arr[i];
}
} else {
y += 1;
if (arr[i] >= ver) {
min2 += arr[i];
} else {
min2 += ver;
ver = arr[i];
}
}
long pro = (n - x) * hor + (n - y) * ver;
min = Math.min(min, min1 + min2 +pro);
}
sb.append(min);
sb.append("\n");
}
System.out.println(sb);
return;
}
public static void sort(long[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) Math.random() * n;
long temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
public static void print(long[] dp) {
for (long ele : dp) {
System.out.print(ele + " ");
}
System.out.println();
}
public static void print(long[][] dp) {
for (long[] a : dp) {
for (long ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
}
| 0 | Non-plagiarised |
3b498a39 | 69b2fd22 | /*
* HI THERE! (:
*
* CREATED BY : NAITIK V
*
* JAI HIND
*
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc=new FastReader();
static long dp[][];
static int mod=1000000007;
static int max;
public static void main(String[] args)
{
PrintWriter out=new PrintWriter(System.out);
//StringBuffer sb=new StringBuffer("");
int ttt=1;
//ttt =i();
outer :while (ttt-- > 0)
{
int n=i();
int A[]=input(n);
dp=new long[n+1][n+1];
for(int i=0;i<=n;i++) {
Arrays.fill(dp[i],-1);
}
ArrayList<Integer> l=new ArrayList<Integer>();
ArrayList<Integer> m=new ArrayList<Integer>();
for(int i=0;i<n;i++) {
if(A[i]==0) {
l.add(i+1);
}
else {
m.add(i+1);
}
}
A=new int[m.size()];
int B[]=new int[l.size()];
for(int i=0;i<l.size();i++) {
B[i]=l.get(i);
}
for(int i=0;i<m.size();i++) {
A[i]=m.get(i);
}
n=m.size();
int o=l.size();
System.out.println(go(A,B,0,0,n,o));
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
//CHECK FOR N=1
//CHECK FOR N=1
}
private static long go(int[] A, int[] B, int i, int j, int n, int m) {
if(i==n)
return 0;
if(j==m)
return Integer.MAX_VALUE;
if(dp[i][j]!=-1)
return dp[i][j];
long op1=go(A, B, i+1, j+1, n, m)+Math.abs(A[i]-B[j]);
long op2=go(A, B, i, j+1, n, m);
return dp[i][j]=Math.min(op1, op2);
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void print(int A[]) {
for(int i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
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;
}
}
}
| import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=1;
while(T-->0)
{
int n=input.nextInt();
int a[]=new int[n];
ArrayList<Integer> list=new ArrayList<>();
ArrayList<Integer> space=new ArrayList<>();
for(int i=0;i<n;i++)
{
a[i]=input.nextInt();
if(a[i]==1)
{
list.add(i);
}
else
{
space.add(i);
}
}
int pre[]=new int[space.size()];
for(int i=0;i<list.size();i++)
{
if(i==0)
{
int min=Integer.MAX_VALUE;
for(int j=0;j<space.size();j++)
{
pre[j]=Math.abs(list.get(i)-space.get(j));
min=Math.min(min,pre[j]);
pre[j]=min;
}
}
else
{
int arr[]=new int[space.size()];
for(int j=0;j<i;j++)
{
arr[j]=Integer.MAX_VALUE;
}
int min=Integer.MAX_VALUE;
for(int j=i;j<space.size();j++)
{
int v=Math.abs(list.get(i)-space.get(j));
v+=pre[j-1];
arr[j]=v;
min=Math.min(min,v);
arr[j]=min;
}
for(int j=0;j<space.size();j++)
{
pre[j]=arr[j];
}
}
}
out.println(pre[space.size()-1]);
}
out.close();
}
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;
}
}
} | 0 | Non-plagiarised |
4e28b45e | d7a8434f | import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while(T-->0) {
int n = sc.nextInt();
char[] s = new char[n];
char[] t = new char[n];
s = sc.next().toCharArray();
t = sc.next().toCharArray();
int a = 0, b = 0, c = 0, d = 0;
for(int i = 0; i < n; i++) {
if(s[i] == '0' && t[i] == '0') a++;
if(s[i] == '1' && t[i] == '0') b++;
if(s[i] == '0' && t[i] == '1') c++;
if(s[i] == '1' && t[i] == '1') d++;
}
int res = Integer.MAX_VALUE;
if(b == c || b+1 == c) {
if((b + c) % 2 == 0) {
res = Math.min(res, b + c);
}
}
if(a == d || a+1 == d) {
if((a + d) % 2 == 1) {
res = Math.min(res, a + d);
}
}
if(res == Integer.MAX_VALUE) System.out.println(-1);
else System.out.println(res);
}
}
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 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());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
}
| import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while(T-->0) {
int n = sc.nextInt();
char[] s = new char[n];
char[] t = new char[n];
s = sc.next().toCharArray();
t = sc.next().toCharArray();
int a = 0, b = 0, c = 0, d = 0;
for(int i = 0; i < n; i++) {
if(s[i] == '0' && t[i] == '0') a++;
if(s[i] == '1' && t[i] == '0') b++;
if(s[i] == '0' && t[i] == '1') c++;
if(s[i] == '1' && t[i] == '1') d++;
}
int res = Integer.MAX_VALUE;
if(b == c || b+1 == c) {
if((b + c) % 2 == 0) {
res = Math.min(res, b + c);
}
}
if(a == d || a+1 == d) {
if((a + d) % 2 == 1) {
res = Math.min(res, a + d);
}
}
if(res == Integer.MAX_VALUE) System.out.println(-1);
else System.out.println(res);
}
}
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 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());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
}
| 1 | Plagiarised |
23cb8587 | 48801d9e | import java.io.*;
import java.util.*;
public class C {
static long mod = (long) (1e9 + 7);
public static void main(String[] args) throws IOException {
Scanner scn = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int T = scn.ni(), tcs = 0;
C: while (tcs++ < T) {
int n = scn.ni();
tree = new ArrayList[n + 1];
range = new long[n + 1][2];
for (int i = 0; i <= n; i++)
tree[i] = new ArrayList<>();
for (int i = 1; i <= n; i++) {
range[i][0] = scn.nl();
range[i][1] = scn.nl();
}
for (int i = 0; i < n - 1; i++) {
int x = scn.ni();
int y = scn.ni();
tree[x].add(y);
tree[y].add(x);
}
strg = new long[n + 1][2];
for (long a1[] : strg)
Arrays.fill(a1, -1L);
sb.append(Math.max(DFS(1, -1, 0), DFS(1, -1, 1)));
sb.append("\n");
}
out.print(sb);
out.close();
}
static ArrayList<Integer> tree[];
static long range[][], strg[][];
static long DFS(int u, int pa, int ok) {
if (strg[u][ok] != -1)
return strg[u][ok];
long tg = 0;
for (int ch : tree[u]) {
if (ch == pa)
continue;
long sg = 0;
if (ok == 0) {
sg = Math.max(DFS(ch, u, 0) + Math.abs(range[u][0] - range[ch][0]),
DFS(ch, u, 1) + Math.abs(range[u][0] - range[ch][1]));
} else {
sg = Math.max(DFS(ch, u, 0) + Math.abs(range[u][1] - range[ch][0]),
DFS(ch, u, 1) + Math.abs(range[u][1] - range[ch][1]));
}
tg += sg;
}
return strg[u][ok] = tg;
}
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 int ni() throws IOException {
return Integer.parseInt(next());
}
public long nl() throws IOException {
return Long.parseLong(next());
}
public int[] nia(int n) throws IOException {
int a[] = new int[n];
String sa[] = br.readLine().split(" ");
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(sa[i]);
return a;
}
public long[] nla(int n) throws IOException {
long a[] = new long[n];
String sa[] = br.readLine().split(" ");
for (int i = 0; i < n; i++)
a[i] = Long.parseLong(sa[i]);
return a;
}
public void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int v : a)
l.add(v);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
public void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long v : a)
l.add(v);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
}
} |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class C {
private static FastReader fr = new FastReader();
private static PrintWriter out=new PrintWriter(System.out);
private static Random random = new Random();
private static long[][] dp;
private static long calculate(List<Integer>[] graph, int current, long[][] r, boolean[] stack, int use){
if(dp[current][use] != -1) return dp[current][use];
stack[current] = true;
long max = 0;
if(graph[current] != null){
for(int next : graph[current]){
if(!stack[next]){
stack[next] = true;
long r1 = Math.abs(r[current][use] - r[next][0]) + calculate(graph, next, r, stack, 0);
long r2 = Math.abs(r[current][use] - r[next][1]) + calculate(graph, next, r, stack, 1);
max += Math.max(r1, r2);
}
}
}
stack[current] = false;
dp[current][use] = max;
return max;
}
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
// code goes here
int t = fr.nextInt();
while (t-- > 0){
int n = fr.nextInt();
long[][] r = new long[n][2];
for(int i = 0; i < n; i++){
r[i] = fr.nextLongArray(2);
}
List<Integer>[] graph = new ArrayList[n];
for(int i = 0; i < n - 1; i++){
int u = fr.nextInt();
int v = fr.nextInt();
if(graph[u - 1] == null) graph[u - 1] = new ArrayList<>();
if(graph[v - 1] == null) graph[v - 1] = new ArrayList<>();
graph[u - 1].add(v - 1);
graph[v - 1].add(u - 1);
}
boolean[] stack = new boolean[n];
dp = new long[n][2];
for(int i = 0; i < dp.length; i++){
Arrays.fill(dp[i], -1);
}
long r1 = calculate(graph, 0, r, stack, 0);
long r2 = calculate(graph, 0, r, stack, 1);
sb.append(Math.max(r1, r2)).append("\n");
}
System.out.print(sb.toString());
}
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class FastReader{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextIntArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] nextLongArray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
static class Pair<A, B>{
A first;
B second;
public Pair(A first, B second){
this.first = first;
this.second = second;
}
}
static long mod(String num, long a)
{
// Initialize result
long res = 0;
// One by one process all digits of 'num'
for (int i = 0; i < num.length(); i++)
res = (res*10 + num.charAt(i) - '0') %a;
return res;
}
static long binomialCoeff(long n, long k, long MOD)
{
long res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
res %= MOD;
}
return res;
}
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(int n, int r,
long p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long[] fac = new long[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
}
| 0 | Non-plagiarised |
99bc7da3 | b3638571 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Codeforces {
static int mod =1000000007;
static Set<Integer> set;
public static void main(String[] args) throws Exception {
PrintWriter out=new PrintWriter(System.out);
FastScanner fs=new FastScanner();
int t=fs.nextInt();
while(t-->0) {
int n=fs.nextInt();
int arr[]=new int[n];
// set=new HashSet<>();
for(int i=0;i<n;i++) {
arr[i]=Math.abs(fs.nextInt());
// set.add(arr[i]);
}
// sort(arr);
// for(int i=0;i<n;i++) System.out.print(arr[i]+" ");
// System.out.println();
// if(set.size()<n||set.contains(0)) {
// System.out.println("YES");
// continue;
// }
boolean f=false;
for(int i=0;i<n;i++) {
boolean cur=recur(0,i,arr,0);
if(cur) {
f=true;
break;
}
}
if(f) System.out.println("YES");
else System.out.println("NO");
}
out.close();
}
static boolean recur(int pos,int ind,int arr[],int sum) {
if(pos==ind) return recur(pos+1,ind,arr,sum);
if(sum==arr[ind]) return true;
if(pos==arr.length) {
return false;
}
if(recur(pos+1,ind,arr,sum+arr[pos])) return true;
if(recur(pos+1,ind,arr,sum)) return true;
if(recur(pos+1,ind,arr,sum-arr[pos])) return true;
return false;
}
static long gcd(long a,long b) {
if(b==0) return a;
return gcd(b,a%b);
}
static long nck(int n,int k) {
if(k>n) return 0;
long res=1;
res*=fact(n);
res%=mod;
res*=modInv(fact(k));
res%=mod;
res*=modInv(fact(n-k));
res%=mod;
return res;
}
static long fact(long n) {
long res=1;
for(int i=2;i<=n;i++) {
res*=i;
res%=mod;
}
return res;
}
static long pow(long a,long b) {
long res=1;
while(b!=0) {
if((b&1)!=0) {
res*=a;
res%=mod;
}
a*=a;
a%=mod;
b=b>>1;
}
return res;
}
static long modInv(long n) {
return pow(n,mod-2);
}
static void sort(int[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
int temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Use this to input code since it is faster than a Scanner
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
MyScanner scan = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int num = scan.nextInt();
int[] sequence = new int[num];
for (int j = 0; j < num; j++) {
sequence[j] = scan.nextInt();
}
int[] setSum = new int[(int) Math.pow(3, num)];
for (int k = 0; k < num; k++) {
int pot =(int)Math.pow(3, k);
for (int j = 1; j < setSum.length; j++) {
if(j % (pot*3) >= pot ){
if(j % (pot*3) >= pot){
if(j % (pot*3) >= pot*2){
setSum[j] += sequence[k];
}else{
setSum[j] -= sequence[k];
}
}
}
}
}
String output = "NO";
for (int j = 1; j < setSum.length; j++) {
if(setSum[j] == 0){
output = "YES";
break;
}
}
out.println(output);
}
out.close();
}
public static PrintWriter out;
public static class MyScanner { BufferedReader br; StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | 0 | Non-plagiarised |
c9159d9c | fae0662f | import java.util.*;
public class SolutionB {
public static long gcd(long a, long b){
if(b==0){
return a;
}
return gcd(b, a%b);
}
public static long gcdSum(long b){
long a = 0;
long temp = b;
while(temp!=0){
a = a + temp%10;
temp = temp/10;
}
return gcd(a,b);
}
public static class Pair{
Long a;
Long b;
public Pair(Long a, Long b) {
this.a = a;
this.b = b;
}
}
public static long factorial (long n){
if(n==0)
return 1;
else if(n==1)
return n;
return n * factorial(n-1);
}
public static long lcm (long n){
if(n<3)
return n;
return lcmForBig(n,n-1);
}
private static long lcmForBig(long n, long l) {
if(l==1)
return n;
n = (n * l) / gcd(n, l);
return lcmForBig(n, l-1);
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i =0;i<t;i++) {
int n = s.nextInt();
int arr [] = new int[n];
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int j=0;j<n;j++){
int num = s.nextInt();
arr[j]=num;
}
String color = s.next();
for(int j=0;j<n;j++){
if(color.charAt(j)=='B'){
blue.add(arr[j]);
}
else{
red.add(arr[j]);
}
}
Collections.sort(blue);
String ans = "YES";
int counter = 0;
for(int j=0;j<blue.size();j++){
int current = blue.get(j);
if (current<1){
ans="NO";
break;
}
if(current>counter){
counter++;
}
else{
ans="NO";
break;
}
}
if(ans=="NO"){
System.out.println(ans);
}
else{
int tempCounter = n+1;
Collections.sort(red);
for(int j=red.size()-1;j>=0;j--){
int current = red.get(j);
if(current>=tempCounter){
ans="NO";
break;
}
else{
tempCounter--;
}
}
if(tempCounter-counter!=1)
System.out.println("NO");
else
System.out.println(ans);
}
}
return;
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class D {
public static void main(String[] args) throws IOException {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
for (int tc = 0; tc < t; tc++) {
int n = fs.nextInt();
int[] a = fs.readArray(n);
String s = fs.nextLine();
// let all blue to be 1 -> blueCount
ArrayList<Integer> blues = new ArrayList<Integer>();
ArrayList<Integer> reds = new ArrayList<Integer>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'B') {
blues.add(a[i]);
} else {
reds.add(a[i]);
}
}
Collections.sort(blues);
Collections.sort(reds);
boolean ok = true;
for (int i = 1; i <= blues.size(); i++) {
if (blues.get(i - 1) < i) {
ok = false;
break;
}
}
for (int i = blues.size() + 1; i <= n; i++) {
if (reds.get(i - blues.size() - 1) > i) {
ok = false;
break;
}
}
if (ok) {
out.println("YES");
} else {
out.println("NO");
}
}
out.close();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() throws IOException {
return br.readLine();
}
}
}
| 0 | Non-plagiarised |
a101df86 | f0d91796 | import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// long mod = 1_000_000_007L;
// long mod = 998_244_353L;
int t = sc.nextInt();
for ( int zzz=0; zzz<t; zzz++ ) {
int n = sc.nextInt();
int[] a = new int[n];
String[] b = new String[n];
for ( int i=0; i<n; i++ ) {
a[i] = sc.nextInt();
}
int pos = 0;
if ( n%2==1 ) {
int a01 = a[0]+a[1];
if ( a01==0 ) {
a01 = a[0]-a[1];
b[0] = String.valueOf(a[2]);
b[1] = String.valueOf(0-a[2]);
b[2] = String.valueOf(0-a01);
} else {
b[0] = String.valueOf(a[2]);
b[1] = String.valueOf(a[2]);
b[2] = String.valueOf(0-a01);
}
pos = 3;
}
for ( int i=pos; i<n; i=i+2 ) {
b[i] = String.valueOf(a[i+1]);
b[i+1] = String.valueOf(0-a[i]);
}
System.out.println(String.join(" ", b));
}
}
}
| import java.io.*;
import java.util.*;
public class cf {
public static void main(String[] args){
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-- > 0){
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int ans[]=new int[n];
if(n%2==0){
for(int i=0;i<n;i=i+2){
ans[i]=-arr[i+1];
ans[i+1]=arr[i];
}
}
else{
if(arr[0]+arr[1]!=0){
ans[0]=-arr[2];
ans[1]=-arr[2];
ans[2]=arr[0]+arr[1];
}
else{
if(arr[1]+arr[2]!=0){
ans[1]=-arr[0];
ans[2]=-arr[0];
ans[0]=arr[1]+arr[2];
}
else{
ans[0]=-arr[1];
ans[2]=-arr[1];
ans[1]=arr[0]+arr[2];
}
}
for(int i=3;i<n;i=i+2){
ans[i]=-arr[i+1];
ans[i+1]=arr[i];
}
}
for(int j=0;j<n;j++){
System.out.print(ans[j]+" ");
}
System.out.println();
}
}
}
//////////////////////////////////////////////////////////////
// LCM AND GCD
/*
public static int gcd(int a,int b){
if(b == 0){
return a;
}
return gcd(b,a%b);
}
public static int lcm(int a,int b){
return (a / gcd(a, b)) * b;
}*/
///////////////////////////////////////////////////////////////////////////////////
//Iterator
/*Iterator iterator = object.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}*/
///////////////////////////////////////////////////////////////////////////////////
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
| 0 | Non-plagiarised |
d12f26f0 | df594a00 | import java.io.*;
import java.util.*;
public class AirConditioners {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int q = readInt();
while (q-- > 0) {
int n = readInt(), k = readInt();
int[] a = new int[k], t = new int[k], c = new int[n];
for (int i = 0; i < k; i ++) a[i] = readInt();
for (int i = 0; i < k; i ++) t[i] = readInt();
Arrays.fill(c, Integer.MAX_VALUE);
for (int i = 0; i < k; i ++) c[a[i] - 1] = t[i];
int[] l = new int[n], r = new int[n];
int prev = (int) 2e9;
for (int i = 0; i < n; i ++) {
l[i] = Math.min(prev + 1, c[i]);
prev = l[i];
}
prev = (int) 2e9;
for (int i = n - 1; i >= 0; i --) {
r[i] = Math.min(prev + 1, c[i]);
prev = r[i];
}
for (int i = 0; i < n; i ++) System.out.print(Math.min(l[i], r[i]) + " ");
System.out.println();
}
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readLine() throws IOException {
return br.readLine().trim();
}
} | import java.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
while(cases-- > 0) {
br.readLine();
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int[] a = new int[k];
int[] t = new int[k];
str = br.readLine().split(" ");
for(int i=0; i<k; i++) {
a[i] = Integer.parseInt(str[i]) - 1;
}
str = br.readLine().split(" ");
for(int i=0; i<k; i++) {
t[i] = Integer.parseInt(str[i]);
}
int[] temp = new int[n];
Arrays.fill(temp, Integer.MAX_VALUE);
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, Integer.MAX_VALUE);
Arrays.fill(right, Integer.MAX_VALUE);
int ind = 0;
for(int i=0; i<k; i++) {
left[a[i]] = t[i];
right[a[i]] = t[i];
}
int minleft = Integer.MAX_VALUE;
for(int i=0; i<n; i++) {
left[i] = Math.min(left[i], minleft);
minleft = left[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : left[i]+1;
}
int minright = Integer.MAX_VALUE;
for(int i=n-1; i>=0; i--) {
right[i] = Math.min(right[i], minright);
minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1;
}
for(int i=0; i<n; i++) {
temp[i] = Math.min(right[i], left[i]);
System.out.print(temp[i]+" ");
}
System.out.println();
}
}
} | 0 | Non-plagiarised |
4e87c35b | f1f600d9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
long ans = Long.MAX_VALUE;
// sum so far + min*(n-k)
long[] evenSum = new long[n];
long[] oddSum = new long[n];
int[] evenMin = new int[n];
int[] oddMin = new int[n];
evenSum[0] = arr[0];
oddSum[1] = arr[1];
evenMin[0] = arr[0];
oddMin[1] = arr[1];
for (int i = 2; i < n; i++) {
if (i % 2 == 0) {
evenSum[i] = evenSum[i - 2] + arr[i];
evenMin[i] = Math.min(evenMin[i - 2], arr[i]);
} else {
oddSum[i] = oddSum[i - 2] + arr[i];
oddMin[i] = Math.min(oddMin[i - 2], arr[i]);
}
}
for (int i = 1; i < n; i++) {
ans = Math.min(ans, compute(arr, i, evenSum, oddSum, evenMin, oddMin));
}
out.println(ans);
}
private long compute(int[] arr, int i, long[] evenSum, long[] oddSum, int[] evenMin, int[] oddMin) {
if (i % 2 == 0) {
return evenSum[i] + (arr.length - (i / 2) - 1) * (long) evenMin[i] + oddSum[i - 1] + (arr.length - (i / 2)) * (long) oddMin[i - 1];
} else {
return evenSum[i - 1] + (arr.length - (i / 2) - 1) * (long) evenMin[i - 1] + oddSum[i] + (arr.length - (i / 2) - 1) * (long) oddMin[i];
}
}
}
}
|
// import java.util.Vector;
import java.util.*;
import java.lang.Math;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.management.Query;
import java.io.*;
import java.math.BigInteger;
public class Main {
static int mod = 1000000007;
/* ======================DSU===================== */
static class dsu {
static int parent[], n;// min[],value[];
static long size[];
dsu(int n) {
parent = new int[n + 1];
size = new long[n + 1];
// min=new int[n+1];
// value=new int[n+1];
this.n = n;
makeSet();
}
static void makeSet() {
for (int i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
// min[i]=i;
}
}
static int find(int a) {
if (parent[a] == a)
return a;
else {
return parent[a] = find(parent[a]);// Path Compression
}
}
static void union(int a, int b) {
int setA = find(a);
int setB = find(b);
if (setA == setB)
return;
if (size[setA] >= size[setB]) {
parent[setB] = setA;
size[setA] += size[setB];
} else {
parent[setA] = setB;
size[setB] += size[setA];
}
}
}
/* ======================================================== */
static class Pair implements Comparator<Pair> {
long x;
long y;
// Constructor
public Pair(long x, long y) {
this.x = x;
this.y = y;
}
public Pair() {
}
@Override
public int compare(Main.Pair o1, Main.Pair o2) {
return ((int) (o1.x - o2.x));
}
}
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;
}
int[] intArr(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
long[] longArr(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
}
static FastReader f = new FastReader();
static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int UpperBound(int a[], int x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y >>= 1;
x = (x * x);
}
return res;
}
static int power(int x, int y) {
int res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y >>= 1;
x = (x * x);
}
return res;
}
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
/*
* ===========Modular Operations==================
*/
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
static long modAdd(long a, long b) {
return (a % mod + b % mod) % mod;
}
static long modMul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long nCrModPFermat(int n, int r) {
long p = 1000000007;
if (r == 0)
return 1;
long[] fac = new long[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
/*
* ===============================================
*/
static List<Character> removeDup(ArrayList<Character> list) {
List<Character> newList = list.stream().distinct().collect(Collectors.toList());
return newList;
}
static void ruffleSort(long[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
int temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
/*
* ===========Dynamic prog Recur Section===========
*/
static int DP[][];
static ArrayList<ArrayList<Integer>> g;
static int count = 0;
static ArrayList<Long> bitMask(ArrayList<Long> ar, int n) {
ArrayList<Long> ans = new ArrayList<>();
for (int mask = 0; mask <= Math.pow(2, n) - 1; mask++) {
long sum = 0;
for (int i = 0; i < n; i++) {
if (((1 << i) & mask) > 0) {
sum += ar.get(i);
}
}
ans.add(sum);
}
return ans;
}
/*
* ====================================Main=================================
*/
public static void main(String args[]) throws Exception {
// File file = new File("D:\\VS Code\\Java\\Output.txt");
// FileWriter fw = new FileWriter("D:\\VS Code\\Java\\Output.txt");
Random rand = new Random();
int t = 1;
t = f.nextInt();
int tc = 1;
while (t-- != 0) {
int n = f.nextInt();
int c[] = new int[n];
long minOdd = 0, minEven = 0;
long sumEven = 0, sumOdd = 0;
for (int i = 0; i < n ; i++) {
c[i] = f.nextInt();
// if (i % 2 == 0) {
// minEven = (c[minEven] > c[i]) ? i : minEven;
// sumEven += c[i];
// } else {
// minOdd = (minOdd > c[i]) ? i : minOdd;
// sumOdd += c[i];
// }
}
minEven = c[0];
minOdd = c[1];
sumEven=c[0];
sumOdd=c[1];
long min=minEven*n + minOdd*n;//for k=2
int even=1,odd=1;
for (int k = 3; k <= n; k++) {
if(k%2==1){
sumEven+=c[k-1];
minEven=Math.min(minEven, c[k-1]);
even++;
}else{
sumOdd+=c[k-1];
minOdd=Math.min(minOdd, c[k-1]);
odd++;
}
min=Math.min(min, sumEven-minEven+minEven*(n-even+1) + sumOdd-minOdd+minOdd*(n-odd+1));
}
w.write(min+"\n");
}
w.flush();
}
} | 0 | Non-plagiarised |
c9159d9c | d3a96420 | import java.util.*;
public class SolutionB {
public static long gcd(long a, long b){
if(b==0){
return a;
}
return gcd(b, a%b);
}
public static long gcdSum(long b){
long a = 0;
long temp = b;
while(temp!=0){
a = a + temp%10;
temp = temp/10;
}
return gcd(a,b);
}
public static class Pair{
Long a;
Long b;
public Pair(Long a, Long b) {
this.a = a;
this.b = b;
}
}
public static long factorial (long n){
if(n==0)
return 1;
else if(n==1)
return n;
return n * factorial(n-1);
}
public static long lcm (long n){
if(n<3)
return n;
return lcmForBig(n,n-1);
}
private static long lcmForBig(long n, long l) {
if(l==1)
return n;
n = (n * l) / gcd(n, l);
return lcmForBig(n, l-1);
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i =0;i<t;i++) {
int n = s.nextInt();
int arr [] = new int[n];
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int j=0;j<n;j++){
int num = s.nextInt();
arr[j]=num;
}
String color = s.next();
for(int j=0;j<n;j++){
if(color.charAt(j)=='B'){
blue.add(arr[j]);
}
else{
red.add(arr[j]);
}
}
Collections.sort(blue);
String ans = "YES";
int counter = 0;
for(int j=0;j<blue.size();j++){
int current = blue.get(j);
if (current<1){
ans="NO";
break;
}
if(current>counter){
counter++;
}
else{
ans="NO";
break;
}
}
if(ans=="NO"){
System.out.println(ans);
}
else{
int tempCounter = n+1;
Collections.sort(red);
for(int j=red.size()-1;j>=0;j--){
int current = red.get(j);
if(current>=tempCounter){
ans="NO";
break;
}
else{
tempCounter--;
}
}
if(tempCounter-counter!=1)
System.out.println("NO");
else
System.out.println(ans);
}
}
return;
}
}
| import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
String x=sc.next();
Vector<Integer> R=new Vector<>();
Vector<Integer> B=new Vector<>();
for(int i=0;i<n;i++){
if(x.charAt(i)=='B') R.add(a[i]);
else B.add(a[i]);
}
Collections.sort(R);
Collections.sort(B);
boolean yes=true;
for(int i=0;i<R.size();i++){
if(R.get(i)-i<1){System.out.println("NO");yes=false;break;}
}
if(yes)
{
int s=B.size();
for(int j=0;j<s;j++){
if(B.get(j)+s-j>n+1){System.out.println("NO");yes=false;break;}
}
}
if(yes)System.out.println("YES");
}
sc.close();
}
}
| 0 | Non-plagiarised |
b08b1c3c | d92b4600 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class D {
static PrintWriter out = new PrintWriter(System.out);
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
Node head = null;
boolean notPossible = false;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (head == null) {
head = new Node(x);
} else {
if ((head.next != null && x > head.next.value) || (head.prev != null && x < head.prev.value)) {
notPossible = true;
} else if ((head.next == null || x <= head.next.value) && x > head.value) {
if (head.next != null && x == head.next.value) {
head = head.next;
continue;
}
Node temp = head.next;
Node next = new Node(x);
head.next = next;
next.prev = head;
next.next = temp;
if (temp != null) {
temp.prev = next;
}
head = next;
} else if ((head.prev == null || x >= head.prev.value) && x < head.value) {
if (head.prev != null && x == head.prev.value) {
head = head.prev;
continue;
}
Node temp = head.prev;
Node prev = new Node(x);
head.prev = prev;
prev.next = head;
prev.prev = temp;
if (temp != null) {
temp.next = prev;
}
head = prev;
}
}
}
if (notPossible) {
out.println("NO");
} else {
out.println("YES");
} }
out.close();
}
static class Node {
int value;
Node prev;
Node next;
Node(int value) {
this.value = value;
this.prev = null;
this.next = null;
}
}
} | import java.util.Scanner;
public class D_724 {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int test = 0; test < t; test++){
int n = input.nextInt();
ListNode on = new ListNode(input.nextInt(), null, null);
boolean good = true;
for(int i = 1; i < n; i++){
int num = input.nextInt();
if(good){
int at = on.data;
if(num > at){
if(on.next == null || num < on.next.data){
on.next = new ListNode(num, on, on.next);
on = on.next;
if(on.next != null){
on.next.prev = on;
}
}else if(num == on.next.data){
on = on.next;
}else{
good = false;
}
}else if(num < at){
if(on.prev == null || num > on.prev.data){
on.prev = new ListNode(num, on.prev, on);
on = on.prev;
if(on.prev != null){
on.prev.next = on;
}
}else if(num == on.prev.data){
on = on.prev;
}else{
good = false;
}
}
}
}
if(good){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
static class ListNode{
int data;
ListNode prev;
ListNode next;
ListNode(int data, ListNode prev, ListNode next){
this.data = data;
this.prev = prev;
this.next = next;
}
}
}
| 0 | Non-plagiarised |
2eb89317 | fcc7e8fa | import java.util.*;
import java.io.*;
////***************************************************************************
/* public class E_Gardener_and_Tree implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start();
}
public void run(){
WRITE YOUR CODE HERE!!!!
JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!!
}
}
*/
/////**************************************************************************
public class C_Menorah{
public static void main(String[] args) {
FastScanner s= new FastScanner();
//PrintWriter out=new PrintWriter(System.out);
//end of program
//out.println(answer);
//out.close();
StringBuilder res = new StringBuilder();
int t=s.nextInt();
int p=0;
while(p<t){
int n=s.nextInt();
String str1=s.nextToken();
String str2=s.nextToken();
if(str1.equals(str2)){
res.append("0 \n");
}
else{
long count1=0;
long count0=0;
for(int i=0;i<n;i++){
char ch=str1.charAt(i);
if(ch=='1'){
count1++;
}
}
count0=n-count1;
if(count1==0){
res.append("-1 \n");
}
else{
long nice1=0;
long nice0=0;
for(int i=0;i<n;i++){
char ch=str2.charAt(i);
if(ch=='1'){
nice1++;
}
}
nice0=(n-nice1);
int check1=0;
int check2=0;
if((count1==nice1)&&(count0==nice0)){
check1=1;
}
long yo1=(1+count0);
long yo0=(count1-1);
if((yo1==nice1)&&(yo0==nice0)){
check2=1;
}
if(check1==0 && check2==0){
res.append("-1 \n");
}
else{
//System.out.println("here");
long correct=0;
long wrong=0;
long correct1=0;
long correct0=0;
long wrong1=0;
long wrong0=0;
for(int i=0;i<n;i++){
char ch1=str1.charAt(i);
char ch2=str2.charAt(i);
if(ch1==ch2){
correct++;
if(ch1=='1'){
correct1++;
}
else{
correct0++;
}
}
else{
wrong++;
if(ch1=='1'){
wrong1++;
}
else{
wrong0++;
}
}
}
long ans1= solve(correct1,correct0,wrong1,wrong0,1);
long ans2= solve(correct1,correct0,wrong1,wrong0,0);
long ans=Math.min(ans1,ans2);
if(ans>=Integer.MAX_VALUE){
ans=-1;
}
res.append(ans+" \n");
}
}
}
p++;
}
System.out.println(res);
}
private static long solve( long correct1, long correct0, long wrong1, long wrong0,long a) {
long op1=Integer.MAX_VALUE;
long op2=Integer.MAX_VALUE;
if(wrong1==0 && wrong0==0){
return 0;
}
if(a==1){
{
// using correct1
if(correct1>0){
long newcorrect1=1+wrong0;
long newcorrect0=wrong1;
long newwrong1=correct0;
long newwrong0=correct1-1;
op1=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,0));
}
}
}
else{
{
//using wrong1
{
if(wrong1>0){
long newcorrect1=wrong0;
long newcorrect0=wrong1-1;
long newwrong1=1+correct0;
long newwrong0=correct1;
op2=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,1));
}
}
}
}
long ans=Math.min(op1,op2);
return ans;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
static long modpower(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// SIMPLE POWER FUNCTION=>
static long power(long x, long y)
{
long res = 1; // Initialize result
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = res * x;
// y must be even now
y = y >> 1; // y = y/2
x = x * x; // Change x to x^2
}
return res;
}
} | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n, _00 = 0, _01 = 0, _11 = 0, _10 = 0;
n = in.nextInt();
char[] s = in.next().toCharArray();
char[] s1 = in.next().toCharArray();
for (int i = 0; i < n; i++) {
if(s[i]==s1[i]){
if(s[i]=='0'){
_00++;
}
else{
_11++;
}
}
else{
if(s[i]=='0'){
_01++;
}
else{
_10++;
}
}
}
int ans = Integer.MAX_VALUE;
if(_10 ==_01){
ans = 2*_01;
}
if(_11 == _00 + 1){
ans = Math.min(ans, 2*_00 + 1);
}
if(ans == Integer.MAX_VALUE){
out.println(-1);
}
else{
out.println(ans);
}
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a, b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return o.a - this.a;
}
}
static class arrayListClass {
ArrayList<Integer> arrayList2 ;
public arrayListClass(ArrayList<Integer> arrayList) {
this.arrayList2 = arrayList;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static 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());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | 0 | Non-plagiarised |
6e7cd58b | cfba313d | import java.util.*;
import java.io.*;
public class CodeforcesRound734 {
static FastReader sc = new FastReader();
public static void main(String[] args) throws IOException {
try {
int t = sc.nextInt();
while (t-- > 0) {
// A();
// B1();
// B2();
C();
}
} catch (Exception e) {
// return;
e.printStackTrace();
}
}
static void C() {
int n = sc.nextInt();
int a[][] = new int[5][n];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == 'a')
a[0][i]++;
else
a[0][i]--;
if (s.charAt(j) == 'b')
a[1][i]++;
else
a[1][i]--;
if (s.charAt(j) == 'c')
a[2][i]++;
else
a[2][i]--;
if (s.charAt(j) == 'd')
a[3][i]++;
else
a[3][i]--;
if (s.charAt(j) == 'e')
a[4][i]++;
else
a[4][i]--;
}
}
for (int x[] : a) {
Arrays.sort(x);
}
int ans = 0;
for (int i = 0; i < 5; i++) {
int temp = 0, cnt = 0;
for (int j = n - 1; j >= 0; j--) {
temp += a[i][j];
if (temp <= 0)
break;
else
cnt++;
}
ans = Math.max(ans, cnt);
}
System.out.println(ans);
}
static void B2() {
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<ArrayList<Integer>> al = new ArrayList<>();
for (int i = 0; i <= n; i++) {
al.add(new ArrayList<>());
}
int ans[] = new int[n + 1];
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
al.get(x).add(i + 1);
}
ArrayList<Integer> temp = new ArrayList<>();
for (ArrayList<Integer> ind : al) {
if (ind.size() >= k) {
for (int i = 0; i < k; i++) {
ans[ind.get(i)] = i + 1;
}
} else {
temp.addAll(ind);
}
}
int color = 0;
for (int i = 0; i < temp.size() / k; i++) {
for (int j = i * k; j < i * k + k; j++) {
ans[temp.get(j)] = ++color;
if (color == k)
color = 0;
}
}
for (int i = 1; i <= n; i++) {
System.out.print(ans[i] + " ");
}
System.out.println();
}
static void B1() {
String s = sc.next();
HashMap<Character, Integer> map = new HashMap<>();
for (char ch : s.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
if (s.length() < 2) {
System.out.println(0);
return;
}
int s1 = 0;
for (int v : map.values()) {
if (v > 2) {
s1 += 2;
} else {
s1 += v;
}
}
System.out.println(s1 / 2);
}
static void A() {
int n = sc.nextInt();
if (n % 3 == 0) {
System.out.println(n / 3 + " " + n / 3);
} else if (n % 3 == 1) {
System.out.println((n / 3 + 1) + " " + (n / 3));
} else {
System.out.println((n / 3) + " " + (n / 3 + 1));
}
}
static boolean[] seiveOfEratosthenes(int n) {
boolean[] isPrime = new boolean[n + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
return isPrime;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean isPrime(long n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
}
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());
}
int[] readIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(next());
}
return a;
}
void printIntArray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
long[] readLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = Long.parseLong(next());
}
return a;
}
void printLongArray(long a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| import java.io.*;
import java.util.*;
public class C {//Any Class Name
static class Code {
private void solve(InputReader in, OutputWriter out) throws IOException {
ArrayOpn o= new ArrayOpn();
int t = in.readInt();
while(t -- > 0) {
int n=in.readInt();
int[][] freq= new int[5][n];
for(int i=0; i<n; i++) {
char s[]= in.readString().toCharArray();
for(char e: s) {
if(e=='a') {
freq[0][i]++;
}
else {
freq[0][i]--;
}
if(e=='b') {
freq[1][i]++;
}
else {
freq[1][i]--;
}
if(e=='c') {
freq[2][i]++;
}
else {
freq[2][i]--;
}
if(e=='d') {
freq[3][i]++;
}
else {
freq[3][i]--;
}
if(e=='e') {
freq[4][i]++;
}
else {
freq[4][i]--;
}
}
}
for(int i=0; i<5; i++) {
Arrays.sort(freq[i]);
}
int ans=0;
for(int i=0; i<5; i++) {
int temp=0, count=0;
for(int j=n-1; j>=0; j--) {
temp+=freq[i][j];
if(temp<=0) {
break;
}
count++;
}
ans=Math.max(ans, count);
}
out.printLine(ans);
}
out.close();
}
}
static class ArrayOpn{
private int[] aI(InputReader in, int n) {
int A[]= new int[n];
for(int i=0; i<n; i++) {
A[i]=in.readInt();
}
return A;
}
static final Random random = new Random();
private void sort(int A[]) {
int n = A.length;
for(int i=0; i<n; i++)
{
int j = random.nextInt(n),temp = A[j];
A[j] = A[i];
A[i] = temp;
}
Arrays.sort(A);
// return A;
}
private int[][] mI(InputReader in, int n, int m){
int A[][]= new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
A[i][j]=in.readInt();
}
}
return A;
}
private int minEl(int A[]) {
int min=Integer.MAX_VALUE;
for(int e: A) {
min=Math.min(e, min);
}
return min;
}
private int maxEl(int A[]) {
int max=Integer.MIN_VALUE;
for(int e: A) {
max=Math.max(e, max);
}
return max;
}
private int occurence(int A[], int e) {
int c=0;
for(int k: A) {
if(k==e) {
c++;
}
}
return c;
}
public int linears(int A[], int e) {
for(int i=0; i<A.length; i++) {
if(A[i]==e) {
return i;
}
}
return -1;
}
public int binarys(int A[], int e) {
int low=0, high=A.length-1;
while(low<=high) {
int mid=(low+high)/2;
if(A[mid]==e) {
return mid;
}
else if(A[mid]<e) {
low=mid+1;
}
else {
high=mid-1;
}
}
return -1;
}
public void printAr(OutputWriter out, int A[]) {
for(int e: A) {
out.print(e+" ");
}
}
}
static class Pair<F, S>{
private final F first; //first member of pair
private final S second; //second member of pair
private Pair(F first, S second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass())
return false;
Pair<? , ?> pair = (Pair<?, ?>) o;
if(!first.equals(pair.first))
return false;
return second.equals(pair.second);
}
@Override
public int hashCode() {
return 31 * first.hashCode() + second.hashCode();
}
@Override
public String toString() {
return "(" + first +", "+ second +")";
}
public static <F,S> Pair<F,S> of(F a, S b) {
return new Pair<>(a,b);
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
}
// Comparator for using in Sorting On integers
static class PairCompare implements Comparator<Pair<Integer, Integer>> {
@Override
public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
int diff = Integer.compare(p1.first, p2.first);
if(diff == 0) {
return Integer.compare(p1.second, p2.second);
}
else return diff;
}
}
public static void main(String[] args) throws IOException {
//initialize
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
Code solver = new Code();
solver.solve(in, out);
out.flush();
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private InputReader(InputStream stream) {
this.stream = stream;
}
private 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++];
}
private String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private double readDouble() {
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, readInt());
}
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, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
private boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public String next() {
return readString();
}
private static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
private OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
outputStream)));
}
private OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
private void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.flush();
}
private void printLine(Object... objects) {
print(objects);
writer.println();
writer.flush();
}
private void printLine(int i) {
writer.println(i);
}
private void close() {
writer.close();
}
private void flush() {
writer.flush();
}
}
static class IOUtils {
private static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
private static long[] readLongArray(InputReader in, int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++)
array[i] = in.readLong();
return array;
}
private static char[] readCharArray(InputReader in, int size) {
char[] array = new char[size];
for (int i = 0; i < size; i++)
array[i] = in.readCharacter();
return array;
}
private static char[][] readTable(InputReader in, int rowCount,
int columnCount) {
char[][] table = new char[rowCount][];
for (int i = 0; i < rowCount; i++)
table[i] = readCharArray(in, columnCount);
return table;
}
}
static class ArrayUtils {
private static void fill(int[][] array, int value) {
for (int[] row : array)
Arrays.fill(row, value);
}
}
static class MiscUtils {
private static final int[] DX4 = {1, 0, -1, 0};
private static final int[] DY4 = {0, -1, 0, 1};
private static boolean isValidCell(int row, int column, int rowCount,
int columnCount) {
return row >= 0 && row < rowCount && column >= 0
&& column < columnCount;
}
}
} | 1 | Plagiarised |
4da08761 | a5d5a95f | import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Solution {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Main solver = new Main();
boolean multipleTC = true;
int testCount = multipleTC ? Integer.parseInt(in.next()) : 1;
for (int i = 1; i <= testCount; i++)
solver.solve(in, out, i);
out.close();
}
static class Main {
PrintWriter out;
InputReader in;
public void solve(InputReader in, PrintWriter out, int test) {
this.out = out;
this.in = in;
int n = ni();
String[] arr = new String[n];
int[][] freq = new int[n][5];
int[][] rem = new int[n][5];
for(int i = 0; i < n; i++){
arr[i] = n();
for(int j = 0; j < arr[i].length(); j++)
freq[i][arr[i].charAt(j) - 'a']++;
for(int j = 0; j < 5; j++)
rem[i][j] = arr[i].length() - freq[i][j];
}
int ans = 0;
for(int i = 0; i < 5; i++){
int[] vals = new int[n];
for(int j = 0; j < n; j++)
vals[j] = freq[j][i] - rem[j][i];
Arrays.sort(vals);
int sum = 0, x = 0;
for(int j = n - 1; j >= 0; j--){
if(sum + vals[j] > 0){
x++;
sum += vals[j];
} else {
break;
}
}
if(x > ans) {
ans = x;
}
}
pn(ans);
}
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
String n(){
return in.next();
}
int ni() {
return in.nextInt();
}
long nl() {
return in.nextLong();
}
void pn(long zx) {
out.println(zx);
}
void pn(String sz) {
out.println(sz);
}
void pn(double dx){
out.println(dx);
}
class Tuple {
long x;
long y;
Tuple(long a, long b) {
x = a;
y = b;
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new UnknownError();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | import java.util.*;
import java.io.*;
public class Main {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{ try {br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);}
catch(Exception e) { 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;
}
}
// end of fast i/o code
public static void main(String[] args) {
FastReader reader = new FastReader();
int Q = reader.nextInt();
outer: for (int q = 0; q < Q; q++) {
int N = reader.nextInt();
int[][] scores = new int[5][N];
for (int i = 0; i < N; i++) {
int[] occurs = new int[5];
String word = reader.next();
for (int j = 0; j < word.length(); j++) {
occurs[word.charAt(j) - 'a']++;
}
for (int j = 0; j < 5; j++) {
scores[j][i] = occurs[j] - (word.length() - occurs[j]) ;
}
}
int bestCount = 0;
for (int i = 0; i < 5; i++) {
int[] curr = scores[i];
Arrays.sort(curr);
int currentCount = 1;
int currentScore = curr[curr.length - 1];
for (int j = curr.length - 2; j >= 0 && currentScore > 0; j--) {
currentScore += curr[j];
currentCount++;
}
if (currentScore <= 0) currentCount--;
bestCount = Math.max(currentCount, bestCount);
}
System.out.println(bestCount);
}
}
}
| 0 | Non-plagiarised |
26ad111c | b0a01ee7 | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
FastReader f = new FastReader();
StringBuffer sb=new StringBuffer();
int test=f.nextInt();
while(test-->0)
{
int n=f.nextInt();
String str[]=new String[n];
for(int i=0;i<n;i++)
str[i]=f.next();
int max=0;
for(char ch='a';ch<='e';ch++)
max=Math.max(max,solve(str,ch));
sb.append(max+"\n");
}
System.out.println(sb);
}
static int solve(String str[],char ch)
{
int count=0;
int adv=0,equal=0;
int totalAdv=0;
List<Integer> c=new ArrayList<>();
for(int i=0;i<str.length;i++)
{
int countC=0;
int countTtl=0;
for(int j=0;j<str[i].length();j++)
{
if(str[i].charAt(j)==ch)
countC++;
else
countTtl++;
}
if(countC>countTtl)
{
adv++;
totalAdv+=(countC-countTtl);
}
else if(countC==countTtl)
equal++;
else
c.add(Math.abs(countTtl-countC));
}
if(adv>0)
count+=adv+equal;
Collections.sort(c);
for(int i:c)
{
if(totalAdv>i)
{
totalAdv-=i;
count++;
}
}
return count;
}
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;
}
}
} | import java.util.*;
public class interestingstory {
public static void main(String[] args) throws Exception {
// your code goes here
try{
Scanner sc =new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int answer = 0;
String [] string_arr = new String[n];
for (int i = 0; i< n; i++){
string_arr[i] = sc.next();
}
for(char c = 'a'; c <= 'e'; c++){
int [] diff = new int[n];
for (int j = 0; j < string_arr.length; j++){
for(int i = 0; i < string_arr[j].length(); i++){
if(string_arr[j].charAt(i) == c){
diff[j] += 1;
} else {
diff[j] += -1;
}
}
}
Arrays.sort(diff);
int sum = 0;
int ans = n;
for(int p = n-1; p >= 0;p--){
sum += diff[p];
if (sum <= 0) {
ans = n - p - 1;
break;
}
}
answer = Math.max(answer, ans);
}
System.out.println(answer);
}
} catch(java.lang.Exception e){}
}
} | 0 | Non-plagiarised |
a5d5a95f | ab7507bf | import java.util.*;
import java.io.*;
public class Main {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{ try {br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);}
catch(Exception e) { 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;
}
}
// end of fast i/o code
public static void main(String[] args) {
FastReader reader = new FastReader();
int Q = reader.nextInt();
outer: for (int q = 0; q < Q; q++) {
int N = reader.nextInt();
int[][] scores = new int[5][N];
for (int i = 0; i < N; i++) {
int[] occurs = new int[5];
String word = reader.next();
for (int j = 0; j < word.length(); j++) {
occurs[word.charAt(j) - 'a']++;
}
for (int j = 0; j < 5; j++) {
scores[j][i] = occurs[j] - (word.length() - occurs[j]) ;
}
}
int bestCount = 0;
for (int i = 0; i < 5; i++) {
int[] curr = scores[i];
Arrays.sort(curr);
int currentCount = 1;
int currentScore = curr[curr.length - 1];
for (int j = curr.length - 2; j >= 0 && currentScore > 0; j--) {
currentScore += curr[j];
currentCount++;
}
if (currentScore <= 0) currentCount--;
bestCount = Math.max(currentCount, bestCount);
}
System.out.println(bestCount);
}
}
}
| import java.util.*;
import java.io.*;
public class C1551 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
char[][] arr = new char[n][];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.next().toCharArray();
}
int[][] cnt = new int[n][5];
for (int i = 0; i < cnt.length; i++) {
for (char c : arr[i]) {
cnt[i][c - 'a']++;
}
}
int fans = 0;
for (int letter = 0; letter < 5; letter++) {
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
al.add(2 * cnt[i][letter] - arr[i].length);
}
Collections.sort(al, Collections.reverseOrder());
int sum = 0;
int ans = 0;
for (int x : al) {
sum += x;
if (sum > 0) {
ans++;
} else {
break;
}
}
fans = Math.max(ans, fans);
}
pw.println(fans);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
| 0 | Non-plagiarised |
0588b869 | 69b2fd22 | import java.util.*;
import java.io.*;
public class Solution
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static final long mod=(long)1e9+7;
public static long pow(long a,int p)
{
long res=1;
while(p>0)
{
if(p%2==1)
{
p--;
res*=a;
res%=mod;
}
else
{
a*=a;
a%=mod;
p/=2;
}
}
return res;
}
static class Pair
{
int u,v,w;
Pair(int u,int v,int w)
{
this.u=u;
this.v=v;
this.w=w;
}
}
/*static class Pair implements Comparable<Pair>
{
int v,l;
Pair(int v,int l)
{
this.v=v;
this.l=l;
}
public int compareTo(Pair p)
{
return l-p.l;
}
}*/
static int gcd(int a,int b)
{
if(b%a==0)
return a;
return gcd(b%a,a);
}
public static void dfs(int u,int dist[],int sub[],int mxv[],int par[],ArrayList<Integer> edge[])
{
sub[u]=1;
for(int v:edge[u])
{
if(dist[v]==-1)
{
par[v]=u;
dist[v]=dist[u]+1;
dfs(v,dist,sub,mxv,par,edge);
if(sub[v]+1>sub[u])
{
sub[u]=sub[v]+1;
mxv[u]=v;
}
}
}
}
public static void main(String args[])throws Exception
{
FastReader fs=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
//int tc=fs.nextInt();
int n=fs.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=fs.nextInt();
ArrayList<Integer> o=new ArrayList<>();
ArrayList<Integer> z=new ArrayList<>();
for(int i=0;i<n;i++)
{
if(a[i]==1)o.add(i);
else z.add(i);
}
int ans[][]=new int[o.size()+1][z.size()+1];
for(int i=1;i<=o.size();i++)
{
for(int j=i;j<=z.size();j++)
{
if(i==j)ans[i][j]=ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1));
else
ans[i][j]=Math.min(ans[i][j-1],ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1)));
}
}
pw.println(ans[o.size()][z.size()]);
pw.flush();
pw.close();
}
} | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=1;
while(T-->0)
{
int n=input.nextInt();
int a[]=new int[n];
ArrayList<Integer> list=new ArrayList<>();
ArrayList<Integer> space=new ArrayList<>();
for(int i=0;i<n;i++)
{
a[i]=input.nextInt();
if(a[i]==1)
{
list.add(i);
}
else
{
space.add(i);
}
}
int pre[]=new int[space.size()];
for(int i=0;i<list.size();i++)
{
if(i==0)
{
int min=Integer.MAX_VALUE;
for(int j=0;j<space.size();j++)
{
pre[j]=Math.abs(list.get(i)-space.get(j));
min=Math.min(min,pre[j]);
pre[j]=min;
}
}
else
{
int arr[]=new int[space.size()];
for(int j=0;j<i;j++)
{
arr[j]=Integer.MAX_VALUE;
}
int min=Integer.MAX_VALUE;
for(int j=i;j<space.size();j++)
{
int v=Math.abs(list.get(i)-space.get(j));
v+=pre[j-1];
arr[j]=v;
min=Math.min(min,v);
arr[j]=min;
}
for(int j=0;j<space.size();j++)
{
pre[j]=arr[j];
}
}
}
out.println(pre[space.size()-1]);
}
out.close();
}
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;
}
}
} | 0 | Non-plagiarised |
343cc8e7 | 7f69a9e8 | //make sure to make new file!
import java.io.*;
import java.util.*;
public class D669b{
public static void main(String[] args)throws IOException{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(f.readLine());
StringTokenizer st = new StringTokenizer(f.readLine());
int[] array = new int[n];
for(int k = 0; k < n; k++){
array[k] = Integer.parseInt(st.nextToken());
}
ArrayList<HashSet<Integer>> adj = new ArrayList<HashSet<Integer>>(n);
for(int k = 0; k < n; k++) adj.add(new HashSet<Integer>());
for(int k = 0; k < n-1; k++){
adj.get(k).add(k+1);
}
//closest number before that is <
Stack<Num> stk = new Stack<Num>();
stk.add(new Num(array[0],0));
for(int k = 1; k < n; k++){
while(!stk.isEmpty() && stk.peek().x > array[k]){
stk.pop();
}
if(!stk.isEmpty()){
adj.get(stk.peek().i).add(k);
}
stk.add(new Num(array[k],k));
}
//closest number after that is <
stk = new Stack<Num>();
stk.add(new Num(array[n-1],n-1));
for(int k = n-2; k >= 0; k--){
while(!stk.isEmpty() && stk.peek().x > array[k]){
stk.pop();
}
if(!stk.isEmpty()){
adj.get(k).add(stk.peek().i);
}
stk.add(new Num(array[k],k));
}
//closest number before that is >
stk = new Stack<Num>();
stk.add(new Num(array[0],0));
for(int k = 1; k < n; k++){
while(!stk.isEmpty() && stk.peek().x < array[k]){
stk.pop();
}
if(!stk.isEmpty()){
adj.get(stk.peek().i).add(k);
}
stk.add(new Num(array[k],k));
}
//closest number after that is >
stk = new Stack<Num>();
stk.add(new Num(array[n-1],n-1));
for(int k = n-2; k >= 0; k--){
while(!stk.isEmpty() && stk.peek().x < array[k]){
stk.pop();
}
if(!stk.isEmpty()){
adj.get(k).add(stk.peek().i);
}
stk.add(new Num(array[k],k));
}
int[] path = new int[n];
Arrays.fill(path,Integer.MAX_VALUE);
path[0] = 0;
for(int k = 0; k < n; k++){
for(int nei : adj.get(k)){
path[nei] = Math.min(path[nei],path[k]+1);
}
}
int answer = path[n-1];
out.println(answer);
out.close();
}
public static class Num{
int x;
int i;
public Num(int a, int b){
x = a;
i = b;
}
}
} | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.ArrayList;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DDiscreteCentrifugalJumps solver = new DDiscreteCentrifugalJumps();
solver.solve(1, in, out);
out.close();
}
static class DDiscreteCentrifugalJumps {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
ArrayList<Integer> jumps[] = new ArrayList[n];
int h[] = new int[n];
for (int i = 0; i < n; i++) {
h[i] = in.nextInt();
jumps[i] = new ArrayList<>();
}
Stack<int[]> s = new Stack<>();
for (int i = 0; i < n; i++) {
while (s.size() > 0 && s.peek()[0] > h[i]) {
s.pop();
}
if (s.size() > 0) {
jumps[i].add(s.peek()[1]);
}
s.add(new int[]{h[i], i});
}
s.clear();
for (int i = 0; i < n; i++) {
while (s.size() > 0 && s.peek()[0] < h[i]) {
s.pop();
}
if (s.size() > 0) {
jumps[i].add(s.peek()[1]);
}
s.add(new int[]{h[i], i});
}
s.clear();
for (int i = n - 1; i >= 0; i--) {
while (s.size() > 0 && s.peek()[0] < h[i]) {
s.pop();
}
if (s.size() > 0) {
jumps[s.peek()[1]].add(i);
}
s.add(new int[]{h[i], i});
}
s.clear();
for (int i = n - 1; i >= 0; i--) {
while (s.size() > 0 && s.peek()[0] > h[i]) {
s.pop();
}
if (s.size() > 0) {
jumps[s.peek()[1]].add(i);
}
s.add(new int[]{h[i], i});
}
s.clear();
int dp[] = new int[n];
dp[0] = 0;
for (int i = 1; i < n; i++) {
dp[i] = Integer.MAX_VALUE;
for (int x : jumps[i]) {
dp[i] = Math.min(dp[i], dp[x] + 1);
}
}
out.print(dp[n - 1]);
}
}
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());
}
}
}
| 0 | Non-plagiarised |
3088ca9c | 7a9c69d8 | import java.util.*;
import java.io.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
int n = sc.nextInt();
sc.nextLine();
String [] str = new String[n];
int res = 0;
for(int i=0;i<n;i++)
{
str[i]=sc.nextLine();
}
int [][] freq = new int [n][5];
for(int i=0;i<n;i++)
{
for(int j=0;j<str[i].length();j++)
{
int k = str[i].charAt(j)-'a';
freq[i][k]++;
}
}
for(int i=0;i<5;i++)
{
int [] arr = new int[n];
for(int j=0;j<n;j++)
{
int pos = freq[j][i];
int sum=0;
for(int k = 0;k<5;k++)
{
sum+=freq[j][k];
}
sum-=pos;
arr[j]=(pos-sum);
}
Arrays.sort(arr);
// int p = n-1;
int count=0;
int sum=0;
for(int p=n-1;p>=0;p--)
{
sum+=arr[p];
if(sum>0)
{
count++;
}
else
{
break;
}
}
res=Math.max(count , res);
}
System.out.println(res);
t--;
}
}
} | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
try{
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int lst[][] = new int[n][5];
for(int i=0; i<n; i++){
String s = br.readLine();
for(int j=0; j<s.length(); j++){
lst[i][s.charAt(j)-'a']++;
}
}
int fans = Integer.MIN_VALUE;
for(int i=0; i<5; i++){
int val[] = new int[n];
for(int k=0; k<n; k++){
int sum = 0;
for(int j=0; j<5; j++){
if(i==j){
sum += lst[k][j];
}else{
sum -= lst[k][j];
}
}
val[k] = sum;
}
Arrays.sort(val);
int sum = 0;
int ans = 0;
for(int x = n-1; x>=0; x--){
sum+=val[x];
if(sum>0){
ans++;
}else{
break;
}
}
fans = Math.max(fans, ans);
}
bw.write(fans+"\n");
}
bw.flush();
}catch(Exception e){
return;
}
}
}
| 1 | Plagiarised |
66e74577 | 6e207cbf | import java.io.*;
import java.util.*;
public class Menorah {
public static void main(String[] args) throws Exception {
FastIO in = new FastIO();
int t = in.nextInt();
for (int tc=0; tc<t; tc++) {
int n = in.nextInt();
String original = in.next();
String target = in.next();
int numDiff = 0;
int original1 = 0;
int target1 = 0;
for (int i=0; i<n; i++) {
if (original.charAt(i)!=target.charAt(i)) {
numDiff++;
}
if (original.charAt(i)=='1') original1++;
if (target.charAt(i)=='1') target1++;
}
int evenAns = Integer.MAX_VALUE;
int oddAns = Integer.MAX_VALUE;
if (original1==target1) {
evenAns = numDiff;
}
for (int i=0; i<n; i++) {
if (original.charAt(i)=='1' && target.charAt(i)=='1') {
int ones = (n-original1)+1;
if (ones==target1) {
oddAns = n-numDiff;
}
break;
}
}
for (int i=0; i<n; i++) {
if (original.charAt(i)=='1' && target.charAt(i)=='0') {
int ones = (n-original1)+1;
if (ones==target1) {
oddAns = Math.min(n-numDiff+1, oddAns);
}
break;
}
}
int ans = Math.min(evenAns, oddAns);
if (ans==Integer.MAX_VALUE) System.out.println(-1);
else System.out.println(ans);
}
}
static class FastIO {
BufferedReader br;
StringTokenizer st;
public FastIO() throws IOException
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
public String next() throws IOException
{
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); }
public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); }
public double nextDouble() throws NumberFormatException, IOException
{
return Double.parseDouble(next());
}
public String nextLine() throws IOException
{
String str = br.readLine();
return str;
}
}
}
| import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int sm, n;
while(t > 0) {
t--;
n = sc.nextInt();
String s1,s2;
s1 = sc.next();
s2 = sc.next();
int a[] = new int[4];
a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0;
for(int i = 0 ; i < n ; i++) {
if(s1.charAt(i) == '0'&& s2.charAt(i) == '1') a[0]++;
else if(s1.charAt(i) == '1'&& s2.charAt(i) == '0') a[1]++;
else if(s1.charAt(i) == '1'&& s2.charAt(i) == '1') a[2]++;
else a[3]++;
}
// System.out.println(a[0] + " " + a[1] + " " + a[2] + " " + a[3]);
int n1 = Integer.MAX_VALUE, n2 = Integer.MAX_VALUE, n3 = Integer.MAX_VALUE;
if (a[0] == a[1]) {
n1 = 2*a[0];
}
if((a[2] - 1) == a[3]) {
// System.out.println(a[3] + 1);
n2 = 2*a[3] + 1;
}
if((a[3] + 1) == a[2]) {
// System.out.println(a[2] + 1);
n3 = 2*a[2] + 1;
}
int ans = Math.min(n1, Math.min(n2,n3));
if(ans == Integer.MAX_VALUE) {
System.out.println("-1");
} else {
System.out.println(ans);
}
}
}
}
| 0 | Non-plagiarised |
3c667d4f | a7894e0b | import java.util.*;
public class j
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
while(n-->0)
{
int len=in.nextInt();
int t=in.nextInt();
int pos[]=new int[t];
int temp[]=new int[t];
for(int i=0;i<t;i++)
pos[i]=in.nextInt();
for(int i=0;i<t;i++)
temp[i]=in.nextInt();
long range[]=new long[len];
Arrays.fill(range,Long.MAX_VALUE-10000);
for(int i=0;i<t;i++)
range[pos[i]-1]=temp[i];
for(int i=1;i<len;i++)
{
range[i]=Math.min(range[i],1+range[i-1]);
}
for(int i=len-2;i>=0;i--)
{
range[i]=Math.min(range[i+1]+1,range[i]);
}
for(int i=0;i<len;i++)
{
System.out.print(range[i]+" ");
}System.out.println();
}
}
}
|
import java.io.*;
import java.math.*;
import java.util.*;
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
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[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n=sc.nextInt();
int k=sc.nextInt();
int[] ac=new int[k];
long[] ans=new long[n];
Arrays.fill(ans, Integer.MAX_VALUE/2);
for(int i=0;i<k;i++) {
ac[i]=sc.nextInt()-1;
}
for(int i=0;i<k;i++) {
long x = sc.nextLong();
ans[ac[i]] = x;
}
for(int i=1;i<n;i++) {
ans[i]=Math.min(ans[i], ans[i-1]+1);
}
for(int i=n-2;i>=0;i--) {
ans[i]=Math.min(ans[i], ans[i+1]+1);
}
for(int i=0;i<n;i++) {
res.append(ans[i]+" ");
}
res.append("\n");
}
System.out.println(res);
}
}
| 1 | Plagiarised |
595f5d6c | 6653a758 | import java.math.BigInteger;
//import static java.lang.Math.max;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.Scanner;
public class ahh {
//trihund
static Scanner scn = new Scanner(System.in);
static boolean vis[][];
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader s = new FastReader();
static int MOD = 1000000007;
public static void main(String[] args) {
int n=scn.nextInt(),count=0;
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=scn.nextInt();
}
ArrayList<Integer>zer=new ArrayList<Integer>(),one=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
if(arr[i]==0)
zer.add(i);
else
one.add(i);
}
count=one.size();
long memo[][]=new long[one.size()+1][zer.size()+1];
for(int i=0;i<=one.size();i++)
{
for(int j=0;j<=zer.size();j++)
memo[i][j]=-1;
}
System.out.println(arm(one, zer, 0, 0, count,memo));
}
public static long arm(ArrayList<Integer>one,ArrayList<Integer>zer,int i,int j,int count,long memo[][])
{ if(count==0)
return 0;
if(i==one.size()||j==zer.size())
return Integer.MAX_VALUE;
if(memo[i][j]!=-1)
return memo[i][j];
long a=Integer.MAX_VALUE,b=Integer.MAX_VALUE;
a=arm(one, zer, i+1, j+1,count-1,memo)+Math.abs(one.get(i)-zer.get(j));
b=arm(one, zer, i, j+1,count,memo);
memo[i][j]=Math.min(a, b);
return Math.min(a, b);
}
public static void fac(int n) {
BigInteger b = new BigInteger("1");
for (int i = 1; i <= n; i++) {
b = b.multiply(BigInteger.valueOf(i));
}
System.out.println(b);
}
static void ruffleSort(long[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
}
class pm {
int ini, fin;
pm(int a, int b) {
this.ini = a;
this.fin = b;
}
}
class surt implements Comparator<pm> {
@Override
public int compare(pm o1, pm o2) {
// TODO Auto-generated method stub
int a = o1.ini - o2.ini, b = o1.fin - o2.fin;
if (a < 0)
return -1;
if (a == 0) {
if (b < 0)
return -1;
else
return 1;
} else
return 1;
}
}
class pair {
int x, y;
pair(int a, int b) {
this.x = a;
this.y = b;
}
public int hashCode() {
return x * 31 + y * 31;
}
public boolean equals(Object other) {
if (this == other)
return true;
if (other instanceof pair) {
pair pt = (pair) other;
return pt.x == this.x && pt.y == this.y;
} else
return false;
}
}
class sort implements Comparator<pair> {
@Override
public int compare(pair o1, pair o2) {
// TODO Auto-generated method stub
long a = o1.x - o2.x, b = o1.y - o2.y;
if (b < 0)
return -1;
else if (a == 0) {
if (a < 0)
return -1;
else
return 1;
} else
return 1;
}
} | import java.util.*;
public class D {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>();
int n = sc.nextInt(),dp[][]=new int[n+1][n+1];
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++)
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
System.out.println(dp[o.size()][e.size()]);
}
} | 0 | Non-plagiarised |
0f14b12d | 7d12d33c | //import jdk.nashorn.internal.parser.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.util.*;
import javax.management.Query;
public class Test{
public static void main(String[] args) throws IOException, InterruptedException{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
String [] words = new String[n];
int [] occ = new int[5];
int [] occWord = new int [5];
boolean [] found ;
for(int i =0;i<n;i++){
words[i] = sc.nextLine();
found = new boolean[5];
for(int j=0 ; j<words[i].length();j++){
occ[words[i].charAt(j)-'a']++;
if(!found[words[i].charAt(j)-'a']){
found[words[i].charAt(j)-'a']=true;
occWord[words[i].charAt(j)-'a'] ++;
}
}
}
int maxRes =0;
for(int i =0;i<5;i++){
int maxChar = 'a' +i;
PriorityQueue<Pair> pq = new PriorityQueue<>();
for (String word : words){
pq.add(new Pair(word,occOfMaxChar(word, maxChar)-occOfOtherChar(word, maxChar)));
}
int res = 0;
int curr = 0;
int maxCharCount = 0;
int otherCharCount =0;
while(!pq.isEmpty()){
String word = pq.poll().x;
maxCharCount +=occOfMaxChar(word, maxChar);
otherCharCount += occOfOtherChar(word, maxChar);
curr ++;
if(maxCharCount >otherCharCount){
res = curr;
}
}
maxRes = Math.max(maxRes, res);
}
System.out.println(maxRes);}
}
public static int occOfMaxChar (String s, int maxChar){
int occ = 0;
for(int i =0 ;i<s.length();i++){
if(s.charAt(i)==maxChar){
occ++;
}
}
return occ;
}
public static int occOfOtherChar (String s, int maxChar){
int occ = 0;
for(int i =0 ;i<s.length();i++){
if(s.charAt(i)!=maxChar){
occ++;
}
}
return occ;
}
static int w;
static int n;
static long [][] memo;
static int [] depth ;
static long[] values;
static ArrayList<Pair> gold ;
public static long dp (int idx,int time){
if ( idx == n){
return 0;
}
if (memo[idx][time] != -1){
return memo[idx][time];
}
long take = 0;
if (3 * w*depth[idx] <= time){
take = values[idx]+ dp(idx+1, time-3*w*depth[idx]);
}
long leave = dp(idx+1, time);
return memo[idx][time]=Math.max(take, leave);
}
static class Pair implements Comparable {
String x;
int y;
public Pair (String x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Object o){
Pair p = (Pair) o;
return p.y -y;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| //import jdk.nashorn.internal.parser.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.util.*;
import javax.management.Query;
public class Test{
public static void main(String[] args) throws IOException, InterruptedException{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
String [] words = new String[n];
for(int i =0;i<n;i++){
words[i] = sc.nextLine();
}
int maxRes =0;
for(int i =0;i<5;i++){
int maxChar = 'a' +i;
PriorityQueue<Pair> pq = new PriorityQueue<>();
for (String word : words){
pq.add(new Pair(word,occOfMaxChar(word, maxChar)-occOfOtherChar(word, maxChar)));
}
int res = 0;
int curr = 0;
int maxCharCount = 0;
int otherCharCount =0;
while(!pq.isEmpty()){
String word = pq.poll().x;
maxCharCount +=occOfMaxChar(word, maxChar);
otherCharCount += occOfOtherChar(word, maxChar);
curr ++;
if(maxCharCount >otherCharCount){
res = curr;
}
}
maxRes = Math.max(maxRes, res);
}
System.out.println(maxRes);}
}
public static int occOfMaxChar (String s, int maxChar){
int occ = 0;
for(int i =0 ;i<s.length();i++){
if(s.charAt(i)==maxChar){
occ++;
}
}
return occ;
}
public static int occOfOtherChar (String s, int maxChar){
int occ = 0;
for(int i =0 ;i<s.length();i++){
if(s.charAt(i)!=maxChar){
occ++;
}
}
return occ;
}
static int w;
static int n;
static long [][] memo;
static int [] depth ;
static long[] values;
static ArrayList<Pair> gold ;
public static long dp (int idx,int time){
if ( idx == n){
return 0;
}
if (memo[idx][time] != -1){
return memo[idx][time];
}
long take = 0;
if (3 * w*depth[idx] <= time){
take = values[idx]+ dp(idx+1, time-3*w*depth[idx]);
}
long leave = dp(idx+1, time);
return memo[idx][time]=Math.max(take, leave);
}
static class Pair implements Comparable {
String x;
int y;
public Pair (String x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Object o){
Pair p = (Pair) o;
return p.y -y;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| 1 | Plagiarised |
c287ea9d | d2901569 | import java.util.*;
import java.io.*;
public class monstersandspells {
public static void main(String args[]) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
1
5
1 5 8 9 10
1 2 6 1 2
*/
int t = in.nextInt();
for ( ; t > 0; t--) {
int n = in.nextInt();
long[] times = new long[n];
long[] health = new long[n];
for (int i = 0; i < n; i++)
times[i] = in.nextLong();
for (int i = 0; i < n; i++)
health[i] = in.nextLong();
long small = 0;
long prev = 0;
long lastDamage = 0;
for (int i = 0; i < n; i++) {
long diff = times[i] - prev;
boolean use = true;
long delta = 0;
//System.out.println(i + " " + times[i] + " " + diff);
if (diff >= health[i]) {
delta += health[i];
long curr = health[i];
long lastTime = times[i];
for (int j = i + 1; j < n; j++) {
long increase = times[j] - lastTime;
//long power = damageOverTime(increase + curr, curr + 1);
if (curr + increase >= health[j]) {
curr += increase;
lastTime = times[j];
}
else {
delta += (health[j] - (curr + increase));
curr = health[j];
lastTime = times[j];
}
}
if (delta <= diff) {
use = true;
}
else
use = false;
}
else
use = false;
if (use) {
//System.out.println(damageOverTime(health[i], 1));
small += damageOverTime(delta, 1);
lastDamage = delta;
}
else {
small += damageOverTime((times[i] - prev) + lastDamage, lastDamage + 1);
lastDamage+=(times[i] - prev);
}
// System.out.println(use + " " + delta + " " + lastDamage + " " + small);
prev = times[i];
}
out.println(small);
}
out.close();
}
public static long damageOverTime(long endPower, long startPower) {
return triangleSum(endPower) - triangleSum(startPower - 1);
}
public static long triangleSum(long a) {
return a * (a + 1) / 2;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws IOException {
if (st.hasMoreTokens())
return st.nextToken();
else
st = new StringTokenizer(br.readLine());
return next();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
| import java.util.*;
import java.util.Scanner;
public class Solution {
static int mod=1000000007;;
//
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println();
Scanner sc=new Scanner(System.in);
int tt=sc.nextInt();
//
//
while(tt-->0){
int n=sc.nextInt();
int k[]=new int[n];
int h[]=new int[n];
for(int i=0;i<n;i++) {
k[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
h[i]=sc.nextInt();
}
long ans=0;
int start=k[0]-h[0]-1;
int end=k[0];
int last=0;
for(int j=0;j<n;j++) {
start=k[j]-h[j]+1;
end=k[j];
last=j;
for(int i=j+1;i<n;i++) {
int temp=k[i]-h[i]+1;
if(temp<=end) {
start=Math.min(start, temp);
end=Math.max(end, k[i]);
last=i;
}
}
j=last;
long va=end-start+1;
ans+=(va*(va+1))/2;
}
System.out.println(ans);
}
}
}
| 0 | Non-plagiarised |
54d7c21e | a7e7f371 |
import java.io.*;
import java.util.*;
public class cp {
static int mod=(int)1e9+7;
// static Reader sc=new Reader();
static FastReader sc=new FastReader(System.in);
static int[] sp;
static int size=(int)1e6;
static int[] arInt;
static long[] arLong;
public static void main(String[] args) throws IOException {
long tc=sc.nextLong();
// Scanner sc=new Scanner(System.in);
// int tc=1;
// primeSet=new HashSet<>();
// sieveOfEratosthenes((int)1e6+5);
while(tc-->0)
{
int n=sc.nextInt();
int k[]=new int[n];
int h[]=new int[n];
for(int i=0;i<n;i++)
k[i]=sc.nextInt();
for(int i=0;i<n;i++)
h[i]=sc.nextInt();
ArrayList<Pair> interval=new ArrayList<Pair>();
ArrayList<Pair> act=new ArrayList<Pair>();
for(int i=0;i<n;i++)
interval.add(new Pair(k[i]-h[i]+1,k[i]));
Collections.sort(interval);
// out.println(interval);
act.add(interval.get(0));
for(int i=1;i<n;i++)
{
Pair p=act.get(act.size()-1);
if(p.y<interval.get(i).x)
act.add(interval.get(i));
else
p.y=Math.max(p.y, interval.get(i).y);
}
// out.println(act);
long mana=0;
for(Pair p: act)
{
long x=p.y-p.x+1;
mana+=(x*(x+1))/2;
}
out.println(mana);
// int n=sc.nextInt();
// long days[]=new long[n];
// long power[]=new long[n];
// for (int i = 0; i < power.length; i++) {
// days[i]=sc.nextLong();
// }
// for (int i = 0; i < power.length; i++) {
// power[i]=sc.nextLong();
//
// }
//
// long ans=0;
// for(int i=0;i<n;i++)
// {
// if(i==0)
// {
// ans+=power[i]*(power[i]+1L)/2L;
// continue;
// }
//
// long temp=power[i]*(power[i]+1)/2L;
// long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L;
// temp2-=power[i-1]*(power[i-1]+1L)/2L;
// ans+=Math.min(temp, temp2);
//// if(days[i]-days[i-1]<=power[i])
//// {
//// ans+=power[i]*(power[i]+1)/2;
//// }
//// else {
//// ans+=power[i]*(power[i]+1)/2;
//// ans-=power[i-1]*(power[i-1]+1)/2;
//// }
//
//
// }
//
// out.println(ans);
}
out.flush();
out.close();
System.gc();
}
/*
...SOLUTION ENDS HERE...........SOLUTION ENDS HERE...
*/
static int util(char a,char b)
{
int A=a-'0';
int B=b-'0';
return A+B;
}
static boolean check(int x,int[] rich,int[] poor)
{
int cnt=0;
for(int i=0;i<rich.length;i++)
{
if(x-1-rich[i]<=cnt && cnt<=poor[i])
cnt++;
}
return cnt>=x;
}
static void arrInt(int n) throws IOException
{
arInt=new int[n];
for (int i = 0; i < arInt.length; i++) {
arInt[i]=sc.nextInt();
}
}
static void arrLong(int n) throws IOException
{
arLong=new long[n];
for (int i = 0; i < arLong.length; i++) {
arLong[i]=sc.nextLong();
}
}
static ArrayList<Integer> add(int id,int c)
{
ArrayList<Integer> newArr=new ArrayList<>();
for(int i=0;i<id;i++)
newArr.add(arInt[i]);
newArr.add(c);
for(int i=id;i<arInt.length;i++)
{
newArr.add(arInt[i]);
}
return newArr;
}
// function to find last index <= y
static int upper(ArrayList<Integer> arr, int n, int y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr.get(mid) <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int lower(ArrayList<Integer> arr, int n, int x)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr.get(mid) >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
static int N = 501;
// Array to store inverse of 1 to N
static long[] factorialNumInverse = new long[N + 1];
// Array to precompute inverse of 1! to N!
static long[] naturalNumInverse = new long[N + 1];
// Array to store factorial of first N numbers
static long[] fact = new long[N + 1];
// Function to precompute inverse of numbers
public static void InverseofNumber(int p)
{
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for(int i = 2; i <= N; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] *
(long)(p - p / i) % p;
}
// Function to precompute inverse of factorials
public static void InverseofFactorial(int p)
{
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// Precompute inverse of natural numbers
for(int i = 2; i <= N; i++)
factorialNumInverse[i] = (naturalNumInverse[i] *
factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to N
public static void factorial(int p)
{
fact[0] = 1;
// Precompute factorials
for(int i = 1; i <= N; i++)
{
fact[i] = (fact[i - 1] * (long)i) % p;
}
}
// Function to return nCr % p in O(1) time
public static long Binomial(int N, int R, int p)
{
// n C r = n!*inverse(r!)*inverse((n-r)!)
long ans = ((fact[N] * factorialNumInverse[R]) %
p * factorialNumInverse[N - R]) % p;
return ans;
}
static String tr(String s)
{
int now = 0;
while (now + 1 < s.length() && s.charAt(now)== '0')
++now;
return s.substring(now);
}
static ArrayList<Integer> ans;
static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp)
{
if(cnt==k)
return;
for(Integer each:gg.list[node])
{
if(each==0)
{
temp.add(each);
ans=new ArrayList<>(temp);
temp.remove(temp.size()-1);
continue;
}
temp.add(each);
dfs(each,gg,cnt+1,k,temp);
temp.remove(temp.size()-1);
}
return;
}
static boolean isPrime(long n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static ArrayList<Integer> commDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Count divisors of n.
ArrayList<Integer> Div=new ArrayList<>();
for (int i = 1; i <= Math.sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
Div.add(i);
else
{
Div.add(i);
Div.add(n/i);
}
}
}
return Div;
}
static HashSet<Integer> factors(int x)
{
HashSet<Integer> a=new HashSet<Integer>();
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
a.add(i);
a.add(x/i);
}
}
return a;
}
static class Node
{
int vertex;
HashSet<Node> adj;
boolean rem;
Node(int ver)
{
vertex=ver;
rem=false;
adj=new HashSet<Node>();
}
@Override
public String toString()
{
return vertex+" ";
}
}
static class Tuple{
int a;
int b;
int c;
public Tuple(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
}
//function to find prime factors of n
static HashMap<Long,Long> findFactors(long n2)
{
HashMap<Long,Long> ans=new HashMap<>();
if(n2%2==0)
{
ans.put(2L, 0L);
// cnt++;
while((n2&1)==0)
{
n2=n2>>1;
ans.put(2L, ans.get(2L)+1);
//
}
}
for(long i=3;i*i<=n2;i+=2)
{
if(n2%i==0)
{
ans.put((long)i, 0L);
// cnt++;
while(n2%i==0)
{
n2=n2/i;
ans.put((long)i, ans.get((long)i)+1);
}
}
}
if(n2!=1)
{
ans.put(n2, ans.getOrDefault(n2, (long) 0)+1);
}
return ans;
}
//fenwick tree implementaion
static class fwt
{
int n;
long BITree[];
fwt(int n)
{
this.n=n;
BITree=new long[n+1];
}
fwt(int arr[], int n)
{
this.n=n;
BITree=new long[n+1];
for(int i = 0; i < n; i++)
updateBIT(n, i, arr[i]);
}
long getSum(int index)
{
long sum = 0;
index = index + 1;
while(index>0)
{
sum += BITree[index];
index -= index & (-index);
}
return sum;
}
void updateBIT(int n, int index,int val)
{
index = index + 1;
while(index <= n)
{
BITree[index] += val;
index += index & (-index);
}
}
void print()
{
for(int i=0;i<n;i++)
out.print(getSum(i)+" ");
out.println();
}
}
class sparseTable{
int n;
long[][]dp;
int log2[];
int P;
void buildTable(long[] arr)
{
n=arr.length;
P=(int)Math.floor(Math.log(n)/Math.log(2));
log2=new int[n+1];
log2[0]=log2[1]=0;
for(int i=2;i<=n;i++)
{
log2[i]=log2[i/2]+1;
}
dp=new long[P+1][n];
for(int i=0;i<n;i++)
{
dp[0][i]=arr[i];
}
for(int p=1;p<=P;p++)
{
for(int i=0;i+(1<<p)<=n;i++)
{
long left=dp[p-1][i];
long right=dp[p-1][i+(1<<(p-1))];
dp[p][i]=Math.max(left, right);
}
}
}
long maxQuery(int l,int r)
{
int len=r-l+1;
int p=(int)Math.floor(log2[len]);
long left=dp[p][l];
long right=dp[p][r-(1<<p)+1];
return Math.max(left, right);
}
}
//Function to find number of set bits
static int setBitNumber(long n)
{
if (n == 0)
return 0;
int msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return msb;
}
static int getFirstSetBitPos(long n)
{
return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1;
}
static ArrayList<Integer> primes;
static HashSet<Integer> primeSet;
static boolean prime[];
static void sieveOfEratosthenes(int n)
{
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
prime= new boolean[n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
primeSet.add(i);
}
}
static long mod(long a, long b) {
long c = a % b;
return (c < 0) ? c + b : c;
}
static void swap(long arr[],int i,int j)
{
long temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
static boolean util(int a,int b,int c)
{
if(b>a)util(b, a, c);
while(c>=a)
{
c-=a;
if(c%b==0)
return true;
}
return (c%b==0);
}
static void flag(boolean flag)
{
out.println(flag ? "YES" : "NO");
out.flush();
}
static void print(int a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print(long a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print_int(ArrayList<Integer> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void print_long(ArrayList<Long> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void printYesNo(boolean condition)
{
if (condition) {
out.println("YES");
}
else {
out.println("NO");
}
}
static int LowerBound(int a[], int x)
{ // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int lowerIndex(int arr[], int n, int x)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
// function to find last index <= y
static int upperIndex(int arr[], int n, int y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int upperIndex(long arr[], int n, long y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int UpperBound(int a[], int x)
{// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static int UpperBound(long a[], long x)
{// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static class DisjointUnionSets
{
int[] rank, parent;
int n;
// Constructor
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
// Creates n sets with single item in each
void makeSet()
{
for (int i = 0; i < n; i++)
parent[i] = i;
}
int find(int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
// Unites the set that includes x and the set
// that includes x
void union(int x, int y)
{
int xRoot = find(x), yRoot = find(y);
if (xRoot == yRoot)
return;
if (rank[xRoot] < rank[yRoot])
parent[xRoot] = yRoot;
else if (rank[yRoot] < rank[xRoot])
parent[yRoot] = xRoot;
else // if ranks are the same
{
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + 1;
}
// if(xRoot!=yRoot)
// parent[y]=x;
}
int connectedComponents()
{
int cnt=0;
for(int i=0;i<n;i++)
{
if(parent[i]==i)
cnt++;
}
return cnt;
}
}
static class Graph
{
int v;
ArrayList<Integer> list[];
Graph(int v)
{
this.v=v;
list=new ArrayList[v+1];
for(int i=1;i<=v;i++)
list[i]=new ArrayList<Integer>();
}
void addEdge(int a, int b)
{
this.list[a].add(b);
}
}
// static class GraphMap{
// Map<String,ArrayList<String>> graph;
// GraphMap() {
// // TODO Auto-generated constructor stub
// graph=new HashMap<String,ArrayList<String>>();
//
// }
// void addEdge(String a,String b)
// {
// if(graph.containsKey(a))
// this.graph.get(a).add(b);
// else {
// this.graph.put(a, new ArrayList<>());
// this.graph.get(a).add(b);
// }
// }
// }
// static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok)
// {
// vis.add(src);
//
// if(g.graph.get(src)!=null)
// {
// for(String each:g.graph.get(src))
// {
// if(!vis.contains(each))
// {
// dfsMap(g, vis, each, ok+1);
// }
// }
// }
//
// cnt=Math.max(cnt, ok);
// }
static double sum[];
static long cnt;
// static void DFS(Graph g, boolean[] visited, int u)
// {
// visited[u]=true;
//
// for(int i=0;i<g.list[u].size();i++)
// {
// int v=g.list[u].get(i);
//
// if(!visited[v])
// {
// cnt1=cnt1*2;
// DFS(g, visited, v);
//
// }
//
// }
//
//
// }
static class Pair implements Comparable<Pair>
{
int x;
int y;
Pair(int x,int y)
{
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return this.x-o.x;
}
}
static long sum_array(int a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static long sum_array(long a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static void sort(int[] a)
{
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a)
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void reverse_array(int a[])
{
int n=a.length;
int i,t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static void reverse_array(long a[])
{
int n=a.length;
int i; long t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// static long modInverse(long a, long m)
// {
// long g = gcd(a, m);
//
// return power(a, m - 2, m);
//
// }
static long power(long x, long y)
{
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1; // y = y/2
x = (x * x) % mod;
}
return res;
}
static int power(int x, int y)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1; // y = y/2
x = (x * x) % mod;
}
return res;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
//cnt+=a/b;
return gcd(b%a,a);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static 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();
}
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;
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static PrintWriter out=new PrintWriter(System.out);
static int int_max=Integer.MAX_VALUE;
static int int_min=Integer.MIN_VALUE;
static long long_max=Long.MAX_VALUE;
static long long_min=Long.MIN_VALUE;
}
|
import java.io.*;
import java.util.*;
public class cp {
static int mod=(int)1e9+7;
// static Reader sc=new Reader();
static FastReader sc=new FastReader(System.in);
static int[] sp;
static int size=(int)1e6;
static int[] arInt;
static long[] arLong;
public static void main(String[] args) throws IOException {
long tc=sc.nextLong();
// Scanner sc=new Scanner(System.in);
// int tc=1;
// primeSet=new HashSet<>();
// sieveOfEratosthenes((int)1e6+5);
while(tc-->0)
{
int n=sc.nextInt();
int k[]=new int[n];
int h[]=new int[n];
for(int i=0;i<n;i++)
k[i]=sc.nextInt();
for(int i=0;i<n;i++)
h[i]=sc.nextInt();
ArrayList<Pair> interval=new ArrayList<Pair>();
ArrayList<Pair> act=new ArrayList<Pair>();
for(int i=0;i<n;i++)
interval.add(new Pair(k[i]-h[i]+1,k[i]));
Collections.sort(interval);
// out.println(interval);
act.add(interval.get(0));
for(int i=1;i<n;i++)
{
Pair p=act.get(act.size()-1);
if(p.y<interval.get(i).x)
act.add(interval.get(i));
else
p.y=Math.max(p.y, interval.get(i).y);
}
// out.println(act);
long mana=0;
for(Pair p: act)
{
long x=p.y-p.x+1;
mana+=(x*(x+1))/2;
}
out.println(mana);
// int n=sc.nextInt();
// long days[]=new long[n];
// long power[]=new long[n];
// for (int i = 0; i < power.length; i++) {
// days[i]=sc.nextLong();
// }
// for (int i = 0; i < power.length; i++) {
// power[i]=sc.nextLong();
//
// }
//
// long ans=0;
// for(int i=0;i<n;i++)
// {
// if(i==0)
// {
// ans+=power[i]*(power[i]+1L)/2L;
// continue;
// }
//
// long temp=power[i]*(power[i]+1)/2L;
// long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L;
// temp2-=power[i-1]*(power[i-1]+1L)/2L;
// ans+=Math.min(temp, temp2);
//// if(days[i]-days[i-1]<=power[i])
//// {
//// ans+=power[i]*(power[i]+1)/2;
//// }
//// else {
//// ans+=power[i]*(power[i]+1)/2;
//// ans-=power[i-1]*(power[i-1]+1)/2;
//// }
//
//
// }
//
// out.println(ans);
}
out.flush();
out.close();
System.gc();
}
/*
...SOLUTION ENDS HERE...........SOLUTION ENDS HERE...
*/
static int util(char a,char b)
{
int A=a-'0';
int B=b-'0';
return A+B;
}
static boolean check(int x,int[] rich,int[] poor)
{
int cnt=0;
for(int i=0;i<rich.length;i++)
{
if(x-1-rich[i]<=cnt && cnt<=poor[i])
cnt++;
}
return cnt>=x;
}
static void arrInt(int n) throws IOException
{
arInt=new int[n];
for (int i = 0; i < arInt.length; i++) {
arInt[i]=sc.nextInt();
}
}
static void arrLong(int n) throws IOException
{
arLong=new long[n];
for (int i = 0; i < arLong.length; i++) {
arLong[i]=sc.nextLong();
}
}
static ArrayList<Integer> add(int id,int c)
{
ArrayList<Integer> newArr=new ArrayList<>();
for(int i=0;i<id;i++)
newArr.add(arInt[i]);
newArr.add(c);
for(int i=id;i<arInt.length;i++)
{
newArr.add(arInt[i]);
}
return newArr;
}
// function to find last index <= y
static int upper(ArrayList<Integer> arr, int n, int y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr.get(mid) <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int lower(ArrayList<Integer> arr, int n, int x)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr.get(mid) >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
static int N = 501;
// Array to store inverse of 1 to N
static long[] factorialNumInverse = new long[N + 1];
// Array to precompute inverse of 1! to N!
static long[] naturalNumInverse = new long[N + 1];
// Array to store factorial of first N numbers
static long[] fact = new long[N + 1];
// Function to precompute inverse of numbers
public static void InverseofNumber(int p)
{
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for(int i = 2; i <= N; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] *
(long)(p - p / i) % p;
}
// Function to precompute inverse of factorials
public static void InverseofFactorial(int p)
{
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// Precompute inverse of natural numbers
for(int i = 2; i <= N; i++)
factorialNumInverse[i] = (naturalNumInverse[i] *
factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to N
public static void factorial(int p)
{
fact[0] = 1;
// Precompute factorials
for(int i = 1; i <= N; i++)
{
fact[i] = (fact[i - 1] * (long)i) % p;
}
}
// Function to return nCr % p in O(1) time
public static long Binomial(int N, int R, int p)
{
// n C r = n!*inverse(r!)*inverse((n-r)!)
long ans = ((fact[N] * factorialNumInverse[R]) %
p * factorialNumInverse[N - R]) % p;
return ans;
}
static String tr(String s)
{
int now = 0;
while (now + 1 < s.length() && s.charAt(now)== '0')
++now;
return s.substring(now);
}
static ArrayList<Integer> ans;
static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp)
{
if(cnt==k)
return;
for(Integer each:gg.list[node])
{
if(each==0)
{
temp.add(each);
ans=new ArrayList<>(temp);
temp.remove(temp.size()-1);
continue;
}
temp.add(each);
dfs(each,gg,cnt+1,k,temp);
temp.remove(temp.size()-1);
}
return;
}
static boolean isPrime(long n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static ArrayList<Integer> commDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Count divisors of n.
ArrayList<Integer> Div=new ArrayList<>();
for (int i = 1; i <= Math.sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
Div.add(i);
else
{
Div.add(i);
Div.add(n/i);
}
}
}
return Div;
}
static HashSet<Integer> factors(int x)
{
HashSet<Integer> a=new HashSet<Integer>();
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
a.add(i);
a.add(x/i);
}
}
return a;
}
static class Node
{
int vertex;
HashSet<Node> adj;
boolean rem;
Node(int ver)
{
vertex=ver;
rem=false;
adj=new HashSet<Node>();
}
@Override
public String toString()
{
return vertex+" ";
}
}
static class Tuple{
int a;
int b;
int c;
public Tuple(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
}
//function to find prime factors of n
static HashMap<Long,Long> findFactors(long n2)
{
HashMap<Long,Long> ans=new HashMap<>();
if(n2%2==0)
{
ans.put(2L, 0L);
// cnt++;
while((n2&1)==0)
{
n2=n2>>1;
ans.put(2L, ans.get(2L)+1);
//
}
}
for(long i=3;i*i<=n2;i+=2)
{
if(n2%i==0)
{
ans.put((long)i, 0L);
// cnt++;
while(n2%i==0)
{
n2=n2/i;
ans.put((long)i, ans.get((long)i)+1);
}
}
}
if(n2!=1)
{
ans.put(n2, ans.getOrDefault(n2, (long) 0)+1);
}
return ans;
}
//fenwick tree implementaion
static class fwt
{
int n;
long BITree[];
fwt(int n)
{
this.n=n;
BITree=new long[n+1];
}
fwt(int arr[], int n)
{
this.n=n;
BITree=new long[n+1];
for(int i = 0; i < n; i++)
updateBIT(n, i, arr[i]);
}
long getSum(int index)
{
long sum = 0;
index = index + 1;
while(index>0)
{
sum += BITree[index];
index -= index & (-index);
}
return sum;
}
void updateBIT(int n, int index,int val)
{
index = index + 1;
while(index <= n)
{
BITree[index] += val;
index += index & (-index);
}
}
void print()
{
for(int i=0;i<n;i++)
out.print(getSum(i)+" ");
out.println();
}
}
class sparseTable{
int n;
long[][]dp;
int log2[];
int P;
void buildTable(long[] arr)
{
n=arr.length;
P=(int)Math.floor(Math.log(n)/Math.log(2));
log2=new int[n+1];
log2[0]=log2[1]=0;
for(int i=2;i<=n;i++)
{
log2[i]=log2[i/2]+1;
}
dp=new long[P+1][n];
for(int i=0;i<n;i++)
{
dp[0][i]=arr[i];
}
for(int p=1;p<=P;p++)
{
for(int i=0;i+(1<<p)<=n;i++)
{
long left=dp[p-1][i];
long right=dp[p-1][i+(1<<(p-1))];
dp[p][i]=Math.max(left, right);
}
}
}
long maxQuery(int l,int r)
{
int len=r-l+1;
int p=(int)Math.floor(log2[len]);
long left=dp[p][l];
long right=dp[p][r-(1<<p)+1];
return Math.max(left, right);
}
}
//Function to find number of set bits
static int setBitNumber(long n)
{
if (n == 0)
return 0;
int msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return msb;
}
static int getFirstSetBitPos(long n)
{
return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1;
}
static ArrayList<Integer> primes;
static HashSet<Integer> primeSet;
static boolean prime[];
static void sieveOfEratosthenes(int n)
{
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
prime= new boolean[n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
primeSet.add(i);
}
}
static long mod(long a, long b) {
long c = a % b;
return (c < 0) ? c + b : c;
}
static void swap(long arr[],int i,int j)
{
long temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
static boolean util(int a,int b,int c)
{
if(b>a)util(b, a, c);
while(c>=a)
{
c-=a;
if(c%b==0)
return true;
}
return (c%b==0);
}
static void flag(boolean flag)
{
out.println(flag ? "YES" : "NO");
out.flush();
}
static void print(int a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print(long a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print_int(ArrayList<Integer> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void print_long(ArrayList<Long> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void printYesNo(boolean condition)
{
if (condition) {
out.println("YES");
}
else {
out.println("NO");
}
}
static int LowerBound(int a[], int x)
{ // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int lowerIndex(int arr[], int n, int x)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
// function to find last index <= y
static int upperIndex(int arr[], int n, int y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int upperIndex(long arr[], int n, long y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int UpperBound(int a[], int x)
{// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static int UpperBound(long a[], long x)
{// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static class DisjointUnionSets
{
int[] rank, parent;
int n;
// Constructor
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
// Creates n sets with single item in each
void makeSet()
{
for (int i = 0; i < n; i++)
parent[i] = i;
}
int find(int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
// Unites the set that includes x and the set
// that includes x
void union(int x, int y)
{
int xRoot = find(x), yRoot = find(y);
if (xRoot == yRoot)
return;
if (rank[xRoot] < rank[yRoot])
parent[xRoot] = yRoot;
else if (rank[yRoot] < rank[xRoot])
parent[yRoot] = xRoot;
else // if ranks are the same
{
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + 1;
}
// if(xRoot!=yRoot)
// parent[y]=x;
}
int connectedComponents()
{
int cnt=0;
for(int i=0;i<n;i++)
{
if(parent[i]==i)
cnt++;
}
return cnt;
}
}
static class Graph
{
int v;
ArrayList<Integer> list[];
Graph(int v)
{
this.v=v;
list=new ArrayList[v+1];
for(int i=1;i<=v;i++)
list[i]=new ArrayList<Integer>();
}
void addEdge(int a, int b)
{
this.list[a].add(b);
}
}
// static class GraphMap{
// Map<String,ArrayList<String>> graph;
// GraphMap() {
// // TODO Auto-generated constructor stub
// graph=new HashMap<String,ArrayList<String>>();
//
// }
// void addEdge(String a,String b)
// {
// if(graph.containsKey(a))
// this.graph.get(a).add(b);
// else {
// this.graph.put(a, new ArrayList<>());
// this.graph.get(a).add(b);
// }
// }
// }
// static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok)
// {
// vis.add(src);
//
// if(g.graph.get(src)!=null)
// {
// for(String each:g.graph.get(src))
// {
// if(!vis.contains(each))
// {
// dfsMap(g, vis, each, ok+1);
// }
// }
// }
//
// cnt=Math.max(cnt, ok);
// }
static double sum[];
static long cnt;
// static void DFS(Graph g, boolean[] visited, int u)
// {
// visited[u]=true;
//
// for(int i=0;i<g.list[u].size();i++)
// {
// int v=g.list[u].get(i);
//
// if(!visited[v])
// {
// cnt1=cnt1*2;
// DFS(g, visited, v);
//
// }
//
// }
//
//
// }
static class Pair implements Comparable<Pair>
{
int x;
int y;
Pair(int x,int y)
{
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return this.x-o.x;
}
}
static long sum_array(int a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static long sum_array(long a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static void sort(int[] a)
{
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a)
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void reverse_array(int a[])
{
int n=a.length;
int i,t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static void reverse_array(long a[])
{
int n=a.length;
int i; long t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// static long modInverse(long a, long m)
// {
// long g = gcd(a, m);
//
// return power(a, m - 2, m);
//
// }
static long power(long x, long y)
{
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1; // y = y/2
x = (x * x) % mod;
}
return res;
}
static int power(int x, int y)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1; // y = y/2
x = (x * x) % mod;
}
return res;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
//cnt+=a/b;
return gcd(b%a,a);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static 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();
}
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;
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static PrintWriter out=new PrintWriter(System.out);
static int int_max=Integer.MAX_VALUE;
static int int_min=Integer.MIN_VALUE;
static long long_max=Long.MAX_VALUE;
static long long_min=Long.MIN_VALUE;
}
| 1 | Plagiarised |
4548305b | c0aa6246 | import java.util.function.Consumer;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.io.*;
import java.lang.Math.*;
public class KickStart2020{
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());}
float nextFloat() {return Float.parseFloat(next());}
String nextLine() {
String str = "";
try {str = br.readLine();}
catch (IOException e) { e.printStackTrace();}
return str; }}
static boolean isBracketSequence(String s, int a, int b) {
Stack<Character> ss = new Stack<>();
boolean hachu = true;
for(int i = a; i <= b; i++) {
if(s.charAt(i) == ')' && ss.isEmpty()) {hachu = false; break;}
if(s.charAt(i) == '(') ss.add('(');
else ss.pop();
}
return ss.empty() && hachu;
}
static String reverseOfString(String a) {
StringBuilder ssd = new StringBuilder();
for(int i = a.length() - 1; i >= 0; i--) {
ssd.append(a.charAt(i));
}
return ssd.toString();
}
static char[] reverseOfChar(char a[]) {
char b[] = new char[a.length];
int j = 0;
for(int i = a.length - 1; i >= 0; i--) {
b[i] = a[j];
j++;
}
return b;
}
static boolean isPalindrome(char a[]) {
boolean hachu = true;
for(int i = 0; i <= a.length / 2; i++) {
if(a[i] != a[a.length - 1 - i]) {
hachu = false;
break;
}
}
return hachu;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long powermod(long x, long y, long mod){
long ans = 1;
x = x % mod;
if (x == 0)
return 0;
int i = 1;
while (y > 0){
if ((y & 1) != 0)
ans = (ans * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return ans;
}
static long power(long x, long y){
long ans = 1;
if (x == 0)
return 0;
int i = 1;
while (y > 0){
if ((y & 1) != 0)
ans = (ans * x);
y = y >> 1;
x = (x * x);
}
return ans;
}
static boolean check(String a) {
boolean hachu = true;
for(int i = 0; i < a.length(); i++) {
if(a.charAt(0) != a.charAt(i)) {hachu = false; break;}
}
return hachu;
}
public static class Pair implements Comparable<Pair> {
public int index;
public int value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair other) {
return -1 * Integer.valueOf(this.value).compareTo(other.value);
}
}
static boolean equalString(int i, int j, int arr[], String b) {
int brr[] = new int[26];
for(int k = i; k <= j; k++) brr[b.charAt(k) - 'a']++;
for(int k = 0; k < 26; k++) {
if(arr[k] != brr[k]) return false;
}
return true;
}
static boolean cequalArray(String a, String b) {
int count[] = new int[26];
int count1[] = new int[26];
for(int i = 0; i < a.length(); i++) count[a.charAt(i) - 'a']++;
for(int i = 0; i < a.length(); i++) count1[b.charAt(i) - 'a']++;
for(int i = 0; i < 26; i++) if(count[i] != count1[i]) return false;
return true;
}
static boolean isPrime(long d) {
if(d == 1) return true;
for(int i = 2; i <= (long)Math.sqrt(d); i++) {
if(d % i == 0) return false;
}
return true;
}
public static void main(String[] args) throws Exception{
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[k];
int temp[] = new int[k];
for(int i = 0; i < k; i++) arr[i] = sc.nextInt();
for(int i = 0; i < k; i++) temp[i] = sc.nextInt();
long brr[] = new long[n];
Arrays.fill(brr, Integer.MAX_VALUE);
for(int i = 0; i < k; i++) brr[arr[i] - 1] = temp[i];
for(int i = 1; i < n; i++) {
brr[i] = Math.min(brr[i], brr[i - 1] + 1);
}
for(int i = n - 2; i >= 0; i--) {
brr[i] = Math.min(brr[i], brr[i + 1] + 1);
}
for(long e: brr) out.print(e + " ");
out.println();
}
out.close();
}
} |
import java.util.*;
import java.util.Map.Entry;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
public class CF {
private static FS sc = new FS();
private static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
private static class extra {
static int[] intArr(int size) {
int[] a = new int[size];
for(int i = 0; i < size; i++) a[i] = sc.nextInt();
return a;
}
static long[] longArr(int size) {
long[] a = new long[size];
for(int i = 0; i < size; i++) a[i] = sc.nextLong();
return a;
}
static long intSum(int[] a) {
long sum = 0;
for(int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
static long longSum(long[] a) {
long sum = 0;
for(int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
static LinkedList[] graphD(int vertices, int edges) {
LinkedList<Integer>[] temp = new LinkedList[vertices+1];
for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
for(int i = 0; i < edges; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
temp[x].add(y);
}
return temp;
}
static LinkedList[] graphUD(int vertices, int edges) {
LinkedList<Integer>[] temp = new LinkedList[vertices+1];
for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
for(int i = 0; i < edges; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
temp[x].add(y);
temp[y].add(x);
}
return temp;
}
static void printG(LinkedList[] temp) {
for(LinkedList<Integer> aa:temp) System.out.println(aa);
}
static long cal(long val, long pow) {
if(pow == 0) return 1;
long res = cal(val, pow/2);
// long ret = (res*res)%mod;
// if(pow%2 == 0) return ret;
// return (val*ret)%mod;
long ret = res*res;
if(pow%2 == 0) return ret;
return val*ret;
}
static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); }
}
static int mod = (int) 1e9 + 9;
// static int mod = (int) 998244353;
static int max = (int) 1e6, sq = 316;
static LinkedList<Integer>[] temp;
// static int[] par, rank;
public static void main(String[] args) {
int t = sc.nextInt();
// int t = 1;
StringBuilder ret = new StringBuilder();
while(t-- > 0) {
int n = sc.nextInt(); int m = sc.nextInt();
int[] a = extra.intArr(m);
int[] b = extra.intArr(m);
long[] c = new long[n];
Arrays.fill(c, (int)1e18);
for(int i = 0; i < m; i++) c[a[i]-1] = b[i];
long[] l = new long[n];
long[] r = new long[n];
Arrays.fill(l, (int)1e18);
Arrays.fill(r, (int)1e18);
long min = (long)1e18;
for(int i = 0; i < n; i++) {
min = Math.min(min+1, c[i]);
l[i] = min;
}
min = (int)1e18;
for(int i = n-1; i >= 0; i--) {
min = Math.min(min+1, c[i]);
r[i] = min;
}
for(int i = 0; i < n; i++) {
ret.append(Math.min(l[i], r[i]) + " ");
}
ret.append("\n");
}
System.out.println(ret);
}
} | 0 | Non-plagiarised |
b434c275 | f8c99dd0 | import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
public final class A
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<Integer> g[];
static long mod=(long)1e9+7,INF=Long.MAX_VALUE;
static boolean set[],col[];
static int par[],tot[],partial[];
static int D[],P[][];
static long dp[][],sum=0,max=0,size[];
// static node1 seg[];
//static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)};
public static void main(String args[])throws IOException
{
int T=i();
outer:while(T-->0)
{
int N=i();
int size[]=new int[N];
PriorityQueue<node1> q[]=new PriorityQueue[26];
for(int i=0; i<26; i++)q[i]=new PriorityQueue<node1>();
for(int i=0; i<N; i++)
{
char X[]=in.next().toCharArray();
int s=X.length;
size[i]=s;
int f[]=new int[26];
for(char x:X)f[x-'a']++;
for(int j=0; j<26; j++)q[j].add(new node1(f[j],i,s));
}
int max=0;
for(int i=0; i<26; i++)
{
PriorityQueue<node1> q_new=new PriorityQueue<>();
q_new=q[i];
int c=0;
long f=0;
while(q_new.size()>0)
{
node1 x=q_new.remove();
// System.out.println(x.f+" "+x.size+" "+x.a);
f+=x.a;
if(f>0)
{
c++;
max=Math.max(max, c);
}
else break;
}
}
out.println(max);
}
out.close();
}
static int OR(int i,int j)
{
if(i>j)
{
int t=i;
i=j;
j=t;
}
System.out.println("OR "+i+" "+j);
return i();
}
static int AND(int i,int j)
{
if(i>j)
{
int t=i;
i=j;
j=t;
}
System.out.println("AND "+i+" "+j);
return i();
}
static int XOR(int i,int j)
{
if(i>j)
{
int t=i;
i=j;
j=t;
}
System.out.println("XOR "+i+" "+j);
return i();
}
static boolean f1(int l,char X[])
{
int i=0;
for(; l<X.length; l++)
{
if(X[i]!=X[l])return false;
i++;
}
return true;
}
static int f(int a)
{
for(int i=a+1; a>0; a++)
{
if(GCD(i,a)==1)return i;
}
return 0;
}
static int min(char X[],char str[],int N)
{
int s=0;
for(int i=0; i<N; i++)
{
int it=i%3;
if(X[i]!=str[it])s++;
// ans.append(str[it]);
}
return s;
}
static char f(int i,char X[])
{
int a=0,b=0,c=0;
for(; i<X.length; i+=3)
{
if(X[i]=='R')a++;
if(X[i]=='B')b++;
if(X[i]=='G')c++;
}
if(a>=b && a>=c)return 'R';
if(b>=a && b>=c)return 'B';
return 'G';
}
static void f1(int n,int p,long sum,int N)
{
for(int c:g[n])
{
if(c==p)continue;
long s=sum+N-2*size[c];
f1(c,n,s,N);
max=Math.max(max, s);
}
}
static long f(int i,int j,ArrayList<Integer> A)
{
if(i+1==A.size())
{
return j;
}
int moves=1+A.get(i);
if(j==1)return 1+f(i+1,moves,A);
if(j>0 && dp[i][j-1]==0)f(i,j-1,A);
return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A);
}
// static void build(int v,int tl,int tr,long A[])
// {
// if(tl==tr)
// {
// seg[v]=new node1(A[tl],A[tr],1,true);
// return ;
// }
// int tm=(tl+tr)/2;
// build(2*v,tl,tm,A);
// build(2*v+1,tm+1,tr,A);
// seg[v]=merge(seg[2*v],seg[2*v+1]);
// }
// static node1 ask(int v,int tl,int tr,int l,int r)
// {
// if(l>r)return new node1(0,0,0,false);//verify true or false
// if(tl==l && tr==r)return seg[v];
// int tm=(tl+tr)/2;
// node1 a=ask(v*2,tl,tm,l,Math.min(tm, r));
// node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r);
// return merge(a,b);
// }
// static node1 merge(node1 a,node1 b)
// {
// long s=0;
// long l1=a.L,r1=a.R,c1=a.cnt;
// long l2=b.L,r2=b.R,c2=b.cnt;
// long g=GCD(l2,r1); s=c1+c2;
// if(g==1)
// {
// s--;
// g=(l2*r1)/g;
// if(c1==1)
// {
// l1=g;
// }
// if(c2==1)r2=g;
// return new node1(l1,r2,s,true);
// }
// return new node1(l1,r2,s,a.leaf^b.leaf);
// }
static long f(long l,long r,long a,long b,long N)
{
while(r-l>1)
{
long m=(l+r)/2;
long x=m*b;
if(N<x)
{
r=m;
continue;
}
if((N-x)%a==0)r=m;
else l=m;
}
return r;
}
static long f1(long a,long A[],long bits[],long sum)
{
long s=A.length;
s=mul(s,a);
s=(s+(sum%mod))%mod;
long p=1L;
for(long x:bits)
{
if((a&p)!=0)s=((s-mul(x,p))+mod)%mod;
p<<=1;
}
return s;
}
static long f2(long a,long A[],long bits[])
{
long s=0;
long p=1L;
for(long x:bits)
{
if((a&p)!=0)
{
s=(s+mul(p,x))%mod;
}
p<<=1;
}
return s;
}
static long f(long x1,long y1,long x2,long y2)
{
return Math.abs(x1-x2)+Math.abs(y1-y2);
}
static long f(long x,long max,long s)
{
long l=-1,r=(x/s)+1;
while(r-l>1)
{
long m=(l+r)/2;
if(x-m*s>max)l=m;
else r=m;
}
return l+1;
}
static int f(long A[],long x)
{
int l=-1,r=A.length;
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]>=x)r=m;
else l=m;
}
return r;
}
static int bin(int x,ArrayList<Integer> A)
{
int l=0,r=A.size()-1;
while(l<=r)
{
int m=(l+r)/2;
int a=A.get(m);
if(a==x)return m;
if(a<x)l=m+1;
else r=m-1;
}
return 0;
}
static int left(int x,ArrayList<Integer> A)
{
int l=-1,r=A.size();
while(r-l>1)
{
int m=(l+r)/2;
int a=A.get(m);
if(a<=x)l=m;
else r=m;
}
return l;
}
static int right(int x,ArrayList<Integer> A)
{
int l=-1,r=A.size();
while(r-l>1)
{
int m=(l+r)/2;
int a=A.get(m);
if(a<x)l=m;
else r=m;
}
return r;
}
static boolean equal(long A[],long B[])
{
for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false;
return true;
}
static int max(int a ,int b,int c,int d)
{
a=Math.max(a, b);
c=Math.max(c,d);
return Math.max(a, c);
}
static int min(int a ,int b,int c,int d)
{
a=Math.min(a, b);
c=Math.min(c,d);
return Math.min(a, c);
}
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static long mul(long a, long b)
{
return ( a %mod * 1L * b%mod )%mod;
}
static void swap(int A[],int a,int b)
{
int t=A[a];
A[a]=A[b];
A[b]=t;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
par[a]+=par[b];
par[b]=a;
}
}
static boolean isSorted(int A[])
{
for(int i=1; i<A.length; i++)
{
if(A[i]<A[i-1])return false;
}
return true;
}
static boolean isDivisible(StringBuilder X,int i,long num)
{
long r=0;
for(; i<X.length(); i++)
{
r=r*10+(X.charAt(i)-'0');
r=r%num;
}
return r==0;
}
static int lower_Bound(int A[],int low,int high, int x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static String f(String A)
{
String X="";
for(int i=A.length()-1; i>=0; i--)
{
int c=A.charAt(i)-'0';
X+=(c+1)%2;
}
return X;
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static int sD(long n)
{
if (n % 2 == 0 )
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0 )
return i;
}
return (int)n;
}
static void setGraph(int N)
{
tot=new int[N+1];
partial=new int[N+1];
D=new int[N+1];
P=new int[N+1][(int)(Math.log(N)+10)];
set=new boolean[N+1];
g=new ArrayList[N+1];
for(int i=0; i<=N; i++)
{
g[i]=new ArrayList<>();
D[i]=Integer.MAX_VALUE;
//D2[i]=INF;
}
}
static long pow(long a,long b)
{
//long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static int kadane(int A[])
{
int lsum=A[0],gsum=A[0];
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
class node1 implements Comparable<node1>
{
int index,f,size;
long a;
node1(int f,int i,int size)
{
this.f=f;
this.index=i;
this.size=size;
a=2*f-size;
}
public int compareTo(node1 x)
{
if(this.a==x.a)return 0;
else if(this.a<x.a)return 1;
else return -1;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
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;
}
}
| import java.util.*;
import java.io.*;
public class Solution {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void solve() throws IOException {
int n = Integer.parseInt(br.readLine());
ArrayList<int[]> words = new ArrayList<>();
ArrayList<Integer> lens = new ArrayList<>();
//count [a,b,c,d,e]
for (int i = 0; i < n; i++) {
String temp = br.readLine();
int[] word = new int[5];
for (int j = 0; j < temp.length(); j++) {
word[(int)(temp.charAt(j)) - 97]++;
}
words.add(word);
lens.add(temp.length());
}
int ans = 0;
for (int i = 0; i < 5; i++) {
ArrayList<Integer> sums = new ArrayList<>();
//target occ - other occ
for (int j = 0; j < words.size(); j++) {
sums.add(words.get(j)[i] - (lens.get(j) - words.get(j)[i]));
}
Collections.sort(sums);
int loc = 0;
int run = 0;
for (int j = sums.size() - 1; j >= 0; j--) {
if(run + sums.get(j) > 0) {
loc++;
run += sums.get(j);
}
else break;
}
ans = Math.max(ans, loc);
}
System.out.println(ans);
}
public static void main(String[] args) throws IOException {
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
solve();
}
}
}
| 0 | Non-plagiarised |
0df4050e | f5fde094 | import java.io.*;
import java.util.*;
public class MainClass {
public static void main(String[] args) {
Reader in = new Reader(System.in);
int t = in.nextInt();
StringBuilder stringBuilder = new StringBuilder();
while (t-- > 0) {
ArrayList<Integer> reds = new ArrayList<>();
ArrayList<Integer> blue = new ArrayList<>();
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt() - 1;
}
char[] s = in.next().toCharArray();
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
reds.add(a[i]);
} else {
blue.add(a[i]);
}
}
Collections.sort(reds, Collections.reverseOrder());
Collections.sort(blue);
boolean ff = true;
int start = 0;
for (int i = 0; i < blue.size(); i++) {
if (blue.get(i) < start) {
ff = false;
break;
}
start++;
}
start = n - 1;
for (int i = 0; i < reds.size(); i++) {
if (reds.get(i) > start) {
ff = false;
break;
}
start--;
}
stringBuilder.append(ff?"YES":"NO").append("\n");
}
System.out.println(stringBuilder);
}
}
class Reader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Reader(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());
}
} | //package eround101;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.StringTokenizer;
public class Solution {
static HritikScanner sc = new HritikScanner();
static PrintWriter pw = new PrintWriter(System.out, true);
final static int MOD = 1000000007;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
int t = ni();
while (t-- > 0) {
solve();
}
}
static void solve() {
int n = ni();
int[] arr = nextIntArray(n);
char[] col = sc.next().toCharArray();
int[] cB = new int[n+1];
int[] cR = new int[n+1];
for(int i = 0; i < n; i++)
{
// pl((col[i] == 'R' && arr[i] > n));
// pl((col[i] == 'B' && arr[i] < 1));
if((col[i] == 'R' && arr[i] > n)||(col[i] == 'B' && arr[i] < 1))
{
pl("NO");
return;
}
if(col[i] == 'B')
{
if(arr[i] > n)
continue;
cB[arr[i]]++;
if(cB[arr[i]] > arr[i])
{
pl("NO");
return;
}
}
else
{
if(arr[i] < 1)
continue;
cR[arr[i]]++;
if(cR[arr[i]] > (n-(arr[i]-1)))
{
pl("NO");
return;
}
}
// pa(cB);
// pa(cR);
}
// pa(cB);
// pa(cR);
int[] psum = new int[n+1];
for(int i = 1; i<= n; i++)
{
psum[i] = psum[i-1]+cB[i];
if(psum[i] > i)
{
pl("NO");
return;
}
}
// pa(psum);
int[] psum1 = new int[n+1];
psum1[n] = cR[n];
for(int i = n-1; i>= 0; i--)
{
psum1[i] = psum1[i+1]+cR[i];
if(psum1[i] > (n-(i-1)))
{
pl("NO");
return;
}
}
// pa(psum1);
pl("YES");
}
/////////////////////////////////////////////////////////////////////////////////
static class FenwickTree { // Binary Index Tree
int[] tree;
static int size;
public FenwickTree(int size) {
this.size = size;
tree = new int[size + 5];
}
public void add(int i, int val) {
while (i <= size) {
tree[i] += val;
i += i & -i; // adding the decimal value of the last set bit.
}
}
public int sum(int i) {
int res = 0;
while (i >= 1) {
res += tree[i];
i -= i & -i; // deleting the last set bit
}
return res;
}
public int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
}
/////////////////////////////////////////////////////////////////////////////////
static int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = ni();
}
return arr;
}
static long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nl();
}
return arr;
}
static int[] nextIntArray1(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = ni();
}
return arr;
}
/////////////////////////////////////////////////////////////////////////////////
static int ni() {
return sc.nextInt();
}
static long nl() {
return sc.nextLong();
}
static double nd() {
return sc.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(Object arr[]) {
for (Object o : arr) {
p(o);
}
pl();
}
static void pa(int arr[]) {
for (int o : arr) {
p(o);
}
pl();
}
static void pa(long arr[]) {
for (long o : arr) {
p(o);
}
pl();
}
static void pa(double arr[]) {
for (double o : arr) {
p(o);
}
pl();
}
static void pa(char arr[]) {
for (char o : arr) {
p(o);
}
pl();
}
static void pa(List list) {
for (Object o : list) {
p(o);
}
pl();
}
static void pa(Object[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (Object o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(int[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (int o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(long[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (long o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(char[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (char o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(double[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (double o : arr[i]) {
p(o);
}
pl();
}
}
/////////////////////////////////////////////////////////////////////////////////
static void print(Object s) {
System.out.println(s);
}
/////////////////////////////////////////////////////////////////////////////////
//-----------HritikScanner class for faster input----------//
static class HritikScanner {
BufferedReader br;
StringTokenizer st;
public HritikScanner() {
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 class Pair implements Comparable<Pair> {
int num, row, col;
Pair(int num, int row, int col) {
this.num = num;
this.row = row;
this.col = col;
}
public int get1() {
return num;
}
public int get2() {
return row;
}
public int compareTo(Pair A) {
return this.num - A.num;
}
public String toString() {
return num + " " + row + " " + col;
}
}
//////////////////////////////////////////////////////////////////
// Function to return gcd of a and b time complexity O(log(a+b))
static long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// method to return LCM of two numbers
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
//////////////////////////////////////////////////////////////////
static boolean isPrime(long n) {
// Corner cases
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (long i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////
static boolean isPowerOfTwo(long n) {
if (n == 0) {
return false;
}
return (long) (Math.ceil((Math.log(n) / Math.log(2))))
== (long) (Math.floor(((Math.log(n) / Math.log(2)))));
}
public static long getFact(int n) {
long ans = 1;
while (n > 0) {
ans *= n;
ans %= MOD;
n--;
}
return ans;
}
public static long pow(long n, int pow) {
if (pow == 0) {
return 1;
}
long temp = pow(n, pow / 2) % MOD;
temp *= temp;
temp %= MOD;
if (pow % 2 == 1) {
temp *= n;
}
temp %= MOD;
return temp;
}
public static long nCr(int n, int r) {
long ans = 1;
int temp = n - r;
while (n > temp) {
ans *= n;
ans %= MOD;
n--;
}
ans *= pow(getFact(r) % MOD, MOD - 2) % MOD;
ans %= MOD;
return ans;
}
//////////////////////////////////////////////////////////////////
// method returns Nth power of A
static double nthRoot(int A, int N) {
// intially guessing a random number between
// 0 and 9
double xPre = Math.random() % 10;
// smaller eps, denotes more accuracy
double eps = 0.001;
// initializing difference between two
// roots by INT_MAX
double delX = 2147483647;
// xK denotes current value of x
double xK = 0.0;
// loop untill we reach desired accuracy
while (delX > eps) {
// calculating current value from previous
// value by newton's method
xK = ((N - 1.0) * xPre
+ (double) A / Math.pow(xPre, N - 1)) / (double) N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
}
| 0 | Non-plagiarised |
73f57af1 | ac8ef97c | /*input
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out;
static int MOD = 1000000007;
static FastReader scan;
/*-------- I/O usaing short named function ---------*/
public static String ns(){return scan.next();}
public static int ni(){return scan.nextInt();}
public static long nl(){return scan.nextLong();}
public static double nd(){return scan.nextDouble();}
public static String nln(){return scan.nextLine();}
public static void p(Object o){out.print(o);}
public static void ps(Object o){out.print(o + " ");}
public static void pn(Object o){out.println(o);}
/*-------- for output of an array ---------------------*/
static void iPA(int arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void lPA(long arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void sPA(String arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void dPA(double arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
/*-------------- for input in an array ---------------------*/
static void iIA(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void lIA(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void sIA(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void dIA(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*------------ for taking input faster ----------------*/
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;
}
}
// Method to check if x is power of 2
static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);}
//Method to return lcm of two numbers
static int gcd(int a, int b){return a==0?b:gcd(b % a, a); }
//Method to count digit of a number
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
//Method for sorting
static void ruffle_sort(int[] a) {
//shandom_ruffle
Random r=new Random();
int n=a.length;
for (int i=0; i<n; i++) {
int oi=r.nextInt(n);
int temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//sort
Arrays.sort(a);
}
//Method for checking if a number is prime or not
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long[] l, r;
public static void main (String[] args) throws java.lang.Exception
{
OutputStream outputStream =System.out;
out =new PrintWriter(outputStream);
scan =new FastReader();
//for fast output sometimes
StringBuilder sb = new StringBuilder();
int t = ni();
while(t-->0){
int n = ni();
l = new long[n];
r = new long[n];
for(int i=0; i<n; i++){
l[i] = nl();
r[i] = nl();
}
//lPA(l);
//lPA(r);
ArrayList<Integer> adj[] = new ArrayList[n];
for(int i=0; i<n; i++)
adj[i] = new ArrayList<Integer>();
for(int i=0; i<n-1; i++){
int u = ni()-1, v = ni()-1;
adj[u].add(v);
adj[v].add(u);
}
dp = new Long[n][2];
visited = new boolean[n];
long ans = Math.max(solve(adj, 0, 0, visited), solve(adj, 0, 1, visited));
pn(ans);
}
out.flush();
out.close();
}
static Long dp[][];
static boolean visited[];
static long solve(ArrayList<Integer> adj[], int vertex, int prev, boolean visited[]){
visited[vertex] = true;
if(dp[vertex][prev] != null)
return dp[vertex][prev];
long ans = 0;
for(int x : adj[vertex]){
if(!visited[x]){
if(prev == 0){
ans += Math.max(Math.abs(l[vertex] - l[x]) + solve(adj, x, 0, visited),
Math.abs(l[vertex] - r[x]) + solve(adj, x, 1, visited));
//pn(vertex + " " + x + " " + ans);
}else{
ans += Math.max(Math.abs(r[vertex] - l[x]) + solve(adj, x, 0, visited),
Math.abs(r[vertex] - r[x]) + solve(adj, x, 1, visited));
//pn(vertex + " " + x + " " + ans);
}
}
}
visited[vertex] = false;
//pn(ans);
return dp[vertex][prev] = ans;
}
}
| import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
int n=Integer.parseInt(bu.readLine());
g=new ArrayList[n];
int i;
for(i=0;i<n;i++)
{
g[i]=new ArrayList<>();
String st[]=bu.readLine().split(" ");
a[i][0]=Integer.parseInt(st[0]); a[i][1]=Integer.parseInt(st[1]);
s[i][0]=s[i][1]=0;
}
for(i=0;i<n-1;i++)
{
String st[]=bu.readLine().split(" ");
int u=Integer.parseInt(st[0])-1,v=Integer.parseInt(st[1])-1;
g[u].add(v); g[v].add(u);
}
dfs(0,-1);
sb.append(Math.max(s[0][0],s[0][1])+"\n");
}
System.out.print(sb);
}
static ArrayList<Integer> g[];
static int N=100000,a[][]=new int[N][2];
static long s[][]=new long[N][2];
static void dfs(int n,int p)
{
for(int x:g[n])
if(x!=p)
{
dfs(x,n);
s[n][0]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][0]),s[x][1]+Math.abs(a[x][1]-a[n][0]));
s[n][1]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][1]),s[x][1]+Math.abs(a[x][1]-a[n][1]));
}
}
}
| 0 | Non-plagiarised |
5b9a0551 | 9debf95c | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner in=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int t=in.nextInt();
while(t-->0)
solve(in,out);
out.close();
}
static void solve(FastScanner in,PrintWriter out){
int n=in.nextInt();
long a[]=new long[n];
for (int i = 0; i < n; i++) {
a[i]=in.nextLong();
}
long odd=Integer.MAX_VALUE,even=Integer.MAX_VALUE;
even=a[0];
long sum=a[0];
long ans=Long.MAX_VALUE;
for (int i = 1; i < n; i++) {
if(i%2==0) {
ans=Math.min(ans,(n-i/2)*a[i] + odd*(n-i/2) +sum);
even=Math.min(even,a[i]);
} else {
ans=Math.min(ans,(n-i/2)*a[i] + even*(n-i/2-1) +sum);
odd=Math.min(odd,a[i]);
}
sum+=a[i];
}
out.println(ans);
}
static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> {
public U x;
public V y;
public pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(pair<U, V> other) {
int i = x.compareTo(other.x);
if (i != 0) return i;
return y.compareTo(other.y);
}
public String toString() {
return x.toString() + " " + y.toString();
}
public boolean equals(Object obj) {
if (this.getClass() != obj.getClass()) return false;
pair<U, V> other = (pair<U, V>) obj;
return x.equals(other.x) && y.equals(other.y);
}
public int hashCode() {
return 31 * x.hashCode() + y.hashCode();
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| // A Computer is Like a mischievous genie.
// It will give you exactly what you ask for,
// but not always what you want
// A code by Rahul Verma
import java.io.BufferedOutputStream;
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.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main {
static class Clock {
protected long start, stop;
public void start() {
start = System.currentTimeMillis();
}
public void stop() {
stop = System.currentTimeMillis();
}
public String getTime() {
return ((stop - start) + " ms");
}
}
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
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[] nextSArray() {
String sr[] = null;
try {
sr = br.readLine().trim().split(" ");
} catch (IOException e) {
e.printStackTrace();
}
return sr;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long powmodulo(long a, long p) {
if (p == 0) {
return 1 % mod;
}
if (p == 1) {
return a % mod;
}
long ans = 1;
while (p > 0) {
if ((p & 1) > 0) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
p = p >> 1;
}
return ans % mod;
}
static long mod = 1000000007;
static long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static long fast_powerNumbers(long a, long n) {
if (n == 1) {
return a;
}
long ans = fast_powerNumbers(a, n / 2);
if (n % 2 == 0) {
return (ans * ans);
} else {
return ((ans * ans) * (a));
}
}
static void dfs_helper(int[][] arr, int i, int j, int team, int n, int m) {
arr[i][j] = team;
if (i - 1 >= 0 && arr[i - 1][j] == 1) {
dfs(arr, i - 1, j, team, n, m);
}
if (j - 1 >= 0 && arr[i][j - 1] == 1) {
dfs(arr, i, j - 1, team, n, m);
}
if (i + 1 < n && arr[i + 1][j] == 1) {
dfs(arr, i + 1, j, team, n, m);
}
if (j + 1 < m && arr[i][j + 1] == 1) {
dfs(arr, i, j + 1, team, n, m);
}
}
static void dfs(int[][] arr, int i, int j, int team, int n, int m) {
dfs_helper(arr, i, j, team, n, m);
}
static int parent[];
static int rank[];
static int find(int i) {
if (parent[i] == -1) {
parent[i] = i;
return i;
}
if (parent[i] == i) {
return i;
} else {
parent[i] = find(parent[i]);
}
return parent[i];
}
static void unite(int s1, int s2) {
if (rank[s1] > rank[s2]) {
parent[s2] = s1;
rank[s1] += rank[s2];
} else {
parent[s1] = s2;
rank[s2] += rank[s1];
}
}
public static long arr[];
public static int arr1[];
// static void seive(int n) {
// arr = new int[n + 1];
// arr[0] = arr[1] = 1;
// for (int i = 4; i <= n; i = i + 2) {
// arr[i] = 1;
// }
// for (int i = 3; i * i <= n; i = i + 2) {
// if (arr[i] == 0) {
// for (int j = i * i; j <= n; j = j + i) {
// arr[j] = 1;
// }
//
// }
// }
// }
static void seive(int n)
{
arr1=new int[n+1];
arr1[0]=arr1[1]=1;
for (int i = 4; i <=n ; i+=2) {
arr1[i]=1;
}
for (int i = 3; i*i <=n ; i+=2) {
if (arr1[i] == 0)
{
for (int j = i*i; j <=n ; j+=i) {
arr1[j]=1;
}
}
}
}
public static boolean ccw(Point a,Point b,Point c)
{
long s1=(c.x-b.x)*(b.y-a.y) ;
long s2 = (c.y-b.y)*(b.x-a.x);
if(s1<s2)
return true;
else
return false;
}
public static boolean col(Point a,Point b,Point c)
{
long s1=(c.x-b.x)*(b.y-a.y) ;
long s2 = (c.y-b.y)*(b.x-a.x);
if(s1==s2)
return true;
else
return false;
}
static class Point
{
long x,y;
Point(long x,long y)
{
this.x=x;
this.y=y;
}
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
Clock clock = new Clock();
clock.start();
int t1=sc.nextInt();
for (int t = 0; t <t1 ; t++) {
int n=sc.nextInt();
long arr[]=new long[n];
for (int i = 0; i < n; i++) {
arr[i]=sc.nextLong();
}
long sum1=0;
long sum2=0;
long min1 =Long.MAX_VALUE;
long min2 =Long.MAX_VALUE;
long brr[]=new long[n];
for (int i = 0; i <n ; i+=2) {
int nn =i/2;
min1=Math.min(arr[i],min1);
sum1+=arr[i];
long x =(sum1-min1) + min1*(n-nn);
// System.out.println(x);
brr[i]=x;
}
for (int i = 1; i <n ; i+=2) {
int nn =i/2;
//System.out.println(x);
min2=Math.min(arr[i],min2);
sum2+=arr[i];
long x =(sum2-min2) + min2*(n-nn);
brr[i]=x;
}
long min=Long.MAX_VALUE;
for (int i = 1; i < n; i++) {
min=Math.min(brr[i]+brr[i-1],min);
}
out.println(min);
//out.println(Arrays.toString(brr));
// System.out.println(min1+" "+min2);
}
out.close();
}
}
class Trio{
int a,i1,i2;
Trio(int a,int i1,int i2)
{
this.a=a;
this.i1=i1;
this.i2=i2;
}
}
class Pair{
int a,b;
Pair(int a,int b)
{
this.a=a;
this.b=b;
}
}
class DSU
{
int parent[];
int rank[];
long arr[] = Main.arr;
long sum[] ;
DSU(int n)
{
this.parent=new int[n];
this.rank=new int[n];
sum=new long[n];
Arrays.fill(parent,-1);
Arrays.fill(rank,1);
for (int i = 0; i <n ; i++) {
sum[i]=arr[i];
}
}
int find(int s1)
{
if(parent[s1]==-1)
return s1;
return parent[s1]=find(parent[s1]);
}
void unite(int s1,int s2)
{
int p1=this.find(s1);
int p2=this.find(s2);
if(p1==p2)
return;
if(rank[p1]>rank[p2])
{
parent[p2]=p1;
rank[p1]+=rank[p2];
// System.out.println(arr[p2]);
sum[p1]+=sum[p2];
//sum[p2]+=sum[p1];
}
else
{
parent[p1]=p2;
rank[p2]+=rank[p1];
//System.out.println(arr[p1]);
sum[p2]+=sum[p1];
//sum[p1]+=sum[p2];
}
}
}
class Gaph {
HashMap<Integer, ArrayList<Integer>> hm;
Gaph() {
hm = new HashMap<>();
}
Gaph(int n) {
hm = new HashMap<>();
for (int i = 0; i < n; i++) {
hm.put(i, new ArrayList<Integer>());
}
}
// function for adding an edge.................................................
public void addEdge(int a, int b, boolean isDir) {
if (isDir) {
if (hm.containsKey(a)) {
hm.get(a).add(b);
} else {
hm.put(a, new ArrayList<>(Arrays.asList(b)));
}
} else {
if (hm.containsKey(a)) {
hm.get(a).add(b);
} else if (!hm.containsKey(a)) {
hm.put(a, new ArrayList<>(Arrays.asList(b)));
}
if (hm.containsKey(b)) {
hm.get(b).add(a);
} else if (!hm.containsKey(b)) {
hm.put(b, new ArrayList<>(Arrays.asList(a)));
}
}
}
}
// out.println(al.toString().replaceAll("[\\[|\\]|,]",""));
| 0 | Non-plagiarised |
0ca738ce | 931faca2 | import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class G {
public static void main(String[] args) {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
int t = 1;
for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out);
out.close();
}
static class Task {
static int max = (int) (4e5), MOD = 998244353;
static long[] fact = new long[max+1], invFact = new long[max+1], naturalInverse = new long[max+1];
public void solve(int testNumber, FastReader scan, PrintWriter out) {
int n = scan.nextInt(), k = scan.nextInt();
Item[] lanterns = new Item[2 * n];
for(int i = 0; i < n; i++) {
int l = scan.nextInt(), r = scan.nextInt();
lanterns[i * 2] = new Item(l, 0);
lanterns[i * 2 + 1] = new Item(r, 1);
}
Arrays.sort(lanterns);
precomp();
int have = 0;
long ans = 0;
for(Item x : lanterns) {
if(x.start == 1) have--;
else {
ans = (ans + binomial(have, k - 1)) % MOD;
have++;
}
}
out.println(ans);
}
static class Item implements Comparable<Item> {
int val;
int start;
public Item(int a, int b) {
val = a;
start = b;
}
@Override
public int compareTo(Item item) {
int ret = Integer.compare(val, item.val);
if(ret == 0) ret = Integer.compare(start, item.start);
return ret;
}
}
static void precomp() {
fact[0] = invFact[0] = invFact[1] = naturalInverse[0] = naturalInverse[1] = 1;
for(int i = 1; i <= max; i++) {
fact[i] = (fact[i-1]*i)%MOD;
if(i == 1) continue;
naturalInverse[i] = naturalInverse[MOD % i] * (MOD - MOD/i) % MOD;
invFact[i] = (invFact[i-1]*naturalInverse[i])%MOD;
}
}
static long binomial(int a, int b) {
if(a < b) return 0;
return ((fact[a]*invFact[b])%MOD*invFact[a-b])%MOD;
}
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc=new FastReader();
static int dp[][][][];
static int mod=1000000007;
static int mod1=998244353;
static int max;
static long bit[];
static long seg[];
static long fact[];
static long A[];
static long[] fac = new long[300001];
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args)
{
//CHECK FOR N=1
//CHECK FOR N=1
//StringBuffer sb=new StringBuffer("");
int ttt=1;
// ttt =i();
fac[0] = 1;
for (int i = 1; i <= 300000; i++)
fac[i] = fac[i - 1] * i % mod1;
outer :while (ttt-- > 0)
{
int n=i();
int k=i();
Pair P[]=new Pair[2*n];
int c=0;
for(int i=0;i<n;i++) {
P[c]=new Pair(i(),0);
c++;
P[c]=new Pair(i(),1);
c++;
}
Arrays.sort(P);
int cnt=0;
long ans=0;
for(int i=0;i<2*n;i++) {
if(P[i].y==0)
cnt++;
else {
ans+=nCrModPFermat(cnt-1, k-1, mod1);
ans%=mod1;
cnt--;
}
}
System.out.println(ans);
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
static long modInverse(long n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(int n, int r,
int p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return +1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return +1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
// public int hashCode()
// {
// final int temp = 14;
// int ans = 1;
// ans =x*31+y*13;
// return ans;
// }
// @Override
// public boolean equals(Object o)
// {
// if (this == o) {
// return true;
// }
// if (o == null) {
// return false;
// }
// if (this.getClass() != o.getClass()) {
// return false;
// }
// Pair other = (Pair)o;
// if (this.x != other.x || this.y!=other.y) {
// return false;
// }
// return true;
// }
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
//static void add(int v) {
// if(!map.containsKey(v)) {
// map.put(v, 1);
// }
// else {
// map.put(v, map.get(v)+1);
// }
//}
//static void remove(int v) {
// if(map.containsKey(v)) {
// map.put(v, map.get(v)-1);
// if(map.get(v)==0)
// map.remove(v);
// }
//}
public static int upper(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return A[a]=find(A, A[a]);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void print(int A[]) {
for(int i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
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;
}
}
}
| 0 | Non-plagiarised |
0c00d532 | 521c5645 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class First {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
//int a = 1;
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
//out.print("Case #"+(a++)+": ");
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n;
n = in.nextInt();
int[] arr = new int[n];
long[] array = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
if(i==0){
array[i] = arr[i];
}
else
array[i] = (long)arr[i] + array[i-1];
}
int evenMin = arr[0], oddMin = arr[1];
long ans = ((long) n * (long)arr[0]) + ((long) n * (long)arr[1]);
for (int i = 2; i < n; i++) {
if(i%2==0){
if(evenMin>arr[i]) {
evenMin = arr[i];
}
}
else{
if(oddMin>arr[i]) {
oddMin = arr[i];
}
}
if(i%2==0){
ans = Math.min(ans, array[i] + (long) ((n - (i / 2)) - 1) * (long)evenMin + (long) ((n - ((i-1) / 2)) - 1) * (long)oddMin );
}
else{
ans = Math.min(ans, array[i] + (long) ((n - (i / 2)) - 1) * (long)oddMin + (long) ((n - ((i-1) / 2)) - 1) * (long)evenMin);
}
}
out.println(ans);
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a;
int b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return this.a - o.a;
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public int compareTo(answer1 o) {
return this.a - o.a;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static 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());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
| import java.io.*;
import java.util.*;
/*
*/
public class C {
static FastReader sc=null;
public static void main(String[] args) {
sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int c[]=sc.readArray(n);
long dp[]=new long[n];
dp[0]=Long.MAX_VALUE;
dp[1]=(long)n*c[1]+ (long)n*c[0];
long preMin[]=new long[n],pre[]=new long[n];
preMin[0]=pre[0]=c[0];
preMin[1]=pre[1]=c[1];
for(int i=2;i<n;i++) {
preMin[i]=Math.min(preMin[i-2],c[i]);
pre[i]=pre[i-2]+c[i];
}
for(int i=2;i<n;i++) {
dp[i]=preMin[i]*(n-i/2)+pre[i]-preMin[i]+preMin[i-1]*(n-(i-1)/2)+pre[i-1]-preMin[i-1];
}
long ans=Long.MAX_VALUE;
//print(dp);
for(long e:dp)ans=Math.min(ans, e);
System.out.println(ans);
}
}
static void ruffleSort(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
}
static void ruffleSort(long a[]) {
ArrayList<Long> al=new ArrayList<>();
for(long i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
}
static int[] reverse(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al,Collections.reverseOrder());
for(int i=0;i<a.length;i++)a[i]=al.get(i);
return a;
}
static int gcd(int a,int b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static void print(long a[]) {
for(long e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static void print(char a[]) {
for(char e:a) {
System.out.print(e);
}
System.out.println();
}
static void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
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;
}
int[] readArray(int n) {
int a[]=new int [n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
return a;
}
}
}
| 0 | Non-plagiarised |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.