A. Minimal Square
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int a, b; void solve() { cin >> a >> b; if (a < b) swap(a, b); cout << max(a * a, b * b * 4) << endl; } int main() { int t = 1; cin >> t; while (t--) { solve(); } }
B. Honest Coach
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, a[60], ans; void solve() { cin >> n; ans = 1010; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + n + 1); rep(i, 1, n - 1) ans = min(ans, a[i + 1] - a[i]); cout << ans << endl; } int main() { int t = 1; cin >> t; while (t--) { solve(); } }
C. Similar Pairs
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, a[60], cnt, flag; void solve() { cin >> n; cnt = flag = 0; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + n + 1); a[n + 1] = 0; rep(i, 1, n) { if (a[i + 1] - a[i] == 1) flag = 1; if (a[i] % 2) cnt++; } if (flag || cnt % 2 == 0) puts("YES"); else puts("NO"); } int main() { int t = 1; cin >> t; while (t--) { solve(); } }
D. Buying Shovels
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) ll n, k, ans; void solve() { cin >> n >> k; ans = 0; for (ll i = 1; i * i <= n; i++) if (n % i == 0) { if (i <= k) ans = max(ans, i); if (n / i <= k) ans = max(ans, n / i); } cout << n / ans << endl; } int main() { int t = 1; cin >> t; while (t--) { solve(); } }
E. Polygon
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) ll n, mp[60][60]; char f; int flag = 1; void solve() { cin >> n; flag = 1; rep(i, 1, n) rep(j, 1, n) { cin >> f; mp[i][j] = f == '1'; } rep(i, 1, n) rep(j, 1, n) if (i != n && j != n && mp[i][j] && !(mp[i + 1][j] || mp[i][j + 1])) flag = 0; puts(flag ? "YES" : "NO"); } int main() { int t = 1; cin >> t; while (t--) { solve(); } }
F. Spy-string
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, m; char mp[60][60], f; char ans[60]; int flag = 1, cnt; void solve() { cin >> n >> m; flag = 1; rep(i, 1, n) rep(j, 1, m) cin >> mp[i][j]; memset(ans, 0, sizeof(ans)); rep(i, 1, m) ans[i] = mp[1][i]; rep(i, 1, m) for (f = 'a'; f <= 'z'; f++) { ans[i] = f; cnt = 0; rep(j, 2, n) { cnt = 0; rep(k, 1, m) if (ans[k] != mp[j][k]) cnt++; if (cnt > 1) break; } if (cnt < 2) { cout << ans + 1 << endl; return; } ans[i] = mp[1][i]; } puts("-1"); } int main() { int t = 1; cin >> t; while (t--) { solve(); } }