Recent Posts
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 헥사고날아키텍처 #육각형아키텍처 #유스케이스
- table not found
- VARCHAR (1)
- tolerated
- PersistenceContext
- 오블완
- Spring Batch
- JanusGateway
- pytest
- JanusWebRTC
- kotlin
- mp4fpsmod
- JanusWebRTCServer
- 자원부족
- PytestPluginManager
- 코루틴 컨텍스트
- 달인막창
- python
- taint
- 개성국밥
- preemption #
- k8s #kubernetes #쿠버네티스
- vfr video
- 겨울 부산
- Value too long for column
- JanusWebRTCGateway
- terminal
- 티스토리챌린지
- 깡돼후
- 코루틴 빌더
Archives
너와 나의 스토리
[SW Expert Academy] 9708. 가장 긴 수열 D4 본문
반응형
문제 풀이:
1 ≤ i < K인 i에 대해 $a_i < a_{i+1}$ 이면서, $a_i$이 $a_{i+1}$의 약수여야 한다.
a0, a1, a2가 위 조건을 만족한다면,
a0*k=a1
a0*r=a2 이다.
즉,
a0를 포함하려면, 각 값들은 a0의 배수인 것!
for문을 돌면서, 현재 arr [i] 값을 약수로 두는 값을 찾기 위해, a0*2, a0*3, a0*4 해가며 해당 위치에서 가능한 가장 긴 수열 길이를 증가시킨다.
소스 코드:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define inf 987654321
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int tc, n, res,arr[100001],dp[1000001];
void func(stack<int> st, int pos) {
res = max(res, int(st.size()));
if (pos == n) return;
if (st.empty()||arr[pos] % st.top() == 0) {
st.push(arr[pos]);
return func(st, pos + 1);
}
//선택 안 하기
func(st, pos + 1);
//전 값 지우고, 현재 값 넣기
while (!st.empty()) {
st.pop();
if (arr[pos] % st.top() == 0) {
st.push(arr[pos]);
return func(st, pos + 1);
}
}
st.push(arr[pos]);
return func(st, pos + 1);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> tc;
for (int i = 1; i <= tc; i++) {
cin >> n ;
stack<int> st;
res = 1;
memset(dp, 0, sizeof(dp));
for (int j = 0; j < n; j++) {
cin >> arr[j];
dp[arr[j]] = 1;
}
sort(arr, arr + n);
for (int i = 0; i < n; i++) {
int tmp = arr[i]+arr[i];
while (tmp <= arr[n - 1]) {
if (dp[tmp]&&tmp % arr[i] == 0) {
dp[tmp] = max(dp[tmp],dp[arr[i]] + 1);
res = max(res, dp[tmp]);
}
tmp += arr[i];
}
}
cout << "#" << i << " " << res<< '\n';
}
return 0;
}
반응형
'Algorithm > 기타' 카테고리의 다른 글
"2018 KAKAO BLIND RECRUITMENT" > [1차] 셔틀버스 (0) | 2020.10.09 |
---|---|
"2018 KAKAO BLIND RECRUITMENT" > [1차] 뉴스 클러스터링 (0) | 2020.10.09 |
[BOJ] 1740 거듭제곱 (0) | 2020.05.23 |
[BOJ] 5525번 IOIOI (0) | 2020.04.07 |
[BOJ] 1541 잃어버린 괄호 (0) | 2020.03.26 |