Codeforces Round #501 (Div. 3) // Rated
### Codeforces Round #501 (Div. 3) ###
### Rated : 1275 >> 1380 ###
< Solved > : 4 out of 7 ( A, B, C, E1)
A. Points in Segments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <cstdio> int main() { int n, m; scanf("%d%d", &n, &m); int seg[101]={0}; while(n--) { int l, r; scanf("%d%d", &l, &r); for(int i=l; i<=r; i++) seg[i] = 1; } int ans=0; for(int i=1; i<=m; i++) if(!seg[i]) ans++; printf("%d\n", ans); for(int i=1; i<=m; i++) if(!seg[i]) printf("%d ", i); puts(""); return 0; } | cs |
B. Obtaining the String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <cstdio> #include <vector> using namespace std; int main() { int n; scanf("%d", &n); char s[55]; char t[55]; scanf("%s%s", s, t); int check[30]={0}; for(int i=0; s[i]; i++) { check[s[i]-'a']++; check[t[i]-'a']--; } for(int i=0; i<26; i++) { if(check[i]) { puts("-1"); return 0; } } vector<int> ans; for(int i=0; i<n; i++) { while(s[i] != t[i]) { for(int k=i; k<n; k++) { if(s[k] == t[i]) { char tmp = s[k-1]; s[k-1] = s[k]; s[k] = tmp; ans.push_back(k); break; } } } } int size = ans.size(); if(size > 10000) puts("-1"); else { printf("%d\n", size); for(int i=0; i<size; i++) printf("%d ", ans[i]); puts(""); } return 0; } | cs |
C. Songs Compression
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; struct P { int b, s; }; typedef long long int lli; bool cmp(P a, P b) { return (a.b-a.s) > (b.b-b.s); } int main() { int n, m; scanf("%d%d", &n, &m); vector<P> v; lli sum=0LL; for(int i=0; i<n; i++) { int b, s; scanf("%d%d", &b, &s); v.push_back((P){b, s}); sum += b; } sort(v.begin(), v.end(), cmp); int ans=0; for(int i=0; i<n; i++) { if(sum > m) { ans++; sum = sum - (v[i].b-v[i].s); } else break; } if(ans == n && sum > m) ans = -1; printf("%d\n", ans); return 0; } | cs |
E!. Stars Drawing (Easy Edition)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <cstdio> #include <vector> using namespace std; struct P { int r, c, n; }; int n, m; char g[1005][1005]; bool check[1005][1005]; int di[]={1, 0, -1, 0}; int dj[]={0, 1, 0, -1}; bool safe(int r, int c) { return (0 < r && r <= n) && (0 < c && c <= m); } int main() { scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) scanf("%s", g[i]+1); vector<P> ans; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(g[i][j] == '*') { int size = 1; while(1) { bool ok = 1; for(int k=0; k<4; k++) { int ni = i+di[k]*size; int nj = j+dj[k]*size; if(!safe(ni, nj) || g[ni][nj] != '*') { ok = 0; break; } } if(ok) { for(int k=0; k<4; k++) { int ni = i+di[k]*size; int nj = j+dj[k]*size; check[ni][nj] = 1; } check[i][j] = 1; size++; } else { if(size > 1) { ans.push_back((P){i, j, size-1}); } break; } } } } } for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if((check[i][j] == 1 && g[i][j] == '.') || (check[i][j] == 0 && g[i][j] == '*')) { puts("-1"); return 0; } } } int sz = ans.size(); printf("%d\n", sz); for(int i=0; i<sz; i++) printf("%d %d %d\n", ans[i].r, ans[i].c, ans[i].n); return 0; } | cs |
>> '*' 을 찾으면 이 좌표 기준 상하좌우에 '*' 가 몇바퀴 나오는지, 이런 방식으로 구현했다.
>> 시간복잡도는 O(n^3) 정도 되는것 같다.
-------------
D 는 웬지 시간만 조금 더 있었으면 할 수 있었을 거 같았는데, 핵당한 유저들이 많았다. E1 먼저 풀길 잘한듯..
E2 에서는 E1 코드를 그대로 제출했더니, pretest 43 에서 TLE...
F 얼필 봤을때, 할만해 보였는데, 시간 없었다...ㅜㅜ
### Tutorial : http://codeforces.com/blog/entry/60949 ###
< Solution >
A.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | vector<int> cnt(m + 2); for (int i = 0; i < n; ++i) { int l, r; cin >> l >> r; ++cnt[l]; --cnt[r + 1]; } for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; vector<int> ans; for (int i = 1; i <= m; ++i) { if (cnt[i] == 0) ans.push_back(i); } | cs |
>> O(n+m) 풀이.
>> 쉽게 생각하면, L 번째에 1을 증가시키고, R+1 번째에 -1을 한다.
>> 그러고 난 다음 L 부터 R+1 까지의 pSum 을 구하면 [L, R] 까지는 1이되고, R+1 번째는 0이 되서 원하는 답을 구할 수 있게 된다.
B.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | vector<int> ans; for (int i = 0; i < n; ++i) { if (s[i] == t[i]) continue; int pos = -1; for (int j = i + 1; j < n; ++j) { if (s[j] == t[i]) { pos = j; break; } } if (pos == -1) { cout << -1 << endl; return 0; } for (int j = pos - 1; j >= i; --j) { swap(s[j], s[j + 1]); ans.push_back(j); } } | cs |
>> O(n^2)
>> 솔루션은 그위치를 찾은다음 i번째까지 거꾸로 스왑해나가는 풀이.
>> 반면에, 내 풀이는 한번스왑하면 다시 그곳을 찾아서 반복하는 것으로 좀 비효율적인 풀이다. (구현이 어렵지 않고, 시간이 시간인지라 이런식으로 해버렸다....ㅜㅜ)
C.
>> O(blogs) because of sort.
>> 솔루션과 풀이거 거의 비슷하다.
E1.
>> Um...
'PS > Code Force' 카테고리의 다른 글
Codeforces Round #515 (Div. 3) (0) | 2018.10.16 |
---|---|
Codeforces Round #506 (Div. 3) // rated (0) | 2018.08.30 |
Codeforces Round #496 (Div. 3) (0) | 2018.08.12 |
Codeforces Round #498 (Div. 3) (0) | 2018.08.12 |
[1162] 도로포장
### Dijkstra ###
[1162] 도로포장 : http://boj.kr/1162
<소스코드>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <cstdio> #include <vector> #include <queue> using namespace std; const int INF = 1e9; typedef pair<int, int> P; typedef pair<int, P> K; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<P> g[10001]; while(m--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); g[a].push_back(P(b, c)); g[b].push_back(P(a, c)); } vector<vector<int> > dist(k+1, vector<int>(n+1, INF)); dist[k][1] = 0; // kth pave, cur vertex priority_queue<K> pq; pq.push(K(0, P(1, k))); // dist, v, k bool visited[21][10001]={0}; while(!pq.empty()) { int curV, curK; do { curV = pq.top().second.first; curK = pq.top().second.second; pq.pop(); } while(!pq.empty() && visited[curK][curV]); if(visited[curK][curV]) break; visited[curK][curV] = 1; for(auto &p : g[curV]) { int nextV = p.first; int d = p.second; if(dist[curK][nextV] > dist[curK][curV] + d) { dist[curK][nextV] = dist[curK][curV] + d; pq.push(K(-dist[curK][nextV], P(nextV, curK))); } if(curK-1 >= 0 && dist[curK-1][nextV] > dist[curK][curV]) { dist[curK-1][nextV] = dist[curK][curV]; pq.push(K(-dist[curK-1][nextV], P(nextV, curK-1))); } } } int ans = 1e9; for(int i=0; i<=k; i++) { ans = min(ans, dist[i][n]); } printf("%d\n", ans); return 0; } | cs |
>> dist[k][i] : 포장을 k개 할 수 있는 상태에서 시작정점에서 i번 정점까지의 최단거리,
k값의 변화가 없는 겨우, 우리가 지금까지 구했던 다익스트라 dist 배열이다.
'BOJ > Shortest Path' 카테고리의 다른 글
[5917] 거의 최단경로 (0) | 2018.07.26 |
---|---|
[13424] 비밀 모임 (0) | 2018.07.24 |
[10282] 해킹 (0) | 2018.07.24 |
[5917] 거의 최단경로
### Dijkstra ###
[5917] 거의 최단경로 : http://boj.kr/5917
<소스코드>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <cstdio> #include <vector> #include <queue> using namespace std; int n, m, s, e; const int INF=1e9; typedef pair<int, int> P; vector<int> from[501]; void remove(vector<int> *from, int path[][501], int next) { if(next == s) return ; for(int i=0; i<from[next].size(); i++) { int cur = from[next][i]; path[cur][next] = INF; remove(from, path, cur); } } int dijkstra(vector<P> *g, int path[][501]) { priority_queue<P> pq; pq.push(P(0, s)); vector<int> dist(n+1, INF); dist[s] = 0; vector<bool> visited(n+1, 0); while(!pq.empty()) { int cur; do { cur = pq.top().second; pq.pop(); } while(!pq.empty() && visited[cur]); if(visited[cur]) break; visited[cur] = 1; for(auto &p : g[cur]) { int next = p.first; int d = path[cur][next]; if(dist[cur] + d == dist[next]) { from[next].push_back(cur); } else if(dist[next] > dist[cur] + d) { dist[next] = dist[cur] + d; pq.push(P(-dist[next], next)); from[next].clear(); from[next].push_back(cur); } } } return dist[e]; } int main() { while(1) { scanf("%d%d", &n, &m); if(n == 0 && m == 0) break; scanf("%d%d", &s, &e); vector<P> g[501]; int path[501][501]={0}; while(m--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); g[a].push_back(P(b, c)); path[a][b] = c; } for(int i=0; i<n; i++) from[i].clear(); int ans = dijkstra(g, path); if(ans == INF) { puts("-1"); continue; } remove(from, path, e); ans = dijkstra(g, path); if(ans == INF) ans = -1; printf("%d\n", ans); } return 0; } | cs |
>> 푸는데 참 오래 걸렸다.
>> 결론적으로 다익스트라 두번 돌렸으며, 처음 돌릴때, from 벡터배열에 한 정점까지의 최단경로에 대한 이전 정점들에 대해 기록을 해두고,
>> remove 로 삭제. 그리고 다시 다익스트라를 한번 돌리면 된다.
>> 주의할 것이 두가지 있었다. 처음 다익스트라를 돌렸을때 처음부터 갈수 없는 경로가 존재하는 경우에 대해 처리하는 것과
>> 다음으론 k 번 정점까지의 최단거리로 오는 방법이 한가지 가 아닌경우, 그 모든 경우에 대한 간선을 제거하는 것.!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 7 9 0 6 0 1 1 0 2 1 0 3 2 0 4 3 1 5 2 2 6 4 3 6 2 4 6 4 5 6 1 4 6 0 2 0 1 1 1 2 1 1 3 1 3 2 1 2 0 3 3 0 2 6 8 0 1 0 1 1 0 2 2 0 3 3 2 5 3 3 4 2 4 1 1 5 1 1 3 0 1 5 3 0 4 0 1 1 0 2 1 0 3 1 5 6 0 4 0 1 10 0 2 1 2 1 2 1 3 1 3 4 2 1 4 5 6 8 0 5 0 1 1 1 2 1 2 3 1 3 4 1 4 5 1 0 2 2 2 5 10 0 5 5 0 0 | cs |
위 테스트 케이스에 대해 아래와 같이 정답이 나와야 한다.
마지막 테스트 케이스가 12가 나오면 여러가지 길이 있을 때, 한가지 길만 제거해서 생긴거라고 생각하면 된다.
5
-1
6
-1
15
-1
'BOJ > Shortest Path' 카테고리의 다른 글
[1162] 도로포장 (0) | 2018.07.28 |
---|---|
[13424] 비밀 모임 (0) | 2018.07.24 |
[10282] 해킹 (0) | 2018.07.24 |