├── 2022 ├── ingredient_optimization.py3 ├── interesting_outing.py3 ├── inventor_outlasting.py3 └── inversions_organize.py3 ├── LICENSE └── README.md /2022/ingredient_optimization.py3: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2022 kamyu. All rights reserved. 2 | # 3 | # Google Code Jam to I/O for Women 2022 - Problem B. Ingredient Optimization 4 | # https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a341ec 5 | # 6 | # Time: O(N + DlogD) 7 | # Space: O(D) 8 | # 9 | 10 | from heapq import heappush, heappop 11 | 12 | def check(min_heap, cnt): 13 | while min_heap and cnt: 14 | c = min(min_heap[0][1], cnt) 15 | min_heap[0][1] -= c 16 | if not min_heap[0][1]: 17 | heappop(min_heap) 18 | cnt -= c 19 | return cnt == 0 20 | 21 | def ingredient_optimization(): 22 | D, N, U = map(int, input().strip().split()) 23 | M_L_E = [list(map(int, input().strip().split())) for _ in range(D)] 24 | O = list(map(int, input().strip().split())) 25 | min_heap = [] 26 | result = left = 0 27 | for x in O: 28 | while left < len(M_L_E) and M_L_E[left][0] <= x: 29 | heappush(min_heap, [M_L_E[left][0]+M_L_E[left][2], M_L_E[left][1]]) 30 | left += 1 31 | while min_heap and min_heap[0][0] <= x: 32 | heappop(min_heap) 33 | if not check(min_heap, U): 34 | break 35 | result += 1 36 | return result 37 | 38 | for case in range(int(input())): 39 | print('Case #%d: %s' % (case+1, ingredient_optimization())) 40 | -------------------------------------------------------------------------------- /2022/interesting_outing.py3: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2022 kamyu. All rights reserved. 2 | # 3 | # Google Code Jam to I/O for Women 2022 - Problem C. Interesting Outing 4 | # https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a33bc7 5 | # 6 | # Time: O(N) 7 | # Space: O(N) 8 | # 9 | 10 | def bfs(adj, u): 11 | q = [u] 12 | dist = [-1]*len(adj) 13 | dist[u] = 0 14 | while q: 15 | new_q = [] 16 | for u in q: 17 | for v, d in adj[u]: 18 | if dist[v] != -1: 19 | continue 20 | dist[v] = dist[u]+d 21 | new_q.append(v) 22 | q = new_q 23 | return dist 24 | 25 | def interesting_outing(): 26 | N = int(input()) 27 | adj = [[] for _ in range(N)] 28 | total = 0 29 | for _ in range(N-1): 30 | A, B, C = map(int, input().strip().split()) 31 | adj[A-1].append((B-1, C)) 32 | adj[B-1].append((A-1, C)) 33 | total += C 34 | dist = bfs(adj, 0) 35 | start = max(range(N), key=dist.__getitem__) 36 | max_dist = max(bfs(adj, start)) 37 | return 2*total-max_dist 38 | 39 | for case in range(int(input())): 40 | print('Case #%d: %s' % (case+1, interesting_outing())) 41 | -------------------------------------------------------------------------------- /2022/inventor_outlasting.py3: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2022 kamyu. All rights reserved. 2 | # 3 | # Google Code Jam to I/O for Women 2022 - Problem D. Inventor Outlasting 4 | # https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a33fb0 5 | # 6 | # Time: O(R^3 * C^3) 7 | # Space: O(R^2 * C^2) 8 | # 9 | 10 | from functools import lru_cache 11 | from collections import Counter 12 | 13 | def round_up(x, parity): 14 | return x+(parity^(x%2)) 15 | 16 | def round_down(x, parity): 17 | return x-(parity^(x%2)) 18 | 19 | def mex(lookup): 20 | result = 0 21 | while result in lookup: 22 | result += 1 23 | return result 24 | 25 | def inventor_outlasting(): 26 | depth = [0] 27 | cnt = [Counter(), Counter()] 28 | @lru_cache(None) 29 | def memoization(l, r, u, d): 30 | depth[0] += 1 31 | lookup = set() 32 | for x in range(l+2, r, 2): 33 | # 0 <= i < len(L) and 0 <= j < len(L[0]) and u+2 <= y < d 34 | # => 0 <= y+x < 2*len(L) and 0 <= y-x < 2*len(L[0]) and u+2 <= y < d 35 | # => max(-x, x, u+2) <= y < min(2*len(L)-x, 2*len(L[0])+x, d) 36 | for y in range(max(-x, x, u+2), min(2*len(L)-x, 2*len(L[0])+x, d), 2): 37 | if L[(y+x)//2][(y-x)//2] != 'X': 38 | continue 39 | g = (memoization(l, x, u, y)^ 40 | memoization(l, x, y, d)^ 41 | memoization(x, r, u, y)^ 42 | memoization(x, r, y, d)) 43 | lookup.add(g) 44 | if depth[0] == 1: 45 | cnt[l%2][g] += 1 46 | depth[0] -= 1 47 | return mex(lookup) 48 | 49 | R, C = map(int, input().strip().split()) 50 | L = [input().strip() for _ in range(R)] 51 | for parity in range(2): 52 | memoization(round_up(0-(C-1), parity)-2, 53 | round_down((R-1)-0, parity)+2, 54 | round_up(0+0, parity)-2, 55 | round_down((R-1)+(C-1), parity)+2) 56 | grundy = list(map(mex, cnt)) 57 | return cnt[0][0^grundy[1]]+cnt[1][0^grundy[0]] 58 | 59 | for case in range(int(input())): 60 | print('Case #%d: %s' % (case+1, inventor_outlasting())) 61 | -------------------------------------------------------------------------------- /2022/inversions_organize.py3: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2022 kamyu. All rights reserved. 2 | # 3 | # Google Code Jam to I/O for Women 2022 - Problem A. Inversions Organize 4 | # https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a33e95 5 | # 6 | # Time: O(N^2) 7 | # Space: O(1) 8 | # 9 | 10 | def inversions_organize(): 11 | N = int(input()) 12 | C = [input().strip() for _ in range(2*N)] 13 | cnt = [[0]*2 for _ in range(2)] 14 | for i in range(len(C)): 15 | for j, x in enumerate(C[i]): 16 | cnt[i//N][j//N] += int(x == 'I') 17 | return abs(cnt[0][0]-cnt[1][1])+abs(cnt[1][0]-cnt[0][1]) 18 | 19 | for case in range(int(input())): 20 | print('Case #%d: %s' % (case+1, inversions_organize())) 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 kamyu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [GoogleCodeJamIO 2022](https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870) ![Language](https://img.shields.io/badge/language-Python3-orange.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) ![Progress](https://img.shields.io/badge/progress-4%20%2F%204-ff69b4.svg) ![Visitors](https://visitor-badge.laobi.icu/badge?page_id=kamyu104.googlecodejamio.2022) 2 | 3 | * Python3 solutions of Google Code Jam to I/O for Women 2022. Solution begins with `*` means it will get TLE in the largest data set. 4 | * Total computation amount > `10^8` is not friendly for Python3 to solve in 5 ~ 15 seconds. 5 | * A problem was marked as `Very Hard` means that it was an unsolved one during the contest and may not be that difficult. 6 | 7 | ## Rounds 8 | 9 | * [2022](https://github.com/kamyu104/GoogleCodeJamIO-2022#2022) 10 | * [Code Jam Farewell Rounds](https://github.com/kamyu104/GoogleCodeJam-Farewell-Rounds) 11 | 12 | ## 2022 13 | | # | Title | Solution | Time | Space | Difficulty | Tag | Note | 14 | |---| ----- | -------- | ---- | ----- | ---------- | --- | ---- | 15 | |A| [Inversions Organize](https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a33e95)| [Python3](./2022/inversions_organize.py3)| _O(N^2)_ | _O(1)_ | Easy | | Math | 16 | |B| [Ingredient Optimization](https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a341ec)| [Python3](./2022/ingredient_optimization.py3) | _O(N + DlogD)_ | _O(D)_ | Medium | | Greedy, Heap | 17 | |C| [Interesting Outing](https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a33bc7)| [Python3](./2022/interesting_outing.py3) | _O(N)_ | _O(N)_ | Medium | | Graph, BFS | 18 | |D| [Inventor Outlasting](https://codingcompetitions.withgoogle.com/codejamio/round/00000000009d9870/0000000000a33fb0)| [Python3](./2022/inventor_outlasting.py3) | _O(R^3 * C^3)_ | _O(R^2 * C^2)_ | Hard | | Sprague-Grundy Theorem, Memoization | 19 | --------------------------------------------------------------------------------