F1
stringlengths 8
8
| F2
stringlengths 8
8
| text_1
stringlengths 607
24.4k
| text_2
stringlengths 591
24.4k
| label
int64 0
1
|
---|---|---|---|---|
2470b521 | f6ca6fc8 | // package com.company;
import com.sun.security.jgss.GSSUtil;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
//ankit singh
public class Main {
static int inf=Integer.MAX_VALUE/2;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int nn=input.nextInt();
int a[]=new int[nn];
ArrayList<Integer> one=new ArrayList<>();
ArrayList<Integer> zero=new ArrayList<>();
for (int i = 0; i <nn ; i++) {
a[i]=input.nextInt();
if(a[i]==1){
one.add(i);
}else zero.add(i);
}
if (one.size()==0) {
System.out.println(0);
return;
}
int n=one.size();
int m=zero.size();
int dp[][]=new int[n+1][m+1];
int ans=0;
for (int i = 1; i <=n ; i++) {
dp[i][0]=inf;
for (int j = 1; j <=m ; j++) {
dp[i][j]=inf;
//take the current
dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+Math.abs(one.get(i-1)-zero.get(j-1)));
//take previos
dp[i][j]=Math.min(dp[i][j],dp[i][j-1]);
}
}
ans=inf;
for (int i = 1; i <=m ; i++) {
ans=Math.min(ans,dp[n][i]);
}
System.out.println(ans);
}
} | import java.util.*;
import java.io.*;
public class D {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> o=new ArrayList<Integer>();
ArrayList<Integer> e=new ArrayList<Integer>();
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
int dp[][]=new int[o.size()+1][e.size()+1];
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++){
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
}
System.out.println(dp[o.size()][e.size()]);
}
}
| 0 |
a8e2cefc | ee270b2a | import java.io.*;
import java.sql.SQLOutput;
import java.util.*;
public class airconditioner {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() throws java.io.IOException {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class compare implements Comparator<int[]> {
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
}
public static void main(String[] args) throws IOException {
FastReader input = new FastReader();
int testcases = input.nextInt();
for (int t = 0; t < testcases; t++) {
int length = input.nextInt();
int conditioners = input.nextInt();
int[] array = new int[length];
int[][] conpositions = new int[conditioners][2]; //0 = position, 1 = value
for (int i = 0; i < length; i++) {
array[i] = 2000000069;
}
for (int i = 0; i < conditioners; i++) {
conpositions[i][0] = input.nextInt() - 1;
}
for (int i = 0; i < conditioners; i++) {
conpositions[i][1] = input.nextInt();
array[conpositions[i][0]] = conpositions[i][1];
}
Arrays.sort(conpositions, new compare());
int position = conditioners - 2;
int counter = conpositions[conditioners - 1][1];
for (int i = conpositions[conditioners - 1][0]; i >= 0; i--) { //i = arrayposition
array[i] = counter;
if (position >= 0 && i == conpositions[position][0]) {
if (conpositions[position][1] < counter) {
counter = conpositions[position][1];
array[i] = counter;
}
position--;
}
counter++;
}
position = 1;
counter = conpositions[0][1];
for (int i = conpositions[0][0]; i < length; i++) {
if (counter < array[i]) {
array[i] = counter;
}
if (position < conditioners && i == conpositions[position][0]) {
if (conpositions[position][1] < counter) {
counter = conpositions[position][1];
if (counter < array[i]) {
array[i] = counter;
}
}
position++;
}
counter++;
}
for (int i = 0; i < length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
} | import java.util.*;
public class D{
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
int q = scanner.nextInt();
while(q-- > 0){
int n = scanner.nextInt(),
k = scanner.nextInt();
int[] a = new int[k];
for(int i=0;i<k;i++){
a[i] = scanner.nextInt();
}
int[] t = new int[k];
for(int j=0;j<k;j++){
t[j] = scanner.nextInt();
}
long[] L = new long[n];
long[] R = new long[n];
for(int i=0;i<n;i++){
L[i] = Integer.MAX_VALUE;
R[i] = Integer.MAX_VALUE;
}
for(int i=0;i<k;i++){
L[a[i]-1] = t[i];
R[a[i]-1] = t[i];
}
long min = Integer.MAX_VALUE;
for(int i=0;i<n;i++){
L[i] = Math.min(min+1,L[i]);
min = L[i];
}
min = Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
R[i] = Math.min(min+1,R[i]);
min = R[i];
}
for(int i=0;i<n;i++){
System.out.print(Math.min(L[i],R[i]) + " ");
}
System.out.println();
}
}
}
| 0 |
90f01508 | e00b1794 | // import java.io.DataInputStream;
// import java.io.FileInputStream;
// import java.io.IOException;
import java.io.*;
import java.util.*;
public class one
{
static Scanner sc=new Scanner(System.in);
boolean prime[];
static int prev=-1;
static int dp[][];
public static int[] input(int size){
int arr[]=new int[size];
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();
return arr;
}
public static void main(String[] args) {
//int testcase=1;
int testcase=sc.nextInt();
//System.out.println("HI");
while(testcase-->0){
// int x=sc.nextInt();
// int y=sc.nextInt();
//String str[]=new String[size];
solve();
System.out.println();
}
}
public static void solve(){
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
int size=sc.nextInt();
int arr[][]=new int[size-1][2];
for(int i=0;i<size-1;i++){
arr[i][0]=sc.nextInt();
arr[i][1]=sc.nextInt();
}
for(int x[]:arr){
map.put(x[0],map.getOrDefault(x[0], 0)+1);
map.put(x[1],map.getOrDefault(x[1], 0)+1);
if(map.get(x[0])>2||map.get(x[1])>2){
System.out.println(-1);
return;
}
}
List<List<Integer>> adj=new ArrayList<>();
for(int i=0;i<=size;i++)
adj.add(new ArrayList<Integer>());
for(int x[]:arr){
adj.get(x[0]).add(x[1]);
adj.get(x[1]).add(x[0]);
}
//System.out.println(adj);
int vist[]=new int[size+1];
HashMap<String,Integer> ans=new HashMap<String,Integer>();
for(int i=1;i<=size;i++){
if(vist[i]==0){
dfs(i,vist,adj,ans,2);
}
}
//System.out.println(ans);
for(int x[]:arr){
//System.out.print(map.get(x[0]));
int a=Math.min(x[0],x[1]);
int b=Math.max(x[0],x[1]);
String s=a+" "+b;
System.out.print(ans.get(s)+" ");
}
// map=new HashMap<Integer,Integer>();
// for(int x[]:arr){
// if(map.containsKey(x[0])){
// int val=13-map.get(x[0]);
// map.put(x[1],val);
// System.out.print(val+" ");
// }else if(map.containsKey(x[1])){
// int val=13-map.get(x[1]);
// map.put(x[0],val);
// System.out.print(val+" ");
// }else{
// System.out.print(2+" ");
// map.put(x[0],2);
// map.put(x[1],2);
// }
// }
}
public static void dfs(int node,int vist[],List<List<Integer>> adj,HashMap<String,Integer> ans,int val){
vist[node]=1;
for(int i:adj.get(node)){
if(vist[i]==1)
continue;
int x=Math.min(i, node);
int y=Math.max(i, node);
ans.put(x+" "+y,val);
dfs(i,vist,adj,ans,5-val);
val=5-val;
}
}
} | import java.util.*;
import java.io.*;
public class Main{
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n){
int[] res=new int[n];
for(int i=0;i<n;i++)res[i]=nextInt();
return res;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=in.nextInt();
//out.println("hee");
//int testCases=1;
while(testCases-- > 0){
solve(in);
}
out.close();
} catch (Exception e) {
return;
}
}
public static void solve( FastReader in){
int n=in.nextInt();
List<List<Pair>> graph=new ArrayList<>();
for(int i=0;i<n;i++){
graph.add(new ArrayList<>());
}
//out.println("hee");
for(int i=0;i<n-1;i++){
int u=in.nextInt();
int v=in.nextInt();
u--;v--;
graph.get(u).add(new Pair(v,i));
graph.get(v).add(new Pair(u,i));
}
int start=0;
for(int i=0;i<n;i++){
if(graph.get(i).size()>2){
System.out.println(""+-1);
return;
}
else if(graph.get(i).size()==1){
start=i;
}
}
int[] ans=new int[n-1];
int[] vis=new int[n+1];
vis[start]=1;
int w=2;
Queue<Integer> q=new LinkedList<>();
q.add(start);
while(!q.isEmpty()){
int x=q.poll();
for(Pair p:graph.get(x)){
if(vis[p.node]==0){
vis[p.node]=1;
q.add(p.node);
int idx=p.eg_indx;
ans[idx]=w;
if(w==2)w=5;
else w=2;
}
}
}
for(int i:ans){
System.out.print(i+" ");
}
System.out.println("");
}
static class Pair{
int node;
int eg_indx;
Pair(int node,int eg_indx){
this.node=node;
this.eg_indx=eg_indx;
}
}
static int gcd(int a,int b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
} | 0 |
16857116 | f3d7ce08 | 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(count2==1&&count1==0)
ans=1;
if(count3==0&&count4==0)
ans=0;
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;
}
}
}
| 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;
}
}
}
| 1 |
51151974 | c23278ec | import java.io.*;
import java.util.*;
public class Solution{
public static void main (String[] args) throws java.lang.Exception {
FastReader sc = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int testCase = sc.nextInt();
while (testCase-->0){
int n = sc.nextInt();
String[] strArr = new String[n];
for(int i=0; i<n; i++) {
strArr[i]=sc.nextLine();
}
int[] total = new int[5];
ArrayList<int[]> al = new ArrayList<>();
for(int i=0; i<n; i++){
int[] arr= new int[5];
for(int j=0; j<strArr[i].length(); j++){
arr[strArr[i].charAt(j)-'a']++;
}
for(int j=0; j<5; j++){
total[j]+=arr[j];
}
al.add(arr);
}
int ans=0;
for(int i=0; i<5; i++){
ArrayList<Integer> all = new ArrayList<>();
for(int j=0; j<n; j++){
all.add(strArr[j].length()-2*al.get(j)[i]);
}
java.util.Collections.sort(all);
int c=0, d=0;
for(int j=0; j<n; j++){
c+=all.get(j);
if(c<0) d=j+1;
}
ans = Math.max(ans,d);
}
System.out.println(ans);
}
}
// Fast Reader Class
static class FastReader {
BufferedReader br; StringTokenizer st;
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } }return st.nextToken(); }
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }return str; }
}
} | import java.io.*;
import java.sql.Array;
import java.util.*;
public class P1551C {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
static class Task {
public void solve(InputReader in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
String[] s = new String[n];
int[][] freq = new int[n][6];
for (int i = 0; i < n; i++) {
s[i] = in.nextLine();
for (char c : s[i].toCharArray()) {
freq[i][c - 'a']++;
freq[i][5]++;
}
}
int totmax = 0;
for (int i = 0; i < 5; i++) {
List<Integer> diffs = new ArrayList<>(n);
for (int j = 0; j < n; j++) {
diffs.add(2 * freq[j][i] - freq[j][5]);
}
Collections.sort(diffs);
Collections.reverse(diffs);
int curr = 0, cnt = 0;
while (cnt < diffs.size() && curr + diffs.get(cnt) > 0) {
curr += diffs.get(cnt++);
}
totmax = Math.max(totmax, cnt);
}
out.println(totmax);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
}
}
| 0 |
00f79486 | ce0b2178 | /* In_The_Name_Of_Allah_The_Merciful */
import java.util.*;
import java.io.*;
public class Main
{
class Pair implements Comparable<Pair>{
int f,s;
Pair(int x,int y){
f=x;
s=y;
}
@Override
public int compareTo(Pair a) {
if(a.f!=this.f)return Integer.compare(f,a.f);
else return Integer.compare(s,a.s);
}
}
PrintWriter out;
FastReader sc;
long[] m1= {(long)(1e9+7),998244353};
long mod=m1[1];
long maxlong=Long.MAX_VALUE;
long minlong=Long.MIN_VALUE;
StringBuilder sb;
/******************************************************************************************
*****************************************************************************************/
public void sol() {
int n=ni();
int[] ar=new int[n];
for(int i=0;i<n;i++)ar[i]=ni();
char[] s=rl();
PriorityQueue<Integer> red=new PriorityQueue<>(Collections.reverseOrder()),blue=new PriorityQueue<>();
for(int i=0;i<n;i++) {
if(s[i]=='R')red.add(ar[i]);
else blue.add(ar[i]);
}
int a=n;
while(red.size()>0) {
if(red.poll()>a) {
no();return;
}a--;
}a=1;
while(blue.size()>0) {
if(blue.poll()<a) {
no();return;
}a++;
}yes();
}
public static void main(String[] args)
{
Main g=new Main();
g.out=new PrintWriter(System.out);
g.sc=new FastReader();
int t=1;
t=g.ni();
while(t-->0)
g.sol();
g.out.flush();
}
/****************************************************************************************
*****************************************************************************************/
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 int ni(){
return sc.nextInt();
}public long nl(){
return sc.nextLong();
}public double nd(){
return sc.nextDouble();
}public char[] rl(){
return sc.nextLine().toCharArray();
}public String rl1(){
return sc.nextLine();
}
public void pl(Object s){
out.println(s);
}
public void pr(Object s){
out.print(s);
}public String next(){
return sc.next();
}public long abs(long x){
return Math.abs(x);
}
public int abs(int x){
return Math.abs(x);
}
public double abs(double x){
return Math.abs(x);
}public long min(long x,long y){
return (long)Math.min(x,y);
}
public int min(int x,int y){
return (int)Math.min(x,y);
}
public double min(double x,double y){
return Math.min(x,y);
}public long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}public long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
void sort1(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) {
l.add(i);
}
Collections.sort(l,Collections.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}
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);
}
}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);
}
}void sort1(char[] a) {
ArrayList<Character> l = new ArrayList<>();
for (char i : a) {
l.add(i);
}
Collections.sort(l,Collections.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}
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);
}
}void sort1(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) {
l.add(i);
}
Collections.sort(l,Collections.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}
void sort(double[] a) {
ArrayList<Double> l = new ArrayList<>();
for (double i : a) {
l.add(i);
}
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}long pow(long a,long b){
if(b==0){
return 1;
}long p=pow(a,b/2);
if(b%2==0) return mMultiplication(p,p)%mod;
else return (mMultiplication(mMultiplication(p,p),a))%mod;
}
int swap(int a,int b){
return a;
}long swap(long a,long b){
return a;
}double swap(double a,double b){
return a;
}
boolean isPowerOfTwo (int x)
{
return x!=0 && ((x&(x-1)) == 0);
}boolean isPowerOfTwo (long x)
{
return x!=0 && ((x&(x-1)) == 0);
}public long max(long x,long y){
return (long)Math.max(x,y);
}
public int max(int x,int y){
return (int)Math.max(x,y);
}
public double max(double x,double y){
return Math.max(x,y);
}long sqrt(long x){
return (long)Math.sqrt(x);
}int sqrt(int x){
return (int)Math.sqrt(x);
}void input(int[] ar,int n){
for(int i=0;i<n;i++)ar[i]=ni();
}void input(long[] ar,int n){
for(int i=0;i<n;i++)ar[i]=nl();
}void fill(int[] ar,int k){
Arrays.fill(ar,k);
}void yes(){
pl("YES");
}void no(){
pl("NO");
}
long c2(long n) {
return (n*(n-1))/2;
}
long[] sieve(int n)
{
long[] k=new long[n+1];
boolean[] pr=new boolean[n+1];
for(int i=1;i<=n;i++){
k[i]=i;
pr[i]=true;
}for(int i=2;i<=n;i++){
if(pr[i]){
for(int j=i+i;j<=n;j+=i){
pr[j]=false;
if(k[j]==j){
k[j]=i;
}
}
}
}return k;
}
int strSmall(int[] arr, int target)
{
int start = 0, end = arr.length-1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
} int strSmall(ArrayList<Integer> arr, int target)
{
int start = 0, end = arr.size()-1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr.get(mid) > target) {
start = mid + 1;
ans=start;
}
else {
end = mid - 1;
}
}
return ans;
}long mMultiplication(long a,long b)
{
long res = 0;
a %= mod;
while (b > 0)
{
if ((b & 1) > 0)
{
res = (res + a) % mod;
}
a = (2 * a) % mod;
b >>= 1;
}
return res;
}long nCr(int n, int r ,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i %p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}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;
}long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
int[][] floydWarshall(int graph[][],int INF,int V)
{
int dist[][] = new int[V][V];
int i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}return dist;
}
class minque {
Deque<Long> q;
minque(){
q=new ArrayDeque<Long>();
}public void add(long p){
while(!q.isEmpty()&&q.peekLast()>p)q.pollLast();
q.addLast(p);
}public void remove(long p) {
if(!q.isEmpty()&&q.getFirst()==p)q.removeFirst();
}public long min() {
return q.getFirst();
}
}
int find(subset[] subsets, int i)
{
if (subsets[i].parent != i)
subsets[i].parent
= find(subsets, subsets[i].parent);
return subsets[i].parent;
}void Union(subset[] subsets, int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank) {
subsets[xroot].parent = yroot;
}
else if (subsets[yroot].rank < subsets[xroot].rank) {
subsets[yroot].parent = xroot;
}
else {
subsets[xroot].parent = yroot;
subsets[yroot].rank++;
}
}class subset
{
int parent;
int rank;
}
} | import java.io.*;
import java.util.*;
public class D {
static class Pair implements Comparable<Pair>{
int a;
char b;
public Pair(int a, char b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair p) {
if(this.b == p.b)
return this.a - p.a;
return this.b - p.b;
}
}
public static void main(String[] args)throws IOException {
FastScanner scan = new FastScanner();
PrintWriter output = new PrintWriter(System.out);
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt();
int arr[] = scan.readArray(n);
char line[] = scan.next().toCharArray();
ArrayList<Pair> pairs = new ArrayList<Pair>();
for(int i = 0;i<n;i++) {
pairs.add(new Pair(arr[i], line[i]));
}
Collections.sort(pairs);
boolean possible = true;
for(int i = 1;i<=n;i++) {
if(pairs.get(i-1).a == i) {
continue;
}
else if(pairs.get(i-1).a < i && pairs.get(i-1).b == 'R') {
continue;
}
else if(pairs.get(i-1).a > i && pairs.get(i-1).b == 'B') {
continue;
}
else {
possible = false;
break;
}
}
output.println(possible == true ? "YES" : "NO");
}
output.flush();
}
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr) list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) arr[i] = list.get(i);
return arr;
}
public static int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b%a, a);
}
static boolean isPrime(int n) {
if (n <= 1) return false;
else if (n == 2) return true;
else if (n % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) if (n % i == 0) return false;
return true;
}
public static void printArray(int arr[]) {
for(int i:arr) System.out.print(i+" ");
System.out.println();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| 0 |
976fe834 | da54dec3 | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
/*
-> Give your 100%, that's it!
-> Feynman Rule To Solve Any Problem:
1. Read the problem.
2. Think About It.
3. Solve it!
*/
public class Template {
static int mod = 1000000007;
public static void main(String[] args){
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int yo = sc.nextInt();
while (yo-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int[][] arr = new int[n][m];
for(int i = 0; i < n; i++){
String s = sc.next();
for(int j = 0; j < m; j++){
arr[i][j] = s.charAt(j)-'0';
}
}
List<String> list = new ArrayList<>();
for(int i = 0; i < n-1; i++){
for(int j = 0; j < m-1; j++){
check(list,i,j,n,m,arr);
}
}
// for(int i = 0; i < n; i++){
// for(int j = 0; j < m; j++){
// out.print(arr[i][j] + " ");
// }
// out.println();
// }
out.println(list.size()/3);
for(int i = 0; i < list.size(); i+=3){
out.println(list.get(i) + " " + list.get(i+1) + " " + list.get(i+2));
}
// out.println(list.size());
// for(int i = 0; i < list.size(); i++){
// out.println(list.get(i));
// }
}
out.close();
}
static void check(List<String> list, int i, int j, int n, int m, int[][] arr){
int[][] a = new int[2][2];
a[0][0] = arr[i][j];
a[0][1] = arr[i][j+1];
a[1][0] = arr[i+1][j];
a[1][1] = arr[i+1][j+1];
int ones = 0;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
if(a[x][y] == 1) ones++;
}
}
if(ones == 0) return;
if(ones == 1){
ones(i,j,list,a);
twos(i,j,list,a);
threes(i,j,list,a);
}
else if(ones == 2){
twos(i,j,list,a);
threes(i,j,list,a);
}
else if(ones == 3){
threes(i,j,list,a);
}
else if(ones == 4){
fours(i,j,list,a);
// for(int x = 0; x < 2; x++){
// for(int y = 0; y < 2; y++){
// out.print(a[x][y] + " ");
// }
// out.println();
// }
ones(i,j,list,a);
// for(int x = 0; x < 2; x++){
// for(int y = 0; y < 2; y++){
// out.print(a[x][y] + " ");
// }
// out.println();
// }
twos(i,j,list,a);
// for(int x = 0; x < 2; x++){
// for(int y = 0; y < 2; y++){
// out.print(a[x][y] + " ");
// }
// out.println();
// }
threes(i,j,list,a);
// for(int x = 0; x < 2; x++){
// for(int y = 0; y < 2; y++){
// out.print(a[x][y] + " ");
// }
// out.println();
// }
}
arr[i][j] = a[0][0];
arr[i+1][j] = a[1][0];
arr[i][j+1] = a[0][1];
arr[i+1][j+1] = a[1][1];
}
public static void twos(int i, int j, List<String> list, int[][] a){
int count = 0;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
if(a[x][y] == 1 && count < 1) {
count++;
a[x][y] = 0;
list.add(get(x,y,i,j));
}
else if(a[x][y] == 0){
list.add(get(x,y,i,j));
a[x][y] = 1;
}
}
}
}
public static void threes(int i, int j, List<String> list, int[][] a){
int count = 0;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
if(a[x][y] == 1) {
a[x][y] = 0;
list.add(get(x,y,i,j));
}
}
}
}
public static void fours(int i, int j, List<String> list, int[][] a){
int count = 0;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
if(a[x][y] == 1 && count < 3) {
count++;
a[x][y] = 0;
list.add(get(x,y,i,j));
}
}
}
}
public static void ones(int i, int j, List<String> list, int[][] a){
int count = 0;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
if(a[x][y] == 1) {
a[x][y] = 0;
list.add(get(x,y,i,j));
}
else if(a[x][y] == 0 && count < 2){
a[x][y] = 1;
count++;
list.add(get(x,y,i,j));
}
}
}
}
public static String get(int x, int y, int i, int j){
if(x == 0 && y == 0) {
return (i+1) + " " + (j+1);
}
else if(x == 0 && y == 1){
return (i+1) + " " + (j+2);
}
else if(x == 1 && y == 0){
return (i+2) + " " + (j+1);
}
else {
return (i+2) + " " + (j+2);
}
}
/*
Random stuff to try when stuck:
-if it's 2C then it's dp
-for combo/probability problems, expand the given form we're interested in
-make everything the same then build an answer (constructive, make everything 0 then do something)
-something appears in parts of 2 --> model as graph
-assume a greedy then try to show why it works
-find way to simplify into one variable if multiple exist
-treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them)
-find lower and upper bounds on answer
-figure out what ur trying to find and isolate it
-see what observations you have and come up with more continuations
-work backwards (in constructive, go from the goal to the start)
-turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements)
-instead of solving for answer, try solving for complement (ex, find n-(min) instead of max)
-draw something
-simulate a process
-dont implement something unless if ur fairly confident its correct
-after 3 bad submissions move on to next problem if applicable
-do something instead of nothing and stay organized
-write stuff down
Random stuff to check when wa:
-if code is way too long/cancer then reassess
-switched N/M
-int overflow
-switched variables
-wrong MOD
-hardcoded edge case incorrectly
Random stuff to check when tle:
-continue instead of break
-condition in for/while loop bad
Random stuff to check when rte:
-switched N/M
-long to int/int overflow
-division by 0
-edge case for empty list/data structure/N=1
*/
public static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void sort(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
for (int i = 0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieve(int N) {
boolean[] sieve = new boolean[N + 1];
for (int i = 2; i <= N; i++)
sieve[i] = true;
for (int i = 2; i <= N; i++) {
if (sieve[i]) {
for (int j = 2 * i; j <= N; j += i) {
sieve[j] = false;
}
}
}
return sieve;
}
public static long power(long x, long y, long p) {
long res = 1L;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res;
}
public static void print(int[] arr) {
//for debugging only
for (int x : arr)
out.print(x + " ");
out.println();
}
static class FastScanner {
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
// For Input.txt and Output.txt
// FileInputStream in = new FileInputStream("input.txt");
// FileOutputStream out = new FileOutputStream("output.txt");
// PrintWriter pw = new PrintWriter(out);
// Scanner sc = new Scanner(in);
} | import java.util.*;
import java.math.*;
public class Solution {
final static int MOD = 1000000007;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = Integer.parseInt(in.next());
for(int tt = 0; tt < t; tt++) {
int n = Integer.parseInt(in.next());
int m = Integer.parseInt(in.next());
char[][] grid = new char[n][m];
List<Integer> ans = new ArrayList<>();
for(int i = 0; i < n; i++) {
grid[i] = in.next().toCharArray();
}
int iii = 0;
int jjj = 0;
if(n % 2 != 0) {
iii = 1;
for(int j = 0; j + 1 < m; j++) {
if(grid[0][j] == '1') {
grid[0][j] = '0';
grid[1][j] = (grid[1][j] == '1') ? '0' : '1';
grid[1][j + 1] = (grid[1][j + 1] == '1') ? '0' : '1';
ans.add(1); ans.add(j + 1);
ans.add(2); ans.add(j + 1);
ans.add(2); ans.add(j + 2);
}
if(grid[0][j + 1] == '1') {
grid[0][j + 1] = '0';
grid[1][j] = (grid[1][j] == '1') ? '0' : '1';
grid[1][j + 1] = (grid[1][j + 1] == '1') ? '0' : '1';
ans.add(1); ans.add(j + 2);
ans.add(2); ans.add(j + 1);
ans.add(2); ans.add(j + 2);
}
}
}
if(m % 2 != 0) {
jjj = 1;
for(int i = (n % 2 == 0) ? 0 : 1; i + 1 < n; i++) {
if(grid[i][0] == '1') {
grid[i][0] = '0';
grid[i][1] = (grid[i][1] == '1') ? '0' : '1';
grid[i + 1][1] = (grid[i + 1][1] == '1') ? '0' : '1';
ans.add(i + 1); ans.add(1);
ans.add(i + 1); ans.add(2);
ans.add(i + 2); ans.add(2);
}
if(grid[i + 1][0] == '1') {
grid[i + 1][0] = '0';
grid[i][1] = (grid[i][1] == '1') ? '0' : '1';
grid[i + 1][1] = (grid[i + 1][1] == '1') ? '0' : '1';
ans.add(i + 2); ans.add(1);
ans.add(i + 1); ans.add(2);
ans.add(i + 2); ans.add(2);
}
}
}
for(int i = iii; i < n; i += 2) {
for(int j = jjj; j < m; j += 2) {
int cnt = count(grid, i, j);
while(cnt > 0) {
List<G> g = new ArrayList<>();
for(int k = i; k < i + 2; k++) {
for(int l = j; l < j + 2; l++) {
g.add(new G(grid[k][l], k, l));
}
}
Collections.sort(g);
int l = 0;
int r = 0;
if(cnt == 1 || cnt == 3) {
l = 1; r = 3;
}else {
l = 0; r = 2;
}
for(int k = l; k <= r; k++) {
int ii = g.get(k).i;
int jj = g.get(k).j;
grid[ii][jj] = (grid[ii][jj] == '1') ? '0' : '1';
ans.add(ii + 1);
ans.add(jj + 1);
}
cnt = count(grid, i, j);
}
}
}
System.out.println(ans.size() / 6);
for(int i = 0; i < ans.size(); i += 6) {
for(int j = i; j < i + 6; j++) {
System.out.print(ans.get(j) + " " );
}
System.out.println();
}
}
}
public static int count(char[][] grid, int i, int j) {
int cnt = 0;
for(int k = i; k < i + 2; k++) {
for(int l = j; l < j + 2; l++) {
if(grid[k][l] == '1') cnt += 1;
}
}
return cnt;
}
}
class G implements Comparable <G> {
Character val;
int i;
int j;
public G(char val, int i, int j) {
this.val = val;
this.i = i;
this.j = j;
}
@Override
public int compareTo(G obj) {
return this.val.compareTo(obj.val);
}
} | 0 |
8d6f1bf5 | e90a198b | //package Div2.C;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Menorah {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t > 0) {
int n = Integer.parseInt(br.readLine());
String source = br.readLine();
String destination = br.readLine();
int sameStatusOnes = 0;
int sameStatusZeros = 0;
int diffStatusOnes = 0;
int diffStatusZeros = 0;
for (int i = 0; i < n; i++) {
char c1 = source.charAt(i);
char c2 = destination.charAt(i);
if (c1 == c2) {
if (c1 == '0') {
sameStatusZeros += 1;
} else {
sameStatusOnes += 1;
}
} else {
if (c1 == '0') {
diffStatusZeros += 1;
} else {
diffStatusOnes += 1;
}
}
}
int sameStatus = sameStatusOnes + sameStatusZeros;
int diffStatus = diffStatusOnes + diffStatusZeros;
//first case
if (sameStatus == n) {
System.out.println(0);
} else if (diffStatus == n) {
//second case
if (diffStatus % 2 == 0 && diffStatusOnes == (n + 1) / 2)
System.out.println(n);
else
System.out.println(-1);
} else {
int op1 = -1;
int op2 = -1;
if (sameStatus % 2 != 0 && sameStatusOnes == (sameStatus + 1) / 2)
op1 = sameStatus;
if (diffStatus % 2 == 0 && diffStatusOnes == (diffStatus + 1) / 2)
op2 = diffStatus;
if (op1 != -1 && op2 != -1)
System.out.println(Integer.min(op1, op2));
else if (op1 != -1)
System.out.println(op1);
else if (op2 != -1)
System.out.println(op2);
else
System.out.println(-1);
}
t--;
}
}
}
| import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class Template {
static int mod = 1000000007;
public static void main(String[] args){
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int yo = sc.nextInt();
while (yo-- > 0) {
int n = sc.nextInt();
String s = sc.next();
String t = sc.next();
int op1 = cal(s,t,n);
int op2 = helper(s,t,n,'0');
int op3 = helper(s,t,n,'1');
int ans = min(min(op1,op2),op3);
if(ans == Integer.MAX_VALUE){
out.println("-1");
}
else {
out.println(ans);
}
}
out.close();
}
static int helper(String str1, String str2, int n, int ch){
char[] s = str1.toCharArray();
char[] t = str2.toCharArray();
int idx = -1;
for(int i = 0; i < n; i++){
if(s[i] == '1' && t[i] == ch){
idx = i;
break;
}
}
if(idx == -1){
return Integer.MAX_VALUE;
}
for(int i = 0; i < n; i++){
if(i == idx) continue;
if(s[i] == '1') s[i] = '0';
else s[i] = '1';
}
int ans = cal(String.valueOf(s),String.valueOf(t),n);
if(ans == Integer.MAX_VALUE) return ans;
return 1 + ans;
}
static int cal(String s, String t, int n){
int op01 = 0;
int op10 = 0;
for(int i = 0; i < n; i++){
if(s.charAt(i) != t.charAt(i)){
if(s.charAt(i) == '1') op10++;
else op01++;
}
}
if(op10 != op01){
return Integer.MAX_VALUE;
}
return op01 + op10;
}
/*
Source: hu_tao
Random stuff to try when stuck:
- use bruteforcer
- check for n = 1, n = 2, so on
-if it's 2C then it's dp
-for combo/probability problems, expand the given form we're interested in
-make everything the same then build an answer (constructive, make everything 0 then do something)
-something appears in parts of 2 --> model as graph
-assume a greedy then try to show why it works
-find way to simplify into one variable if multiple exist
-treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them)
-find lower and upper bounds on answer
-figure out what ur trying to find and isolate it
-see what observations you have and come up with more continuations
-work backwards (in constructive, go from the goal to the start)
-turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements)
-instead of solving for answer, try solving for complement (ex, find n-(min) instead of max)
-draw something
-simulate a process
-dont implement something unless if ur fairly confident its correct
-after 3 bad submissions move on to next problem if applicable
-do something instead of nothing and stay organized
-write stuff down
Random stuff to check when wa:
-if code is way too long/cancer then reassess
-switched N/M
-int overflow
-switched variables
-wrong MOD
-hardcoded edge case incorrectly
Random stuff to check when tle:
-continue instead of break
-condition in for/while loop bad
Random stuff to check when rte:
-switched N/M
-long to int/int overflow
-division by 0
-edge case for empty list/data structure/N=1
*/
public static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void sort(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
for (int i = 0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieve(int N) {
boolean[] sieve = new boolean[N + 1];
for (int i = 2; i <= N; i++)
sieve[i] = true;
for (int i = 2; i <= N; i++) {
if (sieve[i]) {
for (int j = 2 * i; j <= N; j += i) {
sieve[j] = false;
}
}
}
return sieve;
}
public static long power(long x, long y, long p) {
long res = 1L;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res;
}
public static void print(int[] arr, PrintWriter out) {
//for debugging only
for (int x : arr)
out.print(x + " ");
out.println();
}
public static int log2(int a){
return (int)(Math.log(a)/Math.log(2));
}
public static long ceil(long x, long y){
return (x + 0l + y - 1) / y;
}
static class FastScanner {
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
// For Input.txt and Output.txt
// FileInputStream in = new FileInputStream("input.txt");
// FileOutputStream out = new FileOutputStream("output.txt");
// PrintWriter pw = new PrintWriter(out);
// Scanner sc = new Scanner(in);
} | 0 |
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 |
1386da0e | f87eb1b3 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Solution {
public static int INF= Integer.MAX_VALUE;
public static long MOD= 1000000000+7L;
public static int WHITE= 0;
public static int GRAY= 1;
public static int BLACK= 2;
class Pair{
public int day;
int count;
Pair(int d, int c){
day=d; count=c;
}
}
static class Solver {
public void solve(InputReader in, PrintWriter out) {
int tt=in.nextInt();
//int tt=1;
for(int cases=1;cases<=tt;cases++){
int n= in.nextInt();
long odd_min= Long.MAX_VALUE;
long even_min= Long.MAX_VALUE;
long ans= Long.MAX_VALUE;
int odd_length=0;
int even_length=0;
long total=0;
for(int segment=1;segment<=n;segment++){
int cost= in.nextInt();
total+=cost;
if(segment%2==1){
odd_min = Math.min(odd_min, cost);
odd_length++;
}
else{
even_min = Math.min(even_min, cost);
even_length++;
}
if(segment>1){
{
long value=total-(odd_min+even_min);
value+= odd_min * (n-odd_length+1);
value+= even_min * (n-even_length+1);
ans= Math.min(value, ans);
}
}
}
System.out.println(ans);
}
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(in, out);
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());
}
}
} | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.PrintStream;
import java.io.PrintWriter;
public class A {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int aa, int bb)
{
first = aa; second = bb;
}
public int compareTo(Pair o)
{
if(this.second < o.second) return -1;
if(this.second > o.second) return +1;
return this.first - o.first;
}
}
static class Tuple implements Comparable<Tuple>{
long first, second,third;
public Tuple(long first, long second, long third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[n+1];
Arrays.fill(prime, true);
ArrayList<Integer> l = new ArrayList<>();
for (int p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i=p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p=2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long modPow(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return modPow(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n) {
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(int n, int k)
{
long ans = 1;
for(int i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
public static void Sort(int[] a)
{
List<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(int a[], int x)
{
int l=-1, r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0)
{
long INF = 1000000000000000007L;
int n = sc.nextInt();
long[] c = new long[n+1];
for(int i = 1;i<=n;i++) c[i] = sc.nextLong();
long ans = INF;
long mo = INF, so = 0, co = 0;
long me = INF, se = 0, ce = 0;
for(int i=1;i<=n;i++)
{
if(i%2 == 1)
{
mo = min(mo,c[i]);
so += c[i];
co++;
}
else
{
me = min(me,c[i]);
se += c[i];
ce++;
}
if(i>=2)
{
long x = so + (n - co) * mo + se + (n - ce) * me;
ans=min(ans,x);
}
}
fout.println(ans);
}
fout.close();
}
} | 0 |
0fd5b95a | 6490bbe8 | //package codeforces;
import java.io.PrintWriter;
import java.util.*;
public class codeforces {
static int dp[][]=new int[5001][5001];
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int t=1;
for(int tt=0;tt<t;tt++) {
int n=s.nextInt();
int a[]=new int[n];
ArrayList<Integer> z=new ArrayList<>();
ArrayList<Integer> o=new ArrayList<>();
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
if(a[i]==1) {
o.add(i);
}else {
z.add(i);
}
}
for(int i=0;i<5001;i++) {
Arrays.fill(dp[i], -1);
}
System.out.println(sol(0,0,z,o));
}
out.close();
s.close();
}
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 sortcol(int a[][],int c) {
Arrays.sort(a, (x, y) -> {
if (x[c] != y[c]) return(int)( x[c] - y[c]);
return (int)-(x[1]+x[2] - y[1]-y[2]);
});
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int sol(int i,int j,ArrayList<Integer> z,ArrayList<Integer> o) {
if(j==o.size()) {
return 0;
}
int h=z.size()-i;
int l=o.size()-j;
if(i==z.size()) {
return 10000000;
}
if(dp[i][j]!=-1) {
//System.out.println(i+" "+j);
return dp[i][j];
}
int ans1=sol(i+1,j,z,o);
int ans2=sol(i+1,j+1,z,o)+Math.abs(z.get(i)-o.get(j));
dp[i][j]=Math.min(ans1, ans2);
return dp[i][j];
}
}
| 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 |
8d9871a9 | eb6cfca7 | import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
static int[] readArray(int[] x) {
for(int i=0; i<x.length; ++i) x[i] = scan.nextInt();
return x;
}
static long[] readArray(long[] x) {
for(int i=0; i<x.length; ++i) x[i] = scan.nextLong();
return x;
}
public static void go() {
}
public static void main(String[] args) {
int t = scan.nextInt();
for(int it=0; it<t; ++it) {
int n = scan.nextInt();
long[] aa = readArray(new long[n]);
long minEven = aa[0];
long minOdd = aa[1];
long sum = aa[0]+aa[1];
long best = n*minEven + n*minOdd;
int numOdd = 1;
int numEven = 1;
for(int i=2; i<n; ++i) {
if(i%2 == 0) {
minEven = Math.min(aa[i], minEven);
numEven++;
}else {
minOdd = Math.min(aa[i], minOdd);
numOdd++;
}
sum += aa[i];
long score = sum;
score += minEven*(n-numEven);
score += minOdd*(n-numOdd);
best = Math.min(best, score);
}
System.out.println(best);
}
}
}
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;
public class Main {
public Main() throws FileNotFoundException {
// File file = Paths.get("input.txt").toFile();
// if (file.exists()) {
// System.setIn(new FileInputStream(file));
// }
long t = System.currentTimeMillis();
InputReader reader = new InputReader();
int ttt = reader.nextInt();
for (int tt = 0; tt < ttt; tt++) {
int n=reader.nextInt();
long[] s=new long[n];
for(int i=0;i<n;i++) {
s[i]=reader.nextLong();
}
long smallest1=s[0];
long smallest2=s[1];
long val=n*s[0]+n*s[1];
int left1=n-1;
int left2=n-1;
long base=s[0]+s[1];
for(int i=2;i<n;i++) {
if(i%2==0) {
//left1
val=Math.min(val, base+left2*smallest2+left1*s[i]);
base+=s[i];
smallest1=Math.min(smallest1, s[i]);
left1--;
}else {
val=Math.min(val, base+left1*smallest1+left2*s[i]);
base+=s[i];
smallest2=Math.min(smallest2, s[i]);
left2--;
//left2
}
}
System.out.println(val);
}
}
static class InputReader {
private byte[] buf = new byte[16384];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long 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 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 int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private 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;
}
}
public static void main(String[] args) throws FileNotFoundException {
new Main();
}
}
| 0 |
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 |
11373c16 | 7011024d | /* 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.util.*;
public class D {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
ArrayList<Integer> occupied = new ArrayList<>();
ArrayList<Integer> vacant = new ArrayList<>();
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
if (x == 1)
occupied.add(i);
else
vacant.add(i);
}
Solution Solution = new Solution(occupied, vacant);
// System.out.println(Solution.tabulation());
System.out.println(Solution.memoization());
}
}
class Solution {
ArrayList<Integer> occupied, vacant;
int x, y;
public Solution(ArrayList<Integer> occupied, ArrayList<Integer> vacant) {
this.occupied = occupied;
this.vacant = vacant;
x = occupied.size(); y = vacant.size();
}
int tabulation() {
return tabulation(x, y);
}
int tabulation(int x, int y) {
int[][] dp = new int[x+1][y+1];
for (int i = 0; i <= x; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE/2);
}
for (int i = 0; i <= x; i++) {
dp[i][0] = 0;
}
for (int i = 0; i <= y; i++) {
dp[0][i] = 0;
}
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
if(i == j) {
dp[i][j] = dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1));
}
else {
dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1)));
}
}
}
return dp[x][y];
}
int memoization() {
int[][] dp = new int[x][y];
for (int i = 0; i < x; i++) {
Arrays.fill(dp[i], -1);
}
return memoization(dp, x-1, y-1);
}
int memoization(int[][] dp, int n, int m) {
if(n < 0) {
return 0;
}
if(m < n) {
return Integer.MAX_VALUE;
}
if(dp[n][m] != -1) {
return dp[n][m];
}
int first = memoization(dp, n, m-1);
int second = memoization(dp, n-1, m-1) + Math.abs(occupied.get(n) - vacant.get(m));
dp[n][m] = Math.min(first, second);
return dp[n][m];
}
} | 0 |
3c667d4f | a7894e0b | import java.util.*;
public class j
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
while(n-->0)
{
int len=in.nextInt();
int t=in.nextInt();
int pos[]=new int[t];
int temp[]=new int[t];
for(int i=0;i<t;i++)
pos[i]=in.nextInt();
for(int i=0;i<t;i++)
temp[i]=in.nextInt();
long range[]=new long[len];
Arrays.fill(range,Long.MAX_VALUE-10000);
for(int i=0;i<t;i++)
range[pos[i]-1]=temp[i];
for(int i=1;i<len;i++)
{
range[i]=Math.min(range[i],1+range[i-1]);
}
for(int i=len-2;i>=0;i--)
{
range[i]=Math.min(range[i+1]+1,range[i]);
}
for(int i=0;i<len;i++)
{
System.out.print(range[i]+" ");
}System.out.println();
}
}
}
|
import java.io.*;
import java.math.*;
import java.util.*;
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n=sc.nextInt();
int k=sc.nextInt();
int[] ac=new int[k];
long[] ans=new long[n];
Arrays.fill(ans, Integer.MAX_VALUE/2);
for(int i=0;i<k;i++) {
ac[i]=sc.nextInt()-1;
}
for(int i=0;i<k;i++) {
long x = sc.nextLong();
ans[ac[i]] = x;
}
for(int i=1;i<n;i++) {
ans[i]=Math.min(ans[i], ans[i-1]+1);
}
for(int i=n-2;i>=0;i--) {
ans[i]=Math.min(ans[i], ans[i+1]+1);
}
for(int i=0;i<n;i++) {
res.append(ans[i]+" ");
}
res.append("\n");
}
System.out.println(res);
}
}
| 1 |
2fdcf0ad | d20e861b | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static int[][] map;
static int n, m;
static ArrayList<String> actions = new ArrayList<>();
static String[] move = new String[]{"", "0110", "1001", "1101", "1001", "1110", "1011", "0000", "0110", "0111", "1101", "0000", "0111", "0000", "0000", "0001"};
static int[] xdif = new int[]{0, 0, 1, 1}, ydif = new int[]{0, 1, 0, 1};
public static void main(String[] args) throws IOException {
int tests = sc.nextInt();
for (int t = 0; t < tests; t++)
solve();
pw.close();
}
static void solve() throws IOException {
n = sc.nextInt();
m = sc.nextInt();
map = new int[n][m];
actions.clear();
for (int i = 0; i < n; i++) {
String line = sc.next();
for (int j = 0; j < m; j++)
map[i][j] = line.charAt(j) - '0';
}
if (n % 2 + m % 2 == 2)
removeCorner();
if (n % 2 == 1)
for (int y = 0; y + 1 < m; y += 2)
removeDown(y);
if (m % 2 == 1)
for (int x = 0; x + 1 < n; x += 2)
removeRight(x);
for (int x = 0; x + 1 < n; x += 2)
for (int y = 0; y + 1 < m; y += 2)
square(x, y);
pw.println(actions.size());
for (String action : actions)
pw.println(action);
}
static void square(int x, int y) {
String type = "" + map[x][y] + map[x][y + 1] + map[x + 1][y] + map[x + 1][y + 1];
if (map[x][y] + map[x + 1][y] + map[x][y + 1] + map[x + 1][y + 1] > 0) {
int[][] poses = new int[3][];
int index = 0;
for (int t = 0; t < 4; t++) {
if (map[x + xdif[t]][y + ydif[t]] != move[Integer.parseInt(type, 2)].charAt(t) - '0')
poses[index++] = new int[]{x + xdif[t], y + ydif[t]};
}
action(poses[0][0], poses[0][1], poses[1][0], poses[1][1], poses[2][0], poses[2][1]);
square(x, y);
}
}
static void invert(int x, int y) {
map[x][y] = 1 - map[x][y];
}
static void removeCorner() {
if (map[n - 1][m - 1] == 1)
action(n - 1, m - 1, n - 2, m - 1, n - 1, m - 2);
}
static void removeRight(int x) {
int y = m - 1;
if (map[x][y] + map[x + 1][y] == 2)
action(x, y, x + 1, y, x, y - 1);
else if (map[x][y] == 1)
action(x, y, x, y - 1, x + 1, y - 1);
else if (map[x + 1][y] == 1)
action(x + 1, y, x, y - 1, x + 1, y - 1);
}
static void removeDown(int y) {
int x = n - 1;
if (map[x][y] + map[x][y + 1] == 2)
action(x, y, x, y + 1, x - 1, y);
else if (map[x][y] == 1)
action(x, y, x - 1, y, x - 1, y + 1);
else if (map[x][y + 1] == 1)
action(x, y + 1, x - 1, y, x - 1, y + 1);
}
static void action(int x1, int y1, int x2, int y2, int x3, int y3) {
actions.add((x1 + 1) + " " + (y1 + 1) + " " + (x2 + 1) + " " + (y2 + 1) + " " + (x3 + 1) + " " + (y3 + 1));
invert(x1, y1);
invert(x2, y2);
invert(x3, y3);
}
}
class Scanner {
static BufferedReader br;
static StringTokenizer st = new StringTokenizer("");
public Scanner(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public Scanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
public boolean hasNext() throws IOException {
if (st.hasMoreTokens())
return true;
String line;
while ((line = br.readLine()) != null) {
st = new StringTokenizer(line);
if (st.hasMoreTokens())
return true;
}
return false;
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String next() throws IOException {
hasNext();
return st.nextToken();
}
} | import java.util.*;
public class CodeForces{
// 4
// -3 -5 -2 1
// k 2 4 0 3
static ArrayList<int[]> ans = new ArrayList<>();
public static void add(int a,int b,int c,int d,int e,int f){
int[] tem=new int[6];
tem[0]=a;tem[1]=b;tem[2]=c;tem[3]=d;tem[4]=e;tem[5]=f;
ans.add(tem);
}
public static void helper(int[][] arr,int r,int c){
for(int k=0;k<12;k++){
if(arr[r][c]==1){
add(r,c,r+1,c,r,c+1);
arr[r][c]=1-arr[r][c];
arr[r+1][c]=1-arr[r+1][c];
arr[r][c+1]=1-arr[r][c+1];
}
if(arr[r][c+1]==1){
add(r,c+1,r+1,c+1,r,c);
arr[r][c+1]=1-arr[r][c+1];
arr[r+1][c+1]=1-arr[r+1][c+1];
arr[r][c]=1-arr[r][c];
}
if(arr[r+1][c]==1){
add(r+1,c+1,r,c,r+1,c);
arr[r+1][c+1]=1-arr[r+1][c+1];
arr[r][c]=1-arr[r][c];
arr[r+1][c]=1-arr[r+1][c];
}
if(arr[r+1][c+1]==1){
add(r+1,c+1,r+1,c,r,c+1);
arr[r+1][c+1]=1-arr[r+1][c+1];
arr[r+1][c]=1-arr[r+1][c];
arr[r][c+1]=1-arr[r][c+1];
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int m = sc.nextInt();
ans.clear();
int[][] arr=new int[n][m];
for(int i=0;i<n;i++){
String str = sc.next();
for(int j=0;j<m;j++){
arr[i][j]=str.charAt(j)-'0';
}
}
for(int i=0;i<n;i+=2){
for(int j=0;j<m;j+=2){
if(i==n-1&&j==m-1){
helper(arr,i-1,j-1);
}else if(i==n-1){
helper(arr,i-1,j);
}else if(j==m-1){
helper(arr,i,j-1);
}else{
helper(arr,i,j);
}
}
}
System.out.println(ans.size());
for(int[] move:ans){
for(int i=0;i<6;i++){
System.out.print(move[i]+1 +" ");
}
System.out.println();
}
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// System.out.print(arr[i][j] +" ");
// }
// System.out.println();
// }
}
}
} | 0 |
6018974f | bdf7bfb2 | import java.util.*;
public class contestC {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-->0){
int n = scanner.nextInt();
ArrayList<String> list = new ArrayList<>();
for(int i=0;i<n;++i){
String s = scanner.next();
list.add(s);
}
int ans = 0;
for(char a='a';a<='e';++a ){
ArrayList<Integer> num = new ArrayList<>();
for(String s : list){
int cnt = 0;
for(int i=0;i<s.length();++i){
if(s.charAt(i)==a) cnt++;
else cnt--;
}
num.add(cnt);
}
Collections.sort(num,Collections.reverseOrder());
int sum = 0;
for(int i=0;i<num.size();++i){
sum+=num.get(i);
if(sum<=0){
ans = Math.max(ans,i);
break;
}
}
if(sum > 0) {
ans = n;
break;
}
}
System.out.println(ans);
}
}
}
| import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
String[] s = new String[n];
for(int i=0; i<n; i++)
s[i] = sc.next();
int MAX = 0;
for(char c = 'a'; c <= 'e'; c++){
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top;
for(int i=0; i<n; ++i) {
int curChar = 0;
int otherChar = 0;
for(int j=0; j<s[i].length(); j++) {
if(s[i].charAt(j) == c)
curChar++;
else
otherChar++;
}
int diff = curChar - otherChar;
pq.add(diff);
}
int cur = 0;
int numberOfWords = 0;
while(!pq.isEmpty()){
if(cur + pq.peek() > 0){
cur += pq.poll();
numberOfWords++;
}else{
break;
}
}
MAX = Math.max(MAX, numberOfWords);
}
pw.println(MAX);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
| 0 |
829d2024 | 9852706b | import java.util.*;
import java.io.*;
public class Main{
public static class Element implements Comparable<Element>{
public int key;
public int value;
Element(int k, int v)
{
key=k;
value=v;
}
@Override
public int compareTo(Element o) {
if(this.value<o.value)
return -1;
if(this.value>o.value)
return 1;
return 0;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
int x=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;++i)
arr[i]=sc.nextInt();
PriorityQueue<Element> pq=new PriorityQueue<>();
for(int i=1;i<=m;++i)
{
pq.add(new Element(i,0));
}
System.out.println("YES");
for(int j=0;j<n;j++)
{
Element cur = pq.poll();
System.out.print(cur.key+" ");
cur.value+= arr[j];
pq.add(cur);
}
System.out.println();
}
}
}
| import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int modulo=998244353;
public static void main(String[] args) {
FastScanner in = new FastScanner();
int test=in.nextInt();
while(test-->0){
int n=in.nextInt();
int m=in.nextInt();
int x=in.nextInt();
PriorityQueue<pair> pq=new PriorityQueue<>();
int arr[]=new int[n];
for(int i=1;i<=m;i++){
pq.add(new pair(i,0));
}
System.out.println("YES");
for(int i=0;i<n;i++){
pair p=pq.poll();
int a=in.nextInt();
p.y+=a;
pq.add(p);
System.out.print(p.x+" ");
}
System.out.println();
}
// int sum=in.nextInt();
//
// boolean dp[][]=new boolean[n+1][sum+1];
//
// for(int i=0;i<=n;i++){
// for(int j=0;j<=sum;j++){
// if(i==0)
// dp[i][j]=false;
// if(j==0)
// dp[i][j]=true;
//
// }
// }
//
// for(int i=1;i<=n;i++){
// for(int j=1;j<=sum;j++){
// if(arr[i-1]<=j)
// dp[i][j]=(dp[i-1][j])||(dp[i-1][j-arr[i-1]]);
// else
// dp[i][j]=dp[i-1][j];
//
// }
// }
// boolean flag=false;
// for(int i=0;i<=n;i++){
// if(dp[i][sum]==true){
// flag=true;
// System.out.println(1);
// break;
// }
//
// }
// if(!flag)
// System.out.println(0);
}
public static void solve1(int n,int arr[]){
int b[]=new int[2*n+1];
for(int i=0;i<n;i++){
b[arr[i]]=i+1;
}
int count=0;
for(int i=3;i<2*n;i++){
for(int j=1;j*j<i;j++){
if(i%j==0 && i!=j*j){
if(b[j]+b[i/j]==i){
count++;
}
}
}
}
System.out.println(count);
}
public static long modpow(long a, long b){
if(b==0){
return 1;
}
long x = modpow(a, b/2);
x = (x*x)%modulo;
if(b%2==1){
return (x*a)%modulo;
}
return x;
}
public static long factorial(long n){
if(n==1) return 1;
if(n==2) return 2;
if(n==3) return 6;
return n*factorial(n-1);
}
public static long benches(int n){
return ncr(n,5)*n*(n-1)*(n-2)*(n-3)*(n-4);
}
public static long ncr(int n,int r){
long dp[][]=new long[n+1][r+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=Math.min(i,r);j++){
if(j==0 ||j==i)
dp[i][j]=1;
else{
dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
}
}
}
return dp[n][r];
}
public static long select(int n){
return ncr(n,5)+ncr(n,6)+ncr(n,7);
}
public static long ncr1(int n,int r){
long k=Math.max(r,n-r);
long num=1;
long den=1;
long j=1;
for(long i=n;i>k;i--){
num*=i;
den*=j;
j++;
}
return num/den;
}
public static int countDiff(int arr[],int n,int diff){
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
int max=sum/2;
boolean dp[][]=new boolean[n+1][max+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=max;j++){
if(i==0)
dp[i][j]=false;
if(j==0)
dp[i][j]=true;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=max;j++){
if(arr[i-1]<=j)
dp[i][j]=dp[i-1][j]||dp[i-1][j-arr[i-1]];
else
dp[i][j]=dp[i-1][j];
}
}
int min=0;
int count=0;
for(int j=0;j<=max;j++){
min=0;
if(dp[n][j]==true){
min=sum-2*j;
if(min==diff)
count++;
}
}
return count;
}
public static int minimumDiff(int arr[],int n){
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
int max=sum/2;
boolean dp[][]=new boolean[n+1][max+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=max;j++){
if(i==0)
dp[i][j]=false;
if(j==0)
dp[i][j]=true;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=max;j++){
if(arr[i-1]<=j)
dp[i][j]=dp[i-1][j]||dp[i-1][j-arr[i-1]];
else
dp[i][j]=dp[i-1][j];
}
}
int min=Integer.MAX_VALUE;
for(int j=0;j<=max;j++){
if(dp[n][j]==true){
min=Math.min(min,sum-2*j);
}
}
return min;
}
public static int findTargetSumWays(int[] nums, int target) {
int n=nums.length;
int dp[][]=new int[n+1][target+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=target;j++){
if(i==0)
dp[i][j]=0;
if(j==0)
dp[i][j]=1;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=target;j++){
if(nums[i-1]<=j)
dp[i][j]=dp[i-1][j]+dp[i-1][j-nums[i-1]];
else
dp[i][j]=dp[i-1][j];
}
}
// for(int i=0;i<=n;i++){
// for(int j=0;j<=target;j++){
// System.out.print(dp[i][j]+" ");
// }
// System.out.println();
// }
return dp[n][target];
}
static int equalPartition(int n, int arr[],int sum)
{
int max=sum;
boolean dp[][]=new boolean[n+1][max+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=max;j++){
if(i==0)
dp[i][j]=false;
if(j==0)
dp[i][j]=true;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=max;j++){
if(arr[i-1]<=j){
dp[i][j]=(dp[i-1][j]||dp[i-1][j-arr[i-1]]);
}
else{
dp[i][j]=dp[i-1][j];
}
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=max;j++){
System.out.print(dp[i][j]+" ");
}
System.out.println();
}
int count=0;
for(int i=0;i<=n;i++){
if(dp[i][max]==true){
count++;
}
}
return count;
}
public static long power(long base,long power){
long res=1;
while(power!=0){
if(power%2==0){
power/=2;
base=(base*base);
}
else{
power--;
res=(res*base);
}
}
return res;
}
}
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());
}
}
class pair implements Comparable<pair>
{
int x;
int y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o)
{
return (int)(y-o.y);
}
} | 1 |
0b04b41e | 4da08761 | import java.io.BufferedReader;
import java.io.*;
import java.util.*;
public class josph {
static BufferedReader br;
static long mod = 1000000000 + 7;
static HashSet<Integer> p = new HashSet<>();
static boolean debug =true;
// Arrays.sort(time , (a1,a2) -> (a1[0]-a2[0])); 2d array sort lamda
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
int tc = 1;
tc= cinI();
while(tc-->0){
int n =cinI();
String[] a= new String[n];
int[][] f =new int[10][n];
for(int i=0;i<n;i++){
a[i]=cin();
char[] x = a[i].toCharArray();
for(char c:x){
int index = c-'a';
f[index][i]+=1;
}
for(int j=0;j<10;j++){
int rem =x.length-f[j][i];
f[j][i]-=rem;
}
}
int max=0;
for(int j=0;j<10;j++){
Arrays.sort(f[j]);
int cnt=0;
int sum=0;
for(int i=n-1;i>=0;i--){
sum+=f[j][i];
if(sum>0){
cnt+=1;
}
else{
break;
}
}
max=max(max,cnt);
}
System.out.println(max);
}
}
public static <E> void print(String var ,E e){
if(debug==true){
System.out.println(var +" "+e);
}
}
private static long[] sort(long[] e){
ArrayList<Long> x=new ArrayList<>();
for(long c:e){
x.add(c);
}
Collections.sort(x);
long[] y = new long[e.length];
for(int i=0;i<x.size();i++){
y[i]=x.get(i);
}
return y;
}
public static void printDp(long[][] dp) {
int n = dp.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < dp[0].length; j++) {
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
}
private static long gcd(long a, long b) {
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a % b, b);
return gcd(a, b % a);
}
public static long min(long a, long b) {
return Math.min(a, b);
}
public static int min(int a, int b) {
return Math.min(a, b);
}
public static void sieve() {
int[] pf = new int[100000000 + 1];
//0 prime //1 not prime
pf[0] = 1;
pf[1] = 1;
for (int j = 2; j <= 10000; j++) {
if (pf[j] == 0) {
p.add(j);
for (int k = j * j; k < pf.length; k += j) {
pf[k] = 1;
}
}
}
}
public static int[] readArray(int n, int x, int z) throws Exception {
int[] arr = new int[n];
String[] ar = cinA();
for (int i = x; i < n + x; i++) {
arr[i] = getI(ar[i - x]);
}
return arr;
}
public static long[] readArray(int n, int x) throws Exception {
long[] arr = new long[n];
String[] ar = cinA();
for (int i = x; i < n + x; i++) {
arr[i] = getL(ar[i - x]);
}
return arr;
}
public static void arrinit(String[] a, long[] b) throws Exception {
for (int i = 0; i < a.length; i++) {
b[i] = Long.parseLong(a[i]);
}
}
public static HashSet<Integer>[] Graph(int n, int edge, int directed) throws Exception {
HashSet<Integer>[] tree = new HashSet[n];
for (int j = 0; j < edge; j++) {
String[] uv = cinA();
int u = getI(uv[0]);
int v = getI(uv[1]);
if (directed == 0) {
tree[v].add(u);
}
tree[u].add(v);
}
return tree;
}
public static void arrinit(String[] a, int[] b) throws Exception {
for (int i = 0; i < a.length; i++) {
b[i] = Integer.parseInt(a[i]);
}
}
static double findRoots(int a, int b, int c) {
// If a is 0, then equation is not
//quadratic, but linear
int d = b * b - 4 * a * c;
double sqrt_val = Math.sqrt(Math.abs(d));
// System.out.println("Roots are real and different \n");
return Math.max((double) (-b + sqrt_val) / (2 * a),
(double) (-b - sqrt_val) / (2 * a));
}
public static String cin() throws Exception {
return br.readLine();
}
public static String[] cinA() throws Exception {
return br.readLine().split(" ");
}
public static String[] cinA(int x) throws Exception {
return br.readLine().split("");
}
public static String ToString(Long x) {
return Long.toBinaryString(x);
}
public static void cout(String s) {
System.out.println(s);
}
public static Integer cinI() throws Exception {
return Integer.parseInt(br.readLine());
}
public static int getI(String s) throws Exception {
return Integer.parseInt(s);
}
public static long getL(String s) throws Exception {
return Long.parseLong(s);
}
public static long max(long a, long b) {
return Math.max(a, b);
}
public static int max(int a, int b) {
return Math.max(a, b);
}
public static void coutI(int x) {
System.out.println(String.valueOf(x));
}
public static void coutI(long x) {
System.out.println(String.valueOf(x));
}
public static Long cinL() throws Exception {
return Long.parseLong(br.readLine());
}
public static void arrInit(String[] arr, int[] arr1) throws Exception {
for (int i = 0; i < arr.length; i++) {
arr1[i] = getI(arr[i]);
}
}
} | import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Solution {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Main solver = new Main();
boolean multipleTC = true;
int testCount = multipleTC ? Integer.parseInt(in.next()) : 1;
for (int i = 1; i <= testCount; i++)
solver.solve(in, out, i);
out.close();
}
static class Main {
PrintWriter out;
InputReader in;
public void solve(InputReader in, PrintWriter out, int test) {
this.out = out;
this.in = in;
int n = ni();
String[] arr = new String[n];
int[][] freq = new int[n][5];
int[][] rem = new int[n][5];
for(int i = 0; i < n; i++){
arr[i] = n();
for(int j = 0; j < arr[i].length(); j++)
freq[i][arr[i].charAt(j) - 'a']++;
for(int j = 0; j < 5; j++)
rem[i][j] = arr[i].length() - freq[i][j];
}
int ans = 0;
for(int i = 0; i < 5; i++){
int[] vals = new int[n];
for(int j = 0; j < n; j++)
vals[j] = freq[j][i] - rem[j][i];
Arrays.sort(vals);
int sum = 0, x = 0;
for(int j = n - 1; j >= 0; j--){
if(sum + vals[j] > 0){
x++;
sum += vals[j];
} else {
break;
}
}
if(x > ans) {
ans = x;
}
}
pn(ans);
}
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
String n(){
return in.next();
}
int ni() {
return in.nextInt();
}
long nl() {
return in.nextLong();
}
void pn(long zx) {
out.println(zx);
}
void pn(String sz) {
out.println(sz);
}
void pn(double dx){
out.println(dx);
}
class Tuple {
long x;
long y;
Tuple(long a, long b) {
x = a;
y = b;
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new UnknownError();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | 0 |
94b3b86d | efe594c3 | import java.io.*;
import java.util.*;
public class D
{
public static ArrayList<Integer> adj[];
public static int node;
public static int dist = 0;
public static void main(String[] args) throws IOException
{
FastScanner sc = new FastScanner();
int T = sc.nextInt();
PrintWriter out = new PrintWriter(System.out);
for(int t = 0; t < T; t++){
int N = sc.nextInt();
int a = sc.nextInt(); int b = sc.nextInt();
int da = sc.nextInt(); int db = sc.nextInt();
adj = new ArrayList[N+1];
for(int i = 0; i <= N; i++){
adj[i] = new ArrayList<Integer>();
}
for(int i = 0; i < N-1; i++){
int v = sc.nextInt();
int u = sc.nextInt();
adj[v].add(u);
adj[u].add(v);
}
if(db > 2*da){
dfs1(a, 0, b, 0);
if(dist <= da){
out.println("Alice");
}
else{
node = 0;
dist = 0;
dfs(1, 0, 0);
dfs(node, 0, 0);
if(dist > 2*da){
out.println("Bob");
}
else{
out.println("Alice");
}
}
}
else{
out.println("Alice");
}
}
out.close();
}
public static void dfs1(int a, int p, int b, int d){
if(a == b){
dist = d;
}
for(int next : adj[a]){
if(next != p){
dfs1(next, a, b, d+1);
}
}
}
public static void dfs(int i, int p, int d){
if(d > dist){
node = i;
dist = d;
}
for(int next : adj[i]){
if(next != p){
dfs(next, i, d+1);
}
}
}
static class FastScanner {
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] 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;
}
}
}
static void ASSERT(boolean assertion, String message) {
if (!assertion) throw new AssertionError(message);
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class problemD {
static ArrayList<Integer>[] adj;
static boolean found = false;
static int[] D = new int[212345];
private static void solve() throws Exception {
int n = fs.nextInt();
int a = fs.nextInt();
int b = fs.nextInt();
int da = fs.nextInt();
int db = fs.nextInt();
adj = new ArrayList[n+1];
for (int i = 0 ; i <= n ; i ++ ) adj[i] = new ArrayList<Integer>();
for (int i = 0 ; i < n-1; i ++ ) {
int u = fs.nextInt();
int v = fs.nextInt();
adj[u].add(v);
adj[v].add(u);
}
found = false;
dfs(a, 0, 0, da, b);
if (found) {
out.println("Alice");
return;
}
dfs(b,0,0);
int maxD = -1;
int node = -1;
for (int i = 1; i <= n; i ++ ) {
if (D[i] > maxD) {
maxD = D[i];
node = i;
}
}
dfs(node, 0, 0);
maxD = -1;
for (int i = 1; i <= n; i ++ ) {
if (D[i] > maxD) {
maxD = D[i];
}
}
boolean bob = db > da * 2 && maxD > da * 2;
out.println(bob ? "Bob" : "Alice");
}
private static void dfs(int node, int parent, int dist) {
D[node] = dist;
for (int x: adj[node]) {
if (x != parent) {
dfs(x, node, dist+1);
}
}
}
private static void dfs(int node, int parent, int dist, int da, int b) {
if (node == b) { found = true; return; }
for (int x: adj[node]) {
if (x != parent) {
if (dist+1 <= da) {
dfs(x, node, dist + 1, da, b);
}
}
}
}
private static FastScanner fs = new FastScanner();
private static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int T = 1;
T = fs.nextInt();
for (int t = 0; t < T; t++) {
solve();
}
out.close();
}
static void debug(Object... O) {
System.err.print("DEBUG ");
System.err.println(Arrays.deepToString(O));
}
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());
}
String nextString() {
return next();
}
}
}
| 0 |
1230c43e | ce0b2178 | //package com.company;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static boolean[] primecheck = new boolean[1000002];
public static void main(String[] args) throws IOException {
OutputStream outputStream = System.out;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t = 1;
t = in.nextInt();
for (int i = 0; i < t; i++) {
solver.solve(in, out);
}
out.close();
}
static class PROBLEM {
public void solve(FastReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.readArray(n);
char[] c = in.nextLine().toCharArray();
int cur = 1;
ArrayList<Pair> p = new ArrayList<>();
for (int i = 0; i < n; i++) {
p.add(new Pair(c[i], a[i]));
}
Collections.sort(p);
// for (int i = 0; i < n; i++) {
// out.println(p.get(i).x + " " + p.get(i).y);
// }
for (int i = 0; i < n; i++) {
if(p.get(i).x == 'B' && p.get(i).y < i+1){
out.println("NO");
return;
}
if(p.get(i).x == 'R' && p.get(i).y > i+1){
out.println("NO");
return;
}
}
out.println("YES");
// int n = in.nextInt(), m = in.nextInt();
// char[] s = in.nextLine().toCharArray();
//
// int rl = 0, ud = 0, r = 0 , l = 0, rlf = 0, udf = 0;
// int lmax = 0, rmax = 0, umax = 0, dmax = 0;
//
// for (int i = 0; i < s.length; i++) {
// if(s[i] == 'L'){
// if(rlf == 0) rlf = -1;
// rl--;
// l--;
// if(rl == 0) l = 0;
// if(rl < 0 && lmax+rmax <= m) lmax = Math.max(Math.abs(l), lmax);
// }
// if(s[i] == 'R'){
// if(rlf == 0) rlf = 1;
// rl++;
// r++;
//
// if(rl > 0 && lmax+rmax <= m) rmax = Math.max(r, rmax);
// }
// if(s[i] == 'U'){
// if(udf == 0) udf = 1;
// ud++;
// r = Math.max(Math.abs(ud), r);
// if(ud > 0 && umax+dmax <= n) umax = Math.max(Math.abs(ud), umax);
// }
// if(s[i] == 'D'){
// if(udf == 0) udf = -1;
// ud--;
// r = Math.max(Math.abs(ud), r);
// if(ud < 0 && umax+dmax <= n) dmax = Math.max(Math.abs(ud), dmax);
// }
// }
//
// int ansc = 0, ansr = 0;
//
// out.println(rlf + " lmx = " + lmax + " rmax" + rmax);
//
// if(rlf == 1) ansc = m-rmax;
// else if(rlf == -1) ansc = 1+lmax;
// else ansc = 1;
//
// if(udf == 1) ansr = 1+umax;
// else if(udf == -1) ansr = m-dmax;
// else ansr = 1;
//
// out.println(ansr + " " + ansc);
}
}
static HashMap<Integer, Integer> initializeMap(int n){
HashMap<Integer,Integer> hm = new HashMap<>();
for (int i = 0; i <= n; i++) {
hm.put(i, 0);
}
return hm;
}
static boolean isRegular(char[] c){
Stack<Character> s = new Stack<>();
for (char value : c) {
if (s.isEmpty() && value == ')') return false;
if (value == '(') s.push(value);
else s.pop();
}
return s.isEmpty();
}
static ArrayList<ArrayList<Integer>> createAdj(int n, int e){
FastReader in = new FastReader();
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n + 1; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < e; i++) {
int a = in.nextInt(), b = in.nextInt();
System.out.println(a);
adj.get(a).add(b);
adj.get(b).add(a);
}
return adj;
}
static int binarySearch(int[] a, int l, int r, int x){
if(r>=l){
int mid = l + (r-l)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) return binarySearch(a, l, mid-1, x);
else return binarySearch(a,mid+1, r, x);
}
return -1;
}
static boolean isPalindromeI(int n){
int d = 0;
int y = n;
while(y>0){
d++;
y/=10;
}
int[] a = new int[d];
for (int i = 0; i < d; i++) {
a[i] = n%10;
n/=10;
}
System.out.println(Arrays.toString(a));
for (int i = 0; i < d / 2; i++) {
if(a[i] != a[d-i-1]) return false;
}
return true;
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static boolean isSquare(double a) {
boolean isSq = false;
double b = Math.sqrt(a);
double c = Math.sqrt(a) - Math.floor(b);
if (c == 0) isSq = true;
return isSq;
}
static int exponentMod(int A, int B, int C) {
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (int) ((y + C) % C);
}
static class Pair implements Comparable<Pair>{
char x;
int y;
Pair(char x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
static class Tuple implements Comparable<Tuple>{
int x, y, id;
Tuple(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
public int compareTo(Tuple o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
// public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
// public U x;
// public V y;
//
// public Pair(U x, V y) {
// this.x = x;
// this.y = y;
// }
//
// public int hashCode() {
// return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
// }
//
// public boolean equals(Object o) {
// if (this == o)
// return true;
// if (o == null || getClass() != o.getClass())
// return false;
// Pair<U, V> p = (Pair<U, V>) o;
// return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
// }
//
// public int compareTo(Pair<U, V> b) {
// int cmpU = x.compareTo(b.x);
// return cmpU != 0 ? cmpU : y.compareTo(b.y);
// }
//
// public int compareToY(Pair<U, V> b) {
// int cmpU = y.compareTo(b.y);
// return cmpU != 0 ? cmpU : x.compareTo(b.x);
// }
//
// public String toString() {
// return String.format("(%s, %s)", x.toString(), y.toString());
// }
//
// }
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
boolean nextBoolean() {
return !(nextInt() == 0);
}
// boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] readArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = nextInt();
return array;
}
}
private static int[] mergeSort(int[] array) {
//array.length replaced with ctr
int ctr = array.length;
if (ctr <= 1) {
return array;
}
int midpoint = ctr / 2;
int[] left = new int[midpoint];
int[] right;
if (ctr % 2 == 0) {
right = new int[midpoint];
} else {
right = new int[midpoint + 1];
}
for (int i = 0; i < left.length; i++) {
left[i] = array[i];
}
for (int i = 0; i < right.length; i++) {
right[i] = array[i + midpoint];
}
int[] result = new int[array.length];
left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);
return result;
}
private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length || rightPointer < right.length) {
if (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
} else if (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
return result;
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
}
| import java.io.*;
import java.util.*;
public class D {
static class Pair implements Comparable<Pair>{
int a;
char b;
public Pair(int a, char b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair p) {
if(this.b == p.b)
return this.a - p.a;
return this.b - p.b;
}
}
public static void main(String[] args)throws IOException {
FastScanner scan = new FastScanner();
PrintWriter output = new PrintWriter(System.out);
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt();
int arr[] = scan.readArray(n);
char line[] = scan.next().toCharArray();
ArrayList<Pair> pairs = new ArrayList<Pair>();
for(int i = 0;i<n;i++) {
pairs.add(new Pair(arr[i], line[i]));
}
Collections.sort(pairs);
boolean possible = true;
for(int i = 1;i<=n;i++) {
if(pairs.get(i-1).a == i) {
continue;
}
else if(pairs.get(i-1).a < i && pairs.get(i-1).b == 'R') {
continue;
}
else if(pairs.get(i-1).a > i && pairs.get(i-1).b == 'B') {
continue;
}
else {
possible = false;
break;
}
}
output.println(possible == true ? "YES" : "NO");
}
output.flush();
}
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr) list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) arr[i] = list.get(i);
return arr;
}
public static int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b%a, a);
}
static boolean isPrime(int n) {
if (n <= 1) return false;
else if (n == 2) return true;
else if (n % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) if (n % i == 0) return false;
return true;
}
public static void printArray(int arr[]) {
for(int i:arr) System.out.print(i+" ");
System.out.println();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| 0 |
8f31b279 | d2f74dfc | 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.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 m=sc.nextInt();
int x=sc.nextInt();
int[] a=new int[n];
int[] c=new int[n];
Map<Integer,Stack<Integer>> mp=new HashMap<>();
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
//c[i]=a[i];
if(mp.containsKey(a[i])){
Stack<Integer> l=mp.get(a[i]);
l.push(i);
mp.put(a[i],l);
}
else{
Stack<Integer> l=new Stack<>();
l.push(i);
mp.put(a[i],l);
}
}
Arrays.sort(a);
int[] b=new int[n];
int cn=1;
for(int i=0;i<n;i++){
cn=cn%m;
Stack<Integer> li=mp.get(a[i]);
if(!li.isEmpty()){
int val=li.peek();
b[val]=cn+1;
c[i]+=a[i];
li.pop();
mp.put(a[i],li);
}
cn++;
}
int mv=c[0]-c[n-1];
if(Math.abs(mv)>x) System.out.println("NO");
else {
System.out.println("YES");
for(int i=0;i<n;i++){
System.out.print(b[i]+" ");
}
System.out.println();
}
}
}
} | 0 |
085ddefc | 1500a4fa | 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();
String a = sc.next();
String b = sc.next();
int change = 0;
int nochange = 0;
for(int i=0; i<n; i++){
if(a.charAt(i) == b.charAt(i)){
nochange++;
}
else{
change++;
}
}
if(change%2 == 0 && nochange%2 == 0){
int count1 = 0;
for(int i=0; i<n; i++){
if(a.charAt(i) != b.charAt(i) && a.charAt(i) == '1'){
count1++;
}
}
if(count1 == change/2){
System.out.println(change);
}
else{
System.out.println(-1);
}
}
else if(change%2 == 0 && nochange%2 == 1){
// both ways possible
int count1 = 0;
int ans = Integer.MAX_VALUE;
for(int i=0; i<n; i++){
if(a.charAt(i) == b.charAt(i) && a.charAt(i) == '1'){
count1++;
}
}
if(count1 == nochange/2 + 1){
ans = Math.min(ans, nochange);
}
count1 = 0;
for(int i=0; i<n; i++){
if(a.charAt(i) != b.charAt(i) && a.charAt(i) == '1'){
count1++;
}
}
if(count1 == change/2){
ans = Math.min(ans, change);
}
if(ans == Integer.MAX_VALUE){
System.out.println(-1);
}
else{
System.out.println(ans);
}
}
else if(change%2 == 1 && nochange%2 == 0){
System.out.println(-1);
}
else{
int count1 = 0;
for(int i=0; i<n; i++){
if(a.charAt(i) == b.charAt(i) && a.charAt(i) == '1'){
count1++;
}
}
if(count1 == nochange/2 + 1){
System.out.println(nochange);
}
else{
System.out.println(-1);
}
}
}
}
}
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;
}
}
}
|
import java.io.*;
import java.util.*;
public class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
String a=sc.next();
String b=sc.next();
int i;
int zero=0,one=0;
int x=0,y=0,x1=0,y1=0;
for(i=0;i<n;i++){
if(a.charAt(i)=='0')
zero++;
else
one++;
}
if(one==0){
if(a.equals(b))
System.out.println("0");
else
System.out.println("-1");
}
else{
int same=0,diff=0;
for(i=0;i<n;i++){
if(a.charAt(i)==b.charAt(i)){
same++;
x++;
if(a.charAt(i)=='1'){
x1++;
}
}
else{
diff++;
y++;
if(a.charAt(i)=='1'){
y1++;
}
}
}
int ans=Integer.MAX_VALUE;
if(x%2!=0&&(x+1)/2==x1){
ans=x;
}
if(y%2==0&&(y/2)==y1){
ans=Math.min(ans,y);
}
if(ans==Integer.MAX_VALUE){
System.out.println("-1");
}else{
System.out.println(ans);
}
}
}
}
}
| 0 |
48c5f745 | 4d25818e |
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();
}
}
}
| 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 k= sc.nextInt();
int ac[] = new int[k];
int temp[] = new int[k];
int ans[] = new int[n+1];
for(int i=0;i<k;i++)
ac[i] = sc.nextInt();
for(int i=0;i<k;i++)
temp[i] = sc.nextInt();
Arrays.fill(ans,Integer.MAX_VALUE);
for(int i=0;i<k;i++)
ans[ac[i]] = temp[i];
for(int i=2;i<n+1;i++)
ans[i]=Math.min(ans[i],ans[i-1]==Integer.MAX_VALUE ? ans[i-1] : ans[i-1]+1);
for(int i=n-1;i>0;i--)
ans[i]=Math.min(ans[i],ans[i+1]+1);
for(int i=1;i<n+1;i++)
System.out.print(ans[i]+" ");
System.out.println(" ");
}
}
} | 1 |
1f27b677 | 81fb6415 | import java.util.*;
import java.io.*;
public class Codeforces {
static long accurateFloor(long a, long b) {
long val = a/b;
while(val*b > a) val--;
return val;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
Long[] a = new Long[n];
st = new StringTokenizer(br.readLine());
long[] sum = new long[n];
for(int i = 0; i < n; i++) {
a[i] = Long.parseLong(st.nextToken());
}
Arrays.sort(a);
sum[0] = a[0];
for(int i = 1; i < n; i++) {
sum[i] += sum[i-1]+a[i];
}
long ans = Long.MAX_VALUE;
for(int y = 0; y < n; y++) {
// long z = k-sum[n-y-1]+a[0];
// if(z > 0) z = z/(y+1);
// else {
// if(z%(y+1) == 0) z = z/(y+1);
// else {
// z = z/(y+1);
// z -= 1;
// }
// }
long x = a[0]-accurateFloor(k-sum[n-y-1]+a[0], y+1);
x = Math.max(0, x);
ans = Math.min(ans, y+x);
}
writer.println(ans);
}
writer.close();
br.close();
}
}
| import java.math.BigInteger;
import java.util.*;
import java.io.*;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
public class CodeForces {
public void run() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
next : while (t-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
Long[] a = new Long[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
a[i] = Long.parseLong(st.nextToken());
}
Arrays.sort(a);
long ans = Long.MAX_VALUE;
long[] lsum = new long[n + 1];
for (int i = 0; i < n; i++) {
lsum[i + 1] = lsum[i] + a[i];
}
for (long y = 0; y < n; y++) {
long x = 0;
if ((k - lsum[n - (int)y] + a[0]) >= 0) {
x = (k - lsum[n - (int)y] + a[0]) / (y + 1);
} else {
if ((k - lsum[n - (int)y] + a[0]) % (y + 1) == 0) {
x = (k - lsum[n - (int)y] + a[0]) / (y + 1);
} else {
x = (k - lsum[n - (int)y] + a[0]) / (y + 1) - 1;
}
}
x = a[0] - x;
ans = Math.min(ans, Math.max(0, x) + y);
}
System.out.println(ans);
}
}
public static void main(String[] args) throws Exception {
new CodeForces().run();
}
}
| 0 |
End of preview. Expand
in Dataset Viewer.
No dataset card yet
New: Create and edit this dataset card directly on the website!
Contribute a Dataset Card- Downloads last month
- 0