Division 1ã«åå ãã¦ã247/700ç¹ã 100ä½å°ã
ä»å¹´åãã¦éå¬ã§æ¥å¹´ããããããããªãã®ã§ãè²ã ã¨æ¸ãã¦ããã 端çã«è¨ãã¨ãå®è£ ãéãã¦ã¤ããã
Quora Programming Challenge
Quoraã¯æè¿è¯ãç®ã«ããããã«ãªã£ãQ&Aãµã¤ãã Stack Overflowã«æ¯ã¹ã¦åçãå°ãªãããã®ååçã«æ°åããå ¥ã£ã¦ããããã«æãã
ããããã¼ã¸ã«ãªã³ã¯ãè²¼ããã¨æã£ããâ¦â¦ããããã¼ã¸ã¯ãã°ã¤ã³ãã©ã¼ã ãåºã¦ããã ããªã®ãã ãããªæãã®ãµã¤ãã
CEOã競ããã¬ãå¢ãããã
CEOã競ããã¬ãå¢ãªã®ã«ããªãã競ããã³ã³ãã¹ããéãã¦ãªãã£ãQuoraããã¤ãã«ã³ã³ãã¹ããéãã¨ã¯â¦https://t.co/n7A6vAPnZJ
— nico_shindanninï¼è¨ºæäººï¼ (@nico_shindannin) January 14, 2021
Google Code Jamã®ããã«è¤æ°ã©ã¦ã³ãã«åããã¦ãããã¯ããªãä¸çºåè² ã ãã ããã¿ã¤ã ã¾ã¼ã³ãèæ ®ãã¦ãDivision 1ã¨Division 2ã«åããã¦ããã ã©ã¡ããã«ä¸æ¹ã«åºã¦ãè¯ããã両æ¹ã«åºã¦ãè¯ãã ã©ã¡ãã10以å ãªãè³éã50以å ãªãTã·ã£ãã
ã¿ã¤ã ã¾ã¼ã³ã«é æ ®ãã¦ããã¨è¨ã£ã¦ãããã©ãããæ¥æ¬ãã¨ã¦ãä¸å©ãããªãï¼ Division 1ã¯æ¥æ¬æéã®23æããç¿æ¥3æãDivision 2ãåæ¥ã®10æãã14æã 両æ¹åºãã°ãã®åæå©ã æ®éã«1åã ãã®éå¬ãªãã°ç¡ç æéãé©å½ã«ãºã©ããã3æãã10æã®éã«ãã¿ãªã¨ç¡ç æéãæã£ã¦ããã¯ã¤ããã ã¨ãããã¨ã§Division 2ã¯ã¹ã«ã¼ããã
åé¡ã¯Divisionãã¨ã«å ¨7åã§å ¨ã¦100ç¹ã ç°¡åã§ãé£ããã¦ãç¹æ°ãåããªã®ã§ãé ä½è¡¨ã®è§£ãã¦ãã人ã®äººæ°ããç°¡åãªåé¡ãè¦æ¥µããå¿ è¦ãããã ã§ããã³ã³ãã¹ããµã¤ãã«é ä½è¡¨ãç¡ãã 1æéãã¨ãããã«ããã50ä½ã¾ã§ã®é ä½è¡¨ãè²¼ãåºãããã
誤çããã«ãã£ã¯ç¡ãã æåºæ°ã¨ééã«ã¡ãã£ã¨å¶éã¯ãããæ°ã«ããã»ã©ã§ã¯ãªãã
å¶ç´ã®å°ããªãã¹ãã±ã¼ã¹ãéããã¨ã«ããé¨åç¹ãå¤ãã ä»ã¨ãªã£ã¦ã¯ãã®æ¹å¼ãçããã
ç§ã®ã³ã³ãã¹ãä¸ã®æ§åã¨åç
1åç®: digest
Quoraã®è¨äºæ¨è¦ã·ã¹ãã ãé¡æã ä¼æ¥ã³ã³ãã¹ãã§ããããåé¡è¯ããã
ã¦ã¼ã¶ã¼ãã¦ã¼ã¶ã¼ããã©ãã¼ãããã¨ãã§ããããã¦ã¼ã¶ã¼ãè¨äºããã©ãã¼ãããã¨ãã§ããã è¨äºã«ã¯è¨äºãä½æããã¦ã¼ã¶ã¼ãåå¨ããã ãããã®é¢ä¿ãããè¨äºã®ã¦ã¼ã¶ã¼ã«å¯¾ããã¹ã³ã¢ã¯ã
ã¨è¨ç®ãããã é¢æ°ã¨ã®å¤ã¯ã¦ã¼ã¶ã¼å士ãã¦ã¼ã¶ã¼ã®è¨äºã¨ã®ãã©ãã¼ãã¦ãããã©ããã¨ãã®é¢ä¿ãã決ã¾ãã ããã§åã¦ã¼ã¶ã¼ã«ã¤ãã¦ã¹ã³ã¢ä¸ä½3件ã®è¨äºãæ¨è¦ããã¨ããåé¡ã
ã¦ã¼ã¶ã¼æ°ã¨è¨äºæ°ãã©ã¡ããæ大200ãå¶éæéã0.5ç§ã éãããªï¼ãéãããã ã¨ã®å¤ãäºåã«è¨ç®ãã¦ããã
å®è£ ãé¢åã§ãã°ã£ã¦æéã溶ããã è¨ç®ãè¤éã ãããµã³ãã«ã±ã¼ã¹ã¨è¦æ¯ã¹ãã®å¤§å¤ã ä½ã¨ãä¿®æ£ãã¦éã£ãã
#include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; int main() { int n, m; cin>>n>>m; vector<int> creator(n); for (int &c: creator) cin>>c, c--; int p, q; cin>>p>>q; vector<vector<bool>> FU(m, vector<bool>(m)); for (int x=0; x<p; x++) { int i, j; cin>>i>>j; FU[i-1][j-1] = true; } vector<vector<bool>> FS(m, vector<bool>(n)); for (int x=0; x<q; x++) { int i, k; cin>>i>>k; FS[i-1][k-1] = true; } vector<vector<int>> a(m, vector<int>(m, -1)); for (int i=0; i<m; i++) a[i][i] = 0; for (int i=0; i<m; i++) for (int j=0; j<m; j++) if (FU[i][j] && a[i][j]==-1) a[i][j] = 3; for (int i=0; i<m; i++) for (int j=0; j<n; j++) if (FS[i][j] && a[i][creator[j]]==-1) a[i][creator[j]] = 2; for (int i=0; i<m; i++) for (int j=0; j<m; j++) for (int k=0; k<n; k++) if (FS[i][k] && FS[j][k] && a[i][j]==-1) a[i][j] = 1; for (int i=0; i<m; i++) for (int j=0; j<m; j++) if (a[i][j]==-1) a[i][j] = 0; vector<vector<int>> b(m, vector<int>(n)); for (int i=0; i<m; i++) for (int j=0; j<n; j++) if (creator[j]==i) b[i][j] = 2; else if (FS[i][j]) b[i][j] = 1; for (int i=0; i<m; i++) { vector<pair<int, int>> S; for (int j=0; j<n; j++) { int s = 0; if (creator[j]==i || FS[i][j]) s = -1; else for (int k=0; k<m; k++) s += a[i][k]*b[k][j]; S.push_back(make_pair(-s, j)); } sort(S.begin(), S.end()); for (int j=0; j<3; j++) cout<<(j==0 ? "" : " ")<<S[j].second+1; cout<<endl; } }
2åç®: escape
ã°ãªããä¸ã®è¿·è·¯ã«éãè¾¼ãããã¦ãã¾ã£ãã®ã§ãS
ããE
ã¾ã§ç§»åãã¦è±åºãããã
ä½ä½ãã®ã¬ã¼ãããã¦ãåã¬ã¼ãã¯ï¼ãã³ããã¿ã³è·é¢ã§ï¼ã®ç¯å²ã®å¥½ããªå ´æã«ç§»åã§ããã
æã¾ããªãããã«è±åºããæå°ã¹ãããã¯ï¼
ã¬ã¼ãã®ç§»åå¯è½ãªç¯å²ãå£ã«ãã¦ãã¾ãã°è¯ãã åã¬ã¼ãã«ã¤ãã¦æ±ãã¦ããã¨éã«åããªãã®ã§ãåæã«æ±ããã 移åç¯å²ã®åºãã¬ã¼ãã¯å ã«ãçãã¬ã¼ãã¯å¾ã«æéå·®ã§ç½®ãã¦ããã1ãã¹ãã¤åºãã¦ããã¤ã¡ã¼ã¸ã
ããã¯ç´ ç´ã«éã£ãã
#include <iostream> #include <vector> #include <string> #include <utility> using namespace std; int main() { int n, m, k; cin>>n>>m>>k; vector<string> M(n); for (string &s: M) cin>>s; vector<vector<pair<int, int>>> G(n*m+1); for (int i=0; i<k; i++) { int r, c, d; cin>>r>>c>>d; G[d].push_back(make_pair(r-1, c-1)); } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; vector<pair<int, int>> T; for (int s=0; s<=n*m; s++) { vector<pair<int, int>> P; P.swap(T); for (auto p: P) for (int d=0; d<4; d++) { int tx = p.second + dx[d]; int ty = p.first + dy[d]; if (M[ty][tx]!='#') { M[ty][tx] = '#'; T.push_back(make_pair(ty, tx)); } } for (auto p: G[n*m-s]) if (M[p.first][p.second]!='#') { M[p.first][p.second] = '#'; T.push_back(p); } } int sx = -1; int sy = -1; int ex = -1; int ey = -1; for (int y=0; y<n; y++) for (int x=0; x<m; x++) { if (M[y][x]=='S') sx = x, sy = y; if (M[y][x]=='E') ex = x, ey = y; } if (sx==-1 || ex==-1) { cout<<"IMPOSSIBLE"<<endl; return 0; } vector<vector<int>> D(n, vector<int>(m, -1)); D[sy][sx] = 0; T.clear(); T.push_back(make_pair(sy, sx)); for (int s=1; s<=n*m; s++) { vector<pair<int, int>> P; P.swap(T); for (auto p: P) for (int d=0; d<4; d++) { int tx = p.second + dx[d]; int ty = p.first + dy[d]; if (M[ty][tx]!='#' && D[ty][tx]==-1) { D[ty][tx] = s; T.push_back(make_pair(ty, tx)); } } } if (D[ey][ex]==-1) { cout<<"IMPOSSIBLE"<<endl; return 0; } cout<<D[ey][ex]<<endl; }
3åç®: students
ï¼ï¼ã®ã°ãªããã«çå¾ãæ·ãè©°ãããã¦ããã 4è¿åé£æ¥ã®ãã¡ï¼ï¼çµã¯ãããã¹ãããã¦ããã ãããã¹ããã¦ããçå¾å士ãå«ã¾ãªãããã«çå¾ã¯æ大ä½äººé¸ã¹ãï¼
äºé¨ã°ã©ãã®æ大ç¬ç«éåã
ã©ããã£ã¦æ±ãããã ã£ãï¼ãç´æçã«ã¯æ大ãããã³ã°ãæ±ãã¦ä½ã¨ãããã®ã ãããâ¦â¦ã ã°ã°ãã
ãããªãèãã¦ãåããããããªããªã ã°ã°ã£ã¦è¯ãã£ãã ããã¡ãããããããã¨ãã
æã£ã¦ããã©ã¤ãã©ãªã¼ã¯ãããã³ã°æ°ããè¿ããªãã£ãã®ã§ãããããä¿®æ£ã ã©ã¤ãã©ãªã²ã¼æ¢ãã¦ããã ã¾ããä¿®æ£ãå¿ è¦ã«ãªãã¨ãACLã§ã¯ãªãèªåã§ã©ã¤ãã©ãªãæ¸ãã¦ããã®ãçãã¦ããã èªåã§æ¸ããã³ã¼ãã®ã»ããä¿®æ£ãããããã
æåºããã¨47/100ã Memory Limit Exceededã§è½ã¡ã¦ããã å¶éã¯256 MiBãããã
ãããã«ãã£ããã®ãªã®ãªãâ¦â¦ã åãsigned charã«å¤ãã¦ããã¡ã ã®ä¸éãã¡ãã£ã¨å°ããã®ã§ããããã¹ããã¦ããªãçå¾ãé¤å¤ãã¦ããããã¹ããã¦ããçå¾ã ãã§ã°ã©ããä½ã£ã¦ã¿ãã ããããéæ¬è³ªçãªã¨ããã§æéãæããããã®ã¯æ¢ãã¦ããï½
ä»åº¦ã¯Time Limit Exceededã æ大æµã®ã¢ã«ã´ãªãºã ãDinicã«è²¼ãæ¿ãã¦ã¿ããããã°ã£ãã 諦ãã
ä»æ°ãä»ãããã©ããããcin
ãæ¢ããã°éã£ããâ¦â¦ï¼
#include <vector> #include <algorithm> #include <functional> using namespace std; /* void maxflow(vector<vector<int>> E_, vector<vector<int>> &F_, int s, int t) { int n = (int)E_.size(); vector<vector<int>> E(n), R(n), Forg(n); vector<vector<int>> F(n); for (int i=0; i<n; i++) for (int j=0; j<(int)E_[i].size(); j++) { int v = E_[i][j]; R[i].push_back(int(E[v].size())); R[v].push_back(int(E[i].size())); E[i].push_back(v); E[v].push_back(i); F[i].push_back(F_[i][j]); Forg[i].push_back(j); F[v].push_back(0); Forg[v].push_back(-1); } vector<bool> U(n); function<int (int, int)> dfs = [&](int p, int f) { if (p==t) return f; U[p] = true; for (int i=0; i<(int)E[p].size(); i++) if (!U[E[p][i]] && F[p][i]>0) { int t = dfs(E[p][i], min(f, (int)F[p][i])); if (t>0) { F[p][i] -= t; F[E[p][i]][R[p][i]] += t; return t; } } return 0; }; int flow = 0; while (true) { for (int i=0; i<n; i++) U[i] = false; int f = dfs(s, 0x7fffffff); if (f==0) break; flow += f; } //return flow; for (int i=0; i<n; i++) for (int j=0; j<(int)Forg[i].size(); j++) if (Forg[i][j]!=-1) F_[i][Forg[i][j]] = F[i][j]; } */ template<class T> T maxflow(vector<vector<int>> E_, vector<vector<T>> C_, int s, int t) { int n = (int)E_.size(); vector<vector<int>> E(n), R(n), Corg(n); vector<vector<T>> C(n); T mx = {}; for (int i=0; i<n; i++) for (int j=0; j<(int)E_[i].size(); j++) { int v = E_[i][j]; R[i].push_back((int)E[v].size()); R[v].push_back((int)E[i].size()); E[i].push_back(v); E[v].push_back(i); C[i].push_back(C_[i][j]); Corg[i].push_back(j); C[v].push_back(T()); Corg[v].push_back(-1); mx = max(mx, C_[i][j]); } vector<int> D; vector<int> I; auto bfs = [&]() { D = vector<int>(n, -1); vector<int> Q; D[s] = 0; Q.push_back(s); int q = 0; while (q<(int)Q.size()) { int v = Q[q++]; for (int i=0; i<(int)E[v].size(); i++) if (C[v][i]>0 && D[E[v][i]]==-1) { D[E[v][i]] = D[v]+1; Q.push_back(E[v][i]); } } }; function<T(int,T)> dfs = [&](int v, T f) { if (v==t) return f; for (int &i=I[v]; i<(int)E[v].size(); i++) if (C[v][i]>0 && D[v]<D[E[v][i]]) { T d = dfs(E[v][i], min(f, C[v][i])); if (d>0) { C[v][i] -= d; C[E[v][i]][R[v][i]] += d; return d; } } return T(); }; T flow = {}; while (true) { bfs(); if (D[t]==-1) break; I = vector<int>(n, 0); while (true) { T f = dfs(s, mx); if (f == T()) break; flow += f; } } //return flow; for (int i=0; i<n; i++) for (int j=0; j<(int)Corg[i].size(); j++) if (Corg[i][j]!=-1) C_[i][Corg[i][j]] = C[i][j]; return 0; } #include <iostream> #include <queue> int main() { int n, m; cin>>n>>m; vector<int> x1(m), y1(m), x2(m), y2(m); for (int i=0; i<m; i++) { cin>>x1[i]>>y1[i]>>x2[i]>>y2[i]; x1[i]--, y1[i]--, x2[i]--, y2[i]--; } // https://www.slideshare.net/drken1215/ss-86894312 vector<vector<int>> M(n, vector<int>(n, -1)); for (int i=0; i<m; i++) { M[y1[i]][x1[i]] = 0; M[y2[i]][x2[i]] = 0; } int C = 0; for (int y=0; y<n; y++) for (int x=0; x<n; x++) if (M[y][x]>=0) M[y][x] = C++; vector<bool> O(C); for (int y=0; y<n; y++) for (int x=0; x<n; x++) if (M[y][x]>=0) O[M[y][x]] = (y+x)%2!=0; vector<int> e1(m), e2(m); // even, odd for (int i=0; i<m; i++) { e1[i] = M[y1[i]][x1[i]]; e2[i] = M[y2[i]][x2[i]]; if (O[e1[i]]) swap(e1[i], e2[i]); } vector<vector<int>> E(C+2); vector<vector<int>> F(C+2); for (int i=0; i<m; i++) { E[e1[i]].push_back(e2[i]); F[e1[i]].push_back(1); } for (int i=0; i<C; i++) if (!O[i]) { E[C].push_back(i); F[C].push_back(1); } else { E[i].push_back(C+1); F[i].push_back(1); } maxflow(E, F, C, C+1); for (int i=0; i<C+2; i++) E[i].clear(); vector<int> EC(C); vector<bool> FF(C); for (int i=0; i<C; i++) if (!O[i]) FF[i] = true; for (int i=0; i<m; i++) { if (F[e1[i]][EC[e1[i]]]!=0) { E[e2[i]].push_back(e1[i]); FF[e1[i]] = false; } else E[e1[i]].push_back(e2[i]); EC[e1[i]]++; } queue<int> Q; for (int i=0; i<C; i++) if (FF[i]) Q.push(i); while (!Q.empty()) { int q = Q.front(); Q.pop(); for (int e: E[q]) if (!FF[e]) { FF[e] = true; Q.push(e); } } int ans = 0; for (int i=0; i<C; i++) if (!O[i] && FF[i] || O[i] && !FF[i]) ans++; for (int y=0; y<n; y++) for (int x=0; x<n; x++) if (M[y][x]<0) ans++; cout<<ans<<endl; } /* int main() { int n, m; cin>>n>>m; vector<int> x1(m), y1(m), x2(m), y2(m); for (int i=0; i<m; i++) { cin>>x1[i]>>y1[i]>>x2[i]>>y2[i]; x1[i]--, y1[i]--, x2[i]--, y2[i]--; } // https://www.slideshare.net/drken1215/ss-86894312 vector<vector<int>> E(n*n+2); vector<vector<signed char>> F(n*n+2); for (int i=0; i<m; i++) { int p1 = y1[i]*n+x1[i]; int p2 = y2[i]*n+x2[i]; if ((x1[i]+y1[i])%2!=0) swap(p1, p2); E[p1].push_back(p2); F[p1].push_back(1); } for (int y=0; y<n; y++) for (int x=0; x<n; x++) if ((x+y)%2==0) { E[n*n].push_back(y*n+x); F[n*n].push_back(1); } else { E[y*n+x].push_back(n*n+1); F[y*n+x].push_back(1); } maxflow(E, F, n*n, n*n+1); for (int i=0; i<n*n; i++) E[i].clear(); vector<int> EC(n*n); vector<bool> FF(n*n); for (int y=0; y<n; y++) for (int x=0; x<n; x++) if ((x+y)%2==0) FF[y*n+x] = true; for (int i=0; i<m; i++) { int p1 = y1[i]*n+x1[i]; int p2 = y2[i]*n+x2[i]; if ((x1[i]+y1[i])%2!=0) swap(p1, p2); if (F[p1][EC[p1]]==0) { E[p2].push_back(p1); FF[p1] = false; } else E[p1].push_back(p2); EC[p1]++; } queue<int> Q; for (int y=0; y<n; y++) for (int x=0; x<n; x++) if (FF[y*n+x]) Q.push(y*n+x); while (!Q.empty()) { int q = Q.front(); Q.pop(); for (int e: E[q]) if (!FF[e]) { FF[e] = true; Q.push(e); } } int ans = 0; for (int y=0; y<n; y++) for (int x=0; x<n; x++) if ((x+y)%2==0 && FF[y*n+x] || (x+y)%2!=0 && !FF[y*n+x]) ans++; cout<<ans<<endl; } */
3åç®: walls
å ¬éãããé ä½è¡¨ã§è§£ãã¦ãã人æ°ãå°ãªãã£ãã®ã§ã¹ã«ã¼ã
4åç®: tourism
æ¨ãä¸ããããã é ç¹ã§ã¯éãããããã 辺ã§ã¯éãåãããã 移åå ã¨ç§»åå ã®ã¯ã¨ãªãè¤æ°ä¸ããããããããã«ã¤ãã¦ãéãå°½ããªãããã«ç§»åã§ããåæææéã®æå°å¤ãæ±ããã
åé ç¹x
ã«ã¤ãã¦ãx
ããæ ¹ã«ç§»åããã¨ãã«å¿
è¦ãªåæææéã¨ãæ ¹ããx
ã«ç§»åããã¨ãã«å¿
è¦ãªåæææéã¨ãx
ããæ ¹ã«ç§»åããã¨ãã«ææéããããã«ãªã£ã¦ããããæ±ãã¦ããã°ãåã¯ã¨ãªãã§å¦çã§ããã
æ¸ãã¦ã¿ããã©ãµã³ãã«ã±ã¼ã¹ãåããªã â ããçµç±ããã®ã¯æ ¹ã§ã¯ãªãæå°å ±éç¥å ãã
ã ãããå·®åè¨ç®ãã§ãããã ããå¿ è¦ãªéã®æå°å¤ãåã£ã¦ããã¨ããããããããã¯Segment Treeçãªãã¨ãããªãã¨ãããªãã ãã®æç¹ã§æ®ã10åã 解ããããããªãã çµããã
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; int main() { int n, m; cin>>n>>m; vector<long long> x(n); for (long long &t: x) cin>>t; vector<vector<int>> E(n); vector<vector<long long>> T(n); for (int i=0; i<n-1; i++) { int v, w; long long t; cin>>v>>w>>t; v--, w--; E[v].push_back(w); T[v].push_back(t); E[w].push_back(v); T[w].push_back(t); } vector<long long> MF(n); // MF[x]: needed money from x to 0 function<void(int, int, long long)> f1 = [&](int c, int p, long long t) { if (p>=0) MF[c] = max(0LL, t-x[c]+MF[p]); for (int i=0; i<(int)E[c].size(); i++) if (E[c][i]!=p) f1(E[c][i], c, T[c][i]); }; f1(0, -1, 0); vector<long long> MT(n); // MT[x]: needed money from 0 to x function<void(int, int, long long)> f2 = [&](int c, int p, long long m) { if (p>=0) MT[c] = max(0LL, max(MT[p], -m)); for (int i=0; i<(int)E[c].size(); i++) if (E[c][i]!=p) f2(E[c][i], c, m+x[c]-T[c][i]); }; f2(0, -1, 0); vector<long long> MS(n); function<void(int, int, long long)> f3 = [&](int c, int p, long long m) { if (p>=0) { m += x[c]; MS[c] = m; } for (int i=0; i<(int)E[c].size(); i++) if (E[c][i]!=p) f3(E[c][i], c, m-T[c][i]); }; f3(0, -1, 0); for (int i=0; i<n; i++) cout<<i<<" "<<MF[i]<<" "<<MT[i]<<" "<<MS[i]<<endl; for (int i=0; i<m; i++) { int a, b; cin>>a>>b; a--, b--; //cout<<MF[a]+max(0LL, MT[b]-MS[a])<<endl; cout<<MF[a]+max(0LL, MT[b]-MS[a])<<" "<<MF[a]<<" "<<MT[b]<<" "<<MS[a]<<endl; } }