From 7b6a1a907c0a7abe598a2425d46f378444a490cd Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 22 Jul 2021 17:34:19 +0900 Subject: [PATCH 001/213] Delete 4007_LinkedListWithDijkstra.cpp --- SWEA/4007_LinkedListWithDijkstra.cpp | 109 --------------------------- 1 file changed, 109 deletions(-) delete mode 100644 SWEA/4007_LinkedListWithDijkstra.cpp diff --git a/SWEA/4007_LinkedListWithDijkstra.cpp b/SWEA/4007_LinkedListWithDijkstra.cpp deleted file mode 100644 index 8078582..0000000 --- a/SWEA/4007_LinkedListWithDijkstra.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -#include -#include -#include -#include -using namespace std; -typedef pair pii; - -struct Node{ - int to; - int weight; - Node * next; - Node * tail; -}; - -Node node[2][550001]; -int nodeCount[2] = {0,0}; - -void init(){ - nodeCount[0] = 0; - nodeCount[1] = 0; -} - -void makeHead(int idx){ - node[idx][nodeCount[idx]].weight = -1; - node[idx][nodeCount[idx]].to = (nodeCount[idx]) + 1; - node[idx][nodeCount[idx]].tail = nullptr; - node[idx][nodeCount[idx]++].next = nullptr; -} - -void pushNum(int from, int to, int weight, int idx){ - node[idx][nodeCount[idx]].to = to; - node[idx][nodeCount[idx]].weight = weight; - node[idx][nodeCount[idx]].tail = nullptr; - node[idx][nodeCount[idx]++].next = nullptr; - Node * ptr = node[idx][from-1].tail; - if(!ptr){ - node[idx][from-1].next = &node[idx][(nodeCount[idx])-1]; - node[idx][from-1].tail = &node[idx][(nodeCount[idx])-1]; - }else{ - ptr->next = &node[idx][(nodeCount[idx])-1]; - node[idx][from-1].tail = &node[idx][(nodeCount[idx])-1]; - } -} - -//if flag == 0, graph1, flag == 1, graph2 -void dijkstra(int start, vector& dist,vector&visited, int idx){ - dist[start] = 0; - priority_queue, greater>pq; - pq.push({0, start}); - while(!pq.empty()){ - int curNode = pq.top().second; - int curCost = pq.top().first; - pq.pop(); - visited[curNode] = true; - Node * tmp = node[idx][curNode-1].next; - while(tmp){ - int nextNode = tmp->to; - if(visited[nextNode]){ - tmp = tmp->next; - continue; - } - int nextCost = tmp->weight + curCost; - if(nextCost < dist[nextNode]){ - dist[nextNode] = nextCost; - pq.push({nextCost, nextNode}); - } - tmp = tmp->next; - } - } -} - -int main(){ - cin.tie(); - ios_base::sync_with_stdio(false); - int Tc; - cin>>Tc; - for(int tc=1; tc<=Tc; ++tc){ - init(); - int N, M, X, u, v, w; - cin>>N>>M>>X; - for(int i=0;i>u>>v>>w; - //two graphs - pushNum(u, v, w, 0); - pushNum(v, u, w, 1); - } - vectordist1(N+1, 2e9); - vectorvisited(N+1, false); - dijkstra(X, dist1,visited, 0); - vectorvisited2(N+1, false); - vectordist2(N+1, 2e9); - dijkstra(X, dist2,visited2, 1); - int ans = 0; - for(int i=1;i Date: Fri, 23 Jul 2021 18:35:57 +0900 Subject: [PATCH 002/213] SWEA --- SWEA/1244_greedy_bruteForce.cpp | 98 +++++++++++++++++++++++++++++++++ SWEA/1970_greedy.cpp | 77 ++++++++++++++++++++++++++ SWEA/3282_knapsack.cpp | 81 +++++++++++++++++++++++++++ SWEA/3304_dp.cpp | 83 ++++++++++++++++++++++++++++ SWEA/4408_greedy.cpp | 88 +++++++++++++++++++++++++++++ 5 files changed, 427 insertions(+) create mode 100644 SWEA/1244_greedy_bruteForce.cpp create mode 100644 SWEA/1970_greedy.cpp create mode 100644 SWEA/3282_knapsack.cpp create mode 100644 SWEA/3304_dp.cpp create mode 100644 SWEA/4408_greedy.cpp diff --git a/SWEA/1244_greedy_bruteForce.cpp b/SWEA/1244_greedy_bruteForce.cpp new file mode 100644 index 0000000..a95e965 --- /dev/null +++ b/SWEA/1244_greedy_bruteForce.cpp @@ -0,0 +1,98 @@ +// 기본 제공코드는 임의 수정해도 관계 없습니다. 단, 입출력 포맷 주의 +// 아래 표준 입출력 예제 필요시 참고하세요. +// 표준 입력 예제 +// int a; +// float b, c; +// double d, e, f; +// char g; +// char var[256]; +// long long AB; +// cin >> a; // int 변수 1개 입력받는 예제 +// cin >> b >> c; // float 변수 2개 입력받는 예제 +// cin >> d >> e >> f; // double 변수 3개 입력받는 예제 +// cin >> g; // char 변수 1개 입력받는 예제 +// cin >> var; // 문자열 1개 입력받는 예제 +// cin >> AB; // long long 변수 1개 입력받는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// +// 표준 출력 예제 +// int a = 0; +// float b = 1.0, c = 2.0; +// double d = 3.0, e = 0.0; f = 1.0; +// char g = 'b'; +// char var[256] = "ABCDEFG"; +// long long AB = 12345678901234567L; +// cout << a; // int 변수 1개 출력하는 예제 +// cout << b << " " << c; // float 변수 2개 출력하는 예제 +// cout << d << " " << e << " " << f; // double 변수 3개 출력하는 예제 +// cout << g; // char 변수 1개 출력하는 예제 +// cout << var; // 문자열 1개 출력하는 예제 +// cout << AB; // long long 변수 1개 출력하는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include + +using namespace std; + +int ans = 0; +int N, K, lenN; + +//N : 이전 값, cnt : 먗반돌렸는지, +void solve(vectorv, int cnt, int src){ + if(cnt == K){ + int tmp = v[0]; + for(int i=1;i, 혹은 #include 가 필요합니다. + 단, 채점을 위해 코드를 제출하실 때에는 반드시 freopen 함수를 지우거나 주석 처리 하셔야 합니다. + */ + //freopen("input.txt", "r", stdin); + cin>>T; + /* + 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. + */ + + for(test_case = 1; test_case <= T; ++test_case) + { + ///////////////////////////////////////////////////////////////////////////////////////////// + cin>>N>>K; + vectornum; + while(N!=0){ + num.push_back(N%10); + N/=10; + } + ans = 0; + reverse(num.begin(), num.end()); + lenN = num.size(); + if(lenN < K) K = lenN; + solve(num, 0, 0); + cout<<"#"<> a; // int 변수 1개 입력받는 예제 +// cin >> b >> c; // float 변수 2개 입력받는 예제 +// cin >> d >> e >> f; // double 변수 3개 입력받는 예제 +// cin >> g; // char 변수 1개 입력받는 예제 +// cin >> var; // 문자열 1개 입력받는 예제 +// cin >> AB; // long long 변수 1개 입력받는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// +// 표준 출력 예제 +// int a = 0; +// float b = 1.0, c = 2.0; +// double d = 3.0, e = 0.0; f = 1.0; +// char g = 'b'; +// char var[256] = "ABCDEFG"; +// long long AB = 12345678901234567L; +// cout << a; // int 변수 1개 출력하는 예제 +// cout << b << " " << c; // float 변수 2개 출력하는 예제 +// cout << d << " " << e << " " << f; // double 변수 3개 출력하는 예제 +// cout << g; // char 변수 1개 출력하는 예제 +// cout << var; // 문자열 1개 출력하는 예제 +// cout << AB; // long long 변수 1개 출력하는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// + +#include + +using namespace std; + +int main(int argc, char** argv) +{ + int test_case; + int T; + /* + 아래의 freopen 함수는 input.txt 를 read only 형식으로 연 후, + 앞으로 표준 입력(키보드) 대신 input.txt 파일로부터 읽어오겠다는 의미의 코드입니다. + //여러분이 작성한 코드를 테스트 할 때, 편의를 위해서 input.txt에 입력을 저장한 후, + freopen 함수를 이용하면 이후 cin 을 수행할 때 표준 입력 대신 파일로부터 입력을 받아올 수 있습니다. + 따라서 테스트를 수행할 때에는 아래 주석을 지우고 이 함수를 사용하셔도 좋습니다. + freopen 함수를 사용하기 위해서는 #include , 혹은 #include 가 필요합니다. + 단, 채점을 위해 코드를 제출하실 때에는 반드시 freopen 함수를 지우거나 주석 처리 하셔야 합니다. + */ + //freopen("input.txt", "r", stdin); + cin>>T; + /* + 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. + */ + int arr[8] = {50000, 10000, 5000, 1000, 500, 100, 50, 10}; + for(test_case = 1; test_case <= T; ++test_case) + { + + ///////////////////////////////////////////////////////////////////////////////////////////// + int N; + cin>>N; + cout<<"#"<= arr[i]){ + cout<> a; // int 변수 1개 입력받는 예제 +// cin >> b >> c; // float 변수 2개 입력받는 예제 +// cin >> d >> e >> f; // double 변수 3개 입력받는 예제 +// cin >> g; // char 변수 1개 입력받는 예제 +// cin >> var; // 문자열 1개 입력받는 예제 +// cin >> AB; // long long 변수 1개 입력받는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// +// 표준 출력 예제 +// int a = 0; +// float b = 1.0, c = 2.0; +// double d = 3.0, e = 0.0; f = 1.0; +// char g = 'b'; +// char var[256] = "ABCDEFG"; +// long long AB = 12345678901234567L; +// cout << a; // int 변수 1개 출력하는 예제 +// cout << b << " " << c; // float 변수 2개 출력하는 예제 +// cout << d << " " << e << " " << f; // double 변수 3개 출력하는 예제 +// cout << g; // char 변수 1개 출력하는 예제 +// cout << var; // 문자열 1개 출력하는 예제 +// cout << AB; // long long 변수 1개 출력하는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include + +using namespace std; + + +int main(int argc, char** argv) +{ + int test_case; + int T; + /* + 아래의 freopen 함수는 input.txt 를 read only 형식으로 연 후, + 앞으로 표준 입력(키보드) 대신 input.txt 파일로부터 읽어오겠다는 의미의 코드입니다. + //여러분이 작성한 코드를 테스트 할 때, 편의를 위해서 input.txt에 입력을 저장한 후, + freopen 함수를 이용하면 이후 cin 을 수행할 때 표준 입력 대신 파일로부터 입력을 받아올 수 있습니다. + 따라서 테스트를 수행할 때에는 아래 주석을 지우고 이 함수를 사용하셔도 좋습니다. + freopen 함수를 사용하기 위해서는 #include , 혹은 #include 가 필요합니다. + 단, 채점을 위해 코드를 제출하실 때에는 반드시 freopen 함수를 지우거나 주석 처리 하셔야 합니다. + */ + //freopen("input.txt", "r", stdin); + cin>>T; + /* + 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. + */ + + for(test_case = 1; test_case <= T; ++test_case) + { + ///////////////////////////////////////////////////////////////////////////////////////////// + int N, K, c, v; + cin>>N>>K; + vectordp(1001); + for(int i=0;i>v>>c; + for(int j=K; j >= v; --j){ + dp[j] = max(dp[j-v] + c , dp[j]); + } + for(int j=0;j <= K; ++j){ + cout<> a; // int 변수 1개 입력받는 예제 +// cin >> b >> c; // float 변수 2개 입력받는 예제 +// cin >> d >> e >> f; // double 변수 3개 입력받는 예제 +// cin >> g; // char 변수 1개 입력받는 예제 +// cin >> var; // 문자열 1개 입력받는 예제 +// cin >> AB; // long long 변수 1개 입력받는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// +// 표준 출력 예제 +// int a = 0; +// float b = 1.0, c = 2.0; +// double d = 3.0, e = 0.0; f = 1.0; +// char g = 'b'; +// char var[256] = "ABCDEFG"; +// long long AB = 12345678901234567L; +// cout << a; // int 변수 1개 출력하는 예제 +// cout << b << " " << c; // float 변수 2개 출력하는 예제 +// cout << d << " " << e << " " << f; // double 변수 3개 출력하는 예제 +// cout << g; // char 변수 1개 출력하는 예제 +// cout << var; // 문자열 1개 출력하는 예제 +// cout << AB; // long long 변수 1개 출력하는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include + +using namespace std; + +int main(int argc, char** argv) +{ + int test_case; + int T; + /* + 아래의 freopen 함수는 input.txt 를 read only 형식으로 연 후, + 앞으로 표준 입력(키보드) 대신 input.txt 파일로부터 읽어오겠다는 의미의 코드입니다. + //여러분이 작성한 코드를 테스트 할 때, 편의를 위해서 input.txt에 입력을 저장한 후, + freopen 함수를 이용하면 이후 cin 을 수행할 때 표준 입력 대신 파일로부터 입력을 받아올 수 있습니다. + 따라서 테스트를 수행할 때에는 아래 주석을 지우고 이 함수를 사용하셔도 좋습니다. + freopen 함수를 사용하기 위해서는 #include , 혹은 #include 가 필요합니다. + 단, 채점을 위해 코드를 제출하실 때에는 반드시 freopen 함수를 지우거나 주석 처리 하셔야 합니다. + */ + //freopen("input.txt", "r", stdin); + cin>>T; + /* + 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. + */ + + for(test_case = 1; test_case <= T; ++test_case) + { + + ///////////////////////////////////////////////////////////////////////////////////////////// + string s1, s2; + cin>>s1>>s2; + s1 = '0' + s1; + s2 = '0' + s2; + int s1Length = s1.size(); + int s2Length = s2.size(); + vector>dp(s1Length, vector(s2Length, 0)); + for(int i=1; i> a; // int 변수 1개 입력받는 예제 +// cin >> b >> c; // float 변수 2개 입력받는 예제 +// cin >> d >> e >> f; // double 변수 3개 입력받는 예제 +// cin >> g; // char 변수 1개 입력받는 예제 +// cin >> var; // 문자열 1개 입력받는 예제 +// cin >> AB; // long long 변수 1개 입력받는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// +// 표준 출력 예제 +// int a = 0; +// float b = 1.0, c = 2.0; +// double d = 3.0, e = 0.0; f = 1.0; +// char g = 'b'; +// char var[256] = "ABCDEFG"; +// long long AB = 12345678901234567L; +// cout << a; // int 변수 1개 출력하는 예제 +// cout << b << " " << c; // float 변수 2개 출력하는 예제 +// cout << d << " " << e << " " << f; // double 변수 3개 출력하는 예제 +// cout << g; // char 변수 1개 출력하는 예제 +// cout << var; // 문자열 1개 출력하는 예제 +// cout << AB; // long long 변수 1개 출력하는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +using namespace std; + +int main(int argc, char** argv) +{ + int test_case; + int T; + /* + 아래의 freopen 함수는 input.txt 를 read only 형식으로 연 후, + 앞으로 표준 입력(키보드) 대신 input.txt 파일로부터 읽어오겠다는 의미의 코드입니다. + //여러분이 작성한 코드를 테스트 할 때, 편의를 위해서 input.txt에 입력을 저장한 후, + freopen 함수를 이용하면 이후 cin 을 수행할 때 표준 입력 대신 파일로부터 입력을 받아올 수 있습니다. + 따라서 테스트를 수행할 때에는 아래 주석을 지우고 이 함수를 사용하셔도 좋습니다. + freopen 함수를 사용하기 위해서는 #include , 혹은 #include 가 필요합니다. + 단, 채점을 위해 코드를 제출하실 때에는 반드시 freopen 함수를 지우거나 주석 처리 하셔야 합니다. + */ + //freopen("input.txt", "r", stdin); + cin>>T; + /* + 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. + */ + + for(test_case = 1; test_case <= T; ++test_case) + { + + ///////////////////////////////////////////////////////////////////////////////////////////// + int N; + int tf, tt; + cin>>N; + vectorvisited(401, 0); + int ans = 0; + for(int i=0;i>tf>>tt; + if(tf > tt){ + tt ^= tf; + tf ^= tt; + tt ^= tf; + } + tf = (tf+1)/2; + tt = (tt+1)/2; + for(int i= tf; i Date: Fri, 23 Jul 2021 19:58:27 +0900 Subject: [PATCH 003/213] BOJ --- BaekJoon/2223_dp.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 BaekJoon/2223_dp.cpp diff --git a/BaekJoon/2223_dp.cpp b/BaekJoon/2223_dp.cpp new file mode 100644 index 0000000..a49dd46 --- /dev/null +++ b/BaekJoon/2223_dp.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +#define MOD 1000000000 + +int main(){ + //dp[i][j] : i개를 이용해 j를 만들 수 있는 경우의 수 + vector>dp(201, vector(201, 0)); + int N, M; + cin>>N>>M; + for(int i=0;i<201; ++i) { + dp[0][i] = 1; + dp[i][0] = 1; + } + for(int i=1;i Date: Fri, 23 Jul 2021 20:00:11 +0900 Subject: [PATCH 004/213] Delete 2223_dp.cpp --- BaekJoon/2223_dp.cpp | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 BaekJoon/2223_dp.cpp diff --git a/BaekJoon/2223_dp.cpp b/BaekJoon/2223_dp.cpp deleted file mode 100644 index a49dd46..0000000 --- a/BaekJoon/2223_dp.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include -using namespace std; -#define MOD 1000000000 - -int main(){ - //dp[i][j] : i개를 이용해 j를 만들 수 있는 경우의 수 - vector>dp(201, vector(201, 0)); - int N, M; - cin>>N>>M; - for(int i=0;i<201; ++i) { - dp[0][i] = 1; - dp[i][0] = 1; - } - for(int i=1;i Date: Sat, 24 Jul 2021 19:47:48 +0900 Subject: [PATCH 005/213] BOJ --- BaekJoon/1339_greedy.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 BaekJoon/1339_greedy.cpp diff --git a/BaekJoon/1339_greedy.cpp b/BaekJoon/1339_greedy.cpp new file mode 100644 index 0000000..8632a6b --- /dev/null +++ b/BaekJoon/1339_greedy.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int main(){ + int N; + cin>>N; + vectors; + vector>v(26); + for(int i=0;i<26;++i) + v[i].second = i; + s.resize(N); + for(int i=0;i>s[i]; + for(int j=0;jm; + int num =9; + for(int i=0;i<26; ++i){ + m[char('A' + v[i].second)] = num--; + } + int ans =0; + for(int i=0;i Date: Sun, 25 Jul 2021 19:48:17 +0900 Subject: [PATCH 006/213] BOJ --- BaekJoon/9465.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/9465.cpp diff --git a/BaekJoon/9465.cpp b/BaekJoon/9465.cpp new file mode 100644 index 0000000..9f0d892 --- /dev/null +++ b/BaekJoon/9465.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int main(){ + int Tc; + cin>>Tc; + for(int tc =0;tc>N; + vector>v(2, vector(N)); + vector>dp(2, vector(N)); + for(int i=0;i<2; ++i){ + for(int j=0;j>v[i][j]; + } + } + dp[0][0] = v[0][0]; + dp[1][0] = v[1][0]; + dp[0][1] = v[0][1] + v[1][0]; + dp[1][1] = v[1][1] + v[0][0]; + for(int i=2; i Date: Mon, 26 Jul 2021 19:17:09 +0900 Subject: [PATCH 007/213] SWEA --- SWEA/6960_dp.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 SWEA/6960_dp.cpp diff --git a/SWEA/6960_dp.cpp b/SWEA/6960_dp.cpp new file mode 100644 index 0000000..39092a4 --- /dev/null +++ b/SWEA/6960_dp.cpp @@ -0,0 +1,93 @@ +///////////////////////////////////////////////////////////////////////////////////////////// +// 기본 제공코드는 임의 수정해도 관계 없습니다. 단, 입출력 포맷 주의 +// 아래 표준 입출력 예제 필요시 참고하세요. +// 표준 입력 예제 +// int a; +// float b, c; +// double d, e, f; +// char g; +// char var[256]; +// long long AB; +// cin >> a; // int 변수 1개 입력받는 예제 +// cin >> b >> c; // float 변수 2개 입력받는 예제 +// cin >> d >> e >> f; // double 변수 3개 입력받는 예제 +// cin >> g; // char 변수 1개 입력받는 예제 +// cin >> var; // 문자열 1개 입력받는 예제 +// cin >> AB; // long long 변수 1개 입력받는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// +// 표준 출력 예제 +// int a = 0; +// float b = 1.0, c = 2.0; +// double d = 3.0, e = 0.0; f = 1.0; +// char g = 'b'; +// char var[256] = "ABCDEFG"; +// long long AB = 12345678901234567L; +// cout << a; // int 변수 1개 출력하는 예제 +// cout << b << " " << c; // float 변수 2개 출력하는 예제 +// cout << d << " " << e << " " << f; // double 변수 3개 출력하는 예제 +// cout << g; // char 변수 1개 출력하는 예제 +// cout << var; // 문자열 1개 출력하는 예제 +// cout << AB; // long long 변수 1개 출력하는 예제 +///////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +using namespace std; +typedef pair pii; + +int main(int argc, char** argv) +{ + int test_case; + int T; + /* + 아래의 freopen 함수는 input.txt 를 read only 형식으로 연 후, + 앞으로 표준 입력(키보드) 대신 input.txt 파일로부터 읽어오겠다는 의미의 코드입니다. + //여러분이 작성한 코드를 테스트 할 때, 편의를 위해서 input.txt에 입력을 저장한 후, + freopen 함수를 이용하면 이후 cin 을 수행할 때 표준 입력 대신 파일로부터 입력을 받아올 수 있습니다. + 따라서 테스트를 수행할 때에는 아래 주석을 지우고 이 함수를 사용하셔도 좋습니다. + freopen 함수를 사용하기 위해서는 #include , 혹은 #include 가 필요합니다. + 단, 채점을 위해 코드를 제출하실 때에는 반드시 freopen 함수를 지우거나 주석 처리 하셔야 합니다. + */ + //freopen("input.txt", "r", stdin); + cin>>T; + /* + 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. + */ + for(test_case = 1; test_case <= T; ++test_case) + { + int N; + cin>>N; + vectorv(N+1); + vector>dp(N+1, vector(N+1, 0)); + v[0] = {0,0}; + for(int i=0; i>v[i].second>>v[i].first; + } + sort(v.begin(), v.end()); + for(int i=1;i<=N; ++i){ + for(int j=1;j<=i; ++j){ + dp[i][j] = min(dp[i-1][j], dp[i-1][j-1] + v[i].second); + if(dp[i][j] > v[i].first) dp[i][j] = 2e9; + } + } +// for(int i=0;i<=N; ++i){ +// for(int j=0;j<=N; ++j){ +// cout< Date: Tue, 27 Jul 2021 23:57:11 +0900 Subject: [PATCH 008/213] BOJ --- BaekJoon/1517_mergeSort.cpp | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 BaekJoon/1517_mergeSort.cpp diff --git a/BaekJoon/1517_mergeSort.cpp b/BaekJoon/1517_mergeSort.cpp new file mode 100644 index 0000000..0ebd2ce --- /dev/null +++ b/BaekJoon/1517_mergeSort.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long ll; +const int MAX = 500001; +int a[MAX]; +int sorted[MAX]; +ll cnt =0; +void merge(int s, int m, int e){ + int i, j, k; + i = s; j = m+1; k = s; + while(i<=m && j<=e){ + if(a[i] <= a[j]){ + sorted[k++] = a[i++]; + }else{ + cnt += (m - i + 1); + sorted[k++] = a[j++]; + } + } + if(i > m){ + while(j <= e){ + sorted[k++] = a[j++]; + } + }else{ + while(i<=m){ + sorted[k++] = a[i++]; + } + } + for(i = s; i<=e; ++i){ + a[i] = sorted[i]; + } +} +void mergesort(int s, int e){ + int m; + if(s Date: Tue, 27 Jul 2021 23:57:17 +0900 Subject: [PATCH 009/213] SWEA --- SWEA/1248_LCA_SubTree.cpp | 94 +++++++++++++++++++++++++++++++++++++++ SWEA/7701_sort.cpp | 45 +++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 SWEA/1248_LCA_SubTree.cpp create mode 100644 SWEA/7701_sort.cpp diff --git a/SWEA/1248_LCA_SubTree.cpp b/SWEA/1248_LCA_SubTree.cpp new file mode 100644 index 0000000..9b907ad --- /dev/null +++ b/SWEA/1248_LCA_SubTree.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +using namespace std; +typedef pair pii; +#define MAXD 21 +vector>parent(MAXD, vector(10001, 0)); +vector>graph(10001); +vectordepth(10001, 0); + +int lca(int a, int b){ + //a의 깊이가 더 깊게 만들기 + if(depth[a] < depth[b]){ + a^=b; + b^=a; + a^=b; + } + + //깊이 맞춰주기 + for(int i = MAXD-1; i>=0; --i){ + if(depth[a] - depth[b] >= 1<=0; --i){ + if(parent[i][a] != parent[i][b]){ + a = parent[i][a]; + b = parent[i][b]; + } + } + return parent[0][a]; +} + +int numSubtree(int node){ + int ans = 1; + for(int i=0;i>T; + + for(test_case = 1; test_case <= T; ++test_case) + { + for(int i=0;i>V>>E>>Qa>>Qb; + for(int i=0; i>a1>>a2; +// cout<depth + int ansLca = lca(Qa, Qb); + int subTree = numSubtree(ansLca); + cout< +#include +#include +#include +using namespace std; +typedef pair pii; + + +bool compare(string s1, string s2){ + if(s1.size() == s2.size()){ + return s1>T; + + for(test_case = 1; test_case <= T; ++test_case) + { + int N; + cin>>N; + vectors(N); + for(int i=0;i>s[i]; + } + sort(s.begin(), s.end(), compare); + cout<<"#"< Date: Wed, 28 Jul 2021 23:58:12 +0900 Subject: [PATCH 010/213] SWEA --- BaekJoon/13164_greedy.cpp | 22 +++++++ BaekJoon/16120_greedy.cpp | 42 ++++++++++++ BaekJoon/2517_mergeSort.cpp | 123 ++++++++++++++++++++++++++++++++++++ SWEA/1248_LCA_SubTree.cpp | 94 +++++++++++++++++++++++++++ SWEA/7701_sort.cpp | 45 +++++++++++++ 5 files changed, 326 insertions(+) create mode 100644 BaekJoon/13164_greedy.cpp create mode 100644 BaekJoon/16120_greedy.cpp create mode 100644 BaekJoon/2517_mergeSort.cpp create mode 100644 SWEA/1248_LCA_SubTree.cpp create mode 100644 SWEA/7701_sort.cpp diff --git a/BaekJoon/13164_greedy.cpp b/BaekJoon/13164_greedy.cpp new file mode 100644 index 0000000..9be0a84 --- /dev/null +++ b/BaekJoon/13164_greedy.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +int main(){ + int N, K; + scanf("%d%d", &N, &K); + vectorv(N); + vectortmp(N, 2e9); + scanf("%d", &v[0]); + for(int i=1; i +using namespace std; + +int main(){ + cin.tie(); + ios_base::sync_with_stdio(false); + dequedq; + string s; + bool prevA = false; + bool flag = true; + cin>>s; + for(int i=0;i +using namespace std; + +struct run{ + int val; + int idx; +}; + +vectorv(500001); +vectorsorted(500001); +vectormax_(500001); + +void mergeSort(int s, int m, int e){ + int i = s, j = m+1, k = s; + while(i <= m && j <= e){ + if(v[i].val >= v[j].val){ + sorted[k++] = v[i++]; + }else{ + max_[v[j].idx] -= m - i + 1; + sorted[k++] = v[j++]; + } + } + if(i > m){ + while(j <= e){ + sorted[k++] = v[j++]; + } + }else{ + while(i <= m){ + sorted[k++] = v[i++]; + } + } + for(i = s; i<=e; ++i){ + v[i] = sorted[i]; + } +} + +void merge(int s, int e){ + int mid; + if(s +using namespace std; + +struct run{ + int val; + int idx; +}; + +vectorv(500001); +vectorsorted(500001); +vectormax_(500001); + +void mergeSort(int s, int m, int e){ + int i = s, j = m+1, k = s; + while(i <= m && j <= e){ + if(v[i].val >= v[j].val){ + sorted[k++] = v[i++]; + }else{ + max_[v[j].idx] -= m - i + 1; + sorted[k++] = v[j++]; + } + } + if(i > m){ + while(j <= e){ + sorted[k++] = v[j++]; + } + }else{ + while(i <= m){ + sorted[k++] = v[i++]; + } + } + for(i = s; i<=e; ++i){ + v[i] = sorted[i]; + } +} + +void merge(int s, int e){ + int mid; + if(s +#include +#include +#include +using namespace std; +typedef pair pii; +#define MAXD 21 +vector>parent(MAXD, vector(10001, 0)); +vector>graph(10001); +vectordepth(10001, 0); + +int lca(int a, int b){ + //a의 깊이가 더 깊게 만들기 + if(depth[a] < depth[b]){ + a^=b; + b^=a; + a^=b; + } + + //깊이 맞춰주기 + for(int i = MAXD-1; i>=0; --i){ + if(depth[a] - depth[b] >= 1<=0; --i){ + if(parent[i][a] != parent[i][b]){ + a = parent[i][a]; + b = parent[i][b]; + } + } + return parent[0][a]; +} + +int numSubtree(int node){ + int ans = 1; + for(int i=0;i>T; + + for(test_case = 1; test_case <= T; ++test_case) + { + for(int i=0;i>V>>E>>Qa>>Qb; + for(int i=0; i>a1>>a2; +// cout<depth + int ansLca = lca(Qa, Qb); + int subTree = numSubtree(ansLca); + cout< +#include +#include +#include +using namespace std; +typedef pair pii; + + +bool compare(string s1, string s2){ + if(s1.size() == s2.size()){ + return s1>T; + + for(test_case = 1; test_case <= T; ++test_case) + { + int N; + cin>>N; + vectors(N); + for(int i=0;i>s[i]; + } + sort(s.begin(), s.end(), compare); + cout<<"#"< Date: Thu, 29 Jul 2021 15:37:59 +0900 Subject: [PATCH 011/213] SWEA --- SWEA/10507_twoPointer.cpp | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 SWEA/10507_twoPointer.cpp diff --git a/SWEA/10507_twoPointer.cpp b/SWEA/10507_twoPointer.cpp new file mode 100644 index 0000000..02b48dd --- /dev/null +++ b/SWEA/10507_twoPointer.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +using namespace std; + +int main(){ + int Tc; + scanf("%d", &Tc); + for(int tc =0; tcv(N); + for(int i=0;ivisited(max_+M+1, false); + for(int i=0;i Date: Thu, 29 Jul 2021 15:38:17 +0900 Subject: [PATCH 012/213] SWEA --- SWEA/1251_UnionFind.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 SWEA/1251_UnionFind.cpp diff --git a/SWEA/1251_UnionFind.cpp b/SWEA/1251_UnionFind.cpp new file mode 100644 index 0000000..808b4c3 --- /dev/null +++ b/SWEA/1251_UnionFind.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long long ll; +typedef pair pll; + +struct edge{ + double cost; + int x, y; +}; + +bool cmp(edge a, edge b){ + return a.cost < b.cost; +} + +vectoredges; +vectorv; +vectorparent; + +int getParent(int n){ + if(parent[n] == n) return n; + return parent[n] = getParent(parent[n]); +} + +int unionParent(int n, int m){ + int a = getParent(n); + int b = getParent(m); + if(a > b){ + parent[a] = b; + }else{ + parent[b] = a; + } +} + +int main(){ + int tc; + scanf("%d", &tc); + for(int testCase = 1; testCase <= tc; ++testCase){ + int N; + double e; + double ans = 0; + scanf("%d", &N); + v.clear(); + v.resize(N); + parent.clear(); + parent.resize(N); + edges.clear(); + for(int i=0;i Date: Thu, 29 Jul 2021 15:38:23 +0900 Subject: [PATCH 013/213] SWEA --- SWEA/1868_DFS.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 SWEA/1868_DFS.cpp diff --git a/SWEA/1868_DFS.cpp b/SWEA/1868_DFS.cpp new file mode 100644 index 0000000..b62fb8c --- /dev/null +++ b/SWEA/1868_DFS.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +using namespace std; +vector>map; +vector>visited; +int ans = 0; +int N; + +const int dx[] = {1,-1,0,0,1,1,-1,-1}; +const int dy[] = {0,0,-1,1,-1,1,1,-1}; + +void dfs(int i, int j){ + for(int k=0;k<8; ++k){ + int nx = i + dx[k]; + int ny = j + dy[k]; + if(nx >=0 && nx < N && ny >= 0 && ny < N){ + if(!visited[nx][ny]){ + visited[nx][ny] = true; + if(map[nx][ny] == 0){ + dfs(nx, ny); + } + } + } + } +} + +int main(){ + int tc; + cin>>tc; + + for(int testCase = 1; testCase >N; + for(int i=0; i(N, 0)); + visited.resize(N, vector(N, false)); + vectors; + for(int i=0;i>tmp; + s.push_back(tmp); + } + int nx; + int ny; + //calculate + for(int i=0;i= 0 && nx < N && ny >= 0 && ny < N){ + if(s[nx][ny] == '*'){ + map[i][j] += 1; + } + } + } + }else{ + map[i][j] = -1; + visited[i][j] = true; + } + } + } + ans =0; + for(int i=0;i Date: Thu, 29 Jul 2021 15:38:28 +0900 Subject: [PATCH 014/213] SWEA --- SWEA/1461_DFS.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 SWEA/1461_DFS.cpp diff --git a/SWEA/1461_DFS.cpp b/SWEA/1461_DFS.cpp new file mode 100644 index 0000000..cbc935c --- /dev/null +++ b/SWEA/1461_DFS.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +using namespace std; +vector>processor; +vector>map(13, vector(13)); + +int N, M; +int ans = 0; +int maxCon = 0; +int dx[] = {1,-1,0,0}; +int dy[] = {0,0,1,-1}; +// n : processor number +void dfs(int n,int connect, int curCnt){ + if(n == M){ +// printf("curCnt : %d, connect : %d\n", curCnt, connect); +// +// for(int i=0;i, 혹은 #include 가 필요합니다. + 단, 채점을 위해 코드를 제출하실 때에는 반드시 freopen 함수를 지우거나 주석 처리 하셔야 합니다. + */ + //freopen("input.txt", "r", stdin); + scanf("%d", &T); + /* + 여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다. + */ + for(test_case = 1; test_case <= T; ++test_case) + { + maxCon = 0; + ans = 0; + processor.clear(); + scanf("%d", &N); + M = 0; + for(int i=0;i Date: Thu, 29 Jul 2021 15:38:36 +0900 Subject: [PATCH 015/213] SWEA --- SWEA/4006_UnionFund.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 SWEA/4006_UnionFund.cpp diff --git a/SWEA/4006_UnionFund.cpp b/SWEA/4006_UnionFund.cpp new file mode 100644 index 0000000..75981e0 --- /dev/null +++ b/SWEA/4006_UnionFund.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct edge{ + int x, y, cost; +}; +bool cmp(edge a, edge b){ + return a.cost < b.cost; +} +vectorparent; +vectoredges; + +int getParent(int n){ + if(parent[n] == n) return n; + else parent[n] = getParent(parent[n]); +} + +int unionParent(int n, int m){ + int a = getParent(n); + int b = getParent(m); + if(a > b){ + parent[a] = b; + } + else{ + parent[b] = a; + } +} + +int main(){ + int tc; + scanf("%d", &tc); + for(int Tc = 0;Tc < tc; ++Tc){ + int ans =0; + int N, E, tx, ty, cost; + scanf("%d%d", &N, &E); + parent.clear(); + parent.resize(N+1); + edges.clear(); + for(int i=0; i Date: Fri, 30 Jul 2021 19:45:52 +0900 Subject: [PATCH 016/213] SWEA --- SWEA/1855_LCA_BFS.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 SWEA/1855_LCA_BFS.cpp diff --git a/SWEA/1855_LCA_BFS.cpp b/SWEA/1855_LCA_BFS.cpp new file mode 100644 index 0000000..d02e9b1 --- /dev/null +++ b/SWEA/1855_LCA_BFS.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +typedef pair pii; +typedef long long ll; +#define MAX 100005 +#define MAXD 20 +//vector가 너무 느림 +int parent[MAXD][MAX]; +int depth[MAX]; +bool visited[MAX]; +vector>graph(MAX); + +int lca(int a, int b){ + //a의 깊이가 더 깊게 만들기 + if(depth[a] < depth[b]){ + a^=b; + b^=a; + a^=b; + } + //깊이 맞춰주기 + for(int i = MAXD-1; i>=0; --i){ + if(depth[a] - depth[b] >= 1<=0; --i){ + if(parent[i][a] != parent[i][b]){ + a = parent[i][a]; + b = parent[i][b]; + } + } + return parent[0][a]; +} + + +int main(int argc, char** argv) +{ + int test_case; + int T; + + scanf("%d", &T); + + for(test_case = 1; test_case <= T; ++test_case) + { + + int N, tmp; + scanf("%d", &N); + memset(parent, 0, sizeof(parent)); + memset(depth, 0, sizeof(depth)); + memset(visited, 0, sizeof(visited)); + graph.clear(); + graph.resize(N+5); + + for(int i=1; i>dpi; + dpi.push_back({1,0}); + visited[1] = true; + while(!dpi.empty()){ + int cur = dpi.front().first; + int dpt = dpi.front().second; + dpi.pop_front(); + depth[cur] = dpt; + for(int next : graph[cur]){ + if(!visited[next]){ + visited[next] = true; + dpi.push_back({next, dpt+1}); + } + } + } + + for(int i=1; idq; + vectorsearch; + dq.push_back(1); + visited[1] = true; + while(!dq.empty()){ + int cur = dq.front(); + dq.pop_front(); + search.push_back(cur); + for(int next : graph[cur]){ + if(!visited[next]){ + visited[next] = true; + dq.push_back(next); + } + } + } + ll res = 0; + for(int i=0; i Date: Sat, 31 Jul 2021 17:33:48 +0900 Subject: [PATCH 017/213] BOJ --- BaekJoon/10090_mergeSort.cpp | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 BaekJoon/10090_mergeSort.cpp diff --git a/BaekJoon/10090_mergeSort.cpp b/BaekJoon/10090_mergeSort.cpp new file mode 100644 index 0000000..ee7ce17 --- /dev/null +++ b/BaekJoon/10090_mergeSort.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +#define MAXN 1000001 + +//Merge Sort +int A[MAXN + 10]; +int sorted[MAXN + 10]; +long long ans; + +void merge(int l, int m, int r) { + int i, j, k; + i = l; j = m + 1; k = l; + while (i <= m && j <= r) { + if (A[i] <= A[j]) + sorted[k++] = A[i++]; + else { + ans += (m-i+1); + sorted[k++] = A[j++]; + } + } + + // 남아있는 값들 일괄 복사 + if (i > m) { + while (j <= r) + sorted[k++] = A[j++]; + } + else { + while (i <= m) + sorted[k++] = A[i++]; + } + + // 배열 복사 + for (i = l; i <= r; i++) + A[i] = sorted[i]; +} + +void mergeSort(int l, int r) { + int m; + if (l < r) { + m = (l + r) / 2; + mergeSort(l, m); + mergeSort(m + 1, r); + merge(l, m, r); + } +} + +int main(){ + ans = 0; + int N; + scanf("%d", &N); + for(int i=0;i Date: Sat, 31 Jul 2021 17:33:54 +0900 Subject: [PATCH 018/213] SWEA --- SWEA/3998_mergeSort.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 SWEA/3998_mergeSort.cpp diff --git a/SWEA/3998_mergeSort.cpp b/SWEA/3998_mergeSort.cpp new file mode 100644 index 0000000..958d761 --- /dev/null +++ b/SWEA/3998_mergeSort.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +using namespace std; +#define MAXN 100001 + +//Merge Sort +int A[MAXN + 10]; +int sorted[MAXN + 10]; +long long ans; + +void merge(int l, int m, int r) { + int i, j, k; + i = l; j = m + 1; k = l; + while (i <= m && j <= r) { + if (A[i] <= A[j]) + sorted[k++] = A[i++]; + else { + ans += (m-i+1); + sorted[k++] = A[j++]; + } + } + + // 남아있는 값들 일괄 복사 + if (i > m) { + while (j <= r) + sorted[k++] = A[j++]; + } + else { + while (i <= m) + sorted[k++] = A[i++]; + } + + // 배열 복사 + for (i = l; i <= r; i++) + A[i] = sorted[i]; +} + +void mergeSort(int l, int r) { + int m; + if (l < r) { + m = (l + r) / 2; + mergeSort(l, m); + mergeSort(m + 1, r); + merge(l, m, r); + } +} + +int main(){ + int testCase; + scanf("%d", &testCase); + for(int tc = 1; tc <= testCase; ++tc){ + ans = 0; + int N; + memset(A, 0, sizeof(A)); + scanf("%d", &N); + for(int i=0;i Date: Sun, 1 Aug 2021 15:58:03 +0900 Subject: [PATCH 019/213] BOJ --- BaekJoon/13459_simulation_BFS.cpp | 100 ++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 BaekJoon/13459_simulation_BFS.cpp diff --git a/BaekJoon/13459_simulation_BFS.cpp b/BaekJoon/13459_simulation_BFS.cpp new file mode 100644 index 0000000..82d111d --- /dev/null +++ b/BaekJoon/13459_simulation_BFS.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; +typedef pair pii; +typedef struct Balls{ + int rX, rY, bX, bY, cnt; +}; +const int dx[4] = {-1,1,0,0}; +const int dy[4] = {0,0,-1,1}; + +int main(){ + int N,M; + string tmp; + cin>>N>>M; + vectorv; + dequedq; + Balls ball = {0,0,0,0,0}; + for(int i=0;i>tmp; + for(int j=0;jrDq; rDq.push_back(red); + dequebDq; bDq.push_back(blue); + while(!rDq.empty()){ + pii curBall = rDq.front();rDq.pop_front(); + int nx = curBall.first + dx[i]; + int ny = curBall.second + dy[i]; + if(nx>=0 && nx < N && ny>=0 && ny < M && v[nx][ny] != '#'){ + cur.rX = nx; + cur.rY = ny; + dqFlag = true; + if(v[nx][ny]=='.'){ + rDq.push_back({nx, ny}); + }else if(v[nx][ny] == 'O'){ + flag = true; + } + } + } + while(!bDq.empty()){ + pii curBall = bDq.front();bDq.pop_front(); + int nx = curBall.first + dx[i]; + int ny = curBall.second + dy[i]; + if(nx>=0 && nx < N && ny>=0 && ny < M){ + if(v[nx][ny]=='.'){ + cur.bX = nx; + cur.bY = ny; + dqFlag = true; + bDq.push_back({nx, ny}); + }else if(v[nx][ny] == 'O'){ + blueFlag = true; + } + } + } + + if(cur.rX == cur.bX && cur.rY == cur.bY){ + int distA = abs((cur2.rX - cur.rX) + (cur2.rY - cur.rY)); + int distB = abs((cur2.bX - cur.bX) + (cur2.bY - cur.bY)); + if(distA > distB){ + cur.rX -= dx[i]; + cur.rY -= dy[i]; + }else{ + cur.bX -= dx[i]; + cur.bY -= dy[i]; + } + } + + if(flag && !blueFlag){ + ans = cur2.cnt; + break; + } + if(!blueFlag && dqFlag){ + dq.push_back(cur); + } + } + } + cout<< ((ans==-1) ? 0 : 1); + return 0; +} From 371b7c823e6d4b22fd1396a7919718a25bf443b5 Mon Sep 17 00:00:00 2001 From: geunnii Date: Mon, 2 Aug 2021 23:50:12 +0900 Subject: [PATCH 020/213] BOJ --- BaekJoon/13459_simulation_BFS.cpp | 100 ++++++++++++++++++++++++++++++ SWEA/1231_Tree.cpp | 37 +++++++++++ 2 files changed, 137 insertions(+) create mode 100644 BaekJoon/13459_simulation_BFS.cpp create mode 100644 SWEA/1231_Tree.cpp diff --git a/BaekJoon/13459_simulation_BFS.cpp b/BaekJoon/13459_simulation_BFS.cpp new file mode 100644 index 0000000..82d111d --- /dev/null +++ b/BaekJoon/13459_simulation_BFS.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; +typedef pair pii; +typedef struct Balls{ + int rX, rY, bX, bY, cnt; +}; +const int dx[4] = {-1,1,0,0}; +const int dy[4] = {0,0,-1,1}; + +int main(){ + int N,M; + string tmp; + cin>>N>>M; + vectorv; + dequedq; + Balls ball = {0,0,0,0,0}; + for(int i=0;i>tmp; + for(int j=0;jrDq; rDq.push_back(red); + dequebDq; bDq.push_back(blue); + while(!rDq.empty()){ + pii curBall = rDq.front();rDq.pop_front(); + int nx = curBall.first + dx[i]; + int ny = curBall.second + dy[i]; + if(nx>=0 && nx < N && ny>=0 && ny < M && v[nx][ny] != '#'){ + cur.rX = nx; + cur.rY = ny; + dqFlag = true; + if(v[nx][ny]=='.'){ + rDq.push_back({nx, ny}); + }else if(v[nx][ny] == 'O'){ + flag = true; + } + } + } + while(!bDq.empty()){ + pii curBall = bDq.front();bDq.pop_front(); + int nx = curBall.first + dx[i]; + int ny = curBall.second + dy[i]; + if(nx>=0 && nx < N && ny>=0 && ny < M){ + if(v[nx][ny]=='.'){ + cur.bX = nx; + cur.bY = ny; + dqFlag = true; + bDq.push_back({nx, ny}); + }else if(v[nx][ny] == 'O'){ + blueFlag = true; + } + } + } + + if(cur.rX == cur.bX && cur.rY == cur.bY){ + int distA = abs((cur2.rX - cur.rX) + (cur2.rY - cur.rY)); + int distB = abs((cur2.bX - cur.bX) + (cur2.bY - cur.bY)); + if(distA > distB){ + cur.rX -= dx[i]; + cur.rY -= dy[i]; + }else{ + cur.bX -= dx[i]; + cur.bY -= dy[i]; + } + } + + if(flag && !blueFlag){ + ans = cur2.cnt; + break; + } + if(!blueFlag && dqFlag){ + dq.push_back(cur); + } + } + } + cout<< ((ans==-1) ? 0 : 1); + return 0; +} diff --git a/SWEA/1231_Tree.cpp b/SWEA/1231_Tree.cpp new file mode 100644 index 0000000..1a70dd4 --- /dev/null +++ b/SWEA/1231_Tree.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +char buf[101]; +int N; + +void inorder(int n){ + if(2*n <= N){ + inorder(2*n); + } + cout<>N; + for(int i=1; i<=N; ++i){ + cin>>parent>>s; + buf[parent]=s; + if(2*i+1 <= N){ + cin>>child1>>child2; + } + else if(2*i <= N){ + cin>>child1; + } + } + cout<<"#"< Date: Tue, 3 Aug 2021 23:07:27 +0900 Subject: [PATCH 021/213] SWEA --- SWEA/1232_inorder.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 SWEA/1232_inorder.cpp diff --git a/SWEA/1232_inorder.cpp b/SWEA/1232_inorder.cpp new file mode 100644 index 0000000..438516c --- /dev/null +++ b/SWEA/1232_inorder.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +using namespace std; +int buf[1001][3]; +int N; + +int inorder(int n) { + if (buf[n][0] < 0) { + int a = inorder(buf[n][1]); + int b = inorder(buf[n][2]); + if (buf[n][0] == -1) { + return a + b; + } + else if (buf[n][0] == -2) { + return a - b; + } + else if (buf[n][0] == -3) { + return a * b; + } + else if (buf[n][0] == -4) { + return a / b; + } + else { + return buf[n][0]; + } + } + return buf[n][0]; +} + +int main() { + for (int tc = 1; tc <= 10; ++tc) { + int parent, child1, child2; + string s; + cin >> N; + for (int i = 1; i <= N; ++i) { + cin >> parent >> s; + if (s == "+") { + buf[parent][0] = -1; + } + else if (s == "-") { + buf[parent][0] = -2; + } + else if (s == "*") { + buf[parent][0] = -3; + } + else if (s == "/") { + buf[parent][0] = -4; + } + else { + buf[parent][0] = stoi(s); + } + if (s == "+" || s == "-" || s == "*" || s == "/") { + cin >> buf[parent][1] >> buf[parent][2]; + } + } + cout << "#" << tc << " "< Date: Wed, 4 Aug 2021 21:37:33 +0900 Subject: [PATCH 022/213] SWEA --- SWEA/1233_Tree.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 SWEA/1233_Tree.cpp diff --git a/SWEA/1233_Tree.cpp b/SWEA/1233_Tree.cpp new file mode 100644 index 0000000..5b63bbe --- /dev/null +++ b/SWEA/1233_Tree.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +using namespace std; +int buf[201][3]; +int N; +bool flag; + +int inorder(int n) { + int ans = -2e9; + if (buf[n][0] < 0) { + int a = inorder(buf[n][1]); + int b = inorder(buf[n][2]); + if(a == -2e9 || b == -2e9){ + flag = false; + return -2e9; + } + if (buf[n][0] == -1) { + return ans = a + b; + } + else if (buf[n][0] == -2) { + return ans = a - b; + } + else if (buf[n][0] == -3) { + return ans = a * b; + } + else if (buf[n][0] == -4) { + return ans = a / b; + } + else { + return ans = buf[n][0]; + } + } + return ans; +} + +int main() { + for (int tc = 1; tc <= 10; ++tc) { + int parent; + string child1, child2; + flag = true; + string s; + cin >> N; + for (int i = 1; i <= N; ++i) { + cin >> parent >> s; + if(i <= N/2){ + if(i == N/2 && N%2 ==0){ + cin>>child1; + }else{ + cin>>child1>>child2; + } + if(s[0] >= '0' && s[0] <='9'){ + flag = false; + } + }else{ + if(s[0] <'0' || s[0] > '9'){ + flag = false; + } + } + } + cout << "#" << tc << " "< Date: Thu, 5 Aug 2021 14:48:03 +0900 Subject: [PATCH 023/213] SWEA --- SWEA/2930_heap.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 SWEA/2930_heap.cpp diff --git a/SWEA/2930_heap.cpp b/SWEA/2930_heap.cpp new file mode 100644 index 0000000..4a82f48 --- /dev/null +++ b/SWEA/2930_heap.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +using namespace std; + +int main(){ + int tc; + scanf("%d", &tc); + for(int Tc=1; Tc<=tc; ++Tc){ + int num; + scanf("%d", &num); + vectorans; + priority_queuepq; + for(int i=0; i Date: Thu, 5 Aug 2021 22:25:30 +0900 Subject: [PATCH 024/213] SWEA --- SWEA/1249_bfs.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++ SWEA/3000_heap.cpp | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 SWEA/1249_bfs.cpp create mode 100644 SWEA/3000_heap.cpp diff --git a/SWEA/1249_bfs.cpp b/SWEA/1249_bfs.cpp new file mode 100644 index 0000000..471fc15 --- /dev/null +++ b/SWEA/1249_bfs.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +const int dx[] = {1,-1,0,0}; +const int dy[] = {0,0,1,-1}; +int map[101][101]; +int visited[101][101]; + +struct node{ + int weight, x, y; +}; + +struct compare{ + bool operator()(node & a, node & b){ + return a.weight > b.weight; + } +}; + + +int main(){ + int tc; + scanf("%d", &tc); + for(int Tc=1; Tc<=tc; ++Tc){ + priority_queue, compare>pq; + int N; + scanf("%d", &N); + fill(&map[0][0], &map[N][N], 0); + fill(&visited[0][0], &visited[N][N], 2e9); + for(int i=0; i weight + map[nx][ny]){ + visited[nx][ny] = weight + map[nx][ny]; + pq.push({weight + map[nx][ny], nx, ny}); + } + } + + } + } + printf("#%d %d\n", Tc, visited[N-1][N-1]); + } + return 0; +} \ No newline at end of file diff --git a/SWEA/3000_heap.cpp b/SWEA/3000_heap.cpp new file mode 100644 index 0000000..0ce6670 --- /dev/null +++ b/SWEA/3000_heap.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main(){ + int tc; + scanf("%d", &tc); + for(int Tc=1; Tc<=tc; ++Tc){ + long long sum_ =0; + priority_queuemaxPq; + priority_queue, greater>minPq; + int init, N, n1, n2; + scanf("%d%d", &N, &init); + maxPq.push(init); + for(int i=0; i<2*N; ++i) { + scanf("%d", &n1); + if(maxPq.size() == 0) maxPq.push(n1); + else{ + if(maxPq.top() < n1){ + minPq.push(n1); + }else{ + maxPq.push(n1); + } + if(maxPq.size() > minPq.size()){ + while(maxPq.size() - minPq.size() > 1){ + int t = maxPq.top(); + maxPq.pop(); + minPq.push(t); + } + }else{ + while(maxPq.size() < minPq.size()){ + int t = minPq.top(); + minPq.pop(); + maxPq.push(t); + } + } + } + if(i & 1){ + sum_ += maxPq.top(); + sum_ %= 20171109; + } + } + printf("#%d %lld", Tc, sum_); + } + return 0; +} \ No newline at end of file From 64994ea025059e8d58f14faec5b918b324e3742c Mon Sep 17 00:00:00 2001 From: geunnii Date: Fri, 6 Aug 2021 15:49:38 +0900 Subject: [PATCH 025/213] SWEA --- SWEA/1249_bfs.cpp | 64 +++++++++++++++++++++++++++ SWEA/1256_Trie.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++ SWEA/3000_heap.cpp | 51 ++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 SWEA/1249_bfs.cpp create mode 100644 SWEA/1256_Trie.cpp create mode 100644 SWEA/3000_heap.cpp diff --git a/SWEA/1249_bfs.cpp b/SWEA/1249_bfs.cpp new file mode 100644 index 0000000..471fc15 --- /dev/null +++ b/SWEA/1249_bfs.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +const int dx[] = {1,-1,0,0}; +const int dy[] = {0,0,1,-1}; +int map[101][101]; +int visited[101][101]; + +struct node{ + int weight, x, y; +}; + +struct compare{ + bool operator()(node & a, node & b){ + return a.weight > b.weight; + } +}; + + +int main(){ + int tc; + scanf("%d", &tc); + for(int Tc=1; Tc<=tc; ++Tc){ + priority_queue, compare>pq; + int N; + scanf("%d", &N); + fill(&map[0][0], &map[N][N], 0); + fill(&visited[0][0], &visited[N][N], 2e9); + for(int i=0; i weight + map[nx][ny]){ + visited[nx][ny] = weight + map[nx][ny]; + pq.push({weight + map[nx][ny], nx, ny}); + } + } + + } + } + printf("#%d %d\n", Tc, visited[N-1][N-1]); + } + return 0; +} \ No newline at end of file diff --git a/SWEA/1256_Trie.cpp b/SWEA/1256_Trie.cpp new file mode 100644 index 0000000..3d3d4ef --- /dev/null +++ b/SWEA/1256_Trie.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +using namespace std; + +#define ALPHABETS_CNT 27 +#define MAXN 1000001 + +struct Trie; +Trie* myAlloc(); +char s[404]; +int nodeCnt = 0; +int cnt; +int K; +vectorans; + +struct Trie { + Trie* _children[ALPHABETS_CNT]; + bool end; + void add(const char* word) { + if (*word != 0) { + if (_children[*word - 'a'] == nullptr) _children[*word - 'a'] = myAlloc(); + _children[*word - 'a']->add(word + 1); + } + } + + Trie* get(const char* word) { + if (*word != 0) { + if (_children[*word - 'a'] != nullptr) return _children[*word - 'a']->get(word + 1); + return nullptr; + } + return this; + } + void clear() { + for (int i = 0; i < ALPHABETS_CNT; i++) { + _children[i] = nullptr; + } + } +}; + +void dfs(Trie * root){ + bool flag = false; + Trie * tmp = root; + for(int i=0; i<27; ++i){ + if(tmp->_children[i]){ + flag = true; + break; + } + } + if(!flag) { + cnt += 1; +// printf("%d ", cnt); +// for(auto x: ans){ +// printf("%c", x); +// } +// printf("\n"); + } + if(cnt == K) return; + for(int i=0; i<27; ++i){ + if(tmp->_children[i]){ + ans.push_back('a'+i); + dfs(tmp->_children[i]); + if(cnt == K) return; + ans.pop_back(); + } + } +} + +Trie Node[MAXN]; + +Trie* myAlloc() { + Trie* ret = &Node[nodeCnt++]; + ret->clear(); + return ret; +} + +int main(){ + int TC; + scanf("%d", &TC); + for(int tc=1; tc<=TC; ++tc){ + fill(s, s+402, '\0'); + ans.clear(); + nodeCnt = 0; + Trie* root = myAlloc(); + cnt = 0; + scanf("%d", &K); + scanf("%s", s); + s[strlen(s)] = '{'; + for(int i=0; i<=strlen(s); ++i){ + root->add(s+i); + } + dfs(root); + printf("#%d ", tc); + if(ans.back() == '{') ans.pop_back(); + if(ans.size() == 0){ + printf("none\n"); + }else { + for (auto x : ans) { + printf("%c", x); + } + printf("\n"); + } + } + return 0; +} \ No newline at end of file diff --git a/SWEA/3000_heap.cpp b/SWEA/3000_heap.cpp new file mode 100644 index 0000000..0ce6670 --- /dev/null +++ b/SWEA/3000_heap.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main(){ + int tc; + scanf("%d", &tc); + for(int Tc=1; Tc<=tc; ++Tc){ + long long sum_ =0; + priority_queuemaxPq; + priority_queue, greater>minPq; + int init, N, n1, n2; + scanf("%d%d", &N, &init); + maxPq.push(init); + for(int i=0; i<2*N; ++i) { + scanf("%d", &n1); + if(maxPq.size() == 0) maxPq.push(n1); + else{ + if(maxPq.top() < n1){ + minPq.push(n1); + }else{ + maxPq.push(n1); + } + if(maxPq.size() > minPq.size()){ + while(maxPq.size() - minPq.size() > 1){ + int t = maxPq.top(); + maxPq.pop(); + minPq.push(t); + } + }else{ + while(maxPq.size() < minPq.size()){ + int t = minPq.top(); + minPq.pop(); + maxPq.push(t); + } + } + } + if(i & 1){ + sum_ += maxPq.top(); + sum_ %= 20171109; + } + } + printf("#%d %lld", Tc, sum_); + } + return 0; +} \ No newline at end of file From fd13fe43e4064b82408b9b79aa09f4681a3342e7 Mon Sep 17 00:00:00 2001 From: geunnii Date: Sun, 8 Aug 2021 00:13:40 +0900 Subject: [PATCH 026/213] SWEA --- SWEA/1257_trie.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 SWEA/1257_trie.cpp diff --git a/SWEA/1257_trie.cpp b/SWEA/1257_trie.cpp new file mode 100644 index 0000000..87d94c3 --- /dev/null +++ b/SWEA/1257_trie.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +using namespace std; + +#define ALPHABETS_CNT 27 +#define MAXN 1000001 + +struct Trie; +Trie* myAlloc(); +string s; +int nodeCnt = 0; +int cnt; +int K; +vectorans; + +struct Trie { + Trie* _children[ALPHABETS_CNT]; + bool end; + void add(const char* word) { + if(*word == 0){ + end = true; + } + else if (*word != 0) { + if (_children[*word - 'a'] == nullptr) _children[*word - 'a'] = myAlloc(); + _children[*word - 'a']->add(word + 1); + } + } + + Trie* get(const char* word) { + if (*word != 0) { + if (_children[*word - 'a'] != nullptr) return _children[*word - 'a']->get(word + 1); + return nullptr; + } + return this; + } + void clear() { + for (int i = 0; i < ALPHABETS_CNT; i++) { + _children[i] = nullptr; + } + } +}; + +void dfs(Trie * root){ + bool flag = false; + Trie * tmp = root; + if(tmp->end){ + cnt += 1; +// printf("%d ", cnt); +// for(auto x: ans){ +// printf("%c", x); +// } +// printf("\n"); + } + if(cnt == K) return; + for(int i=0; i<27; ++i){ + if(tmp->_children[i]){ + ans.push_back('a'+i); + dfs(tmp->_children[i]); + if(cnt == K) return; + ans.pop_back(); + } + } +} + +Trie Node[MAXN]; + +Trie* myAlloc() { + Trie* ret = &Node[nodeCnt++]; + ret->clear(); + ret->end = false; + return ret; +} + +int main(){ + int TC; + scanf("%d", &TC); + for(int tc=1; tc<=TC; ++tc){ + ans.clear(); + nodeCnt = 0; + Trie* root = myAlloc(); + cnt = 0; + scanf("%d", &K); + cin>>s; + char tmp[400]; + for(int i=0; i<=s.size(); ++i){ + for(int j=1;j<=s.size()-i; ++j ){ + memset(tmp, 0, s.size()); + string str; + str = s.substr(i, j); +// str.push_back('{'); + strcpy(tmp, str.c_str()); + root->add(tmp); +// printf("%d %d %s\n", i, j, tmp); + } + + } + dfs(root); + printf("#%d ", tc); + if(ans.back() == '{') ans.pop_back(); + if(ans.size() == 0){ + printf("none\n"); + }else { + for (auto x : ans) { + printf("%c", x); + } + printf("\n"); + } + } + return 0; +} \ No newline at end of file From fe52252d87c125fda2df834b3b53e92c8d04df33 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 9 Aug 2021 23:36:26 +0900 Subject: [PATCH 027/213] BOJ --- BaekJoon/15663.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 BaekJoon/15663.py diff --git a/BaekJoon/15663.py b/BaekJoon/15663.py new file mode 100644 index 0000000..e8133a4 --- /dev/null +++ b/BaekJoon/15663.py @@ -0,0 +1,16 @@ +import sys +from itertools import permutations +input = sys.stdin.readline + +n,m = map(int,input().split()) +ls = sorted(list(map(int,input().split()))) + +per = [] +for it in permutations(ls, m): + per.append(it) +per = sorted(list(set(per))) + +for it in per: + for i in it: + print(i, end = ' ') + print() From 565382476b2c6f86dd77dadec29629aad7fddfd8 Mon Sep 17 00:00:00 2001 From: geunnii Date: Wed, 11 Aug 2021 19:40:00 +0900 Subject: [PATCH 028/213] SWEA --- SWEA/2948_hash.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 SWEA/2948_hash.cpp diff --git a/SWEA/2948_hash.cpp b/SWEA/2948_hash.cpp new file mode 100644 index 0000000..d4d2413 --- /dev/null +++ b/SWEA/2948_hash.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +int main(){ + cin.tie(0); + ios_base::sync_with_stdio(false); + int Tc; + scanf("%d", &Tc); + char s[51]; + unordered_mapm1; + for(int tc=1; tc<=Tc; ++tc){ + m1.clear(); + int N, M, ans = 0; + scanf("%d%d", &N, &M); + for(int i=0; i Date: Tue, 10 Aug 2021 22:38:47 +0900 Subject: [PATCH 029/213] SWEA --- SWEA/2948_hash.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 SWEA/2948_hash.cpp diff --git a/SWEA/2948_hash.cpp b/SWEA/2948_hash.cpp new file mode 100644 index 0000000..d4d2413 --- /dev/null +++ b/SWEA/2948_hash.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +int main(){ + cin.tie(0); + ios_base::sync_with_stdio(false); + int Tc; + scanf("%d", &Tc); + char s[51]; + unordered_mapm1; + for(int tc=1; tc<=Tc; ++tc){ + m1.clear(); + int N, M, ans = 0; + scanf("%d%d", &N, &M); + for(int i=0; i Date: Thu, 12 Aug 2021 19:32:12 +0900 Subject: [PATCH 030/213] BOJ --- BaekJoon/1654_parametric_Search.cpp | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 BaekJoon/1654_parametric_Search.cpp diff --git a/BaekJoon/1654_parametric_Search.cpp b/BaekJoon/1654_parametric_Search.cpp new file mode 100644 index 0000000..8178360 --- /dev/null +++ b/BaekJoon/1654_parametric_Search.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int chk[10] = {0,}; +void dfs(int n, int idx, int arr[4]){ + if(n == 10){ + return; + } + if(idx == 4){ + for(int i=0;i<4; ++i){ + cout< Date: Thu, 12 Aug 2021 19:32:20 +0900 Subject: [PATCH 031/213] BOJ --- BaekJoon/2110_parametric_Search.cpp | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 BaekJoon/2110_parametric_Search.cpp diff --git a/BaekJoon/2110_parametric_Search.cpp b/BaekJoon/2110_parametric_Search.cpp new file mode 100644 index 0000000..8178360 --- /dev/null +++ b/BaekJoon/2110_parametric_Search.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int chk[10] = {0,}; +void dfs(int n, int idx, int arr[4]){ + if(n == 10){ + return; + } + if(idx == 4){ + for(int i=0;i<4; ++i){ + cout< Date: Thu, 12 Aug 2021 19:32:26 +0900 Subject: [PATCH 032/213] BOJ --- BaekJoon/2805_parametric_Search.cpp | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 BaekJoon/2805_parametric_Search.cpp diff --git a/BaekJoon/2805_parametric_Search.cpp b/BaekJoon/2805_parametric_Search.cpp new file mode 100644 index 0000000..8178360 --- /dev/null +++ b/BaekJoon/2805_parametric_Search.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int chk[10] = {0,}; +void dfs(int n, int idx, int arr[4]){ + if(n == 10){ + return; + } + if(idx == 4){ + for(int i=0;i<4; ++i){ + cout< Date: Thu, 12 Aug 2021 19:32:31 +0900 Subject: [PATCH 033/213] BOJ --- BaekJoon/2792_parametric_Search.cpp | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 BaekJoon/2792_parametric_Search.cpp diff --git a/BaekJoon/2792_parametric_Search.cpp b/BaekJoon/2792_parametric_Search.cpp new file mode 100644 index 0000000..8178360 --- /dev/null +++ b/BaekJoon/2792_parametric_Search.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int chk[10] = {0,}; +void dfs(int n, int idx, int arr[4]){ + if(n == 10){ + return; + } + if(idx == 4){ + for(int i=0;i<4; ++i){ + cout< Date: Fri, 13 Aug 2021 17:12:25 +0900 Subject: [PATCH 034/213] SWEA --- SWEA/3814_paremetric_Search.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SWEA/3814_paremetric_Search.cpp diff --git a/SWEA/3814_paremetric_Search.cpp b/SWEA/3814_paremetric_Search.cpp new file mode 100644 index 0000000..8178360 --- /dev/null +++ b/SWEA/3814_paremetric_Search.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int chk[10] = {0,}; +void dfs(int n, int idx, int arr[4]){ + if(n == 10){ + return; + } + if(idx == 4){ + for(int i=0;i<4; ++i){ + cout< Date: Sat, 14 Aug 2021 16:49:32 +0900 Subject: [PATCH 035/213] BOJ --- BaekJoon/1939_binarySearch.cpp | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 BaekJoon/1939_binarySearch.cpp diff --git a/BaekJoon/1939_binarySearch.cpp b/BaekJoon/1939_binarySearch.cpp new file mode 100644 index 0000000..24d66d2 --- /dev/null +++ b/BaekJoon/1939_binarySearch.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +using namespace std; +typedef pair pii; + + + +int main() { + int N, M; + scanf("%d%d", &N, &M); + vector>v(N+1); + vectorvisited; + for (int i = 0; i < M; ++i) { + int u, f, w; + scanf("%d%d%d", &u, &f, &w); + v[f].push_back({ u, w }); + v[u].push_back({ f, w }); + } + int s, e; + scanf("%d%d", &s, &e); + int l = 0; + int r = 1000000001; + while (l <= r) { + int mid = (l + r) / 2; + dequedq; + dq.push_back(s); + visited.clear(); + visited.resize(N + 1, false); + visited[s] = true; + bool flag = false; + while (!dq.empty()) { + int cur = dq.front(); + dq.pop_front(); + if (cur == e) { + flag = true; + break; + } + for (int i = 0; i < v[cur].size(); ++i) { + int nextNode = v[cur][i].first; + int nextCost = v[cur][i].second; + if (!visited[nextNode] && mid <= nextCost) { + visited[nextNode] = true; + dq.push_back(nextNode); + } + } + } + if (flag) { + l = mid+1; + } + else { + r = mid-1; + } + } + printf("%d", r); + return 0; +} From a780415e2726c8f3e011287a3d6a9af75be712ce Mon Sep 17 00:00:00 2001 From: geunnii Date: Sun, 15 Aug 2021 21:05:03 +0900 Subject: [PATCH 036/213] BOJ --- BaekJoon/2613_parametric_Search.cpp | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 BaekJoon/2613_parametric_Search.cpp diff --git a/BaekJoon/2613_parametric_Search.cpp b/BaekJoon/2613_parametric_Search.cpp new file mode 100644 index 0000000..8178360 --- /dev/null +++ b/BaekJoon/2613_parametric_Search.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int chk[10] = {0,}; +void dfs(int n, int idx, int arr[4]){ + if(n == 10){ + return; + } + if(idx == 4){ + for(int i=0;i<4; ++i){ + cout< Date: Mon, 16 Aug 2021 21:41:36 +0900 Subject: [PATCH 037/213] BOJ --- BaekJoon/1477_binarySearch.cpp | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 BaekJoon/1477_binarySearch.cpp diff --git a/BaekJoon/1477_binarySearch.cpp b/BaekJoon/1477_binarySearch.cpp new file mode 100644 index 0000000..7e29874 --- /dev/null +++ b/BaekJoon/1477_binarySearch.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +#define MAX 200001; + +int main() { + int N, M, L, tmp; + scanf("%d%d%d", &N, &M, &L); + vectorv; + v.push_back(0); + for (int i = 1; i <= N; ++i) { + scanf("%d", &tmp); + v.push_back(tmp); + } + v.push_back(L); + sort(v.begin(), v.end()); + int l = 0; + int r = L; + int ans = 2e9; + while (l <= r) { + int mid = (l + r) / 2; + int cnt = 0; + for (int i = 1; i < N+2; ++i) { + if ((v[i] - v[i - 1]) / mid > 0) { + if((v[i] - v[i-1]) % mid == 0){ + cnt += ((v[i] - v[i - 1]) / mid - 1); + } + else { + cnt += (v[i] - v[i - 1]) / mid; + } + } + } + if (cnt <= M) { + ans = min(ans, mid); + r = mid - 1; + } + else { + l = mid + 1; + } + } + printf("%d\n", ans); + return 0; +} From af51ccf6babbaca27c192e0605db817a1f44a16e Mon Sep 17 00:00:00 2001 From: geunnii Date: Tue, 17 Aug 2021 14:26:38 +0900 Subject: [PATCH 038/213] SWEA --- SWEA/3813_parametric_Search.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SWEA/3813_parametric_Search.cpp diff --git a/SWEA/3813_parametric_Search.cpp b/SWEA/3813_parametric_Search.cpp new file mode 100644 index 0000000..8178360 --- /dev/null +++ b/SWEA/3813_parametric_Search.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int chk[10] = {0,}; +void dfs(int n, int idx, int arr[4]){ + if(n == 10){ + return; + } + if(idx == 4){ + for(int i=0;i<4; ++i){ + cout< Date: Wed, 18 Aug 2021 22:24:36 +0900 Subject: [PATCH 039/213] BOJ --- BaekJoon/2352_LIS.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 BaekJoon/2352_LIS.py diff --git a/BaekJoon/2352_LIS.py b/BaekJoon/2352_LIS.py new file mode 100644 index 0000000..81567be --- /dev/null +++ b/BaekJoon/2352_LIS.py @@ -0,0 +1,14 @@ +import sys +from bisect import bisect_left +input = sys.stdin.readline + +N = int(input()) +ls = list(map(int,input().split())) +tmp = [ls[0]] +for i in range(1, len(ls)): + if tmp[-1] < ls[i]: + tmp.append(ls[i]) + else: + idx = bisect_left(tmp, ls[i]) + tmp[idx] = ls[i] +print(len(tmp)) From 9f2af03a390dec5f091d378cb35a186423664ea6 Mon Sep 17 00:00:00 2001 From: geunnii Date: Thu, 19 Aug 2021 17:45:06 +0900 Subject: [PATCH 040/213] BOJ --- BaekJoon/11049_dp.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 BaekJoon/11049_dp.py diff --git a/BaekJoon/11049_dp.py b/BaekJoon/11049_dp.py new file mode 100644 index 0000000..536a0ed --- /dev/null +++ b/BaekJoon/11049_dp.py @@ -0,0 +1,17 @@ +import sys +input = sys.stdin.readline + +n = int(input()) +ls = [list(map(int,input().split())) for _ in range(n)] + +dp = [[0 for _ in range(n)] for _ in range(n)] + +for i in range(1,n): + for j in range(n-i): + s = j + e = i + j + dp[s][e] = 2e9 + for k in range(s, e): + dp[s][e] = min(dp[s][e], dp[s][k] + dp[k+1][e] + ls[s][0] * ls[k][1] * ls[e][1]) + +print(dp[0][n-1]) \ No newline at end of file From 010ac913e59e5a7cec720e46b8a70ace2e36ef2b Mon Sep 17 00:00:00 2001 From: geunnii Date: Fri, 20 Aug 2021 14:41:02 +0900 Subject: [PATCH 041/213] BOJ --- BaekJoon/13306_UF.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 BaekJoon/13306_UF.cpp diff --git a/BaekJoon/13306_UF.cpp b/BaekJoon/13306_UF.cpp new file mode 100644 index 0000000..04f1f0b --- /dev/null +++ b/BaekJoon/13306_UF.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +using namespace std; +vectorparent(200010); +vectorp(200010); + +int findParent(int a){ + if(parent[a] == a) + return a; + return parent[a] = findParent(parent[a]); +} + +void unionParent(int a, int b){ + a = findParent(a); + b = findParent(b); + if(a != b){ + parent[b] = a; + } +} + +int main(){ + cin.tie(); + ios_base::sync_with_stdio(0); + int N,Q; + cin>>N>>Q; + for(int i=0; i>p[i]; + } + int query[400002][3]; + for(int i=0; i<(Q+N-1); ++i){ + cin>>query[i][0]; + if(query[i][0] == 0){ + cin>>query[i][1]; + }else{ + cin>>query[i][1]>>query[i][2]; + } + } + vectorres; + for(int i=Q+N-2; i>=0; --i){ + if(query[i][0] == 0){ + unionParent(query[i][1], p[query[i][1]]); + }else{ + int a = findParent(query[i][1]); + int b = findParent(query[i][2]); + if(a == b){ + res.push_back("YES"); + }else{ + res.push_back("NO"); + } + } + } + for(auto i = res.rbegin(); i != res.rend(); ++i){ + cout<< *i<<'\n'; + } + return 0; +} \ No newline at end of file From fbbc10f15dc6d433f353e372d32442c12b2ffadd Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 21 Aug 2021 10:20:09 +0900 Subject: [PATCH 042/213] BOJ --- ...210\354\261\204\355\214\205\353\260\251.py" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 "kakaoInternship/\354\230\244\355\224\210\354\261\204\355\214\205\353\260\251.py" diff --git "a/kakaoInternship/\354\230\244\355\224\210\354\261\204\355\214\205\353\260\251.py" "b/kakaoInternship/\354\230\244\355\224\210\354\261\204\355\214\205\353\260\251.py" new file mode 100644 index 0000000..a286e53 --- /dev/null +++ "b/kakaoInternship/\354\230\244\355\224\210\354\261\204\355\214\205\353\260\251.py" @@ -0,0 +1,18 @@ +def solution(record): + tmp = [] + for i in record: + tmp.append(i.split()) + answer = [] + dc = dict() + for rec in tmp: + if rec[0] == "Enter": + dc[rec[1]] = rec[2] + elif rec[0] == "Change": + dc[rec[1]] = rec[2] + for rec in tmp: + if rec[0] == "Enter": + answer.append(dc[rec[1]]+"님이 들어왔습니다.") + elif rec[0] == "Leave": + answer.append(dc[rec[1]]+"님이 나갔습니다.") + + return answer From 01c25fc6dfc451a40fd331e45e21e6cdaa7257d4 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 21 Aug 2021 10:20:41 +0900 Subject: [PATCH 043/213] kakao --- ...64\353\246\254\353\211\264\354\226\274.py" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "kakaoInternship/\353\247\244\353\211\264\353\246\254\353\211\264\354\226\274.py" diff --git "a/kakaoInternship/\353\247\244\353\211\264\353\246\254\353\211\264\354\226\274.py" "b/kakaoInternship/\353\247\244\353\211\264\353\246\254\353\211\264\354\226\274.py" new file mode 100644 index 0000000..d7b484b --- /dev/null +++ "b/kakaoInternship/\353\247\244\353\211\264\353\246\254\353\211\264\354\226\274.py" @@ -0,0 +1,26 @@ +from itertools import combinations + +def solution(orders, course): + answer = [] + for num in course: + candidate = set() + dc = {} + ans = 0 + for i in orders: + for it in combinations(i, num): + s = ''.join(sorted(it)) + if s not in dc: + dc[s] = 1 + else: + dc[s] += 1 + ans = max(ans, dc[s]) + candidate.add(s) + tmp = [] + for i in candidate: + if dc[i] == ans: + tmp.append(i) + + for i in tmp: + answer.append(i) + answer.sort() + return answer From 23dbbd83d6576958afbcfeecba591ba326b5918b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 21 Aug 2021 10:21:06 +0900 Subject: [PATCH 044/213] kakao --- ...4\355\230\270 \353\263\200\355\231\230.py" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "kakaoInternship/\352\264\204\355\230\270 \353\263\200\355\231\230.py" diff --git "a/kakaoInternship/\352\264\204\355\230\270 \353\263\200\355\231\230.py" "b/kakaoInternship/\352\264\204\355\230\270 \353\263\200\355\231\230.py" new file mode 100644 index 0000000..da897d4 --- /dev/null +++ "b/kakaoInternship/\352\264\204\355\230\270 \353\263\200\355\231\230.py" @@ -0,0 +1,70 @@ +def isBalance(s): + lCnt = 0 + rCnt = 0 + for i in s: + if i == '(': + lCnt += 1 + else: + rCnt += 1 + return lCnt == rCnt + +def isCorrect(s): + if s == '': + return False + st = [] + flag = True + for i in s: + if len(st) == 0: + if i == ')': + flag = False + break + else: + st.append(i) + elif i == '(': + st.append(i) + else: + if st[-1] == '(': + st = st[:-1] + else: + flag = False + break + if len(st) != 0: + flag = False + return flag + +def recur(s): + ans = '' + if s == '': + return '' + u = '' + v = '' + for i in range(1, len(s)+1): + u = s[:i] + v = s[i:] + if isBalance(u): + break + if isCorrect(u): + ans = u + recur(v) + else: + tmp = '(' + tmp += recur(v) + tmp += ')' + u = u[1:] + u = u[:-1] + newU = '' + for i in range(len(u)): + if u[i] == "(": + newU += ")" + else: + newU += "(" + ans = tmp + newU + return ans + + + +def solution(p): + answer = '' + if isCorrect(p): + return p + answer = recur(p) + return answer From 560abb4afb3a1474914011e3856ac5b907e66412 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 21 Aug 2021 10:21:28 +0900 Subject: [PATCH 045/213] kakao --- ...54\354\212\244\355\204\260\353\247\201.py" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "kakaoInternship/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" diff --git "a/kakaoInternship/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" "b/kakaoInternship/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" new file mode 100644 index 0000000..3126fd9 --- /dev/null +++ "b/kakaoInternship/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" @@ -0,0 +1,44 @@ +def solution(str1, str2): + answer = 0 + str1 = str1.lower() + str2 = str2.lower() + setA = [] + setB = [] + for i in range(len(str1)-1): + ta = str1[i:i+2] + flag = True + for j in range(2): + if not ta[j].isalpha(): + flag = False + if flag: + setA.append(ta) + + for i in range(len(str2) -1): + tb = str2[i:i+2] + flag = True + for j in range(2): + if not tb[j].isalpha(): + flag = False + if flag: + setB.append(tb) + + setA_copy = setA.copy() + setB_copy = setB.copy() + + inter_ = [] + + for i in setA: + if i in setB_copy: + inter_.append(i) + setA_copy.remove(i) + setB_copy.remove(i) + + sum_ = setA_copy + setB_copy + inter_ + print(sum_, inter_) + if len(sum_) == 0: + answer = 65536 + else: + answer = int(len(inter_) / len(sum_) * 65536) + + + return answer From 12cff9225e9423be06e3634e63a055ae56bdb106 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 21 Aug 2021 10:21:48 +0900 Subject: [PATCH 046/213] kakao --- ...25\354\235\270\355\225\230\352\270\260.py" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 "kakaoInternship/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" diff --git "a/kakaoInternship/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" "b/kakaoInternship/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" new file mode 100644 index 0000000..b84ab0b --- /dev/null +++ "b/kakaoInternship/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" @@ -0,0 +1,47 @@ +visited = [[False for _ in range(5)] for _ in range(5)] +flag = True +dx = [1,-1,0,0] +dy = [0,0,1,-1] + +def dfs(x, y, d, places): + global visited + global flag + if d == 2: + return + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if 0<=nx<5 and 0<=ny<5 and not visited[nx][ny]: + if places[nx][ny] == 'P': + flag = False + break + elif places[nx][ny] == 'X': + continue + else: + visited[nx][ny] = True + dfs(nx, ny, d+1, places) + visited[nx][ny] = False + return + + +def solution(places): + global flag + global visited + answer = [] + for tc in range(5): + for i in range(5): + for j in range(5): + visited[i][j] = False + flag = True + for i in range(5): + for j in range(5): + if not visited[i][j] and places[tc][i][j] == 'P': + visited[i][j] = True + dfs(i, j, 0, places[tc]) + visited[i][j] = False + if flag : + answer.append(1) + else: + answer.append(0) + + return answer From bdfc25f0d28fd62754abae7a500e0fb448ebd696 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 21 Aug 2021 10:22:13 +0900 Subject: [PATCH 047/213] kakao --- "kakaoInternship/\355\212\234\355\224\214.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "kakaoInternship/\355\212\234\355\224\214.py" diff --git "a/kakaoInternship/\355\212\234\355\224\214.py" "b/kakaoInternship/\355\212\234\355\224\214.py" new file mode 100644 index 0000000..1b268da --- /dev/null +++ "b/kakaoInternship/\355\212\234\355\224\214.py" @@ -0,0 +1,37 @@ +def solution(s): + answer = [] + #extract array + + use = [] + st = [] + tmp = [] + num = 0 + for i in s: + if i == '{': + st.append(i) + elif i == '}': + if num != 0: + tmp.append(num) + use.append(tmp) + tmp = [] + num = 0 + st.pop() + elif i == ',': + if len(st) == 1: + continue + else: + tmp.append(num) + num = 0 + else: + num *= 10 + num += int(i) + use.sort(key=len) + dc = {} + for i in range(len(use)): + for j in range(len(use[i])): + if use[i][j] in dc: + continue + else: + answer.append(use[i][j]) + dc[use[i][j]] = 1 + return answer From 3dcb3e9c1efb7058b08cf3c51cd2224963aa8654 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 21 Aug 2021 10:22:40 +0900 Subject: [PATCH 048/213] kakao --- ...4\353\237\254\353\247\201\353\266\201.cpp" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "kakaoInternship/\354\271\264\354\271\264\354\230\244\355\224\204\353\240\214\355\212\270 \354\273\254\353\237\254\353\247\201\353\266\201.cpp" diff --git "a/kakaoInternship/\354\271\264\354\271\264\354\230\244\355\224\204\353\240\214\355\212\270 \354\273\254\353\237\254\353\247\201\353\266\201.cpp" "b/kakaoInternship/\354\271\264\354\271\264\354\230\244\355\224\204\353\240\214\355\212\270 \354\273\254\353\237\254\353\247\201\353\266\201.cpp" new file mode 100644 index 0000000..6f5d402 --- /dev/null +++ "b/kakaoInternship/\354\271\264\354\271\264\354\230\244\355\224\204\353\240\214\355\212\270 \354\273\254\353\237\254\353\247\201\353\266\201.cpp" @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +using namespace std; + +queue> q; // 행, 열 +vector> visited; + +//상,하,좌,우 +int y[] = { 1,-1,0,0 }; +int x[] = { 0,0, 1,-1 }; + +int BFS(int m, int n, vector> picture) { + int areaCnt = 0; + + while (!q.empty()) { + areaCnt += 1; + int currY = q.front().first; + int currX = q.front().second; +// cout< solution(int m, int n, vector> picture) { + int number_of_area = 0; + int max_size_of_one_area = 0; + int areaCnt; + visited.clear(); + visited.resize(m, vector(n, false)); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (!visited[i][j] && picture[i][j]!=0) { + q.push({ i, j }); + visited[i][j] = true; + areaCnt = BFS(m, n, picture); + max_size_of_one_area = max(max_size_of_one_area, areaCnt); + number_of_area++; + } + } + } + +// for (int i = 0; i < m; i++) { +// for (int j = 0; j < n; j++) { +// cout << visited[i][j]; +// } +// cout << endl; +// } + + vector answer(2); + answer[0] = number_of_area; + answer[1] = max_size_of_one_area; +// cout< Date: Sat, 21 Aug 2021 11:58:53 +0900 Subject: [PATCH 049/213] kakao --- ...4\354\234\204 \352\262\200\354\203\211.py" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "kakaoInternship/\354\210\234\354\234\204 \352\262\200\354\203\211.py" diff --git "a/kakaoInternship/\354\210\234\354\234\204 \352\262\200\354\203\211.py" "b/kakaoInternship/\354\210\234\354\234\204 \352\262\200\354\203\211.py" new file mode 100644 index 0000000..1ec31d4 --- /dev/null +++ "b/kakaoInternship/\354\210\234\354\234\204 \352\262\200\354\203\211.py" @@ -0,0 +1,48 @@ +from bisect import bisect_left +def solution(info, query): + answer = [] + dc = {} + dc["java"] = 1 + dc["python"] = 2 + dc["cpp"] = 3 + dc["backend"] = 4 + dc["frontend"] = 5 + dc["junior"] = 6 + dc["senior"] = 7 + dc["chicken"] = 8 + dc["pizza"] = 9 + dc["-"] = 0 + ls = [[[[[]for i in range(10)] for _ in range(10)] for _ in range(10)]for _ in range(10)] + + for it in info: + tmp = it.split() + ls[dc[tmp[0]]][dc[tmp[1]]][dc[tmp[2]]][dc[tmp[3]]].append(int(tmp[4])) + #하나 + ls[0][dc[tmp[1]]][dc[tmp[2]]][dc[tmp[3]]].append(int(tmp[4])) + ls[dc[tmp[0]]][0][dc[tmp[2]]][dc[tmp[3]]].append(int(tmp[4])) + ls[dc[tmp[0]]][dc[tmp[1]]][0][dc[tmp[3]]].append(int(tmp[4])) + ls[dc[tmp[0]]][dc[tmp[1]]][dc[tmp[2]]][0].append(int(tmp[4])) + #둘 + ls[0][0][dc[tmp[2]]][dc[tmp[3]]].append(int(tmp[4])) + ls[0][dc[tmp[1]]][0][dc[tmp[3]]].append(int(tmp[4])) + ls[0][dc[tmp[1]]][dc[tmp[2]]][0].append(int(tmp[4])) + ls[dc[tmp[0]]][0][0][dc[tmp[3]]].append(int(tmp[4])) + ls[dc[tmp[0]]][0][dc[tmp[2]]][0].append(int(tmp[4])) + ls[dc[tmp[0]]][dc[tmp[1]]][0][0].append(int(tmp[4])) + #셋 + ls[0][0][0][dc[tmp[3]]].append(int(tmp[4])) + ls[0][0][dc[tmp[2]]][0].append(int(tmp[4])) + ls[0][dc[tmp[1]]][0][0].append(int(tmp[4])) + ls[dc[tmp[0]]][0][0][0].append(int(tmp[4])) + #넷 + ls[0][0][0][0].append(int(tmp[4])) + for i in [0,1,2,3]: + for j in [0,4,5]: + for k in [0,6,7]: + for l in [0,8,9]: + ls[i][j][k][l].sort() + for it in query: + tmp = it.split() + idx = bisect_left(ls[dc[tmp[0]]][dc[tmp[2]]][dc[tmp[4]]][dc[tmp[6]]], int(tmp[7])) + answer.append(len(ls[dc[tmp[0]]][dc[tmp[2]]][dc[tmp[4]]][dc[tmp[6]]]) - idx) + return answer From e1063d2f48f62e1489baf1288425079171636d5a Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 22 Aug 2021 14:57:44 +0900 Subject: [PATCH 050/213] BOJ --- BaekJoon/16978.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 BaekJoon/16978.py diff --git a/BaekJoon/16978.py b/BaekJoon/16978.py new file mode 100644 index 0000000..b86a48d --- /dev/null +++ b/BaekJoon/16978.py @@ -0,0 +1,71 @@ +import math +import sys +input = sys.stdin.readline +sys.setrecursionlimit(10**6) + +def init(s, e, node): + if s == e: + tree[node] = ls[s] + else: + mid = (s+e)//2 + tree[node] = init(s, mid, 2*node) + init(mid+1, e, 2*node+1) + return tree[node] + +def sum_(s, e, node, l, r): + if l>e or r e: return + tree[node] += dif + if s == e : return + mid = (s+e)//2 + update(s, mid, 2*node, idx, dif) + update(mid+1, e, 2*node+1, idx, dif) + + + +n = int(input()) +ls = list(map(int,input().split())) +tree = [0 for _ in range(4*n)] +q = int(input()) + +oneCnt = 0 +twoCnt = 0 + +oneQ = [] +twoQ = [] + +for i in range(q): + tmp = list(map(int,input().split())) + if tmp[0] == 2: + twoQ.append([twoCnt] + tmp) + twoCnt+=1 + else: + oneQ.append(tmp) + oneCnt+=1 + +twoQ = sorted(twoQ, key=lambda x:x[2]) +oneIdx = 0 +twoIdx = 0 + +answer = [0 for _ in range(twoCnt)] +init(0, n-1, 1) + +while twoIdx != twoCnt: + base = twoQ[twoIdx][2] + if base > oneIdx: + diff = oneQ[oneIdx][2] - ls[oneQ[oneIdx][1]-1] + ls[oneQ[oneIdx][1]-1] = oneQ[oneIdx][2] + update(0, n-1, 1, oneQ[oneIdx][1]-1, diff) + oneIdx += 1 + else: + tmp = sum_(0, n-1, 1, twoQ[twoIdx][3]-1, twoQ[twoIdx][4]-1) + answer[twoQ[twoIdx][0]] = tmp + twoIdx += 1 + +for i in answer: + print(i) From 76958ea28c4a8326929e3fc6c361e17db5967317 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 22 Aug 2021 14:59:14 +0900 Subject: [PATCH 051/213] kakao --- ...\354\227\264 \354\225\225\354\266\225.cpp" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "kakaoInternship/\353\254\270\354\236\220\354\227\264 \354\225\225\354\266\225.cpp" diff --git "a/kakaoInternship/\353\254\270\354\236\220\354\227\264 \354\225\225\354\266\225.cpp" "b/kakaoInternship/\353\254\270\354\236\220\354\227\264 \354\225\225\354\266\225.cpp" new file mode 100644 index 0000000..5908416 --- /dev/null +++ "b/kakaoInternship/\353\254\270\354\236\220\354\227\264 \354\225\225\354\266\225.cpp" @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +int solution(string s) { + if(s.size() == 1) return 1; + int answer = 0; + int n = s.size(); + int ans = 2e9; + for(int i=1; i<=n/2; ++i){ + string t = ""; + vectorv; + int j=0; + while(true){ + v.push_back(s.substr(j, i)); + j += i; + if(j >= n) break; + } + + string prev = ""; + int cnt = 0; + for(j=0; j t.size()) + ans = t.size(); + } + return ans; +} From 9a3e7e22b336ba1c2abba56b33791adf54ff0694 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 23 Aug 2021 02:21:00 +0900 Subject: [PATCH 052/213] BOJ --- BaekJoon/22953_binarySearch.py | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 BaekJoon/22953_binarySearch.py diff --git a/BaekJoon/22953_binarySearch.py b/BaekJoon/22953_binarySearch.py new file mode 100644 index 0000000..5b95590 --- /dev/null +++ b/BaekJoon/22953_binarySearch.py @@ -0,0 +1,35 @@ +import sys +input = sys.stdin.readline +sys.setrecursionlimit(10**6) + + +def bf(ls2, index, remainD): + global ans + if remainD == 0 or index >= len(ls2): + l = 1 + r = 1_000_000_000_001 + while l<=r: + tmp = 0 + mid = (l+r)//2 + for i in range(N): + tmp += mid//ls2[i] + if tmp >= K: + r = mid - 1 + ans = min(ans, mid) + else: + l = mid + 1 + + return + if index >= len(ls2): + return + for i in range(remainD+1): + if ls2[index] >= i+1: + ls2[index] -= i + bf(ls2, index+1, remainD-i) + ls2[index] += i + +N,K,C = map(int,input().split()) +cook = list(map(int,input().split())) +ans = 2e13 +bf(cook, 0, C) +print(ans) From 6fee7c1f86eb7e7d85f85c4bd4131c05335ad151 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 24 Aug 2021 20:00:27 +0900 Subject: [PATCH 053/213] Create 9655.py --- BaekJoon/9655.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 BaekJoon/9655.py diff --git a/BaekJoon/9655.py b/BaekJoon/9655.py new file mode 100644 index 0000000..1abb996 --- /dev/null +++ b/BaekJoon/9655.py @@ -0,0 +1,2 @@ +n = int(input()) +print("SK" if n%2 else "CY") From 40940d5c32751a03317fe630f73172894b9346a5 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 25 Aug 2021 23:43:32 +0900 Subject: [PATCH 054/213] BOJ --- BaekJoon/2156_dp.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 BaekJoon/2156_dp.py diff --git a/BaekJoon/2156_dp.py b/BaekJoon/2156_dp.py new file mode 100644 index 0000000..3afde99 --- /dev/null +++ b/BaekJoon/2156_dp.py @@ -0,0 +1,15 @@ +import sys +input = sys.stdin.readline + +ls = [0 for _ in range(10001)] +dp = [0 for _ in range(10001)] + +n = int(input()) +for i in range(1, n+1): + ls[i] = int(input()) +dp[1] = ls[1] +dp[2] = ls[1] + ls[2] +for i in range(3, n+1): + dp[i] = max(max(dp[i-2] + ls[i], dp[i-3]+ls[i-1]+ls[i]), dp[i-1]) + +print(max(dp)) From 299eb26f6b70473bb50064d55627626ee9979a15 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 26 Aug 2021 22:52:11 +0900 Subject: [PATCH 055/213] kakao --- "kakaoInternship/\354\272\220\354\213\234.py" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "kakaoInternship/\354\272\220\354\213\234.py" diff --git "a/kakaoInternship/\354\272\220\354\213\234.py" "b/kakaoInternship/\354\272\220\354\213\234.py" new file mode 100644 index 0000000..cbb5fcd --- /dev/null +++ "b/kakaoInternship/\354\272\220\354\213\234.py" @@ -0,0 +1,21 @@ +def solution(cacheSize, cities): + answer = 0 + ls = [] + for city in cities: + city = city.lower() + if cacheSize == 0: + answer += 5 + continue + if city in ls: + answer += 1 + ls.remove(city) + ls.append(city) + else: + if len(ls) == cacheSize: + del ls[0] + ls.append(city) + else: + ls.append(city) + answer += 5 + + return answer From d0de7e178527cf27afdce939d8809cdbfa63d87c Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 27 Aug 2021 13:36:02 +0900 Subject: [PATCH 056/213] BOJ --- BaekJoon/2565.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 BaekJoon/2565.py diff --git a/BaekJoon/2565.py b/BaekJoon/2565.py new file mode 100644 index 0000000..d124dd2 --- /dev/null +++ b/BaekJoon/2565.py @@ -0,0 +1,15 @@ +from bisect import bisect_left + +n = int(input()) +ls = [list(map(int,input().split())) for _ in range(n)] +ls.sort(key=lambda x : x[0]) +# print(ls) +ans = [ls[0][1]] +for i, j in ls: + # print(ans, j) + if ans[-1] < j: + ans.append(j) + else: + idx = bisect_left(ans, j) + ans[idx] =j +print(len(ls) - len(ans)) From 7aae115a6efa7487392768e5fb98274b91bb2c1f Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 28 Aug 2021 17:07:21 +0900 Subject: [PATCH 057/213] BOJ --- BaekJoon/2623.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 BaekJoon/2623.py diff --git a/BaekJoon/2623.py b/BaekJoon/2623.py new file mode 100644 index 0000000..2b7057a --- /dev/null +++ b/BaekJoon/2623.py @@ -0,0 +1,47 @@ +import sys +from collections import deque +input = sys.stdin.readline + +n, m = map(int,input().split()) + +graph = [[] for _ in range(n+1)] +ind = [0 for _ in range(n+1)] +ans = [] + +for _ in range(m): + ls = list(map(int,input().split())) + if ls[0] == 0: + continue + prev = ls[1] + for i in range(2, ls[0]+1): + graph[prev].append(ls[i]) + prev = ls[i] + ind[ls[i]] += 1 + +dq = deque() + + +for i in range(1, n+1): + if ind[i] == 0: + dq.append(i) + +flag = True + +while dq: + cur = dq.pop() + ans.append(cur) + for j in graph[cur]: + ind[j] -= 1 + if ind[j] == 0: + dq.append(j) + +for i in ind: + if i == 1: + flag = False + break + +if flag: + for i in range(len(ans)): + print(ans[i]) +else: + print(0) From 4cd8ab5f147c735a75b769b8e4f16cd3724ce7da Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 29 Aug 2021 19:58:30 +0900 Subject: [PATCH 058/213] BOJ --- BaekJoon/1018.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 BaekJoon/1018.py diff --git a/BaekJoon/1018.py b/BaekJoon/1018.py new file mode 100644 index 0000000..8290246 --- /dev/null +++ b/BaekJoon/1018.py @@ -0,0 +1,26 @@ +import sys +input = sys.stdin.readline + +n, m = map(int, input().split()) +l = [] +mini = [] + +for _ in range(n): + l.append(input()) + +for a in range(n - 7): + for i in range(m - 7): + idx1 = 0 + idx2 = 0 + for b in range(a, a + 8): + for j in range(i, i + 8): + if (j + b)%2 == 0: + if l[b][j] != 'W': idx1 += 1 + if l[b][j] != 'B': idx2 += 1 + else : + if l[b][j] != 'B': idx1 += 1 + if l[b][j] != 'W': idx2 += 1 + mini.append(idx1) + mini.append(idx2) + +print(min(mini)) From d59e95fe92994fb82f34b63a9ac993a48055aeca Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 30 Aug 2021 21:09:50 +0900 Subject: [PATCH 059/213] Create 1062.py --- BaekJoon/1062.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 BaekJoon/1062.py diff --git a/BaekJoon/1062.py b/BaekJoon/1062.py new file mode 100644 index 0000000..e62d0ab --- /dev/null +++ b/BaekJoon/1062.py @@ -0,0 +1,30 @@ +import sys +from itertools import combinations +input = sys.stdin.readline + +n,m = map(int,input().split()) + +t = [0 for _ in range(n)] + +for i in range(n): + s = input().rstrip() + for j in range(len(s)): + t[i] |= 1<<(ord(s[j]) - ord('a')) +if m < 5: + print(0) +else: + tmpLs = [i for i in range(26)] + set1 = set(list(chr(i) for i in range(97, 123))) + set2 = set(['a','n','c','i','t']) + set1 = list(set1-set2) + ans =0 + for it in combinations(set1, m-5): + bit = 532741 + cnt = 0 + for j in it: + bit |= 1<<(ord(j) - ord('a')) + for i in range(n): + if t[i] & bit == t[i]: + cnt += 1 + ans = max(ans, cnt) + print(ans) From 1f5ed9898dc148098f4306072ba4c729b421e9aa Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 31 Aug 2021 20:32:59 +0900 Subject: [PATCH 060/213] BOJ --- BaekJoon/1074.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 BaekJoon/1074.cpp diff --git a/BaekJoon/1074.cpp b/BaekJoon/1074.cpp new file mode 100644 index 0000000..84783a2 --- /dev/null +++ b/BaekJoon/1074.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; + +int x, y, N, ans =0; + +void solve(int sx, int sy, int depth){ + if(sx>=pow(2,N) ||sx<0 || sy>=pow(2,N) || sy<0 || depth>N) return; + if(x=sy && y<=y1){ solve(sx, sy, depth+1); } + else if(x>=x1 && x<=x2 && y>=sy && y<=y1){ + ans+=cnt; + solve(x1+1, sy, depth+1); + } + else if(x>=sx && x<=x1 && y>=y1 && y<=y2){ + ans+=2*cnt; + solve(sx, y1+1, depth+1); + } + else{ + ans+=3*cnt; + solve(x1+1, y1+1, depth+1); + } +} + +int main(){ + cin>>N>>y>>x; + solve(0,0,1); + cout< Date: Wed, 1 Sep 2021 23:57:06 +0900 Subject: [PATCH 061/213] BOJ --- BaekJoon/1080.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/1080.py diff --git a/BaekJoon/1080.py b/BaekJoon/1080.py new file mode 100644 index 0000000..eeff361 --- /dev/null +++ b/BaekJoon/1080.py @@ -0,0 +1,33 @@ +#1080 +import sys +input = sys.stdin.readline + +def change(x,y): + for i in range(3): + for j in range(3): + ls1[x+i][y+j] = '1' if ls1[x+i][y+j] == '0' else '0' + +def check(): + for i in range(n): + for j in range(m): + if ls1[i][j] != ls2[i][j]: + return False + return True + +n,m = map(int,input().split()) + +ls1 = [list(input().rstrip()) for _ in range(n)] +ls2 = [list(input().rstrip()) for _ in range(n)] + +cnt = 0 + +for i in range(n-2): + for j in range(m-2): + if ls1[i][j] != ls2[i][j]: + change(i, j) + cnt += 1 + +if check(): + print(cnt) +else: + print(-1) From cc7ec74ba3ec5a6741fe03940875b26c0280afeb Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 2 Sep 2021 23:31:27 +0900 Subject: [PATCH 062/213] BOJ --- BaekJoon/1107.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 BaekJoon/1107.py diff --git a/BaekJoon/1107.py b/BaekJoon/1107.py new file mode 100644 index 0000000..5b5d5cd --- /dev/null +++ b/BaekJoon/1107.py @@ -0,0 +1,21 @@ +import sys +input = sys.stdin.readline + +n = int(input()) +m = int(input()) +ls = list(input().split()) + + +buttonPM = 100000000 + +for i in range(1000001): + flag = True + tmp = list(str(i)) + for j in tmp: + if j in ls: + flag = False + break + if flag: + buttonPM = min(buttonPM, len(str(i)) + abs(n-i)) + +print(min(buttonPM, abs(n-100))) From d8826a04b79b948f94fa0a138b9e4e2f798b62cd Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 3 Sep 2021 12:34:17 +0900 Subject: [PATCH 063/213] BOJ --- BaekJoon/1094.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 BaekJoon/1094.cpp diff --git a/BaekJoon/1094.cpp b/BaekJoon/1094.cpp new file mode 100644 index 0000000..cfa8f28 --- /dev/null +++ b/BaekJoon/1094.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +int main(){ + int rsv = 64; + int base =64; + int x; + int cnt =0; + cin>>x; + while(x!=0){ + if(x=rsv-base){ + x-=base; + rsv-=base; + cnt++; + } + else{ + rsv-=base; + } + } + else if(x == rsv){ + cnt++; + break; + } + } + cout< Date: Sat, 4 Sep 2021 17:12:26 +0900 Subject: [PATCH 064/213] BOJ --- BaekJoon/1120.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 BaekJoon/1120.py diff --git a/BaekJoon/1120.py b/BaekJoon/1120.py new file mode 100644 index 0000000..d9f2fec --- /dev/null +++ b/BaekJoon/1120.py @@ -0,0 +1,14 @@ +s = list(map(str, input().split())) +size = [len(s[i]) for i in range(2)] +dif = size[1] - size[0] +res = 100000 +for i in range(dif+1): + cnt =0 + for j in range(size[0]): + if s[0][j] != s[1][j+i]: + cnt+=1 + res = min(res, cnt) + +print(res) + + From ff1724642e6efd52a59656d59af3616d3c2232cc Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 5 Sep 2021 19:19:51 +0900 Subject: [PATCH 065/213] BOJ --- BaekJoon/1145.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 BaekJoon/1145.cpp diff --git a/BaekJoon/1145.cpp b/BaekJoon/1145.cpp new file mode 100644 index 0000000..39ef4b2 --- /dev/null +++ b/BaekJoon/1145.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main(){ + cin.tie(0); + ios_base::sync_with_stdio(0); + int tmp; + vectornum; + for(int i=0; i<5; i++){ + cin>>tmp; + num.push_back(tmp); + } + sort(num.begin(), num.end()); + int s=num[0]; + while(1){ + int cnt =0; + for(int i=0; i<5; i++){ + if(s%num[i] == 0) + cnt++; + + } + if(cnt>=3)break; + else s++; + } + cout< Date: Mon, 6 Sep 2021 17:30:13 +0900 Subject: [PATCH 066/213] BOJ --- BaekJoon/1145.cpp | 34 ++++++++++++++++++++ BaekJoon/4179.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 BaekJoon/1145.cpp create mode 100644 BaekJoon/4179.py diff --git a/BaekJoon/1145.cpp b/BaekJoon/1145.cpp new file mode 100644 index 0000000..39ef4b2 --- /dev/null +++ b/BaekJoon/1145.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main(){ + cin.tie(0); + ios_base::sync_with_stdio(0); + int tmp; + vectornum; + for(int i=0; i<5; i++){ + cin>>tmp; + num.push_back(tmp); + } + sort(num.begin(), num.end()); + int s=num[0]; + while(1){ + int cnt =0; + for(int i=0; i<5; i++){ + if(s%num[i] == 0) + cnt++; + + } + if(cnt>=3)break; + else s++; + } + cout< Date: Tue, 7 Sep 2021 17:31:39 +0900 Subject: [PATCH 067/213] BOJ --- BaekJoon/4195_DisjointSet.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/4195_DisjointSet.py diff --git a/BaekJoon/4195_DisjointSet.py b/BaekJoon/4195_DisjointSet.py new file mode 100644 index 0000000..fccfd01 --- /dev/null +++ b/BaekJoon/4195_DisjointSet.py @@ -0,0 +1,33 @@ +import sys +input = sys.stdin.readline +sys.setrecursionlimit(10**5) + +def getParent(ls, x): + if ls[x] == x: + return ls[x] + ls[x] = getParent(ls, ls[x]) + return ls[x] + +def unionParent(ls, a, b): + global num + a = getParent(ls, a) + b = getParent(ls, b) + if a != b: + dc[b] = a + num[a] += num[b] + num[b] = num[a] + +for _ in range(int(input())): + m = int(input()) + dc = {} + num = {} + for _ in range(m): + s1, s2 = input().split() + if s1 not in dc: + dc[s1] = s1 + num[s1] = 1 + if s2 not in dc: + dc[s2] = s2 + num[s2] = 1 + unionParent(dc, s1, s2) + print(num[getParent(dc, s1)]) \ No newline at end of file From 3d8949e7874d4b813dab7ad122011e6dff31670b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 8 Sep 2021 23:50:57 +0900 Subject: [PATCH 068/213] BOJ --- BaekJoon/4811.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 BaekJoon/4811.py diff --git a/BaekJoon/4811.py b/BaekJoon/4811.py new file mode 100644 index 0000000..1418ce5 --- /dev/null +++ b/BaekJoon/4811.py @@ -0,0 +1,22 @@ +import sys +input = sys.stdin.readline + +def solve(one, half): + if one == 0: + return 1 + if ls[one][half] != -1: + return ls[one][half] + ls[one][half] = 0 + ls[one][half] += solve(one-1,half+1) + if half != 0: + ls[one][half] += solve(one,half-1) + return ls[one][half] + + +while True: + ls = [[-1 for _ in range(31)] for _ in range(31)] + n = int(input()) + if n == 0: + break + print(solve(n,0)) + From bf8532c7f6dabe8dbb2a2ec5d6a9d4b290ae8bdd Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 12 Sep 2021 23:57:44 +0900 Subject: [PATCH 069/213] BOJ --- BaekJoon/10950.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 BaekJoon/10950.py diff --git a/BaekJoon/10950.py b/BaekJoon/10950.py new file mode 100644 index 0000000..f48d412 --- /dev/null +++ b/BaekJoon/10950.py @@ -0,0 +1,5 @@ +tc = int(input()) +for _ in range(tc): + a,b = map(int,input().split()) + print(a+b) + From efa529d463dd3c8f7a0e14f034d4cdc72cfb0aea Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 14 Sep 2021 16:49:18 +0900 Subject: [PATCH 070/213] BOJ --- BaekJoon/14225.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BaekJoon/14225.py diff --git a/BaekJoon/14225.py b/BaekJoon/14225.py new file mode 100644 index 0000000..89a2508 --- /dev/null +++ b/BaekJoon/14225.py @@ -0,0 +1,20 @@ +import sys +input = sys.stdin.readline + +def recursive(idx, val): + global ans + if idx == n: + return + ans.add(val + ls[idx]) + recursive(idx + 1, val + ls[idx]) + recursive(idx + 1, val) + +n = int(input()) +ls = list(map(int,input().split())) +ans = set(ls[:]) +recursive(0,0) + +for i in range(1, 2000000): + if i not in ans: + print(i) + break From 11d480731d3d2474efc51d2dc4ef7f04383f3666 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 15 Sep 2021 20:34:32 +0900 Subject: [PATCH 071/213] BOJ --- BaekJoon/11650.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 BaekJoon/11650.py diff --git a/BaekJoon/11650.py b/BaekJoon/11650.py new file mode 100644 index 0000000..bc0827a --- /dev/null +++ b/BaekJoon/11650.py @@ -0,0 +1,5 @@ +n = int(input()) +ls = [list(map(int,input().split())) for _ in range(n)] +ls = sorted(ls, key = lambda x:(x[0], x[1])) +for it in ls: + print(*it) From 558e155c018f99503730340a2b140ee52c3d9172 Mon Sep 17 00:00:00 2001 From: geunnii Date: Fri, 17 Sep 2021 15:30:54 +0900 Subject: [PATCH 072/213] BOJ --- BaekJoon/2268_segment_tree.cpp | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 BaekJoon/2268_segment_tree.cpp diff --git a/BaekJoon/2268_segment_tree.cpp b/BaekJoon/2268_segment_tree.cpp new file mode 100644 index 0000000..8510688 --- /dev/null +++ b/BaekJoon/2268_segment_tree.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; +typedef long long ll; + +vector num(1000001, 0); +vector tree(4000001, 0); + +ll query(ll left, ll right, ll node, ll queryLeft, ll queryRight) { + if (right < queryLeft || left > queryRight) return 0; //범위 밖 -> 구할 수 없는 경우 + else if (left >= queryLeft && right <= queryRight) { //범위 안 -> 판단 가능 + return tree[node]; + } + else{ //범위에 걸쳐있음 -> 판단 불가 + ll mid = (left + right) / 2; + return query(left, mid, node * 2, queryLeft, queryRight) + query(mid + 1, right, node * 2 + 1, queryLeft, queryRight); + } +} + +void update(ll left, ll right, ll node, ll target, ll diff) { + if(!(left <= target && target <= right)) return; + //node가 범위 안 -> 업데이트 + tree[node] += diff; + if (left != right) { //리프노드가 아닌 경우 자식 업데이트 + ll mid = (left + right) / 2; + update(left, mid, node * 2, target, diff); + update(mid+1, right, node * 2+1, target, diff); + } +} + +int main() { + ll N, M; + ll n, a, b, c; + scanf("%lld%lld", &N, &M); + + for (int i = 0; i < M; i++) { + scanf("%lld%lld%lld", &a, &b, &c); + + if (a == 1) { //b번째 수를 c로 바꾼다 + ll diff = c - num[b]; + num[b] = c; + update(1, N, 1, b, diff); + } + else if (a == 0) { //b번째 수부터 c번째 수까지의 합 출력 + if(b>c){ + b ^= c; + c ^= b; + b ^= c; + } + printf("%lld\n", query(1, N, 1, b, c)); + } + } + + return 0; +} \ No newline at end of file From 9a62e2d8a62423d1537ed492cd5adf46f51848a1 Mon Sep 17 00:00:00 2001 From: geunnii Date: Fri, 17 Sep 2021 22:38:29 +0900 Subject: [PATCH 073/213] BOJ --- BaekJoon/2268_fenwickTree.cpp | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 BaekJoon/2268_fenwickTree.cpp diff --git a/BaekJoon/2268_fenwickTree.cpp b/BaekJoon/2268_fenwickTree.cpp new file mode 100644 index 0000000..ff20d40 --- /dev/null +++ b/BaekJoon/2268_fenwickTree.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; +typedef long long ll; +vector Tree(1000001, 0); + +void Update(int Idx, ll Value){ + while(Idx < Tree.size()){ + Tree[Idx]+=Value; + Idx = Idx + (Idx & - Idx); + } +} + +ll getSum(int Idx){ + ll Res =0; + while(Idx > 0){ + Res += Tree[Idx]; + Idx = Idx - (Idx & -Idx); + } + return Res; +} + +int main(){ + int N, M; + int a, b, c; + scanf("%d%d", &N, &M); + for(int i=0; i c){ + b ^= c; + c ^= b; + b ^= c; + } + printf("%lld\n", getSum(c) - getSum(b-1)); + }else{ + ll diff = c - getSum(b) + getSum(b - 1); + Update(b, diff); + } + } + return 0; +} \ No newline at end of file From 4c5479f4c344f345887069fc3eb3e1107faf1e2d Mon Sep 17 00:00:00 2001 From: geunnii Date: Sat, 18 Sep 2021 15:03:50 +0900 Subject: [PATCH 074/213] BOJ --- BaekJoon/11658_fenwickTree.py | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 BaekJoon/11658_fenwickTree.py diff --git a/BaekJoon/11658_fenwickTree.py b/BaekJoon/11658_fenwickTree.py new file mode 100644 index 0000000..2bc6ab7 --- /dev/null +++ b/BaekJoon/11658_fenwickTree.py @@ -0,0 +1,39 @@ +import sys +input = sys.stdin.readline + +def update(r, c, value): + tmp = c + while r <= len(tree): + c = tmp + while c <= len(tree): + tree[r][c] = tree[r][c] + value + c += (c & -c) + r += (r & -r) + +def getSum(r, c): + res = 0 + tmp = c + while r != 0 and r < len(tree): + c = tmp + while c != 0 and c < len(tree): + res += tree[r][c] + c -= (c&-c) + r -= (r & -r) + return res + + +N,M = map(int,input().split()) +ls = [[0] * (N+1)] + [[0] + list(map(int,input().split())) for _ in range(N)] +tree = [[0]* (N+1) for _ in range(N+1)] + +for i in range(1, N+1): + for j in range(1, N+1): + update(i, j, ls[i][j]) + +for i in range(M): + t = list(map(int,input().split())) + if t[0] == 1: + print(getSum(t[3], t[4]) - getSum(t[1]-1, t[4]) - getSum(t[3], t[2]-1) + getSum(t[1]-1, t[2]-1)) + else: + update(t[1], t[2], t[3] - ls[t[1]][t[2]]) + ls[t[1]][t[2]] = t[3] \ No newline at end of file From 9398b5525928d1d2ada0dd3fe930e8bc63128b14 Mon Sep 17 00:00:00 2001 From: geunnii Date: Sun, 19 Sep 2021 17:48:37 +0900 Subject: [PATCH 075/213] BOJ --- BaekJoon/2688_dp.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 BaekJoon/2688_dp.py diff --git a/BaekJoon/2688_dp.py b/BaekJoon/2688_dp.py new file mode 100644 index 0000000..4078a95 --- /dev/null +++ b/BaekJoon/2688_dp.py @@ -0,0 +1,14 @@ +n = int(input()) +dp = [[0 for _ in range(10)] for _ in range(101)] +for i in range(10, 0, -1): + dp[0][10-i] = i +dp[1][0] = sum(dp[0]) + +for i in range(1, 100): + for j in range(1, 10): + dp[i][j] = dp[i][j-1] - dp[i-1][j-1] + dp[i+1][0] = sum(dp[i]) + +for _ in range(n): + a = int(input()) + print(dp[a-1][0]) \ No newline at end of file From 2dec35a749809a7efaa7a80c4d2eff7bcba8ec7d Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 20 Sep 2021 11:13:51 +0900 Subject: [PATCH 076/213] BOJ --- BaekJoon/18394.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 BaekJoon/18394.py diff --git a/BaekJoon/18394.py b/BaekJoon/18394.py new file mode 100644 index 0000000..47a8b92 --- /dev/null +++ b/BaekJoon/18394.py @@ -0,0 +1,49 @@ +import sys +from bisect import bisect_left +from collections import deque +input = sys.stdin.readline +sys.setrecursionlimit(30000) + +a = [False,False] + [True]*(1000001-1) +primes=[] +for i in range(2,1000001): + if a[i]: + primes.append(i) + for j in range(2*i, 1000001+1, i): + a[j] = False + +def bfs(): + visit = [2e9 for _ in range(1000001)] + dq = deque() + dq.append(N) + visit[N] = 0 + tm = 0 + while dq: + tm += 1 + sz = len(dq) + while sz: + sz-=1 + cur = dq.popleft() + for k in [cur + 1, cur // 2, cur //3, cur -1]: + if k >= 1000001: + continue + if k <= 0: + continue + if visit[k] > tm: + visit[k] = tm + dq.append(k) + if A<=k<=B and k in primes: + return tm + +for _ in range(int(input())): + N,A,B = map(int,input().split()) + + idx1 = bisect_left(primes, A) + idx2 = bisect_left(primes, B) + + if idx1 == idx2 and primes[idx1] < A or primes[idx1] > B: + print(-1) + elif A<=N<=B and N in primes: + print(0) + else: + print(bfs()) From 0d7a9ac2f3041e59613630e84476b2a5d71ae3c6 Mon Sep 17 00:00:00 2001 From: geunnii Date: Tue, 21 Sep 2021 21:19:27 +0900 Subject: [PATCH 077/213] BOJ --- BaekJoon/23509_topological_sort.py | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 BaekJoon/23509_topological_sort.py diff --git a/BaekJoon/23509_topological_sort.py b/BaekJoon/23509_topological_sort.py new file mode 100644 index 0000000..a93b912 --- /dev/null +++ b/BaekJoon/23509_topological_sort.py @@ -0,0 +1,55 @@ +import sys +import heapq as h +import copy +input = sys.stdin.readline + +n = int(input()) +nameToIdx = {} +names = [0 for _ in range(2*n)] +idx = 0 +graph = [[] for _ in range(2*n)] +ind = [0 for _ in range(2*n)] + +for _ in range(n): + a,b = input().split() + if a not in nameToIdx: + nameToIdx[a] = idx + names[idx] = a + idx+=1 + if b not in nameToIdx: + nameToIdx[b] = idx + names[idx] = b + idx += 1 + graph[nameToIdx[a]].append(nameToIdx[b]) + ind[nameToIdx[b]] += 1 + +ans = [] +q = [] +for i in range(idx): + if ind[i] == 0: + h.heappush(q, names[i]) + +tq = [] +while q: + cur = h.heappop(q) + + ans.append(cur) + for nxt in graph[nameToIdx[cur]]: + ind[nxt] -= 1 + if ind[nxt] == 0: + h.heappush(tq, names[nxt]) + + if len(q) == 0: + q = copy.deepcopy(tq) + tq = [] + +flag = True +for i in ind: + if i != 0: + flag = False + +if flag: + for i in ans: + print(i) +else: + print(-1) \ No newline at end of file From 1199eb0148b8e7bda98f6ac0f9617d390f0ccabc Mon Sep 17 00:00:00 2001 From: geunnii Date: Wed, 22 Sep 2021 19:24:56 +0900 Subject: [PATCH 078/213] BOJ --- BaekJoon/15591_bfs.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/15591_bfs.py diff --git a/BaekJoon/15591_bfs.py b/BaekJoon/15591_bfs.py new file mode 100644 index 0000000..a3a2003 --- /dev/null +++ b/BaekJoon/15591_bfs.py @@ -0,0 +1,33 @@ +import sys +from collections import deque +input = sys.stdin.readline +n,q = map(int,input().split()) +ls = [[] for _ in range(n+1)] + +for _ in range(n-1): + a,b,c = map(int,input().split()) + ls[a].append([b, c]) + ls[b].append([a, c]) + +for _ in range(q): + a,b = map(int,input().split()) + cnt = 0 + q = deque() + visited = [0 for _ in range(n+1)] + visited[b] = 1 + + for nxt, cost in ls[b]: + visited[nxt] = 1 + q.append((nxt, cost)) + + while q: + cur, cost = q.popleft() + # print(cur) + if cost >= a: + cnt += 1 + for nxt, nxtCost in ls[cur]: + if visited[nxt] == 0: + visited[nxt] = 1 + q.append((nxt, min(nxtCost, cost))) + + print(cnt) \ No newline at end of file From b0fcc1fa020e9d5b863ffd9e9639ee10b7bdfa72 Mon Sep 17 00:00:00 2001 From: geunnii Date: Thu, 23 Sep 2021 09:16:08 +0900 Subject: [PATCH 079/213] BOJ --- BaekJoon/10021_MST.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 BaekJoon/10021_MST.cpp diff --git a/BaekJoon/10021_MST.cpp b/BaekJoon/10021_MST.cpp new file mode 100644 index 0000000..f805754 --- /dev/null +++ b/BaekJoon/10021_MST.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +using namespace std; +struct Node{ + int w, u, v; +}; +struct compare{ + bool operator()(const Node& n1, const Node& n2){ + return n1.w > n2.w; + } +}; + +vectorparent; + +int getParent(int node){ + if(parent[node] == node) + return parent[node]; + return parent[node] = getParent(parent[node]); +} + +void unionParent(int node1, int node2){ + int a = getParent(node1); + int b = getParent(node2); + + if(a>b){ + parent[a] = b; + }else{ + parent[b] = a; + } +} + +int main(){ + int N, C; + scanf("%d%d", &N, &C); + vector>v(N, vector(2)); + parent.resize(N+1); + for(int i=0; i, compare>pq; + for(int i=0; i= C){ + + pq.push({tmp, i, j}); + } + } + } + int ans = 0; + while(!pq.empty()){ + Node cur = pq.top(); pq.pop(); + if(getParent(cur.u) != getParent(cur.v)){ + unionParent(cur.u, cur.v); + ans += cur.w; + } + } + bool flag = true; + for(int i=1; i Date: Thu, 23 Sep 2021 10:16:19 +0900 Subject: [PATCH 080/213] BOJ --- BaekJoon/10999_lazyPropagation.cpp | 78 ++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 BaekJoon/10999_lazyPropagation.cpp diff --git a/BaekJoon/10999_lazyPropagation.cpp b/BaekJoon/10999_lazyPropagation.cpp new file mode 100644 index 0000000..a303402 --- /dev/null +++ b/BaekJoon/10999_lazyPropagation.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +using namespace std; +typedef long long ll; +vector nodes; +vector tree; +vector lazy; + +ll init(int node, int s, int e){ + if(s == e) + return tree[node] = nodes[s]; + int mid = (s+e)/2; + return tree[node] = init(2*node, s, mid) + init(2*node + 1, mid+1, e); +} + +void lazy_update(int node, int s, int e){ + if(lazy[node] == 0) + return; + tree[node] += ((e-s+1) * lazy[node]); + if(e != s){ + lazy[node*2] += lazy[node]; + lazy[node*2+1] += lazy[node]; + } + lazy[node] = 0; +} + +void update(int node, int s, int e, int l, int r, int diff){ + lazy_update(node, s, e); + //구간 밖에 있으면 + if(r < s || l > e){ + return; + } + if(l <= s && e <= r){ + lazy[node] += diff; + lazy_update(node, s, e); + return; + } + int mid = (s+e)/2; + update(node*2, s, mid, l, r, diff); + update(node*2 + 1, mid +1, e, l, r, diff); + //루트로 전파해줌 + tree[node] = tree[2*node] + tree[2*node + 1]; +} + +ll sum_(int node, int s, int e, int l, int r){ + lazy_update(node, s, e); + if(l > e || r < s) + return 0; + if(l <= s && e <= r){ + return tree[node]; + } + int mid = (s+e)/2; + return sum_(node*2, s, mid, l, r) + sum_(node*2+1, mid + 1, e, l, r); +} + +int main() { + ll N, M, K, cmd, b, c, d; + scanf("%lld%lld%lld", &N, &M, &K); + tree.resize(4*N); + nodes.resize(N); + lazy.resize(4*N, 0); + for(int i=0; i Date: Fri, 24 Sep 2021 11:44:43 +0900 Subject: [PATCH 081/213] BOJ --- BaekJoon/12837_segmentTree.cpp | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 BaekJoon/12837_segmentTree.cpp diff --git a/BaekJoon/12837_segmentTree.cpp b/BaekJoon/12837_segmentTree.cpp new file mode 100644 index 0000000..5033287 --- /dev/null +++ b/BaekJoon/12837_segmentTree.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; +typedef long long ll; + +vector num(1000001, 0); +vector tree(4000001, 0); + +ll query(ll left, ll right, ll node, ll queryLeft, ll queryRight) { + if (right < queryLeft || left > queryRight) return 0; + else if (left >= queryLeft && right <= queryRight) { + return tree[node]; + } + else{ + ll mid = (left + right) / 2; + return query(left, mid, node * 2, queryLeft, queryRight) + query(mid + 1, right, node * 2 + 1, queryLeft, queryRight); + } +} + +void update(ll left, ll right, ll node, ll target, ll diff) { + if(!(left <= target && target <= right)) return; + tree[node] += diff; + if (left != right) { + ll mid = (left + right) / 2; + update(left, mid, node * 2, target, diff); + update(mid+1, right, node * 2+1, target, diff); + } +} + +int main() { + ll N, M; + ll n, a, b, c; + scanf("%lld%lld", &N, &M); + + for (int i = 0; i < M; i++) { + scanf("%lld%lld%lld", &a, &b, &c); + + if (a == 1) { + num[b] = c+num[b]; + update(1, N, 1, b, c); + } + else if (a == 2) { + if(b>c){ + b ^= c; + c ^= b; + b ^= c; + } + printf("%lld\n", query(1, N, 1, b, c)); + } + } + + return 0; +} From ebc8060ebca68ed132616299c3ba44ebad324dae Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 25 Sep 2021 23:00:28 +0900 Subject: [PATCH 082/213] BOJ --- BaekJoon/14002_LIS.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 BaekJoon/14002_LIS.py diff --git a/BaekJoon/14002_LIS.py b/BaekJoon/14002_LIS.py new file mode 100644 index 0000000..f016d61 --- /dev/null +++ b/BaekJoon/14002_LIS.py @@ -0,0 +1,39 @@ +#Equal to 14003 +import sys +from bisect import bisect_left +input = sys.stdin.readline + +n = int(input()) +ls = list(map(int,input().split())) + +dp = [[0,0] for _ in range(n)] + +dp[0] = [1,ls[0]] +tmp = [ls[0]] + +i = 1 +j = 2 + +while True: + if i >= n: + break + if ls[i] > tmp[-1]: + dp[i] = [j,ls[i]] + tmp.append(ls[i]) + j+=1 + else: + idx = bisect_left(tmp, ls[i]) + dp[i] = [idx+1, ls[i]] + tmp[idx] = ls[i] + i += 1 +comp = len(tmp) +ans = [] +for i in range(len(dp)-1, -1 ,-1): + if dp[i][0] == comp: + ans.append(dp[i][1]) + comp -= 1 + +print(len(tmp)) +for it in sorted(ans): + print(it, end = ' ') +print() From 0acff534527c7a6a87f7895d088e51d579d68a67 Mon Sep 17 00:00:00 2001 From: geunnii Date: Sun, 26 Sep 2021 23:18:58 +0900 Subject: [PATCH 083/213] BOJ --- BaekJoon/14427_segmentTree.cpp | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 BaekJoon/14427_segmentTree.cpp diff --git a/BaekJoon/14427_segmentTree.cpp b/BaekJoon/14427_segmentTree.cpp new file mode 100644 index 0000000..e802a03 --- /dev/null +++ b/BaekJoon/14427_segmentTree.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; + +vectorarr(100001, 0); +vectortree(400004); +int N; + +int init(int node, int s, int e){ + if(s == e){ + return tree[node] = s; + } + int mid = (s+e)/2; + int a = init(2*node, s, mid); + int b = init(2*node+1, mid + 1, e); + if (arr[a] > arr[b]){ + return tree[node] = b; + }else if(arr[a] == arr[b]){ + return tree[node] = a>b?b:a; + }else{ + return tree[node] = a; + } +} + +int update(int node, int s, int e, int idx, int val){ + if(idx < s || e < idx){ + return tree[node]; + } + if(s == e){ + return tree[node]; + } + int mid = (s+e)/2; + int a = update(2 * node, s, mid, idx, val); + int b = update(2 * node + 1, mid + 1, e, idx, val); + if(arr[a] == arr[b]){ + return tree[node] = min(a, b); + }else{ + return tree[node] = arr[a]>arr[b] ? b:a; + } +} + +int main(){ + int q, cmd, idx, val; + scanf("%d", &N); + for(int i=1; i<=N; ++i) + scanf("%d", &arr[i]); + init(1, 1, N); + scanf("%d", &q); + for(int i=0; i Date: Mon, 27 Sep 2021 22:34:53 +0900 Subject: [PATCH 084/213] BOJ --- BaekJoon/14427_segmentTree.cpp | 60 ++++++++++++++++++++++++++++++++++ BaekJoon/15657.py | 10 ++++++ 2 files changed, 70 insertions(+) create mode 100644 BaekJoon/14427_segmentTree.cpp create mode 100644 BaekJoon/15657.py diff --git a/BaekJoon/14427_segmentTree.cpp b/BaekJoon/14427_segmentTree.cpp new file mode 100644 index 0000000..e802a03 --- /dev/null +++ b/BaekJoon/14427_segmentTree.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; + +vectorarr(100001, 0); +vectortree(400004); +int N; + +int init(int node, int s, int e){ + if(s == e){ + return tree[node] = s; + } + int mid = (s+e)/2; + int a = init(2*node, s, mid); + int b = init(2*node+1, mid + 1, e); + if (arr[a] > arr[b]){ + return tree[node] = b; + }else if(arr[a] == arr[b]){ + return tree[node] = a>b?b:a; + }else{ + return tree[node] = a; + } +} + +int update(int node, int s, int e, int idx, int val){ + if(idx < s || e < idx){ + return tree[node]; + } + if(s == e){ + return tree[node]; + } + int mid = (s+e)/2; + int a = update(2 * node, s, mid, idx, val); + int b = update(2 * node + 1, mid + 1, e, idx, val); + if(arr[a] == arr[b]){ + return tree[node] = min(a, b); + }else{ + return tree[node] = arr[a]>arr[b] ? b:a; + } +} + +int main(){ + int q, cmd, idx, val; + scanf("%d", &N); + for(int i=1; i<=N; ++i) + scanf("%d", &arr[i]); + init(1, 1, N); + scanf("%d", &q); + for(int i=0; i Date: Tue, 28 Sep 2021 23:48:25 +0900 Subject: [PATCH 085/213] BOJ --- BaekJoon/15656.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 BaekJoon/15656.py diff --git a/BaekJoon/15656.py b/BaekJoon/15656.py new file mode 100644 index 0000000..b20307c --- /dev/null +++ b/BaekJoon/15656.py @@ -0,0 +1,10 @@ +import sys +from itertools import product +input = sys.stdin.readline + +n,m = map(int,input().split()) +ls = sorted(list(map(int,input().split()))) +for it in product(ls,repeat = m): + for i in it: + print(i, end = ' ') + print() \ No newline at end of file From bf80b454b11e7e6200c037814d7ff350a0d9941c Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 2 Oct 2021 18:08:54 +0900 Subject: [PATCH 086/213] BOJ --- BaekJoon/23057.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 BaekJoon/23057.cpp diff --git a/BaekJoon/23057.cpp b/BaekJoon/23057.cpp new file mode 100644 index 0000000..78a3f23 --- /dev/null +++ b/BaekJoon/23057.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +using namespace std; + +int main() { + int N; + scanf("%d", &N); + vectorv(N+1); + unordered_mapm; + int cnt = 0; + int M = 0; + for (int i = 0; i < N; ++i) { + scanf("%d", &v[i]); + M += v[i]; + } + for (int i = 0; i < (1 << N); ++i) { + int tmp = 0; + for (int j = 0; j < N; ++j) { + if ( i & 1 << j) { + tmp += v[j]; + } + } + if (m.find(tmp) != m.end()) { + continue; + } + else{ + cnt += 1; + m[tmp] = 1; + } + } + printf("%d", M - cnt + 1); + return 0; +} From cde7a22891c4b8df8ff973762f544bbbb89c1bfd Mon Sep 17 00:00:00 2001 From: geunnii Date: Tue, 5 Oct 2021 17:34:15 +0900 Subject: [PATCH 087/213] BOJ --- BaekJoon/13418_MST.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 BaekJoon/13418_MST.cpp diff --git a/BaekJoon/13418_MST.cpp b/BaekJoon/13418_MST.cpp new file mode 100644 index 0000000..590e718 --- /dev/null +++ b/BaekJoon/13418_MST.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +using namespace std; +vectorparent; +struct edge{ + int w, u, v; +}; +struct comp1{ + bool operator()(const edge& e1, const edge& e2){ + return e1.w > e2.w; + } +}; +struct comp2{ + bool operator()(const edge& e1, const edge& e2){ + return e1.w < e2.w; + } +}; +int getParent(int n){ + if(parent[n] == n){ + return n; + } + return parent[n] = getParent(parent[n]); +} + +void unionParent(int n1, int n2){ + int a = getParent(n1); + int b = getParent(n2); + if(a!=b){ + parent[a] = b; + } +} + +int main(){ + int n, m, tw, tu, tv; + scanf("%d%d", &n, &m); + priority_queue, comp1>minQ; + priority_queue, comp2>maxQ; + + for(int i=0; i Date: Tue, 5 Oct 2021 18:21:36 +0900 Subject: [PATCH 088/213] BOJ --- BaekJoon/2261_DivideAndConquer.py | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 BaekJoon/2261_DivideAndConquer.py diff --git a/BaekJoon/2261_DivideAndConquer.py b/BaekJoon/2261_DivideAndConquer.py new file mode 100644 index 0000000..4e00be3 --- /dev/null +++ b/BaekJoon/2261_DivideAndConquer.py @@ -0,0 +1,52 @@ +import sys +input = sys.stdin.readline +sys.setrecursionlimit(10**4) + +def getDistance(ls1, ls2): + return (ls1[0]-ls2[0]) **2 + (ls1[1] - ls2[1])**2 + +def closest(lst): + if len(lst) <= 3: + dst = 2e9 + for i in range(len(lst)): + for j in range(i+1, len(lst)): + dst = min(dst, getDistance(lst[i], lst[j])) + return dst + + mid = len(lst)//2 + pointMid = lst[mid] + + lstL = lst[mid:] + lstR = lst[:mid] + + distanseL = closest(lstL) + distanseR = closest(lstR) + + d = min(distanseL, distanseR) + + points = [] + for i in range(len(lst)): + if (lst[i][0] - pointMid[0])**2 < d: + points.append(lst[i]) + + if len(points) <= 1: + return d + + points = sorted(points, key=lambda x:x[1]) + for i in range(len(points)): + for j in range(i+1, len(points)): + if (points[i][1] - points[j][1])**2 > d: + break + d = min(d, getDistance(points[i], points[j])) + + return d + + +n = int(input()) +ls = [tuple(map(int,input().split())) for _ in range(n)] + +if len(ls) != len(set(ls)): + print(0) +else: + ls = sorted(ls, key=lambda x:x[0]) + print(closest(ls)) From 0736a2cb2c27a7165113450a437c93f445da01be Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 6 Oct 2021 23:34:02 +0900 Subject: [PATCH 089/213] BOJ --- BaekJoon/2961.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 BaekJoon/2961.py diff --git a/BaekJoon/2961.py b/BaekJoon/2961.py new file mode 100644 index 0000000..cee5951 --- /dev/null +++ b/BaekJoon/2961.py @@ -0,0 +1,16 @@ +n = int(input()) +ls = [] +for _ in range(n): + tmp = list(map(int,input().split())) + ls.append(tmp) +ans = 2e20 + +for i in range(1, 2**n): + sour = 1 + bit = 0 + for j in range(n): + if i & (1< Date: Thu, 7 Oct 2021 23:56:29 +0900 Subject: [PATCH 090/213] BOJ --- BaekJoon/12738_LIS.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 BaekJoon/12738_LIS.py diff --git a/BaekJoon/12738_LIS.py b/BaekJoon/12738_LIS.py new file mode 100644 index 0000000..81567be --- /dev/null +++ b/BaekJoon/12738_LIS.py @@ -0,0 +1,14 @@ +import sys +from bisect import bisect_left +input = sys.stdin.readline + +N = int(input()) +ls = list(map(int,input().split())) +tmp = [ls[0]] +for i in range(1, len(ls)): + if tmp[-1] < ls[i]: + tmp.append(ls[i]) + else: + idx = bisect_left(tmp, ls[i]) + tmp[idx] = ls[i] +print(len(tmp)) From 57e569595e4adb79e551b716d4951713385dfb53 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 8 Oct 2021 23:47:27 +0900 Subject: [PATCH 091/213] BOJ --- BaekJoon/18119_bitmask.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 BaekJoon/18119_bitmask.py diff --git a/BaekJoon/18119_bitmask.py b/BaekJoon/18119_bitmask.py new file mode 100644 index 0000000..c2f488a --- /dev/null +++ b/BaekJoon/18119_bitmask.py @@ -0,0 +1,34 @@ +import sys +input = sys.stdin.readline +v = ['a','e','i','o','u'] +idxDc = {} +idx = 0 +for i in range(26): + if chr(97+i) not in v: + idxDc[chr(97+i)] = idx + idx += 1 + +ls = [] +n, m = map(int, input().split()) +for i in range(n): + num = 0 + tmp = list(set(input().rstrip())) + for j in tmp: + if j not in v: + num += 1< Date: Sat, 9 Oct 2021 23:22:07 +0900 Subject: [PATCH 092/213] BOJ --- BaekJoon/23080.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 BaekJoon/23080.py diff --git a/BaekJoon/23080.py b/BaekJoon/23080.py new file mode 100644 index 0000000..4a8e9a3 --- /dev/null +++ b/BaekJoon/23080.py @@ -0,0 +1,10 @@ +n = int(input()) +s = input().rstrip() +ans = '' +cnt = n +for i in s: + if cnt == n: + ans += i + cnt = 0 + cnt += 1 +print(ans) From ba843ef03156ea3bb9a0645e19f2759a31972ff7 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 10 Oct 2021 15:26:49 +0900 Subject: [PATCH 093/213] BOJ --- BaekJoon/23081.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 BaekJoon/23081.py diff --git a/BaekJoon/23081.py b/BaekJoon/23081.py new file mode 100644 index 0000000..6c2138d --- /dev/null +++ b/BaekJoon/23081.py @@ -0,0 +1,49 @@ +import sys +input = sys.stdin.readline + +dx = [1,-1,0,0, 1, 1, -1, -1] +dy = [0,0,1,-1, 1, -1,1, -1] + +n = int(input()) +ls = [list(input().rstrip()) for _ in range(n)] +#x, y, cost +ans = [-1,-1, -1] + +space = [] +for i in range(n): + for j in range(n): + if ls[i][j] =='.': + space.append([i,j]) + +for y, x in space: + cnt = 0 + for d in range(8): + ty = y + tx = x + ct = 0 + for i in range(n): + ty += dy[d] + tx += dx[d] + if 0<=ty y: + ans = [x,y,cnt] + elif ans[1] == y and ans[0] > x: + ans = [x,y,cnt] +if ans == [-1,-1,-1]: + print("PASS") +else: + print(ans[0], ans[1]) + print(ans[2]) From c9c4e77fda3bef3a21bf1a5c1e6411ef88dd6c5b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 14 Oct 2021 23:18:59 +0900 Subject: [PATCH 094/213] BOJ --- BaekJoon/2558.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 BaekJoon/2558.py diff --git a/BaekJoon/2558.py b/BaekJoon/2558.py new file mode 100644 index 0000000..53e3447 --- /dev/null +++ b/BaekJoon/2558.py @@ -0,0 +1,3 @@ +n = int(input()) +m = int(input()) +print(n+m) From 8bd0a801fe6fb93614abf12c49a36519b3992253 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:13:36 +0900 Subject: [PATCH 095/213] BOJ --- codeforces/131A.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 codeforces/131A.py diff --git a/codeforces/131A.py b/codeforces/131A.py new file mode 100644 index 0000000..a75de5b --- /dev/null +++ b/codeforces/131A.py @@ -0,0 +1,13 @@ +flag = True +s = list(input().rstrip()) +for i in range(1, len(s)): + if 'a'<=s[i]<='z': + flag = False + break +if flag: + for i in range(len(s)): + if 'a'<=s[i]<='z': + s[i] = s[i].upper() + else: + s[i] = s[i].lower() +print(''.join(s)) From 4af7b30aa8945fb5508311616545cb601b6ce056 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:13:49 +0900 Subject: [PATCH 096/213] codeforces --- codeforces/131A.py | 1 + 1 file changed, 1 insertion(+) diff --git a/codeforces/131A.py b/codeforces/131A.py index a75de5b..b1a820a 100644 --- a/codeforces/131A.py +++ b/codeforces/131A.py @@ -11,3 +11,4 @@ else: s[i] = s[i].lower() print(''.join(s)) + From be817bef0bf9c998faf9ecb320532546c844d5c9 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:14:08 +0900 Subject: [PATCH 097/213] codeforces --- codeforces/479A.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 codeforces/479A.py diff --git a/codeforces/479A.py b/codeforces/479A.py new file mode 100644 index 0000000..2c31ef3 --- /dev/null +++ b/codeforces/479A.py @@ -0,0 +1,9 @@ +ls = [int(input()) for _ in range(3)] +l1 = ls[0] + ls[1] + ls[2] +l2 = ls[0] * ls[1] * ls[2] +l3 = ls[0] + ls[1] * ls[2] +l4 = ls[0] * (ls[1] + ls[2]) +l5 = (ls[0] + ls[1]) * ls[2] +l6 = ls[0] * ls[1] + ls[2] + +print(max(l1, l2, l3, l4, l5, l6)) \ No newline at end of file From 7c4771f9875390ed2473cc1913f1b23f63f92aad Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:14:33 +0900 Subject: [PATCH 098/213] codeforces --- codeforces/122A.py | 15 +++++++++++++++ codeforces/58A.py | 9 +++++++++ codeforces/69A.py | 7 +++++++ 3 files changed, 31 insertions(+) create mode 100644 codeforces/122A.py create mode 100644 codeforces/58A.py create mode 100644 codeforces/69A.py diff --git a/codeforces/122A.py b/codeforces/122A.py new file mode 100644 index 0000000..be1eed9 --- /dev/null +++ b/codeforces/122A.py @@ -0,0 +1,15 @@ +numList = [4, 7] +for _ in range(4): + for i in range(len(numList)): + tmp = numList[i] * 10 + 4 + numList.append(tmp) + tmp = numList[i] * 10 + 7 + numList.append(tmp) +numList = list(set(numList)) +n = int(input()) +flag = False +for i in numList: + if n % i == 0: + flag = True + break +print("YES" if flag else "NO") \ No newline at end of file diff --git a/codeforces/58A.py b/codeforces/58A.py new file mode 100644 index 0000000..6e7881a --- /dev/null +++ b/codeforces/58A.py @@ -0,0 +1,9 @@ +t = 'hello' +idx = 0 +s = input().rstrip() +for i in s: + if i == t[idx]: + idx += 1 + if idx == 5: + break +print("YES" if idx == 5 else "NO") \ No newline at end of file diff --git a/codeforces/69A.py b/codeforces/69A.py new file mode 100644 index 0000000..accb4f2 --- /dev/null +++ b/codeforces/69A.py @@ -0,0 +1,7 @@ +n = int(input()) +ans = [0,0,0] +for _ in range(n): + tmp = list(map(int,input().split())) + for i in range(3): + ans[i] += tmp[i] +print("YES" if ans == [0,0,0] else "NO") \ No newline at end of file From fe0b49dafd3fb813048defc7f2194540610aa8fb Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 21:15:18 +0900 Subject: [PATCH 099/213] codeforces --- codeforces/230A.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 codeforces/230A.py diff --git a/codeforces/230A.py b/codeforces/230A.py new file mode 100644 index 0000000..eb4f732 --- /dev/null +++ b/codeforces/230A.py @@ -0,0 +1,14 @@ +s, n = map(int,input().split()) +ls = [] +for i in range(n): + tmp = list(map(int,input().split())) + ls.append(tmp) +ls = sorted(ls) +flag = True +for i in range(n): + if s > ls[i][0]: + s += ls[i][1] + else: + flag = Fgitalse + break +print("YES" if flag else "NO") From 0c933ab2d789dd4c8c58241cd9c8322d885879ef Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 21:27:58 +0900 Subject: [PATCH 100/213] codeforces --- codeforces/339B.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 codeforces/339B.py diff --git a/codeforces/339B.py b/codeforces/339B.py new file mode 100644 index 0000000..dc2b321 --- /dev/null +++ b/codeforces/339B.py @@ -0,0 +1,13 @@ +n, m = map(int,input().split()) +ls = list(map(int,input().split())) +cur = 1 +ans = 0 +for i in range(m): + if ls[i] == cur: + continue + elif ls[i] < cur: + ans += n-cur+ls[i] + else: + ans += ls[i] - cur + cur = ls[i] +print(ans) From bf3ed5e428005d4608bed38487fc57554af3099a Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 22:04:29 +0900 Subject: [PATCH 101/213] codeforces --- codeforces/500A.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 codeforces/500A.py diff --git a/codeforces/500A.py b/codeforces/500A.py new file mode 100644 index 0000000..733ef0c --- /dev/null +++ b/codeforces/500A.py @@ -0,0 +1,12 @@ +n, t = map(int,input().split()) +ls = [0] + list(map(int,input().split())) +cur = 1 +flag = False +while cur < n: + if cur == t: + flag = True + break + cur += ls[cur] +if cur == n and n == t: + flag = True +print("YES" if flag else "NO") From 193ccd99a632aa70aee99eae4bc2b4c139d6d3a4 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 22:07:47 +0900 Subject: [PATCH 102/213] codeforces --- codeforces/579A.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 codeforces/579A.py diff --git a/codeforces/579A.py b/codeforces/579A.py new file mode 100644 index 0000000..ca60902 --- /dev/null +++ b/codeforces/579A.py @@ -0,0 +1,7 @@ +n = int(input()) +cnt = 0 +while n!=0: + if n & 1: + cnt += 1 + n = n>>1 +print(cnt) From adee30cc58e252c4f20cba1217cf2b13c7c7faac Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 15 Oct 2021 22:24:21 +0900 Subject: [PATCH 103/213] codeforces --- codeforces/379A.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 codeforces/379A.py diff --git a/codeforces/379A.py b/codeforces/379A.py new file mode 100644 index 0000000..a55cb39 --- /dev/null +++ b/codeforces/379A.py @@ -0,0 +1,17 @@ +a, b = map(int,input().split()) +new = a +old = 0 +flag = True +ans = 0 +while flag: + if new != 0: + ans += 1 + new -=1 + old += 1 + elif old >= b: + old -= b + old += 1 + ans += 1 + else: + flag= False +print(ans) From 95cfc00f2f9c2fedd5077385cb10038b84095daa Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 16 Oct 2021 20:13:55 +0900 Subject: [PATCH 104/213] BOJ --- codeforces/1374C.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 codeforces/1374C.py diff --git a/codeforces/1374C.py b/codeforces/1374C.py new file mode 100644 index 0000000..14d758b --- /dev/null +++ b/codeforces/1374C.py @@ -0,0 +1,14 @@ +for _ in range(int(input())): + n = int(input()) + s = input().rstrip() + tmp = 0 + ans = 0 + for i in s: + if i == '(': + tmp += 1 + else: + tmp -= 1 + if tmp < 0: + tmp = 0 + ans += 1 + print(ans) From 8a5577dc35805063efb5bed4762d716a86862be7 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 16 Oct 2021 20:24:08 +0900 Subject: [PATCH 105/213] BOJ --- codeforces/584A.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 codeforces/584A.py diff --git a/codeforces/584A.py b/codeforces/584A.py new file mode 100644 index 0000000..e88755e --- /dev/null +++ b/codeforces/584A.py @@ -0,0 +1,17 @@ +n,m = map(int,input().split()) +num = 9 +for _ in range(n-1): + num *= 10 + num += 9 + +flag = False +ans = -1 + +for i in range(0, 10): + tmp = num + tmp -= i + if tmp % m == 0 and tmp != 0: + flag = True + ans = tmp + break +print(ans) From 4486428fa51b0a7dde83a417a19b85f52f0bfce9 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 17 Oct 2021 22:29:14 +0900 Subject: [PATCH 106/213] codeforces --- codeforces/1244.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 codeforces/1244.py diff --git a/codeforces/1244.py b/codeforces/1244.py new file mode 100644 index 0000000..ea78ddf --- /dev/null +++ b/codeforces/1244.py @@ -0,0 +1,14 @@ +import sys +from math import ceil +input = sys.stdin.readline + +for _ in range(int(input())): + n = int(input()) + ls = list(input().rstrip()) + ans = n + max_ = 0 + for i in range(n): + #left or right + if ls[i] == '1': + max_ = max(max_, i+1, n-i) + print(2 * max_ if max_ else n) From a1e54d3b8f852c1c41da1e346c88b0ea8d8d2d97 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:43:46 +0900 Subject: [PATCH 107/213] codeforces --- codeforces/158B.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 codeforces/158B.py diff --git a/codeforces/158B.py b/codeforces/158B.py new file mode 100644 index 0000000..bb1ba76 --- /dev/null +++ b/codeforces/158B.py @@ -0,0 +1,7 @@ +n = int(input()) +ls = [0 for _ in range(5)] +t = list(map(int,input().split())) +for i in t: + ls[i] += 1 +ls[1] = max(ls[1] - (ls[2]%2)*2 - ls[3], 0) +print((ls[1]//4 + 1 if ls[1]%4 != 0 else ls[1]//4) + (ls[2]//2 + 1 if ls[2]%2 != 0 else ls[2]//2) + ls[3] + ls[4]) From eb6b9d62dcc94b65f5ba0e5dc6f9fc71a68e75a2 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 19 Oct 2021 23:18:11 +0900 Subject: [PATCH 108/213] codeforces --- codeforces/706B.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 codeforces/706B.py diff --git a/codeforces/706B.py b/codeforces/706B.py new file mode 100644 index 0000000..62ad91a --- /dev/null +++ b/codeforces/706B.py @@ -0,0 +1,9 @@ +import sys +from bisect import bisect_right +input = sys.stdin.readline + +n = int(input()) +x = sorted(list(map(int,input().split()))) +for _ in range(int(input())): + m = int(input()) + print(bisect_right(x, m)) From 811982a56b30ece15c754bc1be440f118b51340d Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:32:13 +0900 Subject: [PATCH 109/213] codeforces --- codeforces/270A.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 codeforces/270A.py diff --git a/codeforces/270A.py b/codeforces/270A.py new file mode 100644 index 0000000..74c308e --- /dev/null +++ b/codeforces/270A.py @@ -0,0 +1,12 @@ +ls = [] +for i in range(1000): + t = 180 * i + tm = 180 * i // (i+2) + if 180 *i % (i+2) == 0: + ls.append(tm) + if tm == 179: + break + +for _ in range(int(input())): + n = int(input()) + print("YES" if n in ls[1:] else "NO") From a5518cad84ebc8de947a2926e39253c585474be1 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 21 Oct 2021 23:17:04 +0900 Subject: [PATCH 110/213] codeforces --- codeforces/363B.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 codeforces/363B.py diff --git a/codeforces/363B.py b/codeforces/363B.py new file mode 100644 index 0000000..c0d49b1 --- /dev/null +++ b/codeforces/363B.py @@ -0,0 +1,15 @@ +import sys +input = sys.stdin.readline + +n, k = map(int,input().split()) +ls = list(map(int,input().split())) +min_ = sum(ls[:k]) +tmp = min_ +ans = 1 +for i in range(1, n-k+1): + tmp -= ls[i-1] + tmp += ls[i+k-1] + if tmp < min_: + min_ = tmp + ans = i+1 +print(ans, min_) From 5babfdf3db19607fba29027cbef40f5871b2e616 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 5 Nov 2021 16:25:20 +0900 Subject: [PATCH 111/213] codeforces --- codeforces/25A.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 codeforces/25A.py diff --git a/codeforces/25A.py b/codeforces/25A.py new file mode 100644 index 0000000..82a89a4 --- /dev/null +++ b/codeforces/25A.py @@ -0,0 +1,15 @@ + +n = int(input()) +ls = list(map(int,input().split())) + +if (ls[0] & 1) ^ (ls[1] & 1): + if ls[0] & 1 == ls[2] & 1: + print(2) + else: + print(1) +else: + flag = ls[0] & 1 + for i in range(n): + if ls[i] & 1 != flag: + print(i+1) + break From ba4f7d039824ba107fccebc9bbdac8ead1475411 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 6 Nov 2021 15:14:28 +0900 Subject: [PATCH 112/213] codeforces --- codeforces/4C.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 codeforces/4C.py diff --git a/codeforces/4C.py b/codeforces/4C.py new file mode 100644 index 0000000..4241ee4 --- /dev/null +++ b/codeforces/4C.py @@ -0,0 +1,10 @@ +n = int(input()) +dc = {} +for _ in range(n): + s = input().rstrip() + if s not in dc: + print("OK") + dc[s] = 0 + else: + dc[s] += 1 + print('{}{}'.format(s, dc[s])) From 9b6ed875e020ab8acdb4deb6903b02c51c50cd7b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 6 Nov 2021 15:50:58 +0900 Subject: [PATCH 113/213] codeforces --- codeforces/230B.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 codeforces/230B.py diff --git a/codeforces/230B.py b/codeforces/230B.py new file mode 100644 index 0000000..29bcf7b --- /dev/null +++ b/codeforces/230B.py @@ -0,0 +1,15 @@ +import sys +input = sys.stdin.readline + +limit = 1000001 +primes = [False] * 2 + [True] * (limit - 2) +for i in range(2, limit): + if primes[i]: + for j in range(i ** 2, limit, i): + primes[j] = False + +n = int(input()) +ls = list(map(int,input().split())) +for it in range(n): + tmp = int(ls[it] ** 0.5) + print("YES" if primes[tmp] and tmp ** 2 == ls[it] else "NO") From 0ae501a9db4dcd311c15c63f5e8a70d350a82afd Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 15 Jun 2022 13:44:27 +0900 Subject: [PATCH 114/213] BOJ --- BaekJoon/13904.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 BaekJoon/13904.cpp diff --git a/BaekJoon/13904.cpp b/BaekJoon/13904.cpp new file mode 100644 index 0000000..68d2d17 --- /dev/null +++ b/BaekJoon/13904.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +typedef pair pii; + + +int main(){ + int n, d, w; + cin>>n; + priority_queuepq; + vectorv(1001, 0); + //input + for(int i=0; i>d>>w; + pq.push({w, d}); // order : w + } + while(!pq.empty()){ + pii cur = pq.top(); pq.pop(); + int gain = cur.first; + int remain = cur.second; + while(remain > 0 && v[remain] != 0) remain --; + v[remain] = gain; + } + int ans = 0; + for(int i=1; i<1001; ++i){ + ans += v[i]; + } + cout< Date: Mon, 20 Jun 2022 01:44:22 +0900 Subject: [PATCH 115/213] BOJ --- BaekJoon/2568.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 BaekJoon/2568.cpp diff --git a/BaekJoon/2568.cpp b/BaekJoon/2568.cpp new file mode 100644 index 0000000..9d429d8 --- /dev/null +++ b/BaekJoon/2568.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +typedef pair pii; + +bool comp(const pii &a, const pii &b) +{ + return a.second < b.second; +} + +int main(void) +{ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int ta, tb, n; + cin>>n; + vector v; + for(int i=0; i>ta>>tb; + v.push_back(make_pair(ta, tb)); + } + sort(v.begin(), v.end()); + + vectordp; + vectoridx; + for(int i=0; i= v[i].second){ + auto iter = lower_bound(dp.begin(), dp.end(), v[i], comp) - dp.begin(); + dp[iter] = v[i]; + idx.push_back(iter); + } + else{ + dp.push_back(v[i]); + idx.push_back(dp.size()-1); + } + } + } + int cur = dp.size()-1; + cout< ans; + for(int i=idx.size()-1; i>=0; --i){ + if(cur == idx[i]) cur -= 1; + else{ + ans.push_back(v[i].first); + } + } + reverse(ans.begin(), ans.end()); + for(auto a : ans){ + cout< Date: Wed, 22 Jun 2022 22:04:42 +0900 Subject: [PATCH 116/213] BOJ --- BaekJoon/17298.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 BaekJoon/17298.cpp diff --git a/BaekJoon/17298.cpp b/BaekJoon/17298.cpp new file mode 100644 index 0000000..64dc15f --- /dev/null +++ b/BaekJoon/17298.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main() { + int n, tmp; + vectorv, ans; + cin >> n; + for (int i = 0; i < n; ++i) { + cin >> tmp; + v.push_back(tmp); + } + stacks; + for (int i = n-1; i >= 0; --i) { + while (!s.empty() && s.top() <= v[i]) + s.pop(); + + if (s.empty()) ans.push_back(-1); + else { + ans.push_back(s.top()); + } + s.push(v[i]); + } + for (int i = ans.size()-1; i >= 0; --i) { + cout << ans[i] << ' '; + } + cout << '\n'; + + return 0; +} From 7dd061eacdca74017819b050b621d3b252f16c4a Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 23 Jun 2022 16:19:41 +0900 Subject: [PATCH 117/213] BOJ --- BaekJoon/4949_2.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 BaekJoon/4949_2.cpp diff --git a/BaekJoon/4949_2.cpp b/BaekJoon/4949_2.cpp new file mode 100644 index 0000000..27a02f3 --- /dev/null +++ b/BaekJoon/4949_2.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + while(true){ + stackst; + bool flag = true; + string s; + getline(cin, s); + if(s.size() == 1 && s[0] == '.') + break; + for(int i=0; i Date: Fri, 24 Jun 2022 20:52:54 +0900 Subject: [PATCH 118/213] BOJ --- BaekJoon/18258.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 BaekJoon/18258.py diff --git a/BaekJoon/18258.py b/BaekJoon/18258.py new file mode 100644 index 0000000..645552f --- /dev/null +++ b/BaekJoon/18258.py @@ -0,0 +1,23 @@ +import sys +from collections import deque +input = sys.stdin.readline + +n = int(input()) +q = deque() +for _ in range(n): + ls = input().rstrip().split() + if ls[0] == "push": + q.append(ls[1]) + elif ls[0] == "front": + print(q[0] if len(q) != 0 else -1) + elif ls[0] == "back": + print(q[-1] if len(q) != 0 else -1) + elif ls[0] == "size": + print(len(q)) + elif ls[0] == "empty": + print(1 if len(q) == 0 else 0) + elif ls[0] == "pop": + if len(q) == 0: + print(-1) + else: + print(q.popleft()) From 18a52981b3a11ab19d33b48ba225c2193571ca69 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 25 Jun 2022 16:12:42 +0900 Subject: [PATCH 119/213] BOJ --- BaekJoon/2629.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 BaekJoon/2629.cpp diff --git a/BaekJoon/2629.cpp b/BaekJoon/2629.cpp new file mode 100644 index 0000000..c0b9647 --- /dev/null +++ b/BaekJoon/2629.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int N, M, tmp, sum_=0; + cin >> N; + vectordp(40001, 0); + vectorprev(40001, 0); + prev[0] = 1; + for (int i = 0; i < N; ++i) { + cin >> tmp; + sum_ += tmp; + for (int j = sum_; j >= 0; -- j) { + if (prev[j] == 1) { + if (j > tmp) { + dp[j - tmp] = 1; + } + else { + dp[tmp - j] = 1; + } + if (j + tmp <= 40000) { + dp[j + tmp] = 1; + } + } + } + for (int j = 0; j <= sum_; ++j) { + if(dp[j] != 0) + prev[j] = dp[j]; + dp[j] = 0; + } + } + cin >> M; + for (int i = 0; i < M; ++i) { + cin >> tmp; + if (prev[tmp]) cout << "Y "; + else cout << "N "; + } + return 0; +} From 364af9e919541c54e1c04dbb7d64ce14070ccbb2 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 26 Jun 2022 13:22:57 +0900 Subject: [PATCH 120/213] BOJ --- BaekJoon/2098.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 BaekJoon/2098.cpp diff --git a/BaekJoon/2098.cpp b/BaekJoon/2098.cpp new file mode 100644 index 0000000..304577b --- /dev/null +++ b/BaekJoon/2098.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +int n, mask; +vector>v, visited; + +int tsp(int cur, int check) { + if (check == mask) + return v[cur][0] ? v[cur][0] : 2e9; + + if (visited[cur][check] != -1) return visited[cur][check]; + visited[cur][check] = 2e9; + + for (int i = 0; i < n; ++i) { + if (check & 1 << i || v[cur][i] == 0 ) continue; + visited[cur][check] = min(visited[cur][check], tsp(i, check | 1 << i) + v[cur][i]); + } + return visited[cur][check]; +} + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + cin >> n; + mask = (1 << n ) - 1; + v.resize(n, vector(n)); + visited.resize(n, vector((1<<16), -1)); + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + cin >> v[i][j]; + int ans = tsp(0, 1); + cout << ans << '\n'; + return 0; +} From d9ed274b6920ceaf990870610be088fface8c0bd Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 26 Jun 2022 19:21:31 +0900 Subject: [PATCH 121/213] BOJ --- BaekJoon/15681.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 BaekJoon/15681.cpp diff --git a/BaekJoon/15681.cpp b/BaekJoon/15681.cpp new file mode 100644 index 0000000..910beb3 --- /dev/null +++ b/BaekJoon/15681.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +vectordp, visited; +vector>v; + +int dfs(int curNode){ + if(dp[curNode]) return dp[curNode]; + dp[curNode] = 1; + int ans = 1; + for(int i=0; i>n>>r>>q; + v.resize(n+1); + dp.resize(n+1, 0); + for(int i=0; i>s>>d; + v[s].push_back(d); + v[d].push_back(s); + } + dfs(r); + for(int i=0; i>d; + cout< Date: Mon, 27 Jun 2022 12:29:35 +0900 Subject: [PATCH 122/213] 1916_chg --- BaekJoon/1916.cpp | 70 ++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/BaekJoon/1916.cpp b/BaekJoon/1916.cpp index 3b48f5a..ffd3cf0 100644 --- a/BaekJoon/1916.cpp +++ b/BaekJoon/1916.cpp @@ -1,52 +1,46 @@ -#include -#include -#include -#include -#include +#include using namespace std; -#define MAX 2e9 -typedef pair P; -vector

d[300100]; -int n, m, x, res = 0; -int u, v, w, dst; -vectordis(20001); -void dijkstra(); -int main() { - ios::sync_with_stdio(false); - cin.tie(0); - cin >> n >> m; - for (int i = 0; i < m;i++) { - cin >> u >> v >> w; - d[u].push_back(make_pair(v, w)); - } - cin>>x>>dst; - for (int i = 1; i <= n; i++) { - dis[i] = MAX; - } - dis[x] = 0; - dijkstra(); - cout<pii; -void dijkstra() { - priority_queue

pq; - pq.push(make_pair(0, x)); +vector>v; +vectorvisited; + +void dijkstra(int start, int end) { + priority_queuepq; + pq.push(make_pair(0, start)); while (!pq.empty()) { int cur = pq.top().second; int weight = -1 * pq.top().first; pq.pop(); + if (visited[cur] < weight) continue; + for (int i = 0; i < v[cur].size(); i++) { + int next_weights = visited[cur] + v[cur][i].second; + int before_change = visited[v[cur][i].first]; - for (int i = 0; i < d[cur].size(); i++) { - int val = dis[cur]+d[cur][i].second; - int be_val = dis[d[cur][i].first]; - if (val < be_val) { - dis[d[cur][i].first] = val; - pq.push({ -1 * val, d[cur][i].first }); + if (next_weights < before_change) { + visited[v[cur][i].first] = next_weights; + pq.push({ -1 * next_weights, v[cur][i].first }); } } + } +} +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, m, s, d, w, start, end; + cin >> n >> m; + v.resize(n + 1); + visited.resize(n + 1, 2e9); + for (int i = 0; i < m; ++i) { + cin >> s >> d >> w; + v[s].push_back({ d, w }); } + cin >> start >> end; + visited[start] = 0; + dijkstra(start, end); + cout << visited[end]; + return 0; } From 64d69f2cb6a25804c1ac90b029a6760551ee3da1 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 27 Jun 2022 13:06:21 +0900 Subject: [PATCH 123/213] BOJ --- BaekJoon/1238.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 BaekJoon/1238.cpp diff --git a/BaekJoon/1238.cpp b/BaekJoon/1238.cpp new file mode 100644 index 0000000..c6dd63a --- /dev/null +++ b/BaekJoon/1238.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +typedef pairpii; + +vector>v; + + +vector dijkstra(int start, int n) { + vectorvisited(n+1, 2e9); + visited[start] = 0; + priority_queuepq; + pq.push(make_pair(0, start)); + + while (!pq.empty()) { + int cur = pq.top().second; + int weight = -1 * pq.top().first; + pq.pop(); + if (visited[cur] < weight) continue; + for (int i = 0; i < v[cur].size(); i++) { + int next_weights = visited[cur] + v[cur][i].second; + int before_change = visited[v[cur][i].first]; + + if (next_weights < before_change) { + visited[v[cur][i].first] = next_weights; + pq.push({ -1 * next_weights, v[cur][i].first }); + } + } + } + return visited; +} + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, m, s, d, w, x; + cin >> n >> m >> x; + v.resize(n + 1); + + for (int i = 0; i < m; ++i) { + cin >> s >> d >> w; + v[s].push_back({ d, w }); + } + vectorans(n + 1,0); + for (int i = 1; i < n + 1; ++i) { + + vectorvisited = dijkstra(i, n); + if (i == x) { + for (int j = 1; j < n + 1; ++j) { + ans[j] += visited[j]; + } + } + else { + ans[i] += visited[x]; + } + } + int a = 0; + for (int i = 1; i < n+1; ++i) { + a = max(a, ans[i]); + } + cout << a; + return 0; +} From deac0ff42160b182aa5f3ccb9b7e4e0c918ae6e6 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 28 Jun 2022 17:00:51 +0900 Subject: [PATCH 124/213] BOJ --- BaekJoon/22352.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 BaekJoon/22352.cpp diff --git a/BaekJoon/22352.cpp b/BaekJoon/22352.cpp new file mode 100644 index 0000000..9a8300e --- /dev/null +++ b/BaekJoon/22352.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +typedef pairpii; + +int dx[4] = {-1,1,0,0}; +int dy[4] = {0,0,1,-1}; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int N, M, val, diff; + cin>>N>>M; + vector>ori(N, vector(M)); + vector>chg(N,vector(M)); + vector>visited(N,vector(M, 0)); + vectorp; + bool flag = true; + for(int i=0; i>ori[i][j]; + + for(int i=0; i>chg[i][j]; + if(ori[i][j] != chg[i][j]){ + p.push_back({i, j}); + diff = ori[i][j] - chg[i][j]; + val = ori[i][j]; + } + } + } + + if(p.size() == 0){ + cout<<"YES"<q; + q.push_back(p[0]); + visited[p[0].first][p[0].second] = 1; + while(!q.empty()){ + pii cur = q.front(); q.pop_front(); + for(int i=0; i<4; ++i){ + int nx = cur.first + dx[i]; + int ny = cur.second + dy[i]; + if(nx >= 0 && nx < N && ny >= 0 && ny < M){ + if(visited[nx][ny] == 0 && ori[nx][ny] == ori[cur.first][cur.second]){ + if(ori[nx][ny] - chg[nx][ny] == diff){ + q.push_back({nx, ny}); + visited[nx][ny] = 1; + }else{ + flag = false; + break; + } + } + } + } + } + int cnt = 0; + for(int i=0; i Date: Wed, 29 Jun 2022 13:30:15 +0900 Subject: [PATCH 125/213] BOJ --- BaekJoon/1949.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 BaekJoon/1949.cpp diff --git a/BaekJoon/1949.cpp b/BaekJoon/1949.cpp new file mode 100644 index 0000000..fe6056a --- /dev/null +++ b/BaekJoon/1949.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +vectorw; +vector>v; +vector>dp; +vectorvisited; + +void dfs(int x) { + visited[x] = true; + dp[x][1] = w[x]; + for (int i = 0; i < v[x].size(); ++i) { + int nxt = v[x][i]; + if (!visited[nxt]){ + dfs(nxt); + dp[x][1] += dp[nxt][0]; + dp[x][0] += max(dp[nxt][1], dp[nxt][0]); + } + } +} + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, src, dst; + cin >> n; + w.resize(n+1); + v.resize(n+1); + dp.resize(n+1, vector(2, 0)); + visited.resize(n+1, false); + for(int i=1; i<=n; ++i) + cin>>w[i]; + for (int i = 0; i < n-1; ++i) { + cin >> src >> dst; + v[src].push_back(dst); + v[dst].push_back(src); + } + dfs(1); + cout << max(dp[1][0], dp[1][1])<<'\n'; + return 0; +} From 6e49f2ce8996096b28c906d3d77852ce9ac24b71 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:52:06 +0900 Subject: [PATCH 126/213] BOJ --- BaekJoon/22357.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 BaekJoon/22357.cpp diff --git a/BaekJoon/22357.cpp b/BaekJoon/22357.cpp new file mode 100644 index 0000000..71b737a --- /dev/null +++ b/BaekJoon/22357.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +vector eratos(int n) { + vectorprime(n+1, true); + prime[1] = false; + for (int i = 2; i < sqrt(n); ++i) { + if (prime[i]) { + for (int j = i*i; j <= n; j += i) { + prime[j] = false; + } + } + } + return prime; +} + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, m,edx = 2003; + vectore = eratos(100000); + cin >> n >> m; + for (int i = 0; i < n; ++i) { + for (int j = 1; j <= m; ++j) { + cout << edx * j <<" "; + } + cout << '\n'; + edx += 1; + while (!e[edx])edx += 1; + } + return 0; +} From e2d13cc39bc850541cbc384f9e036e3afb2c4885 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 1 Jul 2022 15:37:45 +0900 Subject: [PATCH 127/213] BOJ --- BaekJoon/12847.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 BaekJoon/12847.cpp diff --git a/BaekJoon/12847.cpp b/BaekJoon/12847.cpp new file mode 100644 index 0000000..f17f3b5 --- /dev/null +++ b/BaekJoon/12847.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int main(){ + long long n, m, init = 0, max_ = 0; + scanf("%lld%lld", &n, &m); + vectorv(n); + for(int i=0; i>v[i]; + if(i < m) + init += v[i]; + } + max_ = init; + for(int i=0; i Date: Fri, 1 Jul 2022 16:40:29 +0900 Subject: [PATCH 128/213] BOJ --- BaekJoon/19532.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 BaekJoon/19532.cpp diff --git a/BaekJoon/19532.cpp b/BaekJoon/19532.cpp new file mode 100644 index 0000000..8cc83bf --- /dev/null +++ b/BaekJoon/19532.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main(){ + cin.tie(); + int a,b,c,d,e,f; + cin>>a>>b>>c>>d>>e>>f; + for(int i=-999; i<=999; ++i){ + for(int j=-999; j<=999; ++j){ + if(a*i + b*j == c && d*i + e*j ==f){ + cout< Date: Sat, 2 Jul 2022 17:22:53 +0900 Subject: [PATCH 129/213] BOJ --- BaekJoon/11659_2.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BaekJoon/11659_2.cpp diff --git a/BaekJoon/11659_2.cpp b/BaekJoon/11659_2.cpp new file mode 100644 index 0000000..e1e047e --- /dev/null +++ b/BaekJoon/11659_2.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, m, tmp, qa, qb; + cin >> n >> m; + vectorv(n), psum(n + 1); + psum[0] = 0; + for (int i = 0; i < n; ++i) { + cin >> v[i]; + psum[i + 1] = psum[i] + v[i]; + } + for (int i = 0; i < m; ++i) { + cin >> qa >> qb; + cout << psum[qb] - psum[qa - 1] << '\n'; + } + return 0; +} From e4402e04296bb2d59d494b61afbc18d12c7b37f0 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 4 Jul 2022 15:30:26 +0900 Subject: [PATCH 130/213] BOJ --- BaekJoon/14465.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 BaekJoon/14465.cpp diff --git a/BaekJoon/14465.cpp b/BaekJoon/14465.cpp new file mode 100644 index 0000000..ba62421 --- /dev/null +++ b/BaekJoon/14465.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +typedef pair pii; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, k, b, ta; + cin>>n>>k>>b; + vectoris_break(n+1, false); + for(int i=0; i>ta; + is_break[ta] = true; + } + + int a = 0; + for(int i=1; i<=k; ++i){ + if(is_break[i]) a += 1; + } + int ans = a; + for(int i=2; i<=n-k+1; ++i){ + if(is_break[i-1]){ + a -= 1; + } + if(is_break[i+k-1]){ + a += 1; + } + + ans = min(ans, a); + } + cout< Date: Tue, 5 Jul 2022 16:42:55 +0900 Subject: [PATCH 131/213] BOJ --- BaekJoon/19951.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 BaekJoon/19951.cpp diff --git a/BaekJoon/19951.cpp b/BaekJoon/19951.cpp new file mode 100644 index 0000000..57fcfbd --- /dev/null +++ b/BaekJoon/19951.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +typedef pair pii; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int N, M, s, d, w; + cin>>N>>M; + vectorv(N+1), v2(N+2, 0); + + for(int i=1; i<=N; ++i){ + cin>>v[i]; + } + for(int i=0; i>s>>d>>w; + v2[s] += w; + v2[d+1] -= w; + } + for(int i=1; i<=N; ++i){ + v2[i] += v2[i-1]; + cout< Date: Wed, 6 Jul 2022 20:03:23 +0900 Subject: [PATCH 132/213] BOJ --- BaekJoon/3273.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/3273.cpp diff --git a/BaekJoon/3273.cpp b/BaekJoon/3273.cpp new file mode 100644 index 0000000..e2a356a --- /dev/null +++ b/BaekJoon/3273.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, q; + cin >> n; + vectorv(n); + for (int i = 0; i < n; ++i) { + cin >> v[i]; + } + sort(v.begin(), v.end()); + cin >> q; + int l = 0, r = v.size() - 1, cnt = 0; + while (l < r) { + cout << "L : " << l << " R : " << r <<" cnt : "< Date: Thu, 7 Jul 2022 16:30:53 +0900 Subject: [PATCH 133/213] BOJ --- BaekJoon/2580.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 BaekJoon/2580.cpp diff --git a/BaekJoon/2580.cpp b/BaekJoon/2580.cpp new file mode 100644 index 0000000..0a10dbe --- /dev/null +++ b/BaekJoon/2580.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +typedef pair pii; + +int m[9][9]; +int ans[9][9]; +vector v; + +void recursive(int idx){ + if(idx == v.size()){ + for(int i=0; i<9; ++i) + for(int j=0; j<9; ++j) + ans[i][j] = m[i][j]; + return; + } + pii cur = v[idx]; + int bit_ = 0; + //열과 행 + for(int i=0; i<9; ++i){ + if(m[cur.first][i] != 0) + bit_ |= 1<<(m[cur.first][i]-1); + if(m[i][cur.second] != 0) + bit_ |= 1<<(m[i][cur.second]-1); + } + //네모 + for(int i=0; i<3; ++i){ + for(int j=0; j<3; ++j){ + int nx = (cur.first/3) * 3 + i; + int ny = (cur.second/3) * 3 + j; + if(m[nx][ny] != 0) + bit_ |= 1<<(m[nx][ny]-1); + } + } + // 칠하고 다음으로 넘어가기 + for(int i=0; i<9; ++i){ + if(!(bit_ & (1<>m[i][j]; + if(!m[i][j]) + v.push_back({i, j}); + } + } + recursive(0); + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + cout< Date: Fri, 8 Jul 2022 21:06:48 +0900 Subject: [PATCH 134/213] BOJ --- BaekJoon/1992_dnq.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 BaekJoon/1992_dnq.cpp diff --git a/BaekJoon/1992_dnq.cpp b/BaekJoon/1992_dnq.cpp new file mode 100644 index 0000000..ac28025 --- /dev/null +++ b/BaekJoon/1992_dnq.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +int N; +vectorv; + +void dnq(int n, int sx, int sy) { + // check one or zero + char t = v[sx][sy]; + bool flag = true; + for (int i = sx; i < sx + n; ++i) { + for (int j = sy; j < sy + n; ++j) { + if (v[i][j] != t) { + flag = false; + } + } + } + if (flag) { + cout << t; + } + else { + cout << "("; + dnq(n / 2, sx, sy); + dnq(n / 2, sx, sy + n / 2); + dnq(n / 2, sx + n/2, sy); + dnq(n / 2, sx+n/2, sy+n/2); + cout << ")"; + } +} + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + cin >> N; + v.resize(N); + for (int i = 0; i < N; ++i) { + cin >> v[i]; + } + dnq(N, 0, 0); + return 0; +} From 576a6eaf48f4deb98d6b912cb98f64a32c668578 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 9 Jul 2022 21:00:51 +0900 Subject: [PATCH 135/213] BOJ --- BaekJoon/2559.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 BaekJoon/2559.cpp diff --git a/BaekJoon/2559.cpp b/BaekJoon/2559.cpp new file mode 100644 index 0000000..782e053 --- /dev/null +++ b/BaekJoon/2559.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, k, ans = -2e9, t = 0; + cin >> n >> k; + vectorv(n); + for (int i = 0; i < n; ++i) { + cin >> v[i]; + if (i < k) t += v[i]; + } + ans = t; + for (int i = k; i < n; ++i) { + t -= v[i - k]; + t += v[i]; + ans = max(ans, t); + } + cout << ans; + return 0; +} From e7a33ccf51d5bff4cf6c68a133095added1ce983 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 10 Jul 2022 15:18:05 +0900 Subject: [PATCH 136/213] BOJ --- BaekJoon/1780_dnq.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 BaekJoon/1780_dnq.cpp diff --git a/BaekJoon/1780_dnq.cpp b/BaekJoon/1780_dnq.cpp new file mode 100644 index 0000000..f5d8b52 --- /dev/null +++ b/BaekJoon/1780_dnq.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +int arr[2200][2200]; +int res[3]; + +void func(int n, int y, int x){ + int color = arr[y][x]; + for(int i=x; i>n; + for(int i=0; i>arr[j][i]; + } + } + func(n, 0, 0); + for(int i=0; i<3; ++i){ + cout< Date: Mon, 11 Jul 2022 20:57:08 +0900 Subject: [PATCH 137/213] BOJ --- BaekJoon/10830.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 BaekJoon/10830.cpp diff --git a/BaekJoon/10830.cpp b/BaekJoon/10830.cpp new file mode 100644 index 0000000..bb6caa5 --- /dev/null +++ b/BaekJoon/10830.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; + +typedef long long ll; + +ll N, B; + +vector>m; + +vector> calculate_matrix(vector>t1, vector>t2) { + vector>ans(N, vector(N, 0)); + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + for (int k = 0; k < N; ++k) { + ans[i][j] += t1[i][k] * t2[k][j]; + } + ans[i][j] %= 1000; + } + } + return ans; +} + +vector> dnq(ll b) { + if (b == 1) { + return m; + } + vector> t = dnq(b / 2); + t = calculate_matrix(t, t); + if (b & 1) { + t = calculate_matrix(t, m); + } + return t; +} + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(0); + cin >> N >> B; + m.resize(N, vector(N)); + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + cin >> m[i][j]; + m[i][j] %= 1000; + } + } + vector> ans = dnq(B); + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + cout << ans[i][j] << ' '; + } + cout << endl; + } + + return 0; +} From 6e0ffca12333181bf8397525fef5392c64716e2c Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 12 Jul 2022 15:40:27 +0900 Subject: [PATCH 138/213] BOJ --- BaekJoon/2263.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 BaekJoon/2263.cpp diff --git a/BaekJoon/2263.cpp b/BaekJoon/2263.cpp new file mode 100644 index 0000000..7437c71 --- /dev/null +++ b/BaekJoon/2263.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +vectorin_, post_; + +void dnq(int l, int r, int pl, int pr){ + if( l > r || pl > pr) return; + int root_ = post_[pr]; + int t = in_[root_]; + cout << root_<<" "; + dnq(l, t-1, pl, pl + (t-1-l)); + dnq(t+1, r, pl+t-l, pr-1); +} + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, t; + cin>>n; + in_.resize(n+1); + post_.resize(n+1); + for(int i=0; i>t; + in_[t] = i; + } + + for(int i=0; i>post_[i]; + + dnq(0, n-1, 0, n-1); + return 0; +} From f0090619e90a44c79957d2e843101bbfdac82ab1 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 13 Jul 2022 21:39:13 +0900 Subject: [PATCH 139/213] BOJ --- BaekJoon/1477.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 BaekJoon/1477.cpp diff --git a/BaekJoon/1477.cpp b/BaekJoon/1477.cpp new file mode 100644 index 0000000..0ae6a97 --- /dev/null +++ b/BaekJoon/1477.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +vector rest; +int binary_search(int M, int L) { + int l = 1, r = L - 1; + + while (l <= r) { + int mid = (l + r) / 2; + int cnt = 0; + for (int i = 1; i < rest.size(); ++i) { + int t = rest[i] - rest[i - 1]; + cnt += t / mid; + if (t % mid == 0) cnt--; + } + if (cnt > M) l = mid + 1; + else r = mid - 1; + } + + return l; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + int N, M, L; + cin >> N >> M >> L; + + rest.resize(N + 2); + + rest[0] = 0; + for (int i = 1; i <= N; ++i) cin >> rest[i]; + rest[N + 1] = L; + + sort(rest.begin(), rest.end()); + + cout << binary_search(M, L); + + return 0; +} From 6ec3535582992daba130405e3704755870cd6b8d Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 16 Jul 2022 21:30:13 +0900 Subject: [PATCH 140/213] BOJ --- BaekJoon/7662.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 BaekJoon/7662.cpp diff --git a/BaekJoon/7662.cpp b/BaekJoon/7662.cpp new file mode 100644 index 0000000..a7d54de --- /dev/null +++ b/BaekJoon/7662.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(false); + int tc, n, num; + char cmd; + cin >> tc; + for (int i = 0; i < tc; ++i) { + multisetq; + cin >> n; + for (int j = 0; j < n; ++j) { + cin >> cmd >> num; + if (cmd == 'I') { + q.insert(num); + } + else { + if (q.empty()) { + continue; + } + if (num == -1) { + q.erase(q.begin()); + } + else { + auto it = q.end(); + q.erase(--it); + } + } + } + if (q.empty()) { + cout << "EMPTY" << '\n'; + } + else { + auto it = q.end(); + cout << *(--it) << " " << *q.begin() << '\n'; + } + } + return 0; +} From 47480a1da7ff76f3f04c208a75344226869207e6 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 17 Jul 2022 15:15:58 +0900 Subject: [PATCH 141/213] BOJ --- BaekJoon/2492.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 BaekJoon/2492.cpp diff --git a/BaekJoon/2492.cpp b/BaekJoon/2492.cpp new file mode 100644 index 0000000..cc7f5a9 --- /dev/null +++ b/BaekJoon/2492.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +typedef pair pii; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(false); + int N, M, T, K, x, y; + cin >> N >> M >> T >> K; + vector v; + + for (int i = 0; i < T; ++i) { + cin >> x >> y; + v.push_back({ x, y }); + } + pii ansp = { 0,0 }; + int ans = 0; + for (int i = 0; i < T; ++i) { + for (int j = 0; j < T; ++j) { + int nx, ny; + if (v[i].first + K > N) { + nx = N - K; + } + else { + nx = v[i].first; + } + if (v[j].second + K > M) { + ny = M - K; + } + else { + ny = v[j].second; + } + int cnt = 0; + for (pii p : v) { + if (nx <= p.first && p.first <= nx + K && ny <= p.second && p.second <= ny + K) { + cnt += 1; + } + } + if (cnt > ans) { + ansp = { nx, ny+K }; + ans = cnt; + } + } + } + cout << ansp.first << " " << ansp.second << '\n'; + cout << ans<<'\n'; + return 0; +} \ No newline at end of file From e8ac290f3f4780528dfa9ecb57f4b0c7a8c80de0 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 18 Jul 2022 16:05:40 +0900 Subject: [PATCH 142/213] BOJ --- BaekJoon/20440.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 BaekJoon/20440.cpp diff --git a/BaekJoon/20440.cpp b/BaekJoon/20440.cpp new file mode 100644 index 0000000..3ed6d9b --- /dev/null +++ b/BaekJoon/20440.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; + +typedef pair pii; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, ts, te; + cin>>n; + vectorv; + vectorcoord; + + for(int i=0; i>ts>>te; + v.push_back({ts, te}); + coord.push_back(ts); + coord.push_back(te); + } + sort(coord.begin(), coord.end()); + coord.erase(unique(coord.begin(), coord.end()), coord.end()); + + vectorpSum(coord.size(), 0); + + for(int i=0; i max_){ + sans = i; + } + max_ = max(pSum[i], max_); + } + + cout< Date: Tue, 19 Jul 2022 09:39:18 +0900 Subject: [PATCH 143/213] BOJ --- BaekJoon/23295.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 BaekJoon/23295.cpp diff --git a/BaekJoon/23295.cpp b/BaekJoon/23295.cpp new file mode 100644 index 0000000..1159f61 --- /dev/null +++ b/BaekJoon/23295.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +typedef long long ll; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + ll N, T, tmp, a, b, max_time = 0; + cin>>N>>T; + vectorv(100001, 0); + + //입력 & imos + for(int i=0; i>tmp; + for(int j=0; j>a>>b; + v[a]++; + v[b]--; + max_time = max(max_time, max(a, b)); + } + } + + ll ansIdx = 0, ans = v[0]; + tmp = v[0]; + //누적 합 + for(int i=1; i ans){ + ansIdx = i - T + 1; + ans = tmp; + } + } + cout< Date: Thu, 21 Jul 2022 16:46:39 +0900 Subject: [PATCH 144/213] BOJ --- BaekJoon/24041.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 BaekJoon/24041.cpp diff --git a/BaekJoon/24041.cpp b/BaekJoon/24041.cpp new file mode 100644 index 0000000..d6e1a89 --- /dev/null +++ b/BaekJoon/24041.cpp @@ -0,0 +1,58 @@ +// 고려해야 하는 것 +// S와 L을 동시에 생각해야 함. +// n : k개의 ~~~ logn : 이분 탐색 +// 유통기한 +#include +#define x first +#define y second +using namespace std; + +typedef long long ll; +typedef pair pli; + +int K, G; +vectorimportant; +vectornot_important; + +bool decision(int day){ + ll num_germ = 0; + for(int i=0; i, greater> pq; + for(int i=0; i>N>>G>>K; + for(int i=0; i>S>>L>>O; + (O == 0)? important.emplace_back(S, L) : not_important.emplace_back(S, L); + } + ll l = 0, r = 2e9; + while(l <= r){ + ll mid = (l+r)/2; + if(decision(mid)){ + l = mid + 1; + }else{ + r = mid - 1; + } + } + cout< Date: Fri, 22 Jul 2022 15:24:41 +0900 Subject: [PATCH 145/213] BOJ --- BaekJoon/25238.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 BaekJoon/25238.cpp diff --git a/BaekJoon/25238.cpp b/BaekJoon/25238.cpp new file mode 100644 index 0000000..79570d1 --- /dev/null +++ b/BaekJoon/25238.cpp @@ -0,0 +1,9 @@ +#include +using namespace std; + +int main(){ + float a, b; + cin>>a>>b; + cout<<((a - (a * b / 100) >= 100)? 0 : 1); + return 0; +} From 7b7d570aba604cff2e19e7874b34abe2caf5ff22 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 22 Jul 2022 15:47:20 +0900 Subject: [PATCH 146/213] BOJ --- BaekJoon/25239.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 BaekJoon/25239.cpp diff --git a/BaekJoon/25239.cpp b/BaekJoon/25239.cpp new file mode 100644 index 0000000..d4b9c58 --- /dev/null +++ b/BaekJoon/25239.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; + +int curH, curM; +vectorh(6); + +// init time +void initTime(string s){ + int tmp = 0; + for(int i=0; i= 60){ + curM-= 60; + curH += 1; + } + if(curH >= 12){ + curH -= 12; + } +} + +int main(){ + int numCmd; + string s, cmd, _time; + cin>>s; + initTime(s); + for(int i=0; i<6; ++i) + cin>>h[i]; + cin>>numCmd; + for(int i=0; i>cmd>>_time; + calc(_time); + } + int ans = 0; + for(int i=0; i<6; ++i){ + ans += h[i]; + } + cout<<((ans >= 100)?100:ans)< Date: Fri, 22 Jul 2022 17:07:22 +0900 Subject: [PATCH 147/213] BOJ --- BaekJoon/25240.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 BaekJoon/25240.py diff --git a/BaekJoon/25240.py b/BaekJoon/25240.py new file mode 100644 index 0000000..1872402 --- /dev/null +++ b/BaekJoon/25240.py @@ -0,0 +1,51 @@ +import sys +input = sys.stdin.readline + +auth = { + "X" : 1, + "W" : 2, + "R" : 4 +} + +user = {} +file_ = {} + +U, F = map(int, input().split()) + +for j in range(U): + a = input().rstrip().split() + user[a[0]] = [] + + if len(a) == 2: + grp = a[1].split(',') + for i in grp: + user[a[0]].append(i) + user[a[0]].append(a[0]) + +for i in range(F): + info = list(input().split()) + file_[info[0]] = info[1:] + +q = int(input()) + +for _ in range(q): + name, fname, perm = input().split() + p = file_[fname][0] + if name == file_[fname][1]: + if int(p[0]) & auth[perm]: + print(1) + continue + flag = False + for i in user[name]: + if i == file_[fname][2]: + if int(p[1]) & auth[perm]: + print(1) + flag = True + break + if flag: + continue + + if int(p[2]) & auth[perm]: + print(1) + continue + print(0) From fa726f97278a1171520f9b364da2adf3b91641d6 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 25 Jul 2022 22:15:43 +0900 Subject: [PATCH 148/213] BOJ --- BaekJoon/16564.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/16564.cpp diff --git a/BaekJoon/16564.cpp b/BaekJoon/16564.cpp new file mode 100644 index 0000000..1f7de80 --- /dev/null +++ b/BaekJoon/16564.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +typedef long long ll; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + ll n, k; + cin >> n >> k; + vectorv(n); + for (int i = 0; i < n; ++i) + cin >> v[i]; + ll l = 0, r = 2e9; + ll ans = 0; + while (l <= r) { + ll mid = (l + r)/2; + ll tmp = 0; + for (int i = 0; i < n; ++i) { + if (v[i] >= mid) continue; + tmp += (mid - v[i]); + } + if (tmp > k) { + r = mid - 1; + } + else { + ans = mid; + l = mid + 1; + } + } + cout << ans << endl; + return 0; +} From 8bea48db56b8d09a2050c7d729ffc4f59f688cf8 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 26 Jul 2022 18:23:45 +0900 Subject: [PATCH 149/213] BOJ --- BaekJoon/25242.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 BaekJoon/25242.py diff --git a/BaekJoon/25242.py b/BaekJoon/25242.py new file mode 100644 index 0000000..601f628 --- /dev/null +++ b/BaekJoon/25242.py @@ -0,0 +1,74 @@ +import sys +from collections import deque +input = sys.stdin.readline + +N, Q = map(int,input().split()) + +idx = dict() + +graph = [[] for _ in range(200005)] + +transfer_node = [] + +station_info = [[] for _ in range(200005)] + +cnt = 0 +def naming(tt): + global cnt + if tt not in idx: + idx[tt] = cnt + cnt += 1 + return idx[tt] + +for j in range(N): + l = list(input().split()) + num = int(l[0]) + for i in range(1, num + 1): + id = naming(l[i]) + station_info[id].append([j, i]) + if i > 1: + id2 = naming(l[i-1]) + graph[id].append(id2) + graph[id2].append(id) + +for i in range(200005): + if len(station_info[i]) > 1: + transfer_node.append(i) + +num_transfer_node = len(transfer_node) + +dist = [[2000000000 for _ in range(200005)] for _ in range(num_transfer_node)] + +for i in range(num_transfer_node): + # 환승역 기준 + dq = deque() + dq.append(transfer_node[i]) + dist[i][transfer_node[i]] = 0 + while len(dq) != 0: + cur_node = dq.popleft() + for node in graph[cur_node]: + if dist[i][node] == 2000000000: + dist[i][node] = dist[i][cur_node] + 2 + dq.append(node) + +# print("transfer node : ", transfer_node) +# print("station_info : ", station_info[:10]) +# print("dist : ", dist) + +for _ in range(Q): + _from, _to = input().split() + fn = naming(_from) + tn = naming(_to) + + ans = 2000000000 + # 환승 할 때 + for i in range(num_transfer_node): + ans = min(ans, (dist[i][fn] + dist[i][tn])) + # 안 할 떄 + if len(station_info[fn]) == 1 and len(station_info[tn]) == 1 and station_info[fn][0][0] == station_info[tn][0][0]: + # 노선이 같을 경우 + # update + ans = min(ans, abs(station_info[fn][0][1] - station_info[tn][0][1]) * 2) + if ans >= 2000000000: + ans = -1 + print(ans) From 2c93fd146047370503452ddee68246b767c2de2b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 27 Jul 2022 22:50:13 +0900 Subject: [PATCH 150/213] BOJ --- BaekJoon/21921.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 BaekJoon/21921.cpp diff --git a/BaekJoon/21921.cpp b/BaekJoon/21921.cpp new file mode 100644 index 0000000..406ec69 --- /dev/null +++ b/BaekJoon/21921.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int X, N; + cin >> X >> N; + vectorv(X); + int tsum = 0; + int ttsum = 0; + for (int i = 0; i < X; ++i) { + cin >> v[i]; + if (i < N) tsum += v[i]; + } + ttsum = tsum; + int max_ = tsum; + for (int i = 0; i < X - N; ++i) { + tsum -= v[i]; + tsum += v[i + N]; + if (max_ < tsum) { + max_ = tsum; + } + } + int cnt = 0; + if (ttsum == max_) cnt += 1; + for (int i = 0; i < X - N; ++i) { + ttsum -= v[i]; + ttsum += v[i + N]; + if (ttsum == max_) { + cnt += 1; + } + } + if (max_ == 0) { + cout << "SAD" << '\n'; + } + else { + cout << max_ << '\n' << cnt<<'\n'; + } + return 0; +} From 1b9d968784bbedad16f09b618b2253b8f20e736b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 28 Jul 2022 15:47:32 +0900 Subject: [PATCH 151/213] BOJ --- BaekJoon/2133.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BaekJoon/2133.cpp diff --git a/BaekJoon/2133.cpp b/BaekJoon/2133.cpp new file mode 100644 index 0000000..99b90a4 --- /dev/null +++ b/BaekJoon/2133.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n; + cin>>n; + vectordp(n+1, 0); + dp[0] = 1; + dp[2] = 3; + for(int i=4; i<=n; i+=2){ + dp[i] = dp[i-2] * 3; + for(int j=0; j <= i-4; j += 2){ + dp[i] += dp[j] * 2; + } + } + cout< Date: Thu, 28 Jul 2022 21:18:45 +0900 Subject: [PATCH 152/213] BOJ --- BaekJoon/11758.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 BaekJoon/11758.cpp diff --git a/BaekJoon/11758.cpp b/BaekJoon/11758.cpp new file mode 100644 index 0000000..6ff158b --- /dev/null +++ b/BaekJoon/11758.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +typedef pair pii; + +int ccw(pii a, pii b, pii c) { + return a.first * b.second + b.first * c.second + a.second * c.first - (a.second * b.first + b.second * c.first + a.first * c.second); +} + +int main() { + pii a[3]; + for (int i = 0; i < 3; ++i) + cin >> a[i].first >> a[i].second; + int t = ccw(a[0], a[1], a[2]); + if (t == 0) cout << 0 << '\n'; + else cout << ((t < 0) ? -1 : 1) << '\n'; + return 0; +} From dda66dacf7492a8597c6c518307069afd9ea06b2 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 29 Jul 2022 18:12:05 +0900 Subject: [PATCH 153/213] BOJ --- BaekJoon/11051.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 BaekJoon/11051.cpp diff --git a/BaekJoon/11051.cpp b/BaekJoon/11051.cpp new file mode 100644 index 0000000..05c53b6 --- /dev/null +++ b/BaekJoon/11051.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int dp[1010][1010]; + +int recursive(int n, int k){ + if(n == k || k == 0) return dp[n][k] = 1; + if(dp[n][k]) return dp[n][k]; + else + return dp[n][k] = (recursive(n-1, k-1) + recursive(n-1, k)) % 10007; +} + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int N, K; + cin>>N>>K; + int ans = recursive(N, K); + cout< Date: Fri, 29 Jul 2022 18:34:56 +0900 Subject: [PATCH 154/213] BOJ --- BaekJoon/11055.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 BaekJoon/11055.cpp diff --git a/BaekJoon/11055.cpp b/BaekJoon/11055.cpp new file mode 100644 index 0000000..45ff6f1 --- /dev/null +++ b/BaekJoon/11055.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +vectorv, dp; + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n, ans = 0; + cin>>n; + v.resize(n); + dp.resize(n); + for(int i=0; i>v[i]; + dp[i] = v[i]; + for(int j=0; j v[j] && dp[i] < dp[j] + v[i]){ + dp[i] = dp[j] + v[i]; + } + } + ans = max(ans, dp[i]); + } + cout< Date: Fri, 29 Jul 2022 21:05:50 +0900 Subject: [PATCH 155/213] BOJ --- BaekJoon/25243.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 BaekJoon/25243.cpp diff --git a/BaekJoon/25243.cpp b/BaekJoon/25243.cpp new file mode 100644 index 0000000..d6a0919 --- /dev/null +++ b/BaekJoon/25243.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; +#define X first +#define Y second + +typedef pair pii; +typedef pair pis; + +typedef struct train_info { + int num; // 열차 번호 + int curTime; // 현재 시간 + int startTime; // 시작 시간 +}train_info; + +bool cmp(const train_info & a, const train_info & b){ + if(a.curTime == b.curTime){ + return a.num < b.num; + } + return a.curTime < b.curTime; +} + +int parsing(string s) { + int p = 0; + int tmp = 0; + for (int i = 0; i < s.size(); ++i) { + if (s[i] == ':') { + p += 60 * tmp; + tmp = 0; + } + else { + tmp *= 10; + tmp += int(s[i] - '0'); + } + } + p += tmp; + return p; +} + +void toStringTime(int t) { + cout << setw(2) << setfill('0') << ((t / 60) % 24) << ":" << setw(2) << setfill('0') << (t % 60) << "\n"; +} + +int arr[4] = { 7,7,8,10 }; // 소요 시간 + +int main() { + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int C, H, num; + int curTime = 0; + string t; + cin >> C >> H; + // 영역마다 하나씩 정의, 0번 영역에 들어가길 기다리는지, 1번 영역에 들어가길 기다리는지, ... + vectorv[4]; + //false면 들어갈 수 있는 상태임. + vectorstatus(4, false); + vectorvti[4]; + // 영역별 운행이 끝나는 시간 + vectorendTime(4, -1); + + for (int i = 0; i < C + H; ++i) { + cin >> num >> t; + int a = parsing(t); + if (num & 1) + v[0].push_back({ num, a, a }); + else + v[3].push_back({ num, a, a }); + } + for(int i=0; i<4; ++i){ + sort(v[i].begin(), v[i].end(), cmp); + } + vectorans; + + while (true) { + if (ans.size() == C + H) break; + for (int i = 0; i < 4; ++i) { + if (curTime == endTime[i] && status[i]) { + status[i] = false; // 열어줍니다. + train_info cur = vti[i].back(); // 기차의 정보 + cur.curTime = curTime + 1; // 대기시간 포함 + vti[i].pop_back(); + endTime[i] = -1; + if (cur.num & 1) { + if (i == 3) { + cur.curTime -= 1; + ans.push_back({ cur.num, cur.curTime }); + } + else{ + v[i + 1].push_back(cur); + sort(v[i+1].begin(), v[i+1].end(), cmp); + } + } + else { + if (i == 0) { + cur.curTime -= 1; + ans.push_back({ cur.num, cur.curTime }); + } + else{ + v[i - 1].push_back(cur); + sort(v[i-1].begin(), v[i-1].end(), cmp); + } + } + } + if (status[i]) { + // 열차가 이미 구간에 있어서 지나갈 수 없다. + continue; + } + else { + if (!v[i].empty()) { + // 들어갈 수 있는 경우 + int idx = 0; + int flag = false; + for(int j=0; j Date: Mon, 1 Aug 2022 15:41:35 +0900 Subject: [PATCH 156/213] BOJ --- BaekJoon/11054.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/11054.cpp diff --git a/BaekJoon/11054.cpp b/BaekJoon/11054.cpp new file mode 100644 index 0000000..c9d255e --- /dev/null +++ b/BaekJoon/11054.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +int main(){ + int n; + cin>>n; + vectorv(n); + vector>lis(2, vector(n)); + for(int i=0; i>v[i]; + + for(int i=0; i v[j]) + min_ = max(min_, lis[0][j]); + } + lis[0][i] = min_ + 1; + } + int ans = 0; + for(int i=n-1; i>=0; --i){ + int min_ = 0; + for(int j=n-1; j>=i; --j){ + if(v[i] > v[j]) + min_ = max(min_, lis[1][j]); + } + lis[1][i] = min_ + 1; + ans = max(ans, lis[1][i] + lis[0][i]); + } + + cout< Date: Tue, 2 Aug 2022 16:11:49 +0900 Subject: [PATCH 157/213] BOJ --- BaekJoon/15732.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 BaekJoon/15732.cpp diff --git a/BaekJoon/15732.cpp b/BaekJoon/15732.cpp new file mode 100644 index 0000000..b2423af --- /dev/null +++ b/BaekJoon/15732.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +typedef long long ll; + +typedef struct dInfo{ + int src; + int dst; + int interval; +}; + +int N, K, D; +vectorrules; + +bool decision(int m){ + ll dotori = 0; + for(int i=0; i= D; +} + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + cin>>N>>K>>D; + rules.resize(K); + for(int i=0; i>rules[i].src>>rules[i].dst>>rules[i].interval; + } + int l = 0, r = N+1; + while(l<=r){ + int mid = (l+r)/2; + if(decision(mid)){ + r = mid - 1; + }else{ + l = mid + 1; + } + } + cout< Date: Thu, 4 Aug 2022 17:26:05 +0900 Subject: [PATCH 158/213] BOJ --- BaekJoon/6087.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 BaekJoon/6087.py diff --git a/BaekJoon/6087.py b/BaekJoon/6087.py new file mode 100644 index 0000000..8a3c5c2 --- /dev/null +++ b/BaekJoon/6087.py @@ -0,0 +1,45 @@ +import sys +from collections import deque +input = sys.stdin.readline + +# 서 동 북 남 +dx = [-1,1,0,0] +dy = [0,0,-1,1] + +w, h = map(int, input().split()) + +ls = [input().rstrip() for _ in range(h)] + +src = [0,0] + +for i in range(h): + for j in range(w): + if ls[i][j] == 'C': + src = [i,j] + break + +visited = [[-2 for _ in range(w)] for _ in range(h)] + +dq = deque() +visited[src[0]][src[1]] = -1 +dq.append(src) + +while dq: + cur = dq.popleft() + if cur != src and ls[cur[0]][cur[1]] =='C': + print(visited[cur[0]][cur[1]]) + break + for i in range(4): + nx = cur[0] + ny = cur[1] + while(True): + nx += dx[i] + ny += dy[i] + if 0<=nx visited[cur[0]][cur[1]] + 1): + dq.append([nx,ny]) + visited[nx][ny] = visited[cur[0]][cur[1]]+1 + else: + break From dec429f0fcec8b80d0252647eee72b9720eae1f0 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 4 Aug 2022 17:26:40 +0900 Subject: [PATCH 159/213] BOJ --- BaekJoon/14938.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 BaekJoon/14938.py diff --git a/BaekJoon/14938.py b/BaekJoon/14938.py new file mode 100644 index 0000000..9fc2c2a --- /dev/null +++ b/BaekJoon/14938.py @@ -0,0 +1,42 @@ +import sys +import heapq as hq +input = sys.stdin.readline + +def dijkstra(src): + visited = [2e9 for _ in range(n+1)] + h = [] + visited[src] = 0 + hq.heappush(h, [0,src]) + while h: + cur = hq.heappop(h) + # if visited[cur[1]] < cur[0]: + # continue + for info in ls[cur[1]]: + next_weight = cur[0] + info[0] + if visited[info[1]] > next_weight: + visited[info[1]] = next_weight + hq.heappush(h, [next_weight, info[1]]) + + return visited + +n,m,r = map(int,input().split()) + +item = list(map(int,input().split())) + +ls =[[] for _ in range(n+1)] + +for _ in range(r): + s,d,w = map(int,input().split()) + ls[s].append([w, d]) + ls[d].append([w, s]) + +ans = -1 +for i in range(1,n + 1): + visited = dijkstra(i) + tmp = 0 + for j in range(1, n+1): + if visited[j] <= m: + tmp += item[j-1] + ans = max(ans, tmp) + +print(ans) From 4d744bd750cac121136345ef6a1aecc1f646c3c8 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 4 Aug 2022 20:34:17 +0900 Subject: [PATCH 160/213] BOJ --- BaekJoon/2211.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 BaekJoon/2211.py diff --git a/BaekJoon/2211.py b/BaekJoon/2211.py new file mode 100644 index 0000000..39fdcb4 --- /dev/null +++ b/BaekJoon/2211.py @@ -0,0 +1,35 @@ +import sys +import heapq as hq +input = sys.stdin.readline + +def dijkstra(src): + h = [] + hq.heappush(h, [0, src]) + visited = [2e9 for _ in range(N+1)] + visited[src] = 0 + while h: + cur = hq.heappop(h) + # 연결된 노드 + for i in ls[cur[1]]: + next_cost = i[1] + cur[0] + if visited[i[0]] > next_cost: + visited[i[0]] = next_cost + updated[i[0]] = cur[1] + hq.heappush(h, [next_cost, i[0]]) + +N, M = map(int,input().split()) + +ls = [[] for _ in range(N+1)] + +updated = [0 for _ in range(N+1)] + +for _ in range(M): + s, d, w = map(int,input().split()) + ls[s].append([d, w]) + ls[d].append([s, w]) + +ans = N-1 +dijkstra(1) +print(ans) +for i in range(2, N+1): + print(i, updated[i]) From 6d67b44a1edca5f457dfc251e0fea740825cbf77 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 5 Aug 2022 17:23:57 +0900 Subject: [PATCH 161/213] BOJ --- BaekJoon/1774.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 BaekJoon/1774.py diff --git a/BaekJoon/1774.py b/BaekJoon/1774.py new file mode 100644 index 0000000..ea0d835 --- /dev/null +++ b/BaekJoon/1774.py @@ -0,0 +1,59 @@ +import sys +sys.setrecursionlimit(10**6) +input = sys.stdin.readline + +def find(a) : + if parent[a] == a : + return a + parent[a] = find(parent[a]) + return parent[a] + +def union(x, y): + a = find(x) + b = find(y) + + if parent[a] < parent[b]: + parent[parent[a]] = parent[b] + else: + parent[parent[b]] = parent[a] + + return a == b + +def get_distance(a, b): + return (pow(a[0] - b[0], 2) + pow(a[1] - b[1], 2))**(1/2) + +n, m = map(int,input().split()) + +parent = [i for i in range(n)] + +node = [list(map(float,input().split())) for _ in range(n)] + +# 이미 연결된 곳 연결하기 +for i in range(m): + f, t = map(int,input().split()) + dis = get_distance(node[f-1], node[t-1]) + union(f-1, t-1) + +edges = [] + +for i in range(n): + for j in range(n): + if i==j : continue + edges.append([i, j, get_distance(node[i], node[j])]) + +edges.sort(key = lambda x : x[2]) + +cnt = 0 +idx = 0 +ans = 0.0 + +for cur in edges: + flag = union(cur[0], cur[1]) + idx += 1 + if flag: + continue + else: + cnt += 1 + ans += cur[2] + +print("%.2f"%ans) From e548f118806c52bdffaec4efa290dfd7c01da253 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 6 Aug 2022 16:58:31 +0900 Subject: [PATCH 162/213] BOJ --- BaekJoon/4386.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 BaekJoon/4386.py diff --git a/BaekJoon/4386.py b/BaekJoon/4386.py new file mode 100644 index 0000000..a42e3d3 --- /dev/null +++ b/BaekJoon/4386.py @@ -0,0 +1,54 @@ +import sys +sys.setrecursionlimit(10**6) +input = sys.stdin.readline + +def find(a) : + if parent[a] == a : + return a + parent[a] = find(parent[a]) + return parent[a] + +def union(x, y): + a = find(x) + b = find(y) + + if parent[a] < parent[b]: + parent[parent[a]] = parent[b] + else: + parent[parent[b]] = parent[a] + + return a == b + + +def get_distance(a, b): + return round((pow(a[0] - b[0], 2) + pow(a[1] - b[1], 2))**(1/2), 2) + +n = int(input()) + +parent = [i for i in range(n)] + +node = [list(map(float,input().split())) for _ in range(n)] + +edges = [] + +for i in range(n): + for j in range(n): + if i==j : continue + edges.append([i, j, get_distance(node[i], node[j])]) + +edges.sort(key = lambda x : x[2]) + +cnt = 0 +idx = 0 +ans = 0.0 + +for cur in edges: + flag = union(cur[0], cur[1]) + idx += 1 + if flag: + continue + else: + cnt += 1 + ans += cur[2] + +print(ans) From 4419950380517815716aae5ead93f37a156dff1b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 19 Sep 2022 15:13:52 +0900 Subject: [PATCH 163/213] level2 --- ...0 \352\265\254\355\225\230\352\270\260.py" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "programmers/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" diff --git "a/programmers/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" "b/programmers/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" new file mode 100644 index 0000000..2d4c2ce --- /dev/null +++ "b/programmers/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" @@ -0,0 +1,35 @@ +def nNumber(n, k): + ls = [] + while n != 0: + ls.append(n % k) + n //= k + return ls[::-1] + +def chkPrime(n): + if n <= 1: + return False + for i in range(2, int(n**(1/2))+1): + if n % i == 0: + return False + return True + +def solution(n, k): + answer = 0 + ls = nNumber(n, k) + tmp = 0 + + for i in ls: + if i == 0: + if chkPrime(tmp): + answer += 1 + tmp = 0 + + else: + tmp *= 10 + tmp += i + + if tmp != 0: + if chkPrime(tmp): + answer += 1 + + return answer From 686b63565795430e4768480668cffb2f61d1865c Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 19 Sep 2022 16:55:19 +0900 Subject: [PATCH 164/213] level 2 --- ...4\352\270\210 \352\263\204\354\202\260.py" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "programmers/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" diff --git "a/programmers/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" "b/programmers/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" new file mode 100644 index 0000000..852668d --- /dev/null +++ "b/programmers/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" @@ -0,0 +1,43 @@ +import math + +# a : outTime, b : inTime +def calTime(a, b): + if a[1] < b[1]: + t = 60 - (b[1] - a[1]) + a[0] -= 1 + else: + t = a[1] - b[1] + t += (a[0] - b[0]) * 60 + return t + +def solution(fees, records): + a,b,c,d = fees + answer = [] + dc = {} + for record in records: + info = record.split() + h, m = map(int, info[0].split(":")) + number = info[1] + if info[2] == "IN": + if info[1] in dc: + cumul = dc[info[1]][4] + else: + cumul = 0 + dc[info[1]] = [h, m, 0, True, cumul] + else: + e = calTime([h, m], dc[info[1]][:2]) + dc[info[1]][4] += e + dc[info[1]][3] = False + + sd = sorted(dc.items()) + for key in sd: + if key[1][3]: + e = calTime([23, 59], key[1][:2]) + key[1][4] += e + if key[1][4] <= a: + key[1][2] += b + else: + print(key[1][4], a, math.ceil((key[1][4] - a)/c)) + key[1][2] += (b + (math.ceil((key[1][4]-a)/c)) * d) + answer.append(key[1][2]) + return answer From dd24f969e541de40ebc8768804f9625dc4c883f2 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 20 Sep 2022 14:23:02 +0900 Subject: [PATCH 165/213] level 3 - imos --- ...2\354\235\200 \352\261\264\353\254\274.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "programmers/\355\214\214\352\264\264 \353\220\230\354\247\200 \354\225\212\354\235\200 \352\261\264\353\254\274.py" diff --git "a/programmers/\355\214\214\352\264\264 \353\220\230\354\247\200 \354\225\212\354\235\200 \352\261\264\353\254\274.py" "b/programmers/\355\214\214\352\264\264 \353\220\230\354\247\200 \354\225\212\354\235\200 \352\261\264\353\254\274.py" new file mode 100644 index 0000000..83f77d9 --- /dev/null +++ "b/programmers/\355\214\214\352\264\264 \353\220\230\354\247\200 \354\225\212\354\235\200 \352\261\264\353\254\274.py" @@ -0,0 +1,37 @@ +def solution(board, skill): + answer = 0 + imos = [[0 for _ in range(len(board[0]) + 1)] for _ in range(len(board) + 1)] + + for line in skill: + t, r1, c1, r2, c2, weight = line + # attck + if t == 1: + weight *= -1 + + # r1, c1 + + imos[r1][c1] += weight + # r1, c2 - + imos[r1][c2+1] -= weight + # r2, c1 - + imos[r2+1][c1] -= weight + # r2, c2 + + imos[r2+1][c2+1] += weight + + for i in range(len(imos)): + cur = 0 + for j in range(len(imos[0])): + cur += imos[i][j] + imos[i][j] = cur + + for i in range(len(imos[0])): + cur = 0 + for j in range(len(imos)): + cur += imos[j][i] + imos[j][i] = cur + + for i in range(len(board)): + for j in range(len(board[0])): + board[i][j] += imos[i][j] + if board[i][j] > 0: + answer += 1 + return answer From 06f257819a79bf8096e1a6ef2e6eb572bd72f7dd Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 21 Sep 2022 19:02:37 +0900 Subject: [PATCH 166/213] level 2 Why 3 * len(queue1)? --- ...4 \353\247\214\353\223\244\352\270\260.py" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "programmers/\353\221\220 \355\201\220 \355\225\251 \352\260\231\352\262\214 \353\247\214\353\223\244\352\270\260.py" diff --git "a/programmers/\353\221\220 \355\201\220 \355\225\251 \352\260\231\352\262\214 \353\247\214\353\223\244\352\270\260.py" "b/programmers/\353\221\220 \355\201\220 \355\225\251 \352\260\231\352\262\214 \353\247\214\353\223\244\352\270\260.py" new file mode 100644 index 0000000..f335628 --- /dev/null +++ "b/programmers/\353\221\220 \355\201\220 \355\225\251 \352\260\231\352\262\214 \353\247\214\353\223\244\352\270\260.py" @@ -0,0 +1,25 @@ +from collections import deque + +def solution(queue1, queue2): + q1Sum = sum(queue1) + q2Sum = sum(queue2) + dq1 = deque(queue1) + dq2 = deque(queue2) + answer = 0 + + for _ in range(3*len(queue1)): + if q1Sum > q2Sum: + q1Sum -= dq1[0] + q2Sum += dq1[0] + t = dq1.popleft() + dq2.append(t) + elif q1Sum < q2Sum: + q1Sum += dq2[0] + q2Sum -= dq2[0] + t = dq2.popleft() + dq1.append(t) + else: + return answer + answer += 1 + + return -1 From 1afe3d8c32e11dffead2ee37a3819cfa1b9f8840 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 22 Sep 2022 14:34:03 +0900 Subject: [PATCH 167/213] =?UTF-8?q?level=202=20-=20=EC=99=84=EC=A0=84=20?= =?UTF-8?q?=ED=83=90=EC=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...21\352\266\201\353\214\200\355\232\214.py" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "programmers/\354\226\221\352\266\201\353\214\200\355\232\214.py" diff --git "a/programmers/\354\226\221\352\266\201\353\214\200\355\232\214.py" "b/programmers/\354\226\221\352\266\201\353\214\200\355\232\214.py" new file mode 100644 index 0000000..ab5f700 --- /dev/null +++ "b/programmers/\354\226\221\352\266\201\353\214\200\355\232\214.py" @@ -0,0 +1,74 @@ +# 첫 번째 접근 +# 1발에 몇 점의 이득을 가지나? +# 상대가 한 발 이상 맞췄을 때, 해당 점수 * 2 / 화살 개수 +# 그렇지 않을 때 -> 그 점수 그대로 +# [과녁 점수, 화살 개수, cost] +# n : 화살의 개수, info : 상대가 얼마나 맞췄는지 +# 왜 틀렸지? + +# def solution(n, info): +# answer = [0 for _ in range(11)] +# score = [0,0] +# ls = [] + +# for i in range(len(info)): +# if info[i] == 0: +# ls.append([10-i, info[i] + 1, 10-i]) +# else: +# score[0] += 10-i +# ls.append([10-i, info[i] + 1, 2 * (10-i) / (info[i] + 1)]) + +# ls = sorted(ls, key = lambda item: item[2], reverse=True) + + +# for l in ls: +# # 쏠 수 있는 경우 +# if n >= l[1]: +# n -= l[1] +# score[1] += l[0] +# answer[10 - l[0]] = l[1] +# if l[1] != 1: +# score[0] -= l[0] + +# # 남은 화살 처리 +# if n != 0: +# answer[10] = n + +# if score[0] >= score[1]: +# return [-1] +# else: +# return answer + + +# 두 번째 접근 +# 완전 탐색 +# 경우의 수가 n개의 화살 +from itertools import combinations_with_replacement + +def solution(n, info): + gap = 0 + answer = [-1] + for cwr in combinations_with_replacement(list(range(11)), n): + t = [0 for _ in range(11)] + for score in cwr: + t[10-score] += 1 + + # 비교 + a = 0 + b = 0 + + for i in range(11): + if not info[i] and not t[i]: + continue + if info[i] >= t[i]: + a += (10-i) + else: + b += (10-i) + + tmp_gap = b-a + + if tmp_gap > gap: + gap = tmp_gap + answer = t + + return answer From 9c4fa2537923cae5e777f59ec4f98dc874a14664 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 23 Sep 2022 13:56:47 +0900 Subject: [PATCH 168/213] =?UTF-8?q?level=203=20-=20two=20pointer=20[2020?= =?UTF-8?q?=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20=EC=9D=B8=ED=84=B4=EC=8B=AD]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\204\235 \354\207\274\355\225\221.py" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "programmers/\353\263\264\354\204\235 \354\207\274\355\225\221.py" diff --git "a/programmers/\353\263\264\354\204\235 \354\207\274\355\225\221.py" "b/programmers/\353\263\264\354\204\235 \354\207\274\355\225\221.py" new file mode 100644 index 0000000..71aa778 --- /dev/null +++ "b/programmers/\353\263\264\354\204\235 \354\207\274\355\225\221.py" @@ -0,0 +1,50 @@ +def solution(gems): + ansL = 2e9 + answer = [] + dc_cnt = {} + cnt = 0 + for i in gems: + if i not in dc_cnt: + dc_cnt[i] = 0 + cnt += 1 + # init + l = 0 + r = 0 + dc_cnt[gems[0]] = 1 + aCnt = 1 + + while l<=r: + # 전부 다 찾았으면 + if aCnt == cnt: + if ansL > r-l: + answer = [l+1, r+1] + ansL = r-l + # 무작정 false로 만들면 안됨 + # 왜냐면 구간에 해당하는 gem이 있을수도 있기 때문에 + dc_cnt[gems[l]] -= 1 + if dc_cnt[gems[l]] == 0: + # dc[gems[l]] = False + aCnt -= 1 + l += 1 + continue + + # r을 하나씩 오른쪽으로 + if r < len(gems)-1: + r += 1 + if dc_cnt[gems[r]] == 0: + aCnt += 1 + # dc[gems[r]] = True + dc_cnt[gems[r]] += 1 + elif gems[l] == gems[r]: + dc_cnt[gems[l]] -= 1 + l += 1 + + else: + dc_cnt[gems[l]] -= 1 + if dc_cnt[gems[l]] == 0: + # dc[gems[l]] = False + aCnt -= 1 + l += 1 + + # answer = [l+1, r+1] + return answer From b437163734adfc5557411f02c0c5b2785977c66b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 24 Sep 2022 23:43:02 +0900 Subject: [PATCH 169/213] kakao blind --- programmers/kakao_4.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 programmers/kakao_4.py diff --git a/programmers/kakao_4.py b/programmers/kakao_4.py new file mode 100644 index 0000000..7d49b8b --- /dev/null +++ b/programmers/kakao_4.py @@ -0,0 +1,33 @@ +# 분할정복 +# 1. 십진수 -> 이진수로 변환 +# + +def chk(s): + print(s) + if len(s) == 1: + return True + if int(s) == 0: + return True + if s[len(s)//2] != '1': + return False + else: + return chk(s[:len(s)//2]) and chk(s[len(s)//2+1:]) + + +l = [pow(2, i)-1 for i in range(8)] + +def solution(numbers): + answer = [] + for i in numbers: + b = bin(i)[2:] + bl = 0 + for num in range(8): + if len(b) <= l[num]: + bl = num + break + b = ''.join('0' for _ in range(l[bl] - len(b))) + b + answer.append(1 if chk(b) else 0) + # chk + return answer + +print(solution([63, 111, 95, 5, 7])) From 3f334aa515b769231918d98d96c1bc6a093539cc Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 2 Oct 2022 15:27:45 +0900 Subject: [PATCH 170/213] BOJ --- BaekJoon/10282.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 BaekJoon/10282.py diff --git a/BaekJoon/10282.py b/BaekJoon/10282.py new file mode 100644 index 0000000..e047c0a --- /dev/null +++ b/BaekJoon/10282.py @@ -0,0 +1,38 @@ +import sys +import heapq as hq +input = sys.stdin.readline + +def dijkstra(src): + visited = [2e9 for _ in range(n+1)] + h = [] + visited[src] = 0 + hq.heappush(h, [0, src]) + + while h: + cur = hq.heappop(h) + if visited[cur[1]] < cur[0]: + continue + for info in ls[cur[1]]: + next_weight = cur[0] + info[0] + if visited[info[1]] > next_weight: + visited[info[1]] = next_weight + hq.heappush(h, [next_weight, info[1]]) + + return visited + +tc = int(input()) +for _ in range(tc): + n,d,c = map(int,input().split()) + ls = [[] for _ in range(n + 1)] + for i in range(d): + a, b, s = map(int,input().split()) + # ls[a].append([s, b]) + ls[b].append([s, a]) + cnt = 0 + max_ = 0 + for i in dijkstra(c): + if i != 2e9: + cnt += 1 + if i > max_: + max_ = i + print(cnt, max_) From 8d1c4c6f5c31ba1e88b3ecc2b397190290f4ba7c Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 9 Oct 2022 16:02:36 +0900 Subject: [PATCH 171/213] eddited --- BaekJoon/1305.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/BaekJoon/1305.cpp b/BaekJoon/1305.cpp index 637d225..027284f 100644 --- a/BaekJoon/1305.cpp +++ b/BaekJoon/1305.cpp @@ -1,27 +1,29 @@ -#include +#include +#include +#include using namespace std; -vector makeTable(string pattern){ +vector makeTable(string pattern) { int lenPat = pattern.size(); vectorv(lenPat); // i == pattern, j == continuous - int i,j=0; - for(i=1; i 0 && pattern[i] != pattern[j]){ - j = v[j-1]; + int j = 0; + for (int i = 1; i < lenPat; ++i) { + while (j > 0 && pattern[i] != pattern[j]) { + j = v[j - 1]; } - if(pattern[i] == pattern[j]) + if (pattern[i] == pattern[j]) v[i] = ++j; } return v; } -int main(){ +int main() { int n; - cin>>n; + cin >> n; string a; - cin>>a; + cin >> a; vectorans = makeTable(a); - cout< Date: Sun, 16 Oct 2022 15:58:13 +0900 Subject: [PATCH 172/213] BOJ --- BaekJoon/2343.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 BaekJoon/2343.py diff --git a/BaekJoon/2343.py b/BaekJoon/2343.py new file mode 100644 index 0000000..886eb47 --- /dev/null +++ b/BaekJoon/2343.py @@ -0,0 +1,29 @@ +import sys +input = sys.stdin.readline + +def decision(n): + cnt = 0 + sum_ = 0 + for i in ls: + if sum_ + i > n: + sum_ = 0 + cnt += 1 + sum_ += i + if sum_: + cnt += 1 + return cnt > m + +n,m = map(int,input().split()) +ls = list(map(int,input().split())) + +l = max(ls) +r = int(2e9) + +while l <= r: + mid = (l+r) // 2 + if decision(mid): + l = mid + 1 + else: + r = mid - 1 + +print(l) From e385155a295702fdc3671eddedef738e702debc7 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 6 Nov 2022 21:35:05 +0900 Subject: [PATCH 173/213] BOJ --- BaekJoon/25758.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 BaekJoon/25758.py diff --git a/BaekJoon/25758.py b/BaekJoon/25758.py new file mode 100644 index 0000000..d0c0959 --- /dev/null +++ b/BaekJoon/25758.py @@ -0,0 +1,37 @@ +import sys +input = sys.stdin.readline + +dc = {} + +t = [] + +for i in range(26): + for j in range(26): + dc[chr(65 + i)+chr(65 + j)] = 0 + t.append(chr(65 + i)+chr(65 + j)) + +n = int(input()) +s = list(input().split()) + +candidate = [] + +for i in s: + dc[i] += 1 + +for i in range(len(t)): + for j in range(i, len(t)): + if i == j: + if dc[t[i]] >= 2: + a = max(t[i][0], t[i][1]) + if a not in candidate: + candidate.append(a) + elif dc[t[i]] >= 1 and dc[t[j]] >=1: + a = max(t[i][0], t[j][1]) + b = max(t[i][1], t[j][0]) + if a not in candidate: + candidate.append(a) + if b not in candidate: + candidate.append(b) + +print(len(candidate)) +print(*sorted(candidate)) From 5d7ffbc1affc057ce4e7732c32a6af7333331fef Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 13 Nov 2022 19:01:12 +0900 Subject: [PATCH 174/213] programmers --- ...14\354\240\204\355\225\230\352\270\260.py" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "programmers/\355\226\211\353\240\254 \355\205\214\353\221\220\353\246\254 \355\232\214\354\240\204\355\225\230\352\270\260.py" diff --git "a/programmers/\355\226\211\353\240\254 \355\205\214\353\221\220\353\246\254 \355\232\214\354\240\204\355\225\230\352\270\260.py" "b/programmers/\355\226\211\353\240\254 \355\205\214\353\221\220\353\246\254 \355\232\214\354\240\204\355\225\230\352\270\260.py" new file mode 100644 index 0000000..d9df87e --- /dev/null +++ "b/programmers/\355\226\211\353\240\254 \355\205\214\353\221\220\353\246\254 \355\232\214\354\240\204\355\225\230\352\270\260.py" @@ -0,0 +1,40 @@ +import sys +input = sys.stdin.readline + +def solution(rows, columns, queries): + # init + ls = [[0 for _ in range(columns)] for _ in range(rows)] + c = 1 + for i in range(rows): + for j in range(columns): + ls[i][j] = c + c += 1 + + answer = [] + for x1, y1, x2, y2 in queries: + # 가로 방향 + t = ls[x1-1][y1-1] + tmp = ls[x1-1][y1-1] + m = t + for i in range(y1, y2): + tmp = ls[x1-1][i] + ls[x1-1][i] = t + t = tmp + m = min(m, t) + for i in range(x1, x2): + tmp = ls[i][y2-1] + ls[i][y2-1] = t + t = tmp + m = min(m, t) + for i in range(y2-2, y1-2, -1): + tmp = ls[x2-1][i] + ls[x2-1][i] = t + t = tmp + m = min(m, t) + for i in range(x2-2, x1-2, -1): + tmp = ls[i][y1-1] + ls[i][y1-1] = t + t = tmp + m = min(m, t) + answer.append(m) + return answer From 1ef10df2461f0fb586119cb987dc2caa7db5359d Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 4 Dec 2022 20:09:46 +0900 Subject: [PATCH 175/213] BOJ --- BaekJoon/17070.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 BaekJoon/17070.py diff --git a/BaekJoon/17070.py b/BaekJoon/17070.py new file mode 100644 index 0000000..3785dab --- /dev/null +++ b/BaekJoon/17070.py @@ -0,0 +1,26 @@ +import sys +input = sys.stdin.readline + +N = int(input()) +ls = [ list(map(int, input().split())) for _ in range(N) ] +dp = [[[0,0,0] for _ in range(N)] for _ in range(N)] + +# dp[i][j][0] = dp[i][j-1][0] + dp[i][j-1][1] +# dp[i][j][2] = dp[i-1][j][1] + dp[i-1][j][2] +# ls[i][j-1] == 0 and ls[i-1][j] == 0 -> dp[i][j][1] = d[i-1][j-1][0] + dp[i-1][j-1][1] + dp[i-1][j-1][2] + +dp[0][1][0] = 1 + +for i in range(2, N): + if ls[0][i] == 0: + dp[0][i][0] = dp[0][i-1][0] + +for i in range(1, N): + for j in range(2, N): + if ls[i][j] == 0 and ls[i-1][j] ==0 and ls[i][j-1] == 0: + dp[i][j][2] = sum(dp[i-1][j-1]) + if ls[i][j] == 0: + dp[i][j][0] = dp[i][j-1][0] + dp[i][j-1][2] + dp[i][j][1] = dp[i-1][j][1] + dp[i-1][j][2] + +print(sum(dp[N-1][N-1])) From 3d4729257cc25d8333c37bdf162cc42ff4c5b601 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 20 Jan 2023 21:28:40 +0900 Subject: [PATCH 176/213] Create 16236.py --- BaekJoon/16236.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 BaekJoon/16236.py diff --git a/BaekJoon/16236.py b/BaekJoon/16236.py new file mode 100644 index 0000000..c69f81d --- /dev/null +++ b/BaekJoon/16236.py @@ -0,0 +1,96 @@ +# 16236 아기 상어 +# N x N 물고기 M마리, 아기 상어 1마리 +# 아기 상어 크기 : 2, 1초에 상하좌우로 인접한 한 칸씩 이동 +# 자기보다 큰 물고기가 있는 칸은 지나갈 수 없음. +# 자신의 크기보다 작은 물고기만 먹을 수 있음. +# 자신의 크기와 같은 수의 물고기를 먹을 때마다 크기가 1 증가 +# 1. 더이상 먹을 수 있는 물고기가 없다면 엄마한테 도움 요청 +# 2. 먹을 수 있는 물고기가 1마리면, 그 물고기를 먹는다. +# 3. 먹을 수 있는 물고기가 1마리보다 많으면, 거리가 가장 가까운 물고기를 먹으러 간다. +# 거리는 아기 상어가 있는 칸에서 물고기가 있는 칸으로 이동할 때, 지나야하는 칸의 개수의 최솟값 +# 거리가 가까운 물고기가 많다면, 가장 위에 있는 물고기, 그러한 물고기가 여러마리라면, 가장 왼쪽에 있는 물고기 +# 이동은 1초가 걸리고, 물고기를 먹는데 걸리는 시간은 없다. +# 0은 공백, 1,2,3,4,5,6은 칸에 있는 물고기의 크기 + +# 거리를 계산할 때 bfs + +import sys +from collections import deque +input = sys.stdin.readline + +dx = [0,0,1,-1] +dy = [1,-1,0,0] + +# def compare(x, y): +# if x[0] > y[0]: +# return 1 +# else: +# if x[1] > y[1]: +# return 1 +# if x[2] > y[2]: +# return 1 + +# bfs +# retrun [거리, 상어위치] +def getCandidate(start, n): + visited = [[False for _ in range(n)] for _ in range(n)] + dq = deque() + dq.append([start[0], start[1], 0]) + m[start[0]][start[1]] = 0 + res = [] + while dq: + cur = dq.popleft() + for i in range(4): + nx = cur[0] + dx[i] + ny = cur[1] + dy[i] + if 0<= nx Date: Mon, 30 Jan 2023 12:59:25 +0900 Subject: [PATCH 177/213] Create 11066.py --- BaekJoon/11066.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 BaekJoon/11066.py diff --git a/BaekJoon/11066.py b/BaekJoon/11066.py new file mode 100644 index 0000000..7041296 --- /dev/null +++ b/BaekJoon/11066.py @@ -0,0 +1,22 @@ +import sys +input = sys.stdin.readline + +T = int(input()) +for _ in range(T): + K = int(input()) + C = list(map(int, input().split())) + + d=[[-1 for _ in range(K)] for _ in range(K)] + for i in range(K): + d[i][i]=0 + for i in range(K-1): + d[i][i+1]=C[i]+C[i+1] + for l in range(3,K+1): + for i in range(K-l+1): + j=i+l-1 + tmp=2e9 + for t in range(i,j): + if tmp > d[i][t]+d[t+1][j]: + tmp = d[i][t]+d[t+1][j] + d[i][j]=tmp+sum(C[i:j+1]) + print(d[0][K-1]) From ae3a61a9ccd879ac0a2496f3a026b3b43e104f04 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 3 Feb 2023 22:08:56 +0900 Subject: [PATCH 178/213] Create 2482.py --- BaekJoon/2482.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 BaekJoon/2482.py diff --git a/BaekJoon/2482.py b/BaekJoon/2482.py new file mode 100644 index 0000000..202016a --- /dev/null +++ b/BaekJoon/2482.py @@ -0,0 +1,19 @@ +import sys +input = sys.stdin.readline + +n = int(input()) +k = int(input()) + +dp = [[0 for _ in range(k+1)] for _ in range(n+1)] + +MOD = int(1e9+3) + +for i in range(n+1): + dp[i][0] = 1 + dp[i][1] = i + +for i in range(2, n+1): + for j in range(2, k+1): + dp[i][j] = (dp[i-2][j-1] + dp[i-1][j]) % MOD + +print((dp[n-3][k-1] + dp[n-1][k]) % MOD) From 2c2975c392f6de4380f18d4024a8615146c3cbc2 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 4 Feb 2023 23:30:06 +0900 Subject: [PATCH 179/213] Create 24391_dsu.py --- BaekJoon/24391_dsu.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BaekJoon/24391_dsu.py diff --git a/BaekJoon/24391_dsu.py b/BaekJoon/24391_dsu.py new file mode 100644 index 0000000..aa3517d --- /dev/null +++ b/BaekJoon/24391_dsu.py @@ -0,0 +1,33 @@ +import sys +input = sys.stdin.readline +sys.setrecursionlimit(10**6) + +def getParent(n): + if parent[n] == n: + return n + else: + parent[n] = getParent(parent[n]) + return parent[n] + +def unionParent(ta,tb): + ta = getParent(ta) + tb = getParent(tb) + if ta != tb: + parent[tb] = ta + + +n, m = map(int,input().split()) + +parent = [i for i in range(n+1)] + +ans = 0 +for i in range(m): + s, d = map(int,input().split()) + unionParent(s, d) + +ls = list(map(int,input().split())) + +for i in range(n-1): + if getParent(ls[i]) != getParent(ls[i+1]): + ans += 1 +print(ans) From 7107dc0956477330bf6fe9f601df223d88e7f57a Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 6 Feb 2023 23:42:39 +0900 Subject: [PATCH 180/213] Create 2470.py --- BaekJoon/2470.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 BaekJoon/2470.py diff --git a/BaekJoon/2470.py b/BaekJoon/2470.py new file mode 100644 index 0000000..8abb792 --- /dev/null +++ b/BaekJoon/2470.py @@ -0,0 +1,21 @@ +import sys +input = sys.stdin.readline + +N = int(input()) + +ls = sorted(list(map(int,input().split()))) + +l = 0 +r = len(ls)-1 +ans = [2e9 + 1, 0, 0] + +while l < r: + tmp = ls[l] + ls[r] + if abs(tmp) < abs(ans[0]): + ans = [tmp, ls[l], ls[r]] + if tmp < 0: + l += 1 + else: + r -= 1 + +print(ans[1], ans[2]) From bd8dba0101a26f046dee1466cd373fa36403e6cb Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 7 Feb 2023 23:38:26 +0900 Subject: [PATCH 181/213] Create 1253.py --- BaekJoon/1253.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 BaekJoon/1253.py diff --git a/BaekJoon/1253.py b/BaekJoon/1253.py new file mode 100644 index 0000000..64768a6 --- /dev/null +++ b/BaekJoon/1253.py @@ -0,0 +1,26 @@ +import sys +input = sys.stdin.readline + +N = int(input()) + +ls = sorted(list(map(int,input().split()))) + +ans = 0 + +for i in range(len(ls)): + t = ls[:i] + ls[i+1:] + l = 0 + r = len(t)-1 + + + + while l < r: + tmp = t[l] + t[r] + if tmp == ls[i]: + ans += 1 + break + if ls[i] > tmp: + l += 1 + else: + r -= 1 +print(ans) From dc10ed4b9d0e8048f355565bcf663054a209c303 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 8 Feb 2023 22:42:58 +0900 Subject: [PATCH 182/213] Create 1240.py --- BaekJoon/1240.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 BaekJoon/1240.py diff --git a/BaekJoon/1240.py b/BaekJoon/1240.py new file mode 100644 index 0000000..71bcf87 --- /dev/null +++ b/BaekJoon/1240.py @@ -0,0 +1,36 @@ +import sys +import heapq as hq +input = sys.stdin.readline + +def dijkstra(src, dst): + visited = [2e9 for _ in range(N+1)] + h = [] + visited[src] = 0 + hq.heappush(h, [0, src]) + + while h: + cur = hq.heappop(h) + if visited[cur[1]] < cur[0]: + continue + for info in ls[cur[1]]: + next_weight = cur[0] + info[1] + if visited[info[0]] > next_weight: + visited[info[0]] = next_weight + hq.heappush(h, [next_weight, info[0]]) + + return visited[dst] + + +N, M = map(int,input().split()) + +ls = [[] for _ in range(N+1)] + + +for i in range(N-1): + t = list(map(int,input().split())) + ls[t[0]].append([t[1], t[2]]) + ls[t[1]].append([t[0], t[2]]) + +for _ in range(M): + t = list(map(int,input().split())) + print(dijkstra(t[0], t[1])) From 58b266056c244bfd3169f8b72e5b98595c3a3422 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 9 Feb 2023 23:53:31 +0900 Subject: [PATCH 183/213] Create 17951.py --- BaekJoon/17951.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 BaekJoon/17951.py diff --git a/BaekJoon/17951.py b/BaekJoon/17951.py new file mode 100644 index 0000000..01dd6ac --- /dev/null +++ b/BaekJoon/17951.py @@ -0,0 +1,24 @@ +import sys +input = sys.stdin.readline + +n, k = map(int,input().split()) +ls = list(map(int,input().split())) + +l = 0 +r = sum(ls) + +while l <= r: + mid = (l + r) //2 + t = 0 + cnt = 0 + for i in range(n): + t += ls[i] + if t>=mid: + cnt +=1 + t = 0 + if cnt >= k: + l = mid + 1 + else: + r = mid -1 + +print(r) From 3fa2a7a32ca4c7d71b0f6e2eba0536e3a828c19f Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 12 Feb 2023 23:53:15 +0900 Subject: [PATCH 184/213] Create 17352.py --- BaekJoon/17352.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 BaekJoon/17352.py diff --git a/BaekJoon/17352.py b/BaekJoon/17352.py new file mode 100644 index 0000000..fa5bee5 --- /dev/null +++ b/BaekJoon/17352.py @@ -0,0 +1,37 @@ +import sys +input = sys.stdin.readline +sys.setrecursionlimit(10**6) + +def getParent(n): + if parent[n] == n: + return n + else: + parent[n] = getParent(parent[n]) + return parent[n] + +def unionParent(ta,tb): + ta = getParent(ta) + tb = getParent(tb) + if ta != tb: + parent[tb] = ta + +def findParent(ta, tb): + a = getParent(ta) + b = getParent(tb) + return a == b + +n = int(input()) + +parent = [i for i in range(n+1)] + +ans = 0 +for i in range(n-2): + s, d = map(int,input().split()) + unionParent(s, d) + +for i in range(1,n+1): + if findParent(1, i): + continue + else: + print(getParent(1), getParent(i)) + unionParent(1, i) From d8de8e035421e99ce0eddf4e9c2b7583e18be351 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 13 Feb 2023 22:26:05 +0900 Subject: [PATCH 185/213] Create 1261.py --- BaekJoon/1261.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 BaekJoon/1261.py diff --git a/BaekJoon/1261.py b/BaekJoon/1261.py new file mode 100644 index 0000000..95ede76 --- /dev/null +++ b/BaekJoon/1261.py @@ -0,0 +1,36 @@ +import sys +import heapq as hq +input = sys.stdin.readline + +dx = [-1,1,0,0] +dy = [0,0,1,-1] + +def dijkstra(src): + visited = [[2e9 for _ in range(N)] for _ in range(M)] + h = [] + visited[src[0]][src[1]] = 0 + hq.heappush(h, [0, src[0], src[1]]) + + while h: + # cur[0]: cost, cur[1]: x, cur[2]: y + cur = hq.heappop(h) + if visited[cur[1]][cur[2]] < cur[0]: + continue + for i in range(4): + nx = cur[1] + dx[i] + ny = cur[2] + dy[i] + if 0<=nx next_weight: + visited[nx][ny] = next_weight + hq.heappush(h, [next_weight, nx, ny]) + return visited[M-1][N-1] + + +N, M = map(int,input().split()) + +ls = [input().rstrip() for _ in range(M)] + +print(dijkstra([0,0])) From ebcdf557ab1be71b028c38b3abd36220fc702012 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 14 Feb 2023 22:32:31 +0900 Subject: [PATCH 186/213] Create 14921.py --- BaekJoon/14921.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 BaekJoon/14921.py diff --git a/BaekJoon/14921.py b/BaekJoon/14921.py new file mode 100644 index 0000000..90a137b --- /dev/null +++ b/BaekJoon/14921.py @@ -0,0 +1,21 @@ +import sys +input = sys.stdin.readline + +N = int(input()) + +ls = sorted(list(map(int,input().split()))) + +l = 0 +r = len(ls)-1 +ans = [2e9 + 1, 0, 0] + +while l < r: + tmp = ls[l] + ls[r] + if abs(tmp) < abs(ans[0]): + ans = [tmp, ls[l], ls[r]] + if tmp < 0: + l += 1 + else: + r -= 1 + +print(ans[1]+ans[2]) From 853aa369b8419596887ad10be2ef8710292d0999 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 15 Feb 2023 18:25:40 +0900 Subject: [PATCH 187/213] Create 2011.py --- BaekJoon/2011.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 BaekJoon/2011.py diff --git a/BaekJoon/2011.py b/BaekJoon/2011.py new file mode 100644 index 0000000..752d96f --- /dev/null +++ b/BaekJoon/2011.py @@ -0,0 +1,23 @@ +import sys +input = sys.stdin.readline + +MOD = 1000000 + +cipher = input().rstrip() + +t = len(cipher) + +dp = [1] + [0 for _ in range(t)] + +if cipher[0] == '0': + print(0) +else: + cipher = '0' + cipher + for i in range(1, t+1): + if cipher[i] != '0': + dp[i] = (dp[i] + dp[i-1]) % MOD + if i >= 2: + t = cipher[i-1:i+1] + if 10<=int(t)<=26: + dp[i] = (dp[i] + dp[i-2]) % MOD + print(dp[-1]) From 8f00d9fc3a0e067c37ed8b738a71e38383842cfa Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Thu, 16 Feb 2023 22:39:26 +0900 Subject: [PATCH 188/213] Create 12904.py --- BaekJoon/12904.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BaekJoon/12904.py diff --git a/BaekJoon/12904.py b/BaekJoon/12904.py new file mode 100644 index 0000000..1d99151 --- /dev/null +++ b/BaekJoon/12904.py @@ -0,0 +1,20 @@ +import sys +input = sys.stdin.readline + +s = input().rstrip() +t = input().rstrip() + +ans = False + +while True: + if len(s) == len(t): + if s == t: + ans = True + break + if t[-1] == 'A': + t = t[:-1] + else: + t = t[:-1] + t = t[::-1] + +print(1 if ans else 0) From 704b66f90a96e11e0440a7915e2a96c751435a0a Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 17 Feb 2023 23:56:34 +0900 Subject: [PATCH 189/213] Create 2230.py --- BaekJoon/2230.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BaekJoon/2230.py diff --git a/BaekJoon/2230.py b/BaekJoon/2230.py new file mode 100644 index 0000000..61982e5 --- /dev/null +++ b/BaekJoon/2230.py @@ -0,0 +1,20 @@ +import sys +input = sys.stdin.readline + +N, M = map(int,input().split()) + +ls = sorted([int(input()) for _ in range(N)]) + +l = 0 +r = 0 +ans = 2e9 + 1 + +while l < N and r < N: + tmp = ls[r] - ls[l] + if tmp < M: + r += 1 + else: + l += 1 + ans = min(ans, tmp) + +print(ans) From fa08c97f1cba9b23bea83c4212950c83b0eb1b6b Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 18 Feb 2023 23:43:41 +0900 Subject: [PATCH 190/213] pypy3 --- BaekJoon/2589.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 BaekJoon/2589.py diff --git a/BaekJoon/2589.py b/BaekJoon/2589.py new file mode 100644 index 0000000..e1ebcaa --- /dev/null +++ b/BaekJoon/2589.py @@ -0,0 +1,40 @@ +import sys +from collections import deque +input = sys.stdin.readline + +dx = [ -1,1,0,0] +dy = [0,0,-1,1] + +N, M = map(int, input().split()) + +map_ = [input().rstrip() for _ in range(N)] + +def bfs(i, j): + t = 0 + visited = [[0 for _ in range(M)] for _ in range(N)] + dq = deque() + dq.append([0, i, j]) + visited[i][j] = 1 + while len(dq): + [cost, cx,cy] = dq.popleft() + if t < cost: + t = cost + for i in range(4): + nx = cx + dx[i] + ny = cy + dy[i] + if 0<= nx < N and 0<=ny t: + t = nCost + dq.append([nCost, nx, ny]) + return t + +ans = 0 + +for i in range(N): + for j in range(M): + if map_[i][j] == 'L': + ans = max(ans, bfs(i, j)) + +print(ans) From 37b127f6207bf658fd7b3399a6e959460ca236d8 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 19 Feb 2023 21:00:40 +0900 Subject: [PATCH 191/213] Create 9205.py --- BaekJoon/9205.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 BaekJoon/9205.py diff --git a/BaekJoon/9205.py b/BaekJoon/9205.py new file mode 100644 index 0000000..c15acae --- /dev/null +++ b/BaekJoon/9205.py @@ -0,0 +1,31 @@ +import sys +from collections import deque +input = sys.stdin.readline + +def dist(a,b,c,d): + return abs(a-b) + abs(c-d) <= 1000 + +def bfs(x, y): + dq = deque() + dq.append([x, y]) + while dq: + cx, cy = dq.popleft() + # 갈 수 있으면 + if dist(cx, rock[0], cy, rock[1]): + return True + for i in range(t): + if not visited[i]: + nx, ny = conv[i] + if dist(cx, nx, cy, ny): + dq.append([nx, ny]) + visited[i] = True + return False + +for _ in range(int(input())): + t = int(input()) + home = list(map(int,input().split())) + conv = [list(map(int, input().split())) for _ in range(t)] + visited = [False for _ in range(t)] + rock = list(map(int,input().split())) + + print("happy" if bfs(home[0], home[1]) else "sad") From 718b5edcf91eb99ed0aa81516123654b7f836b14 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 24 Feb 2023 13:28:05 +0900 Subject: [PATCH 192/213] Create 2631.cpp --- BaekJoon/2631.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 BaekJoon/2631.cpp diff --git a/BaekJoon/2631.cpp b/BaekJoon/2631.cpp new file mode 100644 index 0000000..0b05cd9 --- /dev/null +++ b/BaekJoon/2631.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main() { + int N, tmp, ans=1; + scanf("%d", &N); + vectorv; + vectordp(N + 1, 1); + for (int i = 0; i < N; ++i) { + scanf("%d", &tmp); + v.push_back(tmp); + } + for (int i = 1; i < N; ++i) { + for (int j = 0; j < i; ++j) { + if (v[i] > v[j] && dp[j] + 1 > dp[i]) { + dp[i] = dp[j] + 1; + ans = max(ans, dp[i]); + } + } + } + printf("%d\n", N - ans); + + return 0; +} From 5940ab72c3b3f876b328b757315a00b411b7ad89 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 24 Feb 2023 17:03:28 +0900 Subject: [PATCH 193/213] Create 1781.cpp --- BaekJoon/1781.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 BaekJoon/1781.cpp diff --git a/BaekJoon/1781.cpp b/BaekJoon/1781.cpp new file mode 100644 index 0000000..d41a9c3 --- /dev/null +++ b/BaekJoon/1781.cpp @@ -0,0 +1,50 @@ +// 받지 않는 경우를 생각하지 않아서 틀림! +#include +using namespace std; + +typedef long long ll; +typedef pair pii; + +struct compare{ + bool operator()(const pii a, const pii b){ + if(a.first > b.first){ + return true; + }else if (a.first == b.first) + { + return a.second < b.second; + } + return false; + } +}; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(false); + int N, ta, tb, today = 1; + ll res = 0; + cin>>N; + priority_queue, compare> pq; + priority_queue, greater >ans; + for(int i=0; i>ta>>tb; + pq.push(make_pair(ta, tb)); + } + while(!pq.empty()){ + pii t = pq.top(); pq.pop(); + if(t.first >= today){ + ans.push(t.second); + today += 1; + }else{ + if(t.second > ans.top()){ + ans.pop(); + ans.push(t.second); + } + } + } + while(!ans.empty()){ + res += ans.top(); + ans.pop(); + } + cout< Date: Sat, 25 Feb 2023 01:01:47 +0900 Subject: [PATCH 194/213] Create 1068_2.cpp --- BaekJoon/1068_2.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 BaekJoon/1068_2.cpp diff --git a/BaekJoon/1068_2.cpp b/BaekJoon/1068_2.cpp new file mode 100644 index 0000000..aeb89c2 --- /dev/null +++ b/BaekJoon/1068_2.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +int N; +vectorparent[50]; +vectorvisited; +int erase; +int _root; +int ans = 0; + +void dfs(int n) { + if (!parent[n].size()) { + ans += 1; return; + } + if (parent[n].size() == 1 && erase == parent[n][0]){ + ans += 1;return ; + } + for (int i = 0; i < parent[n].size(); ++i) { + int next = parent[n][i]; + if (!visited[next] && erase != next) { + visited[next] = true; + dfs(next); + } + } +} + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(false); + cin >> N; + int cnt = 0, t; + for (int i = 0; i < N; ++i) { + cin >>t; + if (t == -1) { + _root = i; + } + else { + parent[t].push_back(i); + } + + } + cin >> erase; + + if (erase != _root) { + visited.resize(N, false); + visited[_root] = true; + dfs(_root); + } + cout << ans; + return 0; +} From ad10da7880eea110bbcd3218e4afedc14a4a4b09 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 25 Feb 2023 01:38:43 +0900 Subject: [PATCH 195/213] Create 2636.cpp --- BaekJoon/2636.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 BaekJoon/2636.cpp diff --git a/BaekJoon/2636.cpp b/BaekJoon/2636.cpp new file mode 100644 index 0000000..528d50e --- /dev/null +++ b/BaekJoon/2636.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(false); + int N, M; + cin >> N >> M; + vector >v(N, vector(M)); + for (int i = 0; i < N; ++i) { + for (int j = 0; j < M; ++j) { + cin >> v[i][j]; + } + } + int ans = 0; + int num_cheese = 0; + // 공기와 닿아있는 부분 찾고 이건 네 모서리에서 BFS 진행하기 + // 닿아있는 부분 체크하고, + // 녹이기 + // 반복 + int dx[4] = {0, 0, N-1, N-1}; + int dy[4] = { 0, M - 1, 0, M - 1 }; + int ddx[4] = { 0,0,1,-1 }; + int ddy[4] = { 1,-1, 0,0 }; + while (true) { + bool flag = true; + for (int i = 0; i < N; ++i) { + for (int j = 0; j < M; ++j) { + if (v[i][j] == 1) flag = false; + } + } + if (flag) break; + num_cheese = 0; + ans += 1; + vector >visited(N, vector(M, 0)); + for (int i = 0; i < 4; ++i) { + if (visited[dx[i]][dy[i]] != 0) continue; + deque>dq; + dq.push_back({ dx[i], dy[i] }); + while (!dq.empty()) { + paircur = dq.front(); + dq.pop_front(); + for (int j = 0; j < 4; ++j) { + int nx = cur.first + ddx[j]; + int ny = cur.second + ddy[j]; + if (0 <= nx && nx < N && 0 <= ny && ny < M) { + if (visited[nx][ny] == 0) { + if (v[nx][ny] == 0) { + visited[nx][ny] = 1; + dq.push_back({ nx, ny }); + } + else { + visited[nx][ny] = 2; + } + } + } + } + } + } + for (int i = 0; i < N; ++i) { + for (int j = 0; j < M; ++j) { + if (visited[i][j] == 2) { + v[i][j] = 0; + num_cheese += 1; + } + } + } + } + cout << ans << '\n' << num_cheese<<'\n'; + + return 0; +} From 321f382ffb46f576117a5fbf184f184b311a62f3 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sat, 25 Feb 2023 12:42:20 +0900 Subject: [PATCH 196/213] Create 1717.cpp --- BaekJoon/1717.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 BaekJoon/1717.cpp diff --git a/BaekJoon/1717.cpp b/BaekJoon/1717.cpp new file mode 100644 index 0000000..64b245a --- /dev/null +++ b/BaekJoon/1717.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; + +vectorparent; + +int find(int x){ + if(parent[x] == x) return x; + return parent[x] = find(parent[x]); +} + +void _union(int x, int y){ + x = find(x); + y = find(y); + if(x > y){ + parent[x] = y; + }else{ + parent[y] = x; + } +} + +int main(){ + cin.tie(0); + ios_base::sync_with_stdio(false); + int n,m, cmd, ta, tb; + cin>>n>>m; + parent.resize(n + 1); + for(int i=0; i<=n; ++i){ + parent[i] = i; + } + for(int i=0; i>cmd>>ta>>tb; + if(cmd == 1){ + cout<<(find(ta) == find(tb) ? "YES" : " NO")<<'\n'; + }else{ + _union(ta, tb); + } + } + + return 0; +} From eb419e829011d6ace348305c7fb6b44b96eb8a0e Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 26 Feb 2023 00:35:25 +0900 Subject: [PATCH 197/213] Create 20040.py --- BaekJoon/20040.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 BaekJoon/20040.py diff --git a/BaekJoon/20040.py b/BaekJoon/20040.py new file mode 100644 index 0000000..6218aba --- /dev/null +++ b/BaekJoon/20040.py @@ -0,0 +1,30 @@ +import sys +sys.setrecursionlimit(10**6) +input = sys.stdin.readline + +def find(a): + if parent[a] == a: + return a + parent[a] = find(parent[a]) + return parent[a] + +def union(x, y): + a = find(x) + b = find(y) + if parent[a] < parent[b]: + parent[parent[a]] = parent[b] + else: + parent[parent[b]] = parent[a] + return a == b + +n, m = map(int,input().split()) + +parent = [i for i in range(n)] + +ans = 0 +for i in range(m): + s, d = map(int,input().split()) + if union(s, d) and ans == 0: + ans = i + 1 + +print(ans) From 062a3dd95fa69d2ac603d81b2ff7c7a78640828a Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:05:31 +0900 Subject: [PATCH 198/213] Create 16938.py --- BaekJoon/16938.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BaekJoon/16938.py diff --git a/BaekJoon/16938.py b/BaekJoon/16938.py new file mode 100644 index 0000000..186f4d0 --- /dev/null +++ b/BaekJoon/16938.py @@ -0,0 +1,20 @@ +import sys +input = sys.stdin.readline + +N,L,R,X = map(int,input().split()) +ls = list(map(int,input().split())) + +ans = 0 + +for i in range(3, (1<= X: + ans += 1 +print(ans) From d96b260e956c6e2d964ae8a6b803da4992a0d2c0 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 28 Feb 2023 14:17:06 +0900 Subject: [PATCH 199/213] =?UTF-8?q?=EC=9E=AC=EC=B1=84=EC=A0=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BaekJoon/3079.cpp | 60 ++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/BaekJoon/3079.cpp b/BaekJoon/3079.cpp index 9f4f848..32e6e25 100644 --- a/BaekJoon/3079.cpp +++ b/BaekJoon/3079.cpp @@ -1,32 +1,34 @@ -#include -#include +#include using namespace std; -typedef long long ll; - -int main(){ - ll n,m; - scanf("%lld%lld", &n, &m); - vectorv(n); - for(ll i=0;i> N >> M; + vectorjudge(N); + for (int i = 0; i < N; ++i) { + cin >> judge[i]; + } + sort(judge.begin(), judge.end()); + ll l = 1, r = judge[N-1] * M; + while (l <= r) { + ll mid = (l + r) / 2; + ll tmp = 0; + for (int i = 0; i < N; ++i) { + if (tmp > M) break; + tmp += (mid / judge[i]); + } + if (tmp < M) { + l = mid + 1; + } + else { + ans = mid; + r = mid - 1; + } + } + cout << ans << endl; + return 0; +} From 2e3c57c1db5c147847338e11c3f91f3cfb228751 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 1 Mar 2023 17:18:25 +0900 Subject: [PATCH 200/213] =?UTF-8?q?Create=20=ED=8D=BC=EC=A6=90=20=EC=A1=B0?= =?UTF-8?q?=EA=B0=81=20=EC=B1=84=EC=9A=B0=EA=B8=B0.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1 \354\261\204\354\232\260\352\270\260.py" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "programmers/\355\215\274\354\246\220 \354\241\260\352\260\201 \354\261\204\354\232\260\352\270\260.py" diff --git "a/programmers/\355\215\274\354\246\220 \354\241\260\352\260\201 \354\261\204\354\232\260\352\270\260.py" "b/programmers/\355\215\274\354\246\220 \354\241\260\352\260\201 \354\261\204\354\232\260\352\270\260.py" new file mode 100644 index 0000000..90f55d1 --- /dev/null +++ "b/programmers/\355\215\274\354\246\220 \354\241\260\352\260\201 \354\261\204\354\232\260\352\270\260.py" @@ -0,0 +1,83 @@ +from collections import deque + +dx = [0,0,1,-1] +dy = [1,-1,0,0] + +def rotate(arr, deg): + if deg == 0: + for i in range(len(arr)): + tmp = arr[i][0] + arr[i][0] = arr[i][1]*(-1) + arr[i][1] = tmp + elif deg == 1: + for i in range(len(arr)): + arr[i][0] *= (-1) + arr[i][1] *= (-1) + elif deg == 2: + for i in range(len(arr)): + tmp = arr[i][0] + arr[i][0] = arr[i][1] + arr[i][1] = tmp*(-1) + arr = to_std(arr) + return arr + +def to_std(arr): + arr = sorted(arr) + std = [arr[0][0],arr[0][1]] + for i in range(len(arr)): + arr[i][0] -= std[0] + arr[i][1] -= std[1] + return arr + +def find_blocks(arr, idx): + length = len(arr) + s = [] + visited = [[0 for _ in range(length)] for _ in range(length)] + for i in range(length): + for j in range(length): + if arr[i][j] == idx and visited[i][j] == 0: + a = [[i,j]] + t = deque() + t.append([i,j]) + visited[i][j] = 1 + while t: + cur = t.popleft() + cur_x = cur[0] + cur_y = cur[1] + for k in range(4): + nx = cur_x + dx[k] + ny = cur_y + dy[k] + if 0<=nx Date: Wed, 1 Mar 2023 17:47:31 +0900 Subject: [PATCH 201/213] =?UTF-8?q?Create=20=ED=94=BC=EB=A1=9C=EB=8F=84.cp?= =?UTF-8?q?p?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\355\224\274\353\241\234\353\217\204.cpp" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "programmers/\355\224\274\353\241\234\353\217\204.cpp" diff --git "a/programmers/\355\224\274\353\241\234\353\217\204.cpp" "b/programmers/\355\224\274\353\241\234\353\217\204.cpp" new file mode 100644 index 0000000..14315c5 --- /dev/null +++ "b/programmers/\355\224\274\353\241\234\353\217\204.cpp" @@ -0,0 +1,30 @@ +#include +#include +#include +#include +using namespace std; + +bool visited[8] = {false, }; +int c = 0; + +void dfs(int rem, vector> dungeons, int cnt){ + c = max(cnt, c); + for(int i=0; i= 0){ + visited[i] = true; + dfs(rem-dungeons[i][1], dungeons, cnt + 1); + visited[i] = false; + }else{ + continue; + } + } + } +} + +int solution(int k, vector> dungeons) { + dfs(k, dungeons, 0); + return c; +} From 883a7124c57719db96ea8a1db86421873f6249d5 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Wed, 1 Mar 2023 18:08:21 +0900 Subject: [PATCH 202/213] =?UTF-8?q?Create=20=EC=A0=84=EB=A0=A5=EB=A7=9D?= =?UTF-8?q?=EC=9D=84=20=EB=91=98=EB=A1=9C=20=EB=82=98=EB=88=84=EA=B8=B0.cp?= =?UTF-8?q?p?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... \353\202\230\353\210\204\352\270\260.cpp" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.cpp" diff --git "a/programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.cpp" "b/programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.cpp" new file mode 100644 index 0000000..0147544 --- /dev/null +++ "b/programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.cpp" @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace std; + +int solution(int n, vector> wires) { + int answer = 2e9; + for(int i=0; i>graph(n+1, vector()); + for(int j=0; jvisited(n+1, false); + int idx =0; + int cnt[2] = {1,1}; + for(int i=1; idq; + dq.push_back(i); + visited[i] = true; + while(!dq.empty()){ + int cur = dq.front(); + dq.pop_front(); + for(int j=0; j Date: Thu, 2 Mar 2023 17:31:43 +0900 Subject: [PATCH 203/213] =?UTF-8?q?Create=20=EA=B2=8C=EC=9E=84=20=EB=A7=B5?= =?UTF-8?q?=20=EC=B5=9C=EB=8B=A8=EA=B1=B0=EB=A6=AC.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\213\250\352\261\260\353\246\254.cpp" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "programmers/\352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254.cpp" diff --git "a/programmers/\352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254.cpp" "b/programmers/\352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254.cpp" new file mode 100644 index 0000000..e45c505 --- /dev/null +++ "b/programmers/\352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254.cpp" @@ -0,0 +1,36 @@ +#include +#include +using namespace std; + +typedef pair pii; + + +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; + +int solution(vector > maps) +{ + int answer = 0; + int w = maps.size(); + int h = maps[0].size(); + vector >visited(maps.size(), vector(maps[0].size())); + dequedq; + dq.push_back({0,0}); + visited[0][0] = 1; + while(!dq.empty()){ + pii cur = dq.front(); + dq.pop_front(); + int cur_x = cur.first; + int cur_y = cur.second; + for(int i=0; i<4; ++i){ + int nx = cur_x + dx[i]; + int ny = cur_y + dy[i]; + if(0<=nx && nx Date: Fri, 3 Mar 2023 00:43:41 +0900 Subject: [PATCH 204/213] =?UTF-8?q?Create=20=EB=8B=A8=EC=96=B4=20=EB=B3=80?= =?UTF-8?q?=ED=99=98.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\226\264 \353\263\200\355\231\230.py" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "programmers/\353\213\250\354\226\264 \353\263\200\355\231\230.py" diff --git "a/programmers/\353\213\250\354\226\264 \353\263\200\355\231\230.py" "b/programmers/\353\213\250\354\226\264 \353\263\200\355\231\230.py" new file mode 100644 index 0000000..dc24d43 --- /dev/null +++ "b/programmers/\353\213\250\354\226\264 \353\263\200\355\231\230.py" @@ -0,0 +1,30 @@ +from collections import deque + +answer = 2e9 + +def differenceBetween(a, b): + diff = 0 + for i in range(len(a)): + if a[i] != b[i]: + diff += 1 + return diff == 1 + +def dfs(cur, end, depth, visited, words): + global answer + length = len(words) + if cur == end: + answer = min(answer, depth) + return + if depth == length: + return + for i in range(length): + if differenceBetween(cur, words[i]) and not visited[i]: + visited[i] = True + dfs(words[i], end, depth + 1, visited, words) + visited[i] = False + return + +def solution(begin, target, words): + visited = [False for _ in range(len(words))] + dfs(begin, target, 0, visited, words) + return answer if answer != 2e9 else 0 From 561f16a614ae1388778a17ce720ef0ce9acfa4e6 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:42:56 +0900 Subject: [PATCH 205/213] =?UTF-8?q?Create=20=EB=93=B1=EA=B5=A3=EA=B8=B8.cp?= =?UTF-8?q?p?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\223\261\352\265\243\352\270\270.cpp" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "programmers/\353\223\261\352\265\243\352\270\270.cpp" diff --git "a/programmers/\353\223\261\352\265\243\352\270\270.cpp" "b/programmers/\353\223\261\352\265\243\352\270\270.cpp" new file mode 100644 index 0000000..59cc547 --- /dev/null +++ "b/programmers/\353\223\261\352\265\243\352\270\270.cpp" @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +using namespace std; + +typedef pair pii; + +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; + +int solution(int m, int n, vector> puddles) { + int answer = 0; + vector>visited(n, vector(m, 1)); + for(int i=0; i Date: Sat, 4 Mar 2023 20:24:33 +0900 Subject: [PATCH 206/213] =?UTF-8?q?Create=20=EC=97=AC=ED=96=89=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\355\226\211 \352\262\275\353\241\234.py" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "programmers/\354\227\254\355\226\211 \352\262\275\353\241\234.py" diff --git "a/programmers/\354\227\254\355\226\211 \352\262\275\353\241\234.py" "b/programmers/\354\227\254\355\226\211 \352\262\275\353\241\234.py" new file mode 100644 index 0000000..09376f6 --- /dev/null +++ "b/programmers/\354\227\254\355\226\211 \352\262\275\353\241\234.py" @@ -0,0 +1,21 @@ +import collections +graph = collections.defaultdict(list) +answer = [] + +def go(s): + while graph[s]: + go(graph[s].pop(0)) + + if not graph[s]: + answer.append(s) + return + +def solution(tickets): + for a,b in tickets: + graph[a].append(b) + for a, b in graph.items(): + graph[a].sort() + + go("ICN") + + return answer[::-1] From 40997c30c561e0bb40a4fc2124636bf6dc905aa5 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Sun, 5 Mar 2023 16:46:16 +0900 Subject: [PATCH 207/213] Create 24313.py --- BaekJoon/24313.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 BaekJoon/24313.py diff --git a/BaekJoon/24313.py b/BaekJoon/24313.py new file mode 100644 index 0000000..ffc3014 --- /dev/null +++ b/BaekJoon/24313.py @@ -0,0 +1,17 @@ +import sys +input = sys.stdin.readline + +a1, a0 = map(int,input().split()) +c = int(input()) +n0 = int(input()) +answer = -1 + +if c - a1 < 0: + for i in range(n0, 101): + answer = i < a0/(c-a1) +elif c - a1 == 0: + answer = a0 <= 0 +else: + answer = n0 >= a0/(c-a1) + +print(1 if answer else 0) From 295852d1d0defa83ccd0dec08c3e7019053b70db Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 6 Mar 2023 18:42:42 +0900 Subject: [PATCH 208/213] Update 13975.cpp --- BaekJoon/13975.cpp | 57 +++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/BaekJoon/13975.cpp b/BaekJoon/13975.cpp index ebc8e63..b4d4dbb 100644 --- a/BaekJoon/13975.cpp +++ b/BaekJoon/13975.cpp @@ -1,37 +1,28 @@ #include using namespace std; - typedef long long ll; +int main() { + cin.tie(0); + ios_base::sync_with_stdio(false); + int tc, t; + cin >> tc; + for (int i = 0; i < tc; ++i) { + priority_queue, greater>pq; + ll answer = 0, num; + cin >> num; + for (int j = 0; j < num; ++j) { + cin >> t; + pq.push(t); + } + while(pq.size() != 1) { + ll t1 = pq.top(); pq.pop(); + ll t2 = pq.top(); pq.pop(); + ll t3 = t1 + t2; + answer += t3; + pq.push(t3); -int main(){ - ll tc,sz,tmp; - cin>>tc; - for(int i=0;ipq; - cin>>sz; - for(int j=0;j>tmp; - pq.push(-tmp); - } - ll ans = 0; - ll prev = -pq.top(); - pq.pop(); - if(sz == 1){ - cout<1){ - prev = -pq.top(); - pq.pop(); - }else{ - break; - } - } - cout< Date: Tue, 7 Mar 2023 23:49:30 +0900 Subject: [PATCH 209/213] Create 14698.py --- BaekJoon/14698.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 BaekJoon/14698.py diff --git a/BaekJoon/14698.py b/BaekJoon/14698.py new file mode 100644 index 0000000..abd501c --- /dev/null +++ b/BaekJoon/14698.py @@ -0,0 +1,19 @@ +import sys +import heapq as hq +input = sys.stdin.readline + +MOD = 1000000007 + +tc = int(input()) +for _ in range(tc): + n = int(input()) + h = sorted(list(map(int,input().split()))) + answer = 1 + while len(h) > 1: + a = hq.heappop(h) + b = hq.heappop(h) + t = a*b + hq.heappush(h, t) + answer = (answer * t) % MOD + + print(answer) From 98a68e2dc88383a4f1d68521f673c3c01109d765 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Mon, 13 Mar 2023 23:23:15 +0900 Subject: [PATCH 210/213] Create 11497.py --- BaekJoon/11497.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 BaekJoon/11497.py diff --git a/BaekJoon/11497.py b/BaekJoon/11497.py new file mode 100644 index 0000000..24a7b65 --- /dev/null +++ b/BaekJoon/11497.py @@ -0,0 +1,7 @@ +for _ in range(int(input())): + n = int(input()) + ls = sorted(list(map(int,input().split()))) + res = 0 + for i in range(2,n): + res = max(ls[i] - ls[i - 2], res) + print(res) From 9e5e9f0aab3f1d0a5984803ea87f4b3d3e6865d8 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Tue, 14 Mar 2023 22:51:56 +0900 Subject: [PATCH 211/213] Create 18352.cpp --- BaekJoon/18352.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 BaekJoon/18352.cpp diff --git a/BaekJoon/18352.cpp b/BaekJoon/18352.cpp new file mode 100644 index 0000000..ac1a4ec --- /dev/null +++ b/BaekJoon/18352.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +typedef long long ll; + +int main() { + cin.tie(0); + ios_base::sync_with_stdio(false); + int N, M, K, X, ts, td; + cin >> N >> M >> K >> X; + vector> v(N + 1); + for (int i = 0; i < M; ++i) { + cin >> ts >> td; + v[ts].emplace_back(td); + } + priority_queue, greater>pq; + vectorvisited(N + 1, 2e9); + visited[X] = 0; + pq.push(X); + while (!pq.empty()) { + int src = pq.top(); + pq.pop(); + for (int i = 0; i < v[src].size(); ++i) { + int nxt = v[src][i]; + if (visited[nxt] > visited[src] + 1) { + visited[nxt] = visited[src] + 1; + pq.push(nxt); + } + } + } + bool flag = false; + for (int i = 0; i < N + 1; ++i) { + if ( visited[i] == K) { + cout << i << endl; + flag = true; + } + } + if (!flag) cout << -1 << endl; + return 0; +} From 11660e2eba68dc45d56174c1f70a63eae399c307 Mon Sep 17 00:00:00 2001 From: Kim HyoGeun <59858440+HiGeuni@users.noreply.github.com> Date: Fri, 31 Mar 2023 17:29:24 +0900 Subject: [PATCH 212/213] Create 1012.py --- BaekJoon/1012.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 BaekJoon/1012.py diff --git a/BaekJoon/1012.py b/BaekJoon/1012.py new file mode 100644 index 0000000..cbebc54 --- /dev/null +++ b/BaekJoon/1012.py @@ -0,0 +1,37 @@ +from collections import deque +import sys +sys.setrecursionlimit(10000) +input = sys.stdin.readline + +dx = [1,-1,0,0] +dy = [0,0,1,-1] + +def dfs(x, y): + global visited + for i in range(4): + nx = x+dx[i] + ny = y+dy[i] + if 0<=nx Date: Sun, 9 Apr 2023 21:30:12 +0900 Subject: [PATCH 213/213] Create 1062.cpp --- BaekJoon/1062.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 BaekJoon/1062.cpp diff --git a/BaekJoon/1062.cpp b/BaekJoon/1062.cpp new file mode 100644 index 0000000..e2095c2 --- /dev/null +++ b/BaekJoon/1062.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +const double PI = acos(-1); // PI 값을 지정해 놓음 +typedef complex cpx; + +void FFT(vector &f, cpx w){ + int n = f.size(); + if(n == 1) return; + + vector even(n/2), odd(n/2); + for(int i = 0; i < n; ++i) + (i%2 ? odd : even)[i/2] = f[i]; + + FFT(even, w*w); + FFT(odd, w*w); + + cpx wp(1, 0); + for(int i = 0; i < n/2; ++i){ + f[i] = even[i] + wp*odd[i]; + f[i + n/2] = even[i] - wp*odd[i]; + wp *= w; + } +} + +vector multiply(vector a, vector b){ + int n = 1; + while(n < a.size()+1 || n < b.size()+1) n *= 2; + n *= 2; + a.resize(n); + b.resize(n); + vector c(n); + + cpx w(cos(2*PI/n), sin(2*PI/n)); // k == 1일 때는 k번 n번 곱해져야 1이므로 + + FFT(a, w); + FFT(b, w); + + for(int i = 0; i < n; ++i) + c[i] = a[i]*b[i]; + + FFT(c, cpx(1, 0)/w); + for(int i = 0; i < n; ++i){ + c[i] /= cpx(n, 0); + c[i] = cpx(round(c[i].real()), round(c[i].imag())); + } + return c; +} + +int main(){ + cin.tie(NULL); + ios_base::sync_with_stdio(false); + int n; + cin>>n; + vectora(2*n), b(2*n, 0); + for(int i=0; i>a[i]; + a[i+n] = a[i]; + } + for(int i=1; i<=n; ++i){ + cin>>b[n-i]; + } + vector ans = multiply(a, b); + double p =0; + for(int i=0; i