A번에서 틀리면 가슴아프다...
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MeetingOfOldFriends {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split(" ");
long l1 = Long.parseLong(temp[0]);
long r1 = Long.parseLong(temp[1]);
long l2 = Long.parseLong(temp[2]);
long r2 = Long.parseLong(temp[3]);
long k = Long.parseLong(temp[4]);
long start = 0;
long end = 0;
if (l1 <= l2) {
start = l2;
if (r1 <= r2) {
end = r1;
} else {
end = r2;
}
} else {
start = l1;
if (r1 <= r2) {
end = r1;
} else {
end = r2;
}
}
if (start <= end) {
if (start <= k && k <= end) {
System.out.println(end - start);
} else {
System.out.println(end - start + 1);
}
} else {
System.out.println(0);
}
}
}
최소/최대값을 구해서 최소/최대/중간값 말고 다른 값이 나오면 Fail인 걸로 판별한다.
중간값 없이 최소랑 최대만 있는 경우 조건을 잘못 걸어서 한번 틀렸다.
import java.util.Scanner;
public class FilyaAndHomework {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n+1];
int min = 1000000000;
int max = 0;
for (int i=1; i<=n; i++) {
a[i] = sc.nextInt();
if (a[i] < min) {
min = a[i];
}
if (a[i] > max) {
max = a[i];
}
}
boolean isPossible = false;
if (min == max) {
isPossible = true;
} else if ((max - min) % 2 == 0) {
int mid = (max + min) / 2;
for (int i=1; i<=n; i++) {
if (a[i] != min && a[i] != mid && a[i] != max) {
break;
}
if (i == n) isPossible = true;
}
} else {
for (int i=1; i<=n; i++) {
if (a[i] != min && a[i] != max) {
break;
}
if (i == n) isPossible = true;
}
}
System.out.println(isPossible?"YES":"NO");
}
}
주어진 숫자를 패턴형태로 바꿔서 그 패턴을 2진수로 사용하는 배열요소에 체크한다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SonyaAndQueries {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long t = Long.parseLong(br.readLine());
int[] cnt = new int[262144]; // 2^18
for (int i=0; i<t; i++) {
String[] temp = br.readLine().split(" ");
if ("+".equals(temp[0])) {
StringBuilder sb = new StringBuilder(temp[1]);
for (int j=0; j<sb.length(); j++) {
if (sb.charAt(j) % 2 == 0) {
sb.setCharAt(j, '0');
} else {
sb.setCharAt(j, '1');
}
}
cnt[Integer.parseInt(sb.toString(), 2)]++;
} else if ("-".equals(temp[0])) {
StringBuilder sb = new StringBuilder(temp[1]);
for (int j=0; j<sb.length(); j++) {
if (sb.charAt(j) % 2 == 0) {
sb.setCharAt(j, '0');
} else {
sb.setCharAt(j, '1');
}
}
cnt[Integer.parseInt(sb.toString(), 2)]--;
} else if ("?".equals(temp[0])) {
System.out.println(cnt[Integer.parseInt(temp[1], 2)]);
}
}
}
}
또 뒤의 한시간은 소득이 없었구만.
2000, 3000 point 문제니깐 어려웠던걸로...