[백준 14569번 C/C++] 시간표 짜기
[백준 14569번 C/C++] 시간표 짜기

https://www.acmicpc.net/problem/14569
해결전략
비트마스킹 Bitmasking
정답코드
#include <iostream> #include <vector> using namespace std; int n; // 총 과목의 수 int m; // 학생의 수 vector<unsigned long long> lectures; // 각 과목의 시간표를 비트마스크로 저장할 벡터 // 시간표를 비트마스크로 변환하는 함수 unsigned long long ConvertToBitmask(const vector<int>& times) { unsigned long long binary = 0; for (int i = 0; i < times.size(); i++){ binary |= (1ULL << times[i]); // 해당 시간에 대응하는 비트를 1로 설정 } return binary; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; lectures.resize(n); for (int i = 0; i < n; i++) { int k; // 각 과목의 수업시간의 수 cin >> k; vector<int> times(k); for (int j = 0; j < k; j++){ cin >> times[j]; } lectures[i] = ConvertToBitmask(times); // 수업시간을 비트마스크로 변환하여 저장 } cin >> m; // 학생 수 for (int i = 0; i < m; i++) { int p; // (각 학생 당) 비어 있는 교시 개수 cin >> p; vector<int> students(p); for (int j = 0; j < p; j++){ cin >> students[j]; } unsigned long long studentBitmask = ConvertToBitmask(students); int cnt = 0; // 학생이 들을 수 있는 과목의 수 for (int j = 0; j < lectures.size(); j++) { // 학생의 비어 있는 교시와 과목의 수업시간을 비교하여 학생이 들을 수 있는지 확인 if ((studentBitmask & lectures[j]) == lectures[j]) { cnt++; } } cout << cnt << "\n"; // 각 학생이 들을 수 있는 과목의 수 출력 } return 0; }
'⭐ 코딩테스트 > 백준' 카테고리의 다른 글
[백준 2800번 C/C++] 괄호 제거 (0) | 2024.07.31 |
---|---|
[백준 15787번 C/C++] 기차가 어둠을 헤치고 은하수를 (0) | 2024.07.30 |
[백준 14503번 C/C++] 로봇 청소기 (0) | 2024.07.28 |
[백준 17135번 C/C++] 캐슬 디펜스 (0) | 2024.07.25 |
[백준 13335번 C/C++] 트럭 (2) | 2024.07.24 |
댓글
이 글 공유하기
다른 글
-
[백준 2800번 C/C++] 괄호 제거
[백준 2800번 C/C++] 괄호 제거
2024.07.31[백준 2800번 C/C++] 괄호 제거 https://www.acmicpc.net/problem/2800 해결전략 문자열재귀비트마스킹 Bitmasking 정답코드 1 - 비트마스킹 사용 #include #include #include #include using namespace std;string input; // 문제에서 주어진 문자열int n;vector> brackets; // 괄호 한 쌍의 위치를 저장set results; // 만들 수 있는 문자열을 기록// 괄호 쌍의 위치를 찾는 함수void FindBrackets(){ stack bracketLocation; // 문자열을 순회하며 괄호 쌍을 찾음 for (int i = 0; i ch){ if (idx == n){ resul… -
[백준 15787번 C/C++] 기차가 어둠을 헤치고 은하수를
[백준 15787번 C/C++] 기차가 어둠을 헤치고 은하수를
2024.07.30[백준 15787번 C/C++] 기차가 어둠을 헤치고 은하수를 https://www.acmicpc.net/problem/15787 해결전략 비트마스킹 BitMasking 정답코드 #include #include #include using namespace std;int n, m; // n: 기차의 수, m: 명령의 수int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; vector v(n, 0); for (int i = 0; i > orderIdx >> train; train--; // 0-based index로 변환 if (orderIdx == 1 || o… -
[백준 14503번 C/C++] 로봇 청소기
[백준 14503번 C/C++] 로봇 청소기
2024.07.28[백준 14503번 C/C++] 로봇 청소기 https://www.acmicpc.net/problem/14503 해결전략 구현 정답코드 #include #include using namespace std;int dirY[4] = { -1, 0, 1, 0 };int dirX[4] = { 0, 1, 0, -1 };int n, m;int currY, currX, currDir;vector> v;int answer; // 청소하는 칸의 개수bool Move(int y, int x, int dir){ // 4 방향 체크 for (int i = 0; i > n >> m; v.resize(n, vector(m)); cin >> currY >> currX >> currDir; for (int y = 0;… -
[백준 17135번 C/C++] 캐슬 디펜스
[백준 17135번 C/C++] 캐슬 디펜스
2024.07.25[백준 17135번 C/C++] 캐슬 디펜스 https://www.acmicpc.net/problem/17135 해결전략 구현 Brute Force, 너비우선탐색 BFS시뮬레이션 Simulation 정답 코드 #include #include #include #include #include using namespace std;// 방향 벡터 (좌, 상, 우)int dirY[3] = { 0, -1, 0 };int dirX[3] = { -1, 0, 1 };int n, m, d; // 행, 열, 궁수의 공격 거리int answer; // 제거할 수 있는 적의 최대 수vector archers; // 궁수의 위치vector> v; // 초기 적…
댓글을 사용할 수 없습니다.