Loading...

[백준] 5430번 AC (C++ , DEQUE)

뒤집힌 상태를 나타내는 BOOL 변수 HEAD를 가지고 뒤집힌 상태면 뒤를 삭제하고 아니면 앞을 삭제한다. 속도를 위해 deque을 사용한다. #define _CRT_SECURE_NO_DEPRECATE #include #include #include using namespace std; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); freopen("input.txt","r",stdin); int t,n; cin >> t; string p; for (int tc = 0; tc > p>>n; string arr; cin >> arr; string tmp = ""; for (int i = 0; i ..

[백준] 1021번 회전하는 큐 (C++, DEQUE)

왼쪽으로 빼는게 더 적은지 오른쪽으로 빼는게 더 빠른지 본 뒤 해당 위치로 이동시켜준다 (덱 사용) 뒤로 빼는 경우 맨 뒤 위치에서 앞으로 이동시켜주는 것까지 포함이라 (앞에서만 빼므로) +1 해준다. #define _CRT_SECURE_NO_DEPRECATE #include #include #include #include #include using namespace std; int n; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); int n,m,x; deque dq; cin >> n >> m; for (int i = 0; i < n; i++) { dq.push_back(i+1); } int sum = 0; for (int i = 0; i ..

[백준] 6198번 옥상 정원 꾸미기 (stack, c++)

스택에는 현재 인덱스 i를 집어넣는다. 그 전에 스택이 비어있지 않으면 스택의 최상단(위치 prev)과 현재 높이(위치 i)의 차이 -1(자기자신은 빼줌) 개수 만큼의 빌딩을 볼 수 있다는 이야기이다. n=80000이고 자기 위치 다음 빌딩들이 모두 자기보다 작다하면 80000+ 79999 + 79998 +... 이런 예시가 들어온다 대략 n^2으로 계산하면 6.4*10^9 으로 int 범위가 넘어가므로 ans는 long long 형으로 해준다. #include #include #include #include using namespace std; int n; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; vector v(n)..

[SWEXPERT] 2814. 최장 경로 (C++, D3)

어느 점에서 시작하는게 가장 긴 거리인지 모르므로 모든 점을 다 돌려본다. DFS로 최대 들어갈 수 있는 깊이까지 들어간다고 보면 된다. BFS써도 될거 같긴 하다. #define _CRT_SECURE_NO_DEPRECATE #include #include #include #include using namespace std; int n, m,t; vector v[11]; bool vis[11]; int ans; void dfs(int cnt,int num) { if (cnt > ans)ans = cnt; for (int i = 0; i < v[num].size(); i++) { int next = v[num][i]; if (!vis[next]) { vis[next]=true; dfs(cnt + 1, ne..

[SWEXPERT] 1232. 사칙연산 (이진트리, C++, D4)

사칙연산 유효성 검사 풀고 풀었다가 완전 이진트리로 착각하여 입력을 이상하게 받아 5번 예제부터 자꾸 틀렸다. ㅠㅠ 아닌 걸 알고 입력부분 고쳐서 해결함. 연산자를 가지는 노드는 자식이 없고 연산자인 노드만 자식을 추가적으로 입력받는다. 나머지는 재귀로 중위 계산해준다. #define _CRT_SECURE_NO_DEPRECATE #include #include #include using namespace std; double tree[1001][2]; string oper[1001]; double inorder(int num) { if (tree[num][0]) {//자식있음 double a = inorder(tree[num][0]); //왼쪽 계산 double b = inorder(tree[num][1..

[SWEA] 1209. SUM2 (C++, D3)

왼쪽 대각선은 i==j 이고 오른쪽 대각선은 i+j=99이다. #define _CRT_SECURE_NO_DEPRECATE #include using namespace std; int arr[101][101]; int ans = 0; void go() { for (int i = 0; i ans)ans = sum; if (sum2 > ans)ans..

[SWEA] 1216. 회문2 (C++, D3)

#define _CRT_SECURE_NO_DEPRECATE #include #include using namespace std; string arr[100]; int ans = 1; bool rowCheck(int level,int left, int right) { //행 체크 중간에 왼쪽 오른쪽 다르면 빠져나온다 while (left

[swexpert] 2806. N-Queen (dfs, C++) [D3]

행을 이동하면서 각 열에 퀸을 놔본다. #include #include using namespace std; int t,n; int col[11];// 각 행에 놓는 열의 위치를 저장한다 int cnt; bool check(int row) { for (int i = 0; i < row; i++) { if (col[i] == col[row] || abs(col[row] - col[i]) == row - i) {// 기울기가 판단 return false; } } return true; } void queen(int row) { if (row == n) {//모든 퀸 다 놓음 cnt += 1; return; } for (int j = 0; j < n; j++) {//놓을 열을 선택 col[row] = j; /..

[swexpert] 1267. 작업순서 (C++, 위상정렬, dfs) [D6]

a -> b 방향으로 화살표가 있다면 a가 작업된 후에 b를 작업할 수 있다는 이야기 edge[a]에는 b를 추가해주고 cnt[b]는 1증가시킨다. (b로 들어오는 간선개수라고 생각해준다) 사전작업(간선)이 없는 곳, 즉 cnt[해당작업번호]=0인 곳부터 출력해준다. dfs의 경우는 방문 배열 처리를 해준다. bfs는 0인 곳을 큐에 먼저 다 넣어줄 수 있어서 필요없다. 현재 방문한 곳 cur와 연결된 곳의 간선의 수를 하나 줄여주고 (사전작업이 하나 줄었으니까!) 연결된 곳의 cnt[다음정점번호]가 0이 된다면 방문할 수 있다. #define _CRT_SECURE_NO_DEPRECATE #include #include #include #include #include using namespace std;..

[swea] 1265. 달란트2 (C++, 수학?)

묶음의 개수 P가 정해졌을 때 각 묶음의 달란트를 곱한 수가 크려면 각 묶음의 달란트가 최대한 골고루 분배되어야 된다는 것을 알 수 있다. 일단 달란드 개수 N/P는 각 묶음의 최소 달란트 개수로 초기화해준다. 그러면 N%P만큼의 분배되지 않은 달란트가 남을 것인데 해당 달란트들은 골고루 +1씩 해줘서 나눠준 후 곱하면 된다. #define _CRT_SECURE_NO_DEPRECATE #include #include #include #include using namespace std; int n, t, p; long long ans; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cin >> t; for (int tc = 1; tc > n >..