F1
stringlengths 8
8
| F2
stringlengths 8
8
| text_1
stringlengths 607
24.4k
| text_2
stringlengths 591
24.4k
| label
int64 0
1
|
---|---|---|---|---|
5af25bd7 | 77ec956f | import java.io.*;
import java.util.*;
public class MySolution {
public static void main(String[] args) throws Exception {
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
StringBuilder out = new StringBuilder();
int numOfTestCases = Integer.parseInt(bu.readLine());
for (int tc = 1; tc <= numOfTestCases; tc++) {
int vertices = Integer.parseInt(bu.readLine());
connections = new ArrayList[vertices];
for (int i = 0; i < vertices; i++) {
connections[i] = new ArrayList<Integer>();
String st[] = bu.readLine().split(" ");
a[i][0] = Integer.parseInt(st[0]);
a[i][1] = Integer.parseInt(st[1]);
s[i][0] = s[i][1] = 0;
}
for (int j = 0; j < vertices-1; j++) {
String st[] = bu.readLine().split(" ");
int u = Integer.parseInt(st[0]) - 1, v = Integer.parseInt(st[1]) - 1;
connections[u].add(v);
connections[v].add(u);
}
dfs(0, -1);
out.append(Math.max(s[0][0], s[0][1]) + "\n");
}
System.out.print(out);
}
static int N = 100000;
static int[][] a = new int[N][2];
static long[][] s = new long[N][2];
static ArrayList<Integer>[] connections;
public static void dfs(int n, int parent) {
for (int child : connections[n]) {
if (child != parent) {
dfs(child, n);
s[n][0] += Math.max(s[child][0] + Math.abs(a[n][0] - a[child][0]), s[child][1] + Math.abs(a[n][0] - a[child][1]));
s[n][1] += Math.max(s[child][0] + Math.abs(a[n][1] - a[child][0]), s[child][1] + Math.abs(a[n][1] - a[child][1]));
}
}
}
}
| import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
int n=Integer.parseInt(bu.readLine());
g=new ArrayList[n];
int i;
for(i=0;i<n;i++)
{
g[i]=new ArrayList<>();
String st[]=bu.readLine().split(" ");
a[i][0]=Integer.parseInt(st[0]); a[i][1]=Integer.parseInt(st[1]);
s[i][0]=s[i][1]=0;
}
for(i=0;i<n-1;i++)
{
String st[]=bu.readLine().split(" ");
int u=Integer.parseInt(st[0])-1,v=Integer.parseInt(st[1])-1;
g[u].add(v); g[v].add(u);
}
dfs(0,-1);
sb.append(Math.max(s[0][0],s[0][1])+"\n");
}
System.out.print(sb);
}
static ArrayList<Integer> g[];
static int N=100000,a[][]=new int[N][2];
static long s[][]=new long[N][2];
static void dfs(int n,int p)
{
for(int x:g[n])
if(x!=p)
{
dfs(x,n);
s[n][0]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][0]),s[x][1]+Math.abs(a[x][1]-a[n][0]));
s[n][1]+=Math.max(s[x][0]+Math.abs(a[x][0]-a[n][1]),s[x][1]+Math.abs(a[x][1]-a[n][1]));
}
}
}
| 1 |
45561f1f | cc9230d3 | /**
* Created by Himanshu
**/
import java.util.*;
import java.io.*;
import java.math.*;
public class C1529 {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
Reader s = new Reader();
int t = s.i();
while (t-- > 0) {
int n = s.i();
pairLong [] arr = new pairLong[n];
for (int i=0;i<n;i++) {
long x = s.l() , y = s.l();
arr[i] = new pairLong(x,y);
}
ArrayList<Integer>[] tree = new ArrayList[n+1];
for (int i=0;i<=n;i++) tree[i] = new ArrayList<>();
for (int i=0;i<n-1;i++) {
int x = s.i() , y = s.i();
tree[x].add(y);
tree[y].add(x);
}
pairLong[] dp = new pairLong[n+1];
boolean [] vis = new boolean[n+1];
pairLong x = value(tree,arr,dp,1,vis);
out.println(Math.max(x.first,x.second));
}
out.flush();
}
private static pairLong value(ArrayList<Integer>[] tree , pairLong [] arr , pairLong [] dp , int in , boolean [] vis) {
if (dp[in] != null) return dp[in];
vis[in] = true;
long a = 0L , b = 0L;
for (int x : tree[in]) {
if (!vis[x]) {
pairLong y = value(tree,arr,dp,x,vis);
a += Math.max(Math.abs(arr[in-1].first-arr[x-1].first) + y.first,Math.abs(arr[in-1].first-arr[x-1].second) + y.second);
b += Math.max(Math.abs(arr[in-1].second-arr[x-1].first) + y.first,Math.abs(arr[in-1].second-arr[x-1].second) + y.second);
}
}
dp[in] = new pairLong(a,b);
return dp[in];
}
private static void bfs(ArrayList<Integer>[] tree , ArrayList<Integer> even ,
ArrayList<Integer> odd , int n) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
int in = 0;
boolean [] vis = new boolean[n+1];
vis[1] = true;
while (!q.isEmpty()) {
Integer a = q.poll();
if (a == null) {
q.add(null);
in++;
} else {
if (in%2 == 0) even.add(a);
else odd.add(a);
for (int x : tree[a]) {
if (!vis[x]) {
q.add(x);
vis[x] = true;
}
}
}
if (q.size() == 0) break;
}
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
long temp = arr[i];
int randomPos = i + rand.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = temp;
}
}
private static int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b,a%b);
}
public static long nCr(long[] fact, long[] inv, int n, int r, long mod) {
if (n < r)
return 0;
return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod;
}
private static void factorials(long[] fact, long[] inv, long mod, int n) {
fact[0] = 1;
inv[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = (fact[i - 1] * i) % mod;
inv[i] = power(fact[i], mod - 2, mod);
}
}
private static long power(long a, long n, long p) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a) % p;
n /= 2;
} else {
result = (result * a) % p;
n--;
}
}
return result;
}
private static long power(long a, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a);
n /= 2;
} else {
result = (result * a);
n--;
}
}
return result;
}
private static long query(long[] tree, int in, int start, int end, int l, int r) {
if (start >= l && r >= end) return tree[in];
if (end < l || start > r) return 0;
int mid = (start + end) / 2;
long x = query(tree, 2 * in, start, mid, l, r);
long y = query(tree, 2 * in + 1, mid + 1, end, l, r);
return x + y;
}
private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) {
if (start == end) {
tree[in] = val;
arr[idx] = val;
return;
}
int mid = (start + end) / 2;
if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val);
else update(arr, tree, 2 * in, start, mid, idx, val);
tree[in] = tree[2 * in] + tree[2 * in + 1];
}
private static void build(int[] arr, long[] tree, int in, int start, int end) {
if (start == end) {
tree[in] = arr[start];
return;
}
int mid = (start + end) / 2;
build(arr, tree, 2 * in, start, mid);
build(arr, tree, 2 * in + 1, mid + 1, end);
tree[in] = (tree[2 * in + 1] + tree[2 * in]);
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String s() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long l() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int i() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double d() throws IOException {
return Double.parseDouble(s());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int[] arr(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = i();
}
return ret;
}
public long[] arrLong(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = l();
}
return ret;
}
}
static class pairLong implements Comparator<pairLong> {
long first, second;
pairLong() {
}
pairLong(long first, long second) {
this.first = first;
this.second = second;
}
@Override
public int compare(pairLong p1, pairLong p2) {
if (p1.first == p2.first) {
if(p1.second > p2.second) return 1;
else return -1;
}
if(p1.first > p2.first) return 1;
else return -1;
}
}
// static class pair implements Comparator<pair> {
// int first, second;
//
// pair() {
// }
//
// pair(int first, int second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pair p1, pair p2) {
// if (p1.first == p2.first) return p1.second - p2.second;
// return p1.first - p2.first;
// }
// }
}
| import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class C {
static int[][] lr = new int[2][(int)2e5+10];
static long[][] dp = new long[2][(int)2e5+10];
static ArrayList<ArrayList<Integer>> g;
public static void main(String[] args) {
FastReader fr = new FastReader();
PrintWriter out = new PrintWriter(System.out, true);
int cases = fr.nextInt();
for(int c = 0; c < cases; c++) {
int nodes = fr.nextInt();
g = new ArrayList<ArrayList<Integer>>();
for(int i = 1; i <= nodes; i++) {
lr[0][i] = fr.nextInt();
lr[1][i] = fr.nextInt();
}
for(int i = 0; i <= nodes; i++) {
g.add(new ArrayList<Integer>());
}
for(int i = 0; i < nodes-1; i++) {
int f = fr.nextInt();
int t = fr.nextInt();
g.get(f).add(t);
g.get(t).add(f);
}
DFS(1, -1);
out.write(Math.max(dp[0][1], dp[1][1]) + "\n");
}
out.close();
}
static void DFS(int v, int p) {
dp[0][v] = dp[1][v] = 0;
for(Integer u : g.get(v)) {
if (u == p) continue;
DFS(u, v);
dp[0][v] += Math.max(Math.abs(lr[0][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[0][v] - lr[0][u]));
dp[1][v] += Math.max(Math.abs(lr[1][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[1][v] - lr[0][u]));
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try{
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| 0 |
1dab88fb | 4138b081 | import java.util.*;
public class Main
{
static class Edge{
public int node;
public int index;
public Edge(int n, int i){
node=n;
index=i;
}
}
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int test=sc.nextInt();
while(test-->0){
solve();
}
}
static void solve(){
int n=sc.nextInt();
ArrayList<ArrayList<Edge>> graph= new ArrayList<ArrayList<Edge>>();
for(int i=0;i<n;i++){
graph.add(new ArrayList<>());
}
for (int i = 0; i < n - 1; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
u--; v--;
graph.get(u).add(new Edge(v, i));
graph.get(v).add(new Edge(u, i));
}
int start = 0;
for (int i = 0; i < n; i++) {
if (graph.get(i).size() > 2) {
System.out.println("-1");
return;
} else if (graph.get(i).size() == 1) {
start = i;
}
}
int[] weight = new int[n - 1];
int prevNode = -1;
int curNode = start;
int curWeight = 2;
while (true) {
ArrayList<Edge> edges = graph.get(curNode);
Edge next = edges.get(0);
if (next.node == prevNode) {
if (edges.size() == 1) {
break;
} else {
next = edges.get(1);
}
}
weight[next.index] = curWeight;
prevNode = curNode;
curNode = next.node;
curWeight = 5 - curWeight;
}
for (int i = 0; i < n - 1; i++) {
System.out.print(weight[i]);
System.out.print(" ");
}
System.out.println();
}
}
| import java.io.*;
import java.util.*;
public class Contest1627C
{
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static long mod = 1000000007; static ArrayList<Integer>[] adj; static ArrayList<Integer>[] num; static int[] ans;
public static void main(String[] args)
{
int t = r.nextInt();
while (t > 0)
{
t--;
int n = r.nextInt();
adj = new ArrayList[n];
num = new ArrayList[n];
for (int i = 0; i < n; i ++)
{
adj[i] = new ArrayList<Integer>();
num[i] = new ArrayList<Integer>();
}
int[] deg = new int[n];
boolean flag = false;
for (int i = 0; i < n - 1; i ++)
{
int a = r.nextInt()-1;
int b = r.nextInt()-1;
adj[a].add(b);
adj[b].add(a);
num[a].add(i);
num[b].add(i);
deg[a] ++;
deg[b] ++;
if (deg[a] > 2 || deg[b] > 2)
{
flag = true;
}
}
if (flag)
{
pw.println(-1);
continue;
}
ans = new int[n];
for (int i = 0; i < n; i ++)
{
if (deg[i] == 1)
{
dfs(i,3,-1);
}
}
for (int i = 0; i < n - 1; i ++)
{
pw.println(ans[i]);
}
}
pw.close();
}
static void dfs(int node, int x, int p)
{
for (int j = 0; j < adj[node].size(); j ++)
{
int i = adj[node].get(j);
if (i == p)
{
continue;
}
ans[num[node].get(j)] = x;
dfs(i,5-x,node);
}
}
} | 0 |
595f5d6c | 6653a758 | import java.math.BigInteger;
//import static java.lang.Math.max;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.Scanner;
public class ahh {
//trihund
static Scanner scn = new Scanner(System.in);
static boolean vis[][];
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader s = new FastReader();
static int MOD = 1000000007;
public static void main(String[] args) {
int n=scn.nextInt(),count=0;
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=scn.nextInt();
}
ArrayList<Integer>zer=new ArrayList<Integer>(),one=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
if(arr[i]==0)
zer.add(i);
else
one.add(i);
}
count=one.size();
long memo[][]=new long[one.size()+1][zer.size()+1];
for(int i=0;i<=one.size();i++)
{
for(int j=0;j<=zer.size();j++)
memo[i][j]=-1;
}
System.out.println(arm(one, zer, 0, 0, count,memo));
}
public static long arm(ArrayList<Integer>one,ArrayList<Integer>zer,int i,int j,int count,long memo[][])
{ if(count==0)
return 0;
if(i==one.size()||j==zer.size())
return Integer.MAX_VALUE;
if(memo[i][j]!=-1)
return memo[i][j];
long a=Integer.MAX_VALUE,b=Integer.MAX_VALUE;
a=arm(one, zer, i+1, j+1,count-1,memo)+Math.abs(one.get(i)-zer.get(j));
b=arm(one, zer, i, j+1,count,memo);
memo[i][j]=Math.min(a, b);
return Math.min(a, b);
}
public static void fac(int n) {
BigInteger b = new BigInteger("1");
for (int i = 1; i <= n; i++) {
b = b.multiply(BigInteger.valueOf(i));
}
System.out.println(b);
}
static void ruffleSort(long[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
}
class pm {
int ini, fin;
pm(int a, int b) {
this.ini = a;
this.fin = b;
}
}
class surt implements Comparator<pm> {
@Override
public int compare(pm o1, pm o2) {
// TODO Auto-generated method stub
int a = o1.ini - o2.ini, b = o1.fin - o2.fin;
if (a < 0)
return -1;
if (a == 0) {
if (b < 0)
return -1;
else
return 1;
} else
return 1;
}
}
class pair {
int x, y;
pair(int a, int b) {
this.x = a;
this.y = b;
}
public int hashCode() {
return x * 31 + y * 31;
}
public boolean equals(Object other) {
if (this == other)
return true;
if (other instanceof pair) {
pair pt = (pair) other;
return pt.x == this.x && pt.y == this.y;
} else
return false;
}
}
class sort implements Comparator<pair> {
@Override
public int compare(pair o1, pair o2) {
// TODO Auto-generated method stub
long a = o1.x - o2.x, b = o1.y - o2.y;
if (b < 0)
return -1;
else if (a == 0) {
if (a < 0)
return -1;
else
return 1;
} else
return 1;
}
} | import java.util.*;
public class D {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>();
int n = sc.nextInt(),dp[][]=new int[n+1][n+1];
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++)
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
System.out.println(dp[o.size()][e.size()]);
}
} | 0 |
0fd5b95a | 41e72d4f | //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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
public class ProblemD {
public static void main(String[] args) throws IOException {
final int INF = 20000000;
InputStream in = System.in;
InputReader scan = new InputReader(in);
int n = scan.nextInt();
int occ = 0;
List<Integer> occPos = new ArrayList<>();
HashSet<Integer> occPosSet = new HashSet<>();
for(int i=1;i<=n;i++) {
int num = scan.nextInt();
if(num==1) {
occ++;
occPos.add(i);
occPosSet.add(i);
}
}
int[][] dp = new int[n+1][occ+1];
for(int i=0;i<=n;i++) {
for(int j=0;j<=occ;j++) {
dp[i][j] = 20000000;
}
}
for(int i=1;i<=n;i++) {
int k=1;
for(int pos: occPos) {
if(occPosSet.contains(i)) {
dp[i][k] = dp[i-1][k];
} else {
dp[i][k] = Math.min(dp[i-1][k], dp[i-1][k-1]+Math.abs(pos-i));
if(k==1) dp[i][k]=Math.min(dp[i][k],Math.abs(pos-i));
}
k++;
}
}
if(dp[n][occ]==INF) {
System.out.println(0);
} else {
System.out.println(dp[n][occ]);
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
InputReader(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| 0 |
9069684f | bf85ab7b | import java.io.*;
import java.util.*;
public class Test {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static boolean[] vis;
static ArrayList<Integer>[] adj;
static int[] l, r;
static long[][] dp;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
int T = readInt();
for (int t = 0; t < T; t++) {
int n = readInt();
l = new int[n + 1];
r = new int[n + 1];
for (int i = 1; i <= n; i++) {
l[i] = readInt();
r[i] = readInt();
}
adj = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
int u = readInt(), v = readInt();
adj[u].add(v);
adj[v].add(u);
}
dp = new long[n + 1][2];
vis = new boolean[n + 1];
vis[1] = true;
dfs(1);
System.out.println(Math.max(dp[1][0], dp[1][1]));
}
}
static void dfs(int u) {
for (int x : adj[u]) {
if (!vis[x]) {
vis[x] = true;
dfs(x);
dp[u][0] += Math.max(dp[x][0] + Math.abs(l[u] - l[x]), dp[x][1] + Math.abs(l[u] - r[x]));
dp[u][1] += Math.max(dp[x][0] + Math.abs(r[u] - l[x]), dp[x][1] + Math.abs(r[u] - r[x]));
}
}
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
}
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class ParsasHumongousTree {
public static void main(String args[]) throws IOException {
Reader scan = new Reader();
StringBuilder sb = new StringBuilder();
int t = scan.nextInt();
while (t-- > 0) {
int n = scan.nextInt();
int[] l = new int[n + 1];
int[] r = new int[n + 1];
for (int i = 1; i <= n; i++) {
l[i] = scan.nextInt();
r[i] = scan.nextInt();
}
Graph g = new Graph(n);
for (int i = 0; i < n - 1; i++) {
g.addEdge(scan.nextInt(), scan.nextInt());
}
sb.append(g.dfs(l, r) + "\n");
}
System.out.println(sb);
}
}
class Graph {
ArrayList<Integer>[] node;
int n;
int c = 0;
boolean[] vis;
Graph(int s) {
n = s + 1;
vis = new boolean[n + 1];
node = new ArrayList[n + 1];
for (int i = 0; i < n + 1; i++) {
node[i] = new ArrayList<>();
}
}
void addEdge(int u, int v) {
node[u].add(v);
node[v].add(u);
if (node[u].size() == 1) {
c = u;
}
if (node[v].size() == 1) {
c = v;
}
}
void cleanVisArray() {
for (int i = 0; i < n + 1; i++) {
vis[i] = false;
}
}
long dfs(int[] l, int[] r) {
cleanVisArray();
long[][] dp = new long[n][2];
dfsMain(1, dp, l, r);
return Math.max(dp[1][0], dp[1][1]);
}
void dfsMain(int v, long[][] dp, int[] l, int[] r) {
vis[v] = true;
for (int i : node[v]) {
if (!vis[i]) {
dfsMain(i, dp, l, r);
dp[v][0] += Math.max(Math.abs(l[v] - l[i]) + dp[i][0], Math.abs(l[v] - r[i]) + dp[i][1]);
dp[v][1] += Math.max(Math.abs(r[v] - l[i]) + dp[i][0], Math.abs(r[v] - r[i]) + dp[i][1]);
}
}
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null) {
return;
}
din.close();
}
}
| 0 |
169e34bf | c9159d9c | import java.util.*;
public class D{
static Scanner sc;
public static void solve(){
int n=sc.nextInt();
Integer a[]=new Integer[n];
int flag;
for(int i=0;i<n;i++) a[i]=sc.nextInt();
String s=sc.next();
ArrayList<Integer> x=new ArrayList<>();
ArrayList<Integer> y=new ArrayList<>();
for(int i=0;i<n;i++){
if(s.charAt(i)=='B') x.add(a[i]);
else y.add(a[i]);
}
Collections.sort(x);
Collections.sort(y);
int p=n;
int q=1;
for(int i=y.size()-1;i>=0;i--){
if(y.get(i)>p){System.out.println("NO"); return;}
p-=1;
}
for(int i=0;i<x.size();i++){
if(x.get(i)<q){System.out.println("NO"); return;}
q+=1;
}
System.out.println("YES");
}
public static void main(String args[]){
sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) solve();
}
}
| import java.util.*;
public class SolutionB {
public static long gcd(long a, long b){
if(b==0){
return a;
}
return gcd(b, a%b);
}
public static long gcdSum(long b){
long a = 0;
long temp = b;
while(temp!=0){
a = a + temp%10;
temp = temp/10;
}
return gcd(a,b);
}
public static class Pair{
Long a;
Long b;
public Pair(Long a, Long b) {
this.a = a;
this.b = b;
}
}
public static long factorial (long n){
if(n==0)
return 1;
else if(n==1)
return n;
return n * factorial(n-1);
}
public static long lcm (long n){
if(n<3)
return n;
return lcmForBig(n,n-1);
}
private static long lcmForBig(long n, long l) {
if(l==1)
return n;
n = (n * l) / gcd(n, l);
return lcmForBig(n, l-1);
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i =0;i<t;i++) {
int n = s.nextInt();
int arr [] = new int[n];
ArrayList<Integer> blue = new ArrayList<>();
ArrayList<Integer> red = new ArrayList<>();
for(int j=0;j<n;j++){
int num = s.nextInt();
arr[j]=num;
}
String color = s.next();
for(int j=0;j<n;j++){
if(color.charAt(j)=='B'){
blue.add(arr[j]);
}
else{
red.add(arr[j]);
}
}
Collections.sort(blue);
String ans = "YES";
int counter = 0;
for(int j=0;j<blue.size();j++){
int current = blue.get(j);
if (current<1){
ans="NO";
break;
}
if(current>counter){
counter++;
}
else{
ans="NO";
break;
}
}
if(ans=="NO"){
System.out.println(ans);
}
else{
int tempCounter = n+1;
Collections.sort(red);
for(int j=red.size()-1;j>=0;j--){
int current = red.get(j);
if(current>=tempCounter){
ans="NO";
break;
}
else{
tempCounter--;
}
}
if(tempCounter-counter!=1)
System.out.println("NO");
else
System.out.println(ans);
}
}
return;
}
}
| 0 |
5288afb7 | df594a00 | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
bu.readLine();
String s[]=bu.readLine().split(" ");
int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]);
int a[]=new int[n],i,x,ac[]=new int[n],b[]=new int[k];
Arrays.fill(a,Integer.MAX_VALUE);
s=bu.readLine().split(" ");
for(i=0;i<k;i++) b[i]=Integer.parseInt(s[i])-1;
s=bu.readLine().split(" ");
for(i=0;i<k;i++)
{
x=Integer.parseInt(s[i]);
ac[b[i]]=x;
}
PriorityQueue<Integer> pq=new PriorityQueue<>();
for(i=0;i<n;i++)
{
if(ac[i]!=0) pq.add(ac[i]-i);
if(!pq.isEmpty()) a[i]=Math.min(a[i],pq.peek()+i);
}
pq=new PriorityQueue<>();
for(i=n-1;i>=0;i--)
{
if(ac[i]!=0) pq.add(ac[i]+i);
if(!pq.isEmpty()) a[i]=Math.min(a[i],pq.peek()-i);
}
for(i=0;i<n;i++) sb.append(a[i]+" ");
sb.append("\n");
}
System.out.print(sb);
}
}
| import java.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
while(cases-- > 0) {
br.readLine();
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int[] a = new int[k];
int[] t = new int[k];
str = br.readLine().split(" ");
for(int i=0; i<k; i++) {
a[i] = Integer.parseInt(str[i]) - 1;
}
str = br.readLine().split(" ");
for(int i=0; i<k; i++) {
t[i] = Integer.parseInt(str[i]);
}
int[] temp = new int[n];
Arrays.fill(temp, Integer.MAX_VALUE);
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, Integer.MAX_VALUE);
Arrays.fill(right, Integer.MAX_VALUE);
int ind = 0;
for(int i=0; i<k; i++) {
left[a[i]] = t[i];
right[a[i]] = t[i];
}
int minleft = Integer.MAX_VALUE;
for(int i=0; i<n; i++) {
left[i] = Math.min(left[i], minleft);
minleft = left[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : left[i]+1;
}
int minright = Integer.MAX_VALUE;
for(int i=n-1; i>=0; i--) {
right[i] = Math.min(right[i], minright);
minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1;
}
for(int i=0; i<n; i++) {
temp[i] = Math.min(right[i], left[i]);
System.out.print(temp[i]+" ");
}
System.out.println();
}
}
} | 0 |
c4ca2ff3 | ccc8ef27 | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
PrintWriter out=new PrintWriter(System.out);
while(t-->0) {
int n=sc.nextInt();
int freq[][]=new int[n][5];
int rem[][]=new int[n][5];
for(int i=0;i<n;i++) {
String str=sc.next();
for(int j=0;j<str.length();j++) {
freq[i][str.charAt(j)-'a']++;
}
for(int k=0;k<5;k++) {
rem[i][k]=str.length()-freq[i][k];
}
}
int ans=0;
for(int i=0;i<5;i++) {
int arr[]=new int[n];
for(int j=0;j<n;j++)
arr[j]=freq[j][i]-rem[j][i];
Arrays.sort(arr);
int total=0;
int sum=0;
for(int k=n-1;k>=0;k--) {
if(sum+arr[k]>0) {
sum=sum+arr[k];
total++;
}
else {
break;
}
}
ans=Math.max(ans,total);
}
out.println(ans);
}
out.flush();
out.close();
}
}
| import java.util.*;
public class Sol
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int a[][]=new int[n][5];
int tot[]=new int[n];
for(int i=0;i<n;i++)
{
String x = sc.next();
for(int j=0;j<x.length();j++)
a[i][x.charAt(j)-'a'] += 1;
tot[i]=x.length();
}
int max=Integer.MIN_VALUE;
for(int i=0;i<5;i++)
max=Math.max(max,function(a,n,i,tot));
System.out.println(max);
}
}
static int function(int a[][],int n,int i,int tot[])
{
Integer ans[] = new Integer[n];
for(int j=0;j<n;j++)
ans[j]=a[j][i]-(tot[j]-a[j][i]);
int res=0,j=0;
Arrays.sort(ans,Collections.reverseOrder());
while(j<n&&res+ans[j]>0)
res+=ans[j++];
return j;
}
} | 0 |
a5d5a95f | c4ca2ff3 | import java.util.*;
import java.io.*;
public class Main {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{ try {br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);}
catch(Exception e) { br = new BufferedReader(new InputStreamReader(System.in));}
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {st = new StringTokenizer(br.readLine());}
catch (IOException e) {
e.printStackTrace();}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() {return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// end of fast i/o code
public static void main(String[] args) {
FastReader reader = new FastReader();
int Q = reader.nextInt();
outer: for (int q = 0; q < Q; q++) {
int N = reader.nextInt();
int[][] scores = new int[5][N];
for (int i = 0; i < N; i++) {
int[] occurs = new int[5];
String word = reader.next();
for (int j = 0; j < word.length(); j++) {
occurs[word.charAt(j) - 'a']++;
}
for (int j = 0; j < 5; j++) {
scores[j][i] = occurs[j] - (word.length() - occurs[j]) ;
}
}
int bestCount = 0;
for (int i = 0; i < 5; i++) {
int[] curr = scores[i];
Arrays.sort(curr);
int currentCount = 1;
int currentScore = curr[curr.length - 1];
for (int j = curr.length - 2; j >= 0 && currentScore > 0; j--) {
currentScore += curr[j];
currentCount++;
}
if (currentScore <= 0) currentCount--;
bestCount = Math.max(currentCount, bestCount);
}
System.out.println(bestCount);
}
}
}
| /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
PrintWriter out=new PrintWriter(System.out);
while(t-->0) {
int n=sc.nextInt();
int freq[][]=new int[n][5];
int rem[][]=new int[n][5];
for(int i=0;i<n;i++) {
String str=sc.next();
for(int j=0;j<str.length();j++) {
freq[i][str.charAt(j)-'a']++;
}
for(int k=0;k<5;k++) {
rem[i][k]=str.length()-freq[i][k];
}
}
int ans=0;
for(int i=0;i<5;i++) {
int arr[]=new int[n];
for(int j=0;j<n;j++)
arr[j]=freq[j][i]-rem[j][i];
Arrays.sort(arr);
int total=0;
int sum=0;
for(int k=n-1;k>=0;k--) {
if(sum+arr[k]>0) {
sum=sum+arr[k];
total++;
}
else {
break;
}
}
ans=Math.max(ans,total);
}
out.println(ans);
}
out.flush();
out.close();
}
}
| 0 |
1162c08f | 6bcc5afd | import java.util.*;
public class CodeForces1525C{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>();
int n = sc.nextInt(),dp[][]=new int[n+1][n+1];
for(int i=1;i<=n;i++){
int x=sc.nextInt();
if(x==1)o.add(i);
else e.add(i);
}
for(int i=1;i<=o.size();i++){
dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1));
for(int j=i+1;j<=e.size();j++)
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1)));
}
System.out.println(dp[o.size()][e.size()]);
}
}
| import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n];
ArrayList<Integer> lt1=new ArrayList<>();
ArrayList<Integer> lt0=new ArrayList<>();
for(int i=0;i<n;i++)
{
int l=s.nextInt();
if(l==0)
lt0.add(i+1);
else
lt1.add(i+1);
}
int dp[][]=new int[lt1.size()+1][lt0.size()+1];
for(int i=1;i<=lt1.size();i++)
{
dp[i][i]=dp[i-1][i-1]+Math.abs(lt0.get(i-1)-lt1.get(i-1));
for(int j=i+1;j<=lt0.size();j++)
{
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(lt1.get(i-1)-lt0.get(j-1)));
}
}
System.out.println(dp[lt1.size()][lt0.size()]);
}
} | 1 |
116765b1 | 38601291 | import java.util.*;
import java.io.*;
public class Codeforces {
static int n;
static int[] h = new int[200001];
static boolean check(int x) {
int[] cur_h = new int[n];
for(int i = 0; i < n; i++) cur_h[i] = h[i];
for(int i = n-1; i >= 2; i--) {
if(cur_h[i] < x) return false;
int d = Math.min(h[i], cur_h[i]-x)/3;
cur_h[i-1] += d;
cur_h[i-2] += 2*d;
}
return cur_h[0] >= x && cur_h[1] >= x;
}
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) {
n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; i++) {
h[i] = Integer.parseInt(st.nextToken());
max = Math.max(max, h[i]);
}
int l = 0;
int r = max;
while(l < r) {
int mid = l + (r-l+1)/2;
if(check(mid)) l = mid;
else r = mid-1;
}
writer.println(l);
}
writer.close();
br.close();
}
}
| import java.io.*;
import java.util.*;
public class stones {
public static void main (String[] args) throws IOException {
// set up
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("test.in")));
PrintWriter out = new PrintWriter(System.out);
int T = Integer.parseInt(input.readLine());
for (int i=0;i<T;i++) {
// read in test case
int n = Integer.parseInt(input.readLine());
int[] seq = new int[n];
StringTokenizer st = new StringTokenizer(input.readLine());
int low = 0;
int high = 0;
for (int j=0;j<n;j++) {
seq[j] = Integer.parseInt(st.nextToken());
high = Math.max(high, seq[j]);
}
// binary search on answer
while (low<high) {
int mid = (low+high+1)/2;
//System.out.println(possible(seq, mid));
if (possible(seq, mid)) {
low = mid; }
else high = mid-1; }
out.println(low);
}
out.close();
}
static boolean possible(int[] seq, int goal) {
/* checks if array can be made to have smallest value
greater than or equal to given goal value */
//System.out.println(goal);
int L = seq.length;
int[] arr = Arrays.copyOf(seq, L);
//System.out.println(Arrays.toString(arr));
for (int i=L-1;i>=2;i--) {
//System.out.println(Arrays.toString(arr));
if (arr[i] < goal) return false;
int max_d = Math.min((arr[i] - goal)/3, seq[i]/3);
//System.out.println(arr[i]);
//System.out.println(max_d);
arr[i-1] += max_d;
arr[i-2] += max_d*2;
arr[i] -= max_d*3;
}
//System.out.println(Arrays.toString(arr));
for (int num: arr) {
if (num < goal) return false; }
return true;
}
}
| 1 |
b7de5c19 | c022c315 | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
ArrayList<ArrayList<Pair>> graph = new ArrayList<>();
for (int j = 0; j < n; j++) {
graph.add(new ArrayList<>());
}
for (int j = 0; j < n - 1; j++) {
int u;
int v;
u = scan.nextInt();
v = scan.nextInt();
u--;
v--;
graph.get(u).add(new Pair(v, j));
graph.get(v).add(new Pair(u, j));
}
boolean soluble = true;
int curV = 0;
int prevV = -1;
int[] ans = new int[n];
int prime = 2;
for (int j = 0; j < n; j++) {
ArrayList<Pair> list = graph.get(j);
if (list.size() > 2) {
soluble = false;
} else if (list.size() == 1) {
curV = j;
}
}
if (soluble) {
for (int j = 0; j < n - 1; j++) {
ArrayList<Pair> list = graph.get(curV);
for (int z = 0; z < list.size(); z++) {
if (list.get(z).vertex != prevV) {
ans[list.get(z).numberOfEdge] = prime;
prime = changePrime(prime);
prevV = curV;
curV = list.get(z).vertex;
break;
}
}
}
for (int j = 0; j < n - 1; j++) {
System.out.print(ans[j] + " ");
}
System.out.println();
} else {
System.out.println(-1);
}
}
}
public static int changePrime(int prime) {
if (prime == 2) {
prime = 3;
} else {
prime = 2;
}
return prime;
}
}
class Pair {
int vertex;
int numberOfEdge;
public Pair(int vertex, int numberOfEdge) {
this.vertex = vertex;
this.numberOfEdge = numberOfEdge;
}
}
| import java.io.*;
import java.util.*;
public class Main {
// static boolean[] prime = new boolean[10000000];
final static long mod = 1000000007;
public static void main(String[] args) {
// sieve();
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int[][] a = new int[n - 1][2];
ArrayList<ArrayList<Data>> g = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
g.add(new ArrayList<>());
a[i][0] = in.nextInt() - 1;
a[i][1] = in.nextInt() - 1;
}
g.add(new ArrayList<>());
for (int i = 0; i < n - 1; i++) {
g.get(a[i][0]).add(new Data(a[i][1], i));
g.get(a[i][1]).add(new Data(a[i][0], i));
}
if (!check(g)) {
System.out.println(-1);
continue;
}
int[] ans = new int[n - 1];
ans[0] = 2;
for(int node: a[0]) {
for(Data d: g.get(node)) {
solve(g, d.a, d.ind, false, ans);
}
}
for (int i : ans)
System.out.print(i + " ");
System.out.println();
}
out.flush();
}
private static void solve(ArrayList<ArrayList<Data>> g, int node, int edge, boolean b, int[] ans) {
if (ans[edge] != 0)
return;
ans[edge] = b ? 2 : 3;
for (Data d : g.get(node)) {
solve(g, d.a, d.ind, !b, ans);
}
}
private static boolean check(ArrayList<ArrayList<Data>> g) {
for (ArrayList<Data> a : g)
if (a.size() > 2)
return false;
return true;
}
static boolean prime(long k) {
for (int i = 2; i * i <= k; i++) {
if (k % i == 0) {
return false;
}
}
return true;
}
static long gcd(long a, long b) {
if (a % b == 0) {
return b;
} else {
return gcd(b, a % b);
}
}
static void reverseArray(Integer[] a) {
for (int i = 0; i < (a.length >> 1); i++) {
Integer temp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = temp;
}
}
static Integer[] intInput(int n, InputReader in) {
Integer[] a = new Integer[n];
for (int i = 0; i < a.length; i++)
a[i] = in.nextInt();
return a;
}
static Long[] longInput(int n, InputReader in) {
Long[] a = new Long[n];
for (int i = 0; i < a.length; i++)
a[i] = in.nextLong();
return a;
}
static String[] strInput(int n, InputReader in) {
String[] a = new String[n];
for (int i = 0; i < a.length; i++)
a[i] = in.next();
return a;
}
// static void sieve() {
// for (int i = 2; i * i < prime.length; i++) {
// if (prime[i])
// continue;
// for (int j = i * i; j < prime.length; j += i) {
// prime[j] = true;
// }
// }
// }
}
class Data {
int a;
int ind;
Data(int val, int ind) {
this.a = Math.abs(val);
this.ind = ind;
}
}
class compareVal implements Comparator<Data> {
@Override
public int compare(Data o1, Data o2) {
// return (o1.val - o2.val == 0 ? o1.ind - o2.ind : o1.val - o2.val);
return (o1.a - o2.a);
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | 0 |
921b6e4a | e7dce35b | import java.io.*;
import java.util.*;
public class D_Java {
public static final int MOD = 998244353;
public static int mul(int a, int b) {
return (int)((long)a * (long)b % MOD);
}
int[] f;
int[] rf;
public int C(int n, int k) {
return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k]));
}
public static int pow(int a, int n) {
int res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
static void shuffleArray(int[] a) {
Random rnd = new Random();
for (int i = a.length-1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
int tmp = a[index];
a[index] = a[i];
a[i] = tmp;
}
}
public static int inv(int a) {
return pow(a, MOD-2);
}
public void doIt() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer(in.readLine());
int n = Integer.parseInt(tok.nextToken());
int k = Integer.parseInt(tok.nextToken());
f = new int[n+42];
rf = new int[n+42];
f[0] = rf[0] = 1;
for (int i = 1; i < f.length; ++i) {
f[i] = mul(f[i-1], i);
rf[i] = mul(rf[i-1], inv(i));
}
int[] events = new int[2*n];
for (int i = 0; i < n; ++i) {
tok = new StringTokenizer(in.readLine());
int le = Integer.parseInt(tok.nextToken());
int ri = Integer.parseInt(tok.nextToken());
events[i] = le*2;
events[i + n] = ri*2 + 1;
}
shuffleArray(events);
Arrays.sort(events);
int ans = 0;
int balance = 0;
for (int r = 0; r < 2*n;) {
int l = r;
while (r < 2*n && events[l] == events[r]) {
++r;
}
int added = r - l;
if (events[l] % 2 == 0) {
// Open event
ans += C(balance + added, k);
if (ans >= MOD) ans -= MOD;
ans += MOD - C(balance, k);
if (ans >= MOD) ans -= MOD;
balance += added;
} else {
// Close event
balance -= added;
}
}
in.close();
System.out.println(ans);
}
public static void main(String[] args) throws IOException {
(new D_Java()).doIt();
}
} | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = nextInt();
int k = nextInt();
f = new int[n + 42];
rf = new int[n + 42];
f[0] = 1;
rf[0] = 1;
for (int i = 1; i < f.length; i++) {
f[i] = mul(f[i - 1], i);
rf[i] = mul(rf[i - 1], inv(i));
}
int[] a = new int[n * 2];
for (int i = 0; i < n; i++) {
a[i] = nextInt() * 2;
a[i + n] = nextInt() * 2 + 1;
}
Arrays.sort(a);
int ans = 0;
int curOpen = 0;
for (int r = 0; r < 2 * n;) {
int l = r;
while (r < 2 * n && a[l] == a[r]) r++;
int intersections = r - l;
if (a[l] % 2 == 0) {
ans += C(curOpen + intersections, k);
if (ans >= mod) ans -= mod;
ans += mod - C(curOpen, k);
if (ans >= mod) ans -= mod;
curOpen += intersections;
} else {
curOpen -= intersections;
}
}
pw.println(ans);
pw.close();
}
static int mod = 998244353;
static int mul(int a, int b) {
return (int) ((long) a * (long) b % mod);
}
static int[] f;
static int[] rf;
static int C(int n, int k) {
return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n - k], rf[k]));
}
static int pow(int a, int n) {
int res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
static int inv(int a) {
return pow(a, mod - 2);
}
static StringTokenizer st = new StringTokenizer("");
static BufferedReader br;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class Coordinates {
int l;
int r;
public Coordinates(int l, int r) {
this.l = l;
this.r = r;
}
}
class CoordinatesComparator implements Comparator<Coordinates> {
@Override
public int compare(Coordinates o1, Coordinates o2) {
if (o1.l == o2.l) return Integer.compare(o1.r, o2.r);
return Integer.compare(o1.l, o2.l);
}
} | 1 |
14b0fb8e | d221162a | import java.io.*;
import java.util.*;
public class Solution {
static long res;
public static void main(String[] args) throws Exception
{
FastReader fr=new FastReader();
int n=fr.nextInt();
ArrayList<Integer> oc=new ArrayList<>();
ArrayList<Integer> em=new ArrayList<>();
res=Long.MAX_VALUE;
for(int i=0;i<n;i++) {
int v=fr.nextInt();
if(v==1)
oc.add(i);
else
em.add(i);
}
Collections.sort(oc);
Collections.sort(em);
long dp[][]=new long[5001][5001];
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[i].length;j++) {
dp[i][j]=-1;
}
}
System.out.println(getMin(oc,em,0,0,dp));
}
public static long getMin(ArrayList<Integer> oc,ArrayList<Integer> em,int idx,int j,long dp[][]) {
if(idx==oc.size())
return 0;
long available=em.size()-j;
long req=oc.size()-idx;
if(available<req)
return Integer.MAX_VALUE;
if(dp[idx][j]!=-1)
return dp[idx][j];
long ch1=getMin(oc,em,idx,j+1,dp);
long ch2=getMin(oc,em,idx+1,j+1,dp)+Math.abs(em.get(j)-oc.get(idx));
return dp[idx][j]=Math.min(ch1,ch2);
}
public static String lcs(String a,String b) {
int dp[][]=new int[a.length()+1][b.length()+1];
for(int i=0;i<=a.length();i++)
{
for(int j=0;j<=b.length();j++)
{
if(i==0||j==0)
dp[i][j]=0;
}
}
for(int i=1;i<=a.length();i++)
{
for(int j=1;j<=b.length();j++)
{
if(a.charAt(i-1)==b.charAt(j-1))
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
}
}
int i=a.length();
int j=b.length();
String lcs="";
while(i>0&&j>0)
{
if(a.charAt(i-1)==b.charAt(j-1)) {
lcs=a.charAt(i-1)+lcs;
i--;
j--;
}
else
{
if(dp[i-1][j]>dp[i][j-1])
i--;
else
j--;
}
}
return lcs;
}
public static long facto(long n) {
if(n==1||n==0)
return 1;
return n*facto(n-1);
}
public static long gcd(long n1,long n2) {
if (n2 == 0) {
return n1;
}
return gcd(n2, n1 % n2);
}
public static boolean isPali(String s) {
int i=0;
int j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j))
return false;
i++;
j--;
}
return true;
}
public static String reverse(String s) {
String res="";
for(int i=0;i<s.length();i++) {
res+=s.charAt(i);
}
return res;
}
public static int bsearch(long suf[],long val) {
int i=0;
int j=suf.length-1;
while(i<=j) {
int mid=(i+j)/2;
if(suf[mid]==val)
return mid;
else if(suf[mid]<val)
j=mid-1;
else
i=mid+1;
}
return -1;
}
public static int[] getFreq(String s) {
int a[]=new int[26];
for(int i=0;i<s.length();i++) {
a[s.charAt(i)-'a']++;
}
return a;
}
public static boolean isPrime(int n) {
for(int i=2;(i*i)<=n;i++) {
if(n%i==0)
return false;
}
return true;
}
}
class Pair{
long i;
long j;
Pair(long num,long freq){
this.i=num;
this.j=freq;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
| import java.util.*;
import java.io.*;
public class Main2 {
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}
catch (IOException e){e.printStackTrace();}}return st.nextToken();}
int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}
String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; }
}
static long mod = 998244353;
// static Scanner sc = new Scanner(System.in);
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args) {
int t = 1;
// t = sc.nextInt();
z : while(t-->0) {
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = sc.nextInt();
List<Integer> a1 = new ArrayList<>();
ArrayList<Integer> a2 = new ArrayList<>();
for (int i = 0; i < n; i++) {
if(a[i] == 0) a1.add(i);
else a2.add(i);
}
long dp[][] = new long[n+1][n+1];
for (int i = 0; i <= n; i++) {
Arrays.fill(dp[i],-1);
}
out.write(find(0,0,a1,a2,dp)+"\n");
}
out.close();
}
private static long find(int i, int j, List<Integer> a1, ArrayList<Integer> a2, long[][] dp) {
if(j == a2.size()) return 0;
int req = a2.size()-j;
int ava = a1.size()-i;
if(ava<req) return Integer.MAX_VALUE/2;
if(dp[i][j] != -1) return dp[i][j];
long ans1 = find(i+1,j,a1,a2,dp);
long ans2 = Math.abs(a1.get(i)-a2.get(j)) + find(i+1,j+1,a1,a2,dp);
return dp[i][j] = Math.min(ans1, ans2);
}
}
| 1 |
1c4d348d | 565f77b7 | /* 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 Solution {
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class SortbyHeight implements Comparator<Struct>
{
public int compare(Struct a, Struct b)
{
return a.h - b.h;
}
}
static class Struct{
int h,ind;
Struct(int h,int ind){
this.h=h;
this.ind=ind;
}
}
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
FastReader scn = new FastReader();
int t, k, i, j, l, f, max=0;
t=scn.nextInt();
while(t-->0){
int n=scn.nextInt();
int m=scn.nextInt();
int x=scn.nextInt();
Struct a[]=new Struct[n];
for (i=0;i<n;i++){
a[i]=new Struct(scn.nextInt(),i);
}
Arrays.sort(a,new SortbyHeight());
int b[]=new int[n];
int ms=1;
for (i=0;i<n;i++){
if (ms>m){
ms=1;
}
b[a[i].ind]=ms;
ms++;
}
System.out.println("YES");
for (i=0;i<n;i++){
System.out.print(b[i]+" ");
}
System.out.println();
}
}
}
| import java.util.*;
import java.io.*;
public class Solution {
static Scanner scn = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
public static void main(String[] HastaLaVistaLa) {
int t = scn.nextInt();
while(t-- > 0) solve();
out.println(sb);
out.close();
}
public static void solve() {
// Road To Specialist Day 3
int n = scn.nextInt(), m = scn.nextInt(), x = scn.nextInt();
int[] a = new int[n], ans = new int[n];
for(int i = 0; i < n; i++) a[i] = scn.nextInt();
PriorityQueue<Pair> pq = new PriorityQueue<>();
for(int i = 0; i < m; i++) pq.add(new Pair(0L, i));
for(int i = 0; i < n; i++) {
int e = a[i];
Pair p = pq.poll();
p.value += e;
pq.add(p);
ans[i] = p.id + 1;
}
boolean check = false;
long prev = pq.poll().value;
while(!pq.isEmpty()) {
long cur = pq.poll().value;
if(Math.abs(cur - prev) > x) check = true;
prev = cur;
}
if(check) sb.append("NO");
else {
sb.append("YES\n");
for(int i : ans) sb.append(i + " ");
}
sb.append("\n");
}
static class Pair implements Comparable<Pair> {
int id;
long value;
public Pair(long value, int id) {
this.id = id;
this.value = value;
}
public int compareTo(Pair o) {
return Long.compare(value, o.value);
}
}
} | 0 |
bf0df1d5 | d1cd194e | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.io.IOException;
public class C_Phoenix_and_Towers {
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class Pair implements Comparable<Pair> {
int id, h;
public Pair(int id, int h) {
this.id = id;
this.h = h;
}
public int compareTo(Pair o) {
return this.h - o.h;
}
}
public static void main(String[] args) throws java.lang.Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int x = sc.nextInt();
int tow[] = new int[n];
int ans[] = new int[n];
PriorityQueue<Pair> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
tow[i] = sc.nextInt();
}
for (int i = 0; i < m; i++) {
ans[i] = i + 1;
pq.add(new Pair(i + 1, tow[i]));
}
for (int i = m; i < n; i++) {
Pair p = pq.poll();
p.h = p.h + tow[i];
ans[i] = p.id;
pq.add(p);
}
System.out.println("YES");
for (int i = 0; i < n; i++) {
System.out.print(ans[i] + " ");
}
System.out.println();
}
}
} | import java.util.*;
import java.lang.*;
import java.io.*;
public class Template {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null ||!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// static void solve(String s)
// {
//// Scanner sc = new Scanner(System.in);
//// String s = sc.next();
//
// int x[] = new int[2];
// x[0] = x[1] = -1;
//
// int ans = 0;
// int n = s.length();
// for(int i=0;i<n;i++)
// {
// int c = s.charAt(i) - '0';
// if(c == 1 || c == 0)
// {
// x[(i%2) ^ c] = i;
// }
// int min = Math.min(x[0], x[1]);
// ans += i - min;
// //System.out.println(ans);
// }
// System.out.println(ans);
// }
//
// public static void main(String args[])
// {
// FastReader sc = new FastReader();
// //solve();
// //Scanner sc = new Scanner(System.in)
// int testcases = sc.nextInt(); // nTest is the number of treasure hunts.
//
//// int testcases = 3;
// while(testcases-- > 0)
// {
// String s = sc.next();
// solve(s);
//
// }
//
// }
static class Pair implements Comparable<Pair>
{
int h;
int ind;
Pair(int h, int ind)
{
this.h = h;
this.ind = ind;
}
@Override
public int compareTo(Pair o) {
return this.h - o.h;
}
}
public static void main(String[] args) {
FastReader fs=new FastReader();
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
int n = fs.nextInt();
int m = fs.nextInt();
int x = fs.nextInt();
if(n < m)
{
System.out.println("NO");
continue;
}
Pair a[] = new Pair[n];
PriorityQueue<Pair> heap = new PriorityQueue<>();
for(int i=0;i<n;i++)
{
a[i] = new Pair(fs.nextInt(), i);
}
Arrays.sort(a);
for(int i=1;i<=m;i++)
{
heap.add(new Pair(0, i));
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int ans[] = new int[n];
int idx = 0;
while(!heap.isEmpty() && idx < n)
{
Pair curr = heap.poll();
curr.h += a[idx].h;
ans[a[idx].ind] = curr.ind;
heap.add(new Pair(curr.h, curr.ind));
idx++;
}
// int towers[] = new int[m+1];
// int tower = 1;
// boolean flag = false;
// boolean inc = true;
// for(int i=0;i<n;i++)
// {
// if(tower == m+1)
// {
// tower = m;
// inc = false;
// }
// if(tower == 0)
// {
// tower = 1;
// inc = true;
// }
// towers[tower] += a[i].h;
// System.out.println(a[i].h +" THis" + tower);
//// min = Math.min(min, towers[tower]);
//// max = Math.max(max, towers[tower]);
// ans[a[i].ind] = tower;
//// if(Math.abs(max - min) > x)
//// {
//// System.out.println("NO" + a[i].ind+" "+a[i].h +" "+min +" "+max);
//// flag = true;
//// break;
//// }
// if(inc)
// tower++;
// else
// tower--;
// }
// for(int i=1;i<=m;i++)
// {
// min = Math.min(min, towers[i]);
// max = Math.max(max, towers[i]);
// }
// if(Math.abs(max - min) > x)
// {
// System.out.println("NO" + max+" "+min);// + a[i].ind+" "+a[i].h +" "+min +" "+max);
// //flag = true;
// continue;
// }
// if(flag)
// continue;
System.out.println("YES");
for(int i:ans)
System.out.print(i+" ");
System.out.println();
}
}
}
| 0 |
6f393cfe | f38ae053 | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
try{
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int lst[][] = new int[n][5];
for(int i=0; i<n; i++){
String s = br.readLine();
for(int j=0; j<s.length(); j++){
lst[i][s.charAt(j)-'a']++;
}
}
int fans = Integer.MIN_VALUE;
for(int i=0; i<5; i++){
int val[] = new int[n];
for(int k=0; k<n; k++){
int sum = 0;
for(int j=0; j<5; j++){
if(i==j){
sum += lst[k][j];
}else{
sum -= lst[k][j];
}
}
val[k] = sum;
}
Arrays.sort(val);
int sum = 0;
int ans = 0;
for(int x = n-1; x>=0; x--){
sum+=val[x];
if(sum>0){
ans++;
}else{
break;
}
}
fans = Math.max(fans, ans);
}
bw.write(fans+"\n");
}
bw.flush();
}catch(Exception e){
return;
}
}
} | import java.util.*;
import java.io.*;
public class Fixed_Points
{
public static void process()throws IOException
{
int n=I();
String s[]=new String[n];
for(int i=0;i<n;i++)
{
s[i]=S();
}
int max=0;
for(char ch='a';ch<='e';ch++)
{
int t=0;
ArrayList<Integer> po=new ArrayList<Integer>();
ArrayList<Integer> ne=new ArrayList<Integer>();
for(int j=0;j<n;j++)
{
int f1=0;int f2=0;
for(int k=0;k<s[j].length();k++)
{
if(s[j].charAt(k)==ch)
{
f1++;
}
else
{
f2++;
}
}
if(f1>f2)
po.add(f1-f2);
else
ne.add(f1-f2);
}
Collections.sort(po);
Collections.reverse(po);
Collections.sort(ne);
Collections.reverse(ne);
int sum=0;
for(int i=0;i<po.size();i++)
{
sum=sum+po.get(i);
}
int tp=0;
for(int i=0;i<ne.size();i++)
{
if(ne.get(i)+sum>0)
{
sum=sum+ne.get(i);
tp++;
}
}
t=tp+po.size();
max=Math.max(max, t);
}
pn(max);
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static void pn(Object o){out.println(o);out.flush();}
static void p(Object o){out.print(o);out.flush();}
static void pni(Object o){out.println(o);System.out.flush();}
static int I() throws IOException{return sc.nextInt();}
static long L() throws IOException{return sc.nextLong();}
static double D() throws IOException{return sc.nextDouble();}
static String S() throws IOException{return sc.next();}
static char C() throws IOException{return sc.next().charAt(0);}
static int[] Ai(int n) throws IOException{int[] arr = new int[n];for (int i = 0; i < n; i++)arr[i] = I();return arr;}
static String[] As(int n) throws IOException{String s[] = new String[n];for (int i = 0; i < n; i++)s[i] = S();return s;}
static long[] Al(int n) throws IOException {long[] arr = new long[n];for (int i = 0; i < n; i++)arr[i] = L();return arr;}
static void dyn(int dp[][],int n,int m,int z)throws IOException {for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ dp[i][j]=z;}} }
// *--------------------------------------------------------------------------------------------------------------------------------*//
static class AnotherReader {BufferedReader br;StringTokenizer st;AnotherReader() throws FileNotFoundException {br = new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a) throws FileNotFoundException {br = new BufferedReader(new FileReader("input.txt"));}
String next() throws IOException{while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}}return st.nextToken();}
int nextInt() throws IOException {return Integer.parseInt(next());}
long nextLong() throws IOException {return Long.parseLong(next());}
double nextDouble() throws IOException {return Double.parseDouble(next());}
String nextLine() throws IOException {String str = "";try {str = br.readLine();} catch (IOException e) {e.printStackTrace();}return str;}}
public static void main(String[] args)throws IOException{try{boolean oj=true;if(oj==true)
{AnotherReader sk=new AnotherReader();PrintWriter out=new PrintWriter(System.out);}
else
{AnotherReader sk=new AnotherReader(100);out=new PrintWriter("output.txt");}
long T=L();while(T-->0)
{process();}out.flush();out.close();}catch(Exception e){return;}}}
//*-----------------------------------------------------------------------------------------------------------------------------------*//
| 0 |
464a03b8 | cf27732e | import java.util.*;
public class Soltion{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
Integer[] arr = new Integer[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
String s = sc.next();
List<Integer> blue = new ArrayList<>();
List<Integer> red = new ArrayList<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='B'){
blue.add(arr[i]);
}
else{
red.add(arr[i]);
}
}
Collections.sort(blue);
Collections.sort(red);
int p=1,q=n;
boolean flag = true;
for(int i=red.size()-1;i>=0;i--){
if(red.get(i)>q){
flag = false;
break;
}
q--;
}
for(int i=0;i<blue.size();i++){
if(blue.get(i)<p){
flag = false;
break;
}
p++;
}
System.out.println(flag? "Yes" : "No");
}
}
}
| import java.util.*;
import java.io.*;
public class code {
// remember long, to reformat ctrl + shift +f
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int []vals=new int[n];
boolean numLine[]=new boolean[n+1];
for(int i=0;i<n;i++)vals[i]=sc.nextInt();
String s=sc.nextLine();
ArrayList<Integer>b=new ArrayList<Integer>();
ArrayList<Integer>r=new ArrayList<Integer>();
for(int i=0;i<n;i++) {
if(s.charAt(i)=='B' && vals[i]>0 )b.add(vals[i]);
else if( s.charAt(i)=='R' && vals[i]<=n)r.add(vals[i]);
}
Collections.sort(b);
Collections.sort(r);
int small=1;
for(int i=0;i<b.size();i++) {
int y=b.get(i);
if(y<small)continue;
numLine[small]=true;
small++;
}
// pw.println(Arrays.toString(numLine));
int large=n;
for(int i=r.size()-1;i>=0;i--) {
int y=r.get(i);
if(y>large)continue;
// y=Math.max(large, y);
numLine[large]=true;
large--;
}
//pw.print(Arrays.toString(numLine));
boolean can=true;
for(int i=1;i<=n;i++) {
if(numLine[i]==false) {
pw.println("no");
can=false;
break;
}
}
if(can)pw.println("yes");
}
pw.close();
}
// --------------------stuff ----------------------
static class pair implements Comparable<pair> {
int v;
int w;
public pair(int v,int w) {
this.v = v;
this.w = w;
}
public int compareTo(pair p) {
return this.w- p.w;// increasing order!!
//return Double.compare(v*w, p.w*p.v);
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | 0 |
22b41936 | 9cea10af |
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.*;
public class Main1 {
static ArrayList<Integer> list1 = new ArrayList<>() ;
static ArrayList<Integer> list2 = new ArrayList<>() ;
static int n , m ;
static long dp[][] ;
static long solver(int i , int j ){
// i = empty chairs
if (j == m)return 0 ;
int tt1 = n-i ;
int tt2 = m-j ;
if (n-i < m-j)return Long.MAX_VALUE/2 ;
if ( dp[i][j] != -1 )return dp[i][j] ;
long a = solver(i+1 , j) ;
long b = abs( list1.get(i) - list2.get(j)) + solver(i+1 , j+1) ;
return dp[i][j] = min(a , b) ;
}
public static void main(String[] args) throws IOException {
// try {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int N = in.nextInt() ;
int a[] = in.readArray(N) ;
for (int i = 0; i <N ; i++) {
if (a[i] == 1)list2.add(i) ;
else list1.add(i) ;
}
n = list1.size() ;
m = list2.size() ;
dp = new long[n][m] ;
for(int i=0 ; i<n ; i++)
for(int j=0 ; j<m ; j++)
dp[i][j] = -1 ;
System.out.println(solver(0 , 0 ));
out.flush();
out.close();
// }
// catch (Exception e){
// return;
// }
}
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static ArrayList<Integer> list = new ArrayList<>();
static boolean A[] = new boolean[2 * 90000001];
static void seive(int n) {
int maxn = n;
//int maxn = 1000000 ;
A[0] = A[1] = true;
for (int i = 2; i * i <= maxn; i++) {
if (!A[i]) {
for (int j = i * i; j <= maxn; j += i)
A[j] = true;
}
}
for (int i = 2; i <= maxn; i++)
if (!A[i])
list.add(i);
}
static int findLCA(int a, int b, int par[][], int depth[]) {
if (depth[a] > depth[b]) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
int diff = depth[b] - depth[a];
for (int i = 19; i >= 0; i--) {
if ((diff & (1 << i)) > 0) {
b = par[b][i];
}
}
if (a == b)
return a;
for (int i = 19; i >= 0; i--) {
if (par[b][i] != par[a][i]) {
b = par[b][i];
a = par[a][i];
}
}
return par[a][0];
}
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
static void formArrayForBinaryLifting(int n, int par[][]) {
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n; i++) {
if (par[i][j - 1] == -1)
continue;
par[i][j] = par[par[i][j - 1]][j - 1];
}
}
}
static void sort(int ar[]) {
int n = ar.length;
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort1(long ar[]) {
int n = ar.length;
ArrayList<Long> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static long ncr(long n, long r, long mod) {
if (r == 0)
return 1;
long val = ncr(n - 1, r - 1, mod);
val = (n * val) % mod;
val = (val * modInverse(r, mod)) % mod;
return val;
}
static long fast_pow(long base, long n, long M) {
if (n == 0)
return 1;
if (n == 1)
return base % M;
long halfn = fast_pow(base, n / 2, M);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
static long modInverse(long n, long M) {
return fast_pow(n, M - 2, M);
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private final Writer os;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(long c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(String c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput println() {
return append(System.lineSeparator());
}
public FastOutput flush() {
try {
// boolean success = false;
// if (stringBuilderValueField != null) {
// try {
// char[] value = (char[]) stringBuilderValueField.get(cache);
// os.write(value, 0, cache.length());
// success = true;
// } catch (Exception e) {
// }
// }
// if (!success) {
os.append(cache);
// }
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| //package currentContest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class P4 {
static int dp[][]=new int[5000+1][5000+1];
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader sc=new FastReader();
int t=1;
//t=sc.nextInt();
StringBuilder s=new StringBuilder();
while(t--!=0) {
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<=n;i++) {
for(int j=0;j<=n;j++) {
P4.dp[i][j]=-1;
}
}
ArrayList<Integer> one=new ArrayList<>();
ArrayList<Integer> zero=new ArrayList<>();
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
if(a[i]==0) {
zero.add(i);
}else {
one.add(i);
}
}
Collections.sort(zero);
Collections.sort(one);
long ans=sol(0,0,zero.size(),one.size(),a,zero,one);
System.out.println(ans);
}
//System.out.println(s);
}
private static long sol(int i, int j, int n, int m,int a[], ArrayList<Integer> zero, ArrayList<Integer> one) {
//System.out.println(i+" "+j);
// TODO Auto-generated method stub
if(j==m) {
return 0;
}
int av=n-i;
int rem=m-j;
if(av<rem) {
return Integer.MAX_VALUE-1;
}
if(dp[i][j]!=-1) {
return dp[i][j];
}
long ans1=sol(i+1,j,n,m,a, zero, one);
long ans2=Math.abs(zero.get(i)-one.get(j))+sol(i+1,j+1,n,m,a, zero, one);
dp[i][j]=(int) Math.min(ans1, ans2);
return dp[i][j];
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| 1 |
0b91922c | 71a4f6d2 |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Prac{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public int[] nia1(int n) {
int a[] = new int[n+1];
for (int i = 1; i <=n; i++) {
a[i] = ni();
}
return a;
}
public long[] nla(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public long[] nla1(int n) {
long a[] = new long[n+1];
for (int i = 1; i <= n; i++) {
a[i] = nl();
}
return a;
}
public Long[] nLa(int n) {
Long a[] = new Long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public Long[] nLa1(int n) {
Long a[] = new Long[n+1];
for (int i = 1; i <= n; i++) {
a[i] = nl();
}
return a;
}
public Integer[] nIa(int n) {
Integer a[] = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public Integer[] nIa1(int n) {
Integer a[] = new Integer[n+1];
for (int i = 1; i <= n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static class Key {
private final int x, y;
public Key(int x, int y) {
this.x = x; this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key key = (Key) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static class Pair{
int x,y;
Pair(int a,int b){
x=a;y=b;
}
// @Override
// public int compareTo(Pair p) {
// if(x==p.x) return y-p.y;
// return x-p.x;
// }
}
static void shuffleArray(long temp[]){
int n = temp.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = temp[i];
int randomPos = i + rnd.nextInt(n-i);
temp[i] = temp[randomPos];
temp[randomPos] = tmp;
}
}
static int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}
//static long lcm(long a,long b){return (a/gcd(a,b))*b;}
static PrintWriter w = new PrintWriter(System.out);
static long mod1=998244353L,mod=1000000007;
//static int r[]={0,1,0,-1}, c[]={1,0,-1,0};
static int[] nextG(int arr[]){
int n = arr.length;
Stack<Integer> s = new Stack<>();
int ng[] = new int[n];
for(int i = 0 ; i < n ; i++){
while(!s.isEmpty() && arr[s.peek()] <= arr[i]){
ng[s.pop()] = i;
}
s.add(i);
}
while(!s.isEmpty()){
ng[s.pop()] = n;
}
return ng;
}
static int[] nextS(int arr[]){
int n = arr.length;
Stack<Integer> s = new Stack<>();
int ns[] = new int[n];
for(int i = 0 ; i < n ; i++){
while(!s.isEmpty() && arr[s.peek()] >= arr[i]){
ns[s.pop()] = i;
}
s.add(i);
}
while(!s.isEmpty()){
ns[s.pop()] = n;
}
return ns;
}
static int[] prevG(int arr[]){
int n = arr.length;
Stack<Integer> s = new Stack<>();
int pg[] = new int[n];
for(int i = n-1 ; i >= 0 ; i--){
while(!s.isEmpty() && arr[s.peek()] <= arr[i]){
pg[s.pop()] = i;
}
s.add(i);
}
while(!s.isEmpty()){
pg[s.pop()] = -1;
}
return pg;
}
static int[] prevS(int arr[]){
int n = arr.length;
Stack<Integer> s = new Stack<>();
int ps[] = new int[n];
for(int i = n-1 ; i >= 0 ; i--){
while(!s.isEmpty() && arr[s.peek()] >= arr[i]){
ps[s.pop()] = i;
}
s.add(i);
}
while(!s.isEmpty()){
ps[s.pop()] = -1;
}
return ps;
}
public static void main(String [] args){
InputReader sc=new InputReader(System.in);
int n = sc.ni();
int arr [] = sc.nia(n);
int ng[] = nextG(arr);
int ns [] = nextS(arr);
int pg[] = prevG(arr);
int ps[] = prevS(arr);
int ans[]=new int[n];
Arrays.fill(ans,10000000);
ans[n-1] = 0;
for(int i = n -1 ; i >= 0 ; i --){
if(ns[i] != n){
ans[i] = Math.min(ans[i] , ans[ns[i]]+1);
}
if(ng[i] != n){
ans[i] = Math.min(ans[i] , ans[ng[i]]+1);
}
if(pg[i] != -1){
ans[pg[i]] = Math.min(ans[pg[i]] , ans[i]+1);
}
if(ps[i] != -1){
ans[ps[i]] = Math.min(ans[ps[i]] , ans[i]+1);
}
}
w.println(ans[0]);
w.close();
}
} | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DDiscreteCentrifugalJumps solver = new DDiscreteCentrifugalJumps();
solver.solve(1, in, out);
out.close();
}
static class DDiscreteCentrifugalJumps {
public void solve(int testNumber, InputReader s, PrintWriter w) {
int n = s.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = s.nextInt();
int[] dp = new int[n];
for (int i = 0; i < n; i++)
dp[i] = i;
Stack<Integer> dec = new Stack<>();
dec.push(0);
Stack<Integer> inc = new Stack<>();
inc.push(0);
for (int i = 1; i < n; i++) {
while (!dec.isEmpty() && a[dec.peek()] < a[i]) {
dp[i] = Math.min(dp[i], dp[dec.peek()] + 1);
dec.pop();
}
if (!dec.isEmpty()) {
dp[i] = Math.min(dp[i], dp[dec.peek()] + 1);
if (a[dec.peek()] == a[i])
dec.pop();
}
dec.push(i);
while (!inc.isEmpty() && a[inc.peek()] > a[i]) {
dp[i] = Math.min(dp[i], dp[inc.peek()] + 1);
inc.pop();
}
if (!inc.isEmpty()) {
dp[i] = Math.min(dp[i], dp[inc.peek()] + 1);
if (a[inc.peek()] == a[i])
inc.pop();
}
inc.push(i);
}
w.println(dp[n - 1]);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| 0 |
63118334 | c392efe7 | import java.util.*;
import java.io.*;
public class MyClass
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static class Pair implements Comparable<Pair>
{
int f,s;
Pair(int f,int s)
{
this.f=f;
this.s=s;
}
public int compareTo(Pair p)
{
return this.f-p.f;
}
}
static ArrayList<Integer> edge[];
public static void main(String args[])
{
FastReader fs=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int tc=fs.nextInt();
while(tc-->0)
{
int n=fs.nextInt();
int a=fs.nextInt();
int b=fs.nextInt();
int da=fs.nextInt();
int db=fs.nextInt();
edge=new ArrayList[n+1];
for(int i=1;i<=n;i++)
edge[i]=new ArrayList<>();
for(int i=1;i<n;i++)
{
int u=fs.nextInt();
int v=fs.nextInt();
edge[u].add(v);
edge[v].add(u);
}
int dist[]=new int[n+1];
Arrays.fill(dist,-1);
dist[a]=0;
Queue<Integer> queue=new LinkedList<>();
queue.add(a);
while(!queue.isEmpty())
{
int node=queue.poll();
for(int v:edge[node])
{
if(dist[v]==-1)
{
dist[v]=dist[node]+1;
queue.add(v);
}
}
}
if(dist[b]<=da)
{
pw.println("Alice");
continue;
}
int mx=0,mxvert=1;
for(int i=1;i<=n;i++)
{
if(dist[i]>mx)
{
mx=dist[i];
mxvert=i;
}
}
Arrays.fill(dist,-1);
dist[mxvert]=0;
queue.add(mxvert);
while(!queue.isEmpty())
{
int node=queue.poll();
for(int v:edge[node])
{
if(dist[v]==-1)
{
dist[v]=dist[node]+1;
queue.add(v);
}
}
}
for(int i=1;i<=n;i++)
mx=Math.max(mx,dist[i]);
db=Math.min(db,mx);
if(db>2*da)
pw.println("Bob");
else
pw.println("Alice");
}
pw.flush();
pw.close();
}
} | import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class D {
static int visited[];
static int distance[];
private static ArrayList<Integer>[] adj;
private static void BFS(int node) {
visited[node] = 1;
Queue<Integer> q = new LinkedList<Integer>();
q.offer(node);
distance[node] = 0;
while(!q.isEmpty()) {
int curr = q.poll();
for(int child : adj[curr]) {
if(visited[child] == 0) {
q.offer(child);
distance[child] = distance[curr] + 1;
visited[child] = 1;
}
}
}
}
public static void process() throws IOException {
int n = sc.nextInt(),a = sc.nextInt(),b = sc.nextInt(),
da = sc.nextInt(),db = sc.nextInt();
adj = new ArrayList[n+1];
distance = new int[n+1];
visited = new int[n+1];
for(int i = 0; i<=n; i++)adj[i] = new ArrayList<Integer>();
for(int i =1; i<n; i++) {
int u = sc.nextInt(),v = sc.nextInt();
adj[u].add(v);
adj[v].add(u);
}
BFS(a);
if(distance[b] <= da || db-da<=da) {
System.out.println("Alice");
return;
}
ArrayList<Pair> lis = new ArrayList<D.Pair>();
for(int i = 1; i<=n; i++)lis.add(new Pair(distance[i], i));
Collections.sort(lis);
Pair e = lis.get(n-1);
distance = new int[n+1];
visited = new int[n+1];
BFS(e.y);
int max = 0;
for(int i = 1; i<=n; i++)max = max(max,distance[i]);
if(max-da<=da) {
System.out.println("Alice");
return;
}
System.out.println("Bob");
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static PrintWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new PrintWriter(System.out);
} else {
sc = new FastScanner(100);
out = new PrintWriter("output.txt");
}
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
out.close();
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (!(o instanceof Pair)) return false;
// Pair key = (Pair) o;
// return x == key.x && y == key.y;
// }
//
// @Override
// public int hashCode() {
// int result = x;
// result = 31 * result + y;
// return result;
// }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static void debug(Object o) {
System.out.println(o);
}
static void debug(Object x, Object y) {
System.out.println("(" + x + " , " + y + ")");
}
static void println(Object o) {
out.println(o);
}
static void println(int[] o) {
for (int e : o)
print(e + " ");
println();
}
static void println(long[] o) {
for (long e : o)
print(e + " ");
println();
}
static void println() {
out.println();
}
static void print(Object o) {
out.print(o);
}
static void pflush(Object o) {
out.println(o);
out.flush();
}
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static int max(int x, int y) {
return Math.max(x, y);
}
static int min(int x, int y) {
return Math.min(x, y);
}
static int abs(int x) {
return Math.abs(x);
}
static long abs(long x) {
return Math.abs(x);
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
static long max(long x, long y) {
return Math.max(x, y);
}
static long min(long x, long y) {
return Math.min(x, y);
}
public static int gcd(int a, int b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public static long gcd(long a, long b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastScanner(int a) throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) throws IOException {
int[] A = new int[n];
for (int i = 0; i != n; i++) {
A[i] = sc.nextInt();
}
return A;
}
long[] readArrayLong(int n) throws IOException {
long[] A = new long[n];
for (int i = 0; i != n; i++) {
A[i] = sc.nextLong();
}
return A;
}
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
}
| 0 |
c57a973e | fa484fdd | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Air {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
for(int tt=0; tt<T;tt++){
int n = sc.nextInt(), k=sc.nextInt();
int [] positions=new int[k], temp=new int[k];
for (int i=0;i<k;i++) positions[i]=sc.nextInt();
for (int i=0;i<k;i++) temp[i]=sc.nextInt();
int[] forced=new int[n];
Arrays.fill(forced, Integer.MAX_VALUE/2);
for (int i=0;i<k;i++) forced[positions[i]-1]=temp[i];
for (int i=1;i<n;i++) forced[i]=Math.min(forced[i], forced[i-1]+1);
for (int i=n-2;i>=0;i--) forced[i]=Math.min(forced[i], forced[i+1]+1);
for (int i=0;i<n;i++) System.out.print(forced[i]+" ");
System.out.println();
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| //import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class AirConditioners {
static InputReader inputReader=new InputReader(System.in);
static void solve()
{
int n=inputReader.nextInt();
int k=inputReader.nextInt();
int pos[]=inputReader.nextIntArray(k);
int power[]=inputReader.nextIntArray(k);
int answer[]=new int[n];
Arrays.fill(answer,(int)(2e9));
for (int i=0;i<k;i++)
{
answer[pos[i]-1]=power[i];
}
for (int i=1;i<n;i++)
{
answer[i]=Math.min(answer[i],answer[i-1]+1);
}
for (int i=n-2;i>=0;i--)
{
answer[i]=Math.min(answer[i],answer[i+1]+1);
}
for (int i=0;i<n;i++)
{
out.print(answer[i]+" ");
}
}
static PrintWriter out=new PrintWriter((System.out));
public static void main(String args[])throws IOException
{
int t=inputReader.nextInt();
while(t-->0)
{
solve();
out.println();
}
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| 1 |
3b10bd6d | f2cf6a74 | import java.util.*;
public class B {
//static int num = (int)Math.pow(10,6);
static Scanner sc = null;
public static void main(String[] args) {
//System.out.println("Enter :");
sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- >0){
int n = sc.nextInt();
sc.nextLine();
String a[] = new String[n];
for(int i=0;i<n;i++){
a[i] = sc.nextLine();
}
ArrayList<Character> a1 = new ArrayList<>();
a1.add('a');
a1.add('b');
a1.add('c');
a1.add('d');
a1.add('e');
int res = 0;
for(Character ch : a1){
ArrayList<Integer> a2 = new ArrayList<>();
for(int i=0;i<n;i++){
int f1 = 0;
int f2 = 0;
String s = a[i];
for(int j=0;j<s.length();j++){
char c2 = s.charAt(j);
if(c2==ch)
f1++;
else
f2++;
}
a2.add(f1-f2);
}
Collections.sort(a2);
int sum = 0;
int count = 0;
for(int j = n-1;j>=0;j--){
int num = a2.get(j);
sum+=num;
if(sum>0){
count++;
}
else
break;
}
res = Math.max(res,count);
}
System.out.println(res);
}
}
}
| import java.util.*;
public class shivam{
public static int diff(String str, char ch){
int cnt=0;
for(int i=0;i<str.length();i++){
if(ch==str.charAt(i)){
cnt++;
}
}
return cnt-(str.length()-cnt);
}
public static int process(char ch,int n,String []arr){
int[]a=new int[n];
for(int i=0;i<n;i++){
a[i]=diff(arr[i],ch);
}
Arrays.sort(a);
int max=0;
int sum=0;
for(int i=n-1;i>=0;i--){
sum+=a[i];
if(sum>0){
max++;
}
else{
break;
}
}
return max;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
while(k-->0){
int n=sc.nextInt();
String []arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.next();
}
int a=process('a',n,arr);
int b=process('b',n,arr);
int c=process('c',n,arr);
int d=process('d',n,arr);
int e=process('e',n,arr);
System.out.println(Math.max(a,Math.max(b,Math.max(c,Math.max(d,e)))));
}
}
} | 0 |
6c4ac8d3 | 829d2024 | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.io.*;
public class ieee1{
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int t=scn.nextInt();
while(t-->0){
HashMap<Integer,Integer> map=new HashMap<>();
int b=scn.nextInt();
int m=scn.nextInt();
int x=scn.nextInt();
int[] arr=new int[b];
PriorityQueue<Node> pq=new PriorityQueue<>(new pqc());
for(int i=0;i<b;i++){
int ele=scn.nextInt();
arr[i]=ele;
}
for(int i=1;i<=m;i++){
pq.add(new Node(i,0));
}
System.out.println("YES");
for(int i=0;i<arr.length;i++){
int ele=arr[i];
Node n=pq.poll();
System.out.print(n.ind+" ");
n.data+=ele;
pq.add(n);
}
System.out.println();
}
}
public static class Node{
int ind;
int data;
Node(int ind,int data){
this.ind=ind;
this.data=data;
}
}
public static class pqc implements Comparator<Node>{
public int compare(Node n1,Node n2){
if(n1.data<n2.data){
return -1;
}
else if(n1.data>n2.data){
return 1;
}
else{
return 0;
}
}
}
}
| 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();
}
}
}
| 1 |
71c7ac71 | bdfe8110 | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class E{
public static void main(String[] args) throws IOException {
// br = new BufferedReader(new FileReader(".in"));
// out = new PrintWriter(new FileWriter(".out"));
//new Thread(null, new (), "peepee", 1<<28).start();
int q =readInt();
while(q-->0) {
br.readLine();
int n =readInt();
int k =readInt();
int[] ans = new int[n];
Arrays.fill(ans, (int)2e9+1);
int[] a = new int[k];
int[] t = new int[k];
int mini = 0, maxi = 0;
for (int i = 0 ;i <k; i+=1) {
a[i]=readInt()-1;
if (a[i] < a[mini]) mini = i;
if (a[i] > a[maxi]) maxi = i;
}
for (int j = 0; j <k; j++) t[j]=readInt();
for (int i = 0; i < k; i++) ans[a[i]]=t[i];
int[] l = new int[n];
int[] r = new int[n];
Arrays.fill(l, (int)2e9);
Arrays.fill(r, (int)2e9);
int temp = t[mini];
for (int i = a[mini]; i < n; i++) {
l[i] = temp = min(temp+1, ans[i]);
}
temp = t[maxi];
for (int i = a[maxi]; i >= 0; i--) {
r[i] = temp = min(temp+1,ans[i]);
}
for (int i = 0; i < n; i++) out.print(min(l[i],r[i]) + " ");
out.println();
}
out.close();
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static StringTokenizer st = new StringTokenizer("");
static String read() throws IOException{return st.hasMoreTokens() ? st.nextToken():(st = new StringTokenizer(br.readLine())).nextToken();}
static int readInt() throws IOException{return Integer.parseInt(read());}
static long readLong() throws IOException{return Long.parseLong(read());}
static double readDouble() throws IOException{return Double.parseDouble(read());}
} | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class E {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
int T=fs.nextInt();
PrintWriter out=new PrintWriter(System.out);
for (int tt=0; tt<T; tt++) {
int n=fs.nextInt(), k=fs.nextInt();
int[] positions=fs.readArray(k), temps=fs.readArray(k);
int[] forced=new int[n];
Arrays.fill(forced, Integer.MAX_VALUE/2);
for (int i=0; i<k; i++) forced[positions[i]-1]=temps[i];
for (int i=1; i<n; i++)
forced[i]=Math.min(forced[i], forced[i-1]+1);
for (int i=n-2; i>=0; i--)
forced[i]=Math.min(forced[i], forced[i+1]+1);
for (int i=0; i<n; i++) out.print(forced[i]+" ");
out.println();
}
out.close();
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| 0 |
1ea771ea | 2f8c3bf3 |
import java.io.*;
import java.util.*;
public class CODECHEF {
static class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
String nextLine() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
return (char) c;
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
static long MOD=1000000000;
static class Pair{
long a;
int b;
Pair(long i,int j){
a=i;
b=j;
}
}
static long[] solve(int[] pos,long[] arr,int n,int k){
long[] ans=new long[n];
long[] left=new long[n];
long[] right=new long[n];
long min=Integer.MAX_VALUE;
for(int i=0;i<n;i++){
min=Math.min(min+1,arr[i]);
left[i]=min;
}
min=Integer.MAX_VALUE;
for(int i=n-1;i>=0;i--){
min=Math.min(min+1,arr[i]);
right[i]=min;
}
for(int i=0;i<n;i++){
ans[i]=Math.min(left[i],right[i]);
}
return ans;
}
public static void main(String[] args) throws java.lang.Exception {
FastReader fs=new FastReader(System.in);
// StringBuilder sb=new StringBuilder();
// PrintWriter out=new PrintWriter(System.out);
int t=fs.nextInt();
while (t-->0){
int n=fs.nextInt();
int k=fs.nextInt();
int[] pos=new int[k];
for(int i=0;i<k;i++)
pos[i]=fs.nextInt()-1;
long[] temp=new long[n];
int ptr=0;
Arrays.fill(temp,Integer.MAX_VALUE);
for(int i=0;i<k;i++)
temp[pos[ptr++]]=fs.nextLong();
long[] ans=solve(pos,temp,n,k);
for(int i=0;i<n;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
//out.close;
}
}
| import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int k=sc.nextInt();
int idx[]=new int[k];
for(int i=0;i<k;i++){
idx[i]=sc.nextInt();
}
long arr[]=new long[n];
Arrays.fill(arr,Integer.MAX_VALUE);
for(int i=0;i<k;i++){
long temp=sc.nextLong();
arr[idx[i]-1]=temp;
}
long left[]=new long[n];
long right[]=new long[n];
Arrays.fill(left,Integer.MAX_VALUE);
Arrays.fill(right,Integer.MAX_VALUE);
left[0]=arr[0];
for(int i=1;i<n;i++){
left[i]=Math.min(left[i-1]+1,arr[i]);
}
right[arr.length-1]=arr[arr.length-1];
for(int i=n-2;i>=0;i--){
right[i]=Math.min(right[i+1]+1,arr[i]);
}
for(int i=0;i<n;i++){
// System.out.print(left[i]+"--"+right[i]+"\\");
System.out.print(Math.min(left[i],right[i])+" ");
}
System.out.println();
}
}
}
| 0 |
317baeaf | e17c5159 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while (t --> 0) {
int n = Integer.parseInt(br.readLine());
String a = br.readLine();
String b = br.readLine();
int alit = 0;
int blit = 0;
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (a.charAt(i) == '1') alit++;
if (b.charAt(i) == '1') blit++;
}
if (alit == blit) {
int count = 0;
for (int i = 0; i < n; i++)
if (a.charAt(i) != b.charAt(i)) count++;
ans = Math.min(count, ans);
}
if (alit == n - blit + 1) {
int count = 0;
for (int i = 0; i < n; i++)
if (a.charAt(i) == b.charAt(i)) count++;
ans = Math.min(ans, count);
}
if (ans == Integer.MAX_VALUE) {
pw.println("-1");
} else {
pw.println(ans);
}
}
pw.close();
}
} | import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=Integer.valueOf(sc.nextLine());
while (t-->0){
int n=Integer.valueOf(sc.nextLine());
int ans=100001;
String a=sc.nextLine();
String b=sc.nextLine();
HashSet<Integer> listb=new HashSet<>();
ArrayList<Integer> lista=new ArrayList<>();
for (int i=0;i<n;i++){
if(a.charAt(i)=='1') lista.add(i);
if(b.charAt(i)=='1') listb.add(i);
}
int num=0;
for (int i=0;i<lista.size();i++){
if(listb.contains(lista.get(i))) num++;
}
//第一种情况
if(lista.size()==listb.size()){
ans=Math.min(ans,(listb.size()-num)*2);
}
//第二种情况 n-lista.size() listb.size()-num
if(listb.size()-(n-lista.size())==1){
ans=Math.min(ans,(num-1)*2+1);
}
System.out.println((ans==100001)?-1:ans);
}
}
}
| 0 |
67996c4c | 7bc92b7f | import java.util.*;
import java.io.*;
public class code{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static void main(String[] arg) throws IOException{
//Reader in=new Reader();
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int[] arr=new int[n];
ArrayList<Integer> zero=new ArrayList<Integer>();
ArrayList<Integer> one=new ArrayList<Integer>();
for(int i=0;i<n;i++){
arr[i]=in.nextInt();
if(arr[i]==0) zero.add(i);
else one.add(i);
}
if(one.size()==0) {
System.out.println(0);
}
else{
int[][] dp=new int[one.size()][zero.size()];
for(int i=0;i<one.size();i++){
for(int j=0;j<zero.size();j++){
if(i==0 && j==0) dp[i][j]=Math.abs(one.get(i)-zero.get(j));
else if(j==0) dp[i][j]=Integer.MAX_VALUE/2;
else if(i==0) dp[i][j]=Math.min(dp[i][j-1],Math.abs(one.get(i)-zero.get(j)));
else{
dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(one.get(i)-zero.get(j)));
}
}
}
System.out.println(dp[one.size()-1][zero.size()-1]);
}
}
}
| import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws java.io.IOException {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
int[][] dp=new int[n][n];
int[][] min=new int[n][n];
ArrayList<Integer> ones=new ArrayList<>();
ArrayList<Integer> zero=new ArrayList<>();
for(int i=0;i<n;++i) {
arr[i] = sc.nextInt();
if(arr[i]==1)
ones.add(i);
else
zero.add(i);
}
for(int i=0;i<n;++i)
for(int j=0;j<n;++j) {
min[i][j] = Integer.MAX_VALUE;
dp[i][j] = Integer.MAX_VALUE;
}
int len=ones.size();
int zlen=zero.size();
int minn=0;
for(int i=0;i<len;++i)
{
int cur = ones.get(i);
for(int j=i;j<zlen;j++)
{
int curz = zero.get(j);
int cost = Math.abs(cur-curz);
if(i!=0 && curz-1>=0)
{
cost+=min[i-1][curz-1];
}
dp[i][curz]=cost;
}
minn=Integer.MAX_VALUE;
for(int j=0;j<n;++j)
{
if(dp[i][j]<minn)
minn=dp[i][j];
min[i][j]=minn;
}
}
System.out.println(minn);
}
}
// 1 0 1 1 0 0 1 | 0 |
28c2d81a | 8f6421f3 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class D {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int cases = fs.nextInt();
while(cases-->0){
int n = fs.nextInt(), k = fs.nextInt();
int[] positions = fs.readArray(k), temps = fs.readArray(k);
int[] forced = new int[n];
Arrays.fill(forced, Integer.MAX_VALUE/2);
for(int i=0; i<k; i++)
forced[positions[i]-1] = temps[i];
for(int i=1; i<n; i++)
forced[i] = Math.min(forced[i],forced[i-1]+1);
for(int i=n-2; i>=0; i--)
forced[i] = Math.min(forced[i], forced[i+1]+1);
for(int i=0; i<n; i++)
System.out.print(forced[i] + " ");
System.out.println();
}
}
//----------------------------------------------------------------------------------//
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
char nextChar(){
return next().charAt(0);
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Debug{
public static void debug(long a){
System.out.println("--> "+a);
}
public static void debug(long a, long b){
System.out.println("--> "+ a +" + " + b);
}
public static void debug(char a, char b){ System.out.println("--> "+ a +" + " + b); }
public static void debug(int[] array){
System.out.print("Array--> ");
System.out.println(Arrays.toString(array));
}
public static void debug(char[] array){
System.out.print("Array--> ");
System.out.println(Arrays.toString(array));
}
public static void debug(HashMap<Integer,Integer> map){
System.out.print("Map--> "+map.toString());
}
}
}
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Main{
public static void main (String[] args){
FastReader s = new FastReader();
int t=1;t=s.ni();
for(int test=1;test<=t;test++){
int n=s.ni(),k=s.ni();
int position[]=s.readArray(k),temp[]=s.readArray(k);
int ans[]=new int[n];
Arrays.fill(ans,Integer.MAX_VALUE/2);
for(int i=0;i<k;i++){
ans[position[i]-1]=temp[i];
}
for(int i=1;i<n;i++){
ans[i]=Math.min(ans[i-1]+1,ans[i]);
}
for(int i=n-2;i>=0;i--){
ans[i]=Math.min(ans[i],ans[i+1]+1);
}
for(int i=0;i<n;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
int[] readArray(int n){
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] =Integer.parseInt(next());
return a;
}
String next(){
while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());}
catch (IOException e){e.printStackTrace();}}return st.nextToken();}
String nextLine(){String str = "";try {str = br.readLine();}catch (IOException e)
{e.printStackTrace();}return str;}
}
}
| 1 |