백준 알고리즘(C++)

백준 1700번 멀티탭 스케줄링 ( C++ )

coding232624 2024. 9. 4. 16:32

문제

https://www.acmicpc.net/problem/1700

 

해설

조건 적용하는것이 생각보다 까다로웠던 문제

입력이 적어서 3중 for문을 사용해 해결했음

코드

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
#include <iostream>
#include <algorithm>
#include <vector>
 
using namespace std;
int n, k, ret;
 
int main()
{
  cin >> n >> k;
  vector<int> list(k), mul(n, 0);

  for (int i = 0; i < k; i++)
  {
    cin >> list[i];
  }
 
  for (int i = 0; i < k; i++)
  {
    int flag = 0;
    for (int j = 0; j < n; j++)
    {
      if (mul[j] == 0)
      {
        mul[j] = list[i];
        flag = 1;
        break;
      }
      else if (mul[j] == list[i])
      {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
    {
      int last_mul = 0;
      int last_cnt = 0;
      for (int x = 0; x < n; x++)
      {
        for (int z = i + 1; z < k; z++)
        {
          if (list[z] == mul[x])
          {
            if (z > last_cnt)
            {
              last_cnt = z;
              last_mul = x;
            }
            break;
          }
          if (z == k - 1)
          {
            last_cnt = z + 1;
            last_mul = x;
          }
        }
      }
      mul[last_mul] = list[i];
      ret++;
    }
  }
 
  cout << ret;
  return 0;
}
 
cs