NOTE/SWEA
10761. 신뢰 - d3
m-inz
2022. 6. 11. 18:51
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
static int time;
static int locationB;
static int locationO;
static ArrayList<Integer> B;
static ArrayList<Integer> O;
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T;
T=Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
StringTokenizer st;
for(int test_case = 1; test_case <= T; test_case++)
{
st = new StringTokenizer(br.readLine()," ");
int N = Integer.parseInt(st.nextToken());
String[] sequenceRobot = new String[N];
B = new ArrayList<>();
O = new ArrayList<>();
for(int i=0; i<N; i++) {
sequenceRobot[i]=st.nextToken();
if(sequenceRobot[i].equals("B")) {
B.add(Integer.parseInt(st.nextToken()));
}else {
O.add(Integer.parseInt(st.nextToken()));
}
}
time = 0;
locationB = 1;
locationO = 1;
int i=0;
while(B.size()!=0 || O.size()!=0) {
if(B.size()!=0 && O.size()==0) {
while(B.size()!=0) {
delete(B.get(0),"B");
}
}else if(O.size()!=0 && B.size()==0) {
while(O.size()!=0) {
delete(O.get(0),"O");
}
}else {
String robot = sequenceRobot[i];
i++;
int buttonB = B.get(0);
int buttonO = O.get(0);
int moveB = Math.abs(locationB-buttonB);
int moveO = Math.abs(locationO-buttonO);
if (robot.equals("B")) {
delete(buttonB,"B");
if (moveB>=moveO) {//O가 움직인 시간동안 갈 수 있음
locationO = buttonO;
} else{ //가까운쪽으로 움직이기
if (locationO > buttonO)
locationO -= (moveB + 1);
else if (locationO < buttonO)
locationO += (moveB + 1);
}
}else{
delete(buttonO,"O");
if (moveO>=moveB) { //B가 움직인 시간동안 갈 수 있음
locationB = buttonB;
} else{
if (locationB > buttonB)
locationB -= (moveO + 1);
else if (locationB < buttonB)
locationB += (moveO + 1);
}
}
}
}
sb.append("#"+test_case+" "+time).append('\n');
}
System.out.print(sb);
}
static void delete(int button,String s) {
if(s.equals("B")) {
time += (Math.abs(locationB-button)+ 1);
locationB = button;
B.remove(0);
}
else {
time += (Math.abs(locationO-button)+ 1);
locationO = button;
O.remove(0);
}
}
}
처음에 문제 이해부터 헤맸다
그리고 테케 5개만 맞았다해서 당황했다..^^
그냥 다 틀린거잖아요~..
그래서 여러 풀이들을 보고.......
나는 B랑 O랑 ArrayList 따로 만들어서 푸는 것을 택했다....