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 +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 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_) 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 +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; +} 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< +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; +} 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< +#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 +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< +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< +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< 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]) 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))) 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) + + 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< +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; +} 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; +} 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; +} 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])) 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) 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])) 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)) 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; +} 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 +#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< +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 +#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 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 +#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 +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/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< 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<= 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() 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 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 +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< 1: + a = hq.heappop(h) + b = hq.heappop(h) + t = a*b + hq.heappush(h, t) + answer = (answer * t) % MOD + + print(answer) 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; +} 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; +} 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]) 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) 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= 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 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 diff --git a/BaekJoon/15657.py b/BaekJoon/15657.py new file mode 100644 index 0000000..458bd7f --- /dev/null +++ b/BaekJoon/15657.py @@ -0,0 +1,10 @@ +import sys +from itertools import combinations_with_replacement +input = sys.stdin.readline + +n,m = map(int,input().split()) +ls = sorted(list(map(int,input().split()))) +for it in combinations_with_replacement(ls,m): + for i in it: + print(i, end = ' ') + print() \ No newline at end of file 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() 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< +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< +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 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 +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< +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; +} 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) 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) 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])) 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; +} 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; +} 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) 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) 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< +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<=mid: + cnt +=1 + t = 0 + if cnt >= k: + l = mid + 1 + else: + r = mid -1 + +print(r) 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< +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; +} 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()) 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; } diff --git a/BaekJoon/1918.py b/BaekJoon/1918.py new file mode 100644 index 0000000..85e5692 --- /dev/null +++ b/BaekJoon/1918.py @@ -0,0 +1,19 @@ +import sys +input = sys.stdin.readline + +s = list(input().rstrip()) +st = [] +flag = 0 +operation = ['+','-','*','/','(',')'] + +# 1. 우선순위 파악하기 +for i in range(len(s)): + if s[i] not in operation: + print(s[i],end = '') + else: + if s[i] == '(': + st.append(s[i]) + elif s[i] == ')': + flag -= 1 + + 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; +} 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; +} 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< +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; +} 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<= 2: + t = cipher[i-1:i+1] + if 10<=int(t)<=26: + dp[i] = (dp[i] + dp[i-2]) % MOD + print(dp[-1]) 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< +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; +} 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< +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< +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; +} 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]) 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) 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 +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; +} 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)) 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; +} 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 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 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) 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; +} 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) 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]) 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< 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) 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 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)) 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<= a0/(c-a1) + +print(1 if answer else 0) 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) 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]) 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) 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 diff --git a/BaekJoon/2517_mergeSort.cpp b/BaekJoon/2517_mergeSort.cpp new file mode 100644 index 0000000..1dbb884 --- /dev/null +++ b/BaekJoon/2517_mergeSort.cpp @@ -0,0 +1,123 @@ +#include +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 +using namespace std; + +int main(){ + float a, b; + cin>>a>>b; + cout<<((a - (a * b / 100) >= 100)? 0 : 1); + return 0; +} 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)< 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) 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 +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; +} 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)) 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<= 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)) 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< 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) 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< +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; +} 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; +} 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; +} 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 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< +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< -#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; +} 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 : "< +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 visited[cur[0]][cur[1]] + 1): + dq.append([nx,ny]) + visited[nx][ny] = visited[cur[0]][cur[1]]+1 + else: + break 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; +} 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") 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 end : + return + if start == end: + tree[node] = val + return tree[node] + mid = (start+end) //2 + update(2*node, start, mid, idx, val) + update(2 * node + 1, mid + 1, end, idx, val) + tree[node] = tree[2*node] * tree[2*node+1] % mod + return tree[node] + +def proVal(node, start, end, src, dst): + if src > end or dst < start: + return 1 + if src <= start and end <= dst: + return tree[node] + mid = (start + end)//2 + return proVal(2*node, start, mid, src, dst) * proVal(2*node+1, mid+1, end, src, dst) % mod + +n,m, q = map(int,input().split()) +ls = [int(input()) for _ in range(n)] + +height = int(math.ceil(math.log2(n))) +size = (1 << (height + 1)) + +tree = [ 0 for _ in range(size+10)] + +proInit(1, 0, n-1) + +for _ in range(m+q): + a,b, c = map(int,input().split()) + if a == 1: + update(1, 0, n-1, b-1, c) + else: + print(proVal(1, 0, n-1, b-1, c-1)) 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 +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<<"#"< +#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 << " "< +#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 << " "<> 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<<"#"< +#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 +#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/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 +#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/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 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 +#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 +#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> 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< +#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 +#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 +#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 diff --git a/SWEA/3282_knapsack.cpp b/SWEA/3282_knapsack.cpp new file mode 100644 index 0000000..51f11c3 --- /dev/null +++ b/SWEA/3282_knapsack.cpp @@ -0,0 +1,81 @@ +///////////////////////////////////////////////////////////////////////////////////////////// +// 기본 제공코드는 임의 수정해도 관계 없습니다. 단, 입출력 포맷 주의 +// 아래 표준 입출력 예제 필요시 참고하세요. +// 표준 입력 예제 +// 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 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 +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< +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< +#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 +#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 -#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 +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<> 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> 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< +#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<<"#"< ls[i][0]: + s += ls[i][1] + else: + flag = Fgitalse + break +print("YES" if flag else "NO") 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") 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 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") 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) 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_) 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) 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 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])) 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") 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) 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) 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 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)) 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 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 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 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 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; +} 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 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 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< 이진수로 변환 +# + +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])) 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 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 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 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 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 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 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] 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 0: + answer += 1 + return answer 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 +#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; +} 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