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 |
---|---|---|---|---|---|
6f02c6d9 | ff34fab2 |
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.ArrayList;
import java.util.Collections;
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;
import java.util.TreeSet;
import java.io.*;
public class Div2 {
private 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 String solution(int [] arr, int n, String st)
{
ArrayList<Integer> red = new ArrayList<>();
ArrayList<Integer> blue = new ArrayList<>();
for(int i = 0; i<n; i++)
{
if(st.charAt(i)=='R')
red.add(arr[i]);
else
blue.add(arr[i]);
}
Collections.sort(red);
Collections.sort(blue);
int cb = 1;
for(int j = 0; j<blue.size(); j++)
{
if(blue.get(j)<cb)
return "NO";
cb++;
}
int cr = n;
for(int j = red.size()-1; j>=0; j--)
{
if(red.get(j)>cr)
return "NO";
cr--;
}
return "YES";
}
private static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args)
{
MyScanner s = new MyScanner();
int t = s.nextInt();
for(int j = 0; j<t ; j++)
{
int n = s.nextInt();
int[] arr = new int[n];
for(int i =0; i<n; i++)
arr[i] = s.nextInt();
String st = s.next();
out.println(solution(arr,n, st));
}
out.flush();
out.close();
}
}
| 1 | Plagiarised |
a5d5a95f | c4ca2ff3 | 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);
}
}
}
| /* 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();
}
}
| 0 | Non-plagiarised |
4e9c4bf9 | f9e08a46 | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class E1525D {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
ArrayList<Integer> occupied = new ArrayList<>();
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
if (arr[i] == 1) occupied.add(i);
}
int[][] dp = new int[n + 1][occupied.size() + 1];
for (int[] row : dp) Arrays.fill(row, (int) 1e9);
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
dp[i][0] = 0;
for (int j = 1; j <= occupied.size(); j++) {
dp[i][j] = dp[i - 1][j];
if (arr[i - 1] == 0)
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1] + Math.abs(i - 1 - occupied.get(j - 1)));
}
}
System.out.println(dp[n][occupied.size()]);
}
}
| import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Armchair {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int arr[]=new int[n];
ArrayList<Integer> list1=new ArrayList<Integer>();
ArrayList<Integer> list2=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
int a=in.nextInt();
if(a==0)
list2.add(i);
else
list1.add(i);
}
long dp[][]=new long[list1.size()+1][list2.size()+1];
solve(list1,list2,dp);
System.out.println(dp[list1.size()][list2.size()]);
}
public static void solve( ArrayList<Integer> list1,ArrayList<Integer> list2,long dp[][]){
for(int i=1;i<=list1.size();i++)
dp[i][0]=Integer.MAX_VALUE;
for(int i=1;i<=list1.size();i++){
for(int j=1;j<=list2.size();j++){
dp[i][j]=Math.min(Math.abs(list1.get(i-1)-list2.get(j-1))+dp[i-1][j-1],dp[i][j-1]);
}
}
}
} | 0 | Non-plagiarised |
31cdf5fe | 80cc284c | import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while(t-- > 0)
{
int n = scanner.nextInt();
int[] time = new int[n];
int[] h = new int[n];
for(int i = 0; i < n; i++)
{
time[i] = scanner.nextInt();
}
for(int j = 0; j < n; j++)
{
h[j] = scanner.nextInt();
}
long prevMTime = time[n-1];
long prevMHealth = h[n-1];
long manaRequired = 0;
long currMTime = time[n-1];
long currMHealth = h[n-1];
long spellCastingTime = prevMTime - prevMHealth + 1;
for(int i = n-2; i >= 0; i--)
{
currMHealth = h[i];
currMTime = time[i];
//spellCastingTime = prevMTime - prevMHealth + 1;
if(spellCastingTime <= currMTime && currMTime - spellCastingTime + 1 < currMHealth)
{
spellCastingTime = currMTime - currMHealth + 1;
}
else if(spellCastingTime > currMTime)
{
long hm = (prevMTime - spellCastingTime) + 1;
manaRequired += (hm * (hm+1)) / 2;
prevMTime =currMTime;
prevMHealth = currMHealth;
spellCastingTime = prevMTime - prevMHealth + 1;
}
}
long hm = (prevMTime - spellCastingTime) + 1;
manaRequired += (hm * (hm+1)) / 2;
System.out.println(manaRequired);
}
}
}
| 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();
long[] k = new long[n];
long[] h = new long[n];
for(int i = 0;i < n;i++){
k[i] = sc.nextLong();
}
for(int i = 0;i < n;i++){
h[i] = sc.nextLong();
}
long length = h[n-1];
long curr = 0;
long ans = 0;
for(int i = n-1;i >= 1;i--){
if(k[i] - k[i-1] >= length){
curr += length;
ans = ans += (curr * (curr + 1))/2;
length = h[i-1];
curr = 0;
}else{
length = Math.max(length - (k[i] - k[i-1]), h[i-1]);
curr += k[i] - k[i-1];
}
}
curr += length;
ans += (curr * (curr + 1))/2;
System.out.println(ans);
}
}
} | 0 | Non-plagiarised |
4a1573d6 | db8d728d |
import java.util.*;
import java.math.*;
import java.io.*;
public class A{
static FastReader scan=new FastReader();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static LinkedList<Integer>edges[];
static boolean stdin = true;
static String filein = "input";
static String fileout = "output";
static int dx[] = { -1, 0, 1, 0 };
static int dy[] = { 0, 1, 0, -1 };
int dx_8[]={1,1,1,0,0,-1,-1,-1};
int dy_8[]={-1,0,1,-1,1,-1,0,1};
static char sts[]={'U','R','D','L'};
static boolean prime[];
static long LCM(long a,long b){
return (Math.abs(a*b))/gcd(a,b);
}
public static int upperBound(long[] array, int length, long value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = low+(high-low) / 2;
if ( array[mid]>value) {
high = mid ;
} else {
low = mid+1;
}
}
return low;
}
static long gcd(long a, long b) {
if(a!=0&&b!=0)
while((a%=b)!=0&&(b%=a)!=0);
return a^b;
}
static int countSetBits(int n)
{
int count = 0;
while (n > 0) {
if((n&1)!=1)
count++;
//count += n & 1;
n >>= 1;
}
return count;
}
static void sieve(long n)
{
prime = new boolean[(int)n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
static boolean isprime(long x)
{
for(long i=2;i*i<=x;i++)
if(x%i==0)
return false;
return true;
}
static int perm=0,FOR=0;
static boolean flag=false;
static int len=100000000;
static ArrayList<Pair>inters=new ArrayList<Pair>();
static StringBuilder sb;
static void swap(int i,int j,StringBuilder st)
{
char tmp=st.charAt(i);
st.setCharAt(i,st.charAt(j));
st.setCharAt(j,tmp);
}
private static int next(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to right side if target is
// greater.
if(arr[mid]==target)
return mid;
if (arr[mid] <target) {
start = mid + 1;
}
// Move left side.
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
//static boolean vis[][];
static long solve(int h,long n,int cur)
{
if(h==0)
return 0;
long half=1L<<(h-1);
if(n<=half)
{
if((cur^1)==0)
return 1+solve(h-1,n,0);
else
return 2*half+solve(h-1,n,0);
}
else
{
if((cur^1)==0)
return 2*half+solve(h-1,n-half,1);
else
return 1+solve(h-1,n-half,1);
}
}
public static class comp1 implements Comparator<String>{
public int compare(String o1,String o2){
return o1.length()-o2.length();
}
}
public static class comp2 implements Comparator<String>{
public int compare(String o1,String o2){
return o1.compareTo(o2);
}
}
static StringBuilder a,b;
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static ArrayList<Integer>v;
static ArrayList<Integer>pows;
static void block(long x)
{
v = new ArrayList<Integer>();
pows=new ArrayList<Integer>();
while (x > 0)
{
v.add((int)x % 3);
x = x / 3;
}
// Displaying the output when
// the bit is '1' in binary
// equivalent of number.
for (int i = 0; i < v.size(); i++)
{
if (v.get(i)==1||v.get(i)==0)
{
pows.add(i);
}
}
}
static long mod=(long)(1e9)+7;
static int countWaysUtil(int x, int n, int num)
{
// Base cases
int val = (int) (x - Math.pow(num, n));
if (val == 0)
return 1;
if (val < 0)
return 0;
// Consider two possibilities, num is
// included and num is not included.
return countWaysUtil(val, n, num + 1) +
countWaysUtil(x, n, num + 1);
}
static int countWays(int x, int n)
{
return countWaysUtil(x, n, 1);
}
static int dp[][];
static int n;
static int arr[];
static int rec(int i,int prev)
{
if(i==n)
return 0;
if(dp[i][prev]!=-1){
//out.println("FUCK");
return dp[i][prev];
}
if(i==0)
return dp[i][prev]=Math.max(rec(i+1,1),rec(i+1,0));
else
return dp[i][prev]=Math.max(rec(i+1,0)+Math.abs(arr[i]-(prev==0?arr[i-1]:1)),rec(i+1,1)+Math.abs(1-(prev==0?arr[i-1]:1)));
}
static boolean can(int i,int j)
{
int x=i,y=j;
int x2=Math.abs(y-x),y2=y+x;
if(x>=x2&&y<=y2)
return true;
return false;
}
public static void main(String[] args) throws Exception
{
//SUCK IT UP AND DO IT ALRIGHT
//scan=new FastReader("div7.in");
//out = new PrintWriter("div7.out");
//System.out.println(countSetBits(2015));
//int elem[]={1,2,3,4,5};
//System.out.println("avjsmlfpb".compareTo("avjsmbpfl"));
int tt=1;
/*for(int i=0;i<=100;i++)
if(prime[i])
arr.add(i);
System.out.println(arr.size());*/
// check(new StringBuilder("05:11"));
// System.out.println(26010000000000L%150);
tt=scan.nextInt();
outer:while(tt-->0)
{
int n=scan.nextInt(),m=scan.nextInt();
char board[][]=new char[n][m];
for(int i=0;i<n;i++)
board[i]=scan.next().toCharArray();
ArrayList<Pair>res[]=new ArrayList[3*n*m];
for(int i=0;i<3*n*m;i++)
res[i]=new ArrayList();
int k=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
ArrayList<Pair>arr=new ArrayList<Pair>();
ArrayList<Pair>arr2=new ArrayList<Pair>();
if(i+1<n&&j+1<m)
{
if(board[i][j]=='1')
arr.add(new Pair(i,j));
else arr2.add(new Pair(i,j));
if(board[i][j+1]=='1')
arr.add(new Pair(i,j+1));
else arr2.add(new Pair(i,j+1));
if(board[i+1][j]=='1')
arr.add(new Pair(i+1,j));
else arr2.add(new Pair(i+1,j));
if(board[i+1][j+1]=='1')
arr.add(new Pair(i+1,j+1));
else arr2.add(new Pair(i+1,j+1));
if(arr.size()==3)
{
for(Pair p:arr){
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
}
k++;
}
else if(arr.size()==2)
{
for(Pair p:arr2)
{
board[p.x][p.y]='1';
res[k].add(new Pair(p.x,p.y));
}
//k++;
Pair p=arr.get(0);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
k++;
for(Pair pp:arr2)
{
board[pp.x][pp.y]='0';
res[k].add(new Pair(pp.x,pp.y));
}
p=arr.get(1);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
k++;
}
else if(arr.size()==1)
{
Pair p=arr.get(0);
Pair tmp=new Pair(p.x,p.y);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
p=arr2.get(1);
board[p.x][p.y]='1';
res[k].add(new Pair(p.x,p.y));
p=arr2.get(2);
board[p.x][p.y]='1';
res[k].add(new Pair(p.x,p.y));
k++;
arr=new ArrayList();
arr.add(new Pair(arr2.get(1).x,arr2.get(1).y));
arr.add(new Pair(arr2.get(2).x,arr2.get(2).y));
arr2.remove(1);
arr2.remove(1);
arr2.add(new Pair(tmp.x,tmp.y));
for(Pair pp:arr2)
{
board[pp.x][pp.y]='1';
res[k].add(new Pair(pp.x,pp.y));
}
//k++;
p=arr.get(0);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
k++;
for(Pair pp:arr2)
{
board[pp.x][pp.y]='0';
res[k].add(new Pair(pp.x,pp.y));
}
p=arr.get(1);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
k++;
}
else if(arr.size()==4)
{
Pair p=arr.get(0);
board[p.x][p.y]='0';
arr2.add(new Pair(p.x,p.y));
res[k].add(new Pair(p.x,p.y));
p=arr.get(1);
board[p.x][p.y]='0';
arr2.add(new Pair(p.x,p.y));
res[k].add(new Pair(p.x,p.y));
p=arr.get(2);
board[p.x][p.y]='0';
arr2.add(new Pair(p.x,p.y));
res[k].add(new Pair(p.x,p.y));
k++;
arr.remove(0);
arr.remove(0);
arr.remove(0);
p=arr.get(0);
Pair tmp=new Pair(p.x,p.y);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
p=arr2.get(1);
board[p.x][p.y]='1';
res[k].add(new Pair(p.x,p.y));
p=arr2.get(2);
board[p.x][p.y]='1';
res[k].add(new Pair(p.x,p.y));
k++;
arr=new ArrayList();
arr.add(new Pair(arr2.get(1).x,arr2.get(1).y));
arr.add(new Pair(arr2.get(2).x,arr2.get(2).y));
arr2.remove(1);
arr2.remove(1);
arr2.add(new Pair(tmp.x,tmp.y));
for(Pair pp:arr2)
{
board[pp.x][pp.y]='1';
res[k].add(new Pair(pp.x,pp.y));
}
//k++;
p=arr.get(0);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
k++;
for(Pair pp:arr2)
{
board[pp.x][pp.y]='0';
res[k].add(new Pair(pp.x,pp.y));
}
p=arr.get(1);
board[p.x][p.y]='0';
res[k].add(new Pair(p.x,p.y));
k++;
}
}
}
}
/*for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
{
out.print(board[i][j]+" ");
}
out.println();
}*/
out.println(k);
for(int i=0;i<k;i++)
{
for(Pair p:res[i])
out.print((p.x+1)+" "+(p.y+1)+" ");
out.println();
}
}
out.close();
//SEE UP
}
static class special{
char c;
int idx;
special(char c,int idx)
{
this.c=c;
this.idx=idx;
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long fastPow(int base, int pow) {
if (pow==0)
return 1;
long half=fastPow(base, pow/2);
if (pow%2==0)
return half*half%mod;
return half*half%mod*base%mod;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return (base % mod+mod)%mod;
long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return (base * R % mod+mod)%mod;
}
else return (R %mod+mod)%mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
//Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private static void sort2(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastReader(String filename)throws Exception
{
br=new BufferedReader(new FileReader(filename));
}
boolean hasNext(){
String line;
while(root.hasMoreTokens())
return true;
return false;
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
}
static class Pair implements Comparable<Pair>{
public int x, y;
public Pair(int x1, int y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y;
}
public int compareTo(Pair o)
{
return (o.y-y);
}
static class pair{
int i;
int j;
pair(int i,int j){
this.i=i;
this.j=j;
}}}
static class tuple{
int x,y,z;
tuple(int a,int b,int c){
x=a;
y=b;
z=c;
}
}
static class Edge{
int d,w;
Edge(int d,int w)
{
this.d=d;
this.w=w;
}
}
}
| import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
public class BinaryTable {
// static int mod = 998244353;
static int mod = 1000000007;
private 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 scn = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int t = scn.nextInt();
outer : while(t-->0){
int n = scn.nextInt();
int m = scn.nextInt();
int[][] arr = new int[n][m];
for(int i=0; i<n; i++){
String s = scn.nextLine();
for(int j=0; j<m; j++){
char ch = s.charAt(j);
arr[i][j] = ch - '0';
}
}
ArrayList<Pair> list = new ArrayList<>();
if(n % 2 == 0 && m % 2 == 0){
for(int i=0; i<n; i+=2){
for(int j=0; j<m; j+=2){
if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}
}
}
}else if(n % 2 == 0 && m % 2 == 1){
int M = m-1;
for(int i=0; i<n; i+=2){
for(int j=0; j<M; j+=2){
if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}
}
}
for(int i=0; i<n; i+=2){
if(arr[i][m-1] == 1 && arr[i+1][m-1] == 1){
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i, m-1));
}else if(arr[i][m-1] == 1){
list.add(new Pair(i, m-2));
list.add(new Pair(i, m-1));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-1));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i, m-1));
}else if(arr[i+1][m-1] == 1){
list.add(new Pair(i, m-1));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-1));
list.add(new Pair(i, m-2));
}
}
}else if(n % 2 == 1 && m % 2 == 0){
int N = n-1;
for(int i=0; i<N; i+=2){
for(int j=0; j<m; j+=2){
if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}
}
}
for(int j=0; j<m; j+=2){
if(arr[n-1][j] == 1 && arr[n-1][j+1] == 1){
list.add(new Pair(n-2, j));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-1, j));
list.add(new Pair(n-2, j));
list.add(new Pair(n-2, j+1));
}else if(arr[n-1][j] == 1){
list.add(new Pair(n-2, j));
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-1, j));
list.add(new Pair(n-2, j));
}else if(arr[n-1][j+1] == 1){
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-2, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-2, j));
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
}
}
}else if(n % 2 == 1 && m % 2 == 1){
int N = n-1;
int M = m-1;
for(int i=0; i<N; i+=2){
for(int j=0; j<M; j+=2){
if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j] == 1 && arr[i][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1 && arr[i+1][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
}else if(arr[i][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}else if(arr[i][j+1] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j] == 1){
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i, j));
}else if(arr[i+1][j+1] == 1){
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j+1));
list.add(new Pair(i+1, j+1));
list.add(new Pair(i, j));
list.add(new Pair(i+1, j));
}
}
}
for(int j=0; j<M; j+=2){
if(arr[n-1][j] == 1 && arr[n-1][j+1] == 1){
list.add(new Pair(n-2, j));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-1, j));
list.add(new Pair(n-2, j));
list.add(new Pair(n-2, j+1));
}else if(arr[n-1][j] == 1){
list.add(new Pair(n-2, j));
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-1, j));
list.add(new Pair(n-2, j));
}else if(arr[n-1][j+1] == 1){
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-2, j));
list.add(new Pair(n-1, j+1));
list.add(new Pair(n-2, j+1));
list.add(new Pair(n-2, j));
list.add(new Pair(n-1, j));
list.add(new Pair(n-1, j+1));
}
}
for(int i=0; i<N; i+=2){
if(arr[i][m-1] == 1 && arr[i+1][m-1] == 1){
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i, m-1));
}else if(arr[i][m-1] == 1){
list.add(new Pair(i, m-2));
list.add(new Pair(i, m-1));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-1));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i, m-1));
}else if(arr[i+1][m-1] == 1){
list.add(new Pair(i, m-1));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-2));
list.add(new Pair(i+1, m-2));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i+1, m-1));
list.add(new Pair(i, m-1));
list.add(new Pair(i, m-2));
}
}
if(arr[n-1][m-1] == 1){
list.add(new Pair(n-1, m-1));
list.add(new Pair(n-2, m-1));
list.add(new Pair(n-1, m-2));
list.add(new Pair(n-2, m-2));
list.add(new Pair(n-1, m-2));
list.add(new Pair(n-1, m-1));
list.add(new Pair(n-1, m-1));
list.add(new Pair(n-2, m-2));
list.add(new Pair(n-2, m-1));
}
}
int k = list.size()/3;
pw.println(k);
for(int i=0; i<k; i++){
for(int j=0; j<3; j++){
Pair p = list.get(i*3+j);
pw.print((p.x+1) + " " + (p.y+1) + " ");
}
pw.println();
}
}
pw.close();
}
public static class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for(int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
// private static void sort(Pair[] arr) {
// List<Pair> list = new ArrayList<>();
// for(int i=0; i<arr.length; i++){
// list.add(arr[i]);
// }
// Collections.sort(list); // collections.sort uses nlogn in backend
// for (int i = 0; i < arr.length; i++){
// arr[i] = list.get(i);
// }
// }
private static void reverseSort(int[] arr){
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list, Collections.reverseOrder()); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void reverseSort(long[] arr){
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list, Collections.reverseOrder()); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void reverseSort(ArrayList<Integer> list){
Collections.sort(list, Collections.reverseOrder());
}
private static int lowerBound(int[] arr, int x){
int n = arr.length, si = 0, ei = n - 1;
while(si <= ei){
int mid = si + (ei - si)/2;
if(arr[mid] == x){
if(mid-1 >= 0 && arr[mid-1] == arr[mid]){
ei = mid-1;
}else{
return mid;
}
}else if(arr[mid] > x){
ei = mid - 1;
}else{
si = mid+1;
}
}
return si;
}
private static int upperBound(int[] arr, int x){
int n = arr.length, si = 0, ei = n - 1;
while(si <= ei){
int mid = si + (ei - si)/2;
if(arr[mid] == x){
if(mid+1 < n && arr[mid+1] == arr[mid]){
si = mid+1;
}else{
return mid + 1;
}
}else if(arr[mid] > x){
ei = mid - 1;
}else{
si = mid+1;
}
}
return si;
}
private static int upperBound(ArrayList<Integer> list, int x){
int n = list.size(), si = 0, ei = n - 1;
while(si <= ei){
int mid = si + (ei - si)/2;
if(list.get(mid) == x){
if(mid+1 < n && list.get(mid+1) == list.get(mid)){
si = mid+1;
}else{
return mid + 1;
}
}else if(list.get(mid) > x){
ei = mid - 1;
}else{
si = mid+1;
}
}
return si;
}
// (x^y)%p in O(logy)
private static long power(long x, long y){
long res = 1;
x = x % mod;
while(y > 0){
if ((y & 1) == 1){
res = (res * x) % mod;
}
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
public static boolean nextPermutation(int[] arr) {
if(arr == null || arr.length <= 1){
return false;
}
int last = arr.length-2;
while(last >= 0){
if(arr[last] < arr[last+1]){
break;
}
last--;
}
if (last < 0){
return false;
}
if(last >= 0){
int nextGreater = arr.length-1;
for(int i=arr.length-1; i>last; i--){
if(arr[i] > arr[last]){
nextGreater = i;
break;
}
}
swap(arr, last, nextGreater);
}
reverse(arr, last+1, arr.length-1);
return true;
}
private static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static void reverse(int[] arr, int i, int j){
while(i < j){
swap(arr, i++, j--);
}
}
private static String reverseStr(String s){
StringBuilder sb = new StringBuilder(s);
return sb.reverse().toString();
}
// TC- O(logmax(a,b))
private static int gcd(int a, int b) {
if(a == 0){
return b;
}
return gcd(b%a, a);
}
private static long gcd(long a, long b) {
if(a == 0L){
return b;
}
return gcd(b%a, a);
}
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
// TC- O(logmax(a,b))
private static int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
private static long inv(long x){
return power(x, mod - 2);
}
private static long summation(long n){
return (n * (n + 1L)) >> 1;
}
} | 0 | Non-plagiarised |
35f0c004 | 8d6f1bf5 | 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;
}
}
} | //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--;
}
}
}
| 0 | Non-plagiarised |
4fd752f7 | c7239c97 | /*==========================================================================*/
/*
* AUTHOR: RonWonWon
* CREATED: 02.05.2021 20:16:37
* EMAIL: rachitpts.2454@gmail.com
*/
/*==========================================================================*/
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt(), tt = 0;
while(t-->0) {
int n = in.nextInt(), m = in.nextInt(), x = in.nextInt();
int aa[] = in.readArray(n);
Pair a[] = new Pair[n];
for(int i=0;i<n;i++) a[i] = new Pair(i,aa[i]);
PriorityQueue<Pair> pq = new PriorityQueue<>();
Arrays.sort(a);
int goneTo[] = new int[n];
for(int i=0;i<m;i++){
pq.add(new Pair(i+1,a[n-1-i].y));
goneTo[a[n-1-i].x] = i+1;
}
for(int i=n-m-1;i>=0;i--){
Pair xx = pq.poll();
xx.y += a[i].y;
goneTo[a[i].x] = xx.x;
pq.add(new Pair(xx.x,xx.y));
}
Iterator<Pair> it = pq.iterator();
ArrayList<Pair> arr = new ArrayList<>();
while(it.hasNext()) arr.add(it.next());
Collections.sort(arr);
//for(Pair i : arr) out.print(i.y+" ");
//out.println();
if(arr.get(m-1).y-arr.get(0).y>x) out.println("NO");
else{
out.println("YES");
int pos[] = new int[n];
for(Pair i : a) pos[i.x] = goneTo[i.x];
for(int i : pos) out.print(i+" ");
out.println();
}
//tt++; out.println("Case #"+tt+": "+ans);
}
out.flush();
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int a, int b){ x = a; y = b; }
@Override
public int compareTo(Pair o) {
if(o.y - this.y>0)
return -1;
else if(o.y - this.y<0)
return 1;
else
return 0;
}
}
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();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
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;
}
}
static final Random random = new Random();
static void ruffleSort(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);
}
}
| /*==========================================================================*/
/*
* AUTHOR: RonWonWon
* CREATED: 02.05.2021 20:16:37
* EMAIL: rachitpts.2454@gmail.com
*/
/*==========================================================================*/
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt(), tt = 0;
while(t-->0) {
int n = in.nextInt(), m = in.nextInt(), x = in.nextInt();
int aa[] = in.readArray(n);
Pair a[] = new Pair[n];
for(int i=0;i<n;i++) a[i] = new Pair(i,aa[i]);
PriorityQueue<Pair> pq = new PriorityQueue<>();
Arrays.sort(a);
int goneTo[] = new int[n];
for(int i=0;i<m;i++){
pq.add(new Pair(i+1,a[n-1-i].y));
goneTo[a[n-1-i].x] = i+1;
}
for(int i=n-m-1;i>=0;i--){
Pair xx = pq.poll();
xx.y += a[i].y;
goneTo[a[i].x] = xx.x;
pq.add(new Pair(xx.x,xx.y));
}
Iterator<Pair> it = pq.iterator();
ArrayList<Pair> arr = new ArrayList<>();
while(it.hasNext()) arr.add(it.next());
Collections.sort(arr);
//for(Pair i : arr) out.print(i.y+" ");
//out.println();
if(arr.get(m-1).y-arr.get(0).y>x) out.println("NO");
else{
out.println("YES");
int pos[] = new int[n];
for(Pair i : a) pos[i.x] = goneTo[i.x];
for(int i : pos) out.print(i+" ");
out.println();
}
//tt++; out.println("Case #"+tt+": "+ans);
}
out.flush();
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int a, int b){ x = a; y = b; }
@Override
public int compareTo(Pair o) {
if(o.y - this.y>0)
return -1;
else if(o.y - this.y<0)
return 1;
else
return 0;
}
}
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();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
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;
}
}
static final Random random = new Random();
static void ruffleSort(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);
}
}
| 1 | Plagiarised |
26e699de | 76ad805a | //package Task1;
import java.util.Scanner;
public class Menorah {
static int MOD9= 1000000000;
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int numberTest=sc.nextInt();
while(numberTest-->0){
int n=sc.nextInt();
char[] s=new char[n+5];
char[] t=new char[n+5];
String ss=sc.next();
String tt=sc.next();
s=ss.toCharArray();
t=tt.toCharArray();
int cntax = 0, cntbx = 0, same = 0;
int ans=MOD9;
for(int i=0; i<n; i++){
if(s[i]=='1')cntax++;
if(t[i]=='1')cntbx++;
if(s[i]==t[i])same++;
}
if(same==n){
System.out.println(0);
continue;
}
else if (cntax==0){
System.out.println(-1);
continue;
}
if(cntax==cntbx){
ans=Math.min(ans,n-same);
}
if(n-cntax+1==cntbx)ans=Math.min(ans,same);
if(ans<MOD9) System.out.println(ans);
else System.out.println(-1);
}
}
}
| import java.util.Arrays;
import java.util.Scanner;
public class First {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String a = sc.next();
String b = sc.next();
char ch1[] = a.toCharArray();
char ch2[] = b.toCharArray();
int zz = 0;
int oz = 0;
int zo = 0;
int oo = 0;
for (int i = 0; i < n; i++) {
if (ch1[i] == '0') {
if (ch2[i] == '0') {
zz += 1;
} else {
oz += 1;
}
} else {
if (ch2[i] == '0') {
zo += 1;
} else {
oo += 1;
}
}
}
int ans = -1;
if ((oo - zz) == 1 || zo == oz) {
int s1 = (int) 1e7;
int s2 = (int) 1e7;
if ((oo - zz) == 1) {
s1 = oo + zz;
}
if (zo == oz)
s2 = zo + oz;
ans = Math.min(s1, s2);
}
System.out.println(ans);
}
}
}
//9
//001011011
//011010101 3 2 2 2
//9
//100010111
//101101100 2 1 3 3 | 0 | Non-plagiarised |
1984bef1 | 2063ba3e | import java.util.*;
import java.io.*;
public class stoneheaps {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for(int i = 0; i < t; i ++) {
int n = Integer.parseInt(br.readLine());
String[] tokens = br.readLine().split(" ");
int[] arr = new int[n];
for(int j = 0; j < n; j++) {
arr[j] = Integer.parseInt(tokens[j]);
}
int l = 1;
int r = 1000000000;
while(l < r) {
int mid = l + (r-l+1)/2;
if(check(arr, mid)) {
l = mid;
}
else {
r = mid-1;
}
}
System.out.println(l);
}
}
static boolean check(int[] arr, int x) {
int[] changed = new int[arr.length];
for(int i = 0; i < arr.length; i ++) {
changed[i] = arr[i];
}
int n = arr.length;
for(int i = n-1; i >= 0; i--) {
if(changed[i] < x) {
return false;
}
else if(i > 1){
int change = Math.min(changed[i]-x, arr[i])/3;
changed[i-1] = changed[i-1]+change;
changed[i-2] = changed[i-2] +change*2;
}
}
return true;
}
}
| import java.io.*;
import java.util.*;
public class c {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int numCases = Integer.parseInt(in.readLine());
for (int casenum = 0; casenum < numCases; casenum++) {
int n = Integer.parseInt(in.readLine());
long[] arr = new long[n];
StringTokenizer tokenizer = new StringTokenizer(in.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(tokenizer.nextToken());
}
long low = 0;
long high = 1000000000;
low--;
while (low < high) {
long mid = low + (high - low + 1) / 2;
boolean works = test(arr, mid);
if (works) {
low = mid;
} else {
high = mid - 1;
}
//System.out.println(mid + " " + works);
}
System.out.println(low);
}
in.close();
out.close();
}
public static boolean test(long[] arr, long k) {
long[] h = new long[arr.length];
for (int i = 0; i < arr.length; i++) {
h[i] = arr[i];
}
for (int i = h.length - 1; i - 2 >= 0; i--) {
if (h[i] < k)
break;
long d = Math.min((h[i]-k) / 3, arr[i]/3);
h[i] -= 3 * d;
h[i - 1] += d;
h[i-+ 2] += 2 * d;
}
for (int i = 0; i < h.length; i++) {
if (h[i] < k)
return false;
}
//System.out.println(Arrays.toString(h));
return true;
}
} | 0 | Non-plagiarised |
1410e423 | da5cf40b | import java.io.*;
import java.util.*;
public class C {
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 t = Integer.parseInt(br.readLine());
while(t --> 0) {
int n = Integer.parseInt(br.readLine());
char[] lineA = br.readLine().toCharArray();
char[] lineB = br.readLine().toCharArray();
boolean[] a = new boolean[n];
boolean[] b = new boolean[n];
int ac = 0;
int aic = 0;
int bc = 0;
int stay = 0;
int flip = 0;
for(int i = 0; i < n; i++) {
if(lineA[i] == '1') {
ac++;
a[i] = true;
}else
aic++;
if(lineB[i] == '1') {
bc++;
b[i] = true;
}
if(a[i] == b[i])
stay++;
else
flip++;
}
if(ac != bc && aic + 1 != bc) {
pw.println(-1);
}else {
if(ac == aic+1)
pw.println(Math.min(stay, flip));
else if(ac == bc)
pw.println(flip);
else
pw.println(stay);
}
}
pw.close();
}
}
| /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=(long)1e9+7;
/* start */
public static void main(String [] args)
{
// int testcases = 1;
int testcases = i();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i();
char c[] = inputC();
char d[] = inputC();
int x01=0,x10=0,x00=0,x11=0;
for(int i=0;i<n;i++)
{
if(c[i]=='0'&&d[i]=='0')x00++;
if(c[i]=='0'&&d[i]=='1')x01++;
if(c[i]=='1'&&d[i]=='0')x10++;
if(c[i]=='1'&&d[i]=='1')x11++;
}
int ans = Integer.MAX_VALUE;
if(x01==0 && x10==0)
{
pl(0);
return ;
}
if(x11==x00+1)
{
ans = min(x11+x00,ans);
}
if(x01==x10)
{
ans = min(x01+x10,ans);
}
if(ans == Integer.MAX_VALUE){
ans = -1;
}
pl(ans);
}
/* end */
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 p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
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 long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
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 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 int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
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 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 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 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 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 long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long f, long s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first > p.first)
return 1;
else if (first < p.first)
return -1;
else {
if (second > p.second)
return 1;
else if (second < p.second)
return -1;
else
return 0;
}
}
}
} | 0 | Non-plagiarised |
3b498a39 | ae775964 | /*
* 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.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.sound.sampled.ReverbType;
public class Edu109 {
static PrintWriter out;
static Scanner sc;
static ArrayList<int[]>q,w,x;
static ArrayList<Integer>adj[];
static HashSet<Integer>primesH;
static boolean prime[];
//static ArrayList<Integer>a;
static HashSet<Long>tmp;
static int[][][]dist;
static boolean[]v;
static int[]a,b,c,d;
static Boolean[][]dp;
static char[][]mp;
static int A,B,n,m,h,ans,sum;
//static String a,b;
static long oo=(long)1e9+7;
public static void main(String[]args) throws IOException {
sc=new Scanner(System.in);
out=new PrintWriter(System.out);
//A();
//B();
//C();
D();
//E();
//F();
//G();
out.close();
}
private static void A() throws IOException {
int t=ni();
while(t-->0) {
int k=ni();
int ans=0;
for(int i=1;i<=100;i++) {
if((i*100)%k==0) {
ans=i*100/k;break;
}
}
ol(ans);
}
}
static void B() throws IOException {
int t=ni();
while(t-->0) {
int n=ni();
a=nai(n);
boolean isSorted=true,one=a[0]==1;
int pos=0;
for(int i=1;i<n;i++) {
if(a[i]<a[i-1])isSorted=false;
if(a[i]==1)pos=i;
}
boolean ok=true;
for(int i=pos+1;i<n;i++) {
if(a[i]<a[i-1])ok=false;
}
boolean lst=a[0]==n;
boolean fir=a[n-1]==1;
if(isSorted)ol(0);
else if(lst&&fir)ol(3);
else if(lst||fir)ol(2);
else if(a[0]==1||a[n-1]==n)ol(1);
else ol(2);
// if(isSorted)ol(0);
// else out.println(fl&&lst?3:((one?1:2)-(ok?1:0)+(lst?1:0)));
}
}
static void C() throws IOException{
int t=ni();
while(t-->0) {
int n=ni();
int m=ni();
a=nai(n);
TreeMap<Integer, Integer>tr=new TreeMap<Integer, Integer>();
for(int i=0;i<n;i++)tr.put(a[i], i);
char[]mv=new char[n];
for(int i=0;i<n;i++) {
String s=ns();
mv[i]=s.charAt(0);
}
long[]ans=new long[n];
Arrays.fill(ans, -1);
PriorityQueue<Integer>lodd=new PriorityQueue<Integer>();
PriorityQueue<Integer>lev=new PriorityQueue<Integer>();
PriorityQueue<Integer>rodd=new PriorityQueue<Integer>();
PriorityQueue<Integer>rev=new PriorityQueue<Integer>();
for(int i=0;i<n;i++) {
if(a[i]%2==0) {
if(mv[i]=='L')lev.add(a[i]);
else rev.add(a[i]);
}else {
if(mv[i]=='L')lodd.add(a[i]);
else rodd.add(a[i]);
}
}
PriorityQueue<Integer>par[]=new PriorityQueue[4];
par[0]=rev;par[1]=rodd;par[2]=lev;par[3]=lodd;
for(int i=0;i<2;i++){
while(par[i].size()>=1&&par[i+2].size()>=1) {
int r=par[i].poll(),l=par[i+2].poll();
int d1=(l-r)/2;
if(l<r) {
d1=clc(l,r,m);
}
int dr=Integer.MAX_VALUE,dl=Integer.MAX_VALUE;
if(par[i].size()==1) {
dr=m-par[i].peek()+(par[i].peek()-r)/2;
}
if(!par[i+2].isEmpty()) {
dl=(par[i+2].peek()+l)/2;
}
int cur=Math.min(d1, Math.min(dl, dr));
if(cur==d1) {
ans[tr.get(r)]=ans[tr.get(l)]=cur;
}
else if(cur==dr) {
ans[tr.get(r)]=ans[tr.get(par[i].poll())]=cur;
par[i+2].add(l);
}else {
ans[tr.get(l)]=ans[tr.get(par[i+2].poll())]=cur;
par[i].add(r);
}
}
}
for(int i=0;i<4;i++) {
if(i<2&&par[i].size()%2==1)par[i].poll();
while(par[i].size()>1) {
int x=par[i].poll(),y=par[i].poll();
int val=0;
if(i<2) {
val=m-y+(y-x)/2;
}else {
val=(x+y)/2;
}
ans[tr.get(x)]=ans[tr.get(y)]=val;
}
}
for(int i=0;i<n;i++) {
out.print(ans[i]+" ");
}ol("");
}
}
private static int clc(int l, int r, int m) {
int b1=0,b2=m;
int ans=0;
if(l>m-r) {
ans+=l;
b2=2*m - l -r;// m-(l-m+r) -> m-l+m-r
}else {
ans+=m-r;
b1=m-r-l;
}
return ans+(b2-b1)/2;
}
private static Boolean dp(int i, int j) {
if(j>sum/2)return false;
if(i==x.size()) {
return sum/2==j;
}
if(dp[i][j]!=null)return dp[i][j];
return dp[i][j]=dp(i+1,j+x.get(i)[0])||dp(i+1,j);
}
static boolean isPrime(long n) {
if(n==2)return true;
if(n<2||n%2==0)return false;
for(long i=3L;i*i<n;i+=2l) {
long rem=(n%i);
if(rem==0)return false;
}
return true;
}
static long[][]mem;
static int ones;
static ArrayList<Integer>pos;
static void D() throws IOException {
int t=1;
while(t-->0) {
n=ni();
a=nai(n);
mem=new long[n][n];
ones=0;
pos=new ArrayList<Integer>();
for(int i=0;i<n;i++) {
Arrays.fill(mem[i], -1);
if(a[i]==1)pos.add(i);
}
ones=pos.size();
long ans=solve(0,0);
out.println(ans);
}
}
private static long solve(int i, int j) {
if(i==n||j>=ones)return j==ones?0:(long)1e14;
if(mem[i][j]!=-1)return mem[i][j];
long lv=solve(i+1,j);
if(a[i]==0) {
int pr=Math.abs(i-pos.get(j));
lv=Math.min(lv, pr+solve(i+1,j+1));
}
return mem[i][j]=lv;
}
private static int bfs(int i, int j,int k) {
boolean [][]vis=new boolean[dist.length][dist[0].length];
Queue<int[]>q=new LinkedList<int[]>();
int mn=Integer.MAX_VALUE;
q.add(new int[] {i,j,0,0});
int[]dx=new int[] {-1,1,0,0};
int[]dy=new int[] {0,0,1,-1};
while(!q.isEmpty()) {
int []x=q.poll();
vis[x[0]][x[1]]=true;
int c=x[2];
if(c>k/2)continue;
if(c>0&&k%c==0&&(k/c)%2==0) {
mn=Math.min(mn,x[3]*k/c );
}
for(int a=0;a<4;a++) {
int nx=x[0]+dx[a];
int ny=x[1]+dy[a];
if(valid(nx,ny)&&!vis[nx][ny]) {
q.add(new int[] {nx,ny,c+1,x[3]+dist[x[0]][x[1]][a]});
}
}
}
return mn;
}
private static boolean valid(int nx, int ny) {
return nx>=0&&nx<dist.length&&ny>=0&&ny<dist[0].length;
}
static int gcd (int a, int b) {
return b==0?a:gcd (b, a % b);
}
static void E() throws IOException {
int t=ni();
while(t-->0) {
}
}
static void F() throws IOException {
int t=ni();
while(t-->0) {
}
}
static void CC() throws IOException {
for(int kk=2;kk<21;kk++) {
ol(kk+" -------");
int n=kk;
int k=n-2;
int msk=1<<k;
int[]a=new int[k];
for(int i=0;i<a.length;i++)a[i]=i+2;
int mx=1;
int ms=0;
for(int i=1;i<msk;i++) {
long prod=1;
int cnt=0;
for(int j=0;j<a.length;j++) {
if(((i>>j)&1)!=0) {
prod*=a[j];
cnt++;
}
}
if(cnt>=mx&&prod%n==1) {
mx=cnt;
ms=i;
}
}
ol(mx==1?mx:mx+1);
out.print(1+" ");
long pr=1;
for(int j=0;j<a.length;j++) {
if(((ms>>j)&1)!=0) {
out.print(a[j]+" ");
pr*=a[j];
}
}
ol("");
ol("Prod: "+pr);
ol(n+"*"+((pr-1)/n)+" + "+1);
}
}
static int ni() throws IOException {
return sc.nextInt();
}
static double nd() throws IOException {
return sc.nextDouble();
}
static long nl() throws IOException {
return sc.nextLong();
}
static String ns() throws IOException {
return sc.next();
}
static int[] nai(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
return a;
}
static long[] nal(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextLong();
return a;
}
static int[][] nmi(int n,int m) throws IOException{
int[][]a=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=sc.nextInt();
}
}
return a;
}
static long[][] nml(int n,int m) throws IOException{
long[][]a=new long[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=sc.nextLong();
}
}
return a;
}
static void o(String x) {
out.print(x);
}
static void ol(String x) {
out.println(x);
}
static void ol(int x) {
out.println(x);
}
static void disp1(int []a) {
for(int i=0;i<a.length;i++) {
out.print(a[i]+" ");
}
out.println();
}
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 boolean hasNext() {return st.hasMoreTokens();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
| 0 | Non-plagiarised |
5288afb7 | df594a00 | 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)
{
bu.readLine();
String s[]=bu.readLine().split(" ");
int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]);
int a[]=new int[n],i,x,ac[]=new int[n],b[]=new int[k];
Arrays.fill(a,Integer.MAX_VALUE);
s=bu.readLine().split(" ");
for(i=0;i<k;i++) b[i]=Integer.parseInt(s[i])-1;
s=bu.readLine().split(" ");
for(i=0;i<k;i++)
{
x=Integer.parseInt(s[i]);
ac[b[i]]=x;
}
PriorityQueue<Integer> pq=new PriorityQueue<>();
for(i=0;i<n;i++)
{
if(ac[i]!=0) pq.add(ac[i]-i);
if(!pq.isEmpty()) a[i]=Math.min(a[i],pq.peek()+i);
}
pq=new PriorityQueue<>();
for(i=n-1;i>=0;i--)
{
if(ac[i]!=0) pq.add(ac[i]+i);
if(!pq.isEmpty()) a[i]=Math.min(a[i],pq.peek()-i);
}
for(i=0;i<n;i++) sb.append(a[i]+" ");
sb.append("\n");
}
System.out.print(sb);
}
}
| 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 |
ad2a1ae2 | bc46480a |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.StringTokenizer;
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;
}}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int r[]=new int[n];
int l[]=new int[n];
for(int i=0;i<n;i++) {
r[i]=sc.nextInt();
}
for(int i=0;i<n;i++) l[i]=r[i]-sc.nextInt()+1;
long ans=0,min=l[n-1],max=r[n-1];
for(int i=n-2;i>=0;i--) {
if(r[i]>=min) min=Math.min(min,l[i]);
else {
ans+=(max-min+1)*(max-min+2)/2;
max=r[i];
min=l[i];
}
}
ans+=(max-min+1)*(max-min+2)/2;
System.out.println(ans);
}
}
}
| 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 |
3b5cec19 | 8f31b279 | import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Test{
static class Pair implements Comparable<Pair>{
int wt;
int idx;
Pair(int x,int y){
this.wt=x;
this.idx=y;
}
@Override
public int compareTo(Pair x){
return this.wt-x.wt;
}
public String toString(){
return "("+wt+" "+idx+")";
}
}
public static void main (String[] args) throws java.lang.Exception{
FastReader scan=new FastReader();
int t=scan.nextInt();
while(t-->0){
int n=scan.nextInt();
int m=scan.nextInt();
int x=scan.nextInt();
int[]arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();
}
Queue<Pair>q=new PriorityQueue<>();
for(int i=1;i<=m;i++){
q.add(new Pair(0,i));
}
System.out.println("YES");
for(int i=0;i<n;i++){
Pair temp=q.poll();
int wt=temp.wt;
int idx=temp.idx;
System.out.print(idx+" ");
//System.out.println(temp);
q.add(new Pair(wt+arr[i],idx));
}
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;
}
}
}
| import java.util.*;
import java.io.*;
public class JavaTract
{
static class Pair implements Comparable<Pair>{
int first;
int second;
Pair(int x,int y){
this.first=x;
this.second=y;
}
@Override
public int compareTo(Pair x){
return this.first-x.first;
}
}
public static void main (String[] args)
{
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
while(t-->0){
int n=scan.nextInt();
int m=scan.nextInt();
int x=scan.nextInt();
int[]arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();
}
// TreeSet<Pair> set = new TreeSet<>();
Queue<Pair> set = new PriorityQueue<>();
for(int i=1;i<=m;i++){
set.add(new Pair(0,i));
}
System.out.println("YES");
for(int i=0;i<n;i++){
Pair temp=set.poll();
int first = temp.first;
int second = temp.second;
System.out.print(second+" ");
set.add(new Pair(first+arr[i],second));
}
System.out.println();
}
}
}
| 1 | Plagiarised |
22b41936 | 9cea10af |
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.*;
public class Main1 {
static ArrayList<Integer> list1 = new ArrayList<>() ;
static ArrayList<Integer> list2 = new ArrayList<>() ;
static int n , m ;
static long dp[][] ;
static long solver(int i , int j ){
// i = empty chairs
if (j == m)return 0 ;
int tt1 = n-i ;
int tt2 = m-j ;
if (n-i < m-j)return Long.MAX_VALUE/2 ;
if ( dp[i][j] != -1 )return dp[i][j] ;
long a = solver(i+1 , j) ;
long b = abs( list1.get(i) - list2.get(j)) + solver(i+1 , j+1) ;
return dp[i][j] = min(a , b) ;
}
public static void main(String[] args) throws IOException {
// try {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int N = in.nextInt() ;
int a[] = in.readArray(N) ;
for (int i = 0; i <N ; i++) {
if (a[i] == 1)list2.add(i) ;
else list1.add(i) ;
}
n = list1.size() ;
m = list2.size() ;
dp = new long[n][m] ;
for(int i=0 ; i<n ; i++)
for(int j=0 ; j<m ; j++)
dp[i][j] = -1 ;
System.out.println(solver(0 , 0 ));
out.flush();
out.close();
// }
// catch (Exception e){
// return;
// }
}
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static ArrayList<Integer> list = new ArrayList<>();
static boolean A[] = new boolean[2 * 90000001];
static void seive(int n) {
int maxn = n;
//int maxn = 1000000 ;
A[0] = A[1] = true;
for (int i = 2; i * i <= maxn; i++) {
if (!A[i]) {
for (int j = i * i; j <= maxn; j += i)
A[j] = true;
}
}
for (int i = 2; i <= maxn; i++)
if (!A[i])
list.add(i);
}
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 int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
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 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 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 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);
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private final Writer os;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(long c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(String c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput println() {
return append(System.lineSeparator());
}
public FastOutput flush() {
try {
// boolean success = false;
// if (stringBuilderValueField != null) {
// try {
// char[] value = (char[]) stringBuilderValueField.get(cache);
// os.write(value, 0, cache.length());
// success = true;
// } catch (Exception e) {
// }
// }
// if (!success) {
os.append(cache);
// }
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
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());
}
}
}
| //package currentContest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class P4 {
static int dp[][]=new int[5000+1][5000+1];
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader sc=new FastReader();
int t=1;
//t=sc.nextInt();
StringBuilder s=new StringBuilder();
while(t--!=0) {
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<=n;i++) {
for(int j=0;j<=n;j++) {
P4.dp[i][j]=-1;
}
}
ArrayList<Integer> one=new ArrayList<>();
ArrayList<Integer> zero=new ArrayList<>();
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
if(a[i]==0) {
zero.add(i);
}else {
one.add(i);
}
}
Collections.sort(zero);
Collections.sort(one);
long ans=sol(0,0,zero.size(),one.size(),a,zero,one);
System.out.println(ans);
}
//System.out.println(s);
}
private static long sol(int i, int j, int n, int m,int a[], ArrayList<Integer> zero, ArrayList<Integer> one) {
//System.out.println(i+" "+j);
// TODO Auto-generated method stub
if(j==m) {
return 0;
}
int av=n-i;
int rem=m-j;
if(av<rem) {
return Integer.MAX_VALUE-1;
}
if(dp[i][j]!=-1) {
return dp[i][j];
}
long ans1=sol(i+1,j,n,m,a, zero, one);
long ans2=Math.abs(zero.get(i)-one.get(j))+sol(i+1,j+1,n,m,a, zero, one);
dp[i][j]=(int) Math.min(ans1, ans2);
return dp[i][j];
}
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;
}
}
}
| 1 | Plagiarised |
307ef2cb | 565f77b7 | import java.util.PriorityQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
/*
Solution Created: 18:16:56 02/05/2021
Custom Competitive programming helper.
*/
public class Main {
public static void solve() {
int n = in.nextInt(), m = in.nextInt(), x = in.nextInt();
Pair[] a = new Pair[n];
for(int i = 0; i<n; i++) a[i] = new Pair(i, in.nextInt());
Util.sortArray(a);
Util.reverse(a);
PriorityQueue<Pair> heights = new PriorityQueue<>();
for(int i = 0; i<m; i++) heights.add(new Pair(i, 0));
int[] ans = new int[n];
for(int i = 0; i<n; i++) {
Pair p = heights.poll();
p.val += a[i].val;
ans[a[i].idx] = p.idx+1;
heights.add(p);
}
long mn = Long.MAX_VALUE, mx = Long.MIN_VALUE;
while(!heights.isEmpty()) {
Pair p = heights.poll();
mn = Math.min(mn, p.val);
mx = Math.max(mx, p.val);
}
if(mx-mn>x) {
out.println("NO");
return;
}
out.println("YES");
out.printlnArray(ans);
}
static class Pair implements Comparable<Pair>{
int idx;
long val;
public Pair(int idx, long val) {
this.idx = idx;
this.val = val;
}
public String toString() {
return this.idx+" "+this.val;
}
@Override
public int compareTo(Main.Pair o) {
return Long.compare(this.val, o.val);
}
}
public static void main(String[] args) {
in = new Reader();
out = new Writer();
int t = in.nextInt();
while(t-->0) solve();
out.exit();
}
static Reader in; static Writer out;
static class Reader {
static BufferedReader br;
static StringTokenizer st;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String f){
try {
this.br = new BufferedReader(new FileReader(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] ns(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) a[i] = next();
return a;
}
public int nextInt() {
ensureNext();
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
ensureNext();
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
ensureNext();
return Long.parseLong(st.nextToken());
}
public String next() {
ensureNext();
return st.nextToken();
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void ensureNext() {
if (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class Util{
private static Random random = new Random();
static long[] fact;
public static void initFactorial(int n, long mod) {
fact = new long[n+1];
fact[0] = 1;
for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod;
}
public static long modInverse(long a, long MOD) {
long[] gcdE = gcdExtended(a, MOD);
if (gcdE[0] != 1) return -1; // Inverted doesn't exist
long x = gcdE[1];
return (x % MOD + MOD) % MOD;
}
public static long[] gcdExtended(long p, long q) {
if (q == 0) return new long[] { p, 1, 0 };
long[] vals = gcdExtended(q, p % q);
long tmp = vals[2];
vals[2] = vals[1] - (p / q) * vals[2];
vals[1] = tmp;
return vals;
}
public static long nCr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nCr(int n, int r) {
return (fact[n]/fact[r])/fact[n-r];
}
public static long nPr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nPr(int n, int r) {
return fact[n]/fact[n-r];
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
return isPrime;
}
public static int gcd(int a, int b) {
int tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static long gcd(long a, long b) {
long tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static int random(int min, int max) {
return random.nextInt(max-min+1)+min;
}
public static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static void reverse(int[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
int tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(int[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(long[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
long tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(long[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(float[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
float tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(float[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(double[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
double tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(double[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(char[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
char tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(char[] s) {
reverse(s, 0, s.length-1);
}
public static <T> void reverse(T[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
T tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static <T> void reverse(T[] s) {
reverse(s, 0, s.length-1);
}
public static void shuffle(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(long[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
long t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(float[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
float t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(double[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
double t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(char[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static <T> void shuffle(T[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
T t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void sortArray(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(float[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(double[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(char[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T extends Comparable<T>> void sortArray(T[] a) {
Arrays.sort(a);
}
}
static class Writer {
private PrintWriter pw;
public Writer(){
pw = new PrintWriter(System.out);
}
public Writer(String f){
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public void printArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void printArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void print(Object o) {
pw.print(o.toString());
}
public void println(Object o) {
pw.println(o.toString());
}
public void println() {
pw.println();
}
public void flush() {
pw.flush();
}
public void exit() {
pw.close();
}
}
}
| import java.util.*;
import java.io.*;
public class Solution {
static Scanner scn = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
public static void main(String[] HastaLaVistaLa) {
int t = scn.nextInt();
while(t-- > 0) solve();
out.println(sb);
out.close();
}
public static void solve() {
// Road To Specialist Day 3
int n = scn.nextInt(), m = scn.nextInt(), x = scn.nextInt();
int[] a = new int[n], ans = new int[n];
for(int i = 0; i < n; i++) a[i] = scn.nextInt();
PriorityQueue<Pair> pq = new PriorityQueue<>();
for(int i = 0; i < m; i++) pq.add(new Pair(0L, i));
for(int i = 0; i < n; i++) {
int e = a[i];
Pair p = pq.poll();
p.value += e;
pq.add(p);
ans[i] = p.id + 1;
}
boolean check = false;
long prev = pq.poll().value;
while(!pq.isEmpty()) {
long cur = pq.poll().value;
if(Math.abs(cur - prev) > x) check = true;
prev = cur;
}
if(check) sb.append("NO");
else {
sb.append("YES\n");
for(int i : ans) sb.append(i + " ");
}
sb.append("\n");
}
static class Pair implements Comparable<Pair> {
int id;
long value;
public Pair(long value, int id) {
this.id = id;
this.value = value;
}
public int compareTo(Pair o) {
return Long.compare(value, o.value);
}
}
} | 0 | Non-plagiarised |
07a0e4dc | bac616ee | import java.io.*;
import java.util.*;
public class C_NotAssigning_1400 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
boolean[] visited = new boolean[n];
ArrayList<Edge>[] adj = new ArrayList[n];
for(int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for(int i = 0; i < n-1; i++) {
int a = sc.nextInt()-1;
int b = sc.nextInt()-1;
adj[a].add(new Edge(b, i));
adj[b].add(new Edge(a, i));
}
int start = -1;
boolean flag = false;
for(int i = 0; i < n; i++) { //start from leaf node
if(adj[i].size() == 1) {
start = i;
} else if(adj[i].size() > 2) {
flag = true;
}
}
int[] weights = new int[n-1];
Queue<Integer> que = new LinkedList<>();
que.offer(start);
visited[start] = true;
int curDist = 0;
while(!que.isEmpty()) {
int cur = que.poll();
for(Edge e : adj[cur]) {
if(!visited[e.to]) {
visited[e.to] = true;
que.offer(e.to);
if(curDist%2 == 0) {
weights[e.index] = 2;
} else {
weights[e.index] = 3;
}
}
}
curDist++;
}
if(flag) {
System.out.println(-1);
} else {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n-1; i++){
sb.append(weights[i] + " ");
}
System.out.println(sb.toString().trim());
}
}
out.close();
}
static class Edge{
public int to;
public int index;
Edge(int to, int index){
this.to = to;
this.index = index;
}
}
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;
}
}
}
| 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 |
13860c31 | 870e16d9 | import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
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();
char[] a=sc.nextLine().toCharArray();
char[] b = sc.nextLine().toCharArray();
int sml=0,nsml=0;
boolean flag=false;
int sm1=0;
int nsm1=0;
for(int i=0;i<n;i++){
if(a[i]==b[i]){
sml++;
if(a[i]=='1')sm1++;
}else {
if (a[i] == '1') nsm1++;
nsml++;
}
}
int d1=sml-2*sm1;
int d2=nsml-2*nsm1;
int ans=-1;
if(sml%2==1&&nsml%2==1&&d1==-1){
ans=sml;
}else if(sml%2==0&&nsml%2==0&&d2==0){
ans=nsml;
}else if(sml%2==1&&nsml%2==0&&(d1==-1||d2==0)){
if(d1==-1&&d2!=0){
ans=sml;
}else if(d1!=-1&&d2==0){
ans=nsml;
}else if(d1==-1&&d2==0){
ans=Math.min(sml,nsml);
}
}
if(sml==n)ans=0;
System.out.println(ans);
}
}
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;
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CF1 {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
int T=sc.nextInt();
// int T=1;
for (int tt=0; tt<T; tt++){
int n =sc.nextInt();
String a = sc.next();
String b= sc.next();
int candles1=0;
int candles2=0;
int inPlace=0;
for (int i=0; i<n; i++){
if (a.charAt(i)=='1'){
candles1++;
}
if (b.charAt(i)=='1') candles2++;
if (b.charAt(i)=='1' && a.charAt(i)=='1') inPlace++;
}
if (candles1==candles2 || n+1-candles2==candles1){
int ans=Math.min(help(candles1-inPlace, inPlace,n+1,candles2,0,0), help(candles1-inPlace, inPlace,n+1,candles2,0,1));
if (ans==Integer.MAX_VALUE) System.out.println(-1);
else System.out.println(ans);
}
else System.out.println(-1);
}
}
static int help(int i, int j, int max, int cands, int moves, int flip){
if (j==cands && i==0) return moves;
else if (j==0 && flip==0 || flip==1 && i==0 ) return Integer.MAX_VALUE;
if (flip==0){
int x=0;
int y=cands-j+1;
if (i+j==max-cands){
x=cands-y;
}
else x=max-cands-y;
return help(x,y,max,cands,moves+1,1);
}
else {
int x=0;
int y=cands-j;
if (i+j==max-cands){
x=cands-y;
}
else x=max-cands-y;
return help(x,y,max,cands,moves+1,0);
}
}
static long mod =998244353L;
static long power (long a, long b){
long res=1;
while (b>0){
if ((b&1)== 1){
res= (res * a % mod)%mod;
}
a=(a%mod * a%mod)%mod;
b=b>>1;
}
return res;
}
boolean[] sieveOfEratosthenes(int n)
{
boolean prime[] = new boolean[n+1];
for(int i=0;i<=n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
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 sortLong(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 long gcd (long n, long m){
if (m==0) return n;
else return gcd(m, n%m);
}
static class Pair implements Comparable<Pair>{
int x,y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair o){
return -this.x+o.x;
}
}
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());
}
}
} | 0 | Non-plagiarised |
213340b3 | 870e16d9 | 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 void sort(int a[]){ // int -> long
ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long
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);
}
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 int log(long n){
int res = 0;
while(n>0){
res++;
n/=2;
}
return res;
}
static int mod = (int)1e9+7;
static int INF = Integer.MAX_VALUE;
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ================================ //
int test = sc.nextInt();
while (test-- > 0) {
int n = sc.nextInt();
String s = sc.nextLine();
String t = sc.nextLine();
solver(s,t, n);
}
// ================================ //
// int n = sc.nextInt();
// solver();
// ================================ //
out.flush();
}
public static void solver(String s, String t, int n) {
int diff = 0;
int one = 0;
int zero = 0;
for(int i=0;i<n;i++){
if(s.charAt(i)!=t.charAt(i)){
diff++;
if(s.charAt(i)=='1')one++;
}
else{
if(s.charAt(i)=='1')zero++;
}
}
if(diff==0){
out.println(0);
return;
}
int res = INF;
if(diff%2==0 && one==(diff-one)){
res = diff;
}
if(n-diff-1>=0 && (n-diff-1)%2==0 && zero>0 && (n-diff-zero)==zero-1){
res = Math.min(res, (n-diff-1)+1);
}
out.println(res==INF?-1:res);
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CF1 {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
int T=sc.nextInt();
// int T=1;
for (int tt=0; tt<T; tt++){
int n =sc.nextInt();
String a = sc.next();
String b= sc.next();
int candles1=0;
int candles2=0;
int inPlace=0;
for (int i=0; i<n; i++){
if (a.charAt(i)=='1'){
candles1++;
}
if (b.charAt(i)=='1') candles2++;
if (b.charAt(i)=='1' && a.charAt(i)=='1') inPlace++;
}
if (candles1==candles2 || n+1-candles2==candles1){
int ans=Math.min(help(candles1-inPlace, inPlace,n+1,candles2,0,0), help(candles1-inPlace, inPlace,n+1,candles2,0,1));
if (ans==Integer.MAX_VALUE) System.out.println(-1);
else System.out.println(ans);
}
else System.out.println(-1);
}
}
static int help(int i, int j, int max, int cands, int moves, int flip){
if (j==cands && i==0) return moves;
else if (j==0 && flip==0 || flip==1 && i==0 ) return Integer.MAX_VALUE;
if (flip==0){
int x=0;
int y=cands-j+1;
if (i+j==max-cands){
x=cands-y;
}
else x=max-cands-y;
return help(x,y,max,cands,moves+1,1);
}
else {
int x=0;
int y=cands-j;
if (i+j==max-cands){
x=cands-y;
}
else x=max-cands-y;
return help(x,y,max,cands,moves+1,0);
}
}
static long mod =998244353L;
static long power (long a, long b){
long res=1;
while (b>0){
if ((b&1)== 1){
res= (res * a % mod)%mod;
}
a=(a%mod * a%mod)%mod;
b=b>>1;
}
return res;
}
boolean[] sieveOfEratosthenes(int n)
{
boolean prime[] = new boolean[n+1];
for(int i=0;i<=n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
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 sortLong(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 long gcd (long n, long m){
if (m==0) return n;
else return gcd(m, n%m);
}
static class Pair implements Comparable<Pair>{
int x,y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair o){
return -this.x+o.x;
}
}
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());
}
}
} | 0 | Non-plagiarised |
0e68e463 | 29cf2e70 | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class D {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for(int tt = 0; tt < t; tt++) {
int n = in.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = in.nextLong();
char[] s = in.next().toCharArray();
if (solve(arr, n, s))pw.println("YES");
else pw.println("NO");
}
pw.close();
}
static boolean solve(long[] arr, int n, char[] s) {
ArrayList<Long> B = new ArrayList<>();
ArrayList<Long> R = new ArrayList<>();
for(int i = 0; i < n; i++) {
if (s[i] == 'B') B.add(arr[i]);
else R.add(arr[i]);
}
Collections.sort(B);
Collections.sort(R);
// debug(B);
long last = n;
for (int i = R.size() - 1; i >= 0; i--) {
long v = R.get(i);
if (v > last) {
return false;
}
last--;
}
long first = 1;
int size = B.size();
for (int i = 0; i < size; i++) {
long v = B.get(i);
// debug(v, first);
if (v < first) return false;
first++;
}
return true;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| 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");
}
}
}
| 0 | Non-plagiarised |
28c2d81a | 48c5f745 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class D {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int cases = fs.nextInt();
while(cases-->0){
int n = fs.nextInt(), k = fs.nextInt();
int[] positions = fs.readArray(k), temps = fs.readArray(k);
int[] forced = new int[n];
Arrays.fill(forced, Integer.MAX_VALUE/2);
for(int i=0; i<k; i++)
forced[positions[i]-1] = temps[i];
for(int i=1; i<n; i++)
forced[i] = Math.min(forced[i],forced[i-1]+1);
for(int i=n-2; i>=0; i--)
forced[i] = Math.min(forced[i], forced[i+1]+1);
for(int i=0; i<n; i++)
System.out.print(forced[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());
}
char nextChar(){
return next().charAt(0);
}
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());
}
}
static class Debug{
public static void debug(long a){
System.out.println("--> "+a);
}
public static void debug(long a, long b){
System.out.println("--> "+ a +" + " + b);
}
public static void debug(char a, char b){ System.out.println("--> "+ a +" + " + b); }
public static void debug(int[] array){
System.out.print("Array--> ");
System.out.println(Arrays.toString(array));
}
public static void debug(char[] array){
System.out.print("Array--> ");
System.out.println(Arrays.toString(array));
}
public static void debug(HashMap<Integer,Integer> map){
System.out.print("Map--> "+map.toString());
}
}
}
|
import java.io.*;
import java.util.*;
public class E {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Reader sc=new Reader();
PrintWriter pw=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int k=sc.nextInt();
int[] idxes=new int[k];
int[] temps=new int[k];
for(int i=0;i<k;i++) {
idxes[i]=sc.nextInt()-1;
}
for(int i=0;i<k;i++) {
temps[i]=sc.nextInt();
}
int[] ans=new int[n];
Arrays.fill(ans, Integer.MAX_VALUE/2);
for(int i=0;i<k;i++) {
ans[idxes[i]]=temps[i];
}
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++) {
pw.print(ans[i]+" ");
}
pw.println();
}
pw.flush();
sc.close();
}
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();
}
}
}
| 1 | Plagiarised |
558df7d4 | d8654140 | import java.io.*;
import java.util.*;
public class Pupsen {
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();
int[] a = new int[n];
for (int i=0; i<n; i++) {
a[i] = in.nextInt();
}
int[] b = new int[n];
if (n%2==0) {
for (int i=0; i<n-1; i+=2) {
b[i] = -a[i+1];
b[i+1] = a[i];
}
for (int i=0; i<n; i++) System.out.print(b[i]+" ");
}
else {
if (a[0]+a[1]!=0) {
b[0] = -a[2];
b[1] = -a[2];
b[2] = a[0]+a[1];
}
else if (a[0]+a[2]!=0) {
b[0] = -a[1];
b[2] = -a[1];
b[1] = a[0]+a[2];
}
else {
b[1] = -a[0];
b[2] = -a[0];
b[0] = a[1]+a[2];
}
for (int i=3; i<n-1; i+=2) {
b[i] = -a[i+1];
b[i+1] = a[i];
}
for (int i=0; i<n; i++) System.out.print(b[i]+" ");
}
System.out.println();
}
}
static class FastIO {
BufferedReader br;
StringTokenizer st;
PrintWriter pr;
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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static int mod=1000000007 ;
static List<Integer>[] adj;
static boolean vst[];
static int dp[];
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[]=fs.readArray(n);
int ans[]=new int[n];
if(n%2==0) {
for(int i=0;i<n;i+=2) {
ans[i]=-arr[i+1];
ans[i+1]=arr[i];
}
}
else {
for(int i=3;i<n;i+=2) {
ans[i]=-arr[i+1];
ans[i+1]=arr[i];
}
int a=0, b=0, c=0;
outer:for(int i=0;i<3;i++) {
for(int j=i+1;j<3;j++) {
if(arr[i]+arr[j]!=0) {
b=i;
c=j;
a= 3-c-b;
break outer;
}
}
}
ans[a]=arr[b]+arr[c];
ans[b]=-arr[a];
ans[c]=-arr[a];
}
for(int i=0;i<n;i++) {
out.print(ans[i]+" ");
}
out.println();
// long sum=0;
// for(int i=0;i<n;i++) {
// sum+=arr[i]*ans[i];
// }
// if(sum!=0) System.out.println(false);
}
out.close();
}
static long pow(long a,long b) {
if(b<0) return 1;
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 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) {
// return fact[(int)n];
long res=1;
for(int i=2;i<=n;i++) {
res*=i;
res%=mod;
}
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());
}
long[] lreadArray(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
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());
}
}
} | 0 | Non-plagiarised |
3a12e509 | 47310b0e | 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.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException{
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
String c = br.readLine();
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (c.charAt(i) == 'B') blue.add(Integer.parseInt(s[i]));
else
red.add(Integer.parseInt(s[i]));
}
Collections.sort(blue);
Collections.sort(red);
int p1 = 0, p2 = 0, num = 1, flag = 1;
while (num <= n) {
if (p1 < blue.size()) {
if (blue.get(p1) < num) {
flag = 0;
break;
}
p1++;
}
else if (p2 < red.size()) {
if (red.get(p2) > num) {
flag = 0;
break;
}
p2++;
}
num++;
}
if (flag == 1) System.out.println("YES");
else
System.out.println("NO");
}
}
}
} | 0 | Non-plagiarised |
4241f473 | 77448a05 | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args)throws IOException {
FastScanner scan = new FastScanner();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt();
ArrayList<String> arr = new ArrayList<>();
for(int i = 0;i<n;i++) arr.add(scan.next());
int max = -1;
for(int cases = 0;cases<5;cases++) {
ArrayList<Integer> list = new ArrayList<>();
char ch = (char)('a'+cases);
for(int i = 0;i<n;i++) {
String s = arr.get(i);
int countch = 0, countTotal = 0;
for(int j = 0;j<s.length();j++) {
if(s.charAt(j)==ch) countch++;
else countTotal++;
}
list.add(countch-countTotal);
}
Collections.sort(list);
int sum = 0, count = 0;
for(int i = n-1;i>=0;i--) {
sum+=list.get(i);
if(sum>0) count++;
else break;
}
max = Math.max(max, count);
}
output.write(max+"\n");
}
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 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());
}
}
}
| //package currentContest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;
public class P4 {
static int N = 1000001;
// 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;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader sc = new FastReader();
int t;
t = sc.nextInt();
StringBuilder st = new StringBuilder();
// int p = 998244353;
// InverseofNumber(p);
// InverseofFactorial(p);
// factorial(p);
// System.out.println(3|1);
while (t-- != 0) {
int n=sc.nextInt();
String s[]=new String[n];
for(int i=0;i<n;i++) {
s[i]=sc.nextLine();
}
int max=0;
ArrayList<Integer> freq=new ArrayList<>();
for(int i=0;i<5;i++) {
int f1=0;
freq=new ArrayList<>();
for(int j=0;j<n;j++) {
f1=0;
for(int k=0;k<s[j].length();k++) {
if(s[j].charAt(k)==i+'a') {
f1++;
}else {
f1--;
}
}
freq.add(f1);
}
Collections.sort(freq);
int x=0;
int total=0;
for(int j=n-1;j>=0;j--) {
total=total+freq.get(j);
if(total>0) {
x++;
}else {
break;
}
}
max=Math.max(max, x);
}
st.append(max+"\n");
}
System.out.println(st);
}
static FastReader sc = new FastReader();
public static void solvegraph() {
int n = sc.nextInt();
int edge[][] = new int[n - 1][2];
for (int i = 0; i < n - 1; i++) {
edge[i][0] = sc.nextInt() - 1;
edge[i][1] = sc.nextInt() - 1;
}
ArrayList<ArrayList<Integer>> ad = new ArrayList<>();
for (int i = 0; i < n; i++) {
ad.add(new ArrayList<Integer>());
}
for (int i = 0; i < n - 1; i++) {
ad.get(edge[i][0]).add(edge[i][1]);
ad.get(edge[i][1]).add(edge[i][0]);
}
int parent[] = new int[n];
Arrays.fill(parent, -1);
parent[0] = n;
ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(0);
int child[] = new int[n];
Arrays.fill(child, 0);
ArrayList<Integer> lv = new ArrayList<Integer>();
while (!queue.isEmpty()) {
int toget = queue.getFirst();
queue.removeFirst();
child[toget] = ad.get(toget).size() - 1;
for (int i = 0; i < ad.get(toget).size(); i++) {
if (parent[ad.get(toget).get(i)] == -1) {
parent[ad.get(toget).get(i)] = toget;
queue.addLast(ad.get(toget).get(i));
}
}
lv.add(toget);
}
child[0]++;
}
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();
}
public 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;
}
}
}
| 1 | Plagiarised |
46bde295 | 8a858867 |
import java.io.*;
import java.util.Arrays;
public class C {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
private static int nextInt() {
try {
in.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return (int) in.nval;
}
private static long nextLong() {
try {
in.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return (long) in.nval;
}
private static String next(){
try {
in.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return in.sval;
}
public static void main(String[] args) {
int t = nextInt();
for (int c = 0; c < t; c++) {
int n = nextInt();
String[] info = new String[n];
for (int i = 0; i < n; i++) {
info[i] = next();
}
int ans = 0;
for (int i = 0; i < 5; i++) {
char cur = (char) (i+'a');
int[] map = new int[n];
for (int j = 0; j < n; j++) {
for (int k = 0; k < info[j].length(); k++) {
if(info[j].charAt(k)==cur)map[j]++;
else map[j]--;
}
}
Arrays.sort(map);
int sum = 0;
for(int j = n-1; j >= 0; j--) {
sum += map[j];
if(sum<=0){
break;
}
ans = Math.max(ans,n-j);
}
}
System.out.println(ans);
}
}
}
| import java.io.*;
import java.util.*;
public class Mainnn{
// static final File ip = new File("input.txt");
// static final File op = new File("output.txt");
// static {
// try {
// System.setOut(new PrintStream(op));
// System.setIn(new FileInputStream(ip));
// } catch (Exception e) {
// }
// // in = new InputReader(System.in);
// }
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
//out.println(result); // print via PrintWriter
/////////////////////////////////////////////
int test = sc.nextInt();
while(test-- != 0)
{
int n = sc.nextInt();
String[] info = new String[n];
for (int i = 0; i < n; i++) {
info[i] = sc.next();
}
int ans = 0;
for (int i = 0; i < 5; i++) {
char cur = (char) (i+'a');
int[] map = new int[n];
for (int j = 0; j < n; j++) {
for (int k = 0; k < info[j].length(); k++) {
if(info[j].charAt(k)==cur)map[j]++;
else map[j]--;
}
}
Arrays.sort(map);
int sum = 0;
for(int j = n-1; j >= 0; j--) {
sum += map[j];
if(sum<=0){
break;
}
ans = Math.max(ans,n-j);
}
}
System.out.println(ans);
}
/////////////////////////////////////////////
// Stop writing your solution here. -------------------------------------
out.close();
}
static void shuffleArrayL(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | 1 | Plagiarised |
80881cae | 9555b9d1 | //package Codeforces;
import java.io.*;
import java.util.*;
public class CP
{
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws IOException, CloneNotSupportedException
{
int t=sc.nextInt();
while(t-->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).clone());
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).clone());
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;
}
System.out.println(mana);
}
}
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)
{
long t=(this.x-a.x);
if(t!=0)
return t>0?1:-1;
else
return (int)(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;
// }
}
} | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
* 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();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int T = in.nextInt();
while (T-- > 0) {
solveOne(in, out);
}
}
private void solveOne(Scanner in, PrintWriter out) {
int N = in.nextInt();
int[] k = L.readIntArray(N, in), h = L.readIntArray(N, in);
Set<Integer> toRemove = new HashSet<>();
int prevCost = h[0], prevX = k[0];
for (int i = 1; i < N; i++) {
for (int j = i; j < N; j++) {
int currCost = h[j], currX = k[j];
if (currX - currCost + 1 <= prevX) {
if (prevCost + (currX - prevX) > h[j]) h[j] = prevCost + (currX - prevX);
}
}
prevX = k[i];
prevCost = h[i];
}
int nextCost = h[N - 1], nextX = k[N - 1];
for (int i = N - 2; i >= 0; i--) {
int currCost = h[i], currX = k[i];
if ((nextCost - currCost) >= (nextX - currX)) {
toRemove.add(i);
continue;
}
nextCost = currCost;
nextX = currX;
}
long cost = 0;
for (int i = N - 1; i >= 0; i--)
if (!toRemove.contains(i)) cost += (h[i] * ((long) h[i] + 1)) / 2;
out.println(cost);
}
}
static class L {
public static int[] readIntArray(int size, Scanner in) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.nextInt();
}
return array;
}
}
}
| 0 | Non-plagiarised |
6f02c6d9 | 884f5678 |
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.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class JaiShreeRam{
static Scanner in=new Scanner();
static long mod = 1000000007;
static ArrayList<ArrayList<Integer>> adj;
public static void main(String[] args) throws Exception{
class Element{
int x;
char c;
public Element(int y,char k) {
x=y;
c=k;
}
}
int z=in.readInt();
while(z-->0) {
int n=in.readInt();
int a[]=nia(n);
char c[]=in.readString().toCharArray();
ArrayList<Integer> d=new ArrayList<>();
ArrayList<Integer> in=new ArrayList<>();
for(int i=0;i<n;i++) {
if(c[i]=='R') {
in.add(a[i]);
}
else {
d.add(a[i]);
}
}
String ans="YES";
Collections.sort(d);
int k=1;
for(int i:d) {
if(i<k) {
ans="NO";
}
k++;
}
Collections.sort(in);
for(int i=in.size()-1;i>=0;i--) {
if(in.get(i)>n) {
ans="NO";
break;
}
n--;
}
System.out.println(ans);
}
}
static long ans(long l,long r,long x,long y) {
long mid=(r-l)/2+l;
long a=mid%x;
long b=y%mid;
long c=0;
if(l>r) {
return 0;
}
if(a==b) {
return mid;
}
else if(a<b) {
c=ans(l,mid-1,x,y);
if(c==0) {
c=ans(mid+1,r,x,y);
}
}
else{
c=ans(l,mid-1,x,y);
if(c==0) {
c=ans(mid+1,r,x,y);
}
}
return c;
}
static int[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
} | 1 | Plagiarised |
1a31f5cb | dc575a6c | import java.util.*;
import java.io.*;
public class Solution
{
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();
}
}
static void dfs(int n , LinkedList<Integer> g[] , int arr[][] , int p , long dp[][])
{
for(Integer i : g[n])
{
if(i != p)
{
dfs(i,g,arr,n,dp);
dp[n][0] += Math.max(dp[i][0]+Math.abs(arr[n][0]-arr[i][0]) ,
dp[i][1]+Math.abs(arr[n][0]-arr[i][1]));
dp[n][1] += Math.max(dp[i][0]+Math.abs(arr[n][1]-arr[i][0]) ,
dp[i][1]+Math.abs(arr[n][1]-arr[i][1]));
}
}
}
public static void main(String []args) throws IOException
{
Reader sc = new Reader();
StringBuffer str = new StringBuffer("");
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt();
int arr[][] = new int[n][2];
for(int i = 0 ; i < n; i++)
{
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
}
LinkedList<Integer> g[] = new LinkedList[n];
for(int i = 0 ; i < n ; i++)
{
g[i] = new LinkedList<Integer>();
}
for(int i = 0 ; i < n-1 ; i++)
{
int x = sc.nextInt()-1;
int y = sc.nextInt()-1;
g[x].add(y);
g[y].add(x);
}
long dp[][] = new long[n][2];
dfs(0,g,arr,-1 , dp);
str.append(Math.max(dp[0][0] , dp[0][1]));
str.append(System.lineSeparator());
}
System.out.println(str);
}
}
| // "static void main" must be defined in a public class.
import java.util.*;
import java.io.*;
public class Main {
static long[][]dp;
static ArrayList<Integer>[]adj;
static int[][]arr;
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();
arr=new int[n][2];
adj=new ArrayList[n];
for(int i=0;i<n;i++){
arr[i][0]=sc.nextInt();
arr[i][1]=sc.nextInt();
adj[i]=new ArrayList<>();
}
for(int i=0;i<n-1;i++){
int a=sc.nextInt()-1;
int b=sc.nextInt()-1;
adj[a].add(b);
adj[b].add(a);
}
dp=new long[n][2];
dfs(0,-1);
System.out.println(Math.max(dp[0][0],dp[0][1]));
}
}
public static void dfs(int v,int parent){
for(int u:adj[v]){
if(u!=parent){
dfs(u,v);
}
}
long ans1=0;
long ans2=0;
for(int u:adj[v]){
if(u!=parent){
long a=Math.abs(arr[v][0]-arr[u][0])+dp[u][0];
long b=Math.abs(arr[v][0]-arr[u][1])+dp[u][1];
long a1=Math.abs(arr[v][1]-arr[u][0])+dp[u][0];
long b1=Math.abs(arr[v][1]-arr[u][1])+dp[u][1];
ans1+=Math.max(a,b);
ans2+=Math.max(a1,b1);
}
}
dp[v][0]=ans1;
dp[v][1]=ans2;
}
} | 0 | Non-plagiarised |
ccc8ef27 | f7a0ea6d | import java.util.*;
public class Sol
{
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][5];
int tot[]=new int[n];
for(int i=0;i<n;i++)
{
String x = sc.next();
for(int j=0;j<x.length();j++)
a[i][x.charAt(j)-'a'] += 1;
tot[i]=x.length();
}
int max=Integer.MIN_VALUE;
for(int i=0;i<5;i++)
max=Math.max(max,function(a,n,i,tot));
System.out.println(max);
}
}
static int function(int a[][],int n,int i,int tot[])
{
Integer ans[] = new Integer[n];
for(int j=0;j<n;j++)
ans[j]=a[j][i]-(tot[j]-a[j][i]);
int res=0,j=0;
Arrays.sort(ans,Collections.reverseOrder());
while(j<n&&res+ans[j]>0)
res+=ans[j++];
return j;
}
} | 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) {
solution();
}
}
private static void solution() {
int ans=0;
int n = in.nextInt();
String s[] = new String[n];
int occurance[][] = new int[n][5];
for(int i=0;i<n;i++) {
s[i] = in.next();
for(int j=0;j<s[i].length();j++) {
occurance[i][s[i].charAt(j)-'a']++;
}
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<5;j++)
// System.out.println(occurance[i][j]);
// System.out.println();
// }
for(int i=0;i<5;i++) {
int arr[] = new int[n];
for(int j=0;j<n;j++) {
arr[j] = s[j].length() - (2 * occurance[j][i]);
}
Arrays.sort(arr);
// for(int j=0;j<n;j++)
// System.out.println(arr[j]);
int temp=0;
int count=0;
for(int j=0;j<n;j++) {
if(temp+arr[j] < 0)
{
count++;
temp += arr[j];
}
else
break;
}
ans = Math.max(ans, count);
}
System.out.println(ans);
}
} | 0 | Non-plagiarised |
9ab3c0e1 | d5fa4746 | //package CF672;
/**
* @version 1.0
* @Package_Name: CF672
* @Author: Redorain
* @Date: 2020/9/25 8:47
*/
import java.util.*;
public class d {
public static Scanner sc = new Scanner(System.in);
public static final int MOD = 998244353;
int []f; int [] lf;
public static int mul(int a, int b) {
return (int)((long)a * (long)b % MOD);
}
public static int ksm(int a, int n) {
int ans = 1;
while(n > 0) {
if((n & 1) == 1)
ans = mul(a, ans);
a = mul(a, a);
n >>= 1;
}
return ans;
}
public int C(int n, int k) {
return (k < 0 || k > n) ? 0 : mul(f[n], mul(lf[n - k], lf[k]));
}
public static int inv(int a) {
return ksm(a, MOD - 2);
}
public void solve() {
int n = sc.nextInt();
int k = sc.nextInt();
f = new int[n + 42];
lf = new int[n + 42];
f[0] = lf[0] = 1;
for(int i = 1; i < f.length; i++) {
f[i] = mul(f[i - 1], i);
lf[i] = mul(lf[i - 1], inv(i));
}
int[] events = new int[2 * n];
for(int i = 0; i < n; i++) {
int le = sc.nextInt();
int ri = sc.nextInt();
events[i] = le * 2;
events[i + n] = ri * 2 + 1;
}
Arrays.sort(events);
int ans = 0, 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) {
ans += C(balance + added, k);
if(ans >= MOD) ans -= MOD;
ans += MOD - C(balance, k);
if(ans >= MOD) ans -= MOD;
balance += added;
}
else balance -= added;
}
sc.close();
System.out.println(ans);
}
public static void main(String[] args) {
(new d()).solve();
}
} | import java.lang.*;
import java.util.*;
import java.io.*;
public class Main {
static FastScanner in = new FastScanner();
static final int MOD = 998244353;
static int n, k;
static int[] fact, invFact;
static int mul(int a, int b) {
return (int)((long)a * (long)b % MOD);
}
static int pow(int a, int p) {
int ans = 1;
while (p != 0) {
if ((p & 1) != 0)
ans = mul(ans, a);
a = mul(a, a);
p >>= 1;
}
return ans;
}
static int inv(int a) {
return pow(a, MOD - 2);
}
static void precalc() {
fact = new int[n + 42]; invFact = new int[n + 42];
fact[0] = invFact[0] = 1;
for (int i = 1; i < fact.length; ++i) {
fact[i] = mul(fact[i - 1], i);
invFact[i] = mul(invFact[i - 1], inv(i));
}
}
static int nCk(int a, int b) {
if (b < 0 || a < b)
return 0;
return mul(fact[a], mul(invFact[a - b], invFact[b]));
}
static void solve() {
n = in.nextInt(); k = in.nextInt();
precalc();
int[] events = new int[2 * n];
for (int i = 0; i < n; ++i) {
int l = in.nextInt(), r = in.nextInt();
events[i] = 2 * l;
events[i + n] = 2 * r + 1;
}
Arrays.sort(events);
int ans = 0, 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) {
ans += nCk(balance + added, k);
if (ans >= MOD) ans -= MOD;
ans += MOD - nCk(balance, k);
if (ans >= MOD) ans -= MOD;
balance += added;
} else
balance -= added;
}
System.out.println(ans);
}
public static void main(String[] args) {
int T = 1;
while (T-- > 0)
solve();
}
static class Pair<X, Y> {
X x;
Y y;
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
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;
}
} | 1 | Plagiarised |
47d54299 | 63bfa731 | // package com.company.Codechef;
// import com.sun.security.jgss.GSSUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class a {
static int depth[];
static ArrayList<Integer> tree[];
static int diameter=0;
public static void main(String[] args) {
FastReader input=new FastReader();
int t=input.nextInt();
while (t-->0){
diameter=0;
int n=input.nextInt();
int a=input.nextInt();
int b=input.nextInt();
int da=input.nextInt();
int db=input.nextInt();
depth=new int[n+1];
tree=new ArrayList[n+1];
for (int i = 0; i <=n ; i++) {
tree[i]=new ArrayList<>();
}
for (int i = 0; i <n-1 ; i++) {
int x=input.nextInt();
int y=input.nextInt();
tree[x].add(y);
tree[y].add(x);
}
dfs(a,0);
// System.out.println(depth[b]);
if (2 * da >= Math.min(diameter, db) ||depth[b]<=da){
System.out.println("Alice");
}else {
System.out.println("Bob");
}
}
}
private static int dfs(int root, int p) {
int len=0;
for(int child:tree[root]){
if (child!=p){
depth[child]=depth[root]+1;
int curr=1+dfs(child,root);
diameter=Math.max(diameter,curr+len);
len=Math.max(len,curr);
}
}
return len;
}
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 D {
static ArrayList<Integer> adj[] = new ArrayList[(int)1e5+7];
static int diameter = 0;
static int[] depth = new int[(int)1e5 + 7];
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt();
while(t-- > 0){
int n = in.nextInt();
int a = in.nextInt(), b = in.nextInt(), da = in.nextInt(), db = in.nextInt();
for(int i = 1; i <= n; i++){
adj[i] = new ArrayList<>();
}
for(int i = 1; i <=n; i++){
adj[i].clear();
}
for(int i = 0; i < n- 1; i++){
int u = in.nextInt();
int v = in.nextInt();
adj[u].add(v);
adj[v].add(u);
}
diameter = 0;
depth[a] = 0;
dfs(a, -1);
System.out.println(2 * da >= Math.min(diameter, db) || depth[b] <= da ? "Alice" : "Bob");
}
}
static int dfs(int node, int parent){
int len = 0;
for(int x : adj[node]){
if(x != parent){
depth[x] = depth[node] + 1;
int cur = 1 + dfs(x, node);
diameter = Math.max(diameter, cur + len);
len = Math.max(len, cur);
// System.out.print("x " + x + " node " + node + " par " + parent);
// System.out.println(" cur " + cur + " len " + len + " diam " + diameter);
}
}
return len;
}
static long getDigitSum(long n)
{
long sum = 0;
while (n > 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
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;
}
}
}
| 1 | Plagiarised |
e185bce5 | f3d7ce08 |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
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) {
Scanner scc = new Scanner(System.in);
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;
static LinkedList<Integer>[] temp, idx;
static long inf = (long) Long.MAX_VALUE;
// static long inf = Long.MAX_VALUE;
// static int max;
public static void main(String[] args) {
int t = sc.nextInt();
// int t = 1;
StringBuilder ret = new StringBuilder();
while(t-- > 0) {
int n = sc.nextInt();
String a = sc.next(), b = sc.next();
int _00 = 0, _01 = 0, _10 = 0, _11 = 0;
for(int i = 0; i < n; i++) {
if(a.charAt(i) == '0' && b.charAt(i) == '0') _00++;
if(a.charAt(i) == '0' && b.charAt(i) == '1') _01++;
if(a.charAt(i) == '1' && b.charAt(i) == '0') _10++;
if(a.charAt(i) == '1' && b.charAt(i) == '1') _11++;
}
int ans = Integer.MAX_VALUE;
if(_10 == _01) ans = _01 + _10;
if(_10 > 0) {
int n00 = _10 - 1;
int n01 = _11;
int n10 = _00 + 1;
int n11 = _01;
if (n01 == n10) {
ans = Math.min(ans, 1 + n01 + n10);
}
}
if(_11 > 0) {
int n00 = _10;
int n01 = _11 - 1;
int n10 = _00;
int n11 = _01 + 1;
if (n01 == n10) {
ans = Math.min(ans, 1 + n01 + n10);
}
}
ret.append(ans == Integer.MAX_VALUE ? -1 : ans);
ret.append("\n");
}
System.out.println(ret);
}
}
| import javax.swing.plaf.IconUIResource;
import java.lang.reflect.Array;
import java.text.CollationElementIterator;
import java.util.*;
import java.io.*;
//Timus judge id- 323935JJ
public class Main {
//----------------------------------------------------------------------------------------------
public static class Pair implements Comparable<Pair> {
int x=0,y=0;
int z=0;
public Pair(int a, int b) {
this.x=a;
this.y=b;
}
public int compareTo(Pair o) {
return this.x - o.x;
}
}
public static int mod = (int) (1e9 + 7);
static int ans = Integer.MAX_VALUE;
public static void main(String hi[]) throws Exception {
FastReader sc = new FastReader();
int t =sc.nextInt();
while(t-->0)
{
int n =sc.nextInt();
String a = sc.nextLine(),b=sc.nextLine();
int count1=0,count2=0,count3=0,count4=0;
for(int i=0;i<n;i++)
{
if(a.charAt(i)=='0'&&b.charAt(i)=='0')
count1++;
else if(a.charAt(i)=='1'&&b.charAt(i)=='1')
count2++;
else if(a.charAt(i)=='1'&&b.charAt(i)=='0')
count3++;
else if(a.charAt(i)=='0'&&b.charAt(i)=='1')
count4++;
}
int ans=Integer.MAX_VALUE;
if(count3==count4)
ans=Math.min(count3*2,ans);
if(count2==count1+1)
ans=Math.min(ans,2*count1+1);
if(ans==Integer.MAX_VALUE)
System.out.println(-1);
else System.out.println(ans);
}
}
static int find(int[] a,int x)
{
if(a[x]==-1)
return x;
else return find(a,a[x]);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// method to return LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static long gcdl(long a, long b)
{
if (a == 0)
return b;
return gcdl(b % a, a);
}
// method to return LCM of two numbers
static long lcml(long a, long b)
{
return (a / gcdl(a, b)) * b;
}
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 |
2a35cd81 | 464ad4dd | import java.util.*;
import java.io.*;
public class Transform_the_experssion {
static Scanner sc=new Scanner(System.in);
static PrintWriter pw=new PrintWriter(System.out);
static ArrayList<Integer> []arr;
public static void main(String[] args) throws IOException, InterruptedException {
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
String [] strings=new String [n];
int [][]arr=new int [5][n];
int [] total=new int [5];
for(int i=0;i<n;i++)
strings[i]=sc.next();
for(int i=0;i<5;i++) {
char x=(char)('a'+i);
for(int j=0;j<n;j++) {
int sum=0;
int end=strings[j].length();
for(int k=0;k<end;k++) {
if(strings[j].charAt(k)==x) {
sum++;
}
}
arr[i][j]=sum-(end-sum);
total[i]+=sum-(end-sum);
}
}
boolean f=false;
for(int i=0;i<5 && !f;i++) {
if(total[i]>0) {
pw.println(n);
f=true;
}
}
//pw.println(Arrays.toString(total));
if(!f) {
int [] MaxS=new int [5];
for(int i=0;i<5;i++) {
Arrays.sort(arr[i]);
if(arr[i][n-1]<=0) {
MaxS[i]=0;
}
else {
int sum=arr[i][n-1],countWords=1;
for(int j=n-2;j>-1 && sum+arr[i][j]>0;j--) {
sum+=arr[i][j];
countWords++;
}
MaxS[i]=countWords;
}
}
int ans=0;
//pw.println(Arrays.toString(MaxS));
for(int i=0;i<5;i++)
ans=Math.max(ans, MaxS[i]);
pw.println(ans);
}
}
pw.flush();
}
/*public class UnionFind {
int[] p, rank, setSize;
int numSets;
public UnionFind(int N)
{
p = new int[numSets = N];
rank = new int[N];
setSize = new int[N];
for (int i = 0; i < N; i++) { p[i] = i; setSize[i] = 1; }
}
public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); }
public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); }
public void unionSet(int i, int j)
{
if (isSameSet(i, j))
return;
numSets--;
int x = findSet(i), y = findSet(j);
if(rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; }
else
{ p[x] = y; setSize[y] += setSize[x];
if(rank[x] == rank[y]) rank[y]++;
}
}
public int numDisjointSets() { return numSets; }
public int sizeOfSet(int i) { return setSize[findSet(i)]; }
}
static class pair implements Comparai)ble<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Double(x).hashCode() * 31 + new Double(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}*/
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
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 long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | import java.io.*;
import java.util.*;
public class Solution{
public static int in = 0, count=0;
static class comparator implements Comparator<int[]>{
public int compare(int[] arr1 ,int[] arr2){
int gain1 =0, gain2=0;
for(int i:arr1) gain1+=i;
for(int i:arr2) gain2+=i;
if((gain1-2*arr1[in])>(gain2-2*arr2[in]))
return 1;
else if((gain1-2*arr1[in])==(gain2-2*arr2[in])) {
count++;
return 0;
}
return -1;
}
}
public static int solve(ArrayList<int[]> al, int[] total){
ArrayList<int[]> c = (ArrayList<int[]>) al.clone();
java.util.Collections.sort(c, new comparator());
int i= al.size()-1;
int sum=0; for(int ii:total) sum+=ii;
int check = total[in];
sum-=check;
for(; i>=0; i--){
if(check>sum) return i+1;
else{
int newSum=0; for(int ii:c.get(i)) newSum+=ii;
newSum-=c.get(i)[in];
sum-=newSum;
check-=c.get(i)[in];
}
}
return 0;
}
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 = Integer.MIN_VALUE;
for(int i=0; i<5; i++) {
in = i;
ans = Math.max(solve(al, total), ans);
count=0;
}
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; }
}
} | 0 | Non-plagiarised |
fadc1365 | fdd85afb | import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class a{
public static void main(String args[]) throws java.lang.Exception{
FastScanner s=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int t=s.nextInt();
for(int tt=0;tt<t;tt++) {
int n=s.nextInt(),k=s.nextInt();
int pos[]=s.readArray(k);
int temp[]=s.readArray(k);
long ans[]=new long[n];
Arrays.fill(ans,Integer.MAX_VALUE);
for(int i=0;i<k;i++){
ans[pos[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++){
out.print(ans[i]+" ");
}
out.println();
}
out.close();
}
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;
}
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 sort(char [] a) {
ArrayList<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);
}
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 gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sortcol(int a[][],int c) {
Arrays.sort(a, (x, y) -> {
if(x[c]>y[c]) {
return 1;
}else {
return -1;
}
});
}
public static void printb(boolean ans) {
if(ans) {
System.out.println("Yes");
}else {
System.out.println("No");
}
}
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();
}
double nextDouble() {
return Double.parseDouble(next());
}
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());
}
}
static class Pair implements Comparable<Pair>{
int a , b;
Pair(int x , int y){
a=x;
b=y;
}
public int compareTo(Pair o) {
return a != o.a ? a - o.a : b - o.b;
}
}
}
| import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
long max=(long)1e18;
StringBuilder sb=new StringBuilder();
while(t-->0){
int n=sc.nextInt();
int k=sc.nextInt();
long[] ans=new long[n+1];
long[] tem=new long[n+1];
int[] item=new int[k];
Arrays.fill(ans,max);
for(int i=0;i<k;i++){
item[i]=sc.nextInt();
}
for(int i=0;i<k;i++){
tem[item[i]]=sc.nextLong();
}
for(int i=n;i>=1;i--){
if(tem[i]!=0){
if(i==n)ans[i]=Math.min(ans[i],tem[i]);
else{ ans[i]=Math.min(ans[i],ans[i+1]+1);
ans[i]=Math.min(tem[i],ans[i]);
}
}
else{
if(i<n)ans[i]=Math.min(ans[i],ans[i+1]+1);
}
}
for(int i=1;i<=n;i++){
if(tem[i]!=0){
if(i==1)ans[i]=Math.min(ans[i],tem[i]);
else{ ans[i]=Math.min(ans[i],ans[i-1]+1);
ans[i]=Math.min(tem[i],ans[i]);
}
}
else{
if(i>1)ans[i]=Math.min(ans[i],ans[i-1]+1);
}
}
for(int i=1;i<=n;i++){
sb.append(ans[i]+" ");
}
sb.append("\n");
}
System.out.println(sb);
}
} | 0 | Non-plagiarised |
1f6b81b1 | 51151974 | import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class InterestingStory {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
int t = sc.nextInt();
// int t = 1;
while (t-- != 0) {
solver.solve(sc, out);
}
out.close();
}
static class Solver {
public void solve(InputReader sc, PrintWriter out) {
int n = sc.nextInt();
char[][] words = new char[n][];
for(int i = 0; i < n; i++) words[i] = sc.next().toCharArray();
int[][] arr = new int[n][];
for(int i = 0; i < n; i++) {
arr[i] = new int[words[i].length];
for(int j = 0; j < arr[i].length; j++) arr[i][j] = words[i][j]-'a';
}
int max = 0;
for(int now = 0; now < 5; now++) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i < n; i++) {
int nowcnt = 0;
int othercnt = 0;
for(int j = 0; j < arr[i].length; j++) {
if(arr[i][j]==now) nowcnt++;
else othercnt++;
}
pq.add(nowcnt-othercnt);
}
int canTake = 0;
int sum = 0;
while(!pq.isEmpty()) {
int nowAdd = pq.poll();
if(sum+nowAdd>0) {
sum += nowAdd;
canTake++;
} else {
break;
}
}
max = Math.max(max,canTake);
}
out.println(max);
}
}
static void sort(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sort(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sortDec(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static void sortDec(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public 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 String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
}
| 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; }
}
} | 0 | Non-plagiarised |
034030f3 | bf992c91 | import java.util.*;
import java.io.*;
public class D_1525 {
static int INF = (int)1e9;
static int n, m;
static int[] full, free;
static int[][] memo;
public static int dp(int i, int j) {
if(i == n)
return 0;
if(j == m)
return INF;
if(memo[i][j] != -1)
return memo[i][j];
return memo[i][j] = Math.min(dp(i, j + 1), Math.abs(free[j] - full[i]) + dp(i + 1, j + 1));
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int l = sc.nextInt();
int[] array = sc.nextIntArray(l);
n = 0;
for(int i = 0; i < l; i++)
if(array[i] == 1)
n++;
m = l - n;
full = new int[n];
free = new int[m];
int ind1 = 0, ind2 = 0;
for(int i = 0; i < l; i++)
if(array[i] == 0)
free[ind2++] = i;
else
full[ind1++] = i;
memo = new int[n][m];
for(int[] i : memo)
Arrays.fill(i, -1);
pw.println(dp(0, 0));
pw.flush();
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++)
array[i] = new Integer(nextInt());
return array;
}
public long[] nextLongArray(int n) throws IOException {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
public static int[] shuffle(int[] a) {
int n = a.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
int tmpIdx = rand.nextInt(n);
int tmp = a[i];
a[i] = a[tmpIdx];
a[tmpIdx] = tmp;
}
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
| import java.util.Arrays;
import java.util.Scanner;
public class P1525D {
public static int[] ones, zeros;
public static int[][] memo;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] arr = new int[n];
int oneCount = 0;
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
oneCount += arr[i] % 2;
}
int o = 0, z = 0;
ones = new int[oneCount];
zeros = new int[n - oneCount];
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
ones[o++] = i;
} else {
zeros[z++] = i;
}
}
memo = new int[oneCount][n - oneCount];
for (int[] row : memo) Arrays.fill(row, -1);
System.out.println(dp(0, 0));
}
public static int dp(int oi, int zi) {
if (oi == ones.length) {
return 0;
} else if (zi == zeros.length) {
return 100000000;
} else {
if (memo[oi][zi] == -1) {
memo[oi][zi] = Math.min(Math.abs(ones[oi] - zeros[zi]) + dp(oi + 1, zi + 1), dp(oi, zi + 1));
}
return memo[oi][zi];
}
}
}
| 1 | Plagiarised |
5b9a0551 | db1ef8b3 | 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());
}
}
}
| import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
public class C {
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
public static void process() throws IOException {
int n = sc.nextInt();
PriorityQueue<Integer> odd = new PriorityQueue<Integer>();
PriorityQueue<Integer> even = new PriorityQueue<Integer>();
int arr[] = sc.readArray(n);
long min = INF;
long sumOdd = 0;
long sumEven = 0;
for (int i = 0; i < n; i++) {
int x = arr[i];
if (i % 2 == 0) {
even.add(x);
sumEven += x;
} else {
odd.add(x);
sumOdd += x;
}
if (i > 0) {
long ans = 0;
ans += 1l * even.peek() * (n - (even.size()));
ans += 1l * odd.peek() * (n - (odd.size()));
ans += sumOdd + sumEven;
min = Math.min(ans, min);
}
}
System.out.println(min);
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
static FastScanner sc;
static PrintWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new PrintWriter(System.out);
} else {
sc = new FastScanner(100);
out = new PrintWriter("output.txt");
}
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
process();
}
out.flush();
out.close();
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (!(o instanceof Pair)) return false;
// Pair key = (Pair) o;
// return x == key.x && y == key.y;
// }
//
// @Override
// public int hashCode() {
// int result = x;
// result = 31 * result + y;
// return result;
// }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static void println(Object o) {
out.println(o);
}
static void println() {
out.println();
}
static void print(Object o) {
out.print(o);
}
static void pflush(Object o) {
out.println(o);
out.flush();
}
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));
}
static int max(int x, int y) {
return Math.max(x, y);
}
static int min(int x, int y) {
return Math.min(x, y);
}
static int abs(int x) {
return Math.abs(x);
}
static long abs(long x) {
return Math.abs(x);
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
static long max(long x, long y) {
return Math.max(x, y);
}
static long min(long x, long y) {
return Math.min(x, y);
}
public static int gcd(int a, int b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public static long gcd(long a, long b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastScanner(int a) throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) throws IOException {
int[] A = new int[n];
for (int i = 0; i != n; i++) {
A[i] = sc.nextInt();
}
return A;
}
long[] readArrayLong(int n) throws IOException {
long[] A = new long[n];
for (int i = 0; i != n; i++) {
A[i] = sc.nextLong();
}
return A;
}
}
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);
}
}
| 0 | Non-plagiarised |
3e28571b | 77b5c134 | import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import static java.lang.Math.*;
public class Ans implements Runnable {
public static void main(String args[]) {
Ans s = new Ans();
s.run();
}
static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
InputReader sc = null;
PrintWriter pw = null;
static ArrayList<Integer>[] G = new ArrayList[(int)(3e5+10)];
static int[] dist = new int[(int)(3e5+10)];
static int[] a = new int[(int)(3e5+10)];
private static int[] nge(int n){
int[] edges = new int[n];
Arrays.fill(edges, -1);
Stack<Integer> st = new Stack<>();
st.push(0);
for(int i = 1; i < n; i++){
while(!st.isEmpty() && a[i] >= a[st.peek()]){
edges[st.pop()] = i;
}
st.push(i);
}
//debug("nge", edges);
return edges;
}
private static int[] nle(int n){
int[] edges = new int[n];
Arrays.fill(edges, -1);
Stack<Integer> st = new Stack<>();
st.push(0);
for(int i = 1; i < n; i++){
while(!st.isEmpty() && a[i] <= a[st.peek()]){
edges[st.pop()] = i;
}
st.push(i);
}
//debug("nle", edges);
return edges;
}
private static int[] pge(int n){
int[] edges = new int[n];
Arrays.fill(edges, -1);
Stack<Integer> st = new Stack<>();
st.push(n-1);
for(int i = n-2; i >= 0; i--){
while(!st.isEmpty() && a[i] >= a[st.peek()]){
edges[st.pop()] = i;
}
st.push(i);
}
//debug("pge", edges);
return edges;
}
private static int[] ple(int n){
int[] edges = new int[n];
Arrays.fill(edges, -1);
Stack<Integer> st = new Stack<>();
st.push(n-1);
for(int i = n-2; i >= 0; i--){
while(!st.isEmpty() && a[i] <= a[st.peek()]){
edges[st.pop()] = i;
}
st.push(i);
}
//debug("ple", edges);
return edges;
}
private static void buildGraph(int[] edges){
for(int i = 0; i < edges.length; i++){
if(edges[i] != -1){
// G[i].add(edges[i]);
// G[edges[i]].add(i);
G[min(i, edges[i])].add(max(i, edges[i]));
}
}
}
private static void bfs(int n){
dist[0] = 0;
ArrayDeque<Integer> q = new ArrayDeque<>();
q.add(0);
while(!q.isEmpty()){
int front = q.pollFirst();
if(front == n-1){
break;
}
for(int adj : G[front]){
if(dist[adj] == (int)(1e9)){
dist[adj] = 1 + dist[front];
q.add(adj);
}
}
}
}
public void run() {
// InputStream is;
// is = new FileInputStream(new File("input.txt"));
sc = new InputReader(System.in);
pw = new PrintWriter(System.out);
int n = sc.nextInt();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
G[i] = new ArrayList<Integer>();
}
buildGraph(nge(n));
buildGraph(nle(n));
buildGraph(ple(n));
buildGraph(pge(n));
Arrays.fill(dist, (int)(1e9));
bfs(n);
pw.println(dist[n-1]);
// is.close();
pw.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[5];
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(stream));
String stock = "";
try {
stock = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return stock;
}
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.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
// https://codeforces.com/contest/1407/problem/D
public class Discrete_Centrifugal_Jumps {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] h = new int[n];
for (int i = 0; i < n; i++) h[i] = scan.nextInt();
System.out.println(getMinJump(h, n));
}
static int getMinJump(int[] h, int n) {
int[] rightG = new int[n], leftG = new int[n];
int[] rightS = new int[n], leftS = new int[n];
Arrays.fill(rightG, -1); Arrays.fill(rightS, -1);
Arrays.fill(leftG, -1); Arrays.fill(leftS, -1);
Stack<Integer> stack = new Stack<>();
// next greater in the right segment
int i = 0;
while (i < n) {
if (!stack.empty() && h[i] >= h[stack.peek()]) rightG[stack.pop()] = i;
else stack.push(i++);
}
stack = new Stack<>();
// next smaller in the right segment
i = 0;
while (i < n) {
if (!stack.empty() && h[i] <= h[stack.peek()]) rightS[stack.pop()] = i;
else stack.push(i++);
}
stack = new Stack<>();
// next greater in left segment
i = n-1;
while (i >= 0) {
if (!stack.empty() && h[i] >= h[stack.peek()]) leftG[stack.pop()] = i;
else stack.push(i--);
}
stack = new Stack<>();
// next smaller in left segment
i = n-1;
while (i >= 0) {
if (!stack.empty() && h[i] <= h[stack.peek()]) leftS[stack.pop()] = i;
else stack.push(i--);
}
ArrayList<Integer>[] jump = new ArrayList[n];
for (i = 0; i < n; i++) jump[i] = new ArrayList<>();
for (i = 0; i < n; i++) {
// max(h[i+1] ... h[j-1]) < min(h[i], h[j])
if (rightG[i] != -1) jump[i].add(rightG[i]);
if (leftG[i] != -1) jump[leftG[i]].add(i);
// max(h[i], h[j]) < min(h[i+1] ... h[j])
if (rightS[i] != -1) jump[i].add(rightS[i]);
if (leftS[i] != -1) jump[leftS[i]].add(i);
}
int[] dp = new int[n]; Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int u = 0; u < n; u++) {
for (int v: jump[u]) {
dp[v] = Math.min(dp[v], dp[u] + 1);
}
}
return dp[n-1];
}
}
| 0 | Non-plagiarised |
63a24497 | 6e5ab1d2 | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
void solve() {
int n = in.nextInt();
char[] a = in.nextLine().toCharArray();
char[] b = in.nextLine().toCharArray();
int ans = MAX;
ans = Math.min(ans, operations(a, b));
ans = Math.min(ans, change(a, b, '1'));
ans = Math.min(ans, change(a, b, '0'));
if (ans == MAX)ans = -1;
out.append(ans + "\n");
}
int operations(char[] a, char[] b) {
int count01 = 0 , count10 = 0;
int n = a.length;
for (int i = 0 ; i < n; i++) {
if (a[i] != b[i]) {
if (a[i] == '0')count01++;
else count10++;
}
}
if (count01 != count10)return MAX;
return count01 + count10;
}
int change(char[] a, char[] b , char ch) {
int n = a.length;
char[] c = new char[n];
for (int i = 0 ; i < n; i++)c[i] = a[i];
int index = -1;
for (int i = 0; i < n; i++) {
if (c[i] == '1' && b[i] == ch) {
index = i;
break;
}
}
if (index == -1)return MAX;
for (int i = 0 ; i < n; i++) {
if (i == index)continue;
c[i] = (char)( '0' + ('1' - c[i]) );
}
int local = operations(c, b);
if (local == MAX)return MAX;
return 1 + local;
}
public static void main (String[] args) {
// It happens - Syed Mizbahuddin
Main sol = new Main();
int t = 1;
t = in.nextInt();
while (t-- != 0) {
sol.solve();
}
System.out.print(out);
}
<T> void println(T[] s) {
if (err == null)return;
err.println(Arrays.toString(s));
}
<T> void println(T s) {
if (err == null)return;
err.println(s);
}
void println(int s) {
if (err == null)return;
err.println(s);
}
void println(int[] a) {
if (err == null)return;
println(Arrays.toString(a));
}
int[] array(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
return a;
}
int[] array1(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
return a;
}
int max(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; i++)max = Math.max(max, a[i]);
return max;
}
int min(int[] a) {
int min = a[0];
for (int i = 0; i < a.length; i++)min = Math.min(min, a[i]);
return min;
}
int count(int[] a, int x) {
int count = 0;
for (int i = 0; i < a.length; i++)if (x == a[i])count++;
return count;
}
void printArray(int[] a) {
for (int ele : a)out.append(ele + " ");
out.append("\n");
}
static {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
err = new PrintStream(new FileOutputStream("error.txt"));
} catch (Exception e) {}
}
static FastReader in;
static StringBuilder out;
static PrintStream err;
final int MAX;
final int MIN;
int mod ;
Main() {
in = new FastReader();
out = new StringBuilder();
MAX = Integer.MAX_VALUE;
MIN = Integer.MIN_VALUE;
mod = (int)1e9 + 7;
}
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 Pair implements Comparable<Pair> {
int first , second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair b) {
return this.first - b.first;
}
public String toString() {
String s = "{ " + Integer.toString(first) + " , " + Integer.toString(second) + " }";
return s;
}
}
}
| import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
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);
CMenorah solver = new CMenorah();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CMenorah {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
char[] a = in.next().toCharArray();
char[] b = in.next().toCharArray();
int ans = checkPairs(a, b, -1);
ans = Math.min(ans, changePair(a, b, '1'));
ans = Math.min(ans, changePair(a, b, '0'));
if (ans == Integer.MAX_VALUE) ans = -1;
out.println(ans);
}
int changePair(char[] a, char[] b, char t) {
int index = -1;
int n = a.length;
for (int i = 0; i < n; ++i) {
if (a[i] == '1' && b[i] == t) {
index = i;
break;
}
}
return checkPairs(a, b, index);
}
int checkPairs(char[] a, char[] b, int changeStringIndex) {
int n = a.length;
int val = 0;
char[] tmp = new char[n];
System.arraycopy(a, 0, tmp, 0, n);
if (changeStringIndex != -1) {
val = 1;
for (int i = 0; i < n; ++i) {
if (i == changeStringIndex) continue;
tmp[i] = a[i] == '0' ? '1' : '0';
}
}
int _10 = 0, _01 = 0;
for (int i = 0; i < n; ++i) {
if (tmp[i] != b[i]) {
if (tmp[i] == '0') _01++;
else _10++;
}
}
return _01 == _10 ? 2 * _01 + val : Integer.MAX_VALUE;
}
}
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);
}
}
}
| 1 | Plagiarised |
b2001d68 | ba468e1f |
/*
بسم الله الرحمن الرحيم
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$
|__ $$ /$$__ $$ |$$ |$$ /$$__ $$
| $$| $$ \ $$| $$|$$| $$ \ $$
| $$| $$$$$$$$| $$ / $$/| $$$$$$$$
/ $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$
| $$ | $$| $$ | $$ \ $$$/ | $$ | $$
| $$$$$$/| $$ | $$ \ $/ | $$ | $$
\______/ |__/ |__/ \_/ |__/ |__/
/$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$
| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$
| $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/
| $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$
| $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$
| $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$
|__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class D753 {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
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();
}
char[] color=sc.next().toCharArray();
ArrayList<Integer> b=new ArrayList<>();
ArrayList<Integer> r=new ArrayList<>();
for(int i=0;i<n;i++){
if(color[i]=='B')b.add(arr[i]);
else r.add(arr[i]);
}
Collections.sort(b);
Collections.sort(r);
boolean ok=true;
int cur=1;
for(int i:b){
if(i>=cur)cur++;
else{
ok=false;
break;
}
//cur++;
}
for(int i:r){
if(i<=cur)cur++;
else{
ok=false;
break;
}
}
if(ok)System.out.println("YES");
else System.out.println("NO");
}
} catch (Exception e) {
return;
}
}
public static int lowerbound(long[] ar,int k)
{
int s=0;
int e=ar.length;
while (s !=e)
{
int mid = s+e>>1;
if (ar[mid] <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.length)
{
return -1;
}
return s;
}
public static class pair {
int ff;
char ss;
pair(int ff, char ss) {
this.ff = ff;
this.ss = ss;
}
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
public static int upperbound(long[] arr, int k) {
int s = 0;
int e = arr.length;
while (s != e) {
int mid = s + e >> 1;
if (arr[mid] <= k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == arr.length) {
return -1;
}
return s;
}
public static long pow(long x,long y,long mod){
if(x==0)return 0l;
if(y==0)return 1l;
//(x^y)%mod
if(y%2l==1l){
return ((x%mod)*(pow(x,y-1l,mod)%mod))%mod;
}
return pow(((x%mod)*(x%mod))%mod,y/2l,mod);
}
public static long gcd_long(long a, long b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_long(b, a % b);
}
public static int gcd_int(int a, int b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_int(b, a % b);
}
public static int lcm(int a, int b) {
int gcd = gcd_int(a, b);
return (a * b) / gcd;
}
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());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
* return false; }
*/
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Codeforces {
public static void main(String[] args) {
FastReader fastReader = new FastReader();
int t = fastReader.nextInt();
while (t-- > 0) {
int n = fastReader.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = fastReader.nextInt();
}
ArrayList<Integer> b = new ArrayList<>();
ArrayList<Integer> r = new ArrayList<>();
char c[] = fastReader.next().toCharArray();
for (int i = 0; i < n; i++) {
if (c[i] == 'B') {
b.add(a[i]);
} else {
r.add(a[i]);
}
}
Collections.sort(b);
Collections.sort(r);
int sizeb = b.size();
boolean isValid = true;
for (int i = 1 , j = 0; i <=sizeb; i++ , j++){
if (b.get(j) < i){
isValid =false;
}
}
for (int i = sizeb+1 , j = 0; i <=n && j < r.size(); i++ , j++){
if (r.get(j) > i){
isValid =false;
}
}
if (isValid){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
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 |
ac180326 | b1e9f1f6 | //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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class test {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
String[] st = br.readLine().split(" ");
int n = Integer.parseInt(st[0]);
int m = Integer.parseInt(st[1]);
int x = Integer.parseInt(st[2]);
int[] arr = new int[n];
st = br.readLine().split(" ");
Pair[] temp = new Pair[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st[i]);
temp[i] = new Pair(arr[i], i);
}
int bcount = n / m;
int extra = n % m;
int[] ans = new int[n];
sort(temp);
int id = 0;
int minmax = 0;
boolean flag = false;
for (int i = 0; i < bcount; i++) {
minmax -= temp[id].val;
for (int j = 0; j < m; j++) {
ans[temp[id].idx] = j + 1;
if (j == m) {
minmax += temp[id].val;
}
id++;
}
if (minmax > x) {
sb.append("NO\n");
flag = true;
break;
}
}
for (int i = 0; i < extra; i++) {
ans[temp[id].idx] = i + 1;
id++;
}
if (flag == false) {
sb.append("YES\n");
for (int i = 0; i < n; i++) {
sb.append(ans[i] + " ");
}
sb.append("\n");
}
}
System.out.println(sb);
}
public static class Pair implements Comparable<Pair> {
int val;
int idx;
public Pair(int val, int idx) {
this.val = val;
this.idx = idx;
}
@Override
public int compareTo(Pair o) {
return this.val - o.val;
}
}
public static void sort(Pair[] arr) {
ArrayList<Pair> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
list.add(new Pair(arr[i].val, arr[i].idx));
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
}
| 0 | Non-plagiarised |
3f939694 | 54488276 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[])throws Exception{
Input sc=new Input();
StringBuilder sb=new StringBuilder();
int t=sc.readInt();
for(int f=0;f<t;f++){
int n=sc.readInt();
String str[]=new String[n];
for(int i=0;i<n;i++){
str[i]=sc.readString();
}
char ch[]={'a','b','c','d','e'};
int ans=0;
for(int i=0;i<5;i++){
char c=ch[i];
ArrayList<pair> lst=new ArrayList<>();
int a[]=new int[n];
for(int j=0;j<n;j++)
{
int count=0;
for(int k=0;k<str[j].length();k++){
if(str[j].charAt(k)==c)
count++;
}
lst.add(new pair(count,(str[j].length()-count)));
}
Collections.sort(lst);
int s1=0;int s2=0;
// for(int j=0;j<lst.size();j++){
// System.out.println(lst.get(j).a+" "+lst.get(j).b);
// }
// System.out.println("--------");
int co=0;
for(int j=0;j<lst.size();j++){
int v1=lst.get(j).a;
int v2=lst.get(j).b;
if((s1+v1)>(s2+v2)){
s1+=v1;
s2+=v2;
co++;
}
//System.out.println(lst.get(j).a+" "+lst.get(j).b);
}
ans=Math.max(co,ans);
}
// count all 'a' in each string
sb.append(ans+"\n");
}
System.out.print(sb);
}
}
class pair implements Comparable<pair>{
int a;
int b;
pair(int a,int b){
this.a=a;
this.b=b;
}
@Override
public int compareTo(pair o) {
// if(this.a!=o.a){
// return o.a-this.a;
// }else
// return this.b-o.b;
return -(this.a-this.b)+(o.a-o.b);
}
}
class Input{
BufferedReader br;
StringTokenizer st;
Input(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public int[] readArray() throws Exception{
st=new StringTokenizer(br.readLine());
int a[]=new int[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Integer.parseInt(st.nextToken());
}
return a;
}
public long[] readArrayLong() throws Exception{
st=new StringTokenizer(br.readLine());
long a[]=new long[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Long.parseLong(st.nextToken());
}
return a;
}
public int readInt() throws Exception{
st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
public long readLong() throws Exception{
st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
public String readString() throws Exception{
return br.readLine();
}
public int[][] read2dArray(int n,int m)throws Exception{
int a[][]=new int[n][m];
for(int i=0;i<n;i++){
st=new StringTokenizer(br.readLine());
for(int j=0;j<m;j++){
a[i][j]=Integer.parseInt(st.nextToken());
}
}
return a;
}
}
| import java.io.*;
import java.util.*;
public class C {
static int n;
public static void main (String[] args) throws IOException {
Kattio io = new Kattio();
int t = io.nextInt();
for (int ii=0; ii<t; ii++) {
n = io.nextInt();
String[] arr = new String[n];
for (int i=0; i<n; i++) {
String str = io.next();
arr[i] = str;
}
char[] chars = new char[]{'a','b','c','d','e'};
int ans = -1;
for (int i=0; i<5; i++) {
ans = Math.max(ans, solve(arr, chars[i]));
}
System.out.println(ans);
}
}
static int solve(String[] arr, char c) {
//System.out.println("Comparing based on " + c);
Arrays.sort(arr, new Comp(c));
int good = 0;
int total = 0;
int ret = 0;
for (int i=0; i<n; i++) {
//System.out.println(good + " " + total);
for (int j=0; j<arr[i].length(); j++) {
if (arr[i].charAt(j) == c) good++;
}
total += arr[i].length();
if (2 * good > total) {
ret++;
} else {
return ret;
}
}
return ret;
}
static class Comp implements Comparator<String> {
char c;
public Comp (char c) {
this.c = c;
}
public int compare(String a, String b) {
double cnt1 = 0;
double cnt2 = 0;
for (int i=0; i<a.length(); i++) {
if (a.charAt(i) == c) {
cnt1++;
}
}
for (int i=0; i<b.length(); i++) {
if (b.charAt(i) == c) {
cnt2++;
}
}
//higher ratio is better
return -Double.compare(cnt1 - (a.length() - cnt1), cnt2 - (b.length() - cnt2));
}
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
} | 0 | Non-plagiarised |
9310ad0c | f36138e7 | 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);
}
}
}
| import java.io.*;
import java.util.*;
public class a {
public static void main(String[] args){
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
long time[] = new long[n];
long health[] = new long[n];
for(int i=0; i<n; i++){
long temp = sc.nextLong();
time[i] = temp;
}
for(int i=0; i<n; i++){
long temp = sc.nextLong();
health[i] = temp;
}
long ans = 0L;
int peak = n-1;
int end = n-1;
for(int i=n-1; i>=0; i--){
if(i == n-1){
ans += (health[i]*(health[i]+1))/2;
continue;
}
if(health[peak] - (time[peak]-time[i]) <= 0){
peak = i;
end = i;
ans += (health[i]*(health[i]+1))/2;
}
else if(health[peak] - (time[peak]-time[i]) < health[i]){
long val = health[peak] + (time[end]-time[peak]);
ans -= (val*(val+1))/2;
//System.out.println("Val1: " + val);
val = health[i] + (time[end]-time[i]);
ans += (val*(val+1))/2;
//System.out.println("Val2: " + val);
peak = i;
}
}
System.out.println(ans);
}
}
}
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);
}
}
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[] 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 |
30e0cc81 | b55888de | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.lang.*;
public class Main {
static class sortLong implements Comparator<long[]>{
public int compare(long[] a,long[] b){
if(a[0] > b[0]) return 1;return -1;
}
}
static class sortInt implements Comparator<int[]>{
public int compare(int[] a,int[] b){
if(a[1] > b[1]) return 1;return -1;
}
}
public static String[] F(BufferedReader bf) throws Exception
{
return (bf.readLine().split(" "));
}
public static void pr(PrintWriter out,Object o)
{
out.println(o.toString());//out.flush();
}
public static void prW(PrintWriter out,Object o)
{
out.print(o.toString());//out.flush();
}
public static int intIn(String st)
{
return Integer.parseInt(st);
}
public static void pr(Object o)
{
System.out.println(o.toString());
}
public static void prW(Object o)
{
System.out.print(o.toString());
}
public static int inInt(String s)
{
return Integer.parseInt(s);
}
public static long in(String s)
{
return Long.parseLong(s);
}
static int[] toIntArray(String[] m)
{
int[] p=new int[m.length];
for(int o=0;o<m.length;o++)
{
p[o]= inInt(m[o]);
}
return p;
}
static double[] toDArray(String[] m)
{
double[] p=new double[m.length];
for(int o=0;o<m.length;o=0)
{
p[o]= Double.parseDouble(m[o]);
}
return p;
}
static long[] toLArray(String[] m)
{
long[] p=new long[m.length];
for(int o=0;o<m.length;o++)
{
p[o]= in(m[o]);
}
return p;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long pow(long x, long y, long p)
{
if(y == 0) return 1l;
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0l; // 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;
}
static long __gcd(long n1, long n2)
{
if(n1==0l) return n2;
if(n2==0l) return n1;
if(n1==1l || n2==1l) return 1l;
// long gcd = 1;
if(n1 == n2) return n1;
if(n1>n2) return __gcd(n1%n2,n2);
return __gcd(n1,n2%n1);
}
public static int F(String[] arr,char ch){
int[] nums = new int[arr.length];
for(int i=0;i<arr.length;i++){
String str = arr[i];int a=0;
for(int j=0;j<str.length();j++){
if(str.charAt(j) == ch){
a++;
}
}
//int b = (str.length()/2);
// if((str.length()%2) != 0) b++;
nums[i] = (2*a - (str.length()));
}
Arrays.sort(nums);
int sum = 0;
for(int i=arr.length-1;i>=0;i--){
if((sum+nums[i]) <= 0){
return arr.length-1-i;
}
sum += nums[i];
}
return arr.length;
}
public static void main (String[] args) throws Exception {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);;;//
//int[] map=new int[1000001];
int yy=inInt(bf.readLine());
for(int w=0;w<yy;w++)
{
//String str = bf.readLine();
out.flush();
String[] xlp = bf.readLine().split(" ");
//String st = bf.readLine();
int n;//boolean bol=false;
// m;//long a,b,c;
long k;long l;
// int l;
//int double avg k;//pr(out,"vbc");
// boolean bol = false;
//long mod=1000000000+7
n =inInt(xlp[0]);//int m = inInt(xlp[1]);//long b=in(xlp[3]);//long k=in(xlp[4]);
String[] arr = new String[n];
for(int i=0;i<n;i++){
arr[i] = bf.readLine();
}
int ans=0;
for(int i=0;i<5;i++){
int v = F(arr,(char)(i+'a'));
// pr(out,v+" "+i);
ans = Math.max(ans,F(arr,(char)(i+'a')));
}
pr(out,ans);
}
out.close();//
bf.close();//
}}
/*
Kickstart
String rp;
rp = "Case #"+(w+1)+": "+(n-ans)+" ";
static int[][] dir={{0,1},{1,0},{-1,0},{0,-1}};
static class SegmentTreeRMQ
{
int st[];
int minVal(int x, int y) {
return (x > y) ? x : y;
}
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int RMQUtil(int ss, int se, int qs, int qe, int index)
{
if (qs <= ss && qe >= se)
return st[index];
// If segment of this node is outside the given range
if (se < qs || ss > qe)
return Integer.MIN_VALUE;
// If a part of this segment overlaps with the given range
int mid = getMid(ss, se);
return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1),
RMQUtil(mid + 1, se, qs, qe, 2 * index + 2));
}
// Return minimum of elements in range from index qs (query
// start) to qe (query end). It mainly uses RMQUtil()
int RMQ(int n, int qs, int qe)
{
// Check for erroneous input values
return RMQUtil(0, n - 1, qs, qe, 0);
}
// A recursive function that constructs Segment Tree for
// array[ss..se]. si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int si)
{
// If there is one element in array, store it in current
// node of segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then recur for left and
// right subtrees and store the minimum of two values in this node
int mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, si * 2 + 2));
return st[si];
}
void con(int arr[])
{
// Allocate memory for segment tree
//Height of segment tree
int n = (arr.length);
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
//Maximum size of segment tree
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // allocate memory
// Fill the allocated memory st
constructSTUtil(arr, 0, n - 1, 0);
}
}
static class DSU {
int[] p;int[] sz;int op;int c;;
int[] last;
public void G(int n)
{
last=new int[n];
p=new int[n];
sz=new int[n];c=n;
op=n;
for(int h=0;h<n;h++)
{
sz[h]=1;p[h]=h;
last[h]=h;
}
}
public int find(int x)
{
int y=x;
while(x!=p[x]) x=p[x];
while(y!=p[y])
{
int tem=p[y];
p[y]=x;y=tem;
}
return p[y];
}
public void union(int a,int b)
{
int x,y;
x=find(a);y=find(b);
if(x==y) return;
if(sz[x]>sz[y])
{
p[y] = x;
sz[x]+=sz[y];
last[x]=Math.max(last[x],last[y]);
}
else
{
p[x]=y;sz[y]+=sz[x];
last[y]=Math.max(last[y],last[x]);
}
c--;
}}
static long pow(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 0l; // 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;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int gcd(int a, int b,int o)
{
if (b == 0)
return a;
return gcd(b, a % b,o);
}
Geometric median
public static double F(double[] x,double[] w)
{
double d1,d2;
double S=0.00;
for(double dp : w) S += dp;
int k = 0;
double sum = S - w[0]; // sum is the total weight of all `x[i] > x[k]`
while(sum > S/2)
{
++k;
sum -= w[k];
}
d1=x[k];
return d1;
k = w.length-1;
sum = S - w[k]; // sum is the total weight of all `x[i] > x[k]`
while(sum > S/2)
{
--k;
sum -= w[k];
}
d2=x[k];
return new double[]{d1,d2};
}
*/ | import java.lang.reflect.Array;
import java.util.*;
public class Rough {
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 s[] = new String[n];
int f[][] = new int[n][5];
for (int i = 0; i < n; i++) {
s[i] = sc.nextLine();
for (int j = 0; j < s[i].length(); j++) {
f[i][s[i].charAt(j)-'a']++;
}
}
int ans = 0;
for ( int i = 0; i < 5; i++) {
ArrayList<Integer> al = new ArrayList<>();
for (int j = 0; j < n; j++) {
int o = 0;
for (int k = 0; k < 5; k++) {
if(k != i)
o+=f[j][k];
}
al.add(f[j][i]-o);
}
Collections.sort(al,Collections.reverseOrder());
int max = 0;
int x = 0;
for (int j = 0; j < n; j++) {
x+=al.get(j);
if(x<=0)break;
max++;
}
ans = Math.max(max,ans);
}
System.out.println(ans);
}
sc.close();
}
} | 0 | Non-plagiarised |
3c667d4f | be3b1289 | 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.util.*;
import java.lang.*;
import java.io.*;
public class cf {
static PrintWriter out;
static int MOD = 1000000007;
static FastReader scan;
/*-------- I/O using 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;
}
}
public static ArrayList<Integer> 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.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] 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;
}
}
ArrayList<Integer> arr = new ArrayList<>();
// Print all prime numbers
for (int i = 2; i <= n; i++) {
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// 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) {
String sex = Long.toString(n);
return sex.length();
}
static void reverse(int a[]) {
int i, k, t;
int n = a.length;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static final Random random = new Random();
//Method for sorting
static void ruffleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int j = random.nextInt(n);
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static void sortadv(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long value : a) {
l.add(value);
}
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a[i] = l.get(i);
}
//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;
}
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();
int k=ni();
int[] a=new int[k];
int[] temp=new int[k];
iIA(a);
iIA(temp);
long dp[]=new long[n];
Arrays.fill(dp,Integer.MAX_VALUE);
for(int i=0;i<k;i++){
dp[a[i]-1]=temp[i];
}
//iPA(dp);
for(int i=1;i<n;i++){
dp[i]=Math.min(dp[i-1]+1,dp[i]);
}
//iPA(dp);
for(int i=n-2;i>=0;i--){
dp[i]=Math.min(dp[i+1]+1,dp[i]);
}
lPA(dp);
//pn("");
}
out.flush();
out.close();
}
}
| 1 | Plagiarised |
2bbf754b | 3a12e509 | import java.util.*;
/**
__ __
( _) ( _)
/ / \\ / /\_\_
/ / \\ / / | \ \
/ / \\ / / |\ \ \
/ / , \ , / / /| \ \
/ / |\_ /| / / / \ \_\
/ / |\/ _ '_| \ / / / \ \\
| / |/ 0 \0\ / | | \ \\
| |\| \_\_ / / | \ \\
| | |/ \.\ o\o) / \ | \\
\ | /\\`v-v / | | \\
| \/ /_| \\_| / | | \ \\
| | /__/_ `-` / _____ | | \ \\
\| [__] \_/ |_________ \ | \ ()
/ [___] ( \ \ |\ | | //
| [___] |\| \| / |/
/| [____] \ |/\ / / ||
( \ [____ / ) _\ \ \ \| | ||
\ \ [_____| / / __/ \ / / //
| \ [_____/ / / \ | \/ //
| / '----| /=\____ _/ | / //
__ / / | / ___/ _/\ \ | ||
(/-(/-\) / \ (/\/\)/ | / | /
(/\/\) / / //
_________/ / /
\____________/ (
*/
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[] arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
String str=sc.next();
ArrayList<Pair> plist=new ArrayList<>();
for(int i=0;i<n;i++) {
char ch=str.charAt(i);
plist.add(new Pair(arr[i],ch));
}
//B-reduce
//R-increse
Collections.sort(plist);
int counter=1;
boolean flag=false;
for(int i=0;i<plist.size();i++) {
int val=plist.get(i).number;
int clr=plist.get(i).color;
if(clr=='B') {
if(val<counter) {
flag=true;
break;
}
}
else {
if(val>counter) {
flag=true;
break;
}
}
counter++;
}
System.out.println(flag?"NO":"YES");
}
}
public static class Pair implements Comparable<Pair>{
int number;
char color;
Pair(int number,char color){
this.number=number;
this.color=color;
}
@Override
public int compareTo(Pair o) {
if(o.color==this.color) {
return this.number-o.number;
}
else {
return this.color-o.color;
}
}
public String toString() {
return number+"-"+color+".";
}
}
}
| 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();
}
}
}
| 0 | Non-plagiarised |
1ea771ea | 2f8c3bf3 |
import java.io.*;
import java.util.*;
public class CODECHEF {
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();
}
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;
}
}
static long MOD=1000000000;
static class Pair{
long a;
int b;
Pair(long i,int j){
a=i;
b=j;
}
}
static long[] solve(int[] pos,long[] arr,int n,int k){
long[] ans=new long[n];
long[] left=new long[n];
long[] right=new long[n];
long min=Integer.MAX_VALUE;
for(int i=0;i<n;i++){
min=Math.min(min+1,arr[i]);
left[i]=min;
}
min=Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
min=Math.min(min+1,arr[i]);
right[i]=min;
}
for(int i=0;i<n;i++){
ans[i]=Math.min(left[i],right[i]);
}
return ans;
}
public static void main(String[] args) throws java.lang.Exception {
FastReader fs=new FastReader(System.in);
// StringBuilder sb=new StringBuilder();
// PrintWriter out=new PrintWriter(System.out);
int t=fs.nextInt();
while (t-->0){
int n=fs.nextInt();
int k=fs.nextInt();
int[] pos=new int[k];
for(int i=0;i<k;i++)
pos[i]=fs.nextInt()-1;
long[] temp=new long[n];
int ptr=0;
Arrays.fill(temp,Integer.MAX_VALUE);
for(int i=0;i<k;i++)
temp[pos[ptr++]]=fs.nextLong();
long[] ans=solve(pos,temp,n,k);
for(int i=0;i<n;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
//out.close;
}
}
| 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 k=sc.nextInt();
int idx[]=new int[k];
for(int i=0;i<k;i++){
idx[i]=sc.nextInt();
}
long arr[]=new long[n];
Arrays.fill(arr,Integer.MAX_VALUE);
for(int i=0;i<k;i++){
long temp=sc.nextLong();
arr[idx[i]-1]=temp;
}
long left[]=new long[n];
long right[]=new long[n];
Arrays.fill(left,Integer.MAX_VALUE);
Arrays.fill(right,Integer.MAX_VALUE);
left[0]=arr[0];
for(int i=1;i<n;i++){
left[i]=Math.min(left[i-1]+1,arr[i]);
}
right[arr.length-1]=arr[arr.length-1];
for(int i=n-2;i>=0;i--){
right[i]=Math.min(right[i+1]+1,arr[i]);
}
for(int i=0;i<n;i++){
// System.out.print(left[i]+"--"+right[i]+"\\");
System.out.print(Math.min(left[i],right[i])+" ");
}
System.out.println();
}
}
}
| 0 | Non-plagiarised |
9291ca83 | d61f51c5 | import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Practice {
static HashMap<String, Integer> map = new HashMap<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-->0) {
int n = sc.nextInt();
int[][] occurances = new int[5][n];
for(int i=0;i<n;i++){
String s = sc.next();
int[] count = new int[5];
int len = s.length();
for(int j=0;j<s.length();j++){
count[s.charAt(j)-'a']++;
}
for(int j=0;j<5;j++){
occurances[j][i] = count[j] - (len-count[j]);
}
}
int ans = 0;
for(int i=0;i<5;i++){
Arrays.sort(occurances[i]);
int tmpAns = 0; int tmpSum=0;
for(int j=n-1;j>=0;j--){
tmpSum+=occurances[i][j];
if(tmpSum>0) tmpAns++;
else break;
}
ans = Math.max(ans, tmpAns);
}
System.out.println(ans);
}
}
}
|
/*
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$
|__ $$ /$$__ $$ |$$ |$$ /$$__ $$
| $$| $$ \ $$| $$|$$| $$ \ $$
| $$| $$$$$$$$| $$ / $$/| $$$$$$$$
/ $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$
| $$ | $$| $$ | $$ \ $$$/ | $$ | $$
| $$$$$$/| $$ | $$ \ $/ | $$ | $$
\______/ |__/ |__/ \_/ |__/ |__/
/$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$
| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$
| $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/
| $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$
| $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$
| $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$
|__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class InterestingStrory {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
int t =sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int[][] occ=new int[n][5];
for(int i=0;i<n;i++){
char[] warr=sc.next().toCharArray();
for(char ch:warr){
occ[i][ch-'a']++;
}
}
int ans=Integer.MIN_VALUE;;
for(int c=0;c<5;c++){
int[] sums=new int[n];
for(int i=0;i<n;i++){
int sum=0;
for(int j=0;j<5;j++){
if(c==j){
sum+=occ[i][j];
}
else{
sum-=occ[i][j];
}
}
sums[i]=sum;
}
Arrays.sort(sums);
int currSum=0;
int l=n-1;
for(l=n-1;l>=0;l--){
if(currSum+sums[l]<=0){
break;
}
else{
currSum+=sums[l];
}
}
ans=Math.max(ans,n-l-1);
}
System.out.println(ans);
}
} catch (Exception e) {
return;
}
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
public static long gcd_long(long a,long b){
//a/b,a-> dividant b-> divisor
if(b==0)return a;
return gcd_long(b, a%b);
}
public static int gcd_int(int a,int b){
//a/b,a-> dividant b-> divisor
if(b==0)return a;
return gcd_int(b, a%b);
}
public static int lcm(int a,int b){
int gcd=gcd_int(a, b);
return (a*b)/gcd;
}
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());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
* return false; }
*/
| 0 | Non-plagiarised |
9fd33d5a | c220c822 | 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);
}
} | // package com.company;
import java.util.*;
import java.io.*;
import java.lang.*;
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 {
static int dp[] = new int[31];
static {
dp[0]=1;
for(int i=1;i<31;i++)
{
dp[i]=dp[i-1]*2;
}
}
void solve(InputReader in, int testNumber) {
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[k];
int l[] = new int[n+1];
long pre[] = new long[n+2];
long suff[] = new long[n+2];
Arrays.fill(pre,Long.MAX_VALUE/2);
Arrays.fill(suff,Long.MAX_VALUE/2);
for(int i=0;i<k;i++)
{
a[i]=in.nextInt();
}
for(int i=0;i<k;i++)
{
int x = in.nextInt();
l[a[i]]=x;
}
for(int i=1;i<=n;i++)
{
pre[i]=pre[i-1];
if(l[i]!=0)
{
pre[i]=Math.min(pre[i],l[i]-i);
}
}
for(int i=n;i>=1;i--)
{
suff[i]=suff[i+1];
if(l[i]!=0)
{
suff[i]=Math.min(suff[i],l[i]+i);
}
}
for(int i=1;i<=n;i++)
{
System.out.print(Math.min(pre[i]+i,suff[i]-i)+" ");
}
System.out.println();
}
}
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 == 0)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
}
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);
}
}
}
| 0 | Non-plagiarised |
3c74c140 | 732c98a0 |
//package com.company;
import java.math.*;
import java.util.*;
import java.lang.*;
import java.io.*;
public final class Main {
FastReader s;
public static void main (String[] args) throws java.lang.Exception
{
new Main().run();
}
void run()
{
s = new FastReader();
solve();
}
StringBuffer sb;
// int counter;
void solve()
{
sb = new StringBuffer();
for(int T = s.nextInt();T > 0;T--)
{
start();
}
// System.out.print(sb);
}
void start()
{
int n = s.nextInt();
int mat[][] = new int[n][5];
for(int i = 0; i<n; i++)
{
char [] x = s.nextLine().toCharArray();
for(char c : x)
{
mat[i][c-'a']++;
}
int p[] = new int[5];
int to = 0;
for(int j : mat[i])
to+=j;
for(int j = 0; j<5; j++)
{
mat[i][j]=2*mat[i][j]-to;
}
}
int ans = 0;
for(int i = 0; i<5; i++)
{
int g = check(i,mat, n);
ans = Math.max(g,ans);
}
System.out.println(ans);
}
int check(int i, int mat[][], int n)
{
ArrayList<Integer> x = new ArrayList<>();
for(int j = 0; j<n; j++)
{
x.add(mat[j][i]);
}
Collections.sort(x);
int s = 0;
int cnt = 0;
int l = n-1;
while(l>=0)
{
int u = x.get(l);
if(s+u>0)
{
s+=u;
cnt++;
}
else
break;
l--;
}
return cnt;
}
long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
long power(long x, long y, long p)
{
long res = 1; // Initialize result
// Update x if it is more
// than or equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply
// x with the 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;
}
int lower_bound(int [] arr , int key)
{
int i = 0;
int j = arr.length-1;
//if(arr[i] > key)return -1;
if(arr[j] < key)return -1;
while(i<j)
{
int mid = (i+j)/2;
if(arr[mid] == key)
{
j = mid;
}
else if(arr[mid] < key)
{
i = mid+1;
}
else
j = mid-1;
}
return i;
}
int upper_bound(int [] arr , int key)
{
int i = 0;
int j = arr.length-1;
if(arr[j] <= key)return j+1;
while(i<j)
{
int mid = (i+j)/2;
if(arr[mid] <= key)
{
i = mid+1;
}
else
j = mid;
}
return i;
}
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();
}
//long array input
public long [] longArr(int len)
{
// long arr input
long [] arr = new long[len];
String [] strs = s.nextLine().trim().split("\\s+");
for(int i =0; i<len; i++)
{
arr[i] = Long.parseLong(strs[i]);
}
return arr;
}
// int arr input
public int [] intArr(int len)
{
// long arr input
int [] arr = new int[len];
String [] strs = s.nextLine().trim().split("\\s+");
for(int i =0; i<len; i++)
{
arr[i] = Integer.parseInt(strs[i]);
}
return 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;
}
}
}
| import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
import static java.lang.System.out;
import static java.lang.Long.MAX_VALUE;
public final class Main {
FastReader in;
StringBuffer sb;
public static void main(String[] args) {
new Main().run();
}
void run(){
in= new FastReader();
start();
}
void start(){
sb= new StringBuffer();
for(int t=in.nextInt();t>0;t--)
solve();
out.print(sb);
}
void solve(){
int n = in.nextInt();
int[][] mat = new int[n][5];
for(int i = 0; i<n; i++) {
char[] pp =in.nextLine().toCharArray();
for(char c : pp) {
mat[i][c-'a']++;
}
int[] p = new int[5];
int tt = 0;
for(int j : mat[i])
tt +=j;
for(int j = 0; j<5; j++) {
mat[i][j]=2*mat[i][j]-tt;
}
}
int ans = 0;
for(int i = 0; i<5; i++) {
int g = check(i,mat, n);
ans = Math.max(g,ans);
}
sb.append(ans).append("\n");
}
int check(int i, int[][] mat, int n) {
ArrayList<Integer> x = new ArrayList<>();
for(int j = 0; j<n; j++) {
x.add(mat[j][i]);
}
Collections.sort(x);
int s = 0;
int cnt = 0;
int l = n-1;
while(l>=0) {
int u = x.get(l);
if(s+u>0) {
s+=u;
cnt++;
}
else
break;
l--;
}
return cnt;
}
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;
x = (x * x) % p;
}
return res;
}
int upper_bound(long[] arr, int key) {
int i=0, j=arr.length-1;
if (arr[j]<=key) return j+1;
if(arr[i]>key) return i;
while (i<j){
int mid= (i+j)/2;
if(arr[mid]<=key){
i= mid+1;
}else{
j=mid;
}
}
return i;
}
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);
}
long[] longArr(int n){
long[] res= new long[n];
for(int i=0;i<n;i++){
res[i]= in.nextLong();
}
return res;
}
// sieve of eratosthenes code for precomputing whether numbers are prime or not up to MAX_VALUE
long MAX= MAX_VALUE;
int[] precomp= new int[(int) (MAX+1)];
void sieve(){
long n= MAX;
boolean[] prime = new boolean[(int) (n+1)];
for(int i=0;i<=n;i++)
prime[i] = true;
for(long p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[(int) p])
{
// Update all multiples of p
for(long i = p*p; i <= n; i += p)
prime[(int) i] = false;
}
}
// Print all prime numbers
for(long i = 2; i <= n; i++)
{
if(prime[(int) i])
precomp[(int) i]= 1;
}
}
boolean isDigitSumPalindrome(long N) {
// code here
long sum= sumOfDigits(String.valueOf(N));
long rev=0;
long org= sum;
while (sum!=0){
long d= sum%10;
rev = rev*10 +d;
sum /= 10;
}
return org == rev;
}
long sumOfDigits(String n){
long sum= 0;
for (char c: n.toCharArray()){
sum += Integer.parseInt(String.valueOf(c));
}
return sum;
}
long[] revArray(long[] arr) {
int n= arr.length;
int i=0, j=n-1;
while (i<j){
long temp= arr[i];
arr[i]= arr[j];
arr[j]= temp;
i++;
j--;
}
return arr;
}
long gcd(long a, long b){
if (b==0)
return a;
return gcd(b, a%b);
}
long lcm(long a,long b){
return (a*b)/gcd(a,b);
}
static class Pair{
long first;
long second;
Pair(long x,long y){
this.first=x;
this.second=y;
}
}
static class Compare {
static void compare(ArrayList<Pair> arr, long n) {
arr.sort(new Comparator<Pair>() {
@Override
public int compare(Pair p1, Pair p2) {
return (int) (p2.first - p1.first);
}
});
}
}
public static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st==null || !st.hasMoreElements()){
try{
st=new StringTokenizer(br.readLine());
}catch (Exception 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 (Exception e){
e.printStackTrace();
}
return str;
}
}
} | 1 | Plagiarised |
2915ac5b | 9b8c6bd7 | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
// static Scanner sc=new Scanner(System.in);
// static ArrayList<Integer> al;
static long min;
// static long[] dp;
static FastReader sc=new FastReader();
public static void main (String[] args) throws java.lang.Exception
{
PrintWriter w=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0){
// dp=new long[n+1][n+1][n];
int n=sc.nextInt();
long[] c=new long[n];
for(int i=0;i<n;i++) {
c[i]=sc.nextLong();
}
min=c[0]*n+c[1]*n;
// f(n,c,n,n,0,0);
long x=Long.MAX_VALUE-1,y=Long.MAX_VALUE-1,sumx=0,sumy=0,cx=0,cy=0,min1=Long.MAX_VALUE-1,min2=Long.MAX_VALUE-1;
for(int i=0;i<n;i++) {
if(i%2==0){
x=Math.min(x,c[i]);sumx+=c[i];cx=i/2+1;
min1=sumx+x*(n-cx);
}
else {y=Math.min(y,c[i]);sumy+=c[i];cy=(i+1)/2;
min2=sumy+y*(n-cy);
}
if(i>=1)min=Math.min(min,min1+min2);
}
System.out.println(min);
}
w.flush();
w.close();
}
// public static long f(int n, long[] c, int x, int y, int i, long cost) {
// long l=Long.MAX_VALUE;
// if(i>=n || x>n || y>n)return 0;
// if(dp[x][y][i]!=-1)return dp[x][y][i];
// if(x==0 && y==0) {min=Math.min(cost, min);return;}
// if(x==0) {
// if(i%2!=0)min=Math.min(cost+c[i]*y, min);
// return;
// }
// if(y==0) {
// if(i%2==0)min=Math.min(cost+c[i]*x, min);
// return;
// }
// if(i%2==0) {
// for(int j=1;j<=x;j++) {
// l=Math.min(l,f(n,c,x-j,y,i+1,cost+j*c[i]));
// }
// }
// else {
// for(int j=1;j<=y;j++) {
// l=Math.min(l,f(n,c,x,y-j,i+1,cost+j*c[i]));
// }
// }
// }
}
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.*;
import java.math.*;
public final class Main{
public static void main(String[] args) throws IOException{
// Scanner sc=new Scanner(System.in);
FastReader sc=new FastReader();
PrintWriter writer=new PrintWriter(System.out);
int tc=sc.nextInt();
while(tc-->0) {
int n=sc.nextInt();
long[] c=new long[n];
for(int i=0;i<n;i++) c[i]=sc.nextInt();
long ans=c[0]*n+c[1]*n;
long x=Long.MAX_VALUE-1,y=Long.MAX_VALUE-1,sumx=0,sumy=0,cx=0,cy=0,min1=Long.MAX_VALUE-1,min2=Long.MAX_VALUE-1;
for(int i=0;i<n;i++) {
if(i%2==0){
x=Math.min(x,c[i]);
sumx+=c[i];
cx=i/2+1;
min1=sumx+x*(n-cx);
}
else {
y=Math.min(y,c[i]);
sumy+=c[i];cy=(i+1)/2;
min2=sumy+y*(n-cy);
}
if(i>=1) ans=Math.min(ans,min1+min2);
}
System.out.println(ans);
}
writer.flush();
writer.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;
}
}
} | 1 | Plagiarised |
8f31b279 | a3e272af | import java.util.*;
import java.io.*;
public class JavaTract
{
static class Pair implements Comparable<Pair>{
int first;
int second;
Pair(int x,int y){
this.first=x;
this.second=y;
}
@Override
public int compareTo(Pair x){
return this.first-x.first;
}
}
public static void main (String[] args)
{
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
while(t-->0){
int n=scan.nextInt();
int m=scan.nextInt();
int x=scan.nextInt();
int[]arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();
}
// TreeSet<Pair> set = new TreeSet<>();
Queue<Pair> set = new PriorityQueue<>();
for(int i=1;i<=m;i++){
set.add(new Pair(0,i));
}
System.out.println("YES");
for(int i=0;i<n;i++){
Pair temp=set.poll();
int first = temp.first;
int second = temp.second;
System.out.print(second+" ");
set.add(new Pair(first+arr[i],second));
}
System.out.println();
}
}
}
|
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 |
5af25bd7 | fa1bf524 | import java.io.*;
import java.util.*;
public class MySolution {
public static void main(String[] args) throws Exception {
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
StringBuilder out = new StringBuilder();
int numOfTestCases = Integer.parseInt(bu.readLine());
for (int tc = 1; tc <= numOfTestCases; tc++) {
int vertices = Integer.parseInt(bu.readLine());
connections = new ArrayList[vertices];
for (int i = 0; i < vertices; i++) {
connections[i] = new ArrayList<Integer>();
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 (int j = 0; j < vertices-1; j++) {
String st[] = bu.readLine().split(" ");
int u = Integer.parseInt(st[0]) - 1, v = Integer.parseInt(st[1]) - 1;
connections[u].add(v);
connections[v].add(u);
}
dfs(0, -1);
out.append(Math.max(s[0][0], s[0][1]) + "\n");
}
System.out.print(out);
}
static int N = 100000;
static int[][] a = new int[N][2];
static long[][] s = new long[N][2];
static ArrayList<Integer>[] connections;
public static void dfs(int n, int parent) {
for (int child : connections[n]) {
if (child != parent) {
dfs(child, n);
s[n][0] += Math.max(s[child][0] + Math.abs(a[n][0] - a[child][0]), s[child][1] + Math.abs(a[n][0] - a[child][1]));
s[n][1] += Math.max(s[child][0] + Math.abs(a[n][1] - a[child][0]), s[child][1] + Math.abs(a[n][1] - a[child][1]));
}
}
}
}
| import java.util.*;
import java.io.*;
public class Solution {
static LinkedList<Integer> graph[];
static long key[][];
static long value[][];
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
StringBuffer out=new StringBuffer();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
String input[];
OUTER:
while(t--!=0) {
int n=Integer.parseInt(br.readLine());
value=new long[n][2];
key=new long[n][2];
for(int i=0; i<n; i++) {
input= br.readLine().split("\\s");
for(int j=0; j<2; j++) {
key[i][j] = Integer.parseInt(input[j]);
}
}
graph=new LinkedList[n];
for(int i=0; i<n; i++)
graph[i]=new LinkedList();
for(int i=1; i<n; i++) {
input= br.readLine().split("\\s");
int u=Integer.parseInt(input[0])-1, v=Integer.parseInt(input[1])-1;
graph[u].add(v);
graph[v].add(u);
}
int root=-1;
for(int i=0; i<n; i++)
if(graph[i].size()==1) {
root=i;
break;
}
solve(root, -1);
out.append(Math.max(value[root][0], value[root][1])+"\n");
}
System.out.print(out);
}
private static void solve(int vertex, int parent) {
for(int child: graph[vertex]) {
if(child!=parent) {
solve(child, vertex);
for(int i=0; i<2; i++) {
long max=Long.MIN_VALUE;
for(int j=0; j<2; j++) {
max=Math.max(max, Math.abs(key[vertex][i]-key[child][j])+value[child][j]);
}
value[vertex][i]+=max;
}
}
}
// System.out.println(vertex+" "+Arrays.toString(key[vertex])+" "+Arrays.toString(value[vertex]));
}
} | 0 | Non-plagiarised |
489930000000 | 96cd6ee9 | import java.io.*;
import java.util.*;
public class Codeforce {
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();
}
}
static int mod = (int) (1e9 + 7);
public static class pair implements Comparator<pair> {
int x;
int y;
public pair() {
}
public pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compare(pair o1, pair o2) {
return o1.y - o2.y;
}
}
public static long modularpow(long a, long b) {
long res = 1;
if (b == 0)
return res;
else {
while (b > 0) {
if (b % 2 == 1) {
res *= a;
res %= mod;
}
a = a * a;
a %= mod;
b /= 2;
}
return res % mod;
}
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static String binary(int a) {
String s1 = "";
while (a > 0) {
s1 = a % 2 + s1;
a /= 2;
}
return s1;
}
public static int Lower_Bound(long a[], int l, int r, long k) {
while (r - l > 1) {
int mid = l + (r - l) / 2;
if (a[mid] <= k) l = mid;
else
r = mid;
}
return l;
}
public static int Upper_Bound(int a[], int l, int r, int k) {
while (r - l > 1) {
int mid = (l + r) / 2;
if (a[mid] <= k)
l = mid;
else
r = mid;
}
return l + 1;
}
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
br.readLine();
String s[]=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int k=Integer.parseInt(s[1]);
long a[]=new long[n];
long b[]=new long[n];
long c[]=new long[n];
Arrays.fill(c,Integer.MAX_VALUE);
String s1[]=br.readLine().split(" ");
s=br.readLine().split(" ");
for(int i=0;i<k;i++)
c[Integer.parseInt(s1[i])-1]=Integer.parseInt(s[i]);
long p=Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
p=Math.min(p+1,c[i]);
a[i]=p;
}
p=Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
p=Math.min(p+1,c[i]);
b[i]=p;
}
for(int i=0;i<n;i++)
sb.append((Math.min(a[i],b[i]))+" ");
sb.append("\n");
}
System.out.println(sb);
}
} | import java.io.*;
import java.util.*;
import java.math.BigInteger;
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);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
// main solver
static class Task{
double eps= 0.00000001;
static final int MAXN = 100005;
static final int MOD= 998244353;
// stores smallest prime factor for every number
static int spf[] = new int[MAXN];
Map<Integer,Set<Integer>> dp= new HashMap<>();
// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
public void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
// marking smallest prime factor for every
// number to be itself.
spf[i] = i;
// separately marking spf for every even
// number as 2
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
// checking if i is prime
if (spf[i] == i)
{
// marking SPF for all numbers divisible by i
for (int j=i*i; j<MAXN; j+=i)
// marking spf[j] if it is not
// previously marked
if (spf[j]==j)
spf[j] = i;
}
}
}
// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
public Set<Integer> getFactorization(int x)
{
if(dp.containsKey(x)) return dp.get(x);
Set<Integer> ret = new HashSet<>();
while (x != 1)
{
if(spf[x]!=2) ret.add(spf[x]);
x = x / spf[x];
}
dp.put(x,ret);
return ret;
}
// function to find first index >= x
public int lowerIndex(List<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;
}
// function to find last index <= y
public int upperIndex(List<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;
}
// function to count elements within given range
public int countInRange(List<Integer> arr, int n, int x, int y)
{
// initialize result
int count = 0;
count = upperIndex(arr, n, y) -
lowerIndex(arr, n, x) + 1;
return count;
}
public int add(int a, int b){
a+=b;
if(a>=MOD) a-=MOD;
else if(a<0) a+=MOD;
return a;
}
public int mul(int a, int b){
long res= (long)a*(long)b;
return (int)(res%MOD);
}
public int power(int a, int b) {
int ans=1;
while(b>0){
if((b&1)!=0) ans= mul(ans,a);
b>>=1;
a= mul(a,a);
}
return ans;
}
int[] fact= new int[MAXN];
int[] inv= new int[MAXN];
public int Ckn(int n, int k){
if(k<0 || n<0) return 0;
return mul(mul(fact[n],inv[k]),inv[n-k]);
}
public int inverse(int a){
return power(a,MOD-2);
}
public void preprocess() {
fact[0]=1;
for(int i=1;i<MAXN;i++) fact[i]= mul(fact[i-1],i);
inv[MAXN-1]= inverse(fact[MAXN-1]);
for(int i=MAXN-2;i>=0;i--){
inv[i]= mul(inv[i+1],i+1);
}
}
public void solve(InputReader in, PrintWriter out) {
int test= in.nextInt();
while(test-->0){
int n= in.nextInt(), k= in.nextInt();
int[] a= new int[k];
int[] t= new int[k];
for(int i=0;i<k;i++) a[i]= in.nextInt();
for(int i=0;i<k;i++) t[i]= in.nextInt();
int[] c= new int[n];
Arrays.fill(c, Integer.MAX_VALUE);
for(int i=0;i<k;i++){
c[a[i]-1]=t[i];
}
int[] L= new int[n];
int[] R= new int[n];
Arrays.fill(L, Integer.MAX_VALUE);
Arrays.fill(R, Integer.MAX_VALUE);
long p= Integer.MAX_VALUE;
for(int i=0;i<n;i++){
p=Math.min(p+1,c[i]);
L[i]=(int)p;
}
p= Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
p= Math.min(p+1,c[i]);
R[i]=(int)p;
}
for(int i=0;i<n;i++){
out.print(Math.min(L[i],R[i])+" ");
}
out.println();
}
}
// public static class compareL implements Comparator<Tuple>{
// @Override
// public int compare(Tuple t1, Tuple t2) {
// return t2.l - t1.l;
// }
// }
public int _gcd(int a, int b)
{
if(b == 0) {
return a;
}
else {
return _gcd(b, a % b);
}
}
}
static class Venice{
public Map<Long,Long> m= new HashMap<>();
public long base=0;
public long totalValue=0;
private int M= 1000000007;
private long addMod(long a, long b){
a+=b;
if(a>=M) a-=M;
return a;
}
public void reset(){
m= new HashMap<>();
base=0;
totalValue=0;
}
public void update(long add){
base= base+ add;
}
public void add(long key, long val){
long newKey= key-base;
m.put(newKey, addMod(m.getOrDefault(newKey,(long)0),val));
}
}
static class Tuple implements Comparable<Tuple>{
int x, y, z;
public Tuple(int x, int y, int z){
this.x= x;
this.y= y;
this.z=z;
}
@Override
public int compareTo(Tuple o){
return this.x-o.x;
}
}
static class Pair implements Comparable<Pair>{
public int x;
public int y;
public Pair(int x, int y){
this.x= x;
this.y= y;
}
@Override
public int compareTo(Pair o) {
return this.x-o.x;
}
}
// fast input reader class;
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble(){
return Double.parseDouble(nextToken());
}
public long nextLong(){
return Long.parseLong(nextToken());
}
}
} | 1 | Plagiarised |
0b5cff5a | 4fb09c5f | import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class E {
static long mod=(long)1e9+7;
static long mod1=998244353;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int t= in.nextInt();
while(t-->0) {
int n = in.nextInt();
int k = in.nextInt();
int[] a= in.readArray(k);
int[] temp = in.readArray(k);
int[] pre = new int[n];
Arrays.fill(pre,Integer.MAX_VALUE);
int[] suf = new int[n];
Arrays.fill(suf,Integer.MAX_VALUE);
for(int i = 0;i<k;i++){
pre[a[i]-1]=temp[i];
suf[a[i]-1]=temp[i];
}
int min = Integer.MAX_VALUE;
E.ruffleSort(a);
for(int i=a[0]-1;i<n;i++){
min = Math.min(min,pre[i]);
pre[i] = min;
min++;
}
min = Integer.MAX_VALUE;
for(int i = a[k-1]-1;i>=0;i--){
min = Math.min(min,suf[i]);
suf[i] = min;
min++;
}
for(int i=0;i<n;i++)
out.print(Math.min(pre[i],suf[i])+" ");
out.println();
}
out.close();
}
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 long gcd(long x, long y){
if(x==0)
return y;
if(y==0)
return x;
long r=0, a, b;
a = Math.max(x, y);
b = Math.min(x, y);
r = b;
while(a % b != 0){
r = a % b;
a = b;
b = r;
}
return r;
}
static long modulo(long a,long b,long c){
long x=1,y=a%c;
while(b > 0){
if(b%2 == 1)
x=(x*y)%c;
y = (y*y)%c;
b = b>>1;
}
return x%c;
}
public static void debug(Object... o){
System.err.println(Arrays.deepToString(o));
}
static int upper_bound(int[] arr,int n,int x){
int mid;
int low=0;
int high=n;
while(low<high){
mid=low+(high-low)/2;
if(x>=arr[mid])
low=mid+1;
else
high=mid;
}
return low;
}
static int lower_bound(int[] arr,int n,int x){
int mid;
int low=0;
int high=n;
while(low<high){
mid=low+(high-low)/2;
if(x<=arr[mid])
high=mid;
else
low=mid+1;
}
return low;
}
static String printPrecision(double d){
DecimalFormat ft = new DecimalFormat("0.00000000000");
return String.valueOf(ft.format(d));
}
static int countBit(long mask){
int ans=0;
while(mask!=0){
mask&=(mask-1);
ans++;
}
return 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());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] readArray(int n)
{
int[] arr=new int[n];
for(int i=0;i<n;i++) arr[i]=nextInt();
return arr;
}
}
} | import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class Trial {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int q = sc.nextInt();
while (q-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[k];
int[] t = new int[k];
HashMap<Integer, Integer> hm = new HashMap<>();
for (int i = 0; i < k; i++) {
arr[i] = sc.nextInt() - 1;
}
for (int i = 0; i < k; i++) {
t[i] = sc.nextInt();
hm.put(arr[i], t[i]);
}
int[] left = new int[n];
int[] right = new int[n];
left[0] = hm.getOrDefault(0, -1);
right[n - 1] = hm.getOrDefault(n - 1, -1);
for (int i = 1; i < n; i++) {
if (hm.containsKey(i)) {
if (left[i - 1] < 0) {
left[i] = hm.get(i);
} else {
left[i] = Math.min(hm.get(i), left[i - 1] + 1);
}
} else {
left[i] = left[i - 1] < 0 ? -1 : left[i - 1] + 1;
}
}
for (int i = n - 2; i >= 0; i--) {
if (hm.containsKey(i)) {
if (right[i + 1] < 0) {
right[i] = hm.get(i);
} else {
right[i] = Math.min(hm.get(i), right[i + 1] + 1);
}
} else {
right[i] = right[i + 1] < 0 ? -1 : right[i + 1] + 1;
}
}
for (int i = 0; i < n; i++) {
if (left[i] < 0) {
pw.print(right[i] + " ");
} else if (right[i] < 0) {
pw.print(left[i] + " ");
} else {
pw.print(Math.min(left[i], right[i]) + " ");
}
}
pw.println();
}
pw.flush();
pw.close();
}
// inclusive
private static void rotate(int[] arr, int l, int r) {
int temp = arr[r];
for (int i = r - 1; i >= l; i--) {
arr[i + 1] = arr[i];
}
arr[l] = temp;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream System) throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(System));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() {
for (long i = 0; i < 3e9; i++)
;
}
}
static class Pair {
int a;
int b;
boolean asc;
Pair(int a, int b, boolean asc) {
this.a = a;
this.b = b;
this.asc = asc;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a == pair.a &&
b == pair.b &&
asc == pair.asc;
}
@Override
public int hashCode() {
return Objects.hash(a, b, asc);
}
}
}
| 0 | Non-plagiarised |
3cbcb1cc | 6045e5d1 |
import java.util.*;
import java.lang.*;
import java.io.*;
public class E {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
br.readLine();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
String[] scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
int m = Integer.parseInt(scn[1]);
long[] land = new long[n + 1];
scn = (br.readLine()).trim().split(" ");
String[] scn1 = (br.readLine()).trim().split(" ");
for (int i = 0; i < m; i++) {
int idx = (int) Long.parseLong(scn[i]);
long temp = Long.parseLong(scn1[i]);
land[idx] = temp;
}
long[] ans = new long[n + 1];
long min = (long) (1e15);
for (int i = 1; i <= n; i++) {
if (land[i] != 0) {
min = Math.min(min, land[i]);
}
ans[i] = min;
min += 1;
}
min = (long) (1e15);
for (int i = n; i >= 1; i--) {
if (land[i] != 0) {
min = Math.min(min, land[i]);
}
ans[i] = Math.min(min, ans[i]);
min += 1;
}
for (int i = 1; i <= n; i++) {
sb.append(ans[i] + " ");
}
sb.append("\n");
if (t != 0) {
br.readLine();
}
}
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 sort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) (Math.random() * n);
int temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
public static void print(long[][] dp) {
for (long[] a : dp) {
for (long ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
public static void print(int[][] dp) {
for (int[] a : dp) {
for (int ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
public static void print(int[] dp) {
for (int ele : dp) {
System.out.print(ele + " ");
}
System.out.println();
}
public static void print(long[] dp) {
for (long ele : dp) {
System.out.print(ele + " ");
}
System.out.println();
}
}
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class E {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
br.readLine();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
String[] scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
int m = Integer.parseInt(scn[1]);
long[] land = new long[n + 1];
scn = (br.readLine()).trim().split(" ");
String[] scn1 = (br.readLine()).trim().split(" ");
for (int i = 0; i < m; i++) {
int idx = (int) Long.parseLong(scn[i]);
long temp = Long.parseLong(scn1[i]);
land[idx] = temp;
}
long[] ans = new long[n + 1];
long min = (long) (1e15);
for (int i = 1; i <= n; i++) {
if (land[i] != 0) {
min = Math.min(min, land[i]);
}
ans[i] = min;
min += 1;
}
min = (long) (1e15);
for (int i = n; i >= 1; i--) {
if (land[i] != 0) {
min = Math.min(min, land[i]);
}
ans[i] = Math.min(min, ans[i]);
min += 1;
}
for (int i = 1; i <= n; i++) {
sb.append(ans[i] + " ");
}
sb.append("\n");
if (t != 0) {
br.readLine();
}
}
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 sort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) (Math.random() * n);
int temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
public static void print(long[][] dp) {
for (long[] a : dp) {
for (long ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
public static void print(int[][] dp) {
for (int[] a : dp) {
for (int ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
public static void print(int[] dp) {
for (int ele : dp) {
System.out.print(ele + " ");
}
System.out.println();
}
public static void print(long[] dp) {
for (long ele : dp) {
System.out.print(ele + " ");
}
System.out.println();
}
}
| 1 | Plagiarised |
8c591975 | b6724dd9 | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
private static void run() throws IOException {
int n, k;
n = in.nextInt();
k = in.nextInt();
int[] a = new int[n];
long[] t = new long[k];
for (int i = 0; i < n; i++) {
a[i] = -1;
}
for (int i = 0; i < k; i++) {
a[in.nextInt() - 1] = i;
}
for (int i = 0; i < k; i++) {
t[i] = in.nextLong();
}
long[] dp = new long[n];
long now;
now = 2000000000;
for (int i = 0; i < n; i++) {
now++;
if (a[i] != -1) {
now = Math.min(now, t[a[i]]);
}
dp[i] = now;
}
now = 2000000000;
for (int i = n - 1; i >= 0; i--) {
now++;
if (a[i] != -1) {
now = Math.min(now, t[a[i]]);
}
dp[i] = Math.min(dp[i], now);
}
for (int i = 0; i < n; i++) {
out.printf("%d ", dp[i]);
}
out.println();
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
} |
import java.io.*;
import java.util.*;
public class AirConditioners {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// System.in and System.out are input and output streams, respectively.
InputStream inputStream = System.in;
InputReader in = new InputReader(inputStream);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
while (t-- != 0) {
int n = in.nextInt();
int k = in.nextInt();
HashMap<Integer, Integer> hm = new HashMap<>();
int[] ac = in.readArray(k);
for (int idx = 0; idx < ac.length; idx++) {
hm.put(ac[idx], in.nextInt());
}
Arrays.sort(ac);
long[] lmin = new long[n];
long val = (long) Integer.MAX_VALUE;
int acdx = 0;
for (int idx = 0; idx < lmin.length; idx++) {
if (acdx < ac.length && ac[acdx] - 1 == idx) {
val = (long) Math.min(val, hm.get(ac[acdx]));
acdx++;
}
lmin[idx] = val;
val++;
}
acdx = ac.length - 1;
val = Integer.MAX_VALUE;
for (int idx = lmin.length-1; idx >= 0; idx--) {
if (acdx >= 0 && ac[acdx] - 1 == idx) {
val = (long) Math.min(val, hm.get(ac[acdx]));
acdx--;
}
lmin[idx] = Math.min(lmin[idx], val);
val++;
}
for (long x : lmin) {
out.write(x + " ");
}
out.newLine();
}
out.close();
}
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());
}
public int[] readArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
}
}
| 0 | Non-plagiarised |
74f4382e | bd584714 |
import java.io.*;
import java.util.*;
public class Solution {
static long modInverse(long a, long m) {
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
long q = a / m;
long t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
static void pla() {
int a = 0;
int b = 2;
int c = 0;
int d = 0;
a = (int) Math.pow(2, 32);
c = a + b;
b = c / a;
d = a + b + c;
}
static int mod = (int) 1e9 + 7;
//static Scanner sc = new Scanner(System.in);
static StringBuilder out = new StringBuilder();
static int pri[] = new int[(int) 1e5 + 5];
static void sieve() {
pri[1] = pri[0] = 1;
for (int i = 2; i < pri.length; i++) {
pri[i] = i;
}
for (int i = 2; i < Math.sqrt(pri.length); i++) {
if (pri[i] != i)
continue;
for (int j = i * i; j < pri.length; j += i) {
if (pri[j] == j)
pri[j] = i;
}
}
}
public static void main(String[] args) throws IOException {
int t = sc.nextInt();
//sieve();
int tc = 1;
while (tc <= t) {
// out.append("Case #" + tc + ": ");
Solution run = new Solution();
run.run();
tc++;
}
System.out.println(out);
}
ArrayList<Integer> gr[];
long a[][];
public static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
public void run() throws IOException {
int n = sc.nextInt();
gr=new ArrayList[n+1];
a=new long[n+1][2];
for(int i=0;i<=n;i++)gr[i]=new ArrayList<>();
for(int i=1;i<=n;i++) {
a[i][0]=sc.nextInt();
a[i][1]=sc.nextInt();
}
for(int i=0;i<n-1;i++) {
int u=sc.nextInt();
int v=sc.nextInt();
gr[u].add(v);
gr[v].add(u);
}
dp=new long[n+1][2];
for(long a1[]:dp)Arrays.fill(a1, -1L);
//dfs(1,-1);
long res=Math.max(dfs(1,-1,0), dfs(1,-1,1));
out.append(res+"\n");
}
long dp[][];
long dfs(int u, int pa,int ok) {
if(dp[u][ok]!=-1)return dp[u][ok];
long ans=0;
for(int ch: gr[u]) {
if(ch==pa)continue;
long res=0;
if(ok==0) {
res=Math.max(dfs(ch,u,0)+Math.abs(a[u][0]-a[ch][0]), dfs(ch,u,1)+Math.abs(a[u][0]-a[ch][1]));
}
else {
res=Math.max(dfs(ch,u,0)+Math.abs(a[u][1]-a[ch][0]), dfs(ch,u,1)+Math.abs(a[u][1]-a[ch][1]));
}
ans+=res;
}
return dp[u][ok]=ans;
}
void dfs(int u, int pa) {
if(gr[u].size()==1) {
dp[u][0]=dp[u][1]=0L;
}
for(int ch:gr[u]) {
if(ch==pa)continue;
dfs(ch,u);
}
for(int ch: gr[u]) {
if(ch==pa)continue;
long ans1=0;
long ans2=0;
ans1=Math.max(dp[ch][0]+Math.abs(a[u][0]-a[ch][0]), dp[ch][1]+Math.abs(a[u][0]-a[u][1]));
dp[u][0]+=ans1;
ans1=Math.max(dp[ch][0]+Math.abs(a[u][1]-a[ch][0]), dp[ch][1]+Math.abs(a[u][1]-a[u][1]));
dp[u][1]+=ans2;
}
}
static void sort(int a[], int n) {
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
al.add(a[i]);
}
Collections.sort(al);
for (int i = 0; i < n; i++) {
a[i] = al.get(i);
}
}
static void sort(long a[], int n) {
ArrayList<Long> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
al.add(a[i]);
}
Collections.sort(al);
for (int i = 0; i < n; i++) {
a[i] = al.get(i);
}
}
static Reader sc = new Reader();
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();
}
}
} | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
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);
CParsasHumongousTree solver = new CParsasHumongousTree();
solver.solve(1, in, out);
out.close();
}
static class CParsasHumongousTree {
int n;
long[] l;
long[] r;
ArrayList<Integer>[] adj;
long[][] dp;
public void solve(int testNumber, Scanner sc, PrintWriter pw) {
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
n = sc.nextInt();
l = new long[n];
r = new long[n];
for (int i = 0; i < n; i++) {
l[i] = sc.nextInt();
r[i] = sc.nextInt();
}
adj = new ArrayList[n];
for (int i = 0; i < n; i++) adj[i] = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
int x = sc.nextInt() - 1;
int y = sc.nextInt() - 1;
adj[x].add(y);
adj[y].add(x);
}
dp = new long[n + 1][2];
for (long[] x : dp) Arrays.fill(x, -1);
long ans = solve(-1, 0, 0);
for (long[] x : dp) Arrays.fill(x, -1);
ans = Math.max(ans, solve(-1, 0, 1));
pw.println(ans);
}
}
long solve(int from, int i, int f) {
if (dp[i][f] != -1) return dp[i][f];
long ans = 0;
long a = f == 0 ? l[i] : r[i];
for (int x : adj[i]) {
if (x != from)
ans += Math.max((Math.abs(l[x] - a) + solve(i, x, 0)), (Math.abs(r[x] - a) + solve(i, x, 1)));
}
return dp[i][f] = ans;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| 0 | Non-plagiarised |
2b9dfed3 | 351c9206 | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class E {
public static void process() throws IOException {
int n = sc.nextInt(),k = sc.nextInt();
int index[] = sc.readArray(k);
long t[] = sc.readArrayLong(k);
PriorityQueue<Pair> q = new PriorityQueue<Pair>();
for(int i = 0; i<k; i++)q.add(new Pair(t[i]+index[i], index[i]));
long ans[] = new long[n+1];
Arrays.fill(ans, INF);
int i = 1;
while(!q.isEmpty() && i<=n) {
while(!q.isEmpty() && q.peek().y < i) {
q.poll();
}
if(q.isEmpty())break;
Pair e = q.peek();
ans[i]=min(ans[i],e.x-i);
i++;
}
// System.out.println(Arrays.toString(ans));
q.clear();
for(i = 0; i<k; i++) {
q.add(new Pair(t[i]+n-index[i], index[i]));
}
i = n;
while(!q.isEmpty() && i>=1) {
while(!q.isEmpty() && q.peek().y > i) {
q.poll();
}
if(q.isEmpty())break;
Pair e = q.peek();
ans[i]=min(ans[i],e.x-(n-i));
i--;
}
for( i = 1; i<=n; i++)out.print(ans[i]+" ");
out.println();
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
long x;
int y;
Pair(long x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Long.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
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));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
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 void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static 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);
}
}
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[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = 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[] readArrayDouble(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;
}
}
}
}
| import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class E {
public static void process() throws IOException {
int n = sc.nextInt(),k = sc.nextInt();
int index[] = sc.readArray(k);
long t[] = sc.readArrayLong(k);
PriorityQueue<Pair> q = new PriorityQueue<Pair>();
for(int i = 0; i<k; i++)q.add(new Pair(t[i]+index[i], index[i]));
long ans[] = new long[n+1];
Arrays.fill(ans, INF);
int i = 1;
while(!q.isEmpty() && i<=n) {
while(!q.isEmpty() && q.peek().y < i) {
q.poll();
}
if(q.isEmpty())break;
Pair e = q.peek();
ans[i]=min(ans[i],e.x-i);
i++;
}
// System.out.println(Arrays.toString(ans));
q.clear();
for(i = 0; i<k; i++) {
q.add(new Pair(t[i]+n-index[i], index[i]));
}
i = n;
while(!q.isEmpty() && i>=1) {
while(!q.isEmpty() && q.peek().y > i) {
q.poll();
}
if(q.isEmpty())break;
Pair e = q.peek();
ans[i]=min(ans[i],e.x-(n-i));
i--;
}
for( i = 1; i<=n; i++)out.print(ans[i]+" ");
out.println();
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
long x;
int y;
Pair(long x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Long.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
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));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
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 void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static 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);
}
}
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[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = 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[] readArrayDouble(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;
}
}
}
}
| 1 | Plagiarised |
49b94994 | f0d91796 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main1582D {
public static void main(String[] args) {
final FastScanner in = new FastScanner(System.in);
final PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = in.nextInt();
}
int[] b = solution(a, n);
for (int j = 0; j < n; j++) {
out.print(b[j]);
out.print(" ");
}
out.println();
}
out.flush();
out.close();
in.close();
}
private static int[] solution(int[] a, int n) {
int[] b = new int[n];
int start = 0;
if (n % 2 == 1) {
if (a[0] + a[1] != 0) {
b[0] = -a[2];
b[1] = -a[2];
b[2] = a[0] + a[1];
} else if (a[0] + a[2] != 0) {
b[0] = -a[1];
b[1] = a[0] + a[2];
b[2] = -a[1];
} else {
b[0] = a[1] + a[2];
b[1] = -a[0];
b[2] = -a[0];
}
start = 3;
} else {
b[0] = -a[1];
b[1] = a[0];
int gcd = gcd(b[0], b[1]);
b[0] /= gcd;
b[1] /= gcd;
start = 2;
}
for (int i = start; i < n; i += 2) {
b[i] = -a[i + 1];
b[i + 1] = a[i];
}
return b;
}
private static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
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());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readIntArr(int n) {
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = Integer.parseInt(next());
}
return result;
}
long[] readLongArr(int n) {
long[] result = new long[n];
for (int i = 0; i < n; i++) {
result[i] = Long.parseLong(next());
}
return result;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| 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 |
6b83b22e | c59194c7 | import java.util.Scanner;
public class Subsequence {
private static Scanner sc = new Scanner(System.in);
public static void main(String args[]) {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
for(int i=0;i<n;i++) {
a[i]= sc.nextInt();
}
if(n%2==0) {
calculateB(a,b,n);
}
else {
calculateB(a,b,n-3);
if (a[n - 2] + a[n - 3] != 0) {
b[n - 3] = -a[n - 1];
b[n - 2] = -a[n - 1];
b[n - 1] = a[n - 2] + a[n - 3];
} else if (a[n - 2] + a[n - 1] != 0) {
b[n - 3] = a[n - 2] + a[n - 1];
b[n - 2] = -a[n - 3];
b[n - 1] = -a[n - 3];
} else {
b[n - 3] = -a[n - 2];
b[n - 2] = a[n - 3] + a[n - 1];
b[n - 1] = -a[n - 2];
}
}
for(int i=0;i<n;i++) {
System.out.print(b[i] + " ");
}
System.out.println();
}
}
private static void calculateB(int[] a, int[] b, int n) {
for(int i=0;i<n-1;i=i+2) {
b[i] = -a[i+1];
b[i+1] = a[i];
}
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class codeforcesB{
public static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]){
FastReader sc=new FastReader();
StringBuilder sb=new StringBuilder();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int ar[]=new int[n];
int sum=0;
for(int i=0;i<n;i++){ar[i]=sc.nextInt();}
if(n%2==0){
for(int i=0;i<n;i++){
if(i%2==0){sb.append(-1*ar[i+1]+" ");}
else{sb.append(ar[i-1]+" ");}
}
}
else{
if(ar[1]+ar[0]!=0){
sb.append(ar[2]+" "+ar[2]+" "+-1*(ar[1]+ar[0])+" ");}
else{
if(ar[2]+ar[1]!=0){
sb.append(-1*(ar[2]+ar[1])+" "+ar[0]+" "+ar[0]+" ");
}
else{
sb.append(ar[1]+" "+-1*(ar[2]+ar[0])+" "+ar[1]+" ");
}
}
for(int i=3;i<n;i++){
if(i%2==1){sb.append(-1*ar[i+1]+" ");}
else{sb.append(ar[i-1]+" ");}
}
}
sb.append("\n");
}
System.out.print(sb.toString());
}
} | 0 | Non-plagiarised |
00af3420 | d92c5342 | 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;
public class Main{
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[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] nextArray(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());
}
}
static class FastWriter extends PrintWriter {
FastWriter(){
super(System.out);
}
void println(int[] array) {
for(int i=0; i<array.length; i++) {
print(array[i]+" ");
}
println();
}
void println(long [] array) {
for(int i=0; i<array.length; i++) {
print(array[i]+" ");
}
println();
}
}
static int MOD=1000003;
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner();
FastWriter out = new FastWriter();
int t=in.nextInt();
//int t=1;
while (t-->0){
int n=in.nextInt();
String[] ar=new String[n];
for (int i = 0; i < n; i++) {
ar[i]=in.next();
}
int ans=0;
for(char ch='a';ch<='e';ch++){
int[] res=new int[n];
for (int i = 0; i < n; i++) {
String ss=ar[i];
for (int j = 0; j < ss.length(); j++) {
if(ss.charAt(j)==ch){
res[i]++;
}else {
res[i]--;
}
}
}
Arrays.sort(res);int max=0,nn=0;
for (int i = n-1; i>=0; i--) {
max+=res[i];
if(max>0){
nn++;
}else {
break;
}
}
ans=Math.max(ans,nn);
}
out.println(ans);
}
out.close();
}
//Arrays.sort(a, (o1, o2) -> (o1[0] - o2[0]));
static int totalPrimeFactors(int n) {
int cnt=0;
while (n%2==0) {
cnt++;
n /= 2;
}
int num= (int) Math.sqrt(n);
for (int i = 3; i <= num; i+= 2) {
while (n%i == 0) {
cnt++;
n /= i;
num=(int)Math.sqrt(n);
}
}
if (n > 2){
cnt++;
}
return cnt;
}
static char get(int i){
return (char)(i+'a');
}
static boolean distinct(int num){
HashSet<Integer> set=new HashSet<>();
int len=String.valueOf(num).length();
while (num!=0){
set.add(num%10);
num/=10;
}
return set.size()==len;
}
static int maxSubArraySum(int a[],int st,int en) {
int max_s = Integer.MIN_VALUE, max_e = 0,ind=0,ind1=1;
for (int i = st; i <= en; i++) {
max_e+= a[i];
if (max_s < max_e){
max_s = max_e;
ind=ind1;
}
if (max_e < 0){
max_e = 0;
ind1=i+1;
}
}
if(st==0){
return max_s;
}
if(ind==1){
return a[0]+2*max_s;
}else {
return 2*max_s+maxSubArraySum(a,0,ind-1);
}
//return max_s;
}
static void segmentedSieve(ArrayList<Integer> res,int low, int high) {
if(low < 2){
low = 2;
}
ArrayList<Integer> prime = new ArrayList<>();
sS2(high, prime);
boolean[] mark = new boolean[high - low + 1];
Arrays.fill(mark, true);
for (int i = 0; i < prime.size(); i++) {
int loLim = (low / prime.get(i)) * prime.get(i);
if (loLim < low){
loLim += prime.get(i);
}
if (loLim == prime.get(i)){
loLim += prime.get(i);
}
for (int j = loLim; j <= high; j += prime.get(i)) {
mark[j - low] = false;
}
}
for (int i = low; i <= high; i++) {
if (mark[i - low]){
System.out.println(i);
res.add(i);
}
}
}
static void sS2(int limit, ArrayList<Integer> prime) {
int bound = (int)Math.sqrt(limit);
boolean[] mark = new boolean[bound + 1];
for (int i = 0; i <= bound; i++){
mark[i] = true;
}
for (int i = 2; i * i <= bound; i++) {
if (mark[i]) {
for (int j = i * i; j <= bound; j += i){
mark[j] = false;
}
}
}
for (int i = 2; i <= bound; i++) {
if (mark[i]){
prime.add(i);
}
}
}
static boolean[] sieveOfEratosthenes(int n){
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime,true);
prime[0]=false;prime[1]=false;
for (int p = 2; p * p <= n; p++) {
if(prime[p]){
for (int i = p * p; i <= n; i += p){
prime[i] = false;
}
}
}
return prime;
}
static int highestPowerOf2(int n) {
if (n < 1){ return 0; }
int res = 1;
for (int i = 0; i < 8 * Integer.BYTES; i++) {
int curr = 1 << i;
if (curr > n){ break; }
res = curr;
}
return res;
}
static int reduceFraction(int x, int y) {
int d= gcd(x, y);
return x/d+y/d;
}
static boolean subset(int[] ar,int n,int sum){
if(sum==0){
return true;
}
if(n<0||sum<0){
return false;
}
return subset(ar,n-1,sum)||subset(ar,n-1,sum-ar[n]);
}
static boolean isPrime(int n){
if(n<=1) return false;
for(int i = 2;i<=Math.sqrt(n);i++){
if (n % i == 0) return false;
}
return true;
}
static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
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 * b) / gcd(a, b);
}
static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
static boolean isPowerOfTwo(long n) {
if(n==0){
return false;
}
while (n%2==0){
n/=2;
}
return n==1;
}
static boolean isPerfectSquare(int x){
if (x >= 0) {
int sr = (int)Math.sqrt(x);
return ((sr * sr) == x);
}
return false;
}
static int lower_bound(int[] arr, int x) {
int low_limit = 0, high_limit = arr.length, mid;
while (low_limit < high_limit) {
mid = (low_limit + high_limit) / 2;
if (arr[mid] >= x){
high_limit = mid;
}else{
low_limit = mid + 1;
}
}
return low_limit;
}
static int upper_bound(int[] arr, int x) {
int low_limit = 0, high_limit = arr.length, mid;
while (low_limit < high_limit) {
mid = (low_limit + high_limit) / 2;
if (arr[mid] > x){
high_limit = mid;
}else{
low_limit = mid + 1;
}
}
return low_limit;
}
static int binarySearch(int[] ar,int n,int num){
int low=0,high=n-1;
while (low<=high){
int mid=(low+high)/2;
if(ar[mid]==num){
return mid;
}else if(ar[mid]>num){
high=mid-1;
}else {
low=mid+1;
}
}
return -1;
}
static int fib(int n) {
long[][] F = new long[][]{{1,1},{1,0}};
if (n == 0){
return 0;
}
power(F, n-1);
return (int)F[0][0];
}
static void multiply(long F[][], long M[][]) {
long x = (F[0][0]*M[0][0])%1000000007 + (F[0][1]*M[1][0])%1000000007;
long y = (F[0][0]*M[0][1])%1000000007 + (F[0][1]*M[1][1])%1000000007;
long z = (F[1][0]*M[0][0])%1000000007 + (F[1][1]*M[1][0])%1000000007;
long w = (F[1][0]*M[0][1])%1000000007 + (F[1][1]*M[1][1])%1000000007;
F[0][0] = x%1000000007;
F[0][1] = y%1000000007;
F[1][0] = z%1000000007;
F[1][1] = w%1000000007;
}
static void power(long F[][], int n) {
if( n == 0 || n == 1){
return;
}
long M[][] = new long[][]{{1,1},{1,0}};
power(F, n/2);
multiply(F, F);
if (n%2 != 0){
multiply(F, M);
}
}
static int binaryExponentiation(int x,int n){
int ans=1;
while (n>0){
if(n%2!=0){
ans=(ans*x)%MOD;
}
x=(x*x)%MOD;
n/=2;
}
return ans;
}
static void bfs(ArrayList<Integer>[] ar,int node){
boolean[] visited=new boolean[ar.length];
Queue<Integer> queue=new LinkedList<>();
visited[node]=true;
queue.add(node);
while(!queue.isEmpty()){
int nn=queue.poll();
System.out.print(nn+" ");
for(int n : ar[nn]) {
if (!visited[n]) {
visited[n] = true;
queue.add(n);
}
}
}
System.out.println();
}
static void dfs(ArrayList<Integer>[] ar,int node){
boolean[] visited=new boolean[ar.length];
dfsUtil(ar,node,visited);
System.out.println();
}
static void dfsUtil(ArrayList<Integer>[] ar,int node,boolean[] visited){
visited[node]=true;
//System.out.print(node+" ");
for(Integer n:ar[node]){
if(!visited[n]){
dfsUtil(ar,n,visited);
}
}
}
} | 0 | Non-plagiarised |
11373c16 | 69b2fd22 | /* 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 Codechef
{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 java.lang.Exception
{
FastReader scan = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int n = scan.nextInt();
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
for(int i=0;i<n;i++){
int x = scan.nextInt();
if(x==1)
a.add(i);
else
b.add(i);
}
int x = a.size();
if(x==0){
pw.println(0);
pw.flush();
return;
}
int y = b.size();
int dp[][] = new int[x][y];
int min = Integer.MAX_VALUE;
for(int i=0;i<y;i++){
min = Math.min(Math.abs(a.get(0) - b.get(i)),min);
dp[0][i] = min;
}
for(int i=1;i<x;i++){
min = Integer.MAX_VALUE;
for(int j=i;j<y;j++){
min = Math.min(Math.abs(a.get(i)-b.get(j))+dp[i-1][j-1],min);
dp[i][j] = min;
}
}
pw.println(dp[x-1][y-1]);
pw.flush();
}
}
| 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 |
7686c854 | 8a729537 | import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntToLongFunction;
import java.lang.*;
import java.io.*;
import java.math.*;
public final class CF
{
public static void main(String[]args)throws IOException
{
FastReader ob=new FastReader();
int t=ob.nextInt();
StringBuffer sb=new StringBuffer();
while(t-->0)
{
int n=ob.nextInt();
PriorityQueue<Long> a=new PriorityQueue<>();
PriorityQueue<Long> b=new PriorityQueue<>();
long ans=Long.MAX_VALUE;
long sum=0;
for(int i=0;i<n;i++)
{
long x=ob.nextInt();
if(i%2==0)
a.add(x);
else
b.add(x);
sum+=x;
if(i!=0)
ans=Math.min(ans,sum+(a.peek()*(n-a.size()))+(b.peek()*(n-b.size())));
}
sb.append(ans+"\n");
}
System.out.println(sb);
}
}
class Pair implements Comparable<Pair>
{
int x,y;
Pair(int x,int y)
{
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair a) {
return a.y-y;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine()
{
String s="";
try {
s=br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
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 test3 {
public static void main(String[] args) throws IOException {
FastReader f = new FastReader();
int t = f.nextInt();
while(t-->0) {
int n = f.nextInt();
int C[] = new int[n];
long ans=Long.MAX_VALUE,pre = 0;
PriorityQueue<Integer> epq = new PriorityQueue<Integer>();
PriorityQueue<Integer> opq = new PriorityQueue<Integer>();
for(int i = 0;i<n;i++) {
C[i] = f.nextInt();
if(i%2==0)epq.add(C[i]);
else opq.add(C[i]);
pre+=C[i];
if(i>0) {
ans = Math.min(ans,pre+ ((long)n-(long)epq.size())*(long)epq.peek()
+ ((long)n-(long)opq.size())*(long)opq.peek());
}
}
System.out.println(ans);
}
}
static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static void print(int x,int y,int d,int n) {
int i = 0;
System.out.print(x+" "+y+" ");
for(int j = x+d;j<y;j+=d) {
System.out.print(j+" ");
i++;
}
for(int j = x-d;j>0;j-=d) {
if(i==n)return;
System.out.print(j+" ");
i++;
}
for(int j = y+d;j<1000000000;j+=d) {
if(i==n)return;
System.out.print(j+" ");
i++;
}
}
static int prime(int n){
int ret = 0;
while(n%2==0){
ret++;
n/=2;
}
for(int i=3;i<=Math.sqrt(n);i+=2){
while(n%i==0){
ret++;
n/=i;
}
}
if(n>2)ret++;
return ret;
}
static long nCr(int n, int r)
{ if(n<r) {
return 0;
}
long[] C=new long[r+1];
C[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j-1]);
}
return C[r];
}
static int power(int a,int n, int p)
{
int res = 1;
a = a % p;
while (n > 0)
{
if ((n & 1) == 1)
res = (res * a) % p;
n = n >> 1;
a = (a * a) % p;
}
return res;
}
static boolean isPrime(int n, int k)
{
if (n <= 1 || n == 4) return false;
if (n <= 3) return true;
while (k > 0)
{
int a = 2 + (int)(Math.random() % (n - 4));
if (power(a, n - 1, n) != 1)
return false;
k--;
}
return true;
}
static long GCD(long a,long b) {
if(a%b==0)return b;
else return GCD(b,a%b);
}
static ArrayList<Integer> readArray(FastReader f,int size){
ArrayList<Integer> ret = new ArrayList<Integer>();
for(int i=0;i<size;i++) {
ret.add(f.nextInt());
}return ret;
}
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;
}
}
}
| 1 | Plagiarised |
1c8bb204 | 7ed5a88d | import javax.print.DocFlavor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BST {
static class pair implements Comparable{
int high;
int idx;
pair(int x , int y){
high = x;
idx = y;
}
public String toString(){
return high + " " + idx;
}
@Override
public int compareTo(Object o) {
pair p = (pair)o;
return high-p.high;
}
}
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());
long [] arr = new long[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
long tmp = Long.parseLong(st.nextToken());
arr[i] = tmp;
}
int h = 1;
int v = 1;
long minHor = arr[0];
long minVir = arr[1];
long sum0 = arr[0];
long sum1 = arr[1];
long total = (arr[0] + arr[1])*n;
for (int i = 2; i < n; i++) {
if(i%2==0){
h++;
sum0 += arr[i];
minHor = Math.min(arr[i] , minHor);
total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir));
}else {
v++;
sum1 += arr[i];
minVir = Math.min(arr[i] , minVir);
total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir));
}
}
System.out.println(total);
}
}
} | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.PrintStream;
//import java.util.*;
public class Solution {
public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null;
static class FastScanner {
BufferedReader br;
StringTokenizer st ;
FastScanner(){
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
FastScanner(String file) {
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
st = new StringTokenizer("");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("file not found");
e.printStackTrace();
}
}
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());
}
String readLine() throws IOException{
return br.readLine();
}
}
static class Pair<T,X>{
T first;
X second;
Pair(T first,X second){
this.first = first;
this.second = second;
}
@Override
public int hashCode(){
return Objects.hash(first,second);
}
@Override
public boolean equals(Object obj){
return obj.hashCode() == this.hashCode();
}
}
public static void main(String[] args) throws Exception {
FastScanner s = new FastScanner();
PrintStream debug = null;
if(LOCAL){
s = new FastScanner("src/input.txt");
PrintStream o = new PrintStream("src/sampleout.txt");
debug = new PrintStream("src/debug.txt");
System.setOut(o);
}
int tcr = s.nextInt();
for(int tc=0;tc<tcr;tc++){
int n = s.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++){
arr[i] = s.nextLong();
}
long min1 = arr[0];
long min2 = arr[1];
long sum1 = arr[0];
long sum2 = arr[1];
int cnt1 = 1;
int cnt2 = 1;
long ans = arr[0]*(n) + arr[1]*(n);
for(int i=2;i<n;i++){
if((i % 2) == 0){
min1 = Math.min(min1,arr[i]);
sum1 += arr[i];
cnt1++;
}else{
min2 = Math.min(min2,arr[i]);
sum2 += arr[i];
cnt2++;
}
long temp = min1*(n - cnt1 + 1) + (sum1 - min1) + (min2*(n- cnt2 + 1) + (sum2 - min2));
ans = Math.min(ans,temp);
}
println(ans);
}
}
static int len(long num){
return Long.toString(num).length();
}
static long mulmod(long a, long b,long mod)
{
return ((a%mod) * (b%mod)) % mod;
}
public static void dbg(PrintStream ps,Object... o) throws Exception{
if(ps == null){
return;
}
Debug.dbg(ps,o);
}
public static long modpow(long num,long pow,long mod){
long val = num;
long ans = 1l;
while(pow > 0l){
long bit = pow & 1l;
if(bit == 1){
ans = (ans * (val%mod))%mod;
}
val = (val * val) % mod;
pow = pow >> 1;
}
return ans;
}
public static char get(int n){
return (char)('a' + n);
}
public static long[] sort(long arr[]){
List<Long> list = new ArrayList<>();
for(long n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
public static int[] sort(int arr[]){
List<Integer> list = new ArrayList<>();
for(int n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
// return the (index + 1)
// where index is the pos of just smaller element
// i.e count of elemets strictly less than num
public static int justSmaller(long arr[],long num){
// System.out.println(num+"@");
int st = 0;
int e = arr.length - 1;
int ans = -1;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] >= num){
e = mid - 1;
}else{
ans = mid;
st = mid + 1;
}
}
return ans + 1;
}
//return (index of just greater element)
//count of elements smaller than or equal to num
public static int justGreater(long arr[],long num){
int st = 0;
int e = arr.length - 1;
int ans = arr.length;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] <= num){
st = mid + 1;
}else{
ans = mid;
e = mid - 1;
}
}
return ans;
}
public static void println(Object obj){
System.out.println(obj.toString());
}
public static void print(Object obj){
System.out.print(obj.toString());
}
public static int gcd(int a,int b){
if(b == 0){return a;}
return gcd(b,a%b);
}
public static long gcd(long a,long b){
if(b == 0l){
return a;
}
return gcd(b,a%b);
}
public static int find(int parent[],int v){
if(parent[v] == v){
return v;
}
return parent[v] = find(parent, parent[v]);
}
public static List<Integer> sieve(){
List<Integer> prime = new ArrayList<>();
int arr[] = new int[100001];
Arrays.fill(arr,1);
arr[1] = 0;
arr[2] = 1;
for(int i=2;i<=100000;i++){
if(arr[i] == 1){
prime.add(i);
for(long j = (i*1l*i);j<100001;j+=i){
arr[(int)j] = 0;
}
}
}
return prime;
}
static boolean isPower(long n,long a){
long log = (long)(Math.log(n)/Math.log(a));
long power = (long)Math.pow(a,log);
if(power == n){return true;}
return false;
}
private static int mergeAndCount(int[] arr, int l,int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l, swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static int mergeSortAndCount(int[] arr, int l,int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
int count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static class Debug{
//change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces
public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null;
private static <T> String ts(T t) {
if(t==null) {
return "null";
}
try {
return ts((Iterable) t);
}catch(ClassCastException e) {
if(t instanceof int[]) {
String s = Arrays.toString((int[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof long[]) {
String s = Arrays.toString((long[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof char[]) {
String s = Arrays.toString((char[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof double[]) {
String s = Arrays.toString((double[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof boolean[]) {
String s = Arrays.toString((boolean[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}
try {
return ts((Object[]) t);
}catch(ClassCastException e1) {
return t.toString();
}
}
}
private static <T> String ts(T[] arr) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: arr) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
private static <T> String ts(Iterable<T> iter) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: iter) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
public static void dbg(PrintStream ps,Object... o) throws Exception {
if(LOCAL) {
System.setErr(ps);
System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": [");
for(int i = 0; i<o.length; i++) {
if(i!=0) {
System.err.print(", ");
}
System.err.print(ts(o[i]));
}
System.err.println("]");
}
}
}
} | 0 | Non-plagiarised |
35eb27da | 8559d49e | /* 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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt();
sc.nextLine();
int a[][] = new int[26][26];
int b[][][] = new int[26][26][26];
boolean bl = false;
String arr[] = new String[n];
for(int i = 0 ; i < n ; i++)
{
arr[i] = sc.nextLine();
if(arr[i].length() == 1)
{
bl = true;
}
else if(arr[i].length() == 2)
{
//a[arr[i].charAt(0)-'a'][arr[i].charAt(1)-'a'] = 1;
if(arr[i].charAt(0)==arr[i].charAt(1))
bl = true;
}
else
{
//b[arr[i].charAt(0)-'a'][arr[i].charAt(1)-'a'][arr[i].charAt(2)-'a'] = 1;
if(arr[i].charAt(0) == arr[i].charAt(2))
bl = true;
}
}
if(bl)
System.out.println("YES");
else
{
for(int i = 0; i < n ; i++)
{
if(arr[i].length() == 2)
{
int p1 = arr[i].charAt(0)-'a';
int p2 = arr[i].charAt(1)-'a';
if(a[p2][p1] == 1)
bl = true;
for(int j = 0; j < 26 ; j++)
{
if(b[p2][p1][j] == 1)
bl = true;
}
a[p1][p2] = 1;
}
else
{
int p1 = arr[i].charAt(0)-'a';
int p2 = arr[i].charAt(1)-'a';
int p3 = arr[i].charAt(2)-'a';
if(a[p3][p2] == 1)
bl = true;
if(b[p3][p2][p1] == 1)
bl = true;
b[p1][p2][p3] = 1;
}
}
if(bl)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
} | import java.io.*;
import java.util.*;
public class Ishu
{
static Scanner scan = new Scanner(System.in);
static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
static void tc() throws Exception
{
int n = scan.nextInt();
String[] str = new String[n];
int i, j;
for(i=0;i<n;++i)
str[i] = scan.next();
String ans = "NO";
for(i=0;i<n;++i)
{
String cur = str[i];
int len = cur.length();
if(cur.charAt(0) == cur.charAt(len - 1))
{
output.write("YES\n");
output.flush();
return;
}
}
Map<String, Integer> map = new HashMap<String, Integer>();
Map<String, Integer> par = new HashMap<String, Integer>();
for(i=0;i<n;++i)
{
String cur = str[i];
int len = cur.length();
if(len == 2)
{
if(!map.containsKey(cur))
map.put(cur, 0);
int value = map.get(cur);
++value;
map.remove(cur);
map.put(cur, value);
}
else
{
String pre = cur.substring(0, 2);
if(!par.containsKey(pre))
par.put(pre, 0);
int value = par.get(pre);
++value;
par.remove(pre);
par.put(pre, value);
if(!map.containsKey(cur))
map.put(cur, 0);
value = map.get(cur);
++value;
map.remove(cur);
map.put(cur, value);
}
}
for(i=n-1;i>=0;--i)
{
String cur = str[i];
int len = cur.length();
if(len == 2)
{
int value = map.get(cur);
--value;
map.remove(cur);
if(value > 0)
map.put(cur, value);
}
else
{
String pre = cur.substring(0, 2);
int value = par.get(pre);
--value;
par.remove(pre);
if(value > 0)
par.put(pre, value);
value = map.get(cur);
--value;
map.remove(cur);
if(value > 0)
map.put(cur, value);
}
if(len == 2)
{
StringBuffer buffer = new StringBuffer(cur);
buffer.reverse();
String rev = String.valueOf(buffer);
if(map.containsKey(rev) || par.containsKey(rev))
{
ans = "YES";
break;
}
}
else
{
String suf = cur.substring(1, 3);
StringBuffer buffer = new StringBuffer(suf);
buffer.reverse();
String rev = String.valueOf(buffer);
if(map.containsKey(rev))
{
ans = "YES";
break;
}
buffer = new StringBuffer(cur);
buffer.reverse();
rev = String.valueOf(buffer);
if(map.containsKey(rev))
{
ans = "YES";
break;
}
}
}
output.write(ans + "\n");
output.flush();
}
public static void main(String[] args) throws Exception
{
int t = 1;
t = scan.nextInt();
while(t-- > 0)
tc();
}
}
| 0 | Non-plagiarised |
0df4050e | 6f02c6d9 | 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.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;
}
}
}
| 1 | Plagiarised |
8532d5ed | b9595381 | import java.util.*;
import java.io.*;
public class C_Interesting_Story{
public static void main(String[] args) {
FastScanner s= new FastScanner();
StringBuilder res = new StringBuilder();
int t=s.nextInt();
int p=0;
while(p<t){
int n=s.nextInt();
ArrayList<String> list = new ArrayList<String>();
for(int i=0;i<n;i++){
String str=s.nextToken();
list.add(str);
}
long max=Integer.MIN_VALUE;
for(int i=97;i<102;i++){
char ch=(char)i;
ArrayList<Integer> nice = new ArrayList<Integer>();
for(int j=0;j<n;j++){
String obj=list.get(j);
int count=0;
for(int k=0;k<obj.length();k++){
char ch2=obj.charAt(k);
if(ch2==ch){
count++;
}
}
int num=obj.length()-count;
nice.add(count-num);
}
Collections.sort(nice,Collections.reverseOrder());
long sum=0;
long counting=0;
for(int j=0;j<nice.size();j++){
sum+=nice.get(j);
if(sum>0){
counting++;
}
else{
break;
}
}
max=Math.max(max,counting);
}
res.append(max+"\n");
p++;
}
System.out.println(res);
}
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());
}
}
} | import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main{
static int dest1;
static int dest2;
public static void main(String args[]){
FastScanner in = new FastScanner();
int test=in.nextInt();
while(test-->0){
int n=in.nextInt();
int count[][]=new int[n][5];
int total[]=new int[n];
String words[]=new String[n];
for(int i=0;i<n;i++){
words[i]=in.next();
for(int j=0;j<words[i].length();j++)
count[i][words[i].charAt(j)-'a']++;
total[i]=words[i].length();
}
int max=Integer.MIN_VALUE;
for(int i=0;i<5;i++){
Integer ans[]=new Integer[n];
for(int j=0;j<n;j++){
ans[j]=count[j][i]-(total[j]-count[j][i]);
}
Arrays.sort(ans,Collections.reverseOrder());
int j=0;
int r=0;
while(j<n && r+ans[j]>0){
r+=ans[j];
j++;
}
max=Math.max(j,max);
}
System.out.println(max);
}
}
public static int solve(int start[], int end[], int n)
{
int distance[][]=new int[n][n];
int r=n;
int c=n;
boolean visited[][]=new boolean[n][n];
int startx=start[0];
int starty=start[1];
int endx=end[0];
int endy=end[1];
Pair tmp=new Pair(startx,starty);
Queue<Pair> q=new LinkedList<>();
q.add(tmp);
visited[startx][starty]=true;
distance[startx][starty]=0;
int dx[]={-2,-1,1,2,2,1,-1,-2};
int dy[]={1,2,2,1,-1,-2,-2,-1};
while(!q.isEmpty())
{
Pair cell=q.poll();
int x=cell.x;
int y=cell.y;
int d=distance[x][y];
for(int i=0;i<8;i++)
{
int childx=x+dx[i];
int childy=y+dy[i];
if(valid(childx,childy,r,c,visited))
{
visited[childx][childy]=true;
distance[childx][childy]=d+1;
q.add(new Pair(childx,childy));
}
}
}
return distance[endx][endy];
}
public static boolean valid(int x,int y,int r,int c,boolean visited[][])
{
if(x<0||y<0||x>=r||y>=c)
return false;
if(visited[x][y])
return false;
return true;
}
public static int knight(int i,int j){
if(i==dest1 && j==dest2)
return 0;
if(i<1 || j<1)
return 0;
if(i>8 || j>8)
return 0;
if(i<1 || j>8)
return 0;
if(i>8 || j<1)
return 0;
int min=0;
if(i-1>=1 && j-2>=1)
min+=1+knight(i-1,j-2);
if(i-1>=1 && j+2<=8)
min+=1+knight(i-1,j+2);
if(i-2>=1 && j-1>=1)
min+=1+knight(i-2,j-1);
if(i-2>=1 && j+1<=8)
min+=1+knight(i-2,j+1);
if(i+1<=8 && j-2>=1)
min+=1+knight(i+1,j-2);
if(i+1<=8 && j+2<=8)
min+=1+knight(i+1,j+2);
if(i+2<=8 && j-1>=1)
min+=1+knight(i+2,j-1);
if(i+2<=8 && j+1<=8)
min+=1+knight(i+2,j+1);
return min;
}
}
class FastScanner {
java.io.BufferedReader br = new java.io.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());
}
}
class Pair
{
int x;
int y;
Pair(int i,int j)
{
x=i;
y=j;
}
}
| 0 | Non-plagiarised |
8a729537 | e270e909 |
import java.io.*;
import java.util.*;
public class test3 {
public static void main(String[] args) throws IOException {
FastReader f = new FastReader();
int t = f.nextInt();
while(t-->0) {
int n = f.nextInt();
int C[] = new int[n];
long ans=Long.MAX_VALUE,pre = 0;
PriorityQueue<Integer> epq = new PriorityQueue<Integer>();
PriorityQueue<Integer> opq = new PriorityQueue<Integer>();
for(int i = 0;i<n;i++) {
C[i] = f.nextInt();
if(i%2==0)epq.add(C[i]);
else opq.add(C[i]);
pre+=C[i];
if(i>0) {
ans = Math.min(ans,pre+ ((long)n-(long)epq.size())*(long)epq.peek()
+ ((long)n-(long)opq.size())*(long)opq.peek());
}
}
System.out.println(ans);
}
}
static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static void print(int x,int y,int d,int n) {
int i = 0;
System.out.print(x+" "+y+" ");
for(int j = x+d;j<y;j+=d) {
System.out.print(j+" ");
i++;
}
for(int j = x-d;j>0;j-=d) {
if(i==n)return;
System.out.print(j+" ");
i++;
}
for(int j = y+d;j<1000000000;j+=d) {
if(i==n)return;
System.out.print(j+" ");
i++;
}
}
static int prime(int n){
int ret = 0;
while(n%2==0){
ret++;
n/=2;
}
for(int i=3;i<=Math.sqrt(n);i+=2){
while(n%i==0){
ret++;
n/=i;
}
}
if(n>2)ret++;
return ret;
}
static long nCr(int n, int r)
{ if(n<r) {
return 0;
}
long[] C=new long[r+1];
C[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j-1]);
}
return C[r];
}
static int power(int a,int n, int p)
{
int res = 1;
a = a % p;
while (n > 0)
{
if ((n & 1) == 1)
res = (res * a) % p;
n = n >> 1;
a = (a * a) % p;
}
return res;
}
static boolean isPrime(int n, int k)
{
if (n <= 1 || n == 4) return false;
if (n <= 3) return true;
while (k > 0)
{
int a = 2 + (int)(Math.random() % (n - 4));
if (power(a, n - 1, n) != 1)
return false;
k--;
}
return true;
}
static long GCD(long a,long b) {
if(a%b==0)return b;
else return GCD(b,a%b);
}
static ArrayList<Integer> readArray(FastReader f,int size){
ArrayList<Integer> ret = new ArrayList<Integer>();
for(int i=0;i<size;i++) {
ret.add(f.nextInt());
}return ret;
}
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 GFG {
public static class Pair{
long first;
long second;
Pair(long first,long second){
this.first = first;
this.second = second;
}
}
public static int gcd(int a, int b)
{
// if b=0, a is the GCD
if (b == 0)
return a;
// call the gcd() method recursively by
// replacing a with b and b with
// modulus(a,b) as long as b != 0
else
return gcd(b, a % b);
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
long pre = 0,ans = Long.MAX_VALUE;
long[] arr = new long[n];
PriorityQueue<Long> epq = new PriorityQueue<>();
PriorityQueue<Long> opq = new PriorityQueue<>();
for(int i=0;i<n;i++)
{
arr[i] = sc.nextLong();
if(i%2==0)
epq.add(arr[i]);
else
opq.add(arr[i]);
pre+=arr[i];
if(i>0)
ans = Math.min(ans,pre+(n-epq.size())*epq.peek()+(n-opq.size())*opq.peek());
}
System.out.println(ans);
}
}
}
| 1 | Plagiarised |
93ee4612 | da5cf40b | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int numTests = in.nextInt();
for (int test = 0; test < numTests; test++) {
int n = in.nextInt();
char[] a = in.next().toCharArray();
char[] b = in.next().toCharArray();
int ka = 0;
int kb = 0;
int k11 = 0;
int k01 = 0;
for (int i = 0; i < n; i++) {
if (a[i] == '1') {
++ka;
}
if (b[i] == '1') {
++kb;
}
if (a[i] == '1' && b[i] == '1') {
++k11;
}
if (a[i] == '0' && b[i] == '1') {
++k01;
}
}
if (ka == 0) {
out.println(kb == 0 ? 0 : -1);
continue;
}
int ans = Integer.MAX_VALUE;
if (ka == kb) {
ans = Math.min(ans, 2 * (ka - k11));
}
if (ka == n - kb + 1) {
int cur = 0;
if (k11 == 0) {
cur = 1 + 2 * (kb - k01);
} else {
cur = 1 + 2 * (kb - k01 - 1);
}
ans = Math.min(ans, cur);
}
if (ans == Integer.MAX_VALUE) {
ans = -1;
}
out.println(ans);
}
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
try {
in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
} catch (Exception e) {
throw new AssertionError();
}
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String rl = in.readLine();
if (rl == null) {
return null;
}
st = new StringTokenizer(rl);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=(long)1e9+7;
/* start */
public static void main(String [] args)
{
// int testcases = 1;
int testcases = i();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i();
char c[] = inputC();
char d[] = inputC();
int x01=0,x10=0,x00=0,x11=0;
for(int i=0;i<n;i++)
{
if(c[i]=='0'&&d[i]=='0')x00++;
if(c[i]=='0'&&d[i]=='1')x01++;
if(c[i]=='1'&&d[i]=='0')x10++;
if(c[i]=='1'&&d[i]=='1')x11++;
}
int ans = Integer.MAX_VALUE;
if(x01==0 && x10==0)
{
pl(0);
return ;
}
if(x11==x00+1)
{
ans = min(x11+x00,ans);
}
if(x01==x10)
{
ans = min(x01+x10,ans);
}
if(ans == Integer.MAX_VALUE){
ans = -1;
}
pl(ans);
}
/* end */
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 p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
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 long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
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 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 int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
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 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 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 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 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 long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long f, long s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first > p.first)
return 1;
else if (first < p.first)
return -1;
else {
if (second > p.second)
return 1;
else if (second < p.second)
return -1;
else
return 0;
}
}
}
} | 0 | Non-plagiarised |
1eda0725 | d2901569 | 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();
int k[]=new int[n];
int h[]=new int[n];
for(int i=0;i<n;i++)
{
k[i]=input.nextInt();
}
for(int i=0;i<n;i++)
{
h[i]=input.nextInt();
}
ArrayList<int []> list=new ArrayList<>();
for(int i=0;i<n;i++)
{
int p=k[i]-h[i]+1;
if(list.size()==0) list.add(new int[]{p,k[i]});
else
{
while(list.size()>0)
{
int j=list.size()-1;
int a[]=list.get(j);
int l=a[0],r=a[1];
int l1=p,r1=k[i];
if(l1>r)
{
list.add(new int[]{l1,r1});
break;
}
else if(l1>=l && l1<=r)
{
list.remove(j);
list.add(new int[]{l,r1});
break;
}
else
{
list.remove(j);
}
}
if(list.size()==0) list.add(new int[]{p,k[i]});
}
}
long sum=0;
for(int i=0;i<list.size();i++)
{
long d=list.get(i)[1]-list.get(i)[0]+1;
long val=d*(d+1)/2;
sum+=val;
}
out.println(sum);
}
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.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 |
927384f2 | d4779c71 | import java.util.*;
import java.io.*;
public class Main{
static class Point{
int x,y,z;
Point(int nx, int ny){
x = nx; y = ny;
}
@Override
public boolean equals(Object o) {
Point p = (Point)o;
return p.x == x && p.y == y;
}
}
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = pint(in);
Stack<Integer> min = new Stack<Integer>();
Stack<Integer> max = new Stack<Integer>();
int[] a = new int[n];
int[] dp = new int[n];
StringTokenizer st = new StringTokenizer(in.readLine());
a[0] = pint(st);
min.add(0);
max.add(0);
for(int i = 1; i < n; i++) {
a[i] = pint(st);
int h = dp[i - 1] + 1;
while(!max.isEmpty() && a[i] > a[max.peek()]) {
int k = a[max.peek()];
h = Math.min(h, dp[max.pop()] + 1);
while(!max.isEmpty() && a[max.peek()] == k) {max.pop();}
}
if(!max.isEmpty()) {
h = Math.min(h, dp[max.peek()] + 1);
}
while(!min.isEmpty() && a[i] < a[min.peek()]) {
int k = a[min.peek()];
h = Math.min(h, dp[min.pop()] + 1);
while(!min.isEmpty() && a[min.peek()] == k) {min.pop();}
}
if(!min.isEmpty()) {
h = Math.min(h, dp[min.peek()] + 1);
}
dp[i] = h;
min.add(i);
max.add(i);
}
System.out.println(dp[n - 1]);
}
static int[] resize(int[] a) {
int[] r = new int[a.length * 2];
for(int i = 0; i < a.length; i++) {
r[i] = a[i];
}
return r;
}
static int pint(BufferedReader in) throws IOException {return Integer.parseInt(in.readLine());}
static int pint(StringTokenizer st) {return Integer.parseInt(st.nextToken());}
} | import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class D {
static Scanner sc = new Scanner(System.in);
static int[] height;
static int[] dp;
public static void main(String[] args) {
int n = sc.nextInt();
height = new int[n];
dp = new int[n];
dp[0] = 0;
for (int i = 0; i < n; i++) {
height[i] = sc.nextInt();
}
Stack<Integer> rise = new Stack<Integer>();
Stack<Integer> fail = new Stack<Integer>();
rise.push(0);
fail.push(0);
for (int i = 1; i < n; i++) {
dp[i] = dp[i-1]+1;
if (rise.isEmpty()) {
rise.push(i);
} else if (height[rise.peek()] < height[i]) {
rise.push(i);
} else {
while (!rise.isEmpty() && height[rise.peek()] > height[i]) {
rise.pop();
if (!rise.isEmpty()) {
dp[i] = Math.min(dp[i], dp[rise.peek()] + 1);
}
}
while (!rise.isEmpty() && height[rise.peek()] == height[i]) {
rise.pop();
}
rise.push(i);
}
if (fail.isEmpty()) {
fail.push(i);
} else if (height[fail.peek()] > height[i]) {
fail.push(i);
} else {
while (!fail.isEmpty() && height[fail.peek()] < height[i]) {
fail.pop();
if (!fail.isEmpty()){
dp[i] = Math.min(dp[i], dp[fail.peek()] + 1);
}
}
while (!fail.isEmpty() && height[fail.peek()] == height[i]) {
fail.pop();
}
fail.push(i);
}
}
System.out.println(dp[n - 1]);
}
}
// a[i]
//
//
// 6 7 6 2
//3 5 6 4 5 6 3 | 0 | Non-plagiarised |
69b2fd22 | 7bc92b7f | 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;
}
}
} | import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws java.io.IOException {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
int[][] dp=new int[n][n];
int[][] min=new int[n][n];
ArrayList<Integer> ones=new ArrayList<>();
ArrayList<Integer> zero=new ArrayList<>();
for(int i=0;i<n;++i) {
arr[i] = sc.nextInt();
if(arr[i]==1)
ones.add(i);
else
zero.add(i);
}
for(int i=0;i<n;++i)
for(int j=0;j<n;++j) {
min[i][j] = Integer.MAX_VALUE;
dp[i][j] = Integer.MAX_VALUE;
}
int len=ones.size();
int zlen=zero.size();
int minn=0;
for(int i=0;i<len;++i)
{
int cur = ones.get(i);
for(int j=i;j<zlen;j++)
{
int curz = zero.get(j);
int cost = Math.abs(cur-curz);
if(i!=0 && curz-1>=0)
{
cost+=min[i-1][curz-1];
}
dp[i][curz]=cost;
}
minn=Integer.MAX_VALUE;
for(int j=0;j<n;++j)
{
if(dp[i][j]<minn)
minn=dp[i][j];
min[i][j]=minn;
}
}
System.out.println(minn);
}
}
// 1 0 1 1 0 0 1 | 0 | Non-plagiarised |
3380fa52 | e1a5831a | import java.util.*;
import java.io.*;
import java.math.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Coder {
static int n, k;
static long a[];
static int pos[];
static int temp[];
static StringBuilder str = new StringBuilder("");
static int cnt[][] = new int[(int)1e5+5][2];
static void solve() {
long []l = new long[n];
long []r = new long[n];
long p = Integer.MAX_VALUE;
for(int i=0;i<n;i++){
p = Math.min(p+1, a[i]);
l[i] = p;
}
p=Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
p = Math.min(p+1, a[i]);
r[i] = p;
}
for(int i=0;i<n;i++){
str.append(Math.min(l[i],r[i])).append(" ");
}
str.append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine().trim());
while (t-- > 0) {
bf.readLine();
String s[] = bf.readLine().trim().split("\\s+");
n = Integer.parseInt(s[0]);
k = Integer.parseInt(s[1]);
s = bf.readLine().trim().split("\\s+");
pos = new int[k];
temp = new int[k];
a = new long[n];
for(int i=0;i<k;i++) pos[i]=Integer.parseInt(s[i]);
s = bf.readLine().trim().split("\\s+");
for(int i=0;i<k;i++) temp[i]=Integer.parseInt(s[i]);
Arrays.fill(a, Integer.MAX_VALUE);
for(int i=0;i<k;i++) a[pos[i]-1] = temp[i];
solve();
}
System.out.print(str);
}
} | /* 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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for(int q=0;q<t;q++){
// int n = Integer.parseInt(br.readLine());
String s = br.readLine();
String s1[] = br.readLine().split(" ");
int n = Integer.parseInt(s1[0]);
int k = Integer.parseInt(s1[1]);
String s2[] = br.readLine().split(" ");
int a[] = new int[k];
for(int i=0;i<k;i++){
a[i] = Integer.parseInt(s2[i]);
}
String s3[] = br.readLine().split(" ");
int b[] = new int[k];
for(int i=0;i<k;i++){
b[i] = Integer.parseInt(s3[i]);
}
long ans[] = new long[n];
for(int i=0;i<n;i++){
ans[i] = Integer.MAX_VALUE;
}
for(int i=0;i<k;i++){
ans[a[i]-1] = b[i];
}
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++){
System.out.print(ans[i]+" ");
}
System.out.println();
}
}
}
| 0 | Non-plagiarised |
0e87b87f | 6369a7a2 | import java.io.*;
import java.util.*;
public class table {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
public FastReader(File file) throws java.io.IOException {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}
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 java.io.IOException {
File file = new File("hoofball.in"); // change this
FastReader input = new FastReader(); // change this
PrintWriter output = new PrintWriter("hoofball.out"); // change this or ph
int testcases = input.nextInt();
for (int t = 0; t < testcases; t++) {
int rows = input.nextInt();
int columns = input.nextInt();
char[][] matrix = new char[rows][columns];
for (int i = 0; i < rows; i++) {
matrix[i] = input.next().toCharArray();
}
int counter = 0;
ArrayList<String> list = new ArrayList<String>();
for (int r = 0; r < rows - 1; r++) {
for (int c = 0; c < columns - 1; c++) {
int r1 = r + 1;
int c1 = c + 1;
int r2 = r + 2;
int c2 = c + 2;
while (matrix[r][c] == '1' || matrix[r+1][c] == '1' || matrix[r][c+1] == '1' || matrix[r+1][c+1] == '1') {
if (matrix[r][c] == '0' && matrix[r+1][c] == '1' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '1') { // 0 1 1 1
list.add(r2 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r+1][c] = '0';
matrix[r][c+1] = '0';
matrix[r+1][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '0' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '1') { // 1 0 1 1
list.add(r1 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r][c] = '0';
matrix[r][c+1] = '0';
matrix[r+1][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '1' && matrix[r][c+1] == '0' && matrix[r+1][c+1] == '1') { // 1 1 0 1
list.add(r1 +" "+ c1 +" "+ r2 +" "+ c1 +" "+ r2 +" "+ c2);
matrix[r][c] = '0';
matrix[r+1][c] = '0';
matrix[r+1][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '1' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '0') { // 1 1 1 0
list.add(r1 +" "+ c1 +" "+ r2 +" "+ c1 +" "+ r1 +" "+ c2);
matrix[r][c] = '0';
matrix[r+1][c] = '0';
matrix[r][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '1' && matrix[r][c+1] == '0' && matrix[r+1][c+1] == '0') { // 1 1 0 0
list.add(r1 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r][c] = '0';
matrix[r][c+1] = '1';
matrix[r+1][c+1] = '1';
} else if (matrix[r][c] == '0' && matrix[r+1][c] == '0' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '1') { // 0 0 1 1
list.add(r1 +" "+ c1 +" "+ r2 +" "+ c1 +" "+ r2 +" "+ c2);
matrix[r][c] = '1';
matrix[r+1][c] = '1';
matrix[r+1][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '0' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '0') { // 1 0 1 0
list.add(r1 +" "+ c1 +" "+ r2 +" "+ c1 +" "+ r1 +" "+ c2);
matrix[r][c] = '0';
matrix[r+1][c] = '1';
matrix[r][c+1] = '0';
} else if (matrix[r][c] == '0' && matrix[r+1][c] == '1' && matrix[r][c+1] == '0' && matrix[r+1][c+1] == '1') { // 0 1 0 1
list.add(r1 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r][c] = '1';
matrix[r][c+1] = '1';
matrix[r+1][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '0' && matrix[r][c+1] == '0' && matrix[r+1][c+1] == '0') { // 1 0 0 0
list.add(r1 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r][c] = '0';
matrix[r][c+1] = '1';
matrix[r+1][c+1] = '1';
} else if (matrix[r][c] == '0' && matrix[r+1][c] == '1' && matrix[r][c+1] == '0' && matrix[r+1][c+1] == '0') { // 0 1 0 0
list.add(r2 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r+1][c] = '0';
matrix[r][c+1] = '1';
matrix[r+1][c+1] = '1';
} else if (matrix[r][c] == '0' && matrix[r+1][c] == '0' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '0') { // 0 0 1 0
list.add(r2 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r+1][c] = '1';
matrix[r][c+1] = '0';
matrix[r+1][c+1] = '1';
} else if (matrix[r][c] == '0' && matrix[r+1][c] == '0' && matrix[r][c+1] == '0' && matrix[r+1][c+1] == '1') { // 0 0 0 1
list.add(r1 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r][c] = '1';
matrix[r][c+1] = '1';
matrix[r+1][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '1' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '1') { // 1 1 1 1
list.add(r2 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c2);
matrix[r+1][c] = '0';
matrix[r][c+1] = '0';
matrix[r+1][c+1] = '0';
} else if (matrix[r][c] == '1' && matrix[r+1][c] == '0' && matrix[r][c+1] == '0' && matrix[r+1][c+1] == '1') { // 1 0 0 1
list.add(r1 +" "+ c1 +" "+ r1 +" "+ c2 +" "+ r2 +" "+ c1);
matrix[r][c] = '0';
matrix[r][c+1] = '1';
matrix[r+1][c] = '1';
} else if (matrix[r][c] == '0' && matrix[r+1][c] == '1' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '0') { // 0 1 1 0
list.add(r1 +" "+ c1 +" "+ r2 +" "+ c1 +" "+ r2 +" "+ c2);
matrix[r][c] = '1';
matrix[r+1][c] = '0';
matrix[r+1][c+1] = '1';
} else {
System.out.println("error");
}
counter++;
}
}
}
System.out.println(counter);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
output.close();
}
}
| import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class C2 {
static ArrayList<Integer[]> list;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
list = new ArrayList<>();
int n = in.nextInt();
int m = in.nextInt();
char[][] arr = new char[n][m];
for (int j = 0; j < n; j++) {
arr[j] = in.next().toCharArray();
}
for (int j = 0; j < n; j += 2) {
for (int k = 0; k < m; k += 2) {
if (j == n - 1) j = n - 2;
if (k == m - 1) k = m - 2;
int cnt = 0;
while (arr[j][k] == '1' || arr[j + 1][k] == '1' || arr[j][k + 1] == '1' || arr[j + 1][k + 1] == '1') {
// System.out.println(arr[j][k] + " " + arr[j][k + 1] + "\n" + arr[j + 1][k] + " " + arr[j + 1][k + 1]);
if (checkThree(arr, j, k)) {
// System.out.println("3:");
// System.out.println(arr[j][k] + " " + arr[j][k + 1] + "\n" + arr[j + 1][k] + " " + arr[j + 1][k + 1]);
continue;
} else if (checkTwo(arr, j, k)) {
// System.out.println("2:");
// System.out.println(arr[j][k] + " " + arr[j][k + 1] + "\n" + arr[j + 1][k] + " " + arr[j + 1][k + 1]);
checkThree(arr, j, k);
// System.out.println("3:");
// System.out.println(arr[j][k] + " " + arr[j][k + 1] + "\n" + arr[j + 1][k] + " " + arr[j + 1][k + 1]);
continue;
} else if (checkDiagonal(arr, j, k)) {
// System.out.println("/2:");
// System.out.println(arr[j][k] + " " + arr[j][k + 1] + "\n" + arr[j + 1][k] + " " + arr[j + 1][k + 1]);
} else if (checkOne(arr, j, k)) {
// System.out.println("1:");
// System.out.println(arr[j][k] + " " + arr[j][k + 1] + "\n" + arr[j + 1][k] + " " + arr[j + 1][k + 1]);
}
// cnt++;
// if (cnt == 10) return;
// System.out.println();
}
// System.out.println("qwe");
// System.out.println("qwe");
}
}
out.println(list.size());
for (Integer[] integers : list) {
for (int j = 0; j < integers.length; j++) {
out.print((integers[j] + 1) + " ");
}
out.println();
}
}
out.close();
}
public static boolean checkThree(char[][] arr, int i, int j) {
if (arr[i][j] == '1' && arr[i][j + 1] == '1' && arr[i + 1][j] == '1') {
arr[i][j] = '0';
arr[i][j + 1] = '0';
arr[i + 1][j] = '0';
list.add(new Integer[]{i, j, i, (j + 1), (i + 1), j});
return true;
} else if (arr[i][j] == '1' && arr[i][j + 1] == '1' && arr[i + 1][j + 1] == '1') {
arr[i][j] = '0';
arr[i][j + 1] = '0';
arr[i + 1][j + 1] = '0';
list.add(new Integer[]{i, j, i, (j + 1), (i + 1), j + 1});
return true;
} else if (arr[i + 1][j] == '1' && arr[i + 1][j + 1] == '1' && arr[i][j] == '1') {
arr[i + 1][j] = '0';
arr[i + 1][j + 1] = '0';
arr[i][j] = '0';
list.add(new Integer[]{i + 1, j, i + 1, j + 1, (i), j});
return true;
} else if (arr[i + 1][j] == '1' && arr[i + 1][j + 1] == '1' && arr[i][j + 1] == '1') {
arr[i + 1][j] = '0';
arr[i + 1][j + 1] = '0';
arr[i][j + 1] = '0';
list.add(new Integer[]{i + 1, j, i + 1, j + 1, (i), j + 1});
return true;
}
return false;
}
public static boolean checkDiagonal(char[][] arr, int i, int j) {
if (arr[i][j] == '1' && arr[i + 1][j + 1] == '1') {
arr[i][j] = '0';
arr[i][j + 1] = '1';
arr[i + 1][j] = '1';
list.add(new Integer[]{i, j, i, j + 1, (i + 1), j});
return true;
} else if (arr[i + 1][j] == '1' && arr[i][j + 1] == '1') {
arr[i + 1][j] = '0';
arr[i][j] = '1';
arr[i + 1][j + 1] = '1';
list.add(new Integer[]{i + 1, j, i, j, (i + 1), j + 1});
return true;
}
return false;
}
public static boolean checkOne(char[][] arr, int i, int j) {
if (arr[i][j] == '1') {
arr[i][j] = '0';
arr[i + 1][j] = '1';
arr[i][j + 1] = '1';
list.add(new Integer[]{i, j, i + 1, j, (i), j + 1});
return true;
} else if (arr[i + 1][j] == '1') {
arr[i + 1][j] = '0';
arr[i][j] = '1';
arr[i + 1][j + 1] = '1';
list.add(new Integer[]{i + 1, j, i, j, (i + 1), j + 1});
return true;
} else if (arr[i][j + 1] == '1') {
arr[i][j + 1] = '0';
arr[i][j] = '1';
arr[i + 1][j + 1] = '1';
list.add(new Integer[]{i, j + 1, i, j, (i + 1), j + 1});
return true;
} else if (arr[i + 1][j + 1] == '1') {
arr[i + 1][j + 1] = '0';
arr[i + 1][j] = '1';
arr[i][j + 1] = '1';
list.add(new Integer[]{i + 1, j + 1, i + 1, j, (i), j + 1});
return true;
}
return false;
}
public static char changeValue(char c) {
return c == '1' ? '0' : '1';
}
public static boolean checkTwo(char[][] arr, int i, int j) {
if (arr[i][j] == '1' && arr[i][j + 1] == '1') {
arr[i][j] = '0';
arr[i + 1][j + 1] = changeValue(arr[i + 1][j + 1]);
arr[i + 1][j] = changeValue(arr[i + 1][j]);
list.add(new Integer[]{i, j, i + 1, j + 1, (i + 1), j});
return true;
} else if (arr[i][j] == '1' && arr[i + 1][j] == '1') {
arr[i][j] = '0';
arr[i + 1][j + 1] = changeValue(arr[i + 1][j + 1]);
arr[i][j + 1] = changeValue(arr[i][j + 1]);
list.add(new Integer[]{i, j, i + 1, j + 1, (i), j + 1});
return true;
} else if (arr[i + 1][j] == '1' && arr[i + 1][j + 1] == '1') {
arr[i + 1][j] = '0';
arr[i][j + 1] = changeValue(arr[i][j + 1]);
arr[i][j] = changeValue(arr[i][j]);
list.add(new Integer[]{i + 1, j, i, j + 1, (i), j});
return true;
} else if (arr[i][j + 1] == '1' && arr[i + 1][j + 1] == '1') {
arr[i][j + 1] = '0';
arr[i][j] = changeValue(arr[i][j]);
arr[i + 1][j] = changeValue(arr[i + 1][j]);
list.add(new Integer[]{i, j + 1, i, j, (i + 1), j});
return true;
}
return false;
}
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());
}
}
}
| 0 | Non-plagiarised |
585af783 | f7a0ea6d | import java.util.*;
import java.io.*;
public class codeforces {
public static void main(String []args){
FastScanner sc = new FastScanner();
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 a[][] = new int[n][6];
for(int i=0;i<n;i++)
for(int j=0;j<s[i].length();j++){
a[i][s[i].charAt(j)-'a']++;
}
for(int i=0;i<n;i++)
a[i][5] = s[i].length();
int ans=0;
for(int i=0;i<5;i++)
{
ans = Math.max(ans,help(a,i,n));
}
System.out.println(ans);
}
}
public static int help(int a[][],int i,int n){
int total=0,count=0,count1=0;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int j=0;j<n;j++)
{
if(2*a[j][i]-a[j][5]>0)
{
total+=(2*a[j][i]-a[j][5]);
count++;
}
else if(2*a[j][i]-a[j][5]==0)
count1++;
else
pq.add(a[j][5]-2*a[j][i]);
}
if(count>0)
{
count+=count1;
int value=0;
while(!pq.isEmpty()&&value<total){
value+=pq.peek();
pq.remove();
count++;
}
if(value>=total)
count--;
}
return count;
}
public static class Pair{
public int x;
public int y;
public Pair(int x,int y){
this.x=x;
this.y=y;
}
}
public 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 Solution {
private static Scanner in = new Scanner(System.in);
public static void main(String args[]) {
int t = in.nextInt();
while(t-->0) {
solution();
}
}
private static void solution() {
int ans=0;
int n = in.nextInt();
String s[] = new String[n];
int occurance[][] = new int[n][5];
for(int i=0;i<n;i++) {
s[i] = in.next();
for(int j=0;j<s[i].length();j++) {
occurance[i][s[i].charAt(j)-'a']++;
}
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<5;j++)
// System.out.println(occurance[i][j]);
// System.out.println();
// }
for(int i=0;i<5;i++) {
int arr[] = new int[n];
for(int j=0;j<n;j++) {
arr[j] = s[j].length() - (2 * occurance[j][i]);
}
Arrays.sort(arr);
// for(int j=0;j<n;j++)
// System.out.println(arr[j]);
int temp=0;
int count=0;
for(int j=0;j<n;j++) {
if(temp+arr[j] < 0)
{
count++;
temp += arr[j];
}
else
break;
}
ans = Math.max(ans, count);
}
System.out.println(ans);
}
} | 0 | Non-plagiarised |
35bb6075 | ed728769 | /*
COLLECTIONS FRAMEWORK TUTORIAL
* HashMap
.add(key, value)
.get(key)
.containsKey(key) : true/false
.size()
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static final long M = 1000000007;
// use main for only io
public static void main(String args[]) {
FastReader io = new FastReader();
new Solver().solve(io);
}
}
//class Pair{
// int key;String value;
//
// public Pair(int key, String value){
// this.key = key;
// this.value = value;
// }
//}
class SparseTable{
int dp[][] = new int[300005][20];
int log[] = new int[300005];
public SparseTable(long a[], int n){
for(int i =0;i<n;i++)dp[i][0] = i;
for(int i = 1;i < 20;i++){
for(int j = 0;j + (1 << i) < n;j++){
if(a[dp[j][i-1]] < a[dp[j + (1 << (i-1))][i-1]]){
dp[j][i] = dp[j][i-1];
}
else{
dp[j][i] = dp[j + (1 << (i - 1))][i-1];
}
}
}
log[1] = 0;
for(int i = 2;i <= n;i++){
log[i] = log[i/2] + 1;
}
}
int getMin(int L, int R, long a[]){
if(L > R)return 0;
int j = log[R - L + 1];
if(a[dp[L][j]] < a[dp[R - (1 << j) + 1][j]])return dp[L][j];
return dp[R-(1 << j) + 1][j];
}
}
class Solver {
static final int M = 998244353;
void solve(FastReader io) {
int t = io.nextInt();
while(t-- > 0){
int n = io.nextInt();
String s[] = new String[n];
for(int i = 0;i < n;i++)s[i] = io.nextLine();
int ans = 0;
for(int i = 0;i < 5;i++){
int count[] = new int[n];
for(int j = 0;j < n;j++){
int freq = 0;
for(int k = 0;k < s[j].length();k++){
// System.out.println(s[j].charAt(k) - 'a');
if((s[j].charAt(k) - 'a') == i){
freq++;
}
}
// System.out.println(i + " " + freq);
count[j] = 2*freq - s[j].length();
}
Arrays.sort(count);
// for(int it : count)System.out.print(it + " ");
// System.out.println();
int curr = 0;
int j = n - 1;
for(;j >= 0 && (curr + count[j] > 0);j--){
curr += count[j];
}
ans = Math.max(ans, n - j - 1);
}
System.out.println(ans);
}
}
// returns the first key greater than or equal to val
int lower_bound(int a[], int val) {
int low = 0, high = a.length - 1, ret = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] < val) low = mid + 1;
else {
ret = mid;
high = mid - 1;
}
}
return ret;
}
// returns the first key strictly greater than val
int upper_bound(int a[], int val) {
int low = 0, high = a.length - 1, ret = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] <= val) low = mid + 1;
else {
ret = mid;
high = mid - 1;
}
}
return ret;
}
long modexp(long n, int m) {
if (m == 0)
return 1;
else if (m == 1)
return n;
else {
long p = modexp(n, m / 2);
if (m % 2 == 1)
return (((p * p) % M) * n) % M;
else
return (p * p) % M;
}
}
long exp(long n, long m) {
if (m == 0)
return 1;
if (m == 1)
return n;
long p = exp(n, m / 2);
if (m % 2 == 1)
return p * p * n;
else
return p * p;
}
long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
long inv(long n) {
return modexp(n, M - 2);
}
long lcm(long a, long b) {
return a * b / gcd(a, b);
}
long factorial(long fact[], int n) {
fact[0] = 1;
fact[1] = 1;
long prod = 1;
for (int i = 2; i <= n; i++) {
prod = (prod * i) % M;
fact[i] = prod;
}
return prod;
}
boolean isPrime(long n) {
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());
}
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 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();
}
}
static int parent(int a , int p[])
{
if(a == p[a])
return a;
return p[a] = parent(p[a],p);
}
static void union(int a , int b , int p[] , int size[])
{
a = parent(a,p);
b = parent(b,p);
if(a == b)
return;
if(size[a] < size[b])
{
int temp = a;
a = b;
b = temp;
}
p[b] = a;
size[a] += size[b];
}
static long dp[][];
static long f(int n , int m)
{
if(m == 0)
return 1;
if(dp[n][m] != -1)
return dp[n][m];
if(n < 9)
dp[n][m] = f(n+1,m-1);
else
dp[n][m] = f(0,m-1)+f(1,m-1);
dp[n][m] %= 1000000007;
return dp[n][m];
}
static long getSum(int index , long BITree[])
{
long sum = 0; // Iniialize result
// index in BITree[] is 1 more than
// the index in arr[]
// index = index + 1;
// Traverse ancestors of BITree[index]
while(index>0)
{
// Add current element of BITree
// to sum
sum += BITree[index];
// Move index to parent node in
// getSum View
index -= index & (-index);
}
return sum;
}
// Updates a node in Binary Index Tree (BITree)
// at given index in BITree. The given value
// 'val' is added to BITree[i] and all of
// its ancestors in tree.
public static void updateBIT(int n, int index,
long val , long BITree[])
{
// index in BITree[] is 1 more than
// the index in arr[]
// index = index + 1;
// Traverse all ancestors and add 'val'
while(index <= n)
{
// Add 'val' to current node of BIT Tree
BITree[index] += val;
// Update index to that of parent
// in update View
index += index & (-index);
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static void main(String []args) throws IOException
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
// sc.nextLine();
while(t-- > 0)
{
int n = sc.nextInt();
sc.nextLine();
//String s = sc.nextLine();
int arr[][] = new int[26][n];
for(int i = 0 ; i < n ; i++)
{
String s = sc.nextLine();
for(int j = 0 ; j < 26 ; j++)
{
int cnt = 0;
for(int k = 0 ; k < s.length() ; k++)
{
if(s.charAt(k)-'a' == j)
cnt++;
}
arr[j][i] = 2*cnt-s.length();
}
}
int ans = 0;
for(int i = 0 ; i < 26 ; i++)
{
Arrays.sort(arr[i]);
int tot = 0;
for(int j = n-1 ; j >= 0 ;j--)
{
tot += arr[i][j];
if(tot <= 0)
break;
ans = Math.max(ans,n-j);
}
}
System.out.println(ans);
}
}
} | 0 | Non-plagiarised |
cc669e02 | d88fa51c |
import java.util.*;
public class Codeforces {
// CHECK CONSTRAINTS ALWAYS EDGE CASE MISSED
static int mod = 1000000007;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
long k = sc.nextLong();
long a[] = new long[n+1];
for(int i = 1 ; i<=n;i++) {
a[i] = sc.nextLong();
}
a[0] = -1;
Arrays.sort(a);
long sum = 0;
for(int i = 1 ; i<=n;i++) {
sum+=a[i];
}
if(sum<=k) {
System.out.println(0);
}
else if(sum == k+1) {
System.out.println(1);
}
else {
long ans = sum-k;
long diff = sum - k;
sum = 0;
for(int i = n ; i>1 ; i--) {
long x = 0;
sum+=a[i];
if(sum - (n-i+1)*a[1] >= diff) {
x = 0;
}
else {
x = (long) Math.max(Math.ceil((double)(diff - sum + ((n-i+1)*a[1]))/(n-i+2)) , 0);
}
ans = Math.min(ans, x +n-i+1);
}
System.out.println(ans);
}
}
}
}
}
| import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class code{
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=in.nextLong();
PriorityQueue<Long> pq=new PriorityQueue<Long>();
long[] sum=new long[n+1];
for(int i=0;i<n;i++){
long a=in.nextLong();
pq.offer(a);
//sum[i+1]=arr[i]+sum[i];
}
for(int i=1;i<=n;i++){
sum[i]=pq.poll()+sum[i-1];
}
long min=(long)1e18;
for(int i=0;i<n;i++){
long x=sum[1]-floor(k-sum[n-i]+sum[1],(long)i+1);
min=Math.min(min,(long)i+Math.max(0L,x));
}
out.println(min);
}
out.flush();
}
public static long floor(long a,long b){
long z=a/b;
if(b*z>a) z--;
return z;
}
}
| 0 | Non-plagiarised |
51d88c51 | c2b7b017 |
import java.awt.Container;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Main
{
public static boolean check(int a[],int value)
{
int b[]= new int[a.length];
for (int i = a.length-1; i >=2; i--) {
if(a[i]+b[i]<value)
{
return false;
}
int d = Math.min(a[i], (a[i]+b[i]-value));
b[i-1]+=d/3;
b[i-2]+=(2*(d/3));
}
if(a[0]+b[0]<value||a[1]+b[1]<value)
{
return false;
}
return true;
}
public static void main(String[] args)
{
FastScanner input = new FastScanner();
StringBuilder result = new StringBuilder();
int tc = input.nextInt();
work:
while (tc-- > 0) {
int n = input.nextInt();
int a[]= new int[n];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
min = Math.min(min, a[i]);
max = Math.max(max, a[i]);
}
int low = min;
int high = max;
int ans = 0;
while(low<=high)
{
int mid = low+(high-low)/2;
if(check(a, mid))
{
ans = mid;
low = mid+1;
}
else
{
high = mid-1;
}
}
result.append(ans+"\n");
}
System.out.println(result);
}
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());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
| // Working program with FastReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
//
public class Example {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t= sc.nextInt();
while(t>0){
t--;
int ans=Integer.MAX_VALUE;
int n=sc.nextInt();
int[] ar= new int[n];
int l=Integer.MAX_VALUE;
int h=Integer.MIN_VALUE;
for(int i=0;i<n;i++){
ar[i]=sc.nextInt();
l=Math.min(l,ar[i]);
h=Math.max(h,ar[i]);
}
int[] extra;
while(l<=h){
int mid=l+(h-l)/2;
if(possibleans(ar,mid)){
ans=mid;
l=mid+1;
}else{
h=mid-1;
}
}
System.out.println(ans);
}
}
private static boolean possibleans(int[] ar, int mid) {
int[] extra=new int[ar.length];
for(int i=ar.length-1;i>=2;i--){
if((ar[i]+extra[i]-mid)<0){
return false;
}
int d=Math.min(ar[i],extra[i]+ar[i]-mid);
extra[i-1]=extra[i-1]+d/3;
extra[i-2]+=2*(d/3);
}
int a=ar[0]+extra[0];
int b=ar[1]+extra[1];
return (a>=mid && b>=mid);
}
private static boolean possible(int a, int b, int c, int mid) {
int min=Math.min(a,Math.min(b,c));
if(3*mid<=(c)){
c=c-3*mid;
a=a+2*mid;
b=b+mid;
int min1=Math.min(a,Math.min(b,c));
if(min1>min){
return true;
}else{
return false;
}
}else{
return false;
}
}
}
| 1 | Plagiarised |
624b8db5 | e9986d44 | //created by Whiplash99
import java.io.*;
import java.util.*;
public class C
{
private static ArrayDeque<Integer>[] edge;
private static HashMap<String,Integer> map;
private static String getHash(int u, int v)
{
if(u>v)
{
int tmp=u;
u=v;
v=tmp;
}
return u+" "+v;
}
private static void DFS(int u, int p, int[] ans, int val)
{
for(int v:edge[u])
{
if(v==p) continue;
ans[map.get(getHash(u,v))]=val;
DFS(v,u,ans,5-val);
val=5-val;
}
}
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,N;
int T=Integer.parseInt(br.readLine().trim());
StringBuilder sb=new StringBuilder();
while (T-->0)
{
N=Integer.parseInt(br.readLine().trim());
edge=new ArrayDeque[N];
for(i=0;i<N;i++) edge[i]=new ArrayDeque<>();
map=new HashMap<>();
int[] ans=new int[N-1];
int[] deg=new int[N];
for(i=0;i<N-1;i++)
{
String[] s=br.readLine().trim().split(" ");
int u=Integer.parseInt(s[0])-1;
int v=Integer.parseInt(s[1])-1;
edge[u].add(v); edge[v].add(u);
deg[u]++; deg[v]++;
map.put(getHash(u,v),i);
}
for(i=0;i<N;i++) if(deg[i]>2) break;
if(i<N)
{
sb.append(-1).append("\n");
continue;
}
DFS(0,0,ans,2);
for(int x:ans) sb.append(x).append(" ");
sb.append("\n");
}
System.out.println(sb);
}
} |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class cp23 {
static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
static int mod = 1000000007;
static String toReturn = "";
static int steps = Integer.MAX_VALUE;
static int maxlen = 1000005;
/*MATHEMATICS FUNCTIONS START HERE
MATHS
MATHS
MATHS
MATHS*/
static long gcd(long a, long b) {
if(b == 0) return a;
else return gcd(b, a % b);
}
static long powerMod(long x, long y, int mod) {
if(y == 0) return 1;
long temp = powerMod(x, y / 2, mod);
temp = ((temp % mod) * (temp % mod)) % mod;
if(y % 2 == 0) return temp;
else return ((x % mod) * (temp % mod)) % mod;
}
static long modInverse(long n, int p) {
return powerMod(n, p - 2, p);
}
static long nCr(int n, int r, int mod, long [] fact, long [] ifact) {
return ((fact[n] % mod) * ((ifact[r] * ifact[n - r]) % mod)) % mod;
}
/*
* static long [] seive(long n) {
*
* }
*/
/*MATHS
MATHS
MATHS
MATHS
MATHEMATICS FUNCTIONS END HERE */
/*SWAP FUNCTION START HERE
SWAP
SWAP
SWAP
SWAP
*/
static void swap(int i, int j, int [] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*SWAP
SWAP
SWAP
SWAP
SWAP FUNCTION END HERE*/
/*BINARY SEARCH METHODS START HERE
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
*/
static boolean BinaryCheck(long test, long [] arr, long health) {
for(int i = 0; i <= arr.length - 1; i++) {
if(i == arr.length - 1) health -= test;
else if(arr[i + 1] - arr[i] > test) {
health = health - test;
}else {
health = health - (arr[i + 1] - arr[i]);
}
if(health <= 0) return true;
}
return false;
}
static long binarySearchModified(long n, long [] arr) {
long start = 0, end = n, ans = n;
while(start < end) {
long mid = (start + end) / 2;
if(BinaryCheck(mid, arr, n)) {
ans = Math.min(ans, mid);
end = mid;
}else {
start = mid + 1;
}
}
return ans;
}
/*BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
BINARY SEARCH METHODS END HERE*/
/*RECURSIVE FUNCTION START HERE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
*/
static int recurse(int x, int y, int n, int steps1, Integer [][] dp) {
if(x > n || y > n) return 0;
if(dp[x][y] != null) {
return dp[x][y];
}
else if(x == n || y == n) {
return steps1;
}
return dp[x][y] = Math.max(recurse(x + y, y, n, steps1 + 1, dp), recurse(x, x + y, n, steps1 + 1, dp));
}
/*RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
RECURSIVE FUNCTION END HERE*/
/*GRAPH FUNCTIONS START HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
* */
static class edge{
int from, to, weight;
public edge(int x, int y, int z) {
this.from = x;
this.to = y;
this.weight = z;
}
}
static void addEdge(ArrayList<ArrayList<edge>> graph, int from, int to, int weight) {
edge temp = new edge(from, to, weight);
edge temp1 = new edge(to, from, weight);
graph.get(from).add(temp);
graph.get(to).add(temp1);
}
static int ans = 0;
static void dfs(ArrayList<ArrayList<edge>> graph, int vertex, boolean [] visited, int [] toReturn, int weight) {
//System.out.println(graph.get(vertex).size());
if(visited[vertex]) return;
visited[vertex] = true;
if(graph.get(vertex).size() > 2) return;
for(int i = 0; i < graph.get(vertex).size(); i++) {
edge temp = graph.get(vertex).get(i);
if(!visited[temp.to]) {
//System.out.println(temp.to);
toReturn[temp.weight] = weight;
dfs(graph, temp.to, visited, toReturn, 5 - weight);
weight = 5 - weight;
}
}
}
static void bfs(ArrayList<ArrayList<edge>> graph, int vertex, boolean [] visited, int [] toReturn, Queue<Integer> q, int weight) {
if(visited[vertex]) return;
visited[vertex] = true;
if(graph.get(vertex).size() > 2) return;
int first = weight;
for(int i = 0; i < graph.get(vertex).size(); i++) {
edge temp = graph.get(vertex).get(i);
if(!visited[temp.to]) {
q.add(temp.to);
toReturn[temp.weight] = weight;
weight = 5 - weight;
}
}
if(!q.isEmpty())bfs(graph, q.poll(), visited, toReturn, q, 5 - first);
}
static void topoSort(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, ArrayList<Integer> toReturn) {
if(visited[vertex]) return;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(!visited[graph.get(vertex).get(i)]) topoSort(graph, graph.get(vertex).get(i), visited, toReturn);
}
toReturn.add(vertex);
}
static boolean isCyclicDirected(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, boolean [] reStack) {
if(reStack[vertex]) return true;
if(visited[vertex]) return false;
reStack[vertex] = true;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(isCyclicDirected(graph, graph.get(vertex).get(i), visited, reStack)) return true;
}
reStack[vertex] = false;
return false;
}
/*GRAPH FUNCTIONS END HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
*/
/*disjoint Set START HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
static int [] rank;
static int [] parent;
static int parent(int [] parent, int x) {
if(parent[x] == x) return x;
else return parent[x] = parent(parent, parent[x]);
}
static void union(int x, int y, int [] rank, int [] parent) {
if(parent(parent, x) == parent(parent, y)) {
return;
}
if(rank[x] > rank[y]) {
swap(x, y, rank);
}
rank[x] += rank[y];
parent[x] = y;
}
/*disjoint Set END HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
/*INPUT START HERE
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT
*/
static int nexInt() throws NumberFormatException, IOException {
return Integer.parseInt(sc.readLine());
}
static long nexLong() throws NumberFormatException, IOException {
return Long.parseLong(sc.readLine());
}
static long [] inputLongArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
long [] toReturn = new long[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Long.parseLong(s[i]);
}
return toReturn;
}
static int [] inputIntArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
int [] toReturn = new int[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Integer.parseInt(s[i]);
}
return toReturn;
}
/*INPUT
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT END HERE
*/
static void solve() throws IOException {
int n = nexInt();
ArrayList<ArrayList<edge>> tree = new ArrayList<>();
for(int i = 0; i < n; i++) tree.add(new ArrayList<edge>());
for(int i = 0; i < n - 1; i++) {
int [] s1 = inputIntArr();
addEdge(tree, s1[0] - 1, s1[1] - 1, i);
}
int vertex = 0;
for(int i = 0; i < tree.size(); i++) {
if(tree.get(i).size() > 2) {
System.out.println(-1);
return;
}else if(tree.size() == 1) {
vertex = i;
}
}
int [] toReturn = new int[n - 1];
//toReturn.add(2);
dfs(tree, vertex, new boolean[n], toReturn, 2);
//System.out.println(toReturn.size());
for(int i = 0; i < toReturn.length; i++)
if(toReturn[i] != 0)System.out.print(toReturn[i] + " ");
System.out.println();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int t = Integer.parseInt(sc.readLine()); for(int i = 0; i < t; i++)
solve();
}
}
| 0 | Non-plagiarised |
c1fef98f | f870e76b | import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Test{
static FastReader scan;
static void solve(){
scan.nextLine();
int n=scan.nextInt();
int k=scan.nextInt();
int []arr=new int[n+1];
int []block=new int[k];
int []temp=new int[k];
for(int i=0;i<k;i++){
block[i]=scan.nextInt();
}
for(int i=0;i<k;i++){
temp[i]=scan.nextInt();
}
for(int i=0;i<k;i++){
arr[block[i]]=temp[i];
}
int []dp=new int[n+1];
int prev=Integer.MAX_VALUE-1000;
for(int i=1;i<=n;i++){
if(arr[i]==0){
if(prev==Integer.MAX_VALUE-1000){
dp[i]=prev;
}
else{
prev=prev+1;
dp[i]=prev;
}
}
else{
prev=Math.min(prev+1,arr[i]);
dp[i]=prev;
}
}
prev=Integer.MAX_VALUE-1000;
for(int i=n;i>=1;i--){
if(arr[i]==0){
if(prev==Integer.MAX_VALUE-1000){
dp[i]=Math.min(prev,dp[i]);
}
else{
prev=prev+1;
dp[i]=Math.min(prev,dp[i]);
}
}
else{
prev=Math.min(prev+1,arr[i]);
dp[i]=Math.min(prev,dp[i]);
}
}
for(int i=1;i<=n;i++){
System.out.print(dp[i]+" ");
}
System.out.println();
}
public static void main (String[] args) throws java.lang.Exception{
scan=new FastReader();
int t=scan.nextInt();
while(t-->0){
solve();
}
}
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 Pair implements Comparable<Pair>{
long wt;
long idx;
Pair(long x,long y){
this.wt=x;
this.idx=y;
}
@Override
public int compareTo(Pair x){
return (int)(this.wt-x.wt);
}
public String toString(){
return "( "+wt+" "+idx+" )";
}
}
static void printLong(long []arr){
for(long x:arr)System.out.print(x+" ");
}
static void printInt(int []arr){
for(int x:arr)System.out.print(x+" ");
}
static void scanInt(int []arr){
for(int i=0;i<arr.length;i++){
arr[i]=scan.nextInt();
}
}
static void scanLong(long []arr){
for(int i=0;i<arr.length;i++){
arr[i]=scan.nextLong();
}
}
static long gcd(long a, long b){
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long x, long y, long mod){
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;
x = (x * x) % mod;
}
return res;
}
static long add(long a,long b,long mod){
a = a % mod;
b = b % mod;
return (((a + b) % mod) + mod) % mod;
}
static long sub(long a, long b,long mod){
a = a % mod;
b = b % mod;
return (((a - b) % mod) + mod) % mod;
}
static long mul(long a, long b,long mod){
a = a % mod;
b = b % mod;
return (((a * b) % mod) + mod) % mod;
}
static long mminvprime(long a, long b,long mod) {
return power(a, b - 2,mod);
}
}
|
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.Collections;
import java.util.HashSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
public class Main {
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
public static void main(String[] args) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int k = in.nextInt();
int[] v1 = new int[k];
int[] v2 = new int[k];
for (int i = 0; i < k; i++) {
v1[i] = in.nextInt();
}
for (int i = 0; i < k; i++) {
v2[i] = in.nextInt();
}
//wejhfduiwehiofhw
int[] res = new int[n + 2];
Arrays.fill(res, 2000000000);
for (int i = 0; i < k; i++) {
res[v1[i]] = v2[i];
}
for (int i = 1; i <= n; i++) {
int val = Math.min(res[i], res[i - 1] + 1);
res[i] = val;
}
//ewhfowiejp
//wedhciuwahidochqowi
//wjdhoiqwnlidhqw
for (int i = n; i >= 1; i--) {
int val1 = Math.min(res[i], res[i + 1] + 1);
res[i] = val1;
// out.println(res[i]);
}
for (int i = 1; i <= n; i++) {
int r = res[i];
out.print(r + " ");
}
out.println();
}
out.close();
}
private static void takeinput(int[] arr, int k) {
for (int i = 0; i < k; i++) {
arr[i] = in.nextInt();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() throws IOException {
return reader.readLine().trim();
}
}
}
| 0 | Non-plagiarised |
169e34bf | fae0662f | import java.util.*;
public class D{
static Scanner sc;
public static void solve(){
int n=sc.nextInt();
Integer a[]=new Integer[n];
int flag;
for(int i=0;i<n;i++) a[i]=sc.nextInt();
String s=sc.next();
ArrayList<Integer> x=new ArrayList<>();
ArrayList<Integer> y=new ArrayList<>();
for(int i=0;i<n;i++){
if(s.charAt(i)=='B') x.add(a[i]);
else y.add(a[i]);
}
Collections.sort(x);
Collections.sort(y);
int p=n;
int q=1;
for(int i=y.size()-1;i>=0;i--){
if(y.get(i)>p){System.out.println("NO"); return;}
p-=1;
}
for(int i=0;i<x.size();i++){
if(x.get(i)<q){System.out.println("NO"); return;}
q+=1;
}
System.out.println("YES");
}
public static void main(String args[]){
sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) solve();
}
}
| 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();
}
}
}
| 1 | Plagiarised |
5af25bd7 | 77ec956f | import java.io.*;
import java.util.*;
public class MySolution {
public static void main(String[] args) throws Exception {
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
StringBuilder out = new StringBuilder();
int numOfTestCases = Integer.parseInt(bu.readLine());
for (int tc = 1; tc <= numOfTestCases; tc++) {
int vertices = Integer.parseInt(bu.readLine());
connections = new ArrayList[vertices];
for (int i = 0; i < vertices; i++) {
connections[i] = new ArrayList<Integer>();
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 (int j = 0; j < vertices-1; j++) {
String st[] = bu.readLine().split(" ");
int u = Integer.parseInt(st[0]) - 1, v = Integer.parseInt(st[1]) - 1;
connections[u].add(v);
connections[v].add(u);
}
dfs(0, -1);
out.append(Math.max(s[0][0], s[0][1]) + "\n");
}
System.out.print(out);
}
static int N = 100000;
static int[][] a = new int[N][2];
static long[][] s = new long[N][2];
static ArrayList<Integer>[] connections;
public static void dfs(int n, int parent) {
for (int child : connections[n]) {
if (child != parent) {
dfs(child, n);
s[n][0] += Math.max(s[child][0] + Math.abs(a[n][0] - a[child][0]), s[child][1] + Math.abs(a[n][0] - a[child][1]));
s[n][1] += Math.max(s[child][0] + Math.abs(a[n][1] - a[child][0]), s[child][1] + Math.abs(a[n][1] - a[child][1]));
}
}
}
}
| 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]));
}
}
}
| 1 | Plagiarised |
8ad1ad84 | f652c678 | import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
//import javafx.util.*;
public final class B
{
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);
static int D1[],D2[],par[];
static boolean set[];
static int value[];
static long INF=Long.MAX_VALUE;
static int N,M;
static long L[],R[],dp[][];
static int s=1;
public static void main(String args[])throws IOException
{
int T=i();
outer:while(T-->0)
{
int N=i();
setGraph(N);
for(int i=1; i<=N; i++)
{
L[i]=l();
R[i]=l();
}
for(int i=1; i<N; i++)
{
int a=i(),b=i();
g[a].add(b);
g[b].add(a);
}
f(1,-1);
out.println(Math.max(dp[0][1], dp[1][1]));
}
out.close();
//print(L);
//print(R);
//print(dp[0]);
//print(dp[1]);
}
static void f(int n,int p)
{
for(int c:g[n])
{
if(c!=p)
{
f(c,n);
long a=dp[0][c]+Math.abs(L[c]-L[n]);
long b=dp[1][c]+Math.abs(R[c]-L[n]);
dp[0][n]+=Math.max(a, b);
a=dp[0][c]+Math.abs(L[c]-R[n]);
b=dp[1][c]+Math.abs(R[c]-R[n]);
dp[1][n]+=Math.max(a, b);
}
}
}
static boolean isPalin(char X[])
{
int l=0,r=X.length-1;
while(l<=r)
{
if(X[l]!=X[r])return false;
l++;
r--;
}
return true;
}
static boolean isSorted(int A[])
{
int N=A.length;
for(int i=0; i<N-1; i++)
{
if(A[i]>A[i+1])return false;
}
return true;
}
static int f1(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 ask(int t,int i,int j,int x)
{
System.out.println("? "+t+" "+i+" "+j+" "+x);
return i();
}
static int ask(int a)
{
System.out.println("? 1 "+a);
return i();
}
static int f(int st,int end,int d)
{
if(st>end)return 0;
int a=0,b=0,c=0;
if(d>1)a=f(st+d-1,end,d-1);
b=f(st+d,end,d);
c=f(st+d+1,end,d+1);
return value[st]+max(a,b,c);
}
static int max(int a,int b,int c)
{
return Math.max(a, Math.max(c, b));
}
static int value(char X[],char Y[])
{
int c=0;
for(int i=0; i<7; i++)
{
if(Y[i]=='1' && X[i]=='0')return -1;
if(X[i]=='1' && Y[i]=='0')c++;
}
return c;
}
static boolean isValid(int i,int j)
{
if(i<0 || i>=N)return false;
if(j<0 || j>=M)return false;
return true;
}
static long fact(long N)
{
long num=1L;
while(N>=1)
{
num=((num%mod)*(N%mod))%mod;
N--;
}
return num;
}
static boolean reverse(long A[],int l,int r)
{
while(l<r)
{
long t=A[l];
A[l]=A[r];
A[r]=t;
l++;
r--;
}
if(isSorted(A))return true;
else return false;
}
static boolean isSorted(long A[])
{
for(int i=1; i<A.length; i++)if(A[i]<A[i-1])return false;
return true;
}
static boolean isPalindrome(char X[],int l,int r)
{
while(l<r)
{
if(X[l]!=X[r])return false;
l++; r--;
}
return true;
}
static long min(long a,long b,long c)
{
return Math.min(a, Math.min(c, b));
}
static void print(int a)
{
System.out.println("! "+a);
}
static int ask(int a,int b)
{
System.out.println("? "+a+" "+b);
return i();
}
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]; //transfers the size
par[b]=a; //changes the parent
}
}
static void swap(char A[],int a,int b)
{
char ch=A[a];
A[a]=A[b];
A[b]=ch;
}
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 void setGraph(int N)
{
g=new ArrayList[N+1];
dp=new long[2][N+1];
L=new long[N+1];
R=new long[N+1];
for(int i=0; i<=N; i++)
{
g[i]=new ArrayList<Integer>();
}
}
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 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 long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
//Debugging Functions Starts
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();
}
//Debugging Functions END
//----------------------
//IO FUNCTIONS STARTS
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 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;
}
//IO FUNCTIONS END
}
class node implements Comparable<node>
{
int x,index; boolean right;
node(int x,boolean right,int index)
{
this.x=x;
this.right=right;
this.index=index;
}
public int compareTo(node X)
{
return this.x-X.x;
}
}
//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());
}
//gey
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.text.DecimalFormat;
import java.util.*;
public class C {
static long mod = (long) 1e9 + 7;
static long mod1 = 998244353;
static ArrayList<Integer>[] adj;
static HashMap<String,Long> hmap;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
adj = new ArrayList[n + 1];
for (int i = 0; i <= n; i++)
adj[i] = new ArrayList<>();
long[][] dp = new long[n + 1][2];
hmap=new HashMap<>();
for (int i = 1; i <= n; i++) {
dp[i][0] = in.nextLong();
dp[i][1] = in.nextLong();
}
for (int i = 0; i < n - 1; i++) {
int u = in.nextInt();
int v = in.nextInt();
adj[u].add(v);
adj[v].add(u);
}
long[] x=DFS(dp,1,-1);
out.println(Math.max(x[0],x[1]));
}
out.close();
}
static long[] DFS(long[][] dp,int s,int prev) {
long max=0;
long min=0;
for (int i : adj[s]) {
if (i != prev) {
long[] dd=DFS(dp,i,s);
min+=Math.max(Math.abs(dp[s][0]-dp[i][0])+dd[0],Math.abs(dp[s][0]-dp[i][1])+dd[1]);
max+=Math.max(Math.abs(dp[s][1]-dp[i][0])+dd[0],Math.abs(dp[s][1]-dp[i][1])+dd[1]);
}
}
return new long[] {min,max};
}
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 long gcd(long x, long y) {
if (x == 0)
return y;
if (y == 0)
return x;
long r = 0, a, b;
a = Math.max(x, y);
b = Math.min(x, y);
r = b;
while (a % b != 0) {
r = a % b;
a = b;
b = r;
}
return r;
}
static long modulo(long a, long b, long c) {
long x = 1, y = a % c;
while (b > 0) {
if (b % 2 == 1)
x = (x * y) % c;
y = (y * y) % c;
b = b >> 1;
}
return x % c;
}
public static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
static int upper_bound(int[] arr, int n, int x) {
int mid;
int low = 0;
int high = n;
while (low < high) {
mid = low + (high - low) / 2;
if (x >= arr[mid])
low = mid + 1;
else
high = mid;
}
return low;
}
static int lower_bound(int[] arr, int n, int x) {
int mid;
int low = 0;
int high = n;
while (low < high) {
mid = low + (high - low) / 2;
if (x <= arr[mid])
high = mid;
else
low = mid + 1;
}
return low;
}
static String printPrecision(double d) {
DecimalFormat ft = new DecimalFormat("0.00000000000");
return String.valueOf(ft.format(d));
}
static int countBit(long mask) {
int ans = 0;
while (mask != 0) {
mask &= (mask - 1);
ans++;
}
return 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());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] readArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt();
return arr;
}
}
} | 0 | Non-plagiarised |
3a12e509 | fae0662f | 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.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 |
29cf2e70 | b2001d68 | 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.lang.*;
import java.io.*;
public class D753 {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
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();
}
char[] color=sc.next().toCharArray();
ArrayList<Integer> b=new ArrayList<>();
ArrayList<Integer> r=new ArrayList<>();
for(int i=0;i<n;i++){
if(color[i]=='B')b.add(arr[i]);
else r.add(arr[i]);
}
Collections.sort(b);
Collections.sort(r);
boolean ok=true;
int cur=1;
for(int i:b){
if(i>=cur)cur++;
else{
ok=false;
break;
}
//cur++;
}
for(int i:r){
if(i<=cur)cur++;
else{
ok=false;
break;
}
}
if(ok)System.out.println("YES");
else System.out.println("NO");
}
} catch (Exception e) {
return;
}
}
public static int lowerbound(long[] ar,int k)
{
int s=0;
int e=ar.length;
while (s !=e)
{
int mid = s+e>>1;
if (ar[mid] <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.length)
{
return -1;
}
return s;
}
public static class pair {
int ff;
char ss;
pair(int ff, char ss) {
this.ff = ff;
this.ss = ss;
}
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
public static int upperbound(long[] arr, int k) {
int s = 0;
int e = arr.length;
while (s != e) {
int mid = s + e >> 1;
if (arr[mid] <= k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == arr.length) {
return -1;
}
return s;
}
public static long pow(long x,long y,long mod){
if(x==0)return 0l;
if(y==0)return 1l;
//(x^y)%mod
if(y%2l==1l){
return ((x%mod)*(pow(x,y-1l,mod)%mod))%mod;
}
return pow(((x%mod)*(x%mod))%mod,y/2l,mod);
}
public static long gcd_long(long a, long b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_long(b, a % b);
}
public static int gcd_int(int a, int b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_int(b, a % b);
}
public static int lcm(int a, int b) {
int gcd = gcd_int(a, b);
return (a * b) / gcd;
}
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());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
* return false; }
*/
| 1 | Plagiarised |
1adac634 | ee0bc145 | import java.io.*;
import java.util.*;
public class Main {
static int i, j, k, n, m, t, y, x, sum = 0;
static long mod = 998244353;
static FastScanner fs = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static String str;
static long ans;
public static void main(String[] args) {
t = fs.nextInt();
while (t-- > 0) {
n = fs.nextInt();
int [] k = fs.readArray(n);
int [] h = fs.readArray(n);
List<Pair> arr = new ArrayList<>();
for(i=0;i<n;i++){
int a = k[i]-h[i]+1;
arr.add(new Pair(a,k[i]));
}
Collections.sort(arr);
ans=0;
List<Pair> comp = new ArrayList<>();
int temp = arr.get(0).x;
int tempy = arr.get(0).y;
for(i=1;i<n;i++){
if(arr.get(i).x > tempy){
comp.add(new Pair(temp, tempy));
temp = arr.get(i).x;
tempy = arr.get(i).y;
}
else tempy = Math.max(tempy, arr.get(i).y);
}
comp.add(new Pair(temp, tempy));
for(i=0;i<comp.size();i++){
long a = comp.get(i).y - comp.get(i).x +1 ;
ans+=(((a)*(a+1))/2);
}
out.println(ans);
}
out.close();
}
/*static long nck(int n , int k){
long a = fact[n];
long b = modInv(fact[k]);
b*= modInv(fact[n-k]);
b%=mod;
return (a*b)%mod;
}
static void populateFact(){
fact[0]=1;
fact[1] = 1;
for(i=2;i<300005;i++){
fact[i]=i*fact[i-1];
fact[i]%=mod;
}
}
*/
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long exp(long base, long pow) {
if (pow == 0) return 1;
long half = exp(base, pow / 2);
if (pow % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long mul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long add(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
static long modInv(long x) {
return exp(x, mod - 2);
}
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);
}
static void ruffleSort(long[] a) {
//ruffle
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 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());
}
}
static class Pair implements Comparable<Pair> {
public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
if (x == o.x)
return Integer.compare(y, o.y);
return Integer.compare(x, o.x);
}
}
} | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 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();
Stack<Pair> stk = new Stack<>();
stk.push(new Pair(0, 0));
for (int i = 0; i < n; ++i) {
int cur_pos = k[i];
int cur_inc = h[i];
while (!stk.isEmpty() && !(cur_pos - cur_inc + 1 > stk.peek().pos)) {
cur_inc = Math.max(cur_inc, stk.peek().inc + cur_pos - stk.peek().pos);
stk.pop();
}
stk.add(new Pair(cur_pos, cur_inc));
}
long answer = 0;
while (!stk.isEmpty()) {
answer += (1L * stk.peek().inc * (stk.peek().inc + 1) / 2);
stk.pop();
}
out.println(answer);
}
out.flush();
}
static class Pair {
int pos, inc;
public Pair (int pos, int inc) {
this.pos = pos;
this.inc = inc;
}
}
static class Scanner {
BufferedReader in;
StringTokenizer st;
public Scanner() {
this.in = new BufferedReader(new InputStreamReader(System.in));
}
public Scanner(FileReader f) {
this.in = new BufferedReader(f);
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public void close() throws IOException {
in.close();
}
}
}
| 0 | Non-plagiarised |
05ca89ed | 163d0dde |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class er106c
{
//By shwetank_verma
public static void main(String[] args)
{
FastReader sc=new FastReader();
try{
int t=1;
t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
long o=n,e=n;
long maxo=Integer.MAX_VALUE;
long maxe=Integer.MAX_VALUE;
long ans=Long.MAX_VALUE;
long temp=0;
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
if(i%2==1) {
temp+=a[i];
e--;
maxe=Long.min(maxe, a[i]);
ans=Long.min(ans,temp+(o*maxo)+(e*maxe));
}
else {
temp+=a[i];
o--;
maxo=Long.min(maxo, a[i]);
ans=Long.min(ans,temp+(o*maxo)+(e*maxe));
}
}
System.out.println(ans);
}
}catch(Exception e){
return;
}
}
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 mod=1000000007;
static boolean primes[]=new boolean[1000007];
static ArrayList<Integer> b=new ArrayList<>();
static boolean seive(int n){
Arrays.fill(primes,true);
primes[0]=primes[1]=false;
for(int i=2;i*i<=n;i++){
if(primes[i]==true){
for(int p=i*i;p<=n;p+=i){
primes[p]=false;
}
}
}
if(n<1000007){
for(int i=2;i<=n;i++) {
if(primes[i])
b.add(i);
}
return primes[n];
}
return false;
}
static int gcd(int a,int b){
if(b==0)
return a;
return gcd(b,a%b);
}
static long GCD(long a,long b){
if(b==0)
return a;
return GCD(b,a%b);
}
static ArrayList<Integer> segseive(int l,int r){
ArrayList<Integer> isprime=new ArrayList<Integer>();
boolean p[]=new boolean[r-l+1];
Arrays.fill(p, true);
for(int i=0;b.get(i)*b.get(i)<=r;i++) {
int currprime=b.get(i);
int base=(l/currprime)*currprime;
if(base<l) {
base+=currprime;
}
for(int j=base;j<=r;j+=currprime) {
p[j-l]=false;
}
if(base==currprime) {
p[base-l]=true;
}
}
for(int i=0;i<=r-l;i++) {
if(p[i])
isprime.add(i+l);
}
return isprime;
}
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;
}
} | // 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);
}
}
} | 1 | Plagiarised |
4552d8a0 | ec4c7e8e | 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 D_Blue_Red_Permutation{
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();
long array[]= new long[n];
for(int i=0;i<n;i++){
array[i]=s.nextLong();
}
String str=s.nextToken();
ArrayList<Long> red = new ArrayList<Long>();
ArrayList<Long> blue = new ArrayList<Long>();
for(int i=0;i<n;i++){
if(str.charAt(i)=='R'){
red.add(array[i]);
}
else{
blue.add(array[i]);
}
}
Collections.sort(blue);
int check1=0;
for(int i=0;i<blue.size();i++){
int yo=i+1;
if(blue.get(i)<yo){
check1=1;
break;
}
}
Collections.sort(red,Collections.reverseOrder());
int number=n;
int check2=0;
for(int i=0;i<red.size();i++){
if(red.get(i)>number){
check2=1;
break;
}
number--;
}
if(check1==0 && check2==0){
res.append("YES\n");
}
else{
res.append("NO\n");
}
p++;
}
System.out.println(res);
}
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());
}
}
}
| import java.util.*;
public class the_child_and_set
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
in.nextLine();
while(t--!=0)
{
int n=in.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=in.nextInt();
}
in.nextLine();
String s=in.nextLine();
int rc=0;
int bc=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='B')
bc++;
else
rc++;
}
int r[]=new int[rc];
int b[]=new int[bc];
int bi=0;
int ri=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='B')
{
b[bi]=arr[i];
bi++;
}
else
{
r[ri]=arr[i];
ri++;
}
}
Arrays.sort(b);
Arrays.sort(r);
boolean flag=true;
for(int i=0;i<bi;i++)
{
if(b[i]<(i+1))
{
flag=false;
break;
}
}
if(flag)
{
for(int i=0;i<ri;i++)
{
if(r[i]>bi+(i+1))
{
flag=false;
break;
}
}
}
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | 0 | Non-plagiarised |
0c0af0ff | d6a8d884 | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
long mod=1000000007;
void solve() throws IOException {
for (int tc=ni();tc>0;tc--) {
int n=ni();
int[]A=new int[n];
long[]T=new long[n];
A[0]=ni();
T[0]=A[0];
long total=0;
for (int i=1;i<n;i++) { A[i]=ni(); T[i]=T[i-1]+A[i]; }
long[]B=new long[n];
long lefteven=n-1;
long leftodd=n;
int mineven=A[0];
int minodd=A[1];
long ans=Long.MAX_VALUE;
for (int i=1;i<n;i++) {
if (i%2==1) {
leftodd--;
minodd=Math.min(minodd,A[i]);
B[i]=T[i]+lefteven*mineven+leftodd*minodd;
}
else {
lefteven--;
mineven=Math.min(mineven,A[i]);
B[i]=T[i]+lefteven*mineven+leftodd*minodd;
}
ans=Math.min(ans,B[i]);
}
out.println(ans);
}
out.flush();
}
int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); }
long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); }
long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; }
public static void main(String[] args) throws IOException {
new Main().solve();
}
} | //Praise our lord and saviour qlf9
//DecimalFormat f = new DecimalFormat("##.00");
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class C{
public static void main(String[] omkar) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
StringBuilder sb = new StringBuilder();
int cases = Integer.parseInt(st.nextToken());
for(int i = 0; i < cases; i++)
{
solve(in, st, sb);
}
System.out.println(sb);
}
public static void solve(BufferedReader in, StringTokenizer st, StringBuilder sb) throws Exception
{
st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
int[] arr = readArr(n, in, st);
int[] mins = new int[n];
mins[0] = arr[0];
mins[1] = arr[1];
for(int i = 2; i < n; i++)
{
mins[i] = Math.min(arr[i], mins[i-2]);
}
long[] sums = new long[n];
sums[0] = (long)(arr[0]);
for(int i = 1; i < n; i++)
{
sums[i] = sums[i-1]+(long)(arr[i]);
}
long minc = Long.MAX_VALUE;
long temp;
for(int i = 1; i < n; i++)
{
temp = sums[i];
temp += (long)(mins[i])*(long)(n-(i+2)/2);
temp += (long)(mins[i-1])*(long)(n-(i+1)/2);
minc = Math.min(minc, temp);
}
sb.append(minc+"\n");
}
public static int[] readArr(int N, BufferedReader in, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(in.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
} | 0 | Non-plagiarised |
25597bcb | 86232d21 | 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.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class PhoenixAndTowers { // Template for CF
public static class ListComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> l1, List<Integer> l2) {
for (int i = 0; i < l1.size(); ++i) {
if (l1.get(i).compareTo(l2.get(i)) != 0) {
return l1.get(i).compareTo(l2.get(i));
}
}
return 0;
}
}
public static class Pair {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
public void setFirst(int a) {
first = a;
}
@Override
public String toString() {
return first + " " + second;
}
}
public static void main(String[] args) throws IOException {
// Check for int overflow!!!!
// Should you use a long to store the sum or smthn?
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = Integer.parseInt(f.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
List<Integer> list = new ArrayList<>();
for (int j = 0; j < n; j++) {
int a = Integer.parseInt(st.nextToken());
list.add(a);
}
ArrayList<Integer> copy = new ArrayList<>(list);
Collections.sort(list);
TreeSet<List<Integer>> set = new TreeSet<>(new ListComparator());
for (int j = 1; j <= m; j++) {
List<Integer> temp = new ArrayList<>();
temp.add(0);
temp.add(j);
set.add(temp);
// System.out.println(temp);
}
// System.out.println(set);
Map<Integer, LinkedList<Integer>> map = new HashMap<>();
for (int j = n - 1; j >= 0; j--) {
if (map.containsKey(list.get(j))) {
map.get(list.get(j)).addLast(set.first().get(1));
} else {
map.put(list.get(j), new LinkedList<>());
map.get(list.get(j)).addLast(set.first().get(1));
}
List<Integer> temp = new ArrayList<>();
temp.add(set.first().get(0) + list.get(j));
temp.add(set.pollFirst().get(1));
set.add(temp);
}
// System.out.println(set);
if (set.last().get(0) - set.first().get(0) > x) {
out.println("NO");
} else {
out.println("YES");
for (int j = 0; j < n; j++) {
out.print(map.get(copy.get(j)).pollFirst() + " ");
}
out.println();
}
}
out.close();
}
} | import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.exit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class C {
static class Token implements Comparable<Token> {
final int i, h;
Token(int i, int h) {
this.i = i;
this.h = h;
}
public int compareTo(Token o) {
return h - o.h;
}
}
static void solve() throws Exception {
int tests = scanInt();
for (int test = 0; test < tests; test++) {
int n = scanInt(), m = scanInt();
scanInt();
PriorityQueue<Token> pq = new PriorityQueue<>();
int h[] = new int[m];
int ans[] = new int[n];
for (int i = 0; i < m; i++) {
pq.add(new Token(i, 0));
}
for (int i = 0; i < n; i++) {
Token cur = pq.remove();
pq.add(new Token(cur.i, h[cur.i] += scanInt()));
ans[i] = cur.i;
}
out.println("YES");
for (int i = 0; i < n; i++) {
out.print((ans[i] + 1) + " ");
}
out.println();
}
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static String scanString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
} | 0 | Non-plagiarised |
81a9086f | baebdc56 |
import java.io.*;
import java.util.*;
public class D
{
static long mod=998244353;
static long[] facts, factInvs;
public static void main(String[] args)throws IOException
{
FastReader f=new FastReader();
StringBuffer sb = new StringBuffer();
int n=f.nextInt();
int k=f.nextInt();
Time time[]=new Time[n];
for(int i=0;i<n;i++)
{
int x=f.nextInt();
int y=f.nextInt();
time[i]=new Time(x,y);
}
Arrays.sort(time);
precomp();
// for(Time t : time)
// System.out.println(t.x+" "+t.y);
int curr=0;
long ans=0;
Queue<Integer> pq=new PriorityQueue<>();
for(int i=0;i<n;i++)
{
// System.out.println("time seg = "+time[i].x+" "+time[i].y);
int x=time[i].x;
pq.add(time[i].y);
curr++;
while(!pq.isEmpty() && pq.peek()<x)
{
pq.poll();
curr--;
}
// System.out.println("curr = "+curr);
if(curr<k)
continue;
else
{
long add=getNcR(curr-1,k-1)%mod;
ans=(ans%mod + add)%mod;
// System.out.println("add = "+add);
}
}
System.out.println(ans);
}
static void precomp()
{
facts=new long[1000000];
factInvs=new long[1000000];
factInvs[0]=facts[0]=1;
for (int i=1; i<facts.length; i++)
facts[i]=mul(facts[i-1], i);
factInvs[facts.length-1]=modInv(facts[facts.length-1]);
for (int i=facts.length-2; i>=0; i--)
factInvs[i]=mul(factInvs[i+1], i+1);
}
static long getNcR(int n, int k) {
return mul(facts[n], mul(factInvs[k], factInvs[n-k]));
}
static long mul(long a, long b) {
return a*b%mod;
}
static long modInv(long x) {
return exp(x, mod-2);
}
static long exp(long base, long e)
{
if (e==0)
return 1;
long half=exp(base, e/2);
if (e%2==0)
return mul(half, half);
else
return mul(half, mul(half, base));
}
static class Time implements Comparable<Time>
{
int x,y;
Time(int x,int y)
{
this.x=x;
this.y=y;
}
@Override
public int compareTo(Time o)
{
return Integer.compare(x,o.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;
}
}
} | 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 D {
static final int mod=998244353;
static long[] facts, factInvs;
public static void main(String[] args) {
precomp();
FastScanner fs=new FastScanner();
int n=fs.nextInt(), k=fs.nextInt();
Seg[] segs=new Seg[n];
for (int i=0; i<n; i++) segs[i]=new Seg(fs.nextInt(), fs.nextInt());
Event[] events=new Event[n*2];
for (int i=0; i<n; i++) {
events[2*i]=new Event(segs[i], true);
events[2*i+1]=new Event(segs[i], false);
}
long ans=0;
Arrays.sort(events);
int counter=0;
for (Event e:events) {
if (e.start) {
counter++;
}
else {
counter--;
if (counter+1<k)
continue;
else
ans=add(ans, nCk(counter, k-1));
}
}
System.out.println(ans);
}
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 long add(long a, long b) {
return (a+b)%mod;
}
static long mul(long a, long b) {
return a*b%mod;
}
static long exp(long base, long e) {
if (e==0) return 1;
long half=exp(base, e/2);
if (e%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long modInv(long x) {
return exp(x, mod-2);
}
static void precomp() {
facts=new long[1_000_000];
factInvs=new long[1_000_000];
factInvs[0]=facts[0]=1;
for (int i=1; i<facts.length; i++)
facts[i]=mul(facts[i-1], i);
factInvs[facts.length-1]=modInv(facts[facts.length-1]);
for (int i=facts.length-2; i>=0; i--)
factInvs[i]=mul(factInvs[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(facts[n], mul(factInvs[k], factInvs[n-k]));
}
static class Seg {
int l, r;
public Seg(int l, int r) {
this.l=l;
this.r=r;
}
}
static class Event implements Comparable<Event> {
boolean start;
Seg s;
public Event(Seg s, boolean start) {
this.s=s;
this.start=start;
}
int x() {
if (start) return s.l;
return s.r;
}
public int compareTo(Event o) {
if (x()!=o.x()) return Integer.compare(x(), o.x());
if (start==o.start)return 0;
if (start) return -1;
return 1;
}
}
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());
}
}
}
| 0 | Non-plagiarised |
14b0fb8e | 6490bbe8 | import java.io.*;
import java.util.*;
public class Solution {
static long res;
public static void main(String[] args) throws Exception
{
FastReader fr=new FastReader();
int n=fr.nextInt();
ArrayList<Integer> oc=new ArrayList<>();
ArrayList<Integer> em=new ArrayList<>();
res=Long.MAX_VALUE;
for(int i=0;i<n;i++) {
int v=fr.nextInt();
if(v==1)
oc.add(i);
else
em.add(i);
}
Collections.sort(oc);
Collections.sort(em);
long dp[][]=new long[5001][5001];
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[i].length;j++) {
dp[i][j]=-1;
}
}
System.out.println(getMin(oc,em,0,0,dp));
}
public static long getMin(ArrayList<Integer> oc,ArrayList<Integer> em,int idx,int j,long dp[][]) {
if(idx==oc.size())
return 0;
long available=em.size()-j;
long req=oc.size()-idx;
if(available<req)
return Integer.MAX_VALUE;
if(dp[idx][j]!=-1)
return dp[idx][j];
long ch1=getMin(oc,em,idx,j+1,dp);
long ch2=getMin(oc,em,idx+1,j+1,dp)+Math.abs(em.get(j)-oc.get(idx));
return dp[idx][j]=Math.min(ch1,ch2);
}
public static String lcs(String a,String b) {
int dp[][]=new int[a.length()+1][b.length()+1];
for(int i=0;i<=a.length();i++)
{
for(int j=0;j<=b.length();j++)
{
if(i==0||j==0)
dp[i][j]=0;
}
}
for(int i=1;i<=a.length();i++)
{
for(int j=1;j<=b.length();j++)
{
if(a.charAt(i-1)==b.charAt(j-1))
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
}
}
int i=a.length();
int j=b.length();
String lcs="";
while(i>0&&j>0)
{
if(a.charAt(i-1)==b.charAt(j-1)) {
lcs=a.charAt(i-1)+lcs;
i--;
j--;
}
else
{
if(dp[i-1][j]>dp[i][j-1])
i--;
else
j--;
}
}
return lcs;
}
public static long facto(long n) {
if(n==1||n==0)
return 1;
return n*facto(n-1);
}
public static long gcd(long n1,long n2) {
if (n2 == 0) {
return n1;
}
return gcd(n2, n1 % n2);
}
public static boolean isPali(String s) {
int i=0;
int j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j))
return false;
i++;
j--;
}
return true;
}
public static String reverse(String s) {
String res="";
for(int i=0;i<s.length();i++) {
res+=s.charAt(i);
}
return res;
}
public static int bsearch(long suf[],long val) {
int i=0;
int j=suf.length-1;
while(i<=j) {
int mid=(i+j)/2;
if(suf[mid]==val)
return mid;
else if(suf[mid]<val)
j=mid-1;
else
i=mid+1;
}
return -1;
}
public static int[] getFreq(String s) {
int a[]=new int[26];
for(int i=0;i<s.length();i++) {
a[s.charAt(i)-'a']++;
}
return a;
}
public static boolean isPrime(int n) {
for(int i=2;(i*i)<=n;i++) {
if(n%i==0)
return false;
}
return true;
}
}
class Pair{
long i;
long j;
Pair(long num,long freq){
this.i=num;
this.j=freq;
}
}
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.*;
// 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);
// }
// }
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
// primes();
// ________________________________
// int t = sc.nextInt();
// StringBuilder output = new StringBuilder();
// while (t-- > 0) {
// output.append(solver()).append("\n");
// }
// out.println(output);
// _______________________________
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
out.println(solver(n, arr));
// ________________________________
out.flush();
}
public static long solver(int n, int[] arr) {
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
for(int i=0;i<n;i++){
if(arr[i] ==1){
a.add(i);
}
else{
b.add(i);
}
}
// System.out.println("__"+ a);
// System.out.println("__"+ b);
long inf = (long)1e10;
int aLen = a.size(), bLen = b.size();
long[][] dp = new long[bLen+1][aLen+1];
for(int i=0;i<bLen+1;i++)Arrays.fill(dp[i],inf);
// dp[0][0] = 0;
for(int i=0;i<=bLen;i++){
dp[i][0] = 0;
}
for(int i=1;i<=bLen;i++){
for(int j=1;j<=i && j<=aLen;j++){
int aa = a.get(j-1);
int bb = b.get(i-1);
// System.out.println((i-1)+" "+(j-1)+"__"+ aa+" "+bb);
dp[i][j] = Math.min(
Math.abs(aa-bb)+dp[i-1][j-1],
dp[i-1][j]
);
// System.out.println((i-1)+" "+(j-1)+"__"+ dp[i][j]);
}
}
// for(int i=0;i<=bLen;i++){
// for(int j=0;j<=aLen;j++){
// System.out.print(dp[i][j]+" ");
// }
// System.out.println("__" );
// }
return dp[bLen][aLen]==inf?0:dp[bLen][aLen];
}
} | 0 | Non-plagiarised |
701c3f35 | d56bcb0c | import java.util.*;
import java.io.*;
public class Menorah {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(f.readLine());
while(t-->0) {
int n=Integer.parseInt(f.readLine());
String s=f.readLine();
String s2=f.readLine();
int[] arr=new int[4];
//0 00 1 01 2 10 3 11
for(int i=0;i<n;i++) {
if(s.charAt(i)=='0') {
if(s2.charAt(i)=='0') {
arr[0]++;
}
else {
arr[1]++;
}
}
else {
if(s2.charAt(i)=='0') {
arr[2]++;
}
else {
arr[3]++;
}
}
}
int min=Integer.MAX_VALUE;
if(arr[1]==arr[2]) {
min=arr[1]*2;
}
int temp=arr[1];
arr[1]=arr[3];
arr[3]=temp;
temp=arr[0];
arr[0]=arr[2];
arr[2]=temp;
arr[3]++;
arr[1]--;
if(arr[1]==arr[2]) {
min=Math.min(min, arr[1]*2+1);
}
System.out.println(min!=Integer.MAX_VALUE?min:-1);
}
}
}
| import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static int func(char a[],char b[]){
int n=a.length;
int a1=0,b1=0;
for(int i=0;i<n;i++){
if(a[i]=='1')a1++;
if(b[i]=='1')b1++;
}
if(a1!=b1)return 100000000;
int cnt=0;
for(int i=0;i<n;i++){
if(a[i]=='1'&&b[i]!='1')cnt++;
}
return cnt*2;
}
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//int t=1;
int t=Integer.parseInt(br.readLine());
while(--t>=0){
int n=Integer.parseInt(br.readLine());
char a[]=br.readLine().toCharArray();
char b[]=br.readLine().toCharArray();
int x=func(a,b);
int ind=-1;
for(int i=0;i<n;i++){
if(a[i]==b[i]&&a[i]=='1'){
ind=i;
break;
}
}
int y=100000000;
if(ind>=0){
for(int i=0;i<n;i++){
if(i==ind)continue;
if(a[i]=='1')a[i]='0';
else a[i]='1';
}
y=func(a,b)+1;
}
if(x>=1000000&&y>=1000000){
System.out.println(-1);
}
else
System.out.println(Math.min(x,y));
}
}
}
| 0 | Non-plagiarised |
04df7bb8 | 85125ecb | import java.math.BigInteger;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
static HashMap<Integer,Boolean>map;
static long dp[][];
static boolean flag;
static HashSet<Long>hs;
static long mod=(long)(1e9+7);
public static void main(String[] args) {
StringBuilder ans=new StringBuilder();
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
//int n=sb.length();
int k=sc.nextInt();
long L[]=new long[n];
long R[]=new long[n];
int a[]=new int[k];
int temp[]=new int[k];
for(int i=0;i<k;i++)
a[i]=sc.nextInt();
for(int i=0;i<k;i++)
temp[i]=sc.nextInt();
int c[]=new int [n];
Arrays.fill(c, Integer.MAX_VALUE);
for(int i=0;i<k;i++)
c[a[i]-1]=temp[i];
long p=Integer.MAX_VALUE;
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++)
{
ans.append(Math.min(L[i], R[i])+" ");
}
ans.append("\n");
}
System.out.println(ans);
}
static class Sparse{
int log[];
long sparse[][];
Sparse(int n){
log=new int [n+1];
for(int i=1;i<n+1;i++)
{
log[i]=log[i/2]+1;
}
sparse=new long[n][18];
}
}
static long solve(String X,String Y,int n,int m,int M){
if(m==0&&n==0)return 0;
// if(m==1&&X.charAt(n-1)!='u')return Integer.MIN_VALUE;
if(n==0||m==0)return Integer.MIN_VALUE;
if(dp[n][m]!=-1)return dp[n][m];
if(Y.charAt(m-1)==X.charAt(n-1))
{
// else if(n==1)return Integer.MAX_VALUE;
return dp[n][m]=1+ Math.max(solve (X,Y,n-1,m,M),solve (X,Y,n-1,m-1,M));
}
else{
return dp[n][m]= solve(X,Y,n-1,m,M);
}
}
static long solve(long [][]g,int n,int m,boolean visited [][]) {
if(n==0||m==0)return 0;
visited[n][m]=true;
long ans=g[n-1][m-1]+Math.max(solve(g, n, m-1, visited),solve(g, n-1, m, visited));
visited[n][m]=false;
return ans;
}
static boolean isP(long x,long n,long m) {
return (x^n)<=m;
}
static class pair{
int n;int i;
pair(int n,int i){
this.n=n;
this.i=i;
}
}
static long solve(char g[][],int n,int m) {
if(n==0||m==0)return 0;
if(n==1&&m==1)return 1;
if(g[n-1][m-1]=='#')return 0;
if(dp[n][m]!=-1)return dp[n][m];
return dp[n][m]= (solve(g, n-1, m)%mod+solve(g, n, m-1)%mod)%mod;
}
static int solve (int v,ArrayList<ArrayList<Integer>>adj,int d[],boolean visited[]) {
visited[v]=true;
for(int u:adj.get(v)) {
if(!visited[u])
{
solve(u, adj, d, visited);
}
d[v]=Math.max(1+d[u], d[v]);
}
return d[v];
}
static class colors{
int c;int n;
public colors(int c,int n) {
// TODO Auto-generated constructor stub
this.c=c;
this.n=n;
}
}
static int CeilIndex(long A[], int l, int r, long key)
{
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key)
r = m;
else
l = m;
}
return r;
}
static int CeilIndexd(long A[], int l, int r, long key)
{
while (r - l > 1) {
int m = l + (r - l) / 2;
if (-1*A[m] >= key)
r = m;
else
l = m;
}
return r;
}
static void solve(ArrayList<Long>A,long bd) {
if(bd>(long)1e9)return;
// if(hs.contains(bd))return;
//A.add(bd);
hs.add(bd);
A.add(bd*10);
A.add(bd*10+1);
// hs.add(bd*10);
//hs.add(bd*10+1);
solve(A,bd*10);
solve(A,bd*10+1);
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long lcm(long a, long b)
{
return ((a / gcd(a, b))%mod * b%mod)%mod;
}
static long gcd(long a,long b)
{
if (a == 0)
return b%mod;
return (gcd(b % a, a))%mod;
}
//System.out.println(count);
static void dfs(int v,boolean visited[],ArrayList<ArrayList<Integer>>adj,int div[],int t)
{
visited[v]=true;
div[v]=t+1;
for(int u:adj.get(v)) {
if(!visited[u]) {
dfs(u,visited,adj,div,(t+1)%2);
}
}
}
static class Helper{
int a;int b;int t;
public Helper(int a,int b) {
// TODO Auto-generated constructor stub
this.a=a;
this.b=b;
}
}
// System.out.println(max);
//System.out.println(ans.toString());
//main(
static void solvedfs(ArrayList<ArrayList<Integer>>adj,int n,int v,int subt[],int subtAns[],boolean []visited) {
int count=1;
int ans=0;
visited[v]=true;
for(int u:adj.get(v)) {
if(!visited[u])
{
//System.out.println(v+" "+subt[v]+" "+n);
subtAns[u]=Math.max(subtAns[u], subt[v]-subtAns[u]);
solvedfs(adj, n, u, subt, subtAns, visited);
}
}
}
static int dfs(ArrayList<ArrayList<Integer>>adj,int v,int subt[],int subtAns[],boolean []visited) {
int count=0;
int ans=0;
visited[v]=true;
for(int u:adj.get(v)) {
if(!visited[u])
{
count+=ans;
ans=Math.max(dfs(adj,u,subt,subtAns,visited),ans);
}
}
subt[v]=count;
subtAns[v]=ans;
return ans+1;
}
static int solve(ArrayList<ArrayList<Integer>>adj,int node,ArrayList<Integer>A)
{
if(adj.get(node).size()==0)return 1;
int count=0;
for(int v:adj.get(node)) {
count+=solve(adj,v,A);
}
A.set(node,count);
return count+1;
}
static void dfs(String[]building,int i,int j,int n,int m, boolean visited[][]) {
visited[i][j]=true;
if(isValid(building,i+1,j,n,m,visited))
{ visited[i+1][j]=true;
dfs(building,i+1,j,n,m,visited);
}
if(isValid(building,i-1,j,n,m,visited))
{
visited[i-1][j]=true;
dfs(building,i-1,j,n,m,visited);
}
if(isValid(building,i,j+1,n,m,visited))
{visited[i][j+1]=true;
dfs(building,i,j+1,n,m,visited);
}
if(isValid(building,i,j-1,n,m,visited))
{visited[i][j-1]=true;
dfs(building,i,j-1,n,m,visited);
}
}
static boolean isValid(String[]building,int i,int j,int n,int m, boolean visited[][])
{
if(i==-1||j==-1||i==n||j==m||visited[i][j]||building[i].charAt(j)=='#')
return false;
return true;
}
static void compute(boolean sieve[],int n) {
for(int i=2;i<=n;i++) {
if(sieve[i])continue;
for(int j=2*i;j<n;j+=i)
{
sieve[j]=true;
}
}
}
static void computeHs(boolean sieve[]) {
int n=(int)(1e9-1e7+1);
for(int i=1;i<n;i++) {
if(sieve[i])continue;
for(int j=2*i;j<n;j+=i)
{
sieve[j]=true;
}
}
}
static boolean isValid(StringBuilder s,int w) {
if(w>s.length())return false;
HashSet<Character>hs=new HashSet<Character>();
int a[]=new int[3];
for(int i=0;i<w;i++) {
++a[s.charAt(i)-49];
}
if(a[0]>0&&a[1]>0&&a[2]>0)return true;
int start=0;
int end=w;
while(end!=s.length()) {
--a[s.charAt(start)-49];
++a[s.charAt(end)-49];
start++;
end++;
if(a[0]>0&&a[1]>0&&a[2]>0)return true;
}
return false;
}
static int find(int parent[],int i) {
if(parent[i]==-1)return i;
return parent[i]=find(parent,parent[i]);
}
static void union(int parent[],int rank[],int s1,int s2) {
if(rank[s1]>=rank[s2]) {
parent[s2]=s1;
rank[s1]+=rank[s2];
}
else {
parent[s1]=s2;
rank[s2]+=rank[s1];
}
}
static int solve(String S,int K) {
if(K<=0)return 0;
if(S.charAt(K-1)!=S.charAt(K))
return 1+solve(S,K-1);
else return solve(S,K-1);
}
static boolean isValid(int g[][],int r,int c,int n,int m,boolean visited[][],int s) {
if(r==-1||r==n||c==-1||c==m||visited[r][c]||g[r][c]!=s)return false;
return true;
}
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.Arrays;
import java.util.Hashtable;
public class AirConditionersUpsolve{
static class InputReader {
private static final int DEFAULT_BUFFER_SIZE = 1 << 16; // Change this to increase your input size //
private static final InputStream DEFAULT_STREAM = System.in;private static final int MAX_DECIMAL_PRECISION = 21;private int c;private byte[] buf;private int bufferSize, bufIndex, numBytesRead;private InputStream stream;private static final byte EOF = -1;private static final byte NEW_LINE = 10;private static final byte CARRIAGE_RETURN = 13;private static final byte SPACE = 32;private static final byte DASH = 45;private static final byte DOT = 46;private char[] charBuffer;private static byte[] bytes = new byte[58];private static int[] ints = new int[58];private static char[] chars = new char[128];static { char ch = ' ';int value = 0;byte _byte = 0;for (int i = 48; i < 58; i++) bytes[i] = _byte++;for (int i = 48; i < 58; i++) ints[i] = value++;for (int i = 32; i < 128; i++) chars[i] = ch++; }private static final double[][] doubles = {{0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d, 0.0000000000d, 0.00000000000d, 0.000000000000d, 0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d, 0.00000000000000000d, 0.000000000000000000d, 0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d}, {0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d, 0.0000000001d, 0.00000000001d, 0.000000000001d, 0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d, 0.00000000000000001d, 0.000000000000000001d, 0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d}, {0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d, 0.0000000002d, 0.00000000002d, 0.000000000002d, 0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d, 0.00000000000000002d, 0.000000000000000002d, 0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d}, {0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d, 0.0000000003d, 0.00000000003d, 0.000000000003d, 0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d, 0.00000000000000003d, 0.000000000000000003d, 0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d}, {0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d, 0.0000000004d, 0.00000000004d, 0.000000000004d, 0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d, 0.00000000000000004d, 0.000000000000000004d, 0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d}, {0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d, 0.0000000005d, 0.00000000005d, 0.000000000005d, 0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d, 0.00000000000000005d, 0.000000000000000005d, 0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d}, {0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d, 0.0000000006d, 0.00000000006d, 0.000000000006d, 0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d, 0.00000000000000006d, 0.000000000000000006d, 0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d}, {0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d, 0.0000000007d, 0.00000000007d, 0.000000000007d, 0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d, 0.00000000000000007d, 0.000000000000000007d, 0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d}, {0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d, 0.0000000008d, 0.00000000008d, 0.000000000008d, 0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d, 0.00000000000000008d, 0.000000000000000008d, 0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d}, {0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d, 0.0000000009d, 0.00000000009d, 0.000000000009d, 0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d, 0.00000000000000009d, 0.000000000000000009d, 0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d}};
public InputReader() { this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE); }
public InputReader(int bufferSize) { this(DEFAULT_STREAM, bufferSize); }
public InputReader(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); }
public InputReader(InputStream stream, int bufferSize) { if (stream == null || bufferSize <= 0) throw new IllegalArgumentException();buf = new byte[bufferSize];charBuffer = new char[128];this.bufferSize = bufferSize;this.stream = stream; }
private byte read() throws IOException { if (numBytesRead == EOF) throw new IOException();if (bufIndex >= numBytesRead) { bufIndex = 0;numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF; }return buf[bufIndex++]; }
private int readJunk(int token) throws IOException { if (numBytesRead == EOF) return EOF;
do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > token) return 0;bufIndex++; }
numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF;bufIndex = 0; } while (true); }
public byte nextByte() throws IOException {return (byte) nextInt(); }
public int nextInt() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1, res = 0;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); }
public long nextLong() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1;long res = 0L;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); }
private void doubleCharBufferSize() { char[] newBuffer = new char[charBuffer.length << 1];for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i];charBuffer = newBuffer; }
public String nextLine() throws IOException { try { c = read(); } catch (IOException e) { return null; }if (c == NEW_LINE) return "";if (c == EOF) return null;int i = 0;charBuffer[i++] = (char) c;do { while (bufIndex < numBytesRead) { if (buf[bufIndex] != NEW_LINE && buf[bufIndex] != CARRIAGE_RETURN) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else { if(buf[bufIndex] == CARRIAGE_RETURN) bufIndex++;bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } while (true); }
public String nextString() throws IOException { if (numBytesRead == EOF) return null;if (readJunk(SPACE) == EOF) return null;for (int i = 0; ; ) { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else {bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } }
public double nextDoubleFast() throws IOException {c = read();int sgn = 1;while (c <= SPACE) c = read();if (c == DASH) { sgn = -1;c = read(); }double res = 0.0;while (c > DOT) { res *= 10.0;res += ints[c];c = read(); }if (c == DOT) { int i = 0;c = read();while (c > SPACE && i < MAX_DECIMAL_PRECISION) { res += doubles[ints[c]][i++];c = read(); } }return res * sgn; }
public void close() throws IOException {stream.close(); }
}
// region variables
static InputReader sc = new InputReader();
static OutputStream outputStream = System.out;
static PrintWriter w = new PrintWriter(outputStream);
// endregion
private static void initiateIO()
throws IOException {if (System.getProperty("ONLINE_JUDGE") == null) { try { w = new PrintWriter("output.txt");sc = new InputReader(new FileInputStream("input.txt")); } catch (Exception e) { throw new IOException(); }} }
public static void main(String[] args)
throws IOException {
initiateIO();
int t = sc.nextInt();
while(t-- > 0) {
solve();
}
w.close();
}
static void solve() throws IOException {
int n = sc.nextInt();
int k = sc.nextInt();
int[] ac = new int[n+1];
Arrays.fill(ac, Integer.MAX_VALUE);
int[] pos = new int[k+1];
for(int i = 1; i <= k; i++) {
pos[i] = sc.nextInt();
}
for(int i = 1; i <= k; i++) {
int posi = pos[i];
int temp = sc.nextInt();
ac[posi] = temp;
}
long[] prev = new long[n+1];
long temp = Integer.MAX_VALUE;
for(int i = 1; i <= n; i++) {
temp = Math.min(temp+1, ac[i]);
prev[i] = temp;
}
long[] next = new long[n+1];
temp = Integer.MAX_VALUE;
for(int i = n; i > 0; i--) {
temp = Math.min(temp+1, ac[i]);
next[i] = temp;
}
for(int i = 1; i <= n; i++) {
w.print(Math.min(prev[i], next[i])+" ");
}
w.println();
}
} | 1 | Plagiarised |
0d11fb94 | 8c79c384 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static int i, j, k, n, m, t, y, x, sum = 0;
static long mod = 1000000007;
static FastScanner fs = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static String str;
static long ans;
static List<Integer> zeros = new ArrayList<>();
static List<Integer> ones = new ArrayList<>();
static int oneCount = 0;
static int[][] dp = new int[5005][5005];
public static void main(String[] args) {
t = 1;
while (t-- > 0) {
n = fs.nextInt();
for(int i = 0;i<n;i++){
x = fs.nextInt();
if(x==1){
ones.add(i);
oneCount++;
}
else
zeros.add(i);
}
for(int i=0;i<n;i++){
for(int j = 0; j<n;j++){
dp[i][j]=-1;
}
}
out.println(minCost(0,0));
}
out.close();
}
static int minCost(int zIndex, int oIndex){
if(oIndex == ones.size())
return 0;
if(zIndex == zeros.size())
return 1000000007;
if(dp[zIndex][oIndex]==-1)
dp[zIndex][oIndex]= Math.min(Math.abs(zeros.get(zIndex) - ones.get(oIndex))+minCost(zIndex+1, oIndex+1) , minCost(zIndex+1, oIndex));
return dp[zIndex][oIndex];
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
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);
}
static void ruffleSort(long[] a) {
//ruffle
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;
}
//then sort
Arrays.sort(a);
}
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());
}
}
static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long first, long second) {
this.first = first;
this.second = second;
}
@Override
public int compareTo(Pair o) {
return Long.compare(first, o.first);
}
}
} | /*
JAI MATA DI
*/
import java.util.*;
import javax.print.attribute.HashAttributeSet;
import java.io.*;
import java.math.BigInteger;
import java.sql.Array;
public class CP {
static class FR{
BufferedReader br;
StringTokenizer st;
public FR() {
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 mod = 1000000007;
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
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 class Pair implements Comparable<Pair>{
int ind;
int val;
Pair(int key , int value){
this.ind = key;
this.val = value;
}
@Override
public int compareTo(Pair o) {
return this.val - o.val;
}
// @Override
// public int hashCode(){
// return first + second;
// }
}
/* ***************************************************************************************************************************************************/
static FR sc = new FR();
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) {
// int tc = sc.nextInt();
// while(tc-- > 0) {
TEST_CASE();
// }
System.out.println(sb);
}
static void TEST_CASE() {
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0 ; i< n ;i++) {
arr[i] = sc.nextInt();
}
ArrayList<Integer> ao = new ArrayList<Integer>();
ArrayList<Integer> az = new ArrayList<Integer>();
for(int i = 0 ; i< n ;i++) {
if(arr[i] == 1) ao.add(i);
else az.add(i);
}
long[][] dp = new long[n+1][n+1];
for(int i = 0 ; i<n ; i++) Arrays.fill(dp[i], -1);
sb.append(fnc(dp, ao, az, 0, 0));
}
static long fnc(long[][] dp ,ArrayList<Integer> ao , ArrayList<Integer> az ,int i , int j) {
if(i == ao.size()) return 0;
if(j == az.size()) return Long.parseLong("1000000000000");
if(dp[i][j] != -1) return dp[i][j];
long a = Math.abs(ao.get(i) - az.get(j)) + fnc(dp, ao, az, i+1, j+1);
long b = fnc(dp, ao, az, i, j+1);
dp[i][j] = Math.min(a, b);
return dp[i][j];
}
} | 0 | Non-plagiarised |
680ba922 | ee4b9467 | import java.util.*;
import java.io.*;
public class Solution
{
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();
}
}
static int parent(int a , int p[])
{
if(a == p[a])
return a;
return p[a] = parent(p[a],p);
}
static void union(int a , int b , int p[] , int size[])
{
a = parent(a,p);
b = parent(b,p);
if(a == b)
return;
if(size[a] < size[b])
{
int temp = a;
a = b;
b = temp;
}
p[b] = a;
size[a] += size[b];
}
static long getSum(int index , long BITree[])
{
long sum = 0; // Iniialize result
// index in BITree[] is 1 more than
// the index in arr[]
// index = index + 1;
// Traverse ancestors of BITree[index]
while(index>0)
{
// Add current element of BITree
// to sum
sum += BITree[index];
// Move index to parent node in
// getSum View
index -= index & (-index);
}
return sum;
}
// Updates a node in Binary Index Tree (BITree)
// at given index in BITree. The given value
// 'val' is added to BITree[i] and all of
// its ancestors in tree.
public static void updateBIT(int n, int index,
long val , long BITree[])
{
// index in BITree[] is 1 more than
// the index in arr[]
// index = index + 1;
// Traverse all ancestors and add 'val'
while(index <= n)
{
// Add 'val' to current node of BIT Tree
BITree[index] += val;
// Update index to that of parent
// in update View
index += index & (-index);
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int dp[][];
static int f(int pos , int take , int arr[])
{
if(pos == -1)
{
if(take == 0)
return 0;
return -10000000;
}
if(dp[pos][take] != -1)
return dp[pos][take];
if(pos+1-take == arr[pos])
dp[pos][take] = Math.max(dp[pos][take],1 + f(pos-1,take,arr));
dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take,arr));
if(take > 0)
dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take-1,arr));
return dp[pos][take];
}
public static void main(String []args) throws IOException
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt();
sc.nextLine();
String a = sc.nextLine();
String b = sc.nextLine();
int same = 0 , zo = 0 , oz = 0 , oo = 0 , zz = 0;
for(int i = 0 ; i < n ; i++)
{
if(a.charAt(i) == '0' && b.charAt(i) == '1')
oz++;
else if(a.charAt(i) == '1' && b.charAt(i) == '0')
zo++;
else if(a.charAt(i) == '1' && b.charAt(i) == '1')
oo++;
else
zz++;
}
if(oz == zo || (zz == oo-1))
{
int mx = Integer.MAX_VALUE;
if(oz == zo)
mx = Math.min(mx,2*oz);
if(oo-1 == zz)
mx = Math.min(mx,zz+oo);
System.out.println(mx);
}
else
{
System.out.println(-1);
}
}
}
} | import java.io.*;
import java.util.*;
public class _1615c {
FastScanner scn;
PrintWriter w;
PrintStream fs;
long MOD = 1000000007;
int MAX = 200005;
long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);}
long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
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), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);}
void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}}
boolean LOCAL;
void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));}
//SUFFICIENT DRY RUN????LOGIC VERIFIED FOR ALL TEST CASES???
void solve(){
int t=scn.nextInt();
while(t-->0)
{
int n=scn.nextInt();
String s1= scn.next();
String s2 = scn.next();
int op1=0,op0=0,one=0,zr=0;
for(int i=0;i<n;i++){
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
if(ch1=='1'&&ch2=='0'){
op1++;
}else if(ch1=='0'&&ch2=='1'){
op0++;
}else if(ch1==ch2){
if(ch1=='0'){
zr++;
}else{
one++;
}
}
}
if((one-zr)==1&&op1==op0){
w.println(Math.min(2*op1,one+zr));
}else if((one-zr)==1){
w.println(one+zr);
}else if(op1==op0){
w.println(2*op1);
}else{
w.println(-1);
}
}
}
void run() {
try {
long ct = System.currentTimeMillis();
scn = new FastScanner(new File("input.txt"));
w = new PrintWriter(new File("output.txt"));
fs=new PrintStream("error.txt");
System.setErr(fs);
LOCAL=true;
solve();
w.close();
System.err.println(System.currentTimeMillis() - ct);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
scn = new FastScanner(System.in);
w = new PrintWriter(System.out);
LOCAL=false;
solve();
w.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n) {
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());
}
}
int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; }
void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}
long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;}
int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b))
boolean nextPermutation(int[] arr) {if(arr == null || arr.length <= 1){return false;}int last = arr.length-2;while(last >= 0){if(arr[last] < arr[last+1]){break;}last--;}if (last < 0){return false;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;}
public static void main(String[] args) {
new _1615c().runIO();
}
}
| 0 | Non-plagiarised |
132281000000000 | f415c0bf |
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 Duo{
int x;
String s;
Duo(int x,String s){
this.x = x;
this.s = s;
}
}
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;
}
}
static boolean function(long a[],long x) {
long arr[] = new long[a.length];
for(int i=0;i<arr.length;i++) {
arr[i] = a[i];
}
for(int i=arr.length-1;i>=2;i--) {
if(arr[i] < x) {
return false;
}
long d = Math.min(a[i],(arr[i]-x))/3;
arr[i-1] += d;
arr[i-2] += 2*d;
}
return (arr[0]>=x && arr[1]>=x);
}
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();
long a[] = new long[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextLong();
}
long l = 1;
long r = 1000000000;
long ans = 1;
while(l<=r){
long mid = l + (r-l)/2;
if(function(a,mid)){
ans = mid;
l = mid+1;
}
else {
r = mid-1;
}
}
res.append(ans+"\n");
}
System.out.println(res);
}
}
|
import java.util.Scanner;
public class BalancedStoneHeaps {
static boolean check(long[] a, long k) {
int n = a.length;
long[] temp = new long[n];
for (int i = 0; i < n; i++) {
temp[i] = a[i];
}
for (int i = n - 1; i >= 2; i--) {
if (temp[i] < k) {
return false;
}
long p = Math.min(a[i] / 3, (temp[i] - k) / 3);
temp[i - 1] += p;
temp[i - 2] += 2 * p;
}
if (temp[0] >= k && temp[1] >= k) {
return true;
}
return false;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while (t-- > 0) {
int n = scan.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
}
long l = 0;
long r = a[n - 1];
long res = 0;
while (l <= r) {
long mid = (l + r) / 2;
if (check(a, mid)) {
l = mid + 1;
res = Math.max(res, mid);
} else {
r = mid - 1;
}
}
System.out.println(res);
}
}
}
| 1 | Plagiarised |
9028caf7 | e6b7a899 | 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;
}
}
} | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
public class Main {
static final long MOD=1000000007;
static final long MOD1=998244353;
static long ans=0;
//static ArrayList<Integer> ans=new ArrayList<>();
public static void main(String[] args){
PrintWriter out = new PrintWriter(System.out);
InputReader sc=new InputReader(System.in);
int N = sc.nextInt();
int[] A = sc.nextIntArray(N);
ArrayList<Integer> a1 = new ArrayList<Integer>();
ArrayList<Integer> a2 = new ArrayList<Integer>();
for (int i = 0; i < A.length; i++) {
if (A[i]==0) {
a1.add(i);
}else {
a2.add(i);
}
}
int[][] dp = new int[a1.size()+1][a2.size()+1];
for (int i = 0; i < dp.length; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE/2);
}
dp[0][0] = 0;
for (int i = 1; i <= a1.size() ; i++) {
int pos1 = a1.get(i-1);
for (int j = 0; j <= a2.size(); j++) {
dp[i][j] = dp[i-1][j];
if (j-1>=0) {
int pos2 = a2.get(j-1);
dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1] + Math.abs(pos1-pos2));
}
}
}
System.out.println(dp[a1.size()][a2.size()]);
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextDouble();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
}
| 0 | Non-plagiarised |
45b19e51 | f9adc043 | import java.util.*;
import java.io.*;
public class Main {
static FastScanner sc = new FastScanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
static long mod = (long) 1e9 + 7;
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
for(int i = 0; i < n; i++) solve();
pw.flush();
}
public static void solve() {
int n = sc.nextInt();
long[] a = sc.nextLongArray(n);
long left = 1;
long right = (long)1e9;
long[] dec = new long[2];
while(right-left > 1){
long mid = (right+left)/2L;
dec[0] = dec[1] = 0;
boolean ok = true;
for(int i = n-1; i >= 2; i--){
long a1 = a[i] + dec[1];
if(a1 < mid){
ok = false;
break;
}
long d = (Math.min(a[i],a1-mid))/3L;
dec[1] = dec[0]+d;
dec[0] = d*2;
//pw.println(dec[0] + " " + dec[1] + " " + mid + " " + d);
}
if(ok && a[0]+dec[0] >= mid && a[1]+dec[1] >= mid){
left = mid;
}else{
right = mid;
}
/*
for(int i = 2; i < n; i++){
long a1 = a[i-2]-dec[0];
long a2 = a[i-1]-dec[1];
long d = Math.max(0,(mid-a1+1)/2L);
if(d*3 > a[i]){
if(i != 0){
a[i] =
}
ok = false;
break;
}else{
dec[0] = dec[1] - d;
dec[1] = d*3;
}
//pw.println(dec[0] + " " + dec[1] + " " + mid);
}
if(ok && a[n-1]-dec[1] >= mid){
left = mid;
}else{
right = mid;
}
*/
}
pw.println(left);
}
static class GeekInteger {
public static void save_sort(int[] array) {
shuffle(array);
Arrays.sort(array);
}
public static int[] shuffle(int[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
int randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
public static void save_sort(long[] array) {
shuffle(array);
Arrays.sort(array);
}
public static long[] shuffle(long[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
long randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
}
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public FastScanner(FileReader in) {
reader = new BufferedReader(in);
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String[] nextArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
| import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int bruh = in.nextInt();
for (int cases = 0; cases < bruh; cases++) {
int n = in.nextInt();
long lo = (int) 1e9, hi = 0;
long [] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
lo = Math.min(lo, a[i]);
hi = Math.max(hi, a[i]);
}
long m = 0;
//System.out.println(Arrays.toString(a));
while (lo <= hi) {
m = (lo + hi) / 2;
if (works(a, m)) {
lo = m + 1;
}
else {
hi = m - 1;
}
}
out.println(hi);
}
out.close();
}
static boolean works(long[] a, long m) {
long[] d = Arrays.copyOf(a, a.length);
for (int i = a.length - 1; i > 1; i--) {
if (d[i] < m) {
//System.out.println(m + ":x" + Arrays.toString(d));
return false;
}
long canGive = (d[i] - m) / 3 * 3;
canGive = Math.min(canGive, a[i]);
d[i - 1] += canGive / 3;
d[i - 2] += canGive / 3 * 2;
d[i] -= canGive;
}
//System.out.println(m + ": " + Arrays.toString(d));
if (d[0] < m || d[1] < m) return false;
return true;
}
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 double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
return nextLine();
}
String ret = "";
while (st.hasMoreTokens()) {
ret += " " + st.nextToken();
}
return ret.substring(1);
}
public int[] nextIntArr(int size) throws IOException {
int[] arr = new int[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr(int size) throws IOException {
long[] arr = new long[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArr(int size) throws IOException {
double[] arr = new double[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextDouble();
}
return arr;
}
}
} | 0 | Non-plagiarised |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.