├── README.md ├── CCC08S2.java ├── CCC21S2.cpp ├── CCC00S1.cpp ├── CCC12S1.java ├── CCC17S1.java ├── CCC09S1.java ├── CCC11S2.java ├── CCC18S1.java ├── CCC15S1.java ├── CCC08S1.java ├── CCC07S3.cpp ├── CCC05S2.java ├── CCC18S4.java ├── CCC14S3.cpp ├── CCC15S5.cpp ├── CCC05S4.java ├── CCC13S1.java ├── CCC19S2.java ├── CCC16S1.java ├── CCC06S2.java ├── CCC00S4.java ├── CCC14S1.java ├── CCC07S1.java ├── CCC03S1.java ├── CCC21S1.java ├── CCC01S3.cpp ├── CCC10S3.java ├── CCC21S4.cpp ├── CCC16S5.java ├── CCC00S1.java ├── CCC11S3.java ├── CCC07S4.java ├── CCC19S1.java ├── CCC16S2.java ├── CCC10S2.java ├── CCC02S1.java ├── CCC09S5.java ├── CCC02S2.java ├── CCC14S2.java ├── CCC11S1.java ├── CCC20S1.java ├── CCC16S4.java ├── CCC17S2.java ├── CCC01S2.java ├── CCC15S2.java ├── CCC13S2.java ├── CCC21S2.java ├── CCC13S4.java ├── CCC07S3.java ├── CCC07S5.java ├── CCC14S3.java ├── CCC12S3.java ├── CCC05S1.java ├── CCC09S4_NaiveWay.java ├── CCC10S3V2.java ├── CCC07S2.java ├── CCC11S5.java ├── CCC00S2.java ├── CCC13S5.java ├── CCC20S4.java ├── CCC03S3.cpp ├── CCC09S2.java ├── CCC18S2.java ├── CCC05S4Way2.java ├── CCC20S3.java ├── CCC09S3.cpp ├── CCC01S3.java ├── CCC19S5.java ├── CCC21S4.java ├── CCC15S4.java ├── CCC09S4_PriorityQueue.java ├── CCC11S3V2.java ├── CCC05S5.java ├── CCC13S3.java ├── CCC12S5.java ├── CCC21S3.java ├── CCC12S2.java ├── CCC03S4_StringHashingSolution.java ├── CCC15S3.java ├── CCC04S4.java ├── CCC10S1.java ├── CCC02S4.java ├── CCC00S3.java ├── CCC03S3.java ├── CCC06S3.java ├── CCC04S5.java ├── CCC06S1.java ├── CCC15S5.java ├── CCC19S3.java ├── CCC14S4.java ├── CCC04S3.java ├── CCC21S5.java ├── CCC11S4.java ├── CCC08S3.java ├── CCC17S3.java └── CCC06S4.java /README.md: -------------------------------------------------------------------------------- 1 | # CCC-Solutions 2 | This respository contains solutions to most past CCC questions 3 | In the files you will find total score awarded for individual solution 4 | -------------------------------------------------------------------------------- /CCC08S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '08 S2 - Pennies in the Ring 7 | * Question type: Geometry 8 | * 3/3 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC08S2 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static StringTokenizer st; 14 | public static void main(String[] args) throws IOException { 15 | while (true){ 16 | int r = Integer.parseInt(br.readLine()); 17 | if (r==0) break; 18 | int cnt = 0; 19 | for (int i = 0; i <= r; i++) { 20 | cnt += (int) Math.sqrt(r*r-i*i); 21 | } 22 | System.out.println(cnt*4+1); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /CCC21S2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int m, n, k, num, sumr = 0,sumc=0; 4 | char act; 5 | 6 | int col[5000000]; 7 | int row[5000000]; 8 | 9 | 10 | int main() 11 | { 12 | cin.tie(0); 13 | ios::sync_with_stdio(0); 14 | cin >> m >> n >> k; 15 | 16 | for(int i=0;i> act >> num; 25 | if(act=='R'){ 26 | row[num-1] = (row[num-1]+1)%2; 27 | } 28 | else{ 29 | col[num-1] = (col[num-1] +1 )%2; 30 | 31 | } 32 | } 33 | for(int i=0;i 9 | using namespace std; 10 | 11 | int n, a, b, c, sum = 0; 12 | 13 | int main() { 14 | ios_base::sync_with_stdio(0); 15 | cin.tie(0); 16 | cout.tie(0); 17 | cin >> n >> a >> b >> c; 18 | 19 | while (n > 0) { 20 | if (--n >= 0) { 21 | a++; 22 | if (a % 35 == 0) { 23 | n += 30; 24 | } 25 | sum++; 26 | } 27 | if (--n >= 0) { 28 | b++; 29 | if (b % 100 == 0) { 30 | n += 60; 31 | } 32 | sum++; 33 | } 34 | if (--n >= 0) { 35 | c++; 36 | if (c % 10 == 0) { 37 | n += 9; 38 | } 39 | sum++; 40 | } 41 | } 42 | 43 | cout << "Martha plays " << sum << " times before going broke." 44 | << "\n"; 45 | } 46 | -------------------------------------------------------------------------------- /CCC12S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '12 S1 - Don't pass me the ball! 7 | * Question type: Simple Math 8 | * 50/50 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc12s1 10 | * @author Tommy Pang 11 | */ 12 | public class CCC12S1 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | 16 | public static void main(String[] args) throws IOException{ 17 | int N = Integer.parseInt(br.readLine()); 18 | int total = 0; 19 | if (N<4) { 20 | System.out.println(0); 21 | return; 22 | } 23 | else { 24 | int pre = 1; 25 | for (int i = 2; i < N-4+2; i++) { 26 | pre = pre+i; 27 | total+=pre; 28 | } 29 | total+=1; 30 | System.out.println(total); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CCC17S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '17 S1 - Sum Game 7 | * Question type: Implementation 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc17s1 10 | * @author Tommy Pang 11 | */ 12 | public class CCC17S1 { 13 | static StringTokenizer st1, st2; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | public static void main(String[] args) throws IOException{ 16 | int N = Integer.parseInt(br.readLine()); 17 | int K = 0, Swifts = 0, Semaphores = 0; 18 | st1 = new StringTokenizer(br.readLine()); 19 | st2 = new StringTokenizer(br.readLine()); 20 | for (int i = 1; i <= N; i++) { 21 | Swifts += Integer.parseInt(st1.nextToken()); 22 | Semaphores += Integer.parseInt(st2.nextToken()); 23 | if (Swifts == Semaphores) K=i; 24 | } 25 | System.out.println(K); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CCC09S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Map; 5 | import java.util.StringTokenizer; 6 | /** 7 | * CCC '09 S1 - Cool Numbers 8 | * Question type: Simple Math 9 | * 5/5 on DMOJ 10 | * @author Tommy Pang 11 | */ 12 | public class CCC09S1 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | 16 | public static void main(String[] args) throws IOException{ 17 | int a = Integer.parseInt(br.readLine()); 18 | int b = Integer.parseInt(br.readLine()); 19 | int cnt = 0; 20 | long sqrt = (long) Math.sqrt(b); 21 | long cbrt = (long) Math.cbrt(b); 22 | for (long i = (long) Math.sqrt(a); i <= sqrt; i++) { 23 | for (long j = (long) Math.cbrt(a); j <= cbrt; j++) { 24 | if(i * i == j * j * j) 25 | cnt+=1; 26 | } 27 | } 28 | System.out.println(cnt); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /CCC11S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '11 S2 - Multiple Choice 7 | * Question type: Implementation 8 | * 100/100 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc11s2 10 | * @author Tommy Pang 11 | */ 12 | public class CCC11S2 { 13 | 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static StringTokenizer st; 16 | static String [] correct, student; 17 | public static void main(String[] args) throws IOException{ 18 | int N = Integer.parseInt(br.readLine()); 19 | correct = new String[N]; student = new String[N]; 20 | for (int i = 0; i < N; i++) { 21 | student[i] = br.readLine(); 22 | } 23 | for (int i = 0; i < N; i++) { 24 | correct[i] = br.readLine(); 25 | } 26 | int ans = 0; 27 | for (int i = 0; i < N; i++) { 28 | if (correct[i].equals(student[i])) ans+=1; 29 | } 30 | System.out.println(ans); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CCC18S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Arrays; 5 | import java.util.StringTokenizer; 6 | /** 7 | * CCC '18 S1 - Voronoi Villages 8 | * Question type: Implementation 9 | * 15/15 on DMOJ 10 | * Question URL: https://dmoj.ca/problem/ccc18s1 11 | * @author Tommy Pang 12 | */ 13 | public class CCC18S1 { 14 | static StringTokenizer st; 15 | static int [] arr; 16 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 17 | public static void main(String[] args) throws IOException{ 18 | int N = Integer.parseInt(br.readLine()); 19 | arr = new int[N]; 20 | for (int i = 0; i < N; i++) { 21 | arr[i] = Integer.parseInt(br.readLine()); 22 | } 23 | Arrays.sort(arr); 24 | double min = 1e10; 25 | for (int i = 1; i < N-1; i++) { 26 | double size = (double) (arr[i] - arr[i-1])/2 + (double) (arr[i+1] - arr[i])/2; 27 | min = Math.min(size, min); 28 | } 29 | System.out.println(String.format("%.1f", min)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CCC15S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '15 S1 - Zero That Out 9 | * Question type: Implementation 10 | * 15/15 on DMOJ 11 | * Question URL: https://dmoj.ca/problem/ccc15s1 12 | * @author Tommy Pang 13 | */ 14 | public class CCC15S1 { 15 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | static StringTokenizer st; 17 | 18 | public static void main(String[] args) throws IOException{ 19 | int N = Integer.parseInt(br.readLine()); 20 | List list = new ArrayList<>(); 21 | for (int i = 0; i < N; i++) { 22 | int num = Integer.parseInt(br.readLine()); 23 | if (num==0) { 24 | list.remove(list.size()-1); 25 | continue; 26 | } 27 | list.add(num); 28 | } 29 | int total = 0; 30 | for (int i = 0; i < list.size(); i++) { 31 | total+=list.get(i); 32 | } 33 | System.out.println(total); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CCC08S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Arrays; 5 | import java.util.StringTokenizer; 6 | /** 7 | * CCC '08 S1 - It's Cold Here! 8 | * Question type: Implementation 9 | * 3/3 on DMOJ 10 | * @author Tommy Pang 11 | */ 12 | public class CCC08S1 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | 16 | public static void main(String[] args) throws IOException{ 17 | int temperature = 201; 18 | String ans = ""; 19 | while (true){ 20 | st = new StringTokenizer(br.readLine()); 21 | String s = st.nextToken(); 22 | String s2 = st.nextToken(); 23 | if (s2.contains("-")) { 24 | int v = Integer.parseInt((s2.split("-"))[1]); 25 | if (-1 * v < temperature) { 26 | temperature = -1 * v; 27 | ans = s; 28 | } 29 | } 30 | if (s.equals("Waterloo")){ 31 | System.out.println(ans); 32 | return; 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CCC07S3.cpp: -------------------------------------------------------------------------------- 1 | // Problem: CCC '07 S3 - Friends 2 | // Contest: CCC 3 | // URL: https://dmoj.ca/problem/ccc07s3 4 | // Memory Limit: 64 MB 5 | // Time Limit: 1000 ms 6 | // Ryan Liu 7 | 8 | #include 9 | using namespace std; 10 | 11 | typedef long long ll; 12 | typedef vector vi; 13 | #define pb push_back 14 | 15 | vector adj[10000]; 16 | queue q; 17 | 18 | int n, t1, t2; 19 | 20 | void bfs(int a, int b) { 21 | bool used[10000]; 22 | int dist[10000]; 23 | memset(used, false, sizeof(used)); 24 | memset(dist, 0, sizeof(dist)); 25 | q.push(a); 26 | used[a] = true; 27 | while (!q.empty()) { 28 | int v = q.front(); 29 | q.pop(); 30 | for (int u : adj[v]) { 31 | if (!used[u]) { 32 | used[u] = true; 33 | q.push(u); 34 | dist[u] = dist[v] + 1; 35 | } 36 | } 37 | } 38 | 39 | cout << ((used[b]) ? ("Yes " + to_string(dist[b] - 1)) : "No") << "\n"; 40 | } 41 | 42 | int main() { 43 | ios_base::sync_with_stdio(0); 44 | cin.tie(0); 45 | cout.tie(0); 46 | cin >> n; 47 | 48 | for (int i = 0; i < n; i++) { 49 | cin >> t1 >> t2; 50 | adj[t1].pb(t2); 51 | } 52 | 53 | while ((cin >> t1 >> t2) && ((t1 != 0) && (t2 != 0))) { 54 | bfs(t1, t2); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /CCC05S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '05 S2 - Mouse Move 7 | * Question type: Simple Math 8 | * 5/5 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC05S2 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static StringTokenizer st; 14 | 15 | public static void main(String[] args) throws IOException{ 16 | st = new StringTokenizer(br.readLine()); 17 | int x_max = Integer.parseInt(st.nextToken()); 18 | int y_max = Integer.parseInt(st.nextToken()); 19 | int x = 0, y = 0; 20 | while (true){ 21 | st = new StringTokenizer(br.readLine()); 22 | int a = Integer.parseInt(st.nextToken()); 23 | int b = Integer.parseInt(st.nextToken()); 24 | if (a==0 && b==0) return; 25 | if (x+a<0) x = 0; 26 | else if (x+a>x_max) x = x_max; 27 | else x += a; 28 | if (y+b<0) y = 0; 29 | else if (y+b>y_max) y = y_max; 30 | else y+=b; 31 | System.out.println(x+" "+y); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CCC18S4.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '18 S4 - Balanced Trees 9 | * Question type: Dynamic Programming 10 | * 15/15 on DMOJ 11 | * Question URL: https://dmoj.ca/problem/ccc18s4 12 | * @author Tommy Pang 13 | */ 14 | public class CCC18S4 { 15 | static StringTokenizer st; 16 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 17 | static Map map = new HashMap<>(); 18 | public static void main(String[] args) throws IOException { 19 | long W = Long.parseLong(br.readLine()); 20 | map.put(1, 1L); 21 | System.out.println(fun(W)); 22 | } 23 | static long fun(long i){ 24 | if (map.containsKey((int) i)) return map.get((int) i); 25 | long ans = 0; 26 | for (int j = 2; j <= i;) { 27 | int w = (int) (i/j); 28 | int nxt = (int) (i/w) +1; 29 | ans += (nxt-j) * fun(w); 30 | // consider simple cases: N = 3 or N = 4 31 | j=nxt; 32 | } 33 | map.put((int) i, ans); 34 | return ans; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CCC14S3.cpp: -------------------------------------------------------------------------------- 1 | // Problem: CCC '14 S3 - The Geneva Confection 2 | // Contest: DMOJ 3 | // URL: https://dmoj.ca/problem/ccc14s3 4 | // Memory Limit: 64 MB 5 | // Time Limit: 3000 ms 6 | // Ryan Liu 7 | 8 | #include 9 | using namespace std; 10 | 11 | typedef long long ll; 12 | #define pb push_back 13 | 14 | deque m, b; 15 | ll n, temp, cur; 16 | bool pos = true; 17 | 18 | void solve() { 19 | // Initialize each variable for a new test case 20 | m.clear(); 21 | b.clear(); 22 | pos = true; 23 | cur = 1; 24 | 25 | cin >> n; 26 | 27 | for (ll j = 0; j < n; j++) { 28 | cin >> temp; 29 | m.pb(temp); 30 | } 31 | 32 | while (cur != n) { 33 | if (m.empty() && (cur != b.back())) { 34 | pos = false; 35 | break; 36 | } 37 | if (!(m.empty()) && m.back() == cur) { 38 | m.pop_back(); 39 | cur++; 40 | } else if (!(b.empty()) && (b.back() == cur)) { 41 | b.pop_back(); 42 | cur++; 43 | } 44 | 45 | else if (!m.empty()) { 46 | b.pb(m.back()); 47 | m.pop_back(); 48 | } 49 | } 50 | 51 | cout << ((pos) ? "Y" : "N") << "\n"; 52 | } 53 | 54 | int main() { 55 | ios_base::sync_with_stdio(0); 56 | cin.tie(0); 57 | cout.tie(0); 58 | int tc; 59 | cin >> tc; 60 | for (int t = 1; t <= tc; t++) { 61 | solve(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /CCC15S5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | /** 5 | * CCC '15 S5 - Greedy For Pies 6 | * Question URL: Dynamic Programming 7 | * 15/15 on DMOJ 8 | * Question URL: https://dmoj.ca/problem/ccc15s5 9 | * @author Tommy Pang 10 | */ 11 | int n, m; 12 | ll dp[3005][105][105][2]; 13 | int A[3005], B[3005]; 14 | ll fun(int i, int L, int R, int can) { 15 | if (dp[i][L][R][can]!=-1) return dp[i][L][R][can]; 16 | ll ret = 0; 17 | if (i<=n) { 18 | // take current one 19 | if (can==1) ret = max(ret, fun(i+1, L, R, 0) + A[i]); 20 | // skip current one 21 | ret = max(ret, fun(i+1, L, R, 1)); 22 | } 23 | if (L<=R) { 24 | // insert and waste current one 25 | ret = max(ret, fun(i, L+1, R, 1)); 26 | // insert and take current one 27 | if (can==1) ret = max(ret, fun(i, L, R-1, 0) + B[R]); 28 | } 29 | dp[i][L][R][can] = ret; 30 | return ret; 31 | } 32 | int main(){ 33 | ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); 34 | cin>>n; 35 | memset(dp, -1, sizeof (dp)); 36 | for (int i = 1; i <= n; ++i) { 37 | cin>>A[i]; 38 | } 39 | cin>>m; 40 | for (int i = 1; i <= m; ++i) { 41 | cin>>B[i]; 42 | } 43 | sort(B, B+m+1); 44 | cout< mp = new HashMap<>(); 22 | int cur = 0, dep = 0; 23 | mp.put(s[N-1], 0); 24 | for (int j = 0; j < N; j++) { 25 | if (!mp.containsKey(s[j])) { 26 | cur +=1; 27 | mp.put(s[j], cur); dep = Math.max(dep, cur); 28 | } 29 | else { 30 | cur = mp.get(s[j]); 31 | } 32 | } 33 | System.out.println(old_sum - dep*20); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CCC13S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '13 S1 - From 1987 to 2013 7 | * Question type: Implementation 8 | * 150/150 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc13s1 10 | * @author Tommy Pang 11 | */ 12 | public class CCC13S1 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | 16 | public static void main(String[] args) throws IOException{ 17 | int N = Integer.parseInt(br.readLine()); 18 | while (true){ 19 | N++; 20 | int [] count = new int[10]; 21 | boolean isAns = true; 22 | String s = String.valueOf(N); 23 | for (int i = 0; i < s.length(); i++) { 24 | count[Integer.parseInt(String.valueOf(s.charAt(i)))] += 1; 25 | } 26 | for (int nxt : count) { 27 | if (!(nxt==1 || nxt == 0)) { 28 | isAns = false; 29 | break; 30 | } 31 | } 32 | if (isAns) { 33 | System.out.println(s); 34 | return; 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CCC19S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '19 S2 - Pretty Average Primes 7 | * Question type: Simple Math 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc19s2 10 | * @author Tommy Pang 11 | */ 12 | public class CCC19S2 { 13 | static StringTokenizer st; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static boolean success = false; 16 | public static void main(String[] args) throws IOException { 17 | int cases = Integer.parseInt(br.readLine()); 18 | for (int i = 0; i < cases; i++) { 19 | int N = Integer.parseInt(br.readLine()); 20 | success = false; 21 | for (int j = 2; j < N; j++) { 22 | if (success) continue; 23 | if (Prime(j) && Prime(2*N-j)){ 24 | System.out.println(j + " " + (2*N-j)); 25 | success = true; 26 | } 27 | } 28 | } 29 | } 30 | static boolean Prime(int i){ 31 | for (int j = 2; j < (int) Math.sqrt(i)+1; j++) { 32 | if (i%j==0) return false; 33 | } 34 | return true; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /CCC16S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '16 S1 - Ragaman 7 | * Question type: Implementation 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc16s1 10 | * @author Tommy Pang 11 | */ 12 | public class CCC16S1 { 13 | static StringTokenizer st; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static int [] cntA = new int[26], cntB = new int[26]; 16 | static int dif = 0, mark = 0; 17 | public static void main(String[] args) throws IOException{ 18 | String a = br.readLine(); 19 | String b = br.readLine(); 20 | for (int i = 0; i < a.length(); i++) { 21 | if (String.valueOf(b.charAt(i)).equals("*")) { 22 | mark+=1; 23 | cntA[a.charAt(i) - 'a'] += 1; 24 | continue; 25 | } 26 | cntA[a.charAt(i) - 'a'] += 1; 27 | cntB[b.charAt(i) - 'a'] += 1; 28 | } 29 | for (int i = 0; i < 26; i++) { 30 | if (cntA[i]!=cntB[i]) dif+=Math.abs(cntA[i] - cntB[i]); 31 | } 32 | if (dif==mark) System.out.println("A"); 33 | else System.out.println("N"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CCC06S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '06 S2 - Attack of the CipherTexts 9 | * Question type: Implementation 10 | * 5/5 on DMOJ 11 | * @author Tommy Pang 12 | */ 13 | public class CCC06S2 { 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static StringTokenizer st; 16 | static Map map = new HashMap<>(); 17 | public static void main(String[] args) throws IOException{ 18 | String plaintext = br.readLine(); 19 | String ciphertext = br.readLine(); 20 | String outputText = br.readLine(); 21 | for (int i = 0; i < plaintext.length(); i++) { 22 | String plainChar = String.valueOf(plaintext.charAt(i)); 23 | String cipherChar = String.valueOf(ciphertext.charAt(i)); 24 | if (!map.containsKey(cipherChar)){ 25 | map.put(cipherChar, plainChar); 26 | } 27 | } 28 | String ans = ""; 29 | for (int i = 0; i < outputText.length(); i++) { 30 | ans += map.getOrDefault(String.valueOf(outputText.charAt(i)), "."); 31 | } 32 | System.out.println(ans); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CCC00S4.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.*; 5 | /** 6 | * CCC '00 S4 - Golf 7 | * Question type: Dynamic Programming 8 | * 6/6 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC00S4 { 12 | static StringTokenizer st; 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | public static void main(String[] args) throws IOException{ 15 | int dis = Integer.parseInt(br.readLine()); 16 | int N = Integer.parseInt(br.readLine()); 17 | int [] clubs = new int[N+1]; 18 | int [] dp = new int[dis+1]; 19 | Arrays.fill(dp, Integer.MAX_VALUE); 20 | for (int i = 1; i <= N; i++) { 21 | clubs[i] = Integer.parseInt(br.readLine()); 22 | dp[clubs[i]] = 1; 23 | } 24 | for (int i = 1; i <= N; i++) { 25 | for (int j = 1; j <= dis; j++) { 26 | if (!(j-clubs[i]<0) && dp[j-clubs[i]] != Integer.MAX_VALUE){ 27 | dp[j] = Math.min(dp[j], dp[j-clubs[i]] + 1); 28 | } 29 | } 30 | } 31 | if (dp[dis] == Integer.MAX_VALUE) System.out.println("Roberta acknowledges defeat."); 32 | else System.out.println("Roberta wins in " + dp[dis] + " strokes."); 33 | } 34 | } -------------------------------------------------------------------------------- /CCC14S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '14 S1 - Party Invitation 9 | * Question type: Implementation 10 | * 250/250 on DMOJ 11 | * Question URL: https://dmoj.ca/problem/ccc14s1 12 | * @author Tommy Pang 13 | */ 14 | public class CCC14S1 { 15 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | static StringTokenizer st; 17 | 18 | public static void main(String[] args) throws IOException { 19 | List list1 = new ArrayList<>(); 20 | List list2 = new ArrayList<>(); 21 | int N = Integer.parseInt(br.readLine()); 22 | for (int i = 1; i <= N; i++) { 23 | list1.add(i); 24 | } 25 | int M = Integer.parseInt(br.readLine()); 26 | for (int i = 0; i < M; i++) { 27 | int a = Integer.parseInt(br.readLine()); 28 | for (int j = 1; j <= list1.size(); j++) { 29 | if (!(j%a==0)) list2.add(list1.get(j-1)); 30 | } 31 | list1=list2; 32 | list2 = new ArrayList<>(); 33 | } 34 | for (int nxt : list1) { 35 | System.out.println(nxt); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CCC07S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '07 S1 - Federal Voting Age 7 | * Question type: Simple Math 8 | * 4/4 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC07S1 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static StringTokenizer st; 14 | 15 | public static void main(String[] args) throws IOException{ 16 | int N = Integer.parseInt(br.readLine()); 17 | for (int i = 0; i < N; i++) { 18 | st = new StringTokenizer(br.readLine()); 19 | int y = Integer.parseInt(st.nextToken()); 20 | int m = Integer.parseInt(st.nextToken()); 21 | int d = Integer.parseInt(st.nextToken()); 22 | if (2007-y > 18) System.out.println("Yes"); 23 | else if (2007-y < 18) System.out.println("No"); 24 | else { 25 | if (m<2) System.out.println("Yes"); 26 | else if (m>2) System.out.println("No"); 27 | else { 28 | if (d==27) System.out.println("Yes"); 29 | else if (d<27) System.out.println("Yes"); 30 | else System.out.println("No"); 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CCC03S1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '03 S1 - Snakes and Ladders 5 | * Question type: Implementation 6 | * 2/2 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC03S1 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | public static void main(String[] args) throws IOException { 13 | Map map = new HashMap<>(); 14 | map.put(99, 77); map.put(90, 48); map.put(54, 19); 15 | map.put(9, 34); map.put(40, 64); map.put(67, 86); 16 | int cur = 1; 17 | while (true){ 18 | int dice = Integer.parseInt(br.readLine()); 19 | if (dice==0) { 20 | System.out.println("You Quit!"); 21 | return; 22 | } 23 | else { 24 | if (cur+dice<=100) cur+=dice; 25 | else { 26 | System.out.println("You are now on square "+cur); 27 | continue; 28 | } 29 | if (map.containsKey(cur)) cur = map.get(cur); 30 | System.out.println("You are now on square "+cur); 31 | if (cur == 100) { 32 | System.out.println("You Win!"); 33 | return; 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CCC21S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.text.DecimalFormat; 5 | import java.util.*; 6 | /** 7 | * CCC '21 S1 - Crazy Fencing 8 | * Question type: Simple Math 9 | * 15/15 on DMOJ 10 | * Question URL: https://dmoj.ca/problem/ccc21s1 11 | * @author Tommy Pang 12 | */ 13 | public class CCC21S1 { 14 | static StringTokenizer st; 15 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | static int [] heights; 17 | static int [] width; 18 | public static void main(String[] args) throws IOException { 19 | int N = Integer.parseInt(br.readLine()); 20 | st = new StringTokenizer(br.readLine()); 21 | heights = new int[N+1]; width = new int[N]; 22 | for (int i = 0; i < N+1; i++) { 23 | heights[i] = Integer.parseInt(st.nextToken()); 24 | } 25 | st = new StringTokenizer(br.readLine()); 26 | for (int i = 0; i < N; i++) { 27 | width[i] = Integer.parseInt(st.nextToken()); 28 | } 29 | double ans = 0; 30 | DecimalFormat format = new DecimalFormat("0.#"); 31 | for (int i = 0; i < N; i++) { 32 | double v =(double)((heights[i]+heights[i+1])*width[i])/2; 33 | ans+=v; 34 | } 35 | System.out.println(format.format(ans)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CCC01S3.cpp: -------------------------------------------------------------------------------- 1 | // Problem: CCC '01 S3 - Strategic Bombing 2 | // Contest: DMOJ 3 | // URL: https://dmoj.ca/problem/ccc01s3 4 | // Memory Limit: 16 MB 5 | // Time Limit: 1000 ms 6 | // Ryan Liu 7 | 8 | #include 9 | using namespace std; 10 | 11 | typedef long long ll; 12 | typedef vector vi; 13 | #define pb push_back 14 | 15 | vector adj[27]; 16 | queue q; 17 | bool used[27]; 18 | vector edges; 19 | string t; 20 | ll sum = 0; 21 | 22 | void bfs(int a, int b) { 23 | q.push(1); 24 | used[1] = true; 25 | while (!q.empty()) { 26 | int v = q.front(); 27 | q.pop(); 28 | for (int u : adj[v]) { 29 | if (!used[u] && (!(v == a && u == b) && !(v == b && u == a))) { 30 | used[u] = true; 31 | q.push(u); 32 | } 33 | } 34 | } 35 | } 36 | 37 | void addEdge(int frm, int to) { 38 | adj[frm].pb(to); 39 | adj[to].pb(frm); 40 | } 41 | 42 | int main() { 43 | ios_base::sync_with_stdio(0); 44 | cin.tie(0); 45 | cout.tie(0); 46 | 47 | while (cin >> t && t != "**") { 48 | edges.pb(t); 49 | addEdge((int)t[0] - 64, (int)t[1] - 64); 50 | } 51 | 52 | for (int i = 0; i < edges.size(); i++) { 53 | memset(used, false, sizeof(used)); 54 | t = edges[i]; 55 | bfs((int)t[0] - 64, (int)t[1] - 64); 56 | 57 | if (!(used[2])) { 58 | sum++; 59 | cout << t << "\n"; 60 | } 61 | } 62 | cout << "There are " << sum << " disconnecting roads.\n"; 63 | } 64 | -------------------------------------------------------------------------------- /CCC10S3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '10 S3 - Firehose 5 | * Question type: Data Structures, Implementation 6 | * 9/9 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC10S3 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | static int H, house[], LEN=1000000; 13 | public static void main(String[] args) throws IOException { 14 | H = Integer.parseInt(br.readLine()); house = new int[H]; 15 | for(int i=0; i lst){ lst = house[nxt] + d; cnt++; } 31 | } 32 | ret = Math.min(ret, cnt); 33 | } 34 | return ret; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CCC21S4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * CCC '21 S4 - Daily Commute 3 | * Question type: Graph Theory 4 | * 15/15 on DMOJ 5 | * Question URL: https://dmoj.ca/problem/ccc21s4 6 | * @author Tommy Pang 7 | */ 8 | #include 9 | using namespace std; 10 | const int MM = 2e5+5; 11 | typedef pair pi; 12 | int N, W, D, s[MM], idx[MM], dis[MM]; vector adj[MM]; bool vis[MM]; set st; 13 | void bfs(int st){ 14 | fill(dis, dis+N+1, 1e9); 15 | queue q; q.push(st); dis[st]=0; vis[st]=true; 16 | while(!q.empty()){ 17 | int u = q.front(); q.pop(); 18 | for(int v: adj[u]){ 19 | if(!vis[v]) { q.push(v); dis[v] = dis[u] + 1; vis[v] = true;} 20 | } 21 | } 22 | } 23 | int main(){ 24 | ios::sync_with_stdio(0); cin.tie(0); 25 | cin >> N >> W >> D; 26 | for(int i=1, u, v; i<=W; i++){ 27 | cin >> u >> v; adj[v].push_back(u); 28 | } 29 | bfs(N); 30 | for(int i=1; i<=N; i++){ 31 | cin >> s[i]; idx[s[i]] = i - 1; 32 | } 33 | for(int i=1; i<=N; i++){ 34 | st.insert({idx[i] + dis[i], i}); 35 | } 36 | for(int i=1, x, y; i<=D; i++){ 37 | cin >> x >> y; int sx = s[x], sy = s[y]; 38 | st.erase({idx[sx]+dis[sx], sx}); st.erase({idx[sy]+dis[sy], sy}); 39 | swap(idx[sx], idx[sy]); swap(s[x], s[y]); 40 | st.insert({idx[sx]+dis[sx], sx}); st.insert({idx[sy]+dis[sy], sy}); 41 | cout << st.begin()->first << "\n"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CCC16S5.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Arrays; 5 | import java.util.StringTokenizer; 6 | /** 7 | * CCC '16 S5 - Circle of Life 8 | * Question type: Simulation 9 | * 15/15 on DMOJ 10 | * Question URL: https://dmoj.ca/problem/ccc16s5 11 | * @author Tommy Pang 12 | */ 13 | public class CCC16S5 { 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static StringTokenizer st; 16 | 17 | public static void main(String[] args) throws IOException { 18 | st = new StringTokenizer(br.readLine()); 19 | int N = Integer.parseInt(st.nextToken()); 20 | long T = Long.parseLong(st.nextToken()); 21 | int[] a = new int[N]; 22 | String s = br.readLine(); 23 | for(int i = 0; i < N; i++) 24 | a[i] = s.charAt(i) - '0'; 25 | int[] tmp; 26 | 27 | for(int i = 0; i <= 50; i++){ 28 | tmp = new int[N]; 29 | if((T & (1L << i)) != 0){ 30 | int r = (int)((1L << i) % N); 31 | int l = (N - r) % N; 32 | 33 | for(int j = 0; j < N; j++){ 34 | tmp[j] = a[(j + r) % N] ^ a[(j + l) % N]; 35 | } 36 | a = Arrays.copyOf(tmp, a.length); 37 | } 38 | } 39 | for(int x : a) 40 | System.out.print(x); 41 | System.out.println(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CCC00S1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '00 S1 - Slot Machines 5 | * Question type: Simulation 6 | * 5/5 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC00S1 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | public static void main(String[] args) throws IOException { 13 | int plays = 0; 14 | int money = Integer.parseInt(br.readLine()); 15 | int a = Integer.parseInt(br.readLine()); 16 | int b = Integer.parseInt(br.readLine()); 17 | int c = Integer.parseInt(br.readLine()); 18 | while (money >= 1) { 19 | money -= 1; 20 | plays += 1; 21 | a += 1; 22 | if (a == 35) { 23 | money += 30; 24 | a = 0; 25 | } 26 | if (money == 0) break; 27 | money -= 1; 28 | plays += 1; 29 | b += 1; 30 | if (b == 100) { 31 | money += 60; 32 | b = 0; 33 | } 34 | if (money == 0) break; 35 | money -= 1; 36 | plays += 1; 37 | c += 1; 38 | if (c == 10) { 39 | money += 9; 40 | c = 0; 41 | } 42 | if (money == 0) break; 43 | } 44 | System.out.println("Martha plays " + plays + " times before going broke."); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /CCC11S3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '11 S3 - Alice Through the Looking Glass 7 | * Question type: Recursion 8 | * 50/50 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc11s3 10 | * @author Tommy Pang 11 | */ 12 | public class CCC11S3 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | 16 | public static void main(String[] args) throws IOException{ 17 | int N = Integer.parseInt(br.readLine()); 18 | for (int i = 0; i < N; i++) { 19 | st = new StringTokenizer(br.readLine()); 20 | int m = Integer.parseInt(st.nextToken()); 21 | int x = Integer.parseInt(st.nextToken()); 22 | int y = Integer.parseInt(st.nextToken()); 23 | if (solve(m, x, y)) System.out.println("crystal"); 24 | else System.out.println("empty"); 25 | } 26 | } 27 | static boolean solve(int m, int x, int y){ 28 | int x0 = (int) (x / Math.pow(5, m - 1)); 29 | int y0 = (int) (y / Math.pow(5, m - 1)); 30 | if (x0==0) return false; 31 | if (x0 > 0 && x0 < 4 && y0 == 0) return true; 32 | if (x0 == 2 && y0 == 1) return true; 33 | if (((x0==1||x0==3)&&y0==1) || (x0==2&&y0==2)) 34 | return solve(m - 1, x % (int) Math.pow(5, m - 1), y % (int) Math.pow(5, m - 1)); 35 | return false; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CCC07S4.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '07 S4 - Waterpark 9 | * Question type: Dynamic Programming, Graph Theory 10 | * 5/5 on DMOJ 11 | * @author Tommy Pang 12 | */ 13 | public class CCC07S4 { 14 | static StringTokenizer st; 15 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | static List [] adj; 17 | static int [] nums; 18 | static int N; 19 | public static void main(String[] args) throws IOException { 20 | N = Integer.parseInt(br.readLine()); 21 | adj = new ArrayList[N+1]; 22 | nums = new int[N+1]; 23 | for (int i = 0; i < N+1; i++) { 24 | adj[i] = new ArrayList<>(); 25 | } 26 | while (true){ 27 | st = new StringTokenizer(br.readLine()); 28 | int a = Integer.parseInt(st.nextToken()); 29 | int b = Integer.parseInt(st.nextToken()); 30 | if (a==0 && b==0) break; 31 | adj[a].add(b); 32 | } 33 | find_path(1); 34 | System.out.println(nums[1]); 35 | } 36 | static void find_path(int i){ 37 | for (int nxt : adj[i]) { 38 | if (nxt==N) nums[i]+=1; 39 | else if (nums[nxt]!=0) nums[i]+=nums[nxt]; 40 | else { 41 | find_path(nxt); 42 | nums[i]+=nums[nxt]; 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /CCC19S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '19 S1 - Flipper 7 | * Question type: Implementation 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc19s1 10 | * @author Tommy Pang 11 | */ 12 | public class CCC19S1 { 13 | static StringTokenizer st; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static int [][] grid = new int[2][2]; 16 | public static void main(String[] args) throws IOException { 17 | String s = br.readLine(); 18 | grid[0][0] = 1; grid[0][1] = 2; grid[1][0] = 3; grid[1][1] = 4; 19 | for (int i = 0; i < s.length(); i++) { 20 | String temp = String.valueOf(s.charAt(i)); 21 | if (temp.equals("V")){ 22 | int temp1 = grid[0][1]; 23 | grid[0][1] = grid[0][0]; 24 | grid[0][0] = temp1; 25 | int temp2 = grid[1][1]; 26 | grid[1][1] = grid[1][0]; 27 | grid[1][0] = temp2; 28 | } 29 | else { 30 | int temp1 = grid[1][0]; 31 | grid[1][0] = grid[0][0]; 32 | grid[0][0] = temp1; 33 | int temp2 = grid[1][1]; 34 | grid[1][1] = grid[0][1]; 35 | grid[0][1] = temp2; 36 | } 37 | } 38 | for (int i = 0; i < 2; i++) { 39 | for (int j = 0; j < 2; j++) { 40 | System.out.print(grid[i][j] + " "); 41 | } 42 | System.out.println(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CCC16S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '16 S2 - Tandem Bicycle 9 | * Question type: Greedy Algorithms, Implementation 10 | * 15/15 on DMOJ 11 | * Question URL: https://dmoj.ca/problem/ccc16s2 12 | * @author Tommy Pang 13 | */ 14 | public class CCC16S2 { 15 | static StringTokenizer D, P; 16 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 17 | public static void main(String[] args) throws IOException{ 18 | int question = Integer.parseInt(br.readLine()); 19 | int N = Integer.parseInt(br.readLine()); 20 | D = new StringTokenizer(br.readLine()); 21 | P = new StringTokenizer(br.readLine()); 22 | Integer [] d = new Integer[N], p = new Integer[N]; 23 | for (int i = 0; i < N; i++) { 24 | d[i] = Integer.parseInt(D.nextToken()); 25 | p[i] = Integer.parseInt(P.nextToken()); 26 | } 27 | if (question==1) { 28 | Arrays.sort(d); 29 | Arrays.sort(p); 30 | int total = 0; 31 | for (int i = 0; i < N; i++) { 32 | total+=Math.max(d[i], p[i]); 33 | } 34 | System.out.println(total); 35 | } 36 | else { 37 | Arrays.sort(d, Collections.reverseOrder()); 38 | Arrays.sort(p); 39 | int total = 0; 40 | for (int i = 0; i < N; i++) { 41 | total+=Math.max(d[i], p[i]); 42 | } 43 | System.out.println(total); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /CCC10S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.*; 5 | /** 6 | * CCC '10 S2 - Huffman Encoding 7 | * Question type: String Algorithms 8 | * 6/6 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC10S2 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static StringTokenizer st; 14 | static Map map = new HashMap<>(); 15 | static int[] binary_length; 16 | public static void main(String[] args) throws IOException{ 17 | int N = Integer.parseInt(br.readLine()); 18 | binary_length = new int[N]; 19 | for (int i = 0; i < N; i++) { 20 | st = new StringTokenizer(br.readLine()); 21 | String letter = st.nextToken(), binary = st.nextToken(); 22 | binary_length[i] = binary.length(); 23 | map.put(binary, letter); 24 | } 25 | Arrays.sort(binary_length); 26 | String code = br.readLine(); 27 | int i = 0; 28 | StringBuilder ans = new StringBuilder(); 29 | while (true){ 30 | try { 31 | for (int nxt : binary_length) { 32 | String sub = code.substring(i, i+nxt); 33 | if (map.containsKey(sub)) { 34 | ans.append(map.get(sub)); 35 | i += nxt; 36 | break; 37 | } 38 | } 39 | 40 | } 41 | catch (StringIndexOutOfBoundsException e){ 42 | System.out.println(ans); 43 | return; 44 | } 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /CCC02S1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '02 S1 - The Students' Council Breakfast 5 | * Question type: Implementation 6 | * 3/3 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC02S1 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | static int total = 0; 13 | public static void main(String[] args) throws IOException { 14 | int P = Integer.parseInt(br.readLine()); 15 | int G = Integer.parseInt(br.readLine()); 16 | int R = Integer.parseInt(br.readLine()); 17 | int O = Integer.parseInt(br.readLine()); 18 | int N = Integer.parseInt(br.readLine()); 19 | int min = 100000, combination = 0; 20 | for (int i = 0; i < N/P + 1; i++) { 21 | for (int j = 0; j < N/G + 1; j++) { 22 | for (int k = 0; k < N/R + 1; k++) { 23 | for (int l = 0; l < N/O + 1; l++) { 24 | total += (i*P + j*G + k*R + l*O); 25 | if (total == N) { 26 | System.out.println("# of PINK is " + i + " # of GREEN is " + j + " # of RED is " + k + " # of ORANGE is " +l); 27 | min = Math.min(min, i+j+k+l); 28 | combination+=1; 29 | total=0; 30 | } 31 | total=0; 32 | } 33 | total=0; 34 | } 35 | total=0; 36 | } 37 | total=0; 38 | } 39 | System.out.println("Total combinations is " + combination + "."); 40 | System.out.println("Minimum number of tickets to print is " + min + "."); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /CCC09S5.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '09 S5 - Wireless 7 | * Question type: Data Structures 8 | * 60/60 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc09s5 10 | * @author Tommy Pang 11 | */ 12 | public class CCC09S5 { 13 | static StringTokenizer st; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | 16 | public static void main(String[] args) throws IOException { 17 | int M = Integer.parseInt(br.readLine()), N = Integer.parseInt(br.readLine()), K = Integer.parseInt(br.readLine()); 18 | int [][] dif = new int[M+2][N+2]; 19 | for (int i = 0; i < K; i++) { 20 | st = new StringTokenizer(br.readLine()); 21 | int x = Integer.parseInt(st.nextToken()), y = Integer.parseInt(st.nextToken()); 22 | int r = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken()); 23 | for (int j = Math.max(1, x-r); j <= Math.min(N, x+r); j++) { 24 | int d = (int) Math.sqrt(r*r-(x-j)*(x-j)); 25 | dif[Math.max(1, y-d)][j] += b; 26 | dif[Math.min(M, y+d)+1][j] -= b; 27 | } 28 | } 29 | int ans = 0, cnt = 0; 30 | for (int i = 1; i <= M; i++) { 31 | for (int j = 1; j <= N; j++) { 32 | dif[i][j] += dif[i-1][j]; 33 | if (dif[i][j]>ans) { 34 | ans = dif[i][j]; 35 | cnt=1; 36 | } 37 | else if (dif[i][j]==ans){ 38 | cnt++; 39 | } 40 | } 41 | } 42 | System.out.println(ans); 43 | System.out.println(cnt); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CCC02S2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '02 S2 - Fraction Action 5 | * Question type: Simple Math 6 | * 9/9 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC02S2 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | public static void main(String[] args) throws IOException { 13 | int numerator = Integer.parseInt(br.readLine()); 14 | int denominator = Integer.parseInt(br.readLine()); 15 | if (numerator == 0) { 16 | System.out.println(0); 17 | return; 18 | } 19 | if (numerator%denominator == 0) { 20 | System.out.println(numerator/denominator); 21 | return; 22 | } 23 | if (numerator == denominator){ 24 | System.out.println(1); 25 | } 26 | if (numerator > denominator) { 27 | int integer = numerator/denominator; 28 | int fra_numerator = numerator - denominator*integer; 29 | int gcd = gcdByEuclidsAlgorithm(fra_numerator, denominator); 30 | if (gcd == 1){ 31 | System.out.println(integer + " " + fra_numerator + "/" + denominator); 32 | } 33 | else { 34 | System.out.println(integer + " " + fra_numerator/gcd + "/" + denominator/gcd); 35 | } 36 | } 37 | if (numerator < denominator) { 38 | int gcd = gcdByEuclidsAlgorithm(numerator, denominator); 39 | System.out.println(numerator/gcd + "/" + denominator/gcd); 40 | } 41 | } 42 | static int gcdByEuclidsAlgorithm(int n1, int n2) { 43 | if (n2 == 0) { 44 | return n1; 45 | } 46 | return gcdByEuclidsAlgorithm(n2, n1 % n2); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /CCC14S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '14 S1 - Party Invitation 9 | * Question type: Implementation 10 | * 500/500 on DMOJ 11 | * Question URL: https://dmoj.ca/problem/ccc14s2 12 | * @author Tommy Pang 13 | */ 14 | public class CCC14S2 { 15 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | static StringTokenizer st1, st2; 17 | 18 | public static void main(String[] args) throws IOException { 19 | int N = Integer.parseInt(br.readLine()); 20 | Map map = new HashMap<>(); 21 | st1 = new StringTokenizer(br.readLine()); 22 | st2 = new StringTokenizer(br.readLine()); 23 | for (int i = 0; i < N; i++) { 24 | String a = st1.nextToken(); 25 | String b = st2.nextToken(); 26 | if (a.equals(b)){ 27 | System.out.println("bad"); 28 | return; 29 | } 30 | if (!map.containsKey(a) && !map.containsKey(b)) { 31 | map.put(a, b); 32 | map.put(b, a); 33 | } 34 | else { 35 | if (map.containsKey(a)) { 36 | if (!map.get(a).equals(b)) { 37 | System.out.println("bad"); 38 | return; 39 | } 40 | } 41 | else { 42 | if (!map.get(b).equals(a)) { 43 | System.out.println("bad"); 44 | return; 45 | } 46 | } 47 | } 48 | } 49 | System.out.println("good"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /CCC11S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '11 S1 - English or French? 7 | * Question type: String Algorithms 8 | * 100/100 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc11s1 10 | * @author Tommy Pang 11 | */ 12 | public class CCC11S1 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | static int s = 0, S = 0, t = 0, T = 0; 16 | public static void main(String[] args) throws IOException{ 17 | int N = Integer.parseInt(br.readLine()); 18 | for (int i = 0; i < N; i++) { 19 | st = new StringTokenizer(br.readLine()); 20 | while (true) { 21 | if (st.hasMoreTokens()) { 22 | String letters = st.nextToken(); 23 | for (int j = 0; j < letters.length(); j++) { 24 | switch (String.valueOf(letters.charAt(j))) { 25 | case "s": 26 | s += 1; 27 | break; 28 | case "S": 29 | S += 1; 30 | break; 31 | case "t": 32 | t += 1; 33 | break; 34 | case "T": 35 | T += 1; 36 | } 37 | } 38 | } 39 | else break; 40 | } 41 | } 42 | if (t+T > s+S) System.out.println("English"); 43 | else if (s+S > t+T) System.out.println("French"); 44 | else if (t+T == s+S) System.out.println("French"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /CCC20S1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '20 S1 - Surmising a Sprinter's Speed 5 | * Question type: Implementation 6 | * 15/15 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc20s1 8 | * @author Tommy Pang 9 | */ 10 | public class CCC20S1 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, n; 15 | public static void main(String[] args) throws IOException { 16 | n = readInt(); 17 | List list = new ArrayList<>(); 18 | for (int i = 0; i < n; i++) { 19 | list.add(new info(readInt(), readInt())); 20 | } 21 | Collections.sort(list); 22 | double ans = 0; 23 | for (int i = 0; i < n-1; i++) { 24 | info cur = list.get(i), nxt = list.get(i+1); 25 | ans = Math.max(((double) Math.abs(cur.X - nxt.X)) / Math.abs(cur.T-nxt.T), ans); 26 | } 27 | System.out.println(ans); 28 | } 29 | public static class info implements Comparable { 30 | int T, X; 31 | info(int time, int dis) { 32 | T = time; X = dis; 33 | } 34 | 35 | @Override 36 | public int compareTo(info o) { 37 | return T-o.T; // sort by time 38 | } 39 | } 40 | 41 | 42 | 43 | 44 | static String next() throws IOException { 45 | while (st == null || !st.hasMoreTokens()) 46 | st = new StringTokenizer(br.readLine().trim()); 47 | return st.nextToken(); 48 | } 49 | static long readLong() throws IOException { 50 | return Long.parseLong(next()); 51 | } 52 | static int readInt() throws IOException { 53 | return Integer.parseInt(next()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /CCC16S4.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '16 S4 - Combining Riceballs 7 | * Question type: Dynamic Programming 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc16s4 10 | * @author Tommy Pang 11 | */ 12 | public class CCC16S4 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | 16 | public static void main(String[] args) throws IOException { 17 | int N = Integer.parseInt(br.readLine()); 18 | st = new StringTokenizer(br.readLine()); 19 | int [] psa = new int[N+1]; 20 | int ans = 0; 21 | boolean [][] dp = new boolean[N+1][N+1]; 22 | for (int i = 1; i <= N; i++) { 23 | psa[i] = Integer.parseInt(st.nextToken()); 24 | ans = Math.max(psa[i], ans); 25 | psa[i]+=psa[i-1]; dp[i][i] = true; 26 | } 27 | for (int len = 1; len < N; len++) { 28 | for (int l = 1; l+len <= N; l++) { 29 | int r = l+len; 30 | for (int p = l, q = r; p <= q;) { 31 | int sumL = psa[p] - psa[l-1]; 32 | int sumR = psa[r] - psa[q-1]; 33 | if (sumL==sumR && p+1==q && dp[l][p] && dp[q][r]){ 34 | dp[l][r] = true; 35 | } 36 | else if (sumL==sumR && dp[l][p] && dp[q][r] && dp[p+1][q-1]){ 37 | dp[l][r] = true; 38 | } 39 | if (dp[l][r]){ 40 | ans = Math.max(ans, psa[r]-psa[l-1]); 41 | } 42 | if (sumL high = new ArrayList<>(); 20 | st = new StringTokenizer(br.readLine()); 21 | for (int i = 0; i < N; i++) { 22 | tides[i] = Integer.parseInt(st.nextToken()); 23 | } 24 | Arrays.sort(tides); 25 | if (N%2==0){ 26 | for (int i = 0; i < N/2; i++) { 27 | low[i] = tides[i]; 28 | } 29 | for (int i = N/2; i < N; i++) { 30 | high.add(tides[i]); 31 | } 32 | Arrays.sort(low, Collections.reverseOrder()); 33 | for (int i = 0; i < N/2; i++) { 34 | System.out.print(low[i] + " " + high.get(i) + " "); 35 | } 36 | } 37 | 38 | else { 39 | for (int i = 0; i < N/2; i++) { 40 | low[i] = tides[i]; 41 | } 42 | for (int i = N/2; i < N; i++) { 43 | high.add(tides[i]); 44 | } 45 | Arrays.sort(low, Collections.reverseOrder()); 46 | for (int i = 0; i < N/2; i++) { 47 | System.out.print(low[i] + " " + high.get(i) + " "); 48 | } 49 | System.out.print(high.get(N/2) + "\n"); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /CCC01S2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '01 S2 - Spirals 5 | * Question type: Implementation 6 | * 5/5 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC01S2 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | static int [][] spiral; 13 | static int rCur, cCur, n, start, end; 14 | public static void main(String[] args) throws IOException{ 15 | start = Integer.parseInt(br.readLine()); 16 | end = Integer.parseInt(br.readLine()); 17 | if (start == end) { System.out.println(start); return; } 18 | spiral = new int[10][10]; 19 | rCur = cCur = 4; 20 | spiral[rCur][cCur] = start; 21 | int k =1; int cnt = start; 22 | downRight(k, cnt); 23 | for (int i = 0; i < 10; i++) { 24 | for (int j = 0; j < 10; j++) { 25 | if (spiral[i][j] == 0) System.out.print(" "); 26 | else System.out.print(spiral[i][j] + " "); 27 | } 28 | System.out.println(); 29 | } 30 | } 31 | 32 | static void downRight(int k, int cnt){ 33 | for (int i = 0; i < k; i++) { 34 | rCur += 1; cnt+=1; 35 | spiral[rCur][cCur] = cnt; 36 | if (cnt==end) return; 37 | } 38 | for (int i = 0; i < k; i++) { 39 | cCur += 1; cnt+=1; 40 | spiral[rCur][cCur] = cnt; 41 | if (cnt==end) return; 42 | } 43 | upLeft(k+1, cnt); 44 | } 45 | static void upLeft(int k, int cnt){ 46 | for (int i = 0; i < k; i++) { 47 | rCur -= 1; cnt+=1; 48 | spiral[rCur][cCur] = cnt; 49 | if (cnt==end) return; 50 | } 51 | for (int i = 0; i < k; i++) { 52 | cCur -= 1; cnt+=1; 53 | spiral[rCur][cCur] = cnt; 54 | if (cnt==end) return; 55 | } 56 | downRight(k+1, cnt); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /CCC15S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '15 S2 - Jerseys 9 | * Question type: Implementation 10 | * 15/15 on DMOJ 11 | * Question URL: https://dmoj.ca/problem/ccc15s2 12 | * @author Tommy Pang 13 | */ 14 | public class CCC15S2 { 15 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | static StringTokenizer st; 17 | static boolean [] jersey; 18 | static Map map = new HashMap<>(); 19 | static int total = 0; 20 | public static void main(String[] args) throws IOException{ 21 | int J = Integer.parseInt(br.readLine()); 22 | int A = Integer.parseInt(br.readLine()); 23 | jersey = new boolean[J+1]; 24 | for (int i = 1; i <= J; i++) { 25 | map.put(i, br.readLine()); 26 | } 27 | for (int i = 0; i < A; i++) { 28 | st = new StringTokenizer(br.readLine()); 29 | String size = st.nextToken(); 30 | int num = Integer.parseInt(st.nextToken()); 31 | if (jersey[num]) continue; 32 | switch (size){ 33 | case "S": 34 | if (map.get(num).equals("S") || map.get(num).equals("M") || map.get(num).equals("L")){ 35 | total+=1; 36 | jersey[num] = true; 37 | } 38 | break; 39 | case "M": 40 | if (map.get(num).equals("M") || map.get(num).equals("L")){ 41 | total+=1; 42 | jersey[num] = true; 43 | } 44 | break; 45 | case "L": 46 | if (map.get(num).equals("L")){ 47 | total+=1; 48 | jersey[num] = true; 49 | } 50 | } 51 | } 52 | System.out.println(total); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /CCC13S2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.StringTokenizer; 3 | /** 4 | * CCC '13 S2 - Bridge Transport 5 | * Question type: Simple Math 6 | * 130/130 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc13s2 8 | * @author Tommy Pang 9 | */ 10 | public class CCC13S2 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, T, n; 15 | public static void main(String[] args) throws IOException { 16 | T = readInt(); n = readInt(); 17 | int [] cars = new int[n]; 18 | for (int i = 0; i < n; i++) { 19 | cars[i] = readInt(); 20 | } 21 | int sum = 0; 22 | for (int i = 0; i < Math.min(4, n); i++) { 23 | sum+=cars[i]; 24 | if (sum>T) { 25 | System.out.println(i); 26 | return; 27 | } 28 | } 29 | for (int i = 4; i < n; i++) { 30 | sum-=cars[i-4]; 31 | sum+=cars[i]; 32 | if (sum>T) { 33 | System.out.println(i); 34 | return; 35 | } 36 | } 37 | System.out.println(n); 38 | } 39 | static String next() throws IOException { 40 | while (st == null || !st.hasMoreTokens()) 41 | st = new StringTokenizer(br.readLine().trim()); 42 | return st.nextToken(); 43 | } 44 | static long readLong() throws IOException { 45 | return Long.parseLong(next()); 46 | } 47 | static int readInt() throws IOException { 48 | return Integer.parseInt(next()); 49 | } 50 | static double readDouble() throws IOException { 51 | return Double.parseDouble(next()); 52 | } 53 | static char readCharacter() throws IOException { 54 | return next().charAt(0); 55 | } 56 | static String readLine() throws IOException { 57 | return br.readLine().trim(); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /CCC21S2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '21 S2 - Modern Art 7 | * Question type: Implementation 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc21s2 10 | * @author Tommy Pang 11 | */ 12 | public class CCC21S2 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 15 | static StringTokenizer st; 16 | static int mod = (int) 1e9+7, m, n, k; 17 | static int[] row, col; 18 | public static void main(String[] args) throws IOException { 19 | m = readInt(); n = readInt(); k = readInt(); 20 | row = new int[m+1]; col = new int[n+1]; 21 | for (int i = 0; i < k; i++) { 22 | char c = readCharacter(); 23 | int idx = readInt(); 24 | if (c=='R') { 25 | row[idx]++; 26 | } 27 | else { 28 | col[idx]++; 29 | } 30 | } 31 | int ans = 0; 32 | for (int i = 1; i <= m; i++) { 33 | for (int j = 1; j <= n; j++) { 34 | int painted = row[i]+col[j]; 35 | if (painted%2==1) { 36 | ans++; 37 | } 38 | } 39 | } 40 | System.out.println(ans); 41 | } 42 | 43 | static String next() throws IOException { 44 | while (st == null || !st.hasMoreTokens()) 45 | st = new StringTokenizer(br.readLine().trim()); 46 | return st.nextToken(); 47 | } 48 | static long readLong() throws IOException { 49 | return Long.parseLong(next()); 50 | } 51 | static int readInt() throws IOException { 52 | return Integer.parseInt(next()); 53 | } 54 | static double readDouble() throws IOException { 55 | return Double.parseDouble(next()); 56 | } 57 | static char readCharacter() throws IOException { 58 | return next().charAt(0); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /CCC13S4.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '13 S4 - Who is Taller? 5 | * Question URL: Graph Theory 6 | * 300/300 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc13s4 8 | * @author Tommy Pang 9 | */ 10 | public class CCC13S4 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, n, m; 15 | static List [] adj; 16 | static boolean[] vis; 17 | public static void main(String[] args) throws IOException { 18 | n = readInt(); m = readInt(); 19 | adj = new List[n+1]; vis = new boolean[n+1]; 20 | for (int i = 1; i <= n; i++) { 21 | adj[i] = new ArrayList<>(); 22 | } 23 | for (int i = 0; i < m; i++) { 24 | adj[readInt()].add(readInt()); 25 | } 26 | int p = readInt(), q = readInt(); 27 | if (BFS(p, q)) System.out.println("yes"); 28 | else if (BFS(q, p)) System.out.println("no"); 29 | else System.out.println("unknown"); 30 | } 31 | static boolean BFS(int p, int q) { 32 | Queue queue = new LinkedList<>(); vis = new boolean[n+1]; 33 | queue.add(p); 34 | while (!queue.isEmpty()) { 35 | int cur = queue.poll(); 36 | for (int nxt : adj[cur]) { 37 | if (!vis[nxt]) { 38 | vis[nxt] = true; 39 | queue.add(nxt); 40 | } 41 | } 42 | } 43 | return vis[q]; 44 | } 45 | 46 | static String next() throws IOException { 47 | while (st == null || !st.hasMoreTokens()) 48 | st = new StringTokenizer(br.readLine().trim()); 49 | return st.nextToken(); 50 | } 51 | static long readLong() throws IOException { 52 | return Long.parseLong(next()); 53 | } 54 | static int readInt() throws IOException { 55 | return Integer.parseInt(next()); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /CCC07S3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.*; 5 | /** 6 | * CCC '07 S3 - Friends 7 | * Question type: Graph Theory 8 | * 4/4 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC07S3 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static StringTokenizer st; 14 | static int [] friends = new int[10000], distance; 15 | static boolean [] vis; 16 | static int ans = 0; 17 | public static void main(String[] args) throws IOException{ 18 | int N = Integer.parseInt(br.readLine()); 19 | for (int i = 0; i < N; i++) { 20 | st = new StringTokenizer(br.readLine()); 21 | int a = Integer.parseInt(st.nextToken()); 22 | int b = Integer.parseInt(st.nextToken()); 23 | friends[a] = b; 24 | } 25 | while (true) { 26 | st = new StringTokenizer(br.readLine()); 27 | int a = Integer.parseInt(st.nextToken()); 28 | int b = Integer.parseInt(st.nextToken()); 29 | if (a == 0) return; 30 | ans = 0; 31 | BFS(a); 32 | if (!vis[b]) { 33 | System.out.println("No"); 34 | continue; 35 | } 36 | ans = distance[b]-1; 37 | BFS(b); 38 | if (!vis[a]) System.out.println("No"); 39 | else { 40 | System.out.println("Yes "+ ans); 41 | } 42 | } 43 | } 44 | static void BFS(int a){ 45 | Queue queue = new LinkedList<>(); 46 | queue.add(a); 47 | vis = new boolean[10000]; 48 | distance = new int[10000]; 49 | vis[a] = true; 50 | distance[a] = 0; 51 | while (!queue.isEmpty()){ 52 | int cur = queue.poll(); 53 | if (!vis[friends[cur]]){ 54 | queue.add(friends[cur]); 55 | distance[friends[cur]] = distance[cur]+1; 56 | vis[friends[cur]] = true; 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /CCC07S5.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '07 S5 - Bowling for Numbers 7 | * Question type: Dynamic Programming 8 | * 5/5 on DMOJ 9 | * @author Milliken high school -> http://mmhs.ca/ccc/index.htm 10 | */ 11 | public class CCC07S5 { 12 | static StringTokenizer st; 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static int N, K, W, ans; 15 | static int [] balls; 16 | static int [] sum; 17 | static int [][] dp; 18 | public static void main(String[] args) throws IOException { 19 | int cases = Integer.parseInt(br.readLine()); 20 | for (int i = 0; i < cases; i++) { 21 | st = new StringTokenizer(br.readLine()); 22 | N = Integer.parseInt(st.nextToken()); 23 | K = Integer.parseInt(st.nextToken()); 24 | W = Integer.parseInt(st.nextToken()); 25 | dp = new int[K+1][N+1]; 26 | balls = new int[N+1]; 27 | sum = new int[N+1]; 28 | for (int j = 1; j <= N; j++) { 29 | balls[j] = Integer.parseInt(br.readLine()); 30 | } 31 | for (int j = 1; j <= N-W+1; j++) { 32 | for (int k = j; k < j+W; k++) { 33 | sum[j]+=balls[k]; 34 | } 35 | } 36 | solve(); 37 | System.out.println(dp[K][1]); 38 | } 39 | } 40 | static void solve(){ 41 | for (int i = 0; i < N+1; i++) { 42 | dp[0][i] = 0; 43 | } 44 | for (int i = 1; i < K+1; i++) { 45 | for (int j = 1; j < N+1; j++) { 46 | dp[i][j] = -1; 47 | } 48 | } 49 | for (int i = 1; i <= K; i++) { 50 | for (int j = N; j >= 1; j--) { 51 | if (j>=N-W+1){ 52 | dp[i][j] = sum[j]; 53 | } 54 | else { 55 | dp[i][j] = Math.max(dp[i-1][j+W] + sum[j], dp[i][j+1]); 56 | } 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /CCC14S3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '14 S3 - The Geneva Confection 5 | * Question URL: Implementation 6 | * 500/500 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc14s3 8 | * @author Tommy Pang 9 | */ 10 | public class CCC14S3 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, T, n; 15 | static Stack stk; 16 | public static void main(String[] args) throws IOException { 17 | T = readInt(); 18 | for (int i = 0; i < T; i++) { 19 | n = readInt(); 20 | stk = new Stack<>(); 21 | int cur = 1, idx = n-1; 22 | int [] line = new int[n]; 23 | for (int j = 0; j < n; j++) { 24 | line[j] = readInt(); 25 | } 26 | while (cur<=n && idx>=0) { 27 | if (line[idx]==cur) { 28 | cur++; 29 | idx--; 30 | } 31 | else if (!stk.isEmpty() && stk.peek()==cur) { 32 | cur++; stk.pop(); 33 | } 34 | else { 35 | stk.push(line[idx]); 36 | idx--; 37 | } 38 | 39 | } 40 | boolean can = true; 41 | for (int j = cur; j <= n; j++) { 42 | if (stk.peek()!=j) { 43 | can = false; 44 | break; 45 | } 46 | stk.pop(); 47 | } 48 | System.out.println(can?"Y":"N"); 49 | } 50 | } 51 | 52 | 53 | static String next() throws IOException { 54 | while (st == null || !st.hasMoreTokens()) 55 | st = new StringTokenizer(br.readLine().trim()); 56 | return st.nextToken(); 57 | } 58 | static long readLong() throws IOException { 59 | return Long.parseLong(next()); 60 | } 61 | static int readInt() throws IOException { 62 | return Integer.parseInt(next()); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /CCC12S3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.ArrayList; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | import java.util.StringTokenizer; 8 | /** 9 | * CCC '12 S3 - Absolutely Acidic 10 | * Question type: Implementation 11 | * 50/50 on DMOJ 12 | * Question URL: https://dmoj.ca/problem/ccc12s3 13 | * @author Tommy Pang 14 | */ 15 | public class CCC12S3 { 16 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 17 | static StringTokenizer st; 18 | static int [] frequency = new int[1001]; 19 | static int cnt = 1; 20 | static List highest = new ArrayList<>(), secondHighest = new ArrayList<>(); 21 | public static void main(String[] args) throws IOException{ 22 | int N = Integer.parseInt(br.readLine()); 23 | for (int i = 0; i < N; i++) { 24 | int v = Integer.parseInt(br.readLine()); 25 | frequency[v] += 1; 26 | } 27 | int max = 1; 28 | int fre = 0; 29 | for (int i : frequency) { 30 | max = Math.max(i, max); 31 | } 32 | for (int i : frequency) { 33 | if (i == max) { 34 | highest.add(cnt); 35 | fre+=1; 36 | } 37 | cnt+=1; 38 | } 39 | int ans = 0; 40 | if (fre>=2){ 41 | for (int i : highest) { 42 | for (int j : highest) { 43 | ans = Math.max(i-j, ans); 44 | } 45 | } 46 | System.out.println(ans); 47 | return; 48 | } 49 | int cnt = 1; 50 | int sec_max = 1; 51 | for (int i : frequency) { 52 | if (i == max) continue; 53 | sec_max = Math.max(i, sec_max); 54 | } 55 | for (int i : frequency) { 56 | if (i == sec_max) secondHighest.add(cnt); 57 | cnt+=1; 58 | } 59 | for (int i : highest) { 60 | for (int j : secondHighest) { 61 | ans = Math.max(Math.abs(i-j), ans); 62 | } 63 | } 64 | System.out.println(ans); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /CCC05S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '05 S1 - Snow Calls 9 | * Question type: Implementation 10 | * 1/1 on DMOJ 11 | * @author Tommy Pang 12 | */ 13 | public class CCC05S1 { 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static StringTokenizer st; 16 | static Map map = new HashMap<>(); 17 | public static void main(String[] args) throws IOException{ 18 | map.put("A", 2); map.put("B", 2); map.put("C", 2); 19 | map.put("D", 3); map.put("E", 3); map.put("F", 3); 20 | map.put("G", 4); map.put("H", 4); map.put("I", 4); 21 | map.put("J", 5); map.put("K", 5); map.put("L", 5); 22 | map.put("M", 6); map.put("N", 6); map.put("O", 6); 23 | map.put("P", 7); map.put("Q", 7); map.put("R", 7); map.put("S", 7); 24 | map.put("T", 8); map.put("U", 8); map.put("V", 8); 25 | map.put("W", 9); map.put("X", 9); map.put("Y", 9); map.put("Z", 9); 26 | int N = Integer.parseInt(br.readLine()); 27 | for (int i = 0; i < N; i++) { 28 | String input = br.readLine(); 29 | String [] temp = input.split("-"); 30 | String before = ""; 31 | for (String nxt : temp) { 32 | before+=nxt; 33 | } 34 | String [] a = new String[10]; 35 | for (int j = 0; j < 10; j++) { 36 | if (map.containsKey(String.valueOf(before.charAt(j)))){ 37 | a[j] = String.valueOf(map.get(String.valueOf(before.charAt(j)))); 38 | } 39 | else a[j] = String.valueOf(before.charAt(j)); 40 | } 41 | String ans = ""; 42 | for (int k = 0; k < 3; k++) { 43 | ans+=a[k]; 44 | } 45 | ans+="-"; 46 | for (int j = 3; j < 6; j++) { 47 | ans+=a[j]; 48 | } 49 | ans+="-"; 50 | for (int j = 6; j < 10; j++) { 51 | ans+=a[j]; 52 | } 53 | System.out.println(ans); 54 | } 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /CCC09S4_NaiveWay.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.*; 5 | /** 6 | * CCC '09 S4 - Shop and Ship 7 | * Question type: Graph Theory 8 | * 5/5 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC09S4_NaiveWay { 12 | static StringTokenizer st; 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | //Naive(N^2) way 15 | public static void main(String[] args) throws IOException { 16 | int n = Integer.parseInt(br.readLine()), t = Integer.parseInt(br.readLine()); 17 | int [][] adj = new int [n+1][n+1]; 18 | for (int i=0; i<=n; i++) Arrays.fill(adj[i], Integer.MAX_VALUE/2); 19 | for (int i = 1; i <=t; i++) { 20 | st = new StringTokenizer(br.readLine()); 21 | int u = Integer.parseInt(st.nextToken()), v = Integer.parseInt(st.nextToken()), w = Integer.parseInt(st.nextToken()); 22 | adj[u][v] = adj[v][u] = w; 23 | } 24 | int k = Integer.parseInt(br.readLine()), store [] = new int [k]; 25 | int[] cost = new int [k]; 26 | for (int i = 0; i < k;i++) { 27 | st = new StringTokenizer(br.readLine()); 28 | store[i] = Integer.parseInt(st.nextToken()); cost[i] = Integer.parseInt(st.nextToken()); 29 | } 30 | int dest = Integer.parseInt(br.readLine()), dis []= new int [n+1]; boolean vis[] = new boolean[n+1]; 31 | Arrays.fill(dis, Integer.MAX_VALUE/2); dis[dest] = 0; 32 | for (int i = 1; i <=n; i++) { 33 | int min = Integer.MAX_VALUE, u = -1; 34 | for (int a = 1; a <=n; a++) { 35 | if(!vis[a]&&dis[a] dis[u] + adj[u][v]) dis [v] = dis[u] + adj[u][v]; 44 | } 45 | } 46 | int sol = Integer.MAX_VALUE; 47 | for (int i = 0; i < k; i++) { 48 | sol = Math.min(sol, dis[store[i]]+cost[i]); 49 | } 50 | System.out.println(sol); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /CCC10S3V2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.Arrays; 3 | import java.util.StringTokenizer; 4 | /** 5 | * CCC '10 S3 - Firehose 6 | * Question type: Data Structures, Implementation 7 | * 100/100 on DMOJ 8 | * Question URL: https://dmoj.ca/problem/ccc10s3 9 | * @author Tommy Pang 10 | */ 11 | public class CCC10S3V2 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int H, k, mod = (int) 1e9+7; 16 | static int [] house; 17 | public static void main(String[] args) throws IOException { 18 | H = readInt(); house = new int[2*H]; 19 | for (int i = 0; i < H; i++) { 20 | house[i] = readInt(); 21 | house[i+H] = house[i]+1000000; 22 | } 23 | Arrays.sort(house); 24 | k = readInt(); 25 | int lo = 0, hi = 1000000, ans = Integer.MAX_VALUE; 26 | // binary search for length 27 | while (lo<=hi) { 28 | int mid = (lo+hi)/2; 29 | if (check(mid)) { 30 | hi = mid-1; 31 | ans = mid; 32 | } 33 | else lo = mid+1; 34 | } 35 | System.out.println(ans); 36 | } 37 | static boolean check(int len) { 38 | int min = Integer.MAX_VALUE; 39 | for (int i = 0; i < H; i++) { 40 | int up_bound = house[i]+2*len; 41 | int j, required = 1; 42 | for (j = i+1; j < i+H; j++) { 43 | if (house[j]>up_bound) { 44 | required++; 45 | up_bound = house[j]+2*len; 46 | } 47 | } 48 | min = Math.min(required, min); 49 | } 50 | return min<=k; 51 | } 52 | static String next() throws IOException { 53 | while (st == null || !st.hasMoreTokens()) 54 | st = new StringTokenizer(br.readLine().trim()); 55 | return st.nextToken(); 56 | } 57 | static long readLong() throws IOException { 58 | return Long.parseLong(next()); 59 | } 60 | static int readInt() throws IOException { 61 | return Integer.parseInt(next()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /CCC07S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Arrays; 5 | import java.util.StringTokenizer; 6 | /** 7 | * CCC '07 S2 - Boxes 8 | * Question type: Implementation 9 | * 5/5 on DMOJ 10 | * @author Tommy Pang 11 | */ 12 | public class CCC07S2 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | static int [] sizes; 16 | static String [] s; 17 | public static void main(String[] args) throws IOException { 18 | int N = Integer.parseInt(br.readLine()); 19 | s = new String[N]; sizes = new int[N]; 20 | for (int i = 0; i < N; i++) { 21 | st = new StringTokenizer(br.readLine()); 22 | String a = st.nextToken(), b = st.nextToken(), c = st.nextToken(); 23 | s[i] = a + " " + b + " " + c; 24 | sizes[i] = Integer.parseInt(a) * Integer.parseInt(b) * Integer.parseInt(c); 25 | } 26 | int cases = Integer.parseInt(br.readLine()); 27 | for (int i = 0; i < cases; i++) { 28 | boolean found = false; 29 | st = new StringTokenizer(br.readLine()); 30 | String temp = st.nextToken() + " " + st.nextToken() + " " + st.nextToken(); 31 | int ans = Integer.MAX_VALUE; 32 | for (int j = 0; j < s.length; j++) { 33 | if (check(temp, s[j])){ 34 | ans = Math.min(sizes[j], ans); 35 | found = true; 36 | } 37 | } 38 | if (!found) System.out.println("Item does not fit."); 39 | else System.out.println(ans); 40 | } 41 | } 42 | static boolean check(String a, String b){ 43 | String [] first = a.split(" "); 44 | String [] second = b.split(" "); 45 | int [] temp1 = new int[3]; int [] temp2 = new int[3]; 46 | for (int i = 0; i < 3; i++) { 47 | temp1[i] = Integer.parseInt(first[i]); 48 | temp2[i] = Integer.parseInt(second[i]); 49 | } 50 | Arrays.sort(temp1); Arrays.sort(temp2); 51 | for (int i = 0; i < 3; i++) { 52 | if (temp1[i]>temp2[i]) return false; 53 | } 54 | return true; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /CCC11S5.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '11 S5 - Switch 9 | * Question type: Graph Theory 10 | * 100/100 on DMOJ 11 | * Question URL: https://dmoj.ca/problem/ccc11s5 12 | * @author Tommy Pang 13 | */ 14 | public class CCC11S5 { 15 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | static StringTokenizer st; 17 | 18 | public static void main(String[] args) throws IOException { 19 | int N = Integer.parseInt(br.readLine()); 20 | int mask = 0; 21 | for (int i = 1; i <= N; i++) { 22 | mask<<=1; 23 | mask |= Integer.parseInt(br.readLine()); 24 | } 25 | boolean [] vis = new boolean[1< queue = new LinkedList<>(); 27 | vis[mask] = true; 28 | int dis = 0; 29 | queue.add(mask); 30 | while (!queue.isEmpty()) { 31 | int size = queue.size(); 32 | for (int i = 0; i < size; i++) { 33 | int cur_mask = queue.poll(); 34 | if (cur_mask==0){ 35 | System.out.println(dis); 36 | return; 37 | } 38 | for (int j = 0; j < N; j++) { 39 | if ((cur_mask & 1 << j) == 0) { 40 | int new_mask = cur_mask | (1 << j); 41 | new_mask = solve(new_mask, N); 42 | if (!vis[new_mask]) { 43 | queue.add(new_mask); 44 | vis[new_mask] = true; 45 | } 46 | } 47 | } 48 | } 49 | dis += 1; 50 | } 51 | } 52 | static int solve(int mask, int n){ 53 | int cnt = 0; 54 | for (int i = 0; i <= n; i++) { 55 | if ((mask & 1<=4){ 60 | int new_mask = ((1< temp = new ArrayList<>(); 15 | 16 | public static void main(String[] args) throws IOException { 17 | int N = Integer.parseInt(br.readLine()); 18 | for (int i = 0; i < N; i++) { 19 | result[i] = Integer.parseInt(br.readLine()); 20 | } 21 | int operation = Integer.parseInt(br.readLine()); 22 | while (operation != 77) { 23 | babblingAlg(operation); 24 | operation = Integer.parseInt(br.readLine()); 25 | } 26 | for (int nxt : result) { 27 | if (nxt != 0) System.out.print(nxt+" "); 28 | } 29 | } 30 | 31 | static void babblingAlg(int operation) throws IOException { 32 | switch (operation) { 33 | case (99): 34 | int idx = Integer.parseInt(br.readLine()); 35 | int percentage = Integer.parseInt(br.readLine()); 36 | split(idx - 1, percentage); 37 | break; 38 | case (88): 39 | idx = Integer.parseInt(br.readLine()); 40 | join(idx - 1); 41 | break; 42 | default: 43 | break; 44 | } 45 | 46 | } 47 | 48 | private static void split(int idx, int percentage) { 49 | int[] suf = result.clone(); 50 | if (percentage == 0) return; 51 | int left = Math.round(result[idx] * ((float) percentage / 100)); 52 | int right = Math.round(result[idx] * (float) (100 - percentage) / 100); 53 | result[idx] = left; 54 | result[idx + 1] = right; 55 | for (int i = idx + 2; i < MAX; i++) { 56 | result[i] = suf[i - 1]; 57 | } 58 | } 59 | 60 | private static void join(int idx) { 61 | int[] suf = result.clone(); 62 | result[idx] = result[idx] + result[idx + 1]; 63 | for (int i = idx + 1; i < MAX-1; i++) { 64 | result[i] = suf[i + 1]; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /CCC13S5.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '13 S5 - Factor Solitaire 5 | * Question type: Greedy Algorithms, Intermediate Math 6 | * 150/150 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc13s5 8 | * @author Tommy Pang 9 | */ 10 | 11 | public class CCC13S5 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int mod = (int) 1e9+7, n, ans = Integer.MAX_VALUE; 16 | public static void main(String[] args) throws IOException { 17 | n = readInt(); 18 | dfs(n, 0); 19 | System.out.println(ans); 20 | } 21 | public static void dfs(int v, int cost) { 22 | if (v==1) { 23 | ans = Math.min(cost, ans); 24 | return; 25 | } 26 | if (v<0 || cost>ans) return; 27 | int half = v/2; 28 | for (int i = half; i >= 1; i--) { 29 | int c = v-i; 30 | if (c%i==0) { 31 | dfs(v - i, cost + (c / i)); 32 | break; 33 | } 34 | } 35 | } 36 | 37 | static String next() throws IOException { 38 | while (st == null || !st.hasMoreTokens()) 39 | st = new StringTokenizer(br.readLine().trim()); 40 | return st.nextToken(); 41 | } 42 | static long readLong() throws IOException { 43 | return Long.parseLong(next()); 44 | } 45 | static int readInt() throws IOException { 46 | return Integer.parseInt(next()); 47 | } 48 | static double readDouble() throws IOException { 49 | return Double.parseDouble(next()); 50 | } 51 | static char readCharacter() throws IOException { 52 | return next().charAt(0); 53 | } 54 | static String readLine() throws IOException { 55 | return br.readLine().trim(); 56 | } 57 | static int readLongLineInt() throws IOException{ 58 | int x = 0, c; 59 | while((c = br.read()) != ' ' && c != '\n') 60 | x = x * 10 + (c - '0'); 61 | return x; 62 | } 63 | static long pow (long x, long exp){ 64 | if (exp==0) return 1; 65 | long t = pow(x, exp/2); 66 | t = t*t %mod; 67 | if (exp%2 == 0) return t; 68 | return t*x%mod; 69 | } 70 | static long lcm(long a, long b) { 71 | return (a / gcd(a, b)) * b; 72 | } 73 | static long gcd(long a, long b) { 74 | if (b == 0) return a; 75 | return gcd(b, a % b); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /CCC20S4.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '20 S4 - Swapping Seats 5 | * Question type: Greedy Algorithms 6 | * 15/15 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc20s4 8 | * @author Tommy Pang 9 | */ 10 | public class CCC20S4 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, n; 15 | static int[] A, B, C; 16 | static int cntA, cntB, cntC, ans= Integer.MAX_VALUE; 17 | public static void main(String[] args) throws IOException { 18 | String s = readLine(); n = s.length(); 19 | A = new int[n+1]; B = new int[n+1]; C = new int[n+1]; 20 | for (int i = 1; i <= n; i++) { 21 | A[i] = A[i-1]; B[i] = B[i-1]; C[i] = C[i-1]; 22 | char c = s.charAt(i-1); 23 | if (c=='A') A[i]++; 24 | else if (c=='B') B[i]++; 25 | else C[i]++; 26 | } 27 | cntA = A[n]; cntB = B[n]; cntC = C[n]; 28 | for (int l = 1; l <= n; l++) { 29 | if (l>=cntA+cntB) { 30 | compute(A, B, l); compute(B, A, l); 31 | } 32 | if (l>=cntA+cntC) { 33 | compute(A, C, l); compute(C, A, l); 34 | } 35 | if (l>=cntB+cntC) { 36 | compute(B, C, l); compute(C, B, l); 37 | } 38 | } 39 | System.out.println(ans); 40 | } 41 | public static void compute(int[] x, int[] y, int i) { 42 | int nx = x[n], ny = y[n]; 43 | int sum = ny - (y[i]-y[i-ny]) + nx - (x[i-ny] - x[i-ny-nx]); 44 | int both = Math.min(y[i-ny] - y[i-ny-nx], x[i] - x[i-ny]); 45 | ans = Math.min(sum-both, ans); 46 | } 47 | 48 | static String next() throws IOException { 49 | while (st == null || !st.hasMoreTokens()) 50 | st = new StringTokenizer(br.readLine().trim()); 51 | return st.nextToken(); 52 | } 53 | static long readLong() throws IOException { 54 | return Long.parseLong(next()); 55 | } 56 | static int readInt() throws IOException { 57 | return Integer.parseInt(next()); 58 | } 59 | static double readDouble() throws IOException { 60 | return Double.parseDouble(next()); 61 | } 62 | static char readCharacter() throws IOException { 63 | return next().charAt(0); 64 | } 65 | static String readLine() throws IOException { 66 | return br.readLine().trim(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /CCC03S3.cpp: -------------------------------------------------------------------------------- 1 | // Problem: CCC '03 S3 - Floor Plan 2 | // Contest: DMOJ 3 | // URL: https://dmoj.ca/problem/ccc03s3 4 | // Memory Limit: 64 MB 5 | // Time Limit: 1000 ms 6 | // Ryan Liu 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define pb push_back 12 | 13 | int m, w, l, x, y, tmr, numr = 0; 14 | 15 | vector roomarea; 16 | queue q; 17 | bool used[25][25]; 18 | char mc[25][25]; 19 | string t; 20 | 21 | void bfs(int b, int a) { 22 | tmr = 1; 23 | q.push(a); 24 | q.push(b); 25 | used[b][a] = true; 26 | while (!q.empty()) { 27 | int v1 = q.front(); 28 | q.pop(); 29 | int v2 = q.front(); 30 | q.pop(); 31 | 32 | /*for (int u : adj[v]) { 33 | if (!used[u]) { 34 | used[u] = true; 35 | q.push(u); 36 | dist[u] = dist[v] + 1; 37 | } 38 | 39 | }*/ 40 | if ((v2 - 1 >= 0) && (!used[v2 - 1][v1]) && mc[v2 - 1][v1] == '.') { 41 | used[v2 - 1][v1] = true; 42 | q.push(v1); 43 | q.push(v2 - 1); 44 | ++tmr; 45 | } 46 | if ((v2 + 1 < l) && (!used[v2 + 1][v1]) && mc[v2 + 1][v1] == '.') { 47 | used[v2 + 1][v1] = true; 48 | q.push(v1); 49 | q.push(v2 + 1); 50 | ++tmr; 51 | } 52 | if ((v1 - 1 >= 0) && (!used[v2][v1 - 1]) && mc[v2][v1 - 1] == '.') { 53 | used[v2][v1 - 1] = true; 54 | q.push(v1 - 1); 55 | q.push(v2); 56 | ++tmr; 57 | } 58 | if ((v1 + 1 < w) && (!used[v2][v1 + 1]) && mc[v2][v1 + 1] == '.') { 59 | used[v2][v1 + 1] = true; 60 | q.push(v1 + 1); 61 | q.push(v2); 62 | ++tmr; 63 | } 64 | } 65 | roomarea.pb(tmr); 66 | } 67 | 68 | int main() { 69 | ios_base::sync_with_stdio(0); 70 | cin.tie(0); 71 | cout.tie(0); 72 | memset(used, false, sizeof(used)); 73 | 74 | cin >> m >> l >> w; 75 | 76 | // get input 77 | for (int i = 0; i < l; i++) { 78 | for (int j = 0; j < w; j++) { 79 | cin >> mc[i][j]; 80 | } 81 | } 82 | 83 | for (int i = 0; i < l; i++) { 84 | for (int j = 0; j < w; j++) { 85 | if (mc[i][j] == '.' && !used[i][j]) { 86 | bfs(i, j); 87 | } 88 | } 89 | } 90 | 91 | sort(roomarea.rbegin(), roomarea.rend()); 92 | 93 | for (int i = 0; i < roomarea.size(); i++) { 94 | if ((m - roomarea[i]) >= 0) { 95 | m -= roomarea[i]; 96 | numr++; 97 | 98 | } else { 99 | break; 100 | } 101 | } 102 | 103 | if (numr == 1) { 104 | cout << 1 << " room, " << m << " square metre(s) left over" 105 | << "\n"; 106 | return 0; 107 | } 108 | 109 | cout << numr << " rooms, " << m << " square metre(s) left over" 110 | << "\n"; 111 | } 112 | -------------------------------------------------------------------------------- /CCC09S2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.HashSet; 3 | import java.util.Set; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '09 S2 - Lights Going On and Off 7 | * Question type: Implementation 8 | * 5/5 on DMOJ 9 | * @author Tommy Pang 10 | */ 11 | public class CCC09S2 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int mod = (int) 1e9+7, R, L; 16 | public static void main(String[] args) throws IOException { 17 | R = readInt(); L = readInt(); 18 | Set cur = new HashSet<>(); 19 | Set nxt = new HashSet<>(); 20 | int cur_mask; 21 | for (int i = 0; i < R; i++) { 22 | cur_mask = 0; 23 | for (int j = 0; j < L; j++) { 24 | cur_mask = (cur_mask<<1) | readInt(); 25 | } 26 | nxt.add(cur_mask); 27 | for (int nxt_mask : cur) { 28 | nxt.add(cur_mask^nxt_mask); 29 | } 30 | cur = nxt; 31 | nxt = new HashSet<>(); 32 | } 33 | System.out.println(cur.size()); 34 | } 35 | 36 | static String next() throws IOException { 37 | while (st == null || !st.hasMoreTokens()) 38 | st = new StringTokenizer(br.readLine().trim()); 39 | return st.nextToken(); 40 | } 41 | static long readLong() throws IOException { 42 | return Long.parseLong(next()); 43 | } 44 | static int readInt() throws IOException { 45 | return Integer.parseInt(next()); 46 | } 47 | static double readDouble() throws IOException { 48 | return Double.parseDouble(next()); 49 | } 50 | static char readCharacter() throws IOException { 51 | return next().charAt(0); 52 | } 53 | static String readLine() throws IOException { 54 | return br.readLine().trim(); 55 | } 56 | static int readLongLineInt() throws IOException{ 57 | int x = 0, c; 58 | while((c = br.read()) != ' ' && c != '\n') 59 | x = x * 10 + (c - '0'); 60 | return x; 61 | } 62 | static long pow (long x, long exp){ 63 | if (exp==0) return 1; 64 | long t = pow(x, exp/2); 65 | t = t*t %mod; 66 | if (exp%2 == 0) return t; 67 | return t*x%mod; 68 | } 69 | static long lcm(long a, long b) { 70 | return (a / gcd(a, b)) * b; 71 | } 72 | static long gcd(long a, long b) { 73 | if (b == 0) return a; 74 | return gcd(b, a % b); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /CCC18S2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '18 S2 - Sunflowers 7 | * Question type: Implementation 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc18s2 10 | * @author Tommy Pang 11 | */ 12 | public class CCC18S2 { 13 | static StringTokenizer st; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static int [][] table, sectable; 16 | static int N; 17 | public static void main(String[] args) throws IOException{ 18 | N = Integer.parseInt(br.readLine()); 19 | table = new int[N][N]; 20 | sectable = new int[N][N]; 21 | for (int i = 0; i < N; i++) { 22 | st = new StringTokenizer(br.readLine()); 23 | for (int j = 0; j < N; j++) { 24 | table[i][j] = Integer.parseInt(st.nextToken()); 25 | } 26 | } 27 | if (validateRow(table) && validateCol(table)) { 28 | printTable(table); 29 | return; 30 | } 31 | else { 32 | rotate(sectable, table); 33 | } 34 | if (validateRow(sectable) && validateCol(sectable)) { 35 | printTable(sectable); 36 | return; 37 | } 38 | else { 39 | rotate(table, sectable); 40 | } 41 | if (validateRow(table) && validateCol(table)) { 42 | printTable(table); 43 | return; 44 | } 45 | else { 46 | rotate(sectable, table); 47 | } 48 | if (validateRow(sectable) && validateCol(sectable)) printTable(sectable); 49 | } 50 | static void rotate(int [][] toFill, int [][] original){ 51 | for (int i = 0; i < N; i++) { 52 | for (int j = 0; j < N; j++) { 53 | toFill[j][N-1-i] = original[i][j]; 54 | } 55 | } 56 | } 57 | static boolean validateRow(int [][] t){ 58 | for (int i = 0; i < N; i++) { 59 | for (int j = 1; j < N; j++) { 60 | if (t[i][j] < t[i][j-1]) return false; 61 | } 62 | } 63 | return true; 64 | } 65 | static boolean validateCol(int [][] t){ 66 | for (int i = 0; i < N; i++) { 67 | for (int j = 1; j < N; j++) { 68 | if (t[j][i] < t[j-1][i]) return false; 69 | } 70 | } 71 | return true; 72 | } 73 | static void printTable(int [][] t){ 74 | for (int i = 0; i < N; i++) { 75 | for (int j = 0; j < N; j++) { 76 | System.out.print(t[i][j] + " "); 77 | } 78 | System.out.println(); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /CCC05S4Way2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '05 S4 - Pyramid Message Scheme 5 | * Question type: Graph Theory 6 | * 1/1 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc05s4 8 | * @author Tommy Pang 9 | */ 10 | public class CCC05S4Way2 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7; 15 | static int L, n, total, max_depth = 0; 16 | static Map> adj = new HashMap<>(); 17 | public static void main(String[] args) throws IOException { 18 | L = readInt(); 19 | for (int i = 0; i < L; i++) { 20 | n = readInt(); 21 | max_depth = 0; 22 | adj = new HashMap<>(); 23 | total = n*10; 24 | String [] names = new String[n]; 25 | for (int j = 0; j < n; j++) { 26 | names[j] = readLine(); 27 | } 28 | Stack stk = new Stack<>(); 29 | stk.push(names[n-1]); 30 | for (int j = 0; j < n; j++) { 31 | String nxt = names[j]; 32 | if (stk.contains(nxt)) { 33 | while (!stk.peek().equals(nxt)) stk.pop(); 34 | } 35 | else { 36 | adj.putIfAbsent(stk.peek(), new ArrayList<>()); 37 | adj.get(stk.peek()).add(nxt); 38 | } 39 | if (!stk.peek().equals(nxt)) stk.push(nxt); 40 | } 41 | dfs(names[n-1], 0); 42 | System.out.println(total-20*max_depth); 43 | } 44 | } 45 | public static void dfs(String cur, int dep) { 46 | max_depth = Math.max(dep, max_depth); 47 | if (!adj.containsKey(cur)) return; 48 | for (String nxt : adj.get(cur)) { 49 | dfs(nxt, dep+1); 50 | } 51 | } 52 | 53 | static String next() throws IOException { 54 | while (st == null || !st.hasMoreTokens()) 55 | st = new StringTokenizer(br.readLine().trim()); 56 | return st.nextToken(); 57 | } 58 | static long readLong() throws IOException { 59 | return Long.parseLong(next()); 60 | } 61 | static int readInt() throws IOException { 62 | return Integer.parseInt(next()); 63 | } 64 | static double readDouble() throws IOException { 65 | return Double.parseDouble(next()); 66 | } 67 | static char readCharacter() throws IOException { 68 | return next().charAt(0); 69 | } 70 | static String readLine() throws IOException { 71 | return br.readLine().trim(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /CCC20S3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '20 S3 - Searching for Strings 5 | * Question type: Data Structures, String Algorithms 6 | * 20/20 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc20s3 8 | * @author Tommy Pang 9 | */ 10 | public class CCC20S3 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static long [] hsh, hsh2, pw, pw2; 15 | static int mod = (int) 1e9 + 7; 16 | public static void main(String[] args) throws IOException{ 17 | String N = readLine(), H = readLine(); 18 | int n = N.length(); int m = H.length(); 19 | hsh = new long[m+1]; hsh2 = new long[m+1]; 20 | pw = new long[m+1]; pw2 = new long[m+1]; 21 | hsh[0] = 0; hsh2[0] = 0; 22 | pw[0] = 1; pw2[0] = 1; 23 | int base = 131, base2 = 137; 24 | int [] freq = new int[26], freq2 = new int[26]; 25 | for (int i = 0; i < n; i++) { 26 | freq[N.charAt(i)-'a']++; 27 | } 28 | for (int i = 1; i <= m; i++) { 29 | hsh[i] = (hsh[i-1]*base + H.charAt(i-1))%mod; 30 | pw[i] = pw[i-1]*base % mod; 31 | hsh2[i] = (hsh2[i-1]*base2 + H.charAt(i-1))%mod; 32 | pw2[i] = pw2[i-1]*base2 % mod; 33 | } 34 | Set set = new HashSet<>(); 35 | for (int i = 1; i <= m; i++) { 36 | freq2[H.charAt(i-1)-'a'] ++; 37 | if (i>n) freq2[H.charAt(i-n-1)-'a']--; 38 | if (Arrays.equals(freq, freq2)) { 39 | set.add(getSubstrHash(i - n - 1, i)); 40 | } 41 | } 42 | System.out.println(set.size()); 43 | } 44 | static long getSubstrHash(int l, int r){ 45 | long h1 = (hsh[r] - hsh[l+1] * pw[r-l-1] % mod + mod)%mod, h2 = (hsh2[r] - hsh2[l+1] * pw2[r-l-1] % mod + mod)%mod; 46 | return (h1<<31) | h2; 47 | } 48 | static String next() throws IOException { 49 | while (st == null || !st.hasMoreTokens()) 50 | st = new StringTokenizer(br.readLine().trim()); 51 | return st.nextToken(); 52 | } 53 | static long readLong() throws IOException { 54 | return Long.parseLong(next()); 55 | } 56 | static int readInt() throws IOException { 57 | return Integer.parseInt(next()); 58 | } 59 | static double readDouble() throws IOException { 60 | return Double.parseDouble(next()); 61 | } 62 | static char readCharacter() throws IOException { 63 | return next().charAt(0); 64 | } 65 | static String readLine() throws IOException { 66 | return br.readLine().trim(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /CCC09S3.cpp: -------------------------------------------------------------------------------- 1 | // Problem: CCC '09 S3 - Degrees Of Separation 2 | // Contest: DMOJ 3 | // URL: https://dmoj.ca/problem/ccc09s3 4 | // Memory Limit: 64 MB 5 | // Time Limit: 1000 ms 6 | // Ryan Liu 7 | 8 | #include 9 | using namespace std; 10 | 11 | typedef long long ll; 12 | typedef vector vi; 13 | typedef pair pp; 14 | #define mp make_pair 15 | #define pb push_back 16 | 17 | vector adj[51]; 18 | queue q; 19 | bool used[51]; 20 | int dist[51]; 21 | string t; 22 | int x, y, sum; 23 | 24 | void bfs(int x) { 25 | q.push(x); 26 | used[x] = true; 27 | while (!q.empty()) { 28 | int v = q.front(); 29 | q.pop(); 30 | for (int u : adj[v]) { 31 | if (!used[u]) { 32 | dist[u] = dist[v] + 1; 33 | used[u] = true; 34 | q.push(u); 35 | } 36 | } 37 | } 38 | } 39 | void ae(int a, int b) { 40 | adj[a].pb(b); 41 | adj[b].pb(a); 42 | } 43 | 44 | void ifunc() { 45 | cin >> x >> y; 46 | 47 | if (!(find(adj[x].begin(), adj[x].end(), y) != adj[x].end())) { 48 | adj[x].pb(y); 49 | adj[y].pb(x); 50 | } 51 | } 52 | void dfunc() { 53 | cin >> x >> y; 54 | adj[x].erase(std::remove(adj[x].begin(), adj[x].end(), y), adj[x].end()); 55 | adj[y].erase(std::remove(adj[y].begin(), adj[y].end(), x), adj[y].end()); 56 | } 57 | void nfunc() { 58 | cin >> x; 59 | cout << adj[x].size() << "\n"; 60 | } 61 | void ffunc() { 62 | memset(used, false, sizeof(used)); 63 | memset(dist, 0, sizeof(dist)); 64 | cin >> x; 65 | sum = 0; 66 | bfs(x); 67 | for (int i = 1; i <= 50; i++) { 68 | if (dist[i] == 2) { 69 | sum++; 70 | } 71 | } 72 | 73 | cout << sum << "\n"; 74 | } 75 | void sfunc() { 76 | cin >> x >> y; 77 | memset(used, false, sizeof(used)); 78 | memset(dist, 0, sizeof(dist)); 79 | 80 | bfs(x); 81 | 82 | cout << ((used[y]) ? to_string(dist[y]) : "Not connected") << "\n"; 83 | } 84 | 85 | int main() { 86 | ios_base::sync_with_stdio(0); 87 | cin.tie(0); 88 | cout.tie(0); 89 | 90 | ae(2, 6); 91 | ae(1, 6); 92 | 93 | ae(3, 6); 94 | ae(4, 6); 95 | ae(5, 6); 96 | ae(7, 6); 97 | 98 | ae(3, 4); 99 | ae(5, 4); 100 | ae(3, 5); 101 | 102 | ae(3, 15); 103 | ae(15, 13); 104 | ae(13, 14); 105 | 106 | ae(13, 12); 107 | ae(11, 12); 108 | ae(9, 12); 109 | ae(11, 10); 110 | ae(9, 10); 111 | ae(9, 8); 112 | ae(7, 8); 113 | 114 | ae(17, 16); 115 | ae(18, 16); 116 | ae(17, 18); 117 | while (cin >> t && (t != "q")) { 118 | if (t == "i") { 119 | ifunc(); 120 | } else if (t == "d") { 121 | dfunc(); 122 | } else if (t == "n") { 123 | nfunc(); 124 | 125 | } else if (t == "f") { 126 | ffunc(); 127 | } else if (t == "s") { 128 | sfunc(); 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /CCC01S3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '01 S3 - Strategic Bombing 5 | * Question type: Graph Theory 6 | * 5/5 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC01S3 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | static List[] adj = new ArrayList[28]; 13 | static int cnt = 0; 14 | static Map map = new HashMap<>(); 15 | static boolean [] visited = new boolean[30]; 16 | static List [] roads = new ArrayList[200]; 17 | static int total = 0; 18 | public static void main(String[] args) throws IOException { 19 | map.put("A", 1);map.put("B", 2);map.put("C", 3);map.put("D", 4);map.put("E", 5);map.put("F", 6);map.put("G", 7); 20 | map.put("H", 8);map.put("I", 9);map.put("J", 10);map.put("K", 11);map.put("L", 12);map.put("M", 13);map.put("N", 14); 21 | map.put("O", 15);map.put("P", 16);map.put("Q", 17);map.put("R", 18);map.put("S", 19);map.put("T", 20);map.put("U", 21); 22 | map.put("V", 22);map.put("W", 23);map.put("X", 24);map.put("Y", 25);map.put("Z", 26); 23 | for (int i = 0; i <= 27; i++) {adj[i] = new ArrayList<>(); } 24 | for (int i = 0; i <= 199; i++) {roads[i] = new ArrayList<>(); } 25 | while(true){ 26 | st = new StringTokenizer(br.readLine()); 27 | String line = st.nextToken(); 28 | if (line.equals("**")) break; 29 | String a = String.valueOf(line.charAt(0)); 30 | String b = String.valueOf(line.charAt(1)); 31 | adj[map.get(a)].add(b); adj[map.get(b)].add(a); 32 | roads[cnt].add(a); roads[cnt].add(b); 33 | cnt+=1; 34 | } 35 | Queue queue = new LinkedList<>(); 36 | for (int i = 0; i < cnt; i++) { 37 | queue.add("A"); visited[1] = true; 38 | while (!queue.isEmpty()){ 39 | String cur = queue.poll(); 40 | for (String nxt : adj[map.get(cur)]) { 41 | if (roads[i].contains(cur) && roads[i].contains(nxt)); 42 | else { 43 | if (!visited[map.get(nxt)]) { 44 | queue.add(nxt); 45 | visited[map.get(nxt)] = true; 46 | } 47 | } 48 | } 49 | } 50 | if (!visited[map.get("B")]) { 51 | for (String v : roads[i]) { 52 | System.out.print(v); 53 | } 54 | System.out.println(); 55 | total+=1; 56 | } 57 | Arrays.fill(visited, false); 58 | } 59 | System.out.print("There are " + total+" disconnecting roads.\n"); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /CCC19S5.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.Arrays; 3 | import java.util.StringTokenizer; 4 | /** 5 | * CCC '19 S5 - Triangle: The Data Structure 6 | * Question type: Data Structures 7 | * 15/15 on DMOJ 8 | * Question URL: https://dmoj.ca/problem/ccc19s5 9 | * @author Tommy Pang 10 | */ 11 | public class CCC19S5 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int mod = (int) 1e9+7; 16 | public static void main(String[] args) throws IOException{ 17 | int N = readInt(), K =readInt(), a[][] = new int[N+1][N+1]; 18 | for(int i=1; i<=N; i++) 19 | for(int j=1; j<=i; j++) 20 | a[i][j] = readInt(); 21 | int cur = 0, nxt = 0; 22 | for(cur=1, nxt=2; nxt <= K; cur=nxt, nxt=(int)(1.5*cur)){ 23 | int d = nxt - cur; 24 | for(int i=1; i<=N-nxt+1; i++){ 25 | for(int j=1; j<=i; j++){ 26 | a[i][j] = Math.max(a[i][j], Math.max(a[i+d][j], a[i+d][j+d])); 27 | } 28 | } 29 | } 30 | int d = K - cur; long ans = 0; 31 | for(int i=1; i<=N-K+1; i++) 32 | for(int j=1; j<=i; j++) 33 | ans += Math.max(a[i][j], Math.max(a[i+d][j], a[i+d][j+d])); 34 | System.out.println(ans); 35 | } 36 | static String next() throws IOException { 37 | while (st == null || !st.hasMoreTokens()) 38 | st = new StringTokenizer(br.readLine().trim()); 39 | return st.nextToken(); 40 | } 41 | static long readLong() throws IOException { 42 | return Long.parseLong(next()); 43 | } 44 | static int readInt() throws IOException { 45 | return Integer.parseInt(next()); 46 | } 47 | static double readDouble() throws IOException { 48 | return Double.parseDouble(next()); 49 | } 50 | static char readCharacter() throws IOException { 51 | return next().charAt(0); 52 | } 53 | static String readLine() throws IOException { 54 | return br.readLine().trim(); 55 | } 56 | static int readLongLineInt() throws IOException{ 57 | int x = 0, c; 58 | while((c = br.read()) != ' ' && c != '\n') 59 | x = x * 10 + (c - '0'); 60 | return x; 61 | } 62 | static long pow (long x, long exp){ 63 | if (exp==0) return 1; 64 | long t = pow(x, exp/2); 65 | t = t*t %mod; 66 | if (exp%2 == 0) return t; 67 | return t*x%mod; 68 | } 69 | static long lcm(long a, long b) { 70 | return (a / gcd(a, b)) * b; 71 | } 72 | static long gcd(long a, long b) { 73 | if (b == 0) return a; 74 | return gcd(b, a % b); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /CCC21S4.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '21 S4 - Daily Commute 5 | * Question type: Graph Theory 6 | * 7/15 on DMOJ, TLE on Batch 5 7 | * Question URL: https://dmoj.ca/problem/ccc21s4 8 | * @author Tommy Pang 9 | */ 10 | public class CCC21S4 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, MM = 200005, N, W, D; 15 | static Map[] disToDest = new Map[MM]; 16 | static int[] permutation = new int[MM]; 17 | static List [] walk = new List[MM]; 18 | static { 19 | for (int i = 0; i < MM; i++) walk[i] = new ArrayList<>(); 20 | for (int i = 0; i < MM; i++) disToDest[i] = new HashMap<>(); 21 | } 22 | public static void main(String[] args) throws IOException { 23 | N = readInt(); W = readInt(); D = readInt(); 24 | for (int i = 0; i < W; i++) walk[readInt()].add(readInt()); 25 | for (int i = 1; i <= N; i++) permutation[i] = readInt(); 26 | for (int i = 1; i <= N ; i++) { 27 | BFS(i); 28 | } 29 | for (int i = 0; i < D; i++) { 30 | int fir = readInt(), sec = readInt(); 31 | int temp = permutation[fir]; 32 | permutation[fir] = permutation[sec]; 33 | permutation[sec] = temp; 34 | int ans = Integer.MAX_VALUE; 35 | for (int j = 1; j <= N; j++) { 36 | if (permutation[j]==N) { 37 | ans = Math.min(ans, j-1); 38 | } 39 | else ans = Math.min(ans, j-1+disToDest[permutation[j]].getOrDefault(N, Integer.MAX_VALUE/2)); 40 | } 41 | System.out.println(ans); 42 | } 43 | 44 | } 45 | public static void BFS(int i) { 46 | Queue queue = new LinkedList<>(); 47 | queue.add(i); disToDest[i].put(i, 0); 48 | while (!queue.isEmpty()) { 49 | int cur = queue.poll(); 50 | for (int nxt : walk[cur]) { 51 | if (disToDest[i].get(cur)+1 [] adj; 17 | static int [][] dis = new int[2002][202]; 18 | public static void main(String[] args) throws IOException { 19 | st = new StringTokenizer(br.readLine()); 20 | K = Integer.parseInt(st.nextToken()); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); 21 | adj = new ArrayList[N+1]; 22 | for (int i = 1; i < N+1; i++) { 23 | adj[i] = new ArrayList<>(); 24 | } 25 | for (int i = 0; i < M; i++) { 26 | st = new StringTokenizer(br.readLine()); 27 | int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken()); 28 | int t = Integer.parseInt(st.nextToken()), h = Integer.parseInt(st.nextToken()); 29 | adj[a].add(new edge(b, t, h)); 30 | adj[b].add(new edge(a, t, h)); 31 | } 32 | for (int i = 0; i < 2002; i++) { 33 | for (int j = 0; j < 202; j++) { 34 | dis[i][j] = Integer.MAX_VALUE; 35 | } 36 | } 37 | st = new StringTokenizer(br.readLine()); 38 | int A = Integer.parseInt(st.nextToken()), B = Integer.parseInt(st.nextToken()); 39 | PriorityQueue queue = new PriorityQueue<>(); 40 | queue.add(new edge(A, 0, 0)); 41 | dis[A][0] = 0; 42 | while (!queue.isEmpty()){ 43 | edge e = queue.poll(); 44 | if (e.t>dis[e.b][e.h]) continue; 45 | for (edge nxt : adj[e.b]) { 46 | if (nxt.h+e.h>=K) continue; 47 | if (dis[nxt.b][nxt.h+e.h]>e.t+nxt.t){ 48 | dis[nxt.b][nxt.h+e.h] = e.t+nxt.t; 49 | queue.add(new edge(nxt.b, dis[nxt.b][nxt.h+ e.h], nxt.h+e.h)); 50 | } 51 | } 52 | } 53 | int ans = Integer.MAX_VALUE; 54 | for(int k=0; k{ 60 | int b, t, h; 61 | edge(int end, int time, int height){ 62 | b = end; t = time; h = height; 63 | } 64 | 65 | @Override 66 | public int compareTo(edge o) { 67 | return Integer.compare(t, o.t); 68 | } 69 | 70 | } 71 | 72 | 73 | } 74 | -------------------------------------------------------------------------------- /CCC09S4_PriorityQueue.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.*; 5 | /** 6 | * CCC '09 S4 - Shop and Ship 7 | * Question type: Graph Theory 8 | * 3/5 on DMOJ, case 4, 5 tle 9 | * @author Tommy Pang 10 | */ 11 | public class CCC09S4_PriorityQueue { 12 | static StringTokenizer st; 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static List[] adj; 15 | static int [] prices, dis; 16 | static boolean [] vis; 17 | //Priority Queue way 18 | public static void main(String[] args) throws IOException { 19 | int N = Integer.parseInt(br.readLine()); 20 | adj = new ArrayList[N+1]; prices = new int[N+1]; dis = new int[N+1]; 21 | vis = new boolean[N+1]; 22 | Arrays.fill(prices, -1); Arrays.fill(dis, Integer.MAX_VALUE); 23 | for (int i = 0; i <= N; i++) adj[i] = new ArrayList<>(); 24 | int T = Integer.parseInt(br.readLine()); 25 | for (int i = 0; i < T; i++) { 26 | st = new StringTokenizer(br.readLine()); 27 | int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken()), c = Integer.parseInt(st.nextToken()); 28 | adj[a].add(new edge(b, c)); adj[b].add(new edge(a, c)); 29 | } 30 | int K = Integer.parseInt(br.readLine()); 31 | for (int i = 0; i < K; i++) { 32 | st = new StringTokenizer(br.readLine()); 33 | prices[Integer.parseInt(st.nextToken())] = Integer.parseInt(st.nextToken()); 34 | } 35 | int D = Integer.parseInt(br.readLine()); 36 | Dijkstra(D); 37 | int ans = Integer.MAX_VALUE; 38 | for (int i = 1; i <= N; i++) { 39 | if (vis[i] && prices[i] != -1){ 40 | ans = Math.min(ans, prices[i] + dis[i]); 41 | } 42 | } 43 | System.out.println(ans); 44 | } 45 | static void Dijkstra(int i){ 46 | PriorityQueue queue = new PriorityQueue<>(); 47 | queue.add(new edge(i, 0)); dis[i] = 0; 48 | while (!queue.isEmpty()){ 49 | edge e = queue.poll(); 50 | int b = e.b, cost = e.c; 51 | vis[b] = true; 52 | if (cost>dis[e.b]) continue; 53 | for (edge nxt : adj[b]) { 54 | if (dis[nxt.b] > dis[b] + nxt.c){ 55 | dis[nxt.b] = dis[b] + nxt.c; 56 | queue.add(new edge(nxt.b, dis[nxt.b])); 57 | } 58 | } 59 | } 60 | } 61 | public static class edge implements Comparable{ 62 | int b, c; 63 | edge(int end, int cost){ 64 | b = end; c = cost; 65 | } 66 | 67 | @Override 68 | public int compareTo(edge e) { 69 | return Integer.compare(c, e.c); 70 | } 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /CCC11S3V2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.StringTokenizer; 3 | /** 4 | * CCC '11 S3 - Alice Through the Looking Glass 5 | * Question type: Recursion 6 | * 50/50 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc11s3 8 | * @author Tommy Pang 9 | */ 10 | public class CCC11S3V2 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7; 15 | public static void main(String[] args) throws IOException { 16 | int T = readInt(); 17 | for (int i = 0; i < T; i++) { 18 | int m = readInt(), x = readInt(), y = readInt(); 19 | if (recursion(x, y, m)) System.out.println("crystal"); 20 | else System.out.println("empty"); 21 | } 22 | } 23 | public static boolean recursion(int x, int y, int level) { 24 | if (level==1) { 25 | if (y==0 && (x==1 || x==2 || x==3)) return true; 26 | else if (y==1 && x==2) return true; 27 | else return false; 28 | } 29 | int scale = (int) Math.pow(5, level-1); 30 | int X = x/scale, Y = y/scale; 31 | if (X<1 || X>3 || Y>2) return false; 32 | else if (Y==0) return true; // bottom 3 boxes 33 | else if (X==2 && Y==1) return true; // the forth box above 34 | int lX = scale*X, lY = scale*Y; 35 | return recursion((x-lX), (y-lY), level-1); 36 | } 37 | 38 | 39 | static String next() throws IOException { 40 | while (st == null || !st.hasMoreTokens()) 41 | st = new StringTokenizer(br.readLine().trim()); 42 | return st.nextToken(); 43 | } 44 | static long readLong() throws IOException { 45 | return Long.parseLong(next()); 46 | } 47 | static int readInt() throws IOException { 48 | return Integer.parseInt(next()); 49 | } 50 | static double readDouble() throws IOException { 51 | return Double.parseDouble(next()); 52 | } 53 | static char readCharacter() throws IOException { 54 | return next().charAt(0); 55 | } 56 | static String readLine() throws IOException { 57 | return br.readLine().trim(); 58 | } 59 | static int readLongLineInt() throws IOException{ 60 | int x = 0, c; 61 | while((c = br.read()) != ' ' && c != '\n') 62 | x = x * 10 + (c - '0'); 63 | return x; 64 | } 65 | static long pow (long x, long exp){ 66 | if (exp==0) return 1; 67 | long t = pow(x, exp/2); 68 | t = t*t %mod; 69 | if (exp%2 == 0) return t; 70 | return t*x%mod; 71 | } 72 | static long lcm(long a, long b) { 73 | return (a / gcd(a, b)) * b; 74 | } 75 | static long gcd(long a, long b) { 76 | if (b == 0) return a; 77 | return gcd(b, a % b); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /CCC05S5.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.text.DecimalFormat; 3 | import java.text.NumberFormat; 4 | import java.util.*; 5 | /** 6 | * CCC '05 S5 - Pinball Ranking 7 | * Question type: Data Structures, Divide and Conquer 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc05s5 10 | * @author Tommy Pang 11 | */ 12 | public class CCC05S5 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 15 | static StringTokenizer st; 16 | static int mod = (int) 1e9+7; 17 | static int [] bit, arr; 18 | public static void main(String[] args) throws IOException{ 19 | int t = readInt(); bit = new int[100002]; 20 | List list; 21 | long tot = 0; arr = new int[100002]; 22 | Set set = new HashSet<>(); 23 | for (int i = 1; i <= t; i++) { 24 | int v = readInt(); 25 | set.add(v); 26 | arr[i] = v; 27 | } 28 | list = new ArrayList<>(set); 29 | Collections.sort(list); 30 | for (int i = 1; i <= t; i++) { 31 | int p = Collections.binarySearch(list, arr[i])+1; 32 | update(p); tot+=(i-query(p)+1); 33 | } 34 | NumberFormat formatter = new DecimalFormat("#0.00"); 35 | System.out.println(formatter.format((double) tot/t)); 36 | } 37 | static void update(int idx) { 38 | for (int i = idx; i < bit.length; i+=(i&-i)) { 39 | bit[i]+=1; 40 | } 41 | } 42 | static int query(int idx) { 43 | int ret = 0; 44 | for (int i = idx; i > 0; i-=(i&-i)) { 45 | ret+=bit[i]; 46 | } 47 | return ret; 48 | } 49 | static String next() throws IOException { 50 | while (st == null || !st.hasMoreTokens()) 51 | st = new StringTokenizer(br.readLine().trim()); 52 | return st.nextToken(); 53 | } 54 | static long readLong() throws IOException { 55 | return Long.parseLong(next()); 56 | } 57 | static int readInt() throws IOException { 58 | return Integer.parseInt(next()); 59 | } 60 | static double readDouble() throws IOException { 61 | return Double.parseDouble(next()); 62 | } 63 | static char readCharacter() throws IOException { 64 | return next().charAt(0); 65 | } 66 | static String readLine() throws IOException { 67 | return br.readLine().trim(); 68 | } 69 | static int readLongLineInt() throws IOException{ 70 | int x = 0, c; 71 | while((c = br.read()) != ' ' && c != '\n') 72 | x = x * 10 + (c - '0'); 73 | return x; 74 | } 75 | static long pow (long x, long exp){ 76 | if (exp==0) return 1; 77 | long t = pow(x, exp/2); 78 | t = t*t %mod; 79 | if (exp%2 == 0) return t; 80 | return t*x%mod; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /CCC13S3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '13 S3 - Chances of Winning 5 | * Question type: Recursion 6 | * 80/80 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc13s3 8 | * @author Tommy Pang 9 | */ 10 | public class CCC13S3 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, T, G, ans = 0; 15 | static int[][] score = new int[5][5]; 16 | static boolean[][] played = new boolean[5][5]; 17 | public static void main(String[] args) throws IOException { 18 | T = readInt(); G = readInt(); 19 | for (int i = 0; i < G; i++) { 20 | int a = readInt(), b = readInt(), sa = readInt(), sb = readInt(); 21 | int point = sa==sb?1:3; 22 | score[a][b] = sa>=sb?point:0; score[b][a] = sb>=sa?point:0; 23 | played[a][b] = true; played[b][a] = true; 24 | } 25 | List toPlay = new ArrayList<>(); 26 | for (int i = 1; i <= 4; i++) { 27 | for (int j = i+1; j <= 4; j++) { 28 | if (!played[i][j]) { 29 | toPlay.add(new int[]{i, j}); 30 | } 31 | } 32 | } 33 | winning(toPlay, 0); 34 | System.out.println(ans); 35 | } 36 | public static void winning(List toPlay, int idx) { 37 | if (idx==toPlay.size()) { 38 | cnt(); 39 | return; 40 | } 41 | int [] cur = toPlay.get(idx); 42 | // wins 43 | score[cur[0]][cur[1]] = 3; 44 | score[cur[1]][cur[0]] = 0; 45 | winning(toPlay, idx+1); 46 | // loses 47 | score[cur[0]][cur[1]] = 0; 48 | score[cur[1]][cur[0]] = 3; 49 | winning(toPlay, idx+1); 50 | // ties 51 | score[cur[0]][cur[1]] = 1; 52 | score[cur[1]][cur[0]] = 1; 53 | winning(toPlay, idx+1); 54 | } 55 | public static void cnt() { 56 | int fav_score = 0; 57 | for (int i = 1; i <= 4; i++) { 58 | fav_score+=score[T][i]; 59 | } 60 | for (int i = 1; i <= 4; i++) { 61 | if (i==T) continue; 62 | int temp = 0; 63 | for (int j = 1; j <= 4; j++) { 64 | temp+=score[i][j]; 65 | } 66 | if (temp>=fav_score) return; 67 | } 68 | ans++; 69 | } 70 | static String next() throws IOException { 71 | while (st == null || !st.hasMoreTokens()) 72 | st = new StringTokenizer(br.readLine().trim()); 73 | return st.nextToken(); 74 | } 75 | static long readLong() throws IOException { 76 | return Long.parseLong(next()); 77 | } 78 | static int readInt() throws IOException { 79 | return Integer.parseInt(next()); 80 | } 81 | } 82 | 83 | -------------------------------------------------------------------------------- /CCC12S5.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.Arrays; 3 | import java.util.StringTokenizer; 4 | /** 5 | * CCC '12 S5 - Mouse Journey 6 | * Question type: Dynamic Programming 7 | * 70/70 on DMOJ 8 | * Question URL: https://dmoj.ca/problem/ccc12s5 9 | * @author Tommy Pang 10 | */ 11 | public class CCC12S5 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int mod = (int) 1e9+7; 16 | static int [][] dp; 17 | static boolean [][] hasCat, vis; 18 | static int R, C, K; 19 | public static void main(String[] args) throws IOException { 20 | R = readInt(); C = readInt(); K = readInt(); 21 | hasCat = new boolean[R+1][C+1]; dp = new int[R+1][C+1]; vis = new boolean[R+1][C+1]; 22 | for (int i = 1; i <= K; i++) { 23 | hasCat[readInt()][readInt()] = true; 24 | } 25 | for (int[] arr : dp) { 26 | Arrays.fill(arr, -1); 27 | } 28 | System.out.println(dfs(1, 1)); 29 | } 30 | public static int dfs(int r, int c) { 31 | if (r<1 || c<1 || r>R || c>C || hasCat[r][c] || vis[r][c]) return 0; 32 | if (r==R && c==C) return 1; 33 | if (dp[r][c]!=-1) return dp[r][c]; 34 | int ret = 0; 35 | vis[r][c] = true; 36 | ret += dfs(r, c+1); 37 | ret += dfs(r+1, c); 38 | dp[r][c] = ret; 39 | vis[r][c] = false; 40 | return ret; 41 | } 42 | 43 | 44 | static String next() throws IOException { 45 | while (st == null || !st.hasMoreTokens()) 46 | st = new StringTokenizer(br.readLine().trim()); 47 | return st.nextToken(); 48 | } 49 | static long readLong() throws IOException { 50 | return Long.parseLong(next()); 51 | } 52 | static int readInt() throws IOException { 53 | return Integer.parseInt(next()); 54 | } 55 | static double readDouble() throws IOException { 56 | return Double.parseDouble(next()); 57 | } 58 | static char readCharacter() throws IOException { 59 | return next().charAt(0); 60 | } 61 | static String readLine() throws IOException { 62 | return br.readLine().trim(); 63 | } 64 | static int readLongLineInt() throws IOException{ 65 | int x = 0, c; 66 | while((c = br.read()) != ' ' && c != '\n') 67 | x = x * 10 + (c - '0'); 68 | return x; 69 | } 70 | static long pow (long x, long exp){ 71 | if (exp==0) return 1; 72 | long t = pow(x, exp/2); 73 | t = t*t %mod; 74 | if (exp%2 == 0) return t; 75 | return t*x%mod; 76 | } 77 | static long lcm(long a, long b) { 78 | return (a / gcd(a, b)) * b; 79 | } 80 | static long gcd(long a, long b) { 81 | if (b == 0) return a; 82 | return gcd(b, a % b); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /CCC21S3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.ArrayList; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | import java.util.StringTokenizer; 8 | /** 9 | * CCC '21 S3 - Lunch Concert 10 | * Question type: Data Structures 11 | * 15/15 on DMOJ 12 | * Question URL: https://dmoj.ca/problem/ccc21s3 13 | * @author Tommy Pang 14 | */ 15 | public class CCC21S3 { 16 | static StringTokenizer st; 17 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 18 | static friend[] friends; 19 | public static void main(String[] args) throws IOException{ 20 | int n = readInt(); 21 | friends = new friend[n]; 22 | long max = -1, min = Integer.MAX_VALUE; 23 | for (int i = 0; i < n; i++) { 24 | friends[i] = new friend(readLong(), readLong(), readLong()); 25 | max = Math.max(friends[i].P, max); min = Math.min(friends[i].P, min); 26 | } 27 | int lo = (int) min, hi = (int) max; 28 | long ans = Long.MAX_VALUE; 29 | while (lo<=hi) { 30 | int mid = (lo+hi)/2; 31 | long centre = total(mid), l = total(mid-1), r = total(mid+1); 32 | ans = Math.min(centre, ans); 33 | if (l<=r) { 34 | hi = mid-1; 35 | }else { 36 | lo = mid+1; 37 | } 38 | } 39 | System.out.println(ans); 40 | 41 | } 42 | static long total(int v) { 43 | long ret = 0; 44 | for (int i = 0; i < friends.length; i++) { 45 | friend cur = friends[i]; 46 | long dif = Math.abs(cur.P-v); 47 | if (dif<=cur.D) { 48 | continue; 49 | } 50 | ret += ((dif-cur.D)*cur.W); 51 | } 52 | return ret; 53 | } 54 | static class friend { 55 | long P, W, D; 56 | friend(long idx, long w, long d) { P = idx; W = w; D = d; } 57 | } 58 | 59 | static String next() throws IOException { 60 | while (st == null || !st.hasMoreTokens()) 61 | st = new StringTokenizer(br.readLine().trim()); 62 | return st.nextToken(); 63 | } 64 | static long readLong() throws IOException { 65 | return Long.parseLong(next()); 66 | } 67 | static int readInt() throws IOException { 68 | return Integer.parseInt(next()); 69 | } 70 | static double readDouble() throws IOException { 71 | return Double.parseDouble(next()); 72 | } 73 | static char readCharacter() throws IOException { 74 | return next().charAt(0); 75 | } 76 | static String readLine() throws IOException { 77 | return br.readLine().trim(); 78 | } 79 | static int readALotInt() throws IOException{ 80 | int x = 0, c; 81 | while((c = br.read()) != ' ' && c != '\n') 82 | x = x * 10 + (c - '0'); 83 | return x; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /CCC12S2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '12 S2 - Aromatic Numbers 5 | * Question type: Simple Math 6 | * 50/50 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc12s2 8 | * @author Tommy Pang 9 | */ 10 | public class CCC12S2 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7; 15 | static Map map = new HashMap<>(); 16 | static int[] A; 17 | static char[] R; 18 | public static void main(String[] args) throws IOException { 19 | map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); 20 | map.put('C', 100); map.put('D', 500); map.put('M', 1000); 21 | String s = readLine(); 22 | A = new int[s.length()/2]; 23 | R = new char[s.length()/2]; 24 | for (int i = 0; i < s.length(); i+=2) { 25 | A[i/2] = s.charAt(i)-'0'; 26 | R[i/2] = s.charAt(i+1); 27 | } 28 | int ans = 0; 29 | for (int i = 0; i < s.length()/2; i++) { 30 | int v = A[i]*map.get(R[i]); 31 | if (add(i+1, map.get(R[i]))) { 32 | ans+=v; 33 | } 34 | else ans-=v; 35 | } 36 | System.out.println(ans); 37 | } 38 | public static boolean add(int i, int v) { 39 | if (i>=R.length) return true; 40 | return v>=map.get(R[i]); 41 | } 42 | 43 | 44 | 45 | 46 | 47 | static String next() throws IOException { 48 | while (st == null || !st.hasMoreTokens()) 49 | st = new StringTokenizer(br.readLine().trim()); 50 | return st.nextToken(); 51 | } 52 | static long readLong() throws IOException { 53 | return Long.parseLong(next()); 54 | } 55 | static int readInt() throws IOException { 56 | return Integer.parseInt(next()); 57 | } 58 | static double readDouble() throws IOException { 59 | return Double.parseDouble(next()); 60 | } 61 | static char readCharacter() throws IOException { 62 | return next().charAt(0); 63 | } 64 | static String readLine() throws IOException { 65 | return br.readLine().trim(); 66 | } 67 | static int readLongLineInt() throws IOException{ 68 | int x = 0, c; 69 | while((c = br.read()) != ' ' && c != '\n') 70 | x = x * 10 + (c - '0'); 71 | return x; 72 | } 73 | static long pow (long x, long exp){ 74 | if (exp==0) return 1; 75 | long t = pow(x, exp/2); 76 | t = t*t %mod; 77 | if (exp%2 == 0) return t; 78 | return t*x%mod; 79 | } 80 | static long lcm(long a, long b) { 81 | return (a / gcd(a, b)) * b; 82 | } 83 | static long gcd(long a, long b) { 84 | if (b == 0) return a; 85 | return gcd(b, a % b); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /CCC03S4_StringHashingSolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * CCC '03 S4 - Substrings 6 | * Question type: String Algorithms 7 | * 70/100 on DMOJ, TLE on case 6,8,9 8 | * Question URL: https://dmoj.ca/problem/ccc03s4 9 | * @author Tommy Pang 10 | */ 11 | 12 | public class CCC03S4_StringHashingSolution { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 15 | static StringTokenizer st; 16 | static int mod = (int) 1e9+7; 17 | static long [] hsh, pw, hsh2, pw2;; 18 | public static void main(String[] args) throws IOException { 19 | int t = readInt(); 20 | for (int i = 0; i < t; i++) { 21 | String s = readLine(); 22 | 23 | int n = s.length(), base = 131, base2 = 137; 24 | hsh = new long[n+1]; pw = new long[n+1]; 25 | hsh2 = new long[n+1]; pw2 = new long[n+1]; 26 | pw[0] = 1; pw2[0] = 1; 27 | for (int j = 1; j <= n; j++) { 28 | hsh[j] = (hsh[j-1]*base + s.charAt(j-1))%mod; 29 | pw[j] = pw[j-1]*base%mod; 30 | hsh2[j] = (hsh2[j-1]*base2 + s.charAt(j-1))%mod; 31 | pw2[j] = pw2[j-1]*base2%mod; 32 | } 33 | Map map = new HashMap<>(); 34 | Set set = new HashSet<>(); 35 | for (int len = 1; len <= n; len++) { 36 | for (int l = 1; l+len-1 <= n; l++) { 37 | long res = getSubstrHash(l-1, l+len-1); 38 | //System.out.println(res); 39 | //String cur = s.substring(l, l+len-1); 40 | //if (map.containsKey(res)) System.out.println("Collision: " + map.get(res) + " with " + cur); 41 | //else map.put(res, cur); 42 | set.add(res); 43 | } 44 | } 45 | System.out.println(set.size()+1); 46 | } 47 | } 48 | static long getSubstrHash(int l, int r){ 49 | long h1 = (hsh[r] - hsh[l] * pw[r-l] % mod + mod)%mod; 50 | long h2 = (hsh2[r] - hsh2[l] * pw2[r-l] % mod + mod)%mod; 51 | return (h1<<31) | h2; 52 | } 53 | 54 | static String next() throws IOException { 55 | while (st == null || !st.hasMoreTokens()) 56 | st = new StringTokenizer(br.readLine().trim()); 57 | return st.nextToken(); 58 | } 59 | static long readLong() throws IOException { 60 | return Long.parseLong(next()); 61 | } 62 | static int readInt() throws IOException { 63 | return Integer.parseInt(next()); 64 | } 65 | static double readDouble() throws IOException { 66 | return Double.parseDouble(next()); 67 | } 68 | static char readCharacter() throws IOException { 69 | return next().charAt(0); 70 | } 71 | static String readLine() throws IOException { 72 | return br.readLine().trim(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /CCC15S3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '15 S3 - Gates 7 | * Question Type: Data Structures 8 | * 15/15 on CCC, 30/30 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc15s3 10 | * @author Tommy Pang 11 | */ 12 | public class CCC15S3 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 15 | static StringTokenizer st; 16 | static int mod = (int) 1e9+7; 17 | 18 | public static void main(String[] args) throws IOException { 19 | int G = readInt(), N = readInt(), ans = 0; 20 | List list = new ArrayList<>(); 21 | for (int i = 1; i <= G; i++) { 22 | list.add(i); 23 | } 24 | for (int i = 0; i < N; i++) { 25 | int P = readInt(); 26 | int lo = 0, hi = list.size()-1, id = -1; 27 | while (lo<=hi) { 28 | int mid = (lo+hi)/2; 29 | int v = list.get(mid); 30 | if (v==P) { 31 | id = mid; 32 | break; 33 | } 34 | else if (v first = new ArrayList<>(), second = new ArrayList<>(); 15 | static Map name = new HashMap<>(); 16 | static Map price = new HashMap<>(); 17 | public static void main(String[] args) throws IOException{ 18 | int N = Integer.parseInt(br.readLine()); 19 | if (N==1) { 20 | st = new StringTokenizer(br.readLine()); 21 | System.out.println(st.nextToken()); 22 | return; 23 | } 24 | for (int i = 1; i <= N; i++) { 25 | st = new StringTokenizer(br.readLine()); 26 | String s = st.nextToken(); 27 | int R = Integer.parseInt(st.nextToken()); 28 | int S = Integer.parseInt(st.nextToken()); 29 | int D = Integer.parseInt(st.nextToken()); 30 | name.put(i, s); 31 | price.put(s, 2*R + 3*S + D); 32 | } 33 | int max = 0; 34 | for (int i = 1; i <= N; i++) { 35 | max = Math.max(price.get(name.get(i)), max); 36 | } 37 | for (int i = 1; i <= N; i++) { 38 | if (price.get(name.get(i))==max) first.add(i); 39 | } 40 | int max2 = 0; 41 | for (int i = 1; i <= N; i++) { 42 | if (price.get(name.get(i))==max) continue; 43 | max2 = Math.max(max2, price.get(name.get(i))); 44 | } 45 | for (int i = 1; i <= N; i++) { 46 | if (price.get(name.get(i))==max2) second.add(i); 47 | } 48 | if (first.size()==1) { 49 | System.out.println(name.get(first.get(0))); 50 | if (second.size() == 1) { 51 | System.out.println(name.get(second.get(0))); 52 | } 53 | else { 54 | String ans = ""; 55 | int c = 1000000; 56 | for (Integer integer : second) { 57 | if (name.get(integer).charAt(0) - 'A' < c) { 58 | ans = name.get(integer); 59 | c = name.get(integer).charAt(0) - 'A'; 60 | } 61 | } 62 | System.out.println(ans); 63 | } 64 | } 65 | else { 66 | String ans1 = ""; 67 | int c = 1000000; 68 | for (Integer integer : first) { 69 | if (name.get(integer).charAt(0) - 'A' < c) { 70 | ans1 = name.get(integer); 71 | c = name.get(integer).charAt(0) - 'A'; 72 | } 73 | } 74 | System.out.println(ans1); 75 | c = 1000000; 76 | String ans2 = ""; 77 | for (int nxt : first) { 78 | if (!name.get(nxt).equals(ans1) && name.get(nxt).charAt(0) - 'A' < c) { 79 | ans2 = name.get(nxt); c = name.get(nxt).charAt(0) - 'A'; 80 | } 81 | } 82 | System.out.println(ans2); 83 | } 84 | 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /CCC02S4.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * CCC '02 S4 - Bridge Crossing 6 | * Question URL: Dynamic Programming 7 | * 50/50 on DMOJ 8 | * Question URL: https://dmoj.ca/problem/ccc02s4 9 | * @author Tommy Pang 10 | */ 11 | public class CCC02S4 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int M, Q, mod = (int) 1e9+7; 16 | static int [] dp, path; 17 | public static void main(String[] args) throws IOException { 18 | M = readInt(); Q = readInt(); 19 | dp = new int[Q+1]; // dp[i] -> for the first i person, what's the min time required 20 | path = new int[Q+1]; // the index of the last person in the previous group 21 | List queue = new ArrayList<>(); 22 | queue.add(null); 23 | for (int i = 0; i < Q; i++) { 24 | queue.add(new person(readLine(), readInt())); 25 | } 26 | Arrays.fill(dp, Integer.MAX_VALUE); 27 | dp[0] = 0; 28 | for (int i = 1; i <= Q; i++) { 29 | for (int j = 1; j <= M; j++) { 30 | if (i-j>=0) { 31 | int l = i-j; 32 | int max = getMax(queue, l, i); 33 | if (dp[l]+max order = new ArrayList<>(); 42 | order.add(cur); 43 | while (path[cur]!=0) { 44 | order.add(path[cur]); 45 | cur = path[cur]; 46 | } 47 | Collections.reverse(order); 48 | int lst = 1; 49 | System.out.println("Total Time: " + dp[Q]); 50 | for (int i = 0; i < order.size(); i++) { 51 | for (int j = lst; j <= order.get(i); j++) { 52 | System.out.print(queue.get(j).name + " "); 53 | } 54 | lst = order.get(i)+1; 55 | System.out.println(); 56 | } 57 | } 58 | public static int getMax(List q, int l, int r) { 59 | int max = 0; 60 | for (int i = l+1; i <= r; i++) { 61 | max = Math.max(max, q.get(i).time); 62 | } 63 | return max; 64 | } 65 | public static class person { 66 | String name; 67 | int time; 68 | person(String name, int time) { this.name = name; this.time = time; } 69 | } 70 | static String next() throws IOException { 71 | while (st == null || !st.hasMoreTokens()) 72 | st = new StringTokenizer(br.readLine().trim()); 73 | return st.nextToken(); 74 | } 75 | static long readLong() throws IOException { 76 | return Long.parseLong(next()); 77 | } 78 | static int readInt() throws IOException { 79 | return Integer.parseInt(next()); 80 | } 81 | static double readDouble() throws IOException { 82 | return Double.parseDouble(next()); 83 | } 84 | static char readCharacter() throws IOException { 85 | return next().charAt(0); 86 | } 87 | static String readLine() throws IOException { 88 | return br.readLine().trim(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /CCC00S3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '00 S3 - Surfing 5 | * Question type: Graph Theory, Regular Expressions 6 | * 2/2 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC00S3 { 10 | final static String CANT_STRING = "Can't surf from "; 11 | final static String CAN_STRING = "Can surf from "; 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static int N; 14 | static Map> urlDoc = new HashMap<>(); 15 | static Map visitedURL = new HashMap<>(); 16 | 17 | public static void main(String[] args) throws IOException { 18 | N = Integer.parseInt(br.readLine()); 19 | for (int i = 1; i <= N; i++) { 20 | String url = br.readLine(); 21 | String doc; 22 | while (true) { 23 | List arr = new ArrayList<>(); 24 | doc = br.readLine(); 25 | if (doc.equals("")) break; 26 | if(!parseDoc(doc).isEmpty()) { 27 | arr.addAll(parseDoc(doc)); 28 | } 29 | if (!arr.isEmpty()) { 30 | for (String item : arr) { 31 | if(!url.equals(item)){ 32 | System.out.println("Link from " + url + " to " + item); 33 | } 34 | } 35 | } 36 | List teampArr = urlDoc.get(url) == null ? new ArrayList<>() : urlDoc.get(url); 37 | teampArr.addAll(arr); 38 | urlDoc.put(url, teampArr); 39 | } 40 | 41 | } 42 | 43 | while (true) { 44 | String srcURL = br.readLine(); 45 | if (srcURL.equals("The End")) return; 46 | String destURL = br.readLine(); 47 | if (destURL.equals("The End")) return; 48 | 49 | if (isSurfable(srcURL, destURL)) { 50 | System.out.println(CAN_STRING + srcURL + " to " + destURL + "."); 51 | } else { 52 | System.out.println(CANT_STRING + srcURL + " to " + destURL + "."); 53 | } 54 | } 55 | 56 | } 57 | 58 | static List parseDoc(String doc) { 59 | List hrefList = new ArrayList<>(); 60 | if (doc.contains("HREF")) { 61 | String[] line = doc.split(" rooms = new ArrayList<>(); 18 | static int [] dr = new int[] {0, 0, -1, 1}, dc = new int[] {-1, 1, 0, 0}; 19 | static boolean [][] vis; 20 | public static void main(String[] args) throws IOException { 21 | F = readInt(); R = readInt(); C = readInt(); 22 | floor = new char[R][C]; vis = new boolean[R][C]; 23 | for (int i = 0; i < R; i++) { 24 | floor[i] = readLine().toCharArray(); 25 | } 26 | for (int i = 0; i < R; i++) { 27 | for (int j = 0; j < C; j++) { 28 | if (floor[i][j]=='.' && !vis[i][j]) { 29 | rooms.add(BFS(i, j)); 30 | } 31 | } 32 | } 33 | rooms.sort(Collections.reverseOrder()); 34 | for (int i = 0; i < rooms.size(); i++) { 35 | if (F>=rooms.get(i)) { 36 | F-=rooms.get(i); 37 | } 38 | else { 39 | if (i==1) { 40 | System.out.println(i + " room, " + F + " square metre(s) left over"); 41 | } 42 | else System.out.println(i + " rooms, " + F + " square metre(s) left over"); 43 | return; 44 | } 45 | } 46 | System.out.println(rooms.size() + " rooms, " + F + " square metre(s) left over"); 47 | } 48 | public static int BFS(int r, int c) { 49 | int cnt = 1; 50 | Queue queue = new LinkedList<>(); 51 | queue.add(new int[]{r, c}); 52 | vis[r][c] = true; 53 | while (!queue.isEmpty()) { 54 | int [] cur = queue.poll(); 55 | int i = cur[0], j = cur[1]; 56 | for (int k = 0; k < 4; k++) { 57 | if (valid(i+dr[k], j+dc[k])) { 58 | queue.add(new int[] {i+dr[k], j+dc[k]}); 59 | vis[i+dr[k]][j+dc[k]] = true; 60 | cnt++; 61 | } 62 | } 63 | } 64 | return cnt; 65 | } 66 | public static boolean valid(int i, int j) { 67 | if (i<0 || i==R || j<0 || j==C || vis[i][j] || floor[i][j]=='I') return false; 68 | return true; 69 | } 70 | 71 | static String next() throws IOException { 72 | while (st == null || !st.hasMoreTokens()) 73 | st = new StringTokenizer(br.readLine().trim()); 74 | return st.nextToken(); 75 | } 76 | static long readLong() throws IOException { 77 | return Long.parseLong(next()); 78 | } 79 | static int readInt() throws IOException { 80 | return Integer.parseInt(next()); 81 | } 82 | static double readDouble() throws IOException { 83 | return Double.parseDouble(next()); 84 | } 85 | static char readCharacter() throws IOException { 86 | return next().charAt(0); 87 | } 88 | static String readLine() throws IOException { 89 | return br.readLine().trim(); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /CCC06S3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.StringTokenizer; 7 | /** 8 | * CCC '06 S3 - Tin Can Telephone 9 | * Question type: Geometry 10 | * 4/4 on DMOJ 11 | * @author Tommy Pang 12 | */ 13 | public class CCC06S3 { 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static StringTokenizer st; 16 | static int cnt = 0, x, y, slope, y_int; 17 | static boolean horizontal, vertical; 18 | public static void main(String[] args) throws IOException{ 19 | st = new StringTokenizer(br.readLine()); 20 | int jx = Integer.parseInt(st.nextToken()); 21 | int jy = Integer.parseInt(st.nextToken()); 22 | int rx = Integer.parseInt(st.nextToken()); 23 | int ry = Integer.parseInt(st.nextToken()); 24 | if (ry-jy == 0) { 25 | horizontal = true; 26 | y = ry; 27 | } 28 | else if (rx-jx == 0) { 29 | vertical = true; 30 | x = rx; 31 | } 32 | else { 33 | slope = (jy-ry)/(jx-rx); 34 | y_int = jy - slope*jx; 35 | } 36 | int cases = Integer.parseInt(br.readLine()); 37 | for (int i = 0; i < cases; i++) { 38 | st = new StringTokenizer(br.readLine()); 39 | int N = Integer.parseInt(st.nextToken()); 40 | Map max_y = new HashMap<>(), min_y = new HashMap<>(); 41 | int x_max = -1, x_min = 100000, y_max = -1, y_min = 100000; 42 | for (int j = 0; j < N; j++) { 43 | int a = Integer.parseInt(st.nextToken()); 44 | int b = Integer.parseInt(st.nextToken()); 45 | if (a>x_max){ 46 | x_max = a; 47 | } 48 | else if (ay_max){ 52 | y_max = b; 53 | } 54 | else if (bmax_y.get(a)) { 60 | max_y.remove(a); max_y.put(a, b); 61 | } 62 | } 63 | if (!min_y.containsKey(a)) min_y.put(a, b); 64 | else { 65 | if (b=y_min && y<=y_max){ 72 | cnt+=1; 73 | } 74 | } 75 | else if (vertical){ 76 | if (x>=x_min && x<=x_max){ 77 | cnt+=1; 78 | } 79 | } 80 | else { 81 | for (int j = -1000; j <= 1000; j++) { 82 | int ans = slope*j+y_int; 83 | if (min_y.containsKey(j) && max_y.containsKey(j)) { 84 | if (ans >= min_y.get(j) && ans <= max_y.get(j)) { 85 | cnt += 1; 86 | break; 87 | } 88 | } 89 | } 90 | } 91 | } 92 | System.out.println(cnt); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /CCC04S5.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '04 S5 - Super Plumber 7 | * Question type: Dynamic Programming 8 | * 5/5 on DMOJ 9 | * @author Tommy Pang, took idea from Milliken high school -> http://mmhs.ca/ccc/index.htm 10 | */ 11 | public class CCC04S5 { 12 | static StringTokenizer st; 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static String [][] grid; 15 | static int [][] coins; 16 | static int [][] dp; 17 | static int r, c; 18 | public static void main(String[] args) throws IOException { 19 | while (true){ 20 | st = new StringTokenizer(br.readLine()); 21 | r = Integer.parseInt(st.nextToken()); 22 | c = Integer.parseInt(st.nextToken()); 23 | if (r==0 && c==0) return; 24 | grid = new String[r][c]; 25 | coins = new int[r][c]; 26 | dp = new int[r][c]; 27 | for (int i = 0; i < r; i++) { 28 | st = new StringTokenizer(br.readLine()); 29 | String s = st.nextToken(); 30 | for (int j = 0; j < c; j++) { 31 | String symbol = String.valueOf(s.charAt(j)); 32 | if (!symbol.equals(".") && !symbol.equals("*")) { 33 | coins[i][j] = Integer.parseInt(symbol); 34 | } 35 | grid[i][j] = symbol; 36 | } 37 | } 38 | solve(); 39 | } 40 | } 41 | static void solve(){ 42 | for (int i = 0; i < r; i++) { 43 | for (int j = 0; j < c; j++) { 44 | dp[i][j] = -1; 45 | } 46 | } 47 | dp[r - 1][0] = coins[r - 1][0]; 48 | for (int i = r - 2 ; i >= 0 ; i--) 49 | { 50 | if (!grid[i][0].equals("*")) { 51 | dp[i][0] = dp[i + 1][0] + coins[i][0]; 52 | } 53 | else break; 54 | } 55 | 56 | for (int col = 1 ; col < c ; col++) { 57 | // going down from each square (0 to rows-1) 58 | for (int row = 0; row < r; row++) { 59 | if (!grid[row][col-1].equals("*") && dp[row][col-1]!=-1) // can't go right from a wall or a place that cannot be visited 60 | { 61 | int t = dp[row][col - 1]; 62 | for (int k = row; k < r; k++) { 63 | if (!grid[k][col].equals("*")) { 64 | t = t + coins[k][col]; 65 | if (t > dp[k][col]) dp[k][col] = t; 66 | } else break; // get out: hit a wall 67 | } 68 | } 69 | } 70 | // going up from each square (rows-1 to 0) 71 | for (int row = r - 1; row >= 0; row--) { 72 | if (!grid[row][col-1].equals("*") && dp[row][col-1]!=-1) // can't go right from a wall or a place that cannot be visited 73 | { 74 | int t = dp[row][col - 1]; 75 | for (int k = row; k >= 0; k--) { 76 | if (!grid[k][col].equals("*")) { 77 | t = t + coins[k][col]; 78 | if (t > dp[k][col]) dp[k][col] = t; 79 | } else break; // get out: hit a wall 80 | } 81 | } 82 | } 83 | } 84 | System.out.println(dp[r - 1][c - 1]); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /CCC06S1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Arrays; 5 | import java.util.StringTokenizer; 6 | /** 7 | * CCC '06 S1 - Maternity 8 | * Question type: Implementation 9 | * 3/3 on DMOJ 10 | * @author Tommy Pang 11 | */ 12 | public class CCC06S1 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static StringTokenizer st; 15 | static String mother, farther; 16 | public static void main(String[] args) throws IOException { 17 | mother = br.readLine(); 18 | farther = br.readLine(); 19 | int N = Integer.parseInt(br.readLine()); 20 | for (int i = 0; i < N; i++) { 21 | String s = br.readLine(); 22 | evaluate(mother, farther, s); 23 | } 24 | } 25 | static void evaluate(String a, String b, String s){ 26 | for (int i = 0; i < 5; i++) { 27 | switch (s.charAt(i)){ 28 | case 'A': 29 | if (!a.contains("A") && !b.contains("A")){ 30 | System.out.println("Not their baby!"); 31 | return; 32 | } 33 | break; 34 | case 'a': 35 | if (!check(s, "a")) { 36 | System.out.println("Not their baby!"); 37 | return; 38 | } 39 | break; 40 | case 'B': 41 | if (!a.contains("B") && !b.contains("B")){ 42 | System.out.println("Not their baby!"); 43 | return; 44 | } 45 | break; 46 | case 'b': 47 | if (!check(s, "b")) { 48 | System.out.println("Not their baby!"); 49 | return; 50 | } 51 | break; 52 | case 'C': 53 | if (!a.contains("C") && !b.contains("C")){ 54 | System.out.println("Not their baby!"); 55 | return; 56 | } 57 | break; 58 | case 'c': 59 | if (!check(s, "c")) { 60 | System.out.println("Not their baby!"); 61 | return; 62 | } 63 | break; 64 | case 'D': 65 | if (!a.contains("D") && !b.contains("D")){ 66 | System.out.println("Not their baby!"); 67 | return; 68 | } 69 | break; 70 | case 'd': 71 | if (!check(s, "d")) { 72 | System.out.println("Not their baby!"); 73 | return; 74 | } 75 | break; 76 | case 'E': 77 | if (!a.contains("E") && !b.contains("E")){ 78 | System.out.println("Not their baby!"); 79 | return; 80 | } 81 | break; 82 | case 'e': 83 | if (!check(s, "e")) { 84 | System.out.println("Not their baby!"); 85 | return; 86 | } 87 | break; 88 | } 89 | } 90 | System.out.println("Possible baby."); 91 | } 92 | static boolean check(String s, String letter){ 93 | return mother.contains(letter) && farther.contains(letter); 94 | } 95 | } -------------------------------------------------------------------------------- /CCC15S5.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | /** 4 | * CCC '15 S5 - Greedy For Pies 5 | * Question URL: Dynamic Programming 6 | * 15/15 on DMOJ 7 | * Question URL: https://dmoj.ca/problem/ccc15s5 8 | * @author Tommy Pang 9 | */ 10 | public class CCC15S5 { 11 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 13 | static StringTokenizer st; 14 | static int mod = (int) 1e9+7, n, m; 15 | // dp[i][L][R][0/1] -> we are at position i of the regular(N) pies 16 | // we have [L, R] that marks the range of the sorted M pies that are still available 17 | // and we can take(1) or cannot take(0) current one 18 | static int[][][][] dp = new int[3005][2][105][105]; 19 | static int[] A, B; 20 | public static void main(String[] args) throws IOException { 21 | n = readInt(); A = new int[n+1]; 22 | for (int i = 1; i <= n; i++) A[i] = readInt(); 23 | m = readInt(); B = new int[m+1]; 24 | for (int i = 1; i <= m; i++) B[i] = readInt(); 25 | Arrays.sort(B); 26 | for (int i = 0; i < 3005; i++) { 27 | for (int j = 0; j < 2; j++) { 28 | for (int k = 0; k < 105; k++) { 29 | for (int l = 0; l < 105; l++) { 30 | dp[i][j][k][l] = -1; 31 | } 32 | } 33 | } 34 | } 35 | System.out.println(recursion(1, 1, m, 1)); 36 | } 37 | public static int recursion(int i, int L, int R, int can) { 38 | if (dp[i][can][L][R]!=-1) return dp[i][can][L][R]; 39 | int ret = 0; 40 | if (i<=n) { 41 | // take current one 42 | if (can==1) ret = Math.max(ret, recursion(i+1, L, R, 0) + A[i]); 43 | // skip current one 44 | ret = Math.max(ret, recursion(i+1, L, R, 1)); 45 | } 46 | if (L<=R) { 47 | // insert and waste current one 48 | ret = Math.max(ret, recursion(i, L+1, R, 1)); 49 | // insert and take current one 50 | if (can==1) ret = Math.max(ret, recursion(i, L, R-1, 0) + B[R]); 51 | } 52 | dp[i][can][L][R] = ret; 53 | return ret; 54 | } 55 | 56 | 57 | static String next() throws IOException { 58 | while (st == null || !st.hasMoreTokens()) 59 | st = new StringTokenizer(br.readLine().trim()); 60 | return st.nextToken(); 61 | } 62 | static long readLong() throws IOException { 63 | return Long.parseLong(next()); 64 | } 65 | static int readInt() throws IOException { 66 | return Integer.parseInt(next()); 67 | } 68 | static double readDouble() throws IOException { 69 | return Double.parseDouble(next()); 70 | } 71 | static char readCharacter() throws IOException { 72 | return next().charAt(0); 73 | } 74 | static String readLine() throws IOException { 75 | return br.readLine().trim(); 76 | } 77 | static int readLongLineInt() throws IOException{ 78 | int x = 0, c; 79 | while((c = br.read()) != ' ' && c != '\n') 80 | x = x * 10 + (c - '0'); 81 | return x; 82 | } 83 | static long pow (long x, long exp){ 84 | if (exp==0) return 1; 85 | long t = pow(x, exp/2); 86 | t = t*t %mod; 87 | if (exp%2 == 0) return t; 88 | return t*x%mod; 89 | } 90 | static long lcm(long a, long b) { 91 | return (a / gcd(a, b)) * b; 92 | } 93 | static long gcd(long a, long b) { 94 | if (b == 0) return a; 95 | return gcd(b, a % b); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /CCC19S3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '19 S3 - Arithmetic Square 7 | * Question type: Implementation 8 | * 16/16 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc19s3 10 | * @author Tommy Pang 11 | */ 12 | 13 | public class CCC19S3 { 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 16 | static StringTokenizer st; 17 | static int mod = (int) 1e9+7; 18 | public static void main(String[] args) throws IOException { 19 | int a[][] = new int[3][3], x[][] = new int[3][3], row[] = new int[3], col[] = new int[3], cnt=0; 20 | for(int i=0; i<3; i++){ 21 | for(int j=0; j<3; j++){ 22 | String s = next(); 23 | if(s.equals("X")) { x[i][j] = 1; row[i]++; col[j]++; cnt++; } 24 | else a[i][j] = Integer.parseInt(s); 25 | } 26 | } 27 | while(cnt > 0){ 28 | boolean flag = true; 29 | for(int i=0; i<3 && flag; i++){ 30 | if(row[i] == 1){ 31 | if(x[i][0] == 1) { a[i][0] = 2*a[i][1] - a[i][2]; x[i][0] = 0; col[0]--; } 32 | if(x[i][1] == 1) { a[i][1] = (a[i][0] + a[i][2])/2; x[i][1] = 0; col[1]--; } 33 | if(x[i][2] == 1) { a[i][2] = 2*a[i][1] - a[i][0]; x[i][2] = 0; col[2]--; } 34 | cnt --; row[i]--; flag = false; 35 | } 36 | } 37 | for(int i=0; i<3 && flag; i++){ 38 | if(col[i] == 1){ 39 | if(x[0][i] == 1) { a[0][i] = 2*a[1][i] - a[2][i]; x[0][i] = 0; row[0]--; } 40 | if(x[1][i] == 1) { a[1][i] = (a[0][i] + a[2][i])/2; x[1][i] = 0; row[1]--; } 41 | if(x[2][i] == 1) { a[2][i] = 2*a[1][i] - a[0][i]; x[2][i] = 0; row[2]--; } 42 | cnt --; col[i]--; flag = false; 43 | } 44 | } 45 | if(x[1][1] == 1 && flag) { a[1][1] = 0; row[1]--; col[1]--; cnt--; x[1][1]=0; flag = false; } 46 | if(x[1][0] == 1 && flag) { a[1][0] = 0; row[1]--; col[0]--; cnt--; x[1][0]=0; flag = false; } 47 | if(x[1][2] == 1 && flag) { a[1][2] = 0; row[1]--; col[2]--; cnt--; x[1][2]=0; flag = false; } 48 | if(x[0][1] == 1 && flag) { a[0][1] = 0; row[0]--; col[1]--; cnt--; x[0][1]=0; flag = false; } 49 | if(x[2][1] == 1 && flag) { a[2][1] = 0; row[2]--; col[1]--; cnt--; x[2][1]=0; flag = false; } 50 | if(x[0][0] == 1 && flag) { a[0][0] = 0; row[0]--; col[0]--; cnt--; x[0][0]=0; flag = false; } 51 | } 52 | for(int i=0; i<3; i++){ 53 | for(int j=0; j<3; j++) 54 | System.out.print(a[i][j] + " "); 55 | System.out.println(); 56 | } 57 | } 58 | 59 | 60 | 61 | static String next() throws IOException { 62 | while (st == null || !st.hasMoreTokens()) 63 | st = new StringTokenizer(br.readLine().trim()); 64 | return st.nextToken(); 65 | } 66 | static long readLong() throws IOException { 67 | return Long.parseLong(next()); 68 | } 69 | static int readInt() throws IOException { 70 | return Integer.parseInt(next()); 71 | } 72 | static double readDouble() throws IOException { 73 | return Double.parseDouble(next()); 74 | } 75 | static char readCharacter() throws IOException { 76 | return next().charAt(0); 77 | } 78 | static String readLine() throws IOException { 79 | return br.readLine().trim(); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /CCC14S4.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * CCC '14 S4 - Tinted Glass Window 6 | * Question URL: Data Structures, Geometry 7 | * 150/150 on DMOJ 8 | * Question URL: https://dmoj.ca/problem/ccc14s4 9 | * @author Tommy Pang 10 | */ 11 | public class CCC14S4 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int mod = (int) 1e9+7, n, T, x1, y1, x2, y2, v; 16 | static long ans = 0; 17 | static long[][] dif; 18 | static Map xs = new TreeMap<>(); 19 | static Map ys = new TreeMap<>(); 20 | static Set distinctX = new TreeSet<>(); 21 | static Set distinctY = new TreeSet<>(); 22 | static int[][] pair; 23 | static int[] trueX, trueY; 24 | public static void main(String[] args) throws IOException { 25 | n = readInt(); T = readInt(); 26 | pair = new int[n][5]; trueX = new int[2*n+1]; trueY = new int[2*n+1]; 27 | for (int i = 0; i < n; i++) { 28 | x1 = readInt(); y1 = readInt(); x2 = readInt(); y2 = readInt(); v = readInt(); 29 | pair[i] = new int[] {x1, y1, x2, y2, v}; 30 | distinctX.add(x1); distinctX.add(x2); distinctY.add(y1); distinctY.add(y2); 31 | } 32 | dif = new long[2*distinctX.size()+2][2*distinctY.size()+2]; 33 | int idx = 0; for (int nxt : distinctX) { xs.put(nxt, ++idx); trueX[idx] = nxt; } 34 | idx = 0; for (int nxt : distinctY) { ys.put(nxt, ++idx); trueY[idx] = nxt; } 35 | for (int[] nxt : pair) { 36 | dif[xs.get(nxt[0])][ys.get(nxt[1])]+=nxt[4]; 37 | dif[xs.get(nxt[2])][ys.get(nxt[3])]+=nxt[4]; 38 | dif[xs.get(nxt[0])][ys.get(nxt[3])]-=nxt[4]; 39 | dif[xs.get(nxt[2])][ys.get(nxt[1])]-=nxt[4]; 40 | } 41 | for (int i = 1; i < distinctX.size(); i++) { 42 | for (int j = 1; j < distinctY.size(); j++) { 43 | dif[i][j] += dif[i-1][j] + dif[i][j-1] - dif[i-1][j-1]; 44 | if (dif[i][j]>=T) { 45 | long x = trueX[i+1]-trueX[i], y = trueY[j+1]-trueY[j]; 46 | ans += x*y; 47 | } 48 | } 49 | } 50 | System.out.println(ans); 51 | } 52 | 53 | static String next() throws IOException { 54 | while (st == null || !st.hasMoreTokens()) 55 | st = new StringTokenizer(br.readLine().trim()); 56 | return st.nextToken(); 57 | } 58 | static long readLong() throws IOException { 59 | return Long.parseLong(next()); 60 | } 61 | static int readInt() throws IOException { 62 | return Integer.parseInt(next()); 63 | } 64 | static double readDouble() throws IOException { 65 | return Double.parseDouble(next()); 66 | } 67 | static char readCharacter() throws IOException { 68 | return next().charAt(0); 69 | } 70 | static String readLine() throws IOException { 71 | return br.readLine().trim(); 72 | } 73 | static int readLongLineInt() throws IOException{ 74 | int x = 0, c; 75 | while((c = br.read()) != ' ' && c != '\n') 76 | x = x * 10 + (c - '0'); 77 | return x; 78 | } 79 | static long pow (long x, long exp){ 80 | if (exp==0) return 1; 81 | long t = pow(x, exp/2); 82 | t = t*t %mod; 83 | if (exp%2 == 0) return t; 84 | return t*x%mod; 85 | } 86 | static long lcm(long a, long b) { 87 | return (a / gcd(a, b)) * b; 88 | } 89 | static long gcd(long a, long b) { 90 | if (b == 0) return a; 91 | return gcd(b, a % b); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /CCC04S3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | import java.util.stream.Stream; 5 | /** 6 | * CCC '04 S3 - Spreadsheet 7 | * Question type: Graph Theory 8 | * 5/5 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc04s3 10 | * @author Tommy Pang 11 | */ 12 | public class CCC04S3 { 13 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 15 | static StringTokenizer st; 16 | static int mod = (int) 1e9+7; 17 | static String[][] grid = new String[10][10]; 18 | static boolean[][] vis = new boolean[10][10]; 19 | public static void main(String[] args) throws IOException { 20 | for (int i = 0; i < 10; i++) { 21 | String [] cols = readLine().split(" "); 22 | for (int j = 1; j <= 9; j++) { 23 | grid[i][j] = cols[j-1]; 24 | } 25 | } 26 | for (int i = 0; i < 10; i++) { 27 | for (int j = 1; j <= 9; j++) { 28 | vis = new boolean[10][10]; 29 | dfs(i, j); 30 | } 31 | } 32 | for (int i = 0; i < 10; i++) { 33 | for (int j = 1; j <= 9; j++) { 34 | System.out.print(grid[i][j] + " "); 35 | } 36 | System.out.println(); 37 | } 38 | } 39 | public static int dfs(int r, int c) { 40 | if (isNumeric(grid[r][c])) return Integer.parseInt(grid[r][c]); 41 | if (vis[r][c] || grid[r][c].equals("*")) return -1; 42 | vis[r][c] = true; 43 | String [] depend = grid[r][c].split("\\+"); 44 | int sum = 0; 45 | for (int i = 0; i < depend.length; i++) { 46 | int ret = dfs(depend[i].charAt(0)-'A', depend[i].charAt(1)-'0'); 47 | if (ret==-1) { 48 | grid[r][c] = "*"; 49 | return -1; 50 | } 51 | else sum+=ret; 52 | } 53 | grid[r][c] = String.valueOf(sum); 54 | return sum; 55 | } 56 | public static boolean isNumeric(String strNum) { 57 | if (strNum == null) { 58 | return false; 59 | } 60 | try { 61 | int d = Integer.parseInt(strNum); 62 | } 63 | catch (NumberFormatException nfe) { 64 | return false; 65 | } 66 | return true; 67 | } 68 | 69 | static String next() throws IOException { 70 | while (st == null || !st.hasMoreTokens()) 71 | st = new StringTokenizer(br.readLine().trim()); 72 | return st.nextToken(); 73 | } 74 | static long readLong() throws IOException { 75 | return Long.parseLong(next()); 76 | } 77 | static int readInt() throws IOException { 78 | return Integer.parseInt(next()); 79 | } 80 | static double readDouble() throws IOException { 81 | return Double.parseDouble(next()); 82 | } 83 | static char readCharacter() throws IOException { 84 | return next().charAt(0); 85 | } 86 | static String readLine() throws IOException { 87 | return br.readLine().trim(); 88 | } 89 | static int readLongLineInt() throws IOException{ 90 | int x = 0, c; 91 | while((c = br.read()) != ' ' && c != '\n') 92 | x = x * 10 + (c - '0'); 93 | return x; 94 | } 95 | static long pow (long x, long exp){ 96 | if (exp==0) return 1; 97 | long t = pow(x, exp/2); 98 | t = t*t %mod; 99 | if (exp%2 == 0) return t; 100 | return t*x%mod; 101 | } 102 | static long lcm(long a, long b) { 103 | return (a / gcd(a, b)) * b; 104 | } 105 | static long gcd(long a, long b) { 106 | if (b == 0) return a; 107 | return gcd(b, a % b); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /CCC21S5.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.util.StringTokenizer; 4 | /** 5 | * CCC '21 S5 - Math Homework 6 | * Question type: Data Structures, Intermediate Math 7 | * 15/15 on DMOJ 8 | * Question URL: https://dmoj.ca/problem/ccc21s5 9 | * @author Tommy Pang 10 | */ 11 | public class CCC21S5 { 12 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 13 | static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 14 | static StringTokenizer st; 15 | static int mod = (int) 1e9+7; 16 | static int [] segTree; 17 | public static void main(String[] args) throws IOException{ 18 | int n = readInt(), m = readInt(); 19 | int height = (int) Math.ceil(Math.log(n)/Math.log(2))+1; 20 | int size = (int) pow(2, height)-1; segTree = new int[size+1]; 21 | int [] x = new int[m], y = new int[m], z = new int[m]; int [][] dif = new int[17][n+5]; 22 | for (int i = 0; i < m; i++) { 23 | x[i] = readInt(); y[i] = readInt(); z[i] = readInt(); 24 | dif[z[i]][x[i]]++; dif[z[i]][y[i]+1]--; 25 | } 26 | int [] res = new int[n+1]; 27 | Arrays.fill(res, 1); 28 | for (int i = 1; i <= 16; i++) { 29 | for (int j = 1; j <= n; j++) { 30 | dif[i][j] += dif[i][j-1]; 31 | if (dif[i][j]>0) { 32 | // i is a divisor 33 | res[j] = res[j]*i/gcd(res[j], i); 34 | } 35 | } 36 | } 37 | construct(1, n, 1, res); 38 | for (int i = 0; i < m; i++) { 39 | if (query(x[i], y[i], 1, n, 1)!=z[i]) { 40 | System.out.println("Impossible"); 41 | return; 42 | } 43 | } 44 | for (int i = 1; i <= n; i++) { 45 | System.out.print(res[i] + " "); 46 | } 47 | 48 | } 49 | static int query(int qs, int qe, int s, int e, int idx) { 50 | if (qee) return 0; 51 | if (qs<=s && qe>=e) return segTree[idx]; 52 | int mid = (s+e)/2; 53 | return gcd(query(qs, qe, s, mid, 2*idx), query(qs, qe, mid+1, e, 2*idx+1)); 54 | } 55 | static int construct(int s, int e, int idx, int [] arr) { 56 | if (s==e) { 57 | segTree[idx] = arr[s]; 58 | return segTree[idx]; 59 | } 60 | int mid = (s+e)/2; 61 | int v1 = construct(s, mid, 2*idx, arr), v2 = construct(mid+1, e, 2*idx+1, arr); 62 | segTree[idx] = gcd(v1, v2); 63 | return segTree[idx]; 64 | } 65 | static int gcd(int a, int b) { 66 | if (b==0) return a; 67 | return gcd(b,a%b); 68 | } 69 | static String next() throws IOException { 70 | while (st == null || !st.hasMoreTokens()) 71 | st = new StringTokenizer(br.readLine().trim()); 72 | return st.nextToken(); 73 | } 74 | static long readLong() throws IOException { 75 | return Long.parseLong(next()); 76 | } 77 | static int readInt() throws IOException { 78 | return Integer.parseInt(next()); 79 | } 80 | static double readDouble() throws IOException { 81 | return Double.parseDouble(next()); 82 | } 83 | static char readCharacter() throws IOException { 84 | return next().charAt(0); 85 | } 86 | static String readLine() throws IOException { 87 | return br.readLine().trim(); 88 | } 89 | static int readLongLineInt() throws IOException{ 90 | int x = 0, c; 91 | while((c = br.read()) != ' ' && c != '\n') 92 | x = x * 10 + (c - '0'); 93 | return x; 94 | } 95 | static long pow (long x, long exp){ 96 | if (exp==0) return 1; 97 | long t = pow(x, exp/2); 98 | t = t*t %mod; 99 | if (exp%2 == 0) return t; 100 | return t*x%mod; 101 | } 102 | } -------------------------------------------------------------------------------- /CCC11S4.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '11 S4 - Blood Distribution 7 | * Question type: Greedy Algorithms 8 | * 50/50 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc11s4 10 | * @author Tommy Pang 11 | */ 12 | public class CCC11S4 { 13 | static StringTokenizer st1, st2; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static int [] blood_negative = new int[4], blood_positive = new int[4]; 16 | static int [] requirements_negative = new int[4], requirements_positive = new int[4]; 17 | public static void main(String[] args) throws IOException { 18 | st1 = new StringTokenizer(br.readLine()); 19 | st2 = new StringTokenizer(br.readLine()); 20 | for (int i = 0; i < 4; i+=1) { 21 | blood_negative[i] = Integer.parseInt(st1.nextToken()); 22 | blood_positive[i] = Integer.parseInt(st1.nextToken()); 23 | requirements_negative[i] = Integer.parseInt(st2.nextToken()); 24 | requirements_positive[i] = Integer.parseInt(st2.nextToken()); 25 | } 26 | int ans = 0; 27 | for (int i = 0; i < 4; i++) { 28 | if (blood_negative[i]>=requirements_negative[i]){ 29 | ans+=requirements_negative[i]; 30 | blood_negative[i]-=requirements_negative[i]; 31 | requirements_negative[i] = 0; 32 | } 33 | else { 34 | for (int j = i; j >= 0; j--) { 35 | if (i==2 && j==1) continue; 36 | if (requirements_negative[i] == 0) break; 37 | if (blood_negative[j]=requirements_positive[i]){ 52 | ans+=requirements_positive[i]; 53 | blood_positive[i]-=requirements_positive[i]; 54 | requirements_positive[i] = 0; 55 | } 56 | else { 57 | for (int j = i; j >= 0; j--) { 58 | if (i==2 && j==1) continue; 59 | if (requirements_positive[i] == 0) break; 60 | if (blood_positive[j]0) { 72 | if (blood_negative[j] < requirements_positive[i]) { 73 | ans += blood_negative[j]; 74 | requirements_positive[i] -= blood_negative[j]; 75 | blood_negative[j] = 0; 76 | } 77 | else { 78 | ans += requirements_positive[i]; 79 | blood_negative[j] -= requirements_positive[i]; 80 | requirements_positive[i] = 0; 81 | } 82 | } 83 | } 84 | } 85 | } 86 | System.out.println(ans); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /CCC08S3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | /** 4 | * CCC '08 S3 - Maze 5 | * Question type: Graph Theory 6 | * 5/5 on DMOJ 7 | * @author Tommy Pang 8 | */ 9 | public class CCC08S3 { 10 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | static StringTokenizer st; 12 | public static void main(String[] args) throws IOException{ 13 | int test_cases = Integer.parseInt(br.readLine()); 14 | for (int i = 0; i queue_row = new LinkedList<>(); 28 | Queue queue_col = new LinkedList<>(); 29 | visited[1][1] = true; 30 | queue_row.add(1); 31 | queue_col.add(1); 32 | int distance [][] = new int[r+1][c+1]; 33 | distance[1][1] = 1; 34 | while (!queue_row.isEmpty()){ 35 | int cur_row = queue_row.poll(); 36 | int cur_col = queue_col.poll(); 37 | String cur = maze[cur_row][cur_col]; 38 | if (cur.equals("*")) continue; 39 | else if (cur.equals("+")){ 40 | if (cur_row +1 <= r && !visited[cur_row+1][cur_col] && !maze[cur_row+1][cur_col].equals("*")) {queue_row.add(cur_row+1); queue_col.add(cur_col); visited[cur_row+1][cur_col] = true; distance[cur_row+1][cur_col] = distance[cur_row][cur_col] +1;} 41 | if (cur_row -1 >= 1 && !visited[cur_row-1][cur_col] && !maze[cur_row-1][cur_col].equals("*")) {queue_row.add(cur_row-1); queue_col.add(cur_col); visited[cur_row-1][cur_col] = true; distance[cur_row-1][cur_col] = distance[cur_row][cur_col] +1;} 42 | if (cur_col +1 <= c && !visited[cur_row][cur_col+1] && !maze[cur_row][cur_col+1].equals("*")) {queue_row.add(cur_row); queue_col.add(cur_col+1); visited[cur_row][cur_col+1] = true; distance[cur_row][cur_col+1] = distance[cur_row][cur_col] +1;} 43 | if (cur_col -1 >= 1 && !visited[cur_row][cur_col-1] && !maze[cur_row][cur_col-1].equals("*")) {queue_row.add(cur_row); queue_col.add(cur_col-1); visited[cur_row][cur_col-1] = true; distance[cur_row][cur_col-1] = distance[cur_row][cur_col] +1;} 44 | } 45 | else if (cur.equals("|")){ 46 | if (cur_row +1 <= r && !visited[cur_row+1][cur_col] && !maze[cur_row+1][cur_col].equals("*")) {queue_row.add(cur_row+1); queue_col.add(cur_col); visited[cur_row+1][cur_col] = true; distance[cur_row+1][cur_col] = distance[cur_row][cur_col] +1;} 47 | if (cur_row -1 >= 1 && !visited[cur_row-1][cur_col] && !maze[cur_row-1][cur_col].equals("*")) {queue_row.add(cur_row-1); queue_col.add(cur_col); visited[cur_row-1][cur_col] = true; distance[cur_row-1][cur_col] = distance[cur_row][cur_col] +1;} 48 | } 49 | else if (cur.equals("-")){ 50 | if (cur_col +1 <= c && !visited[cur_row][cur_col+1] && !maze[cur_row][cur_col+1].equals("*")) {queue_row.add(cur_row); queue_col.add(cur_col+1); visited[cur_row][cur_col+1] = true; distance[cur_row][cur_col+1] = distance[cur_row][cur_col] +1;} 51 | if (cur_col -1 >= 1 && !visited[cur_row][cur_col-1] && !maze[cur_row][cur_col-1].equals("*")) {queue_row.add(cur_row); queue_col.add(cur_col-1); visited[cur_row][cur_col-1] = true; distance[cur_row][cur_col-1] = distance[cur_row][cur_col] +1;} 52 | } 53 | } 54 | if (visited[r][c]) System.out.println(distance[r][c]); 55 | else System.out.println(-1); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /CCC17S3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.StringTokenizer; 5 | /** 6 | * CCC '17 S3 - Nailed It! 7 | * Question type: Simple Math 8 | * 15/15 on DMOJ 9 | * Question URL: https://dmoj.ca/problem/ccc17s3 10 | * @author Rivers47 on GitHub 11 | */ 12 | public class CCC17S3 { 13 | static StringTokenizer st; 14 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 | static int N, fence_length, height_cnt; 16 | static int[] woods = new int[2001], res_length = new int[4001]; 17 | public static void main(String[] args) throws IOException{ 18 | N = Integer.parseInt(br.readLine()); 19 | st = new StringTokenizer(br.readLine()); 20 | for (int i = 0; i < N; i++) { 21 | woods[Integer.parseInt(st.nextToken())] += 1; 22 | } 23 | cnt(); 24 | search(); 25 | System.out.println(fence_length + " " + height_cnt); 26 | } 27 | static void cnt(){ 28 | for (int i = 1; i <= 2000; i++) { 29 | if (woods[i]!=0){ 30 | if (woods[i]>1){ 31 | res_length[i*2]+=woods[i]/2; 32 | } 33 | for (int j = i+1; j <= 2000; j++) { 34 | if (woods[j]!=0) res_length[i+j] += Math.min(woods[i], woods[j]); 35 | } 36 | } 37 | } 38 | } 39 | static void search(){ 40 | fence_length = 1; height_cnt = 0; 41 | for (int i = 1; i <= 4000; i++) { 42 | if (res_length[i]>fence_length){ 43 | fence_length = res_length[i]; 44 | height_cnt = 1; 45 | } 46 | else if (res_length[i] == fence_length){ 47 | height_cnt+=1; 48 | } 49 | } 50 | } 51 | } 52 | /* 53 | TLE 5/15 on CCC 54 | public class CCC17S3 { 55 | static StringTokenizer st; 56 | static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 57 | static int N, fence_length, board_height; 58 | static int[] woods; 59 | static boolean [] vis; 60 | static ArrayList heights = new ArrayList<>(); 61 | public static void main(String[] args) throws IOException{ 62 | N = Integer.parseInt(br.readLine()); 63 | woods = new int[N+1]; vis = new boolean[N+1]; 64 | st = new StringTokenizer(br.readLine()); 65 | for (int i = 1; i <= N; i++) { 66 | woods[i] = (Integer.parseInt(st.nextToken())); 67 | } 68 | Arrays.sort(woods); 69 | fence_length = 1; 70 | board_height = 1; 71 | int ans = 0; 72 | for (int i = 1; i < N; i++) { 73 | for (int j = i + 1; j <= N; j++) { 74 | if (!vis[i] && !vis[j]) { 75 | vis[i] = true; vis[j] = true; 76 | check(woods[i] + woods[j]); 77 | if (ans