Skip to content

Instantly share code, notes, and snippets.

@so77id
Last active May 3, 2020 05:20
Show Gist options
  • Save so77id/1ca9369bd166020c4a66e2ee782536b3 to your computer and use it in GitHub Desktop.
Save so77id/1ca9369bd166020c4a66e2ee782536b3 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class Solution {
public static class Step {
public int x, y, dist;
public Step(int x, int y, int dist) {
this.x = x;
this.y = y;
this.dist = dist;
}
}
public static void main(String[] args) {
Queue<Step> q = new LinkedList<Step>();
int[] mx = {-2, -2, -1, 1, 2, 2, 1, -1};
int[] my = {-1, 1, 2, 2, 1, -1, -2, -2};
Boolean[][] M = new Boolean[8][8];
for(int i = 0; i<8;i++)
for(int j = 0; j<8;j++)
M[i][j]=false;
Scanner stdIn = new Scanner(System.in);
int cx, cy, ox, oy;
Step tmp;
cx = stdIn.nextInt();
cy = stdIn.nextInt();
ox = stdIn.nextInt();
oy = stdIn.nextInt();
q.add(new Step(cx, cy, 0));
while(!q.isEmpty()) {
tmp = q.remove();
if(tmp.x == ox && tmp.y == oy){
System.out.println(tmp.dist);
break;
}
M[tmp.x][tmp.y] = true;
for(int i=0;i<8;i++){
if(tmp.x + mx[i] > 0 && tmp.x + mx[i] < 8 && tmp.y + my[i] > 0 && tmp.y + my[i] < 8 && !M[tmp.x + mx[i]][tmp.y + my[i]]) {
q.add(new Step(tmp.x + mx[i], tmp.y + my[i], tmp.dist+1));
}
}
}
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
Vector<Integer> songs = new Vector<Integer>();
for(int i=0;i<n;i++) songs.add(stdin.nextInt());
Set<Integer> set = new HashSet<Integer>();
int max = 0, count = 0;
for(int i=0;i<n;i++){
if(!set.contains(songs.get(i))){
set.add(songs.get(i));
count++;
if(max < count) max = count;
} else {
if(max < count) max = count;
set.clear();
count = 1;
set.add(songs.get(i));
}
}
System.out.println(max);
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sdtin = new Scanner(System.in);
int m,t,i;
m = sdtin.nextInt();
t = sdtin.nextInt();
i = 0;
while(true){
if(m > t) break;
m *= 3;
t *= 2;
i++;
}
System.out.println(i);
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static long solve(long i){
if(i == 1) return 0;
if(i == 2) return 6;
long p = (long)Math.pow(i,2);
return (p *(p-1) - 8*(i-1)*(i-2))/2;
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
long n = stdin.nextLong()+1;
for(long i=1;i<n;i++){
System.out.println(solve(i));
}
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
String str = stdin.next();
for(int i = str.length()-1; i>=0; i--){
System.out.print(str.charAt(i));
}
System.out.print("\n");
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
long sum = 0;
long buff;
long max = Long.MIN_VALUE;
int n = stdin.nextInt();
for(int i=0;i<n;i++){
buff = stdin.nextLong();
if(buff > max) max = buff;
sum += buff;
}
if(sum-max < max) System.out.println(2*max);
else System.out.println(sum);
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static class MinDiff {
public int minDiff;
public MinDiff() {
this.minDiff = Integer.MAX_VALUE;
}
public void update(int minDiff) {
if(minDiff<this.minDiff) this.minDiff = minDiff;
}
}
public static void solve(Vector<Integer> v, int i, int lsum, int rsum, MinDiff minDiff){
if(i >= v.size()) {
minDiff.update(Math.abs(lsum - rsum));
return;
}
solve(v, i+1, lsum+v.get(i),rsum, minDiff);
solve(v, i+1, lsum,rsum+v.get(i), minDiff);
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
Vector<Integer> v = new Vector<Integer>();
for(int i =0;i<n;i++) v.add(stdin.nextInt());
MinDiff minDiff = new MinDiff();
solve(v, 0, 0, 0, minDiff);
System.out.println(minDiff.minDiff);
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
int v;
int count=0;
Set<Integer> set = new TreeSet<Integer>();
for(int i=0;i<n;i++){
v = stdin.nextInt();
if(!set.contains(v)) {
count++;
set.add(v);
}
}
System.out.println(count);
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
String str = stdin.next();
long count = 0;
long max = 0;
char actual = Character.MIN_VALUE;;
for(char c:str.toCharArray()) {
if(actual == c) {
count++;
} else {
actual = c;
count = 1;
}
if(max < count) max = count;
}
System.out.println(max);
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static class Node{
public int value;
public Node next;
public Node(int value) {
this.value = value;
this.next = null;
}
}
public static class CircleList{
public Node head;
public Node tail;
public CircleList() {
this.head = null;
this.tail = null;
}
public void insert(int value){
Node n_node = new Node(value);
if(this.head == null && this.tail == null) {
n_node.next = n_node;
this.head = this.tail = n_node;
} else {
this.tail.next = n_node;
n_node.next = this.head;
this.tail = n_node;
}
}
public void leftRotate() {
this.head = this.head.next;
this.tail = this.tail.next;
}
public void print() {
if(this.head != null) {
System.out.print(this.head.value);
for(Node tmp=this.head.next;tmp!=this.head;tmp=tmp.next){
System.out.print(" " + tmp.value);
}
System.out.print("\n");
}
}
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
int r = stdin.nextInt();
CircleList cl = new CircleList();
for(int i=0;i<n;i++){
cl.insert(stdin.nextInt());
}
for(int i=0;i<r;i++){
cl.leftRotate();
}
cl.print();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment