-
Notifications
You must be signed in to change notification settings - Fork 6
/
JIHANEOL.java
131 lines (120 loc) · 4.19 KB
/
JIHANEOL.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class JIHANEOL {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[] order = new int[K];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < K; i++) {
order[i] = Integer.parseInt(st.nextToken());
}
boolean[] use = new boolean[101];
int put = 0;
int ans = 0;
for (int i = 0; i < K; i++) {
int temp = order[i];
if (!use[temp]) { // 콘센트가 꽂혀있지 않은 경우
if (put < N) { // 콘센트를 꽂을 공간이 있는 경우
use[temp] = true;
put++;
} else { // 콘센트를 꽂을 공간이 없는 경우
ArrayList<Integer> arrList = new ArrayList<>();
for (int j = i; j < K; j++) { // 현재 꽂혀 있는 콘센트가 나중에도 사용되는지 확인.
if (use[order[j]] && !arrList.contains(order[j])) {
arrList.add(order[j]);
}
}
if (arrList.size() != N) { // 나중에도 사용되는 콘센트가 구멍의 개수보다 작을 경우.
for (int j = 0; j < use.length; j++) {
if (use[j] && !arrList.contains(j)) { // 그 콘센트를 제거.
use[j] = false;
break;
}
}
} else { // 현재 꽂혀 있는 모든 콘센트가 나중에도 사용될 경우
int remove = arrList.get(arrList.size() - 1); // 가장 마지막에 사용될 콘센트를 제거.
use[remove] = false;
}
use[temp] = true;
ans++;
}
}
}
bw.write(ans + "\n");
bw.flush();
bw.close();
br.close();
}
}
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.util.Arrays;
//import java.util.LinkedList;
//import java.util.Queue;
//import java.util.StringTokenizer;
//
//public class Main {
// static int N, M;
// static int[] tap;
// static boolean[] used=new boolean[101];
// static int answer;
//
// public static void main(String[] args) throws IOException {
//
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// String[] s = br.readLine().split(" ");
// N = Integer.valueOf(s[0]);
// M = Integer.valueOf(s[1]);
// tap = new int[M];
// StringTokenizer st = new StringTokenizer(br.readLine());
// for(int i=0; i<M; i++) {
// tap[i] = Integer.valueOf(st.nextToken());
// }
// int put=0;
// for(int i=0; i<M; i++) {
// int temp = tap[i];
// if(!used[temp]) { // 사용안하는 중
// if(put<N){
// used[temp]=true;// 콘서트가 꽃을 공간이 있는경우
// put++;
//
// }else { // 꽃을 공간이 없다.
// find(i, temp);
// }
// }
// used[temp]=true;
// }
// System.out.println(answer);
// }
// public static void find(int idx,int num) {
// boolean[] temp = new boolean[101];
// int c = 0;
// for(int i=idx+1; i<idx+N+1; i++) {
// if(i==M) break;
// if (used[tap[i]] && !temp[tap[i]]) {
// temp[i]=true;
// c++;
// }
// }
// if(c==N) { // 다 사용된다면.
// used[tap[idx+N]]=false;
// }else {
// for(int i=0; i<101; i++) { // 다 사용 안된다.
// if(used[i] && !temp[i]) {
// used[i]=false;
// break;
// }
// }
// }
// answer++;
// }
//}