buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 100000000; const int tetra_size = 185; int tetra[tetra_size]; int tri[tetra_size]; void settetra() { tetra[0] = 0; tri[0] = 0; for (int i = 1; i < tetra_size; i++) { tri[i] = tri[i - 1] + i; tetra[i] = tetra[i - 1] + tri[i]; } } int dp[1000001]; int dpodd[1000001]; void setdp() { fill(dp, dp + 1000001, INF); dp[0] = 0; fill(dpodd, dpodd + 1000001, INF); dpodd[0] = 0; for (int i = tetra_size; i >= 1; i--) { for (int j = 0; j + tetra[i] < 1000001; j++) { if (dp[j] == INF) continue; dp[j + tetra[i]] = min(dp[j] + 1, dp[j + tetra[i]]); if (tetra[i] % 2 == 1) dpodd[j + tetra[i]] = min(dpodd[j] + 1, dpodd[j + tetra[i]]); } } } int main() { settetra(); setdp(); int n; while (cin >> n) { // cout << tetra[n] << endl; cout << dp[n] << ' ' << dpodd[n] << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 100000000; const int tetra_size = 185; int tetra[tetra_size]; int tri[tetra_size]; void settetra() { tetra[0] = 0; tri[0] = 0; for (int i = 1; i < tetra_size; i++) { tri[i] = tri[i - 1] + i; tetra[i] = tetra[i - 1] + tri[i]; } } int dp[1000001]; int dpodd[1000001]; void setdp() { fill(dp, dp + 1000001, INF); dp[0] = 0; fill(dpodd, dpodd + 1000001, INF); dpodd[0] = 0; for (int i = tetra_size; i >= 1; i--) { for (int j = 0; j + tetra[i] < 1000001; j++) { if (dp[j] == INF) continue; dp[j + tetra[i]] = min(dp[j] + 1, dp[j + tetra[i]]); if (tetra[i] % 2 == 1) dpodd[j + tetra[i]] = min(dpodd[j] + 1, dpodd[j + tetra[i]]); } } } int main() { settetra(); setdp(); int n; while (cin >> n, n) { // cout << tetra[n] << endl; cout << dp[n] << ' ' << dpodd[n] << endl; } return 0; }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
388
import java.util.Scanner; public class Main { public static void main(String[] args) { int millennium = getNumberOfDates(1000, 1, 1); Scanner s = new Scanner(System.in); int line = s.nextInt(); for (int i = 1; i < line; i++) { int year = s.nextInt(); int month = s.nextInt(); int date = s.nextInt(); System.out.println(millennium - getNumberOfDates(year, month, date)); } } private static int getNumberOfDates(int year, int month, int date) { int numberOfDates = 0; for (int i = 0; i < year; i++) { int datesInOddMonth = 20; int datesInEvenMonth = 19; if (i % 3 == 0) { datesInEvenMonth = 20; } numberOfDates += (datesInOddMonth + datesInEvenMonth) * 5; } for (int j = 1; j < month; j++) { if (year % 3 == 0) { numberOfDates += 20; } else if (j % 2 == 0) { numberOfDates += 19; } else { numberOfDates += 20; } } return numberOfDates + date; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { int millennium = getNumberOfDates(1000, 1, 1); Scanner s = new Scanner(System.in); int line = s.nextInt(); for (int i = 0; i < line; i++) { int year = s.nextInt(); int month = s.nextInt(); int date = s.nextInt(); System.out.println(millennium - getNumberOfDates(year, month, date)); } } private static int getNumberOfDates(int year, int month, int date) { int numberOfDates = 0; for (int i = 1; i < year; i++) { int datesInOddMonth = 20; int datesInEvenMonth = 19; if (i % 3 == 0) { datesInEvenMonth = 20; } numberOfDates += (datesInOddMonth + datesInEvenMonth) * 5; } for (int j = 1; j < month; j++) { if (year % 3 == 0) { numberOfDates += 20; } else if (j % 2 == 0) { numberOfDates += 19; } else { numberOfDates += 20; } } return numberOfDates + date; } }
[["-", 8, 196, 0, 7, 502, 503, 49, 200, 51, 499], ["+", 8, 196, 0, 7, 502, 503, 49, 200, 51, 499]]
3
245
#include <stdio.h> int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int y, m, d; scanf("%d%d%d", &y, &m, &d); int answer = 1; for (int j = y; j < 1000; j++) { if (j % 3) { answer += 195; } else { answer += 200; } } if (y % 3) { for (int k = 1; k < m; k++) { if (m % 2) { answer -= 20; } else { answer -= 19; } } } else { for (int k = 1; k < m; k++) { answer -= 20; } } answer -= d; printf("%d\n", answer); } }
#include <stdio.h> int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int y, m, d; scanf("%d%d%d", &y, &m, &d); int answer = 1; for (int j = y; j < 1000; j++) { if (j % 3) { answer += 195; } else { answer += 200; } } if (y % 3) { for (int k = 1; k < m; k++) { if (k % 2) { answer -= 20; } else { answer -= 19; } } } else { for (int k = 1; k < m; k++) { answer -= 20; } } answer -= d; printf("%d\n", answer); } }
[["-", 8, 9, 0, 57, 15, 23, 0, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 31, 22]]
0
180
Y, M, D; main(n) { for (scanf("%d", &n); Y = n--; scanf("%d%d%d", &Y, &M, &D)) Y--, M--, printf("%d\n", 196471 - Y * 195 - Y / 3 * 5 - M * 20 + ((Y + 1) % 3 ? M / 2 : 0) - D); }
Y, M, D; main(n) { for (scanf("%d", &n); Y = n--; scanf("%d%d%d", &Y, &M, &D), Y--, M--, printf("%d\n", 196471 - Y * 195 - Y / 3 * 5 - M * 20 + (++Y % 3 ? M / 2 : 0) - D)) ; }
[["-", 0, 30, 0, 14, 8, 9, 0, 7, 0, 25], ["+", 0, 14, 8, 9, 0, 7, 26, 34, 0, 21], ["-", 3, 4, 0, 16, 31, 16, 12, 23, 0, 24], ["+", 12, 23, 0, 41, 15, 16, 31, 27, 17, 29], ["-", 0, 41, 15, 16, 31, 23, 0, 16, 17, 72], ["-", 0, 41, 15, 16, 31, 23, 0, 16, 12, 13], ["-", 12, 23, 0, 41, 15, 16, 31, 23, 0, 25], ["+", 12, 34, 12, 34, 12, 2, 3, 4, 0, 25]]
0
93
#include <stdio.h> int main() { int s, n, y, m, d, t, i; s = 39 * 5 * 999 + 333 * 5 + 1; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d%d", &y, &m, &d); if (m % 2 == 0) t = (y - 1) * 39 * 5 + ((m - 1) / 2) * 20 + ((m - 1) / 2) * 19 + d + 5 * ((y - 1) / 3); else t = (y - 1) * 39 * 5 + ((m - 1) / 2) * 39 + d + 5 * ((y - 1) / 3); if (y % 3 == 0 && m % 2 == 1) t = t + m / 2; else if (y % 3 == 0 && m % 2 == 0) t = t + m / 2 - 1; printf("%d\n", s - t); } return 0; }
#include <stdio.h> int main() { int s, n, y, m, d, t, i; s = 39 * 5 * 999 + 333 * 5 + 1; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d%d", &y, &m, &d); if (m % 2 == 0) t = (y - 1) * 39 * 5 + (m / 2) * 20 + ((m - 1) / 2) * 19 + d + 5 * ((y - 1) / 3); else t = (y - 1) * 39 * 5 + ((m - 1) / 2) * 39 + d + 5 * ((y - 1) / 3); if (y % 3 == 0 && m % 2 == 1) t = t + m / 2; else if (y % 3 == 0 && m % 2 == 0) t = t + m / 2 - 1; printf("%d\n", s - t); } return 0; }
[["-", 12, 16, 31, 23, 0, 16, 31, 23, 0, 24], ["-", 31, 23, 0, 16, 31, 23, 0, 16, 17, 33], ["-", 31, 23, 0, 16, 31, 23, 0, 16, 12, 13], ["-", 12, 16, 31, 23, 0, 16, 31, 23, 0, 25]]
0
237
#include <stdio.h> int main() { int n, i, j, All = 196471, Y, M, D, sum; scanf("%d", &n); for (j = 0, sum = 0; j < n; j++, sum = 0) { scanf("%d %d %d", &Y, &M, &D); for (i = 1; i < Y; i++) { if (j % 3 == 0) { sum += 200; } else { sum += 195; } } for (i = 1; i < M; i++) { if (Y % 3 == 0 || i % 2 == 1) { sum += 20; } else { sum += 19; } } sum += D; printf("%d\n", All - sum); } return 0; }
#include <stdio.h> int main() { int n, i, j, All = 196471, Y, M, D, sum; scanf("%d", &n); for (j = 0, sum = 0; j < n; j++, sum = 0) { scanf("%d %d %d", &Y, &M, &D); for (i = 1; i < Y; i++) { if (i % 3 == 0) { sum += 200; } else { sum += 195; } } for (i = 1; i < M; i++) { if (Y % 3 == 0 || i % 2 == 1) { sum += 20; } else { sum += 19; } } sum += D; printf("%d\n", All - sum); } return 0; }
[["-", 0, 57, 15, 23, 0, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 23, 0, 16, 31, 16, 31, 22]]
0
173
#include <stdio.h> int main() { int n, y, m, d; int year_sum[1024] = {0}; int month_sum[16] = {0}; int sum = 0; int i; for (i = 1; i < 1024; i++) { if (i % 3 == 0) year_sum[i] = 20 * 10 + year_sum[i - 1]; else year_sum[i] = 20 * 5 + 19 * 5 + year_sum[i - 1]; } for (i = 1; i < 16; i++) { if (i % 2) month_sum[i] = 19 + month_sum[i - 1]; else month_sum[i] = 20 + month_sum[i - 1]; } scanf("%d", &n); for (i = 0; i < n; i++) { int temp = 0; scanf("%d %d %d", &y, &m, &d); temp += year_sum[y - 1]; if (y % 3 == 0) temp += 20 * (m - 1); else temp += month_sum[m - 1]; temp += d; printf("%d\n", year_sum[999] + 1 - temp); } return 0; }
#include <stdio.h> int main() { int n, y, m, d; int year_sum[1024] = {0}; int month_sum[16] = {0}; int sum = 0; int i; for (i = 1; i < 1024; i++) { if (i % 3 == 0) year_sum[i] = 20 * 10 + year_sum[i - 1]; else year_sum[i] = 20 * 5 + 19 * 5 + year_sum[i - 1]; } for (i = 1; i < 16; i++) { if (i % 2 == 0) month_sum[i] = 19 + month_sum[i - 1]; else month_sum[i] = 20 + month_sum[i - 1]; } scanf("%d", &n); for (i = 0; i < n; i++) { int temp = 0; scanf("%d %d %d", &y, &m, &d); temp += year_sum[y - 1]; if (y % 3 == 0) temp += 20 * (m - 1); else temp += month_sum[m - 1]; temp += d; printf("%d\n", year_sum[999] + 1 - temp); } return 0; }
[["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 60], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 12, 13]]
0
262
#include <stdio.h> int main(void) { int i, n; scanf("%d", &n); for (i = 0; i < n; i++) { int y, m, d, total = 0; scanf("%d %d %d", &y, &m, &d); if (y % 3 == 0) { total += (999 - y) / 3 * 590; total += (10 - m) * 20; total += 21 - d; } else if (y % 3 == 1) { total += (997 - y) / 3 * 590 + 395; if (m % 2 == 1) { total += 19 + (9 - m) / 2 * 39; total += 21 - d; } else if (m % 2 == 0) { total += (10 - m) / 2 * 39; total += 20 - d; } } else if (y % 3 == 2) { total += (998 - y) / 3 * 590 + 195; if (m % 2 == 1) { total += 19 + (9 - m) / 2 * 39; total += 21 - d; } else if (m % 2 == 0) { total += (10 - m) / 2 * 39; total += 20 - d; } } printf("%d\n", total); } return 0; }
#include <stdio.h> int main(void) { int i, n; scanf("%d", &n); for (i = 0; i < n; i++) { int y, m, d, total = 0; scanf("%d %d %d", &y, &m, &d); if (y % 3 == 0) { total += (999 - y) / 3 * 590; total += (10 - m) * 20; total += 21 - d; } else if (y % 3 == 1) { total += (997 - y) / 3 * 590 + 395; if (m % 2 == 1) { total += 19 + (9 - m) / 2 * 39; total += 21 - d; } else if (m % 2 == 0) { total += (10 - m) / 2 * 39; total += 20 - d; } } else if (y % 3 == 2) { total += (998 - y) / 3 * 590 + 200; if (m % 2 == 1) { total += 19 + (9 - m) / 2 * 39; total += 21 - d; } else if (m % 2 == 0) { total += (10 - m) / 2 * 39; total += 20 - d; } } printf("%d\n", total); } return 0; }
[["-", 64, 9, 0, 1, 0, 11, 12, 16, 12, 13], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
0
285
#include <cstdio> using namespace std; int n; int y, m, d; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d%d", &y, &m, &d); int cnt = 0; while (!(y == 1000 && m == 1 && d == 1)) { int days = 19; if (y % 3 == 0 || m % 2 == 0) days = 20; cnt += days - d + 1; d = 1; m++; if (m == 11) { m = 1; y++; } } printf("%d\n", cnt); } }
#include <cstdio> using namespace std; int n; int y, m, d; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d%d", &y, &m, &d); int cnt = 0; while (!(y == 1000 && m == 1 && d == 1)) { int days = 19; if (y % 3 == 0 || m % 2 == 1) days = 20; cnt += days - d + 1; d = 1; m++; if (m == 11) { m = 1; y++; } } printf("%d\n", cnt); } }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
1
151
#include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int Y, M, D; cin >> Y >> M >> D; int offset = (10 - M + 1) * 20 - (D - 1); if (Y % 3 != 0) { offset -= (10 - M + 1) / 2; //小の月の分を引く。 int/intだが問題ない } Y++; int uruutime = (1002 - Y) / 3; int ans = offset + 195 * (1000 - Y) + uruutime * 5; cout << ans << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int Y, M, D; cin >> Y >> M >> D; int offset = (10 - M + 1) * 20 - (D - 1); if (Y % 3 != 0) { offset -= (12 - M) / 2; //小の月の分を引く。 int/intだが問題ない } Y++; int uruutime = (1002 - Y) / 3; int ans = offset + 195 * (1000 - Y) + uruutime * 5; cout << ans << endl; } return 0; }
[["-", 12, 16, 31, 23, 0, 16, 31, 16, 31, 13], ["+", 0, 11, 12, 16, 31, 23, 0, 16, 31, 13], ["-", 0, 11, 12, 16, 31, 23, 0, 16, 17, 72], ["-", 0, 11, 12, 16, 31, 23, 0, 16, 12, 13]]
1
132
#include <bits/stdc++.h> #define _ \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; const int mill = 196471; const int nYear = (19 + 20) * 5; int main() { _; int n, y, m, d; cin >> n; REP(i, n) { cin >> y >> m >> d; int days = mill; days -= nYear * (y - 1) + 5 * ((y - 1) / 3); REP(j, m - 1) days -= (y % 3 == 0 || j % 2 == 1) ? 20 : 19; days -= d; cout << days << endl; } }
#include <bits/stdc++.h> #define _ \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; const int mill = 196471; const int nYear = (19 + 20) * 5; int main() { _; int n, y, m, d; cin >> n; REP(i, n) { cin >> y >> m >> d; int days = mill; days -= nYear * (y - 1) + 5 * ((y - 1) / 3); REP(j, m - 1) days -= (y % 3 == 0 || (j + 1) % 2 == 1) ? 20 : 19; days -= d; cout << days << endl; } }
[["+", 0, 16, 12, 16, 31, 16, 31, 23, 0, 24], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 17, 72], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 12, 13], ["+", 0, 16, 12, 16, 31, 16, 31, 23, 0, 25]]
1
138
#include <iostream> using namespace std; int main() { int n, y, m, d; int y3, yd, m2, md, dd, y2; cin >> n; for (int i = 0; i < n; i++) { cin >> y >> m >> d; if (y % 3 != 0) y2 = y + (3 - y % 3); else y2 = y; y3 = 334 - y2 / 3; yd = y3 * 200 + (1000 - y - y3) * 195; m2 = m / 2; if (y % 3 != 0) { md = yd - 39 * m2; if (m % 2 == 1) { dd = md - d + 1; } else { dd = md + (19 - d + 1); } } else { md = yd - 20 * m; dd = md + 20 - d; } cout << dd << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n, y, m, d; int y3, yd, m2, md, dd, y2; cin >> n; for (int i = 0; i < n; i++) { cin >> y >> m >> d; if (y % 3 != 0) y2 = y + (3 - y % 3); else y2 = y; y3 = 334 - y2 / 3; yd = y3 * 200 + (1000 - y - y3) * 195; m2 = m / 2; if (y % 3 != 0) { md = yd - 39 * m2; if (m % 2 == 1) { dd = md - d + 1; } else { dd = md + (19 - d + 1); } } else { md = yd - 20 * m; dd = md + 20 - d + 1; } cout << dd << endl; } return 0; }
[["+", 0, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 0, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
196
#include <iostream> using namespace std; int main() { int y, m, d, i, times, D1, D2, D3; cin >> times; i = 0; while (1) { i++; if (i > times) { break; } cin >> y >> m >> d; D1 = (y - 1) / 3 * 200 + ((y - 1) - (y - 1) / 3) * 195; if (y % 3 == 0) { D2 = (m - 1) * 20 + d; } else { D2 = (m - 1) * 20 - (m - 1) / 2 + d; } D3 = 196471 - D1 + D2; cout << D3 << endl; } return 0; }
#include <iostream> using namespace std; int main() { int y, m, d, i, times, D1, D2, D3; cin >> times; i = 0; while (1) { i++; if (i > times) { break; } cin >> y >> m >> d; D1 = (y - 1) / 3 * 200 + ((y - 1) - (y - 1) / 3) * 195; if (y % 3 == 0) { D2 = (m - 1) * 20 + d; } else { D2 = (m - 1) * 20 - (m - 1) / 2 + d; } D3 = 196471 - D1 - D2; cout << D3 << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 33]]
1
156
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0, num[3], ans; i < n; i++) { for (int j = 0; j < 3; j++) cin >> num[j]; num[0]--; num[1]--; if ((num[0] + 1) % 3 == 0) ans = num[1] * 20; else ans = num[1] / 2 * 39 + num[1] % 2 * 20; cout << 196471 - ans - num[0] / 3 * 590 + num[0] % 3 * 195 + num[2] << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0, num[3], ans; i < n; i++) { for (int j = 0; j < 3; j++) cin >> num[j]; num[0]--; num[1]--; if ((num[0] + 1) % 3 == 0) ans = num[1] * 20; else ans = num[1] / 2 * 39 + num[1] % 2 * 20; cout << 196471 - ans - num[0] / 3 * 590 - num[0] % 3 * 195 - num[2] << endl; } }
[["-", 0, 16, 31, 16, 12, 16, 31, 16, 17, 72], ["+", 0, 16, 31, 16, 12, 16, 31, 16, 17, 33], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33]]
1
151
#include <iostream> using namespace std; int main() { int n, i, c, y, m, d; cin >> n; while (n--) { c = y = m = d = 0; cin >> y >> m >> d; if (y % 3 != 0 && m % 2 == 0) c += 20 - d; else c += 21 - d; for (i = m + 1; m <= 10; m++) { if (y % 3 != 0 && m % 2 == 0) c += 19; else c += 20; } for (i = y + 1; y <= 999; y++) { if (y % 3 == 0) c += 200; else c += 195; } cout << c << endl; } }
#include <iostream> using namespace std; int main() { int n, i, c, y, m, d; cin >> n; while (n--) { c = y = m = d = 0; cin >> y >> m >> d; if (y % 3 != 0 && m % 2 == 0) c += 20 - d; else c += 21 - d; for (i = m + 1; i <= 10; i++) { if (y % 3 != 0 && i % 2 == 0) c += 19; else c += 20; } for (i = y + 1; i <= 999; i++) { if (i % 3 == 0) c += 200; else c += 195; } cout << c << endl; } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 31, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 31, 22], ["-", 0, 52, 8, 9, 0, 7, 26, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 7, 26, 27, 28, 22], ["-", 15, 339, 51, 16, 12, 16, 31, 16, 31, 22], ["+", 15, 339, 51, 16, 12, 16, 31, 16, 31, 22], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22]]
1
161
#include "bits/stdc++.h" using namespace std; //#define int long long #define DBG 0 #define dump(o) \ if (DBG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++) #define all(c) c.begin(), c.end() const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); int f(int Y, int M, int D) { int ret = 0; ret += ((Y - 1) / 3) * 10 * 20; ret += ((Y - 1) - (Y - 1) / 3) * (5 * 19 + 5 * 20); dump(ret); ret += Y % 3 ? (M - 1) / 2 * 20 + (M - 2) / 2 * 19 : (M - 1) * 20; ret += D; return ret; } signed main() { int n; cin >> n; rep(i, 0, n) { int Y, M, D; cin >> Y >> M >> D; int ans = f(1000, 1, 1) - f(Y, M, D); cout << ans << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; //#define int long long #define DBG 0 #define dump(o) \ if (DBG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++) #define all(c) c.begin(), c.end() const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); int f(int Y, int M, int D) { int ret = 0; ret += ((Y - 1) / 3) * 10 * 20; ret += ((Y - 1) - (Y - 1) / 3) * (5 * 19 + 5 * 20); dump(ret); ret += Y % 3 ? M / 2 * 20 + (M - 1) / 2 * 19 : (M - 1) * 20; ret += D; return ret; } signed main() { int n; cin >> n; rep(i, 0, n) { int Y, M, D; cin >> Y >> M >> D; int ans = f(1000, 1, 1) - f(Y, M, D); cout << ans << endl; } return 0; }
[["-", 64, 16, 31, 16, 31, 16, 31, 23, 0, 24], ["-", 31, 16, 31, 16, 31, 23, 0, 16, 17, 33], ["-", 31, 16, 31, 16, 31, 23, 0, 16, 12, 13], ["-", 64, 16, 31, 16, 31, 16, 31, 23, 0, 25], ["-", 12, 16, 31, 16, 31, 23, 0, 16, 12, 13], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 12, 13]]
1
270
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) int main() { int n, Y[110], M[110], D[110], sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> Y[i] >> M[i] >> D[i]; sum = 0; if (Y[i] % 3 == 0) { for (int j = 1; j < Y[i]; j++) { if (j % 3 == 0) sum += 200; else sum += 195; } sum += (M[i] - 1) * 20 + D[i]; } else { for (int j = 1; j < Y[i]; j++) { if (j % 3 == 0) sum += 200; else sum += 195; } for (int j = 1; j < M[i]; j++) { if (j % 2 == 1) sum += 19; else sum += 20; } sum += D[i]; } cout << 196471 - sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) int main() { int n, Y[110], M[110], D[110], sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> Y[i] >> M[i] >> D[i]; sum = 0; if (Y[i] % 3 == 0) { for (int j = 1; j < Y[i]; j++) { if (j % 3 == 0) sum += 200; else sum += 195; } sum += (M[i] - 1) * 20 + D[i]; } else { for (int j = 1; j < Y[i]; j++) { if (j % 3 == 0) sum += 200; else sum += 195; } for (int j = 1; j < M[i]; j++) { if (j % 2 == 1) sum += 20; else sum += 19; } sum += D[i]; } cout << 196471 - sum << endl; } return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["-", 0, 57, 75, 76, 0, 1, 0, 11, 12, 13], ["+", 0, 57, 75, 76, 0, 1, 0, 11, 12, 13]]
1
243
#include "bits/stdc++.h" using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int d1[10] = {20, 19, 20, 19, 20, 19, 20, 19, 20, 19}; int d2[10] = {20, 20, 20, 20, 20, 20, 20, 20, 20, 20}; int Sd1 = 20 * 10 - 5; int Sd2 = 20 * 10; int SD = 196471; int n; cin >> n; int Y, M, D; int Sum = 0; for (int i = 0; i < n; i++) { cin >> Y >> M >> D; Sum += (Y - 1) / 3 * Sd2 + ((Y - 1) - (Y - 1) / 3) * Sd1; if (Y % 3 != 0) { for (int j = 0; j < M - 1; j++) { Sum += d1[j]; } Sum += D; } else { for (int j = 0; j < M - 1; j++) { Sum += d2[j]; } Sum += D; } cout << SD - Sum << endl; } }
#include "bits/stdc++.h" using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int d1[10] = {20, 19, 20, 19, 20, 19, 20, 19, 20, 19}; int d2[10] = {20, 20, 20, 20, 20, 20, 20, 20, 20, 20}; int Sd1 = 20 * 10 - 5; int Sd2 = 20 * 10; int SD = 196471; int n; cin >> n; int Y, M, D; int Sum = 0; for (int i = 0; i < n; i++) { Sum = 0; cin >> Y >> M >> D; Sum += (Y - 1) / 3 * Sd2 + ((Y - 1) - (Y - 1) / 3) * Sd1; if (Y % 3 != 0) { for (int j = 0; j < M - 1; j++) { Sum += d1[j]; } Sum += D; } else { for (int j = 0; j < M - 1; j++) { Sum += d2[j]; } Sum += D; } cout << SD - Sum << endl; } }
[["+", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
1
257
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int mill = 1 + 999 * (20 * 10 - 5) + 333 * 5; for (int i = 0; i < n; ++i) { int y, m, d; cin >> y >> m >> d; int byear = (y - 1) / 3; int ans = d; if (y % 3 == 0) { ans += (m - 1) * 20; } else { ans += (m - 1) * 19 + (m - 1) / 2; } ans += (y - 1) * (20 * 10 - 5) + byear * 5; ans = mill - ans; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int mill = 1 + 999 * (20 * 10 - 5) + 333 * 5; for (int i = 0; i < n; ++i) { int y, m, d; cin >> y >> m >> d; int byear = (y - 1) / 3; int ans = d; if (y % 3 == 0) { ans += (m - 1) * 20; } else { ans += (m - 1) * 19 + m / 2; } ans += (y - 1) * (20 * 10 - 5) + byear * 5; ans = mill - ans; cout << ans << endl; } return 0; }
[["-", 0, 11, 12, 16, 12, 16, 31, 23, 0, 24], ["-", 12, 16, 12, 16, 31, 23, 0, 16, 17, 33], ["-", 12, 16, 12, 16, 31, 23, 0, 16, 12, 13], ["-", 0, 11, 12, 16, 12, 16, 31, 23, 0, 25]]
1
161
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <set> #include <stack> #include <vector> using namespace std; int hoge[1000][10][20]; void Ini() { int cnt = 196470; for (int i = 1; i <= 1000; i++) { for (int j = 1; j <= 10; j++) { int day = 19; if (i % 3 == 0 || j % 2 == 0) day = 20; for (int k = 1; k <= day; k++) { hoge[i - 1][j - 1][k - 1] = cnt; cnt--; } } } } bool solve() { Ini(); int n; cin >> n; for (int i = 0; i < n; i++) { int y, m, d; cin >> y >> m >> d; cout << hoge[y - 1][m - 1][d - 1] << endl; } return true; } int main() { cout.setf(ios::fixed); cout.precision(10); solve(); return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <set> #include <stack> #include <vector> using namespace std; int hoge[1000][10][20]; void Ini() { int cnt = 196470; for (int i = 1; i < 1000; i++) { for (int j = 1; j <= 10; j++) { int day = 19; if (i % 3 == 0 || j % 2 == 1) day = 20; for (int k = 1; k <= day; k++) { hoge[i - 1][j - 1][k - 1] = cnt; cnt--; } } } } bool solve() { Ini(); int n; cin >> n; for (int i = 0; i < n; i++) { int y, m, d; cin >> y >> m >> d; cout << hoge[y - 1][m - 1][d - 1] << endl; } return true; } int main() { cout.setf(ios::fixed); cout.precision(10); solve(); return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
1
247
#include <iostream> #include <stdlib.h> using namespace std; int n; int y; int m; int d; int main() { cin >> n; for (int i = 0; i < n; i++) { int days = 0; cin >> y >> m >> d; if (d != 1) { if (m % 2 == 1 || y % 3 == 0) { days += 20 - d; } else { days += 19 - d; } m++; } while (m < 11) { if (m % 2 == 1 || y % 3 == 0) { days += 20; } else { days += 19; } m++; } y++; while (y < 1000) { if (y % 3 == 0) { days += 20 * 10; } else { days += 20 * 5 + 19 * 5; } y++; } cout << days << endl; } return 0; }
#include <iostream> #include <stdlib.h> using namespace std; int n; int y; int m; int d; int main() { cin >> n; for (int i = 0; i < n; i++) { int days = 0; cin >> y >> m >> d; if (d != 1) { if (m % 2 == 1 || y % 3 == 0) { days += 21 - d; } else { days += 20 - d; } m++; } while (m < 11) { if (m % 2 == 1 || y % 3 == 0) { days += 20; } else { days += 19; } m++; } y++; while (y < 1000) { if (y % 3 == 0) { days += 20 * 10; } else { days += 20 * 5 + 19 * 5; } y++; } cout << days << endl; } return 0; }
[["-", 64, 9, 0, 1, 0, 11, 12, 16, 31, 13], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 31, 13], ["-", 0, 9, 0, 1, 0, 11, 12, 16, 31, 13], ["+", 0, 9, 0, 1, 0, 11, 12, 16, 31, 13]]
1
191
import sys if sys.version_info[0]>=3: raw_input=input n=int(raw_input()) for i in range(0,n): a=[int(e) for e in raw_input().split()] a[0]-=1;a[1]-=1 print(196471-a[0]*195-a[0]/3*5-a[1]*20+(a[1]/2 if a[0]%3!=2 else 0)-a[2])
import sys if sys.version_info[0]>=3: raw_input=input n=int(raw_input()) for i in range(0,n): a=[int(e) for e in raw_input().split()] a[0]-=1;a[1]-=1 print(196471-a[0]*195-a[0]//3*5-a[1]*20+(a[1]//2 if a[0]%3!=2 else 0)-a[2])
[["-", 31, 657, 31, 657, 12, 657, 31, 657, 17, 85], ["+", 31, 657, 31, 657, 12, 657, 31, 657, 17, 676], ["-", 31, 657, 12, 23, 0, 41, 0, 657, 17, 85], ["+", 31, 657, 12, 23, 0, 41, 0, 657, 17, 676]]
5
116
def day(y, m, d): days = 0 days += (y-1)*195 days += (y-1)//3*5 days += (m-1)*19 if y%3 == 0: days += (m-1) else: days += (m-1)//2 days += (d-1) return days def main(): n = int(input()) for _ in range(n): (y, m, d) = [int(x) for x in input().split()] print(day(1000,1,1) - day(y,m,d)) if __name__ == '__main__': main()
def day(y, m, d): days = 0 days += (y-1)*195 days += (y-1)//3*5 days += (m-1)*19 if y%3 == 0: days += (m-1) else: days += m//2 days += (d-1) return days def main(): n = int(input()) for _ in range(n): (y, m, d) = [int(x) for x in input().split()] print(day(1000,1,1) - day(y,m,d)) if __name__ == '__main__': main()
[["-", 0, 1, 0, 677, 12, 657, 31, 23, 0, 24], ["-", 0, 677, 12, 657, 31, 23, 0, 657, 17, 33], ["-", 0, 677, 12, 657, 31, 23, 0, 657, 12, 612], ["-", 0, 1, 0, 677, 12, 657, 31, 23, 0, 25]]
5
151
M = [20, 19] Y = sum(M) * 5 for i in range(int(input())): res = 0 y,m,d = list(map(int,input().split())) for y_2 in range(1000-1, y, -1): res += Y if y_2 % 3 != 0 else 200 for m_2 in range(10, m, -1): res += M[m_2 % 2] if y % 3 != 0 else 20 res += (M[m%2] if y % 3 != 0 else 20) - d +1 print(res)
M = [19, 20] Y = sum(M) * 5 for i in range(int(input())): res = 0 y,m,d = list(map(int,input().split())) for y_2 in range(1000-1, y, -1): res += Y if y_2 % 3 != 0 else 200 for m_2 in range(10, m, -1): res += M[m_2 % 2] if y % 3 != 0 else 20 res += (M[m%2] if y % 3 != 0 else 20) - d +1 print(res)
[["-", 0, 656, 0, 1, 0, 662, 12, 634, 0, 612], ["-", 0, 656, 0, 1, 0, 662, 12, 634, 0, 21], ["+", 0, 656, 0, 1, 0, 662, 12, 634, 0, 21], ["+", 0, 656, 0, 1, 0, 662, 12, 634, 0, 612]]
5
133
#include <bits/stdc++.h> using namespace std; vector<int> mini, maxi, arr; int a, l; void num(vector<int> &ll, vector<int> &ss) { int t = 1; int llcp = ll[l - 1], sscp = ss[l - 1]; for (int i = l - 2; i >= 0; i--) { t *= 10; llcp += ll[i] * t; sscp += ss[i] * t; // cout << ll[i] << " " << ss[i] << endl; } // cout << llcp << " " << sscp << endl; arr.push_back(llcp - sscp); for (int i = 0; i < (int)arr.size(); i++) { for (int j = i + 1; j < (int)arr.size(); j++) { if (arr[i] == arr[j]) { cout << j << " " << arr[i] << " " << j - i << endl; return; } } // cout << arr[i] << " " << llcp << " " << sscp << endl; } maxi.clear(); mini.clear(); t = 1; a = arr[arr.size() - 1]; for (int i = 0; i < l; i++) { t *= 10; mini.push_back((a % t) / (t / 10)); maxi.push_back((a % t) / (t / 10)); // cout << (a % t) / (t / 10) << endl; } sort(maxi.rbegin(), maxi.rend()); sort(mini.begin(), mini.end()); num(maxi, mini); } int main() { while (cin >> a >> l && (a || l)) { int t = 1; arr.push_back(a); for (int i = 0; i < l; i++) { t *= 10; mini.push_back((a % t) / (t / 10)); maxi.push_back((a % t) / (t / 10)); // cout << (a % t) / (t / 10) << endl; } sort(maxi.rbegin(), maxi.rend()); sort(mini.begin(), mini.end()); num(maxi, mini); maxi.clear(); mini.clear(); arr.clear(); } }
#include <bits/stdc++.h> using namespace std; vector<int> mini, maxi, arr; int a, l; void num(vector<int> &ll, vector<int> &ss) { int t = 1; int llcp = ll[l - 1], sscp = ss[l - 1]; for (int i = l - 2; i >= 0; i--) { t *= 10; llcp += ll[i] * t; sscp += ss[i] * t; // cout << ll[i] << " " << ss[i] << endl; } // cout << llcp << " " << sscp << endl; arr.push_back(llcp - sscp); for (int i = 0; i < (int)arr.size(); i++) { for (int j = i + 1; j < (int)arr.size(); j++) { if (arr[i] == arr[j]) { cout << i << " " << arr[i] << " " << j - i << endl; return; } } // cout << arr[i] << " " << llcp << " " << sscp << endl; } maxi.clear(); mini.clear(); t = 1; a = arr[arr.size() - 1]; for (int i = 0; i < l; i++) { t *= 10; mini.push_back((a % t) / (t / 10)); maxi.push_back((a % t) / (t / 10)); // cout << (a % t) / (t / 10) << endl; } sort(maxi.rbegin(), maxi.rend()); sort(mini.begin(), mini.end()); num(maxi, mini); } int main() { while (cin >> a >> l && (a || l)) { int t = 1; arr.push_back(a); for (int i = 0; i < l; i++) { t *= 10; mini.push_back((a % t) / (t / 10)); maxi.push_back((a % t) / (t / 10)); // cout << (a % t) / (t / 10) << endl; } sort(maxi.rbegin(), maxi.rend()); sort(mini.begin(), mini.end()); num(maxi, mini); maxi.clear(); mini.clear(); arr.clear(); } }
[["-", 31, 16, 31, 16, 31, 16, 31, 16, 12, 22], ["+", 31, 16, 31, 16, 31, 16, 31, 16, 12, 22]]
1
468
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int a, l; int i, j, k; int king, kingpoint, flag; int rank[10], past[30]; char max[10], min[10]; while (1) { scanf("%d %d", &a, &l); if (a + l == 0) { break; } past[0] = a; for (i = 0;; ++i) { // テ、ツクツ?ヲツ。ツ?」ツ?氾」ツ?ィテ」ツ?ォテ・ツ按?・ツ可イ for (j = 0; j < l; ++j) { rank[j] = a % 10; a /= 10; } // テヲツ。ツ?ヲツ閉ーテ・ツ按?ァツケツーテ」ツつ甘ィツソツ氾」ツ?? for (j = 0; j < l; ++j) { king = 0; // テヲツ慊?・ツ、ツァテ・ツ?、テ」ツつ津ヲツアツづ」ツつ?」ツつ? for (k = 0; k < l; ++k) { if (king < rank[k]) { king = rank[k]; kingpoint = k; } } // printf("test: %d %d\n", king, kingpoint); // テヲツ慊?・ツ、ツァテ・ツ?、テ」ツつ津ゥツ?ゥテ・ツ按?」ツ?ェテヲツ。ツ?」ツ?ォテ、ツサツ」テ・ツ?・ max[j] = min[l - j - 1] = king + '0'; rank[kingpoint] = 0; } // テァツオツづァツォツッテヲツ鳴?・ツュツ療」ツつ津、ツサツ」テ・ツ?・ max[l] = min[l] = '\0'; // printf("test:%s %s\n", max, min); // テヲツ閉ーテ・ツ?、テ」ツ?ォテ・ツ、ツ嘉ヲツ渉? a = atoi(max) - atoi(min); // printf("test:%d %d %d\n", a, atoi(max), atoi(min)); flag = 0; for (j = 0; j < i; j++) { if (past[j] == a) { // テ・ツ?コテ・ツ環? printf("%d %d %d\n", j, a, i - j); flag = 1; break; } } past[i + 1] = a; if (flag == 1) { break; } } } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int a, l; int i, j, k; int king, kingpoint, flag; int rank[10], past[30]; char max[10], min[10]; while (1) { scanf("%d %d", &a, &l); if (a + l == 0) { break; } past[0] = a; for (i = 1;; ++i) { // テ、ツクツ?ヲツ。ツ?」ツ?氾」ツ?ィテ」ツ?ォテ・ツ按?・ツ可イ for (j = 0; j < l; ++j) { rank[j] = a % 10; a /= 10; } // テヲツ。ツ?ヲツ閉ーテ・ツ按?ァツケツーテ」ツつ甘ィツソツ氾」ツ?? for (j = 0; j < l; ++j) { king = 0; // テヲツ慊?・ツ、ツァテ・ツ?、テ」ツつ津ヲツアツづ」ツつ?」ツつ? for (k = 0; k < l; ++k) { if (king < rank[k]) { king = rank[k]; kingpoint = k; } } // テヲツ慊?・ツ、ツァテ・ツ?、テ」ツつ津ゥツ?ゥテ・ツ按?」ツ?ェテヲツ。ツ?」ツ?ォテ、ツサツ」テ・ツ?・ max[j] = min[l - j - 1] = king + '0'; rank[kingpoint] = 0; } // テァツオツづァツォツッテヲツ鳴?・ツュツ療」ツつ津、ツサツ」テ・ツ?・ max[l] = min[l] = '\0'; // テヲツ閉ーテ・ツ?、テ」ツ?ォテ・ツ、ツ嘉ヲツ渉? a = atoi(max) - atoi(min); flag = 0; for (j = 0; j < i; j++) { if (past[j] == a) { // テ・ツ?コテ・ツ環? printf("%d %d %d\n", j, a, i - j); flag = 1; break; } } past[i] = a; if (flag == 1) { break; } } } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
325
#include <iostream> using namespace std; int search(int *a, int N, int *s) { for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (a[i] == a[j]) { *s = j - i; return j; } } } return -1; } int main(void) { while (true) { int L, N = 21; int *a = new int[N]; int i = 0, j; cin >> a[0] >> L; if (a[0] == 0 && L == 0) break; // cout << a[0] << ", " << L << endl; for (i = 1; i < N; i++) { int *n = new int[L]; int ai = a[i - 1], min = 0, max = 0; for (int k = 0; k < L; k++, ai /= 10) { int tmp = ai % 10; // cout << tmp << " "; int h; for (h = 0; h < k && n[h] <= tmp; h++) ; if (h != k) { int tmp2 = n[h]; n[h] = tmp; tmp = tmp2; // ai = ai / 10; for (h++; h < k; h++) { tmp2 = n[h]; n[h] = tmp; tmp = tmp2; } } n[h] = tmp; } // cout << endl; for (/*min = 0, */ int k = 0; k < L; k++) { min = min * 10 + n[k]; } // cout << min << endl; for (/*max = 0, */ int k = L - 1; k >= 0; k--) { max = max * 10 + n[k]; // cout << n[k];// << endl; } // cout << max << endl; // i++; a[i] = max - min; // cout << i << ": " << max << "-" << min << "= " << a[i] << endl; if (a[i - 1] == a[i]) { N = i + 1; break; } } int s; j = search(a, N, &s); cout << j << " "; cout << a[j] << " " << s << endl; } return 0; }
#include <iostream> using namespace std; int search(int *a, int N, int *s) { for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (a[i] == a[j]) { *s = j - i; return i; } } } return -1; } int main(void) { while (true) { int L, N = 21; int *a = new int[N]; int i = 0, j; cin >> a[0] >> L; if (a[0] == 0 && L == 0) break; // cout << a[0] << ", " << L << endl; for (i = 1; i < N; i++) { int *n = new int[L]; int ai = a[i - 1], min = 0, max = 0; for (int k = 0; k < L; k++, ai /= 10) { int tmp = ai % 10; // cout << tmp << " "; int h; for (h = 0; h < k && n[h] <= tmp; h++) ; if (h != k) { int tmp2 = n[h]; n[h] = tmp; tmp = tmp2; // ai = ai / 10; for (h++; h < k; h++) { tmp2 = n[h]; n[h] = tmp; tmp = tmp2; } } n[h] = tmp; } // cout << endl; for (/*min = 0, */ int k = 0; k < L; k++) { min = min * 10 + n[k]; } // cout << min << endl; for (/*max = 0, */ int k = L - 1; k >= 0; k--) { max = max * 10 + n[k]; // cout << n[k];// << endl; } // cout << max << endl; // i++; a[i] = max - min; // cout << i << ": " << max << "-" << min << "= " << a[i] << endl; if (a[i - 1] == a[i]) { N = i + 1; break; } } int s; j = search(a, N, &s); cout << j << " "; cout << a[j] << " " << s << endl; } return 0; }
[["-", 8, 9, 0, 57, 64, 9, 0, 37, 0, 22], ["+", 8, 9, 0, 57, 64, 9, 0, 37, 0, 22]]
1
445
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string> using namespace std; string Hen(int N) { string S; char s; while (N != 0) { s = '0' - N % 10; S.insert(S.begin(), s); N /= 10; } return S; } int main() { string S, T[40]; int L, N, M, j, len, flag; while (1) { cin >> S >> L; if (S[0] == '0' && L == 0) break; len = S.length(); flag = 0; if (S.length() < L) { for (int i = 0; i < L - len; i++) { S.insert(S.begin(), '0'); } } T[0] = S; for (int i = 1; i < 21; i++) { sort(S.begin(), S.end()); N = atoi(S.c_str()); reverse(S.begin(), S.end()); M = atoi(S.c_str()); S = Hen(N - M); len = S.length(); if (S.length() < L) { for (int j = 0; j < L - len; j++) { S.insert(S.begin(), '0'); } } T[i] = S; } for (int i = 2; i < 21; i++) { for (int j = 1; j < i; j++) { if (T[i] == T[j]) { cout << j << " " << atoi(T[i].c_str()) << " " << i - j << endl; flag = 1; break; } } if (flag == 1) break; } } return 0; }
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string> using namespace std; string Hen(int N) { string S; char s; while (N != 0) { s = '0' - N % 10; S.insert(S.begin(), s); N /= 10; } return S; } int main() { string S, T[40]; int L, N, M, j, len, flag; while (1) { cin >> S >> L; if (S[0] == '0' && L == 0) break; len = S.length(); flag = 0; if (S.length() < L) { for (int i = 0; i < L - len; i++) { S.insert(S.begin(), '0'); } } T[0] = S; for (int i = 1; i < 21; i++) { sort(S.begin(), S.end()); N = atoi(S.c_str()); reverse(S.begin(), S.end()); M = atoi(S.c_str()); S = Hen(N - M); len = S.length(); if (S.length() < L) { for (int j = 0; j < L - len; j++) { S.insert(S.begin(), '0'); } } T[i] = S; } for (int i = 1; i < 21; i++) { for (int j = 0; j < i; j++) { if (T[i] == T[j]) { cout << j << " " << atoi(T[i].c_str()) << " " << i - j << endl; flag = 1; break; } } if (flag == 1) break; } } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
414
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < (n); i++) #define ALL(s) (s).begin(), (s).end() #define rep(i, l, n) for (int i = l; i < n; i++) #define rer(i, l, n) for (int i = l; i <= n; i++) #define rrep(i, n, l) for (int i = n - 1; i >= l; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define pb(a) push_back(a); #define fi first #define se second using namespace std; typedef long long ll; typedef vector<int> vi; // typedef vector<lint> vli; typedef vector<vi> vvi; typedef pair<int, int> pii; vi v; int n, l; string changes(int k) { stringstream ss; string s; ss << k; ss >> s; return s; } int changei(string s) { stringstream ss; int k; ss << s; ss >> k; return k; } int main() { while (cin >> n >> l) { if (n == 0 && l == 0) break; int d[100] = {}; bool f = 0; d[0] = n; rep(i, 1, 21) { string s = changes(n); while (s.size() < l) { s += "0"; } string t1 = s; sort(ALL(t1)); string t2 = t1; reverse(ALL(t2)); int mi, ma; ma = changei(t2); mi = changei(t1); n = ma - mi; d[i] = n; o(n); rep(j, 0, i) { if (d[i] == d[j]) { cout << j << " " << d[i] << " " << i - j << endl; f = 1; break; } } if (f) break; } } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < (n); i++) #define ALL(s) (s).begin(), (s).end() #define rep(i, l, n) for (int i = l; i < n; i++) #define rer(i, l, n) for (int i = l; i <= n; i++) #define rrep(i, n, l) for (int i = n - 1; i >= l; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define pb(a) push_back(a); #define fi first #define se second using namespace std; typedef long long ll; typedef vector<int> vi; // typedef vector<lint> vli; typedef vector<vi> vvi; typedef pair<int, int> pii; vi v; int n, l; string changes(int k) { stringstream ss; string s; ss << k; ss >> s; return s; } int changei(string s) { stringstream ss; int k; ss << s; ss >> k; return k; } int main() { while (cin >> n >> l) { if (n == 0 && l == 0) break; int d[100] = {}; bool f = 0; d[0] = n; rep(i, 1, 21) { string s = changes(n); while (s.size() < l) { s += "0"; } string t1 = s; sort(ALL(t1)); string t2 = t1; reverse(ALL(t2)); int mi, ma; ma = changei(t2); mi = changei(t1); n = ma - mi; d[i] = n; // o(n); rep(j, 0, i) { if (d[i] == d[j]) { cout << j << " " << d[i] << " " << i - j << endl; f = 1; break; } } if (f) break; } } }
[["-", 8, 9, 0, 9, 0, 1, 0, 2, 63, 22], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 0, 52, 8, 9, 0, 9, 0, 1, 0, 35]]
1
394
#include <algorithm> #include <iostream> #include <map> #include <stdio.h> #include <stdlib.h> #include <vector> using namespace std; int main() { vector<int> S; map<int, int> temp; int a, L, min, max; while (1) { bool FLAG = false; cin >> a >> L; if (!a && !L) break; if (a == 0) { cout << a << " " << a << " " << a + 1 << endl; continue; } temp.insert(make_pair(a, 0)); for (int i = 0; i < 100; ++i) { for (int l = 0; l < L; ++l) { S.push_back(a % 10); a = a / 10; } sort(S.begin(), S.end()); int digit = 1; min = 0; max = 0; for (int j = 0; j < L; ++j) { max += S[j] * digit; min += S[L - j - 1] * digit; digit *= 10; } // cout << max << " " << min << endl; auto itr = temp.find(max - min); if (itr != temp.end()) { cout << itr->second << " " << max - min << " " << i - itr->second + 1 << endl; FLAG = true; break; } if (FLAG) break; a = max - min; temp.insert(make_pair(a, i + 1)); // cout << "a" << a << endl; S.clear(); } S.clear(); } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <stdio.h> #include <stdlib.h> #include <vector> using namespace std; int main() { vector<int> S; map<int, int> temp; int a, L, min, max; while (1) { bool FLAG = false; cin >> a >> L; if (!a && !L) break; if (a == 0) { cout << a << " " << a << " " << a + 1 << endl; continue; } temp.insert(make_pair(a, 0)); for (int i = 0; i < 100; ++i) { for (int l = 0; l < L; ++l) { S.push_back(a % 10); a = a / 10; } sort(S.begin(), S.end()); int digit = 1; min = 0; max = 0; for (int j = 0; j < L; ++j) { max += S[j] * digit; min += S[L - j - 1] * digit; digit *= 10; } // cout << max << " " << min << endl; auto itr = temp.find(max - min); if (itr != temp.end()) { cout << itr->second << " " << max - min << " " << i - itr->second + 1 << endl; FLAG = true; break; } if (FLAG) break; a = max - min; temp.insert(make_pair(a, i + 1)); // cout << "a" << a << endl; S.clear(); } S.clear(); temp.clear(); } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
332
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; using std::vector; int digit(int n, int d) { int m = (int)(n / pow(10, d)) * pow(10, d); return (n - m) / pow(10, d - 1); } int max_deci(int n, int l) { int ret = 0; vector<int> d(l); for (int i = 0; i < l; i++) { d[i] = digit(n, i + 1); } sort(d.begin(), d.end(), std::greater<int>()); for (int i = 0; i < l; i++) { ret += d[i] * pow(10, l - i - 1); } return ret; } int min_deci(int n, int l) { int ret = 0; vector<int> d(l); for (int i = 0; i < l; i++) { d[i] = digit(n, i + 1); } sort(d.begin(), d.end()); for (int i = 0; i < l; i++) { ret += d[i] * pow(10, l - i - 1); } return ret; } int check_vec(vector<int> v, int n, int ind) { int ret = 0; for (vector<int>::iterator itr = v.begin(); itr != v.end() && ret < ind + 2; ++itr) { if (*itr == n) { return ret; } ret++; } return -1; } int main(void) { int N, L; cin >> N; cin >> L; while (N != 0 || L != 0) { bool find = false; int a = N; int ind; vector<int> dp(21, 0); dp[0] = a; for (int i = 0; i < 20 && !find; i++) { a = max_deci(a, L) - min_deci(a, L); ind = check_vec(dp, a, i); if (!(ind < 0)) { find = true; cout << ind << " " << a << " " << i - ind + 1 << endl; } else { dp[i + 1] = a; } } cin >> N; cin >> L; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; using std::vector; int digit(int n, int d) { int m = (int)(n / pow(10, d)) * pow(10, d); return (n - m) / pow(10, d - 1); } int max_deci(int n, int l) { int ret = 0; vector<int> d(l); for (int i = 0; i < l; i++) { d[i] = digit(n, i + 1); } sort(d.begin(), d.end(), std::greater<int>()); for (int i = 0; i < l; i++) { ret += d[i] * pow(10, l - i - 1); } return ret; } int min_deci(int n, int l) { int ret = 0; vector<int> d(l); for (int i = 0; i < l; i++) { d[i] = digit(n, i + 1); } sort(d.begin(), d.end()); for (int i = 0; i < l; i++) { ret += d[i] * pow(10, l - i - 1); } return ret; } int check_vec(vector<int> v, int n, int ind) { int ret = 0; for (vector<int>::iterator itr = v.begin(); itr != v.end() && ret < ind + 1; ++itr) { if (*itr == n) { return ret; } ret++; } return -1; } int main(void) { int N, L; cin >> N; cin >> L; while (N != 0 || L != 0) { bool find = false; int a = N; int ind; vector<int> dp(21, -1); dp[0] = a; for (int i = 0; i < 20 && !find; i++) { a = max_deci(a, L) - min_deci(a, L); ind = check_vec(dp, a, i); if (!(ind < 0)) { find = true; cout << ind << " " << a << " " << i - ind + 1 << endl; } else { dp[i + 1] = a; } } cin >> N; cin >> L; } return 0; }
[["-", 0, 7, 15, 16, 12, 16, 12, 16, 12, 13], ["+", 0, 7, 15, 16, 12, 16, 12, 16, 12, 13], ["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13]]
1
546
#include <bits/stdc++.h> using namespace std; const long long INF = 1e7 + 1; const long long MOD = 1000000007; const long double PI = 3.1415926; #define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++) #define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() typedef long long int ll; typedef vector<ll> vi; typedef vector<pair<ll, ll>> vp; typedef vector<string> vs; typedef vector<char> vc; typedef list<ll> lst; ll n, m, k, ans = 0, sum = 0, cnt = 0; string s; char c; /*--------------------template--------------------*/ vi vans; ll f(ll n, ll m) { if ((ll)log10(n) + 1 != m) { n = n * pow(10, m - (ll)log10(n) - 1); } vi v; for (int i = m - 1; i >= 0; i--) { ll b = pow(10, i); ll a = n / b; v.push_back(a); n -= a * b; } sort(ALL(v)); ll mx = 0; REP(i, m) { mx += v[i] * pow(10, i); } sort(RALL(v)); ll mi = 0; REP(i, m) { mi += v[i] * pow(10, i); } /*cout << mx << ' ' << mi << endl; cout << mx - mi << endl; cout << endl;*/ vans.push_back(mx - mi); return mx - mi; } int main() { while (1) { cin >> n >> m; vans.push_back(n); if (n == 0 && m == 0) { return 0; } else { if (n == 0) { cout << 0 << ' ' << 0 << ' ' << 1 << endl; vans.clear(); } else { cnt = 0; ll p = -1; while (1) { ll a = f(n, m); if (a == 0) { cout << vans.size() - 1 << ' ' << 0 << ' ' << 1 << endl; break; } REP(i, vans.size() - 1) { if (a == vans[i]) { p = i; break; } } if (p != -1) { cout << p << ' ' << a << ' ' << vans.size() - 1 - p << endl; vans.clear(); break; } n = a; } } } } }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e7 + 1; const long long MOD = 1000000007; const long double PI = 3.1415926; #define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++) #define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() typedef long long int ll; typedef vector<ll> vi; typedef vector<pair<ll, ll>> vp; typedef vector<string> vs; typedef vector<char> vc; typedef list<ll> lst; ll n, m, k, ans = 0, sum = 0, cnt = 0; string s; char c; /*--------------------template--------------------*/ vi vans; ll f(ll n, ll m) { if ((ll)log10(n) + 1 != m) { n = n * pow(10, m - (ll)log10(n) - 1); } vi v; for (int i = m - 1; i >= 0; i--) { ll b = pow(10, i); ll a = n / b; v.push_back(a); n -= a * b; } sort(ALL(v)); ll mx = 0; REP(i, m) { mx += v[i] * pow(10, i); } sort(RALL(v)); ll mi = 0; REP(i, m) { mi += v[i] * pow(10, i); } /*cout << mx << ' ' << mi << endl; cout << mx - mi << endl; cout << endl;*/ vans.push_back(mx - mi); return mx - mi; } int main() { while (1) { cin >> n >> m; vans.push_back(n); if (n == 0 && m == 0) { return 0; } else { if (n == 0) { cout << 0 << ' ' << 0 << ' ' << 1 << endl; vans.clear(); } else { cnt = 0; ll p = -1; while (1) { ll a = f(n, m); if (a == 0) { cout << vans.size() - 1 << ' ' << 0 << ' ' << 1 << endl; vans.clear(); break; } REP(i, vans.size() - 1) { if (a == vans[i]) { p = i; break; } } if (p != -1) { cout << p << ' ' << a << ' ' << vans.size() - 1 - p << endl; vans.clear(); break; } n = a; } } } } }
[["+", 64, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 64, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 64, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
551
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, char *argv[]) { for (;;) { int a, l; cin >> a >> l; if (a == 0 && l == 0) break; vector<int> ss(6); vector<int> done(2000000, -1); for (int k = 0;; k++) { if (done[a] >= 0) { cout << done[a] << " " << a << " " << k - done[a] << endl; break; } done[a] = k; for (int i = 0; i < l; i++) { ss[i] = a % 10; a /= 10; } sort(ss.begin(), ss.end()); int x = 0, y = 0; for (int i = 0; i < l; i++) { x = x * 10 + ss[i]; y = y * 10 + ss[l - 1 - i]; } a = y - x; } } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, char *argv[]) { for (;;) { int a, l; cin >> a >> l; if (a == 0 && l == 0) break; vector<int> ss(l); vector<int> done(2000000, -1); for (int k = 0;; k++) { if (done[a] >= 0) { cout << done[a] << " " << a << " " << k - done[a] << endl; break; } done[a] = k; for (int i = 0; i < l; i++) { ss[i] = a % 10; a /= 10; } sort(ss.begin(), ss.end()); int x = 0, y = 0; for (int i = 0; i < l; i++) { x = x * 10 + ss[i]; y = y * 10 + ss[l - 1 - i]; } a = y - x; } } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["+", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78]]
1
239
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; vector<int> a; int L; string intString(int n) { stringstream ss; int keta = 1, tmp; tmp = n; while (tmp != 0) { tmp /= 10; keta++; } for (int i = 0; i <= (L - keta); i++) ss << 0; ss << n; return ss.str(); } int func(string str) { sort(str.begin(), str.end()); int low = stoi(str); sort(str.rbegin(), str.rend()); int high = stoi(str); return high - low; } void solve() { //桁変更 string str = intString(a[0]); for (int i = 1;; i++) { a[i] = func(str); for (int j = 0; j < i; j++) { if (a[i] == a[j]) { cout << j << ' ' << a[i] << ' ' << (i - j) << endl; a.clear(); return; } } str = intString(a[i]); } } int main() { int a0; while (true) { cin >> a0 >> L; if (a0 + L == 0) break; a.push_back(a0); solve(); } return 0; }
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; vector<int> a; int L; string intString(int n) { stringstream ss; int keta = 1, tmp; tmp = n; while (tmp != 0) { tmp /= 10; keta++; } for (int i = 0; i <= (L - keta); i++) ss << 0; ss << n; return ss.str(); } int func(string str) { sort(str.begin(), str.end()); int low = stoi(str); sort(str.rbegin(), str.rend()); int high = stoi(str); return high - low; } void solve() { //桁変更 string str = intString(a[0]); for (int i = 1;; i++) { a.push_back(func(str)); for (int j = 0; j < i; j++) { if (a[i] == a[j]) { cout << j << ' ' << a[i] << ' ' << (i - j) << endl; a.clear(); return; } } str = intString(a[i]); } } int main() { int a0; while (true) { cin >> a0 >> L; if (a0 + L == 0) break; a.push_back(a0); solve(); } return 0; }
[["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 70], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 73], ["-", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
1
311
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> //-- #include <bitset> #include <cstdio> #include <cstring> #include <deque> #include <list> #include <map> #include <set> #include <sstream> #define X first #define Y second #define pb push_back using namespace std; priority_queue<int> hoge; int aa = 3, num, maxN, minN, dig, powT[] = {1, 10, 100, 1000, 10000, 100000, 10000000}; map<int, int> mark; int main() { int i, j, t; while (1) { scanf("%d %d", &num, &dig); mark.clear(); t = 0; mark[num] = t; if (num == 0 && dig == 0) break; while (1) { t++; int temp = num; maxN = 0; minN = 0; for (i = 0; i < dig; i++) { hoge.push(temp % 10); temp /= 10; } for (i = 0; i < dig; i++) { maxN += powT[dig - i - 1] * hoge.top(); minN += powT[i] * hoge.top(); hoge.pop(); } num = maxN - minN; // cout<<maxN<<"-"<<minN<<"="<<num<<endl; if (mark[num] > 0) { printf("%d %d %d\n", mark[num], num, t - mark[num]); break; } else { mark[num] = t; } } } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> //-- #include <bitset> #include <cstdio> #include <cstring> #include <deque> #include <list> #include <map> #include <set> #include <sstream> #define X first #define Y second #define pb push_back using namespace std; priority_queue<int> hoge; int aa = 3, num, maxN, minN, dig, powT[] = {1, 10, 100, 1000, 10000, 100000, 10000000}; map<int, int> mark; int main() { int i, j, t; while (1) { scanf("%d %d", &num, &dig); mark.clear(); t = 1; mark[num] = t; if (num == 0 && dig == 0) break; while (1) { t++; int temp = num; maxN = 0; minN = 0; for (i = 0; i < dig; i++) { hoge.push(temp % 10); temp /= 10; } for (i = 0; i < dig; i++) { maxN += powT[dig - i - 1] * hoge.top(); minN += powT[i] * hoge.top(); hoge.pop(); } num = maxN - minN; // cout<<maxN<<"-"<<minN<<"="<<num<<endl; if (mark[num] > 0) { printf("%d %d %d\n", mark[num] - 1, num, t - mark[num]); break; } else { mark[num] = t; } } } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
314
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = a; i < b; i++) #define all(v) (v).begin(), (v).end() #define MP make_pair using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> P; typedef vector<P> vp; void pp(vi v) { rep(i, v.size()) { cout << v[i] << ' '; } cout << endl; } void pp(vector<string> v) { rep(i, v.size()) { cout << v[i] << endl; } } void pp(int (*v)[20], int size = 20) { rep(i, size) { rep(j, size) { cout << v[i][j] << ' '; } cout << endl; } } void pp(char (*v)[80], int size = 30) { rep(i, size) { rep(j, size) { cout << v[i][j]; } cout << endl; } } int check(vector<string> vs, string s) { rep(i, vs.size()) { if (vs[i] == s) return i; } return -1; } int main() { int a, L; char buf[100]; // freopen( "b.out", "w", stdout); while (cin >> a >> L) { if (!a && !L) break; sprintf(buf, "%d", a); string s = buf; while (s.size() < L) { // 先頭を0埋め s = '0' + s; } vector<string> v; v.push_back(s); // sortして while (true) { sort(all(s)); // 最小 string t = s; reverse(all(t)); // 最大 sprintf(buf, "%d", t.c_str()) - atoi(s.c_str()); s = buf; // 最大-最小 while (s.size() < L) { // 先頭を0埋め s = '0' + s; } int i = check(v, s); if (i != -1) { // 過去に出てきてないかチェック // cout << "***"; cout << i << ' ' << atoi(s.c_str()) << ' ' << v.size() - i << endl; break; } v.push_back(s); } } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = a; i < b; i++) #define all(v) (v).begin(), (v).end() #define MP make_pair using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> P; typedef vector<P> vp; void pp(vi v) { rep(i, v.size()) { cout << v[i] << ' '; } cout << endl; } void pp(vector<string> v) { rep(i, v.size()) { cout << v[i] << endl; } } void pp(int (*v)[20], int size = 20) { rep(i, size) { rep(j, size) { cout << v[i][j] << ' '; } cout << endl; } } void pp(char (*v)[80], int size = 30) { rep(i, size) { rep(j, size) { cout << v[i][j]; } cout << endl; } } int check(vector<string> vs, string s) { rep(i, vs.size()) { if (vs[i] == s) return i; } return -1; } int main() { int a, L; char buf[100]; // freopen( "b.out", "w", stdout); while (cin >> a >> L) { if (!a && !L) break; sprintf(buf, "%d", a); string s = buf; while (s.size() < L) { // 先頭を0埋め s = '0' + s; } vector<string> v; v.push_back(s); // sortして while (true) { sort(all(s)); // 最小 string t = s; reverse(all(t)); // 最大 sprintf(buf, "%d", atoi(t.c_str()) - atoi(s.c_str())); s = buf; // 最大-最小 while (s.size() < L) { // 先頭を0埋め s = '0' + s; } int i = check(v, s); if (i != -1) { // 過去に出てきてないかチェック // cout << "***"; cout << i << ' ' << atoi(s.c_str()) << ' ' << v.size() - i << endl; break; } v.push_back(s); } } return 0; }
[["+", 0, 2, 3, 4, 0, 16, 31, 2, 63, 22], ["+", 3, 4, 0, 16, 31, 2, 3, 4, 0, 24], ["+", 12, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
1
523
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int h, w, c; int p[8][8]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void print(int time) { printf("time %d\n", time); REP(y, h) { REP(x, w) printf("%d ", p[y][x]); cout << endl; } } int dfs(int time) { if (time != 5) { bool equal[8][8] = {}; queue<int> qx; queue<int> qy; qx.push(0); qy.push(0); while (!qx.empty()) { int x = qx.front(); qx.pop(); int y = qy.front(); qy.pop(); equal[y][x] = true; REP(r, 4) { int nx = x + dx[r]; int ny = y + dy[r]; if (nx >= 0 && ny >= 0 && nx < w && ny < h && !equal[ny][nx] && p[y][x] == p[ny][nx]) { qx.push(nx); qy.push(ny); equal[ny][nx] = true; } } } int res = 0; int tmp[8][8]; REP(y, h) REP(x, w) tmp[y][x] = p[y][x]; for (int color = 1; color <= 6; color++) { REP(y, h) REP(x, w) if (equal[y][x]) { p[y][x] = color; } res = max(res, dfs(time + 1)); REP(y, h) REP(x, w) p[y][x] = tmp[y][x]; } return res; } else if (time == 5) { if (p[0][0] != c) return 0; bool equal[8][8] = {}; queue<int> qx; queue<int> qy; qx.push(0); qy.push(0); while (!qx.empty()) { int x = qx.front(); qx.pop(); int y = qy.front(); qy.pop(); equal[y][x] = true; REP(r, 4) { int nx = x + dx[r]; int ny = y + dy[r]; if (nx >= 0 && ny >= 0 && nx < w && ny < h && !equal[ny][nx] && p[y][x] == p[ny][nx]) { qx.push(nx); qy.push(ny); equal[ny][nx] = true; } } } int res = 0; REP(y, h) REP(x, w) res += equal[y][x]; return res; } } int main() { while (cin >> h >> w >> c) { REP(y, h) REP(x, w) cin >> p[y][x]; cout << dfs(0) << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int h, w, c; int p[8][8]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void print(int time) { printf("time %d\n", time); REP(y, h) { REP(x, w) printf("%d ", p[y][x]); cout << endl; } } int dfs(int time) { if (time != 5) { bool equal[8][8] = {}; queue<int> qx; queue<int> qy; qx.push(0); qy.push(0); while (!qx.empty()) { int x = qx.front(); qx.pop(); int y = qy.front(); qy.pop(); equal[y][x] = true; REP(r, 4) { int nx = x + dx[r]; int ny = y + dy[r]; if (nx >= 0 && ny >= 0 && nx < w && ny < h && !equal[ny][nx] && p[y][x] == p[ny][nx]) { qx.push(nx); qy.push(ny); equal[ny][nx] = true; } } } int res = 0; int tmp[8][8]; REP(y, h) REP(x, w) tmp[y][x] = p[y][x]; for (int color = 1; color <= 6; color++) { REP(y, h) REP(x, w) if (equal[y][x]) { p[y][x] = color; } res = max(res, dfs(time + 1)); REP(y, h) REP(x, w) p[y][x] = tmp[y][x]; } return res; } else if (time == 5) { if (p[0][0] != c) return 0; bool equal[8][8] = {}; queue<int> qx; queue<int> qy; qx.push(0); qy.push(0); while (!qx.empty()) { int x = qx.front(); qx.pop(); int y = qy.front(); qy.pop(); equal[y][x] = true; REP(r, 4) { int nx = x + dx[r]; int ny = y + dy[r]; if (nx >= 0 && ny >= 0 && nx < w && ny < h && !equal[ny][nx] && p[y][x] == p[ny][nx]) { qx.push(nx); qy.push(ny); equal[ny][nx] = true; } } } int res = 0; REP(y, h) REP(x, w) res += equal[y][x]; return res; } } int main() { while (cin >> h >> w >> c && h) { REP(y, h) REP(x, w) cin >> p[y][x]; cout << dfs(0) << endl; } return 0; }
[["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 98], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22]]
1
860
#include <cstring> #include <iostream> #include <queue> using namespace std; typedef struct a { int x; int y; } coordin; int h, w, c; int res = 0; int maze[8][8]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void changeColor(int tmp[8][8], int color) { int originColor = tmp[0][0]; queue<coordin> q; coordin cc; cc.x = 0; cc.y = 0; q.push(cc); tmp[0][0] = color; while (!q.empty()) { cc = q.front(); q.pop(); for (int i = 0; i < 2; ++i) { int tmp_x = cc.x + dx[i]; int tmp_y = cc.y + dy[i]; if (0 <= tmp_x && tmp_x < w && 0 <= tmp_y && tmp_y < h) { if (tmp[tmp_x][tmp_y] == originColor) { coordin tmp_cc; tmp_cc.x = tmp_x; tmp_cc.y = tmp_y; if (color != originColor) q.push(tmp_cc); tmp[tmp_x][tmp_y] = color; } } } } return; } int cnt(int m[8][8], int x, int y) { int count = 1; int oc = m[x][y]; m[x][y] = 0; for (int i = 0; i < 2; i++) { int tmp_x = x + dx[i]; int tmp_y = y + dy[i]; if (0 <= tmp_x && tmp_x < w && 0 <= tmp_y && tmp_y < h) { if (m[tmp_x][tmp_y] == oc) count += cnt(m, tmp_x, tmp_y); } } return count; } int dfs(int m[8][8], int depth) { if (depth == 4) { if (m[0][0] != c) changeColor(m, c); return cnt(m, 0, 0); } for (int i = 1; i <= 6; ++i) { int tmp_m[8][8]; memcpy(tmp_m, m, sizeof(tmp_m)); if (tmp_m[0][0] != i) changeColor(tmp_m, i); res = max(res, dfs(tmp_m, depth + 1)); } return res; } int main() { cin >> h >> w >> c; while (h != 0) { for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cin >> maze[j][i]; cout << dfs(maze, 0) << endl; res = 0; cin >> h >> w >> c; } return 0; }
#include <cstring> #include <iostream> #include <queue> using namespace std; typedef struct a { int x; int y; } coordin; int h, w, c; int res = 0; int maze[8][8]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void changeColor(int tmp[8][8], int color) { int originColor = tmp[0][0]; queue<coordin> q; coordin cc; cc.x = 0; cc.y = 0; q.push(cc); tmp[0][0] = color; while (!q.empty()) { cc = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { int tmp_x = cc.x + dx[i]; int tmp_y = cc.y + dy[i]; if (0 <= tmp_x && tmp_x < w && 0 <= tmp_y && tmp_y < h) { if (tmp[tmp_x][tmp_y] == originColor) { coordin tmp_cc; tmp_cc.x = tmp_x; tmp_cc.y = tmp_y; if (color != originColor) q.push(tmp_cc); tmp[tmp_x][tmp_y] = color; } } } } return; } int cnt(int m[8][8], int x, int y) { int count = 1; int oc = m[x][y]; m[x][y] = 0; for (int i = 0; i < 4; i++) { int tmp_x = x + dx[i]; int tmp_y = y + dy[i]; if (0 <= tmp_x && tmp_x < w && 0 <= tmp_y && tmp_y < h) { if (m[tmp_x][tmp_y] == oc) count += cnt(m, tmp_x, tmp_y); } } return count; } int dfs(int m[8][8], int depth) { if (depth == 4) { if (m[0][0] != c) changeColor(m, c); return cnt(m, 0, 0); } for (int i = 1; i <= 6; ++i) { int tmp_m[8][8]; memcpy(tmp_m, m, sizeof(tmp_m)); if (tmp_m[0][0] != i) changeColor(tmp_m, i); res = max(res, dfs(tmp_m, depth + 1)); } return res; } int main() { cin >> h >> w >> c; while (h != 0) { for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cin >> maze[j][i]; cout << dfs(maze, 0) << endl; res = 0; cin >> h >> w >> c; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
1
624
#include <iostream> #include <stack> #include <utility> #include <vector> using namespace std; vector<vector<bool>> getunion(const vector<vector<int>> &base) { vector<vector<bool>> ret; stack<pair<int, int>> nxt; int xmax = base[0].size(); int ymax = base.size(); int tar = base[0][0]; int curx, cury; ret.resize(ymax); for (int i = 0; i < ymax; i++) ret[i].resize(xmax); nxt.push(pair<int, int>(0, 0)); while (!nxt.empty()) { curx = nxt.top().first; cury = nxt.top().second; nxt.pop(); if (base[cury][curx] == tar && !ret[cury][curx]) { ret[cury][curx] = true; if (curx > 0) nxt.push(pair<int, int>(curx - 1, cury)); if (curx < xmax - 1) nxt.push(pair<int, int>(curx + 1, cury)); if (cury > 0) nxt.push(pair<int, int>(curx, cury - 1)); if (cury < ymax - 1) nxt.push(pair<int, int>(curx, cury + 1)); } } return ret; } vector<vector<int>> ccolor(const vector<vector<int>> &base, int color) { vector<vector<int>> ret; vector<vector<bool>> uni = getunion(base); ; int ymax = base.size(); int xmax = base[0].size(); ret = base; for (int i = 0; i < ymax; i++) { for (int j = 0; j < xmax; j++) { if (uni[i][j]) ret[i][j] = color; } } return ret; } int score(const vector<vector<int>> &base) { vector<vector<bool>> get = getunion(base); int ymax = get.size(); int xmax = get[0].size(); int ret = 0; for (int i = 0; i < ymax; i++) { for (int j = 0; j < xmax; j++) { if (get[i][j]) ret++; } } return ret; } int cal(const vector<vector<int>> &base, int target, int count) { if (count == 1) { return score(ccolor(base, target)); } else { vector<vector<int>> cald; int max = 0, get; // 最大色数指定 for (int i = 1; i < 7; i++) { if (i != base[0][0]) { cald = ccolor(base, i); get = cal(cald, target, count - 1); if (get > max) max = get; } } return max; } } int main() { int xsize, ysize, target; vector<vector<int>> base; while (true) { cin >> xsize >> ysize >> target; if (xsize == 0) break; base.resize(ysize); for (int i = 0; i < ysize; i++) { base[i].resize(xsize); for (int j = 0; j < xsize; j++) { cin >> base[i][j]; } } cout << cal(base, target, 5) << endl; } return 0; }
#include <iostream> #include <stack> #include <utility> #include <vector> using namespace std; vector<vector<bool>> getunion(const vector<vector<int>> &base) { vector<vector<bool>> ret; stack<pair<int, int>> nxt; int xmax = base[0].size(); int ymax = base.size(); int tar = base[0][0]; int curx, cury; ret.resize(ymax); for (int i = 0; i < ymax; i++) ret[i].resize(xmax); nxt.push(pair<int, int>(0, 0)); while (!nxt.empty()) { curx = nxt.top().first; cury = nxt.top().second; nxt.pop(); if (base[cury][curx] == tar && !ret[cury][curx]) { ret[cury][curx] = true; if (curx > 0) nxt.push(pair<int, int>(curx - 1, cury)); if (curx < xmax - 1) nxt.push(pair<int, int>(curx + 1, cury)); if (cury > 0) nxt.push(pair<int, int>(curx, cury - 1)); if (cury < ymax - 1) nxt.push(pair<int, int>(curx, cury + 1)); } } return ret; } vector<vector<int>> ccolor(const vector<vector<int>> &base, int color) { vector<vector<int>> ret; vector<vector<bool>> uni = getunion(base); int ymax = base.size(); int xmax = base[0].size(); ret = base; for (int i = 0; i < ymax; i++) { for (int j = 0; j < xmax; j++) { if (uni[i][j]) ret[i][j] = color; } } return ret; } int score(const vector<vector<int>> &base) { vector<vector<bool>> get = getunion(base); int ymax = get.size(); int xmax = get[0].size(); int ret = 0; for (int i = 0; i < ymax; i++) { for (int j = 0; j < xmax; j++) { if (get[i][j]) ret++; } } return ret; } int cal(const vector<vector<int>> &base, int target, int count) { if (count == 1) { return score(ccolor(base, target)); } else { vector<vector<int>> cald; int max = 0, get; // 最大色数指定 for (int i = 1; i < 7; i++) { if (i != base[0][0]) { cald = ccolor(base, i); get = cal(cald, target, count - 1); if (get > max) max = get; } } return max; } } int main() { int xsize, ysize, target; vector<vector<int>> base; while (true) { cin >> ysize >> xsize >> target; if (target == 0) break; base.resize(ysize); for (int i = 0; i < ysize; i++) { base[i].resize(xsize); for (int j = 0; j < xsize; j++) { cin >> base[i][j]; } } //繰り返し回数指定 cout << cal(base, target, 5) << endl; } return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["-", 0, 1, 0, 16, 31, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 152], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 152], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
1
794
$dx = [0,1,0,-1] $dy = [1,0,-1,0] def bfs(f, sy, sx, h, w, col) q = [[sy,sx]] visited = [[sy,sx]] while q.size > 0 sy, sx = q.shift for i in 0..3 cy = sy + $dy[i] cx = sx + $dx[i] if cy.between?(0,h-1) && cx.between?(0,w-1) && f[cy][cx]== f[sy][sx] && !visited.include?([cy,cx]) q.push([cy,cx]) visited.push([cy,cx]) end end f[sy][sx] = col end return f end def count(f, sy, sx, h, w) q = [[sy,sx]] visited = [[sy,sx]] c = 1 while q.size > 0 sy, sx = q.shift for i in 0..3 cy = sy + $dy[i] cx = sx + $dx[i] if cy.between?(0,h-1) && cx.between?(0,w-1) && f[cy][cx]== f[sy][sx] && !visited.include?([cy,cx]) q.push([cy,cx]) visited.push([cy,cx]) c += 1 end end end return c end loop do h, w, c = gets.split.map(&:to_i) break if h == 0 && w == 0 && c == 0 p = h.times.map{gets.split.map(&:to_i)} o = (1..6).to_a.repeated_permutation(5).to_a o.select!{|od| od[0] == p[0][1] || od[0] == p[1][0]} if h > 1 && w > 1 && count(p,0,0,h,w) == 1 o.select!{|od| od[0] != od[1] && od[1] != od[2] && od[2] != od[3] && od[3] != od[4]} o.select!{|od| od[4] == c} p o.size max = 0 o.each_with_index do |od, i| pp = Marshal.load(Marshal.dump(p)) od.each do |col| pp = bfs(pp,0,0,h,w,col) end cnt = count(pp,0,0,h,w) max = cnt if cnt > max end puts max end
$dx = [0,1,0,-1] $dy = [1,0,-1,0] def bfs(f, sy, sx, h, w, col) q = [[sy,sx]] visited = [[sy,sx]] while q.size > 0 sy, sx = q.shift for i in 0..3 cy = sy + $dy[i] cx = sx + $dx[i] if cy.between?(0,h-1) && cx.between?(0,w-1) && f[cy][cx]== f[sy][sx] && !visited.include?([cy,cx]) q.push([cy,cx]) visited.push([cy,cx]) end end f[sy][sx] = col end return f end def count(f, sy, sx, h, w) q = [[sy,sx]] visited = [[sy,sx]] c = 1 while q.size > 0 sy, sx = q.shift for i in 0..3 cy = sy + $dy[i] cx = sx + $dx[i] if cy.between?(0,h-1) && cx.between?(0,w-1) && f[cy][cx]== f[sy][sx] && !visited.include?([cy,cx]) q.push([cy,cx]) visited.push([cy,cx]) c += 1 end end end return c end loop do h, w, c = gets.split.map(&:to_i) break if h == 0 && w == 0 && c == 0 p = h.times.map{gets.split.map(&:to_i)} o = (1..6).to_a.repeated_permutation(5).to_a o.select!{|od| od[0] == p[0][1] || od[0] == p[1][0]} if h > 1 && w > 1 && count(p,0,0,h,w) == 1 o.select!{|od| od[0] != od[1] && od[1] != od[2] && od[2] != od[3] && od[3] != od[4]} o.select!{|od| od[4] == c} max = 0 o.each_with_index do |od, i| pp = Marshal.load(Marshal.dump(p)) od.each do |col| pp = bfs(pp,0,0,h,w,col) end cnt = count(pp,0,0,h,w) max = cnt if cnt > max end puts max end
[["-", 0, 652, 196, 737, 8, 736, 0, 652, 735, 22], ["-", 8, 736, 0, 652, 3, 4, 0, 652, 486, 22], ["-", 8, 736, 0, 652, 3, 4, 0, 652, 17, 131], ["-", 8, 736, 0, 652, 3, 4, 0, 652, 735, 22]]
4
584
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define C_MAX(a, b) ((a) > (b) ? (a) : (b)) #define SHOW_VECTOR(v) \ { \ std::cerr << #v << "\t:"; \ for (const auto &xxx : v) { \ std::cerr << xxx << " "; \ } \ std::cerr << "\n"; \ } #define SHOW_MAP(v) \ { \ std::cerr << #v << endl; \ for (const auto &xxx : v) { \ std::cerr << xxx.first << " " << xxx.second << "\n"; \ } \ } constexpr int MAX_N = 1 << 24; int b[25]; bool intersects(vector<int> &x, vector<int> &y, vector<int> &r, int i, int j) { int dx = (x[i] - x[j]) * (x[i] - x[j]); int dy = (y[i] - y[j]) * (y[i] - y[j]); int dist1 = dx + dy; int dist2 = (r[i] + r[j]) * (r[i] + r[j]); return dist1 < dist2; } int main() { while (true) { int N; cin >> N; if (N == 0) break; vector<int> x(N), y(N), r(N), c(N); for (int i = 0; i < N; i++) cin >> x[i] >> y[i] >> r[i] >> c[i]; //上からi枚目 for (int i = 0; i < N; i++) { b[i] = 0; // i枚目よりあとに降ってきたものを見る for (int j = 0; j < i; j++) { if (intersects(x, y, r, i, j)) b[i] |= (1 << j); } } vector<int> dp(1 << N, -1); int ans = 0; int to = (1 << N); dp[0] = 0; //すべての円の使い方の状態を見る for (int i = 0; i < to; i++) { //このような円の状態はありえない if (dp[i] == -1) continue; //消せる円盤のペア(j, k)を探す for (int j = 0; j < N - 1; j++) { if ((i & (1 << j)) != 0 || (i & b[j]) != b[j]) continue; // jより下にあるkをさがす for (int k = j + 1; k < N; k++) { if (c[j] != c[k] || (i & (1 << k)) != 0 || (i & b[k]) != b[k]) continue; int next = i | (1 << j) | (1 << k); dp[next] = max(dp[next], dp[i] + 2); } } } for (int i = 0; i < (1 << N); i++) ans = max(ans, (int)dp[i]); cout << ans * 2 << endl; } }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define C_MAX(a, b) ((a) > (b) ? (a) : (b)) #define SHOW_VECTOR(v) \ { \ std::cerr << #v << "\t:"; \ for (const auto &xxx : v) { \ std::cerr << xxx << " "; \ } \ std::cerr << "\n"; \ } #define SHOW_MAP(v) \ { \ std::cerr << #v << endl; \ for (const auto &xxx : v) { \ std::cerr << xxx.first << " " << xxx.second << "\n"; \ } \ } constexpr int MAX_N = 1 << 24; int b[25]; bool intersects(vector<int> &x, vector<int> &y, vector<int> &r, int i, int j) { int dx = (x[i] - x[j]) * (x[i] - x[j]); int dy = (y[i] - y[j]) * (y[i] - y[j]); int dist1 = dx + dy; int dist2 = (r[i] + r[j]) * (r[i] + r[j]); return dist1 < dist2; } int main() { while (true) { int N; cin >> N; if (N == 0) break; vector<int> x(N), y(N), r(N), c(N); for (int i = 0; i < N; i++) cin >> x[i] >> y[i] >> r[i] >> c[i]; //上からi枚目 for (int i = 0; i < N; i++) { b[i] = 0; // i枚目よりあとに降ってきたものを見る for (int j = 0; j < i; j++) { if (intersects(x, y, r, i, j)) b[i] |= (1 << j); } } vector<int> dp(1 << N, -1); int ans = 0; int to = (1 << N); dp[0] = 0; //すべての円の使い方の状態を見る for (int i = 0; i < to; i++) { //このような円の状態はありえない if (dp[i] == -1) continue; //消せる円盤のペア(j, k)を探す for (int j = 0; j < N - 1; j++) { if ((i & (1 << j)) != 0 || (i & b[j]) != b[j]) continue; // jより下にあるkをさがす for (int k = j + 1; k < N; k++) { if (c[j] != c[k] || (i & (1 << k)) != 0 || (i & b[k]) != b[k]) continue; int next = i | (1 << j) | (1 << k); dp[next] = max(dp[next], dp[i] + 2); } } } for (int i = 0; i < (1 << N); i++) ans = max(ans, (int)dp[i]); cout << ans << endl; } }
[["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 48], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
622
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; // < "D:\D_Download\Visual Studio // 2015\Projects\programing_contest_c++\Debug\a.txt" /* ??????????????¬ */ #include <complex> typedef long double ld; typedef complex<ld> Point; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() const ld pi = acos(-1.0); const ld dtop = pi / 180.; const ld ptod = 1. / dtop; namespace std { bool operator<(const Point &lhs, const Point &rhs) { if (lhs.real() < rhs.real() - eps) return true; if (lhs.real() > rhs.real() + eps) return false; return lhs.imag() < rhs.imag(); } } // namespace std // ????????\??? Point input_point() { ld x, y; cin >> x >> y; return Point(x, y); } // ???????????????????????? bool eq(const ld a, const ld b) { return (abs(a - b) < eps); } // ?????? ld dot(const Point &a, const Point &b) { return real(conj(a) * b); } // ?????? ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); } // ??´???????????? class Line { public: Point a, b; Line() : a(Point(0, 0)), b(Point(0, 0)) {} Line(Point a, Point b) : a(a), b(b) {} Point operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else { assert(false); return Point(); } } }; // ???????????? class Circle { public: Point p; ld r; Circle() : p(Point(0, 0)), r(0) {} Circle(Point p, ld r) : p(p), r(r) {} }; // CCW int ccw(const Point &a, const Point &b, const Point &c) { const Point nb(b - a); const Point nc(c - a); if (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶ if (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶ if (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶ if (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶ return 0; // a,c,b???????????´???????????¶ } /* ???????????? */ // ??´?????¨??´?????????????????? bool isis_ll(const Line &l, const Line &m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); } // ??´?????¨????????????????????? bool isis_ls(const Line &l, const Line &s) { return isis_ll(l, s) && (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps); } // ????????¨????????????????????? bool isis_ss(const Line &s, const Line &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } // ????????´???????????? bool isis_lp(const Line &l, const Point &p) { return (abs(cross(l.b - p, l.a - p)) < eps); } // ????????????????????? bool isis_sp(const Line &s, const Point &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } // ??????????¶? Point proj(const Line &l, const Point &p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } // ??´?????¨??´???????????? Point is_ll(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; assert(cross(sv, tv) != 0); return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } // ??´?????¨??´???????????? vector<Point> is_ll2(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; if (cross(sv, tv) != 0) return vector<Point>(1, is_ll(s, t)); else { vector<Point> ans; for (int k = 0; k < 2; ++k) { if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end()) ans.push_back(t[k]); if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end()) ans.push_back(s[k]); } return ans; } } // ????????¨??????????????? //???????????£????????¨???????????¨assert(false) Point is_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) { for (int k = 0; k < 2; ++k) { for (int l = 0; l < 2; ++l) { if (s[k] == t[l]) return s[k]; } } return is_ll(s, t); } else { //??????isis_ss????????? assert(false); return Point(0, 0); } } // ??´?????¨???????????¢ ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); } // ??´?????¨??´???????????¢ ld dist_ll(const Line &l, const Line &m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } // ??´?????¨??????????????¢ ld dist_ls(const Line &l, const Line &s) { return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b)); } // ????????¨???????????¢ ld dist_sp(const Line &s, const Point &p) { Point r = proj(s, p); return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p)); } // ????????¨??????????????¢ ld dist_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) return 0; return min( {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)}); } //??´?????¨??´????????????????????????????????? Line bisection(const Line &s, const Line &t) { const Point laglanju(is_ll(s, t)); const Point avec = !(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju; const Point bvec = !(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju; return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) / (abs(avec) + abs(bvec))); } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? Point inner_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0]) return vertics[0]; Line bi1( bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2]))); Line bi2( bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0]))); if (bi1[0] == bi2[0]) return bi1[0]; else { return is_ll(bi1, bi2); } } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? vector<Point> ex_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (abs(vertics[0] - vertics[1]) < eps || abs(vertics[1] - vertics[2]) < eps || (abs(vertics[2] - vertics[0]) < eps)) return vector<Point>(); vector<Point> ecs; for (int i = 0; i < 3; ++i) { Line bi1( bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3]))); Line bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i]))); ecs.push_back(is_ll(bi1, bi2)); } return ecs; } // a,b:?????? // c:????????§?????? //???????????´?????????????????¢?????????????±?????????? vector<Point> same_dis(const vector<Line> &ls) { vector<Point> vertics; vertics.push_back(is_ll(ls[0], ls[2])); vertics.push_back(is_ll(ls[1], ls[2])); if (abs(vertics[0] - vertics[1]) < eps) return vector<Point>{vertics[0]}; Line bis(bisection(ls[0], ls[1])); vector<Point> ecs; Line abi(bisection(Line(vertics[0], vertics[1]), ls[0])); ecs.push_back(is_ll(bis, abi)); Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0])); ecs.push_back(is_ll(bis, bbi)); return ecs; } /* ??? */ // ?????¨???????????? vector<Point> is_cc(const Circle &c1, const Circle &c2) { vector<Point> res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); Point diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * Point(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs)); return res; } //??????????????????????????? // 0 => out // 1 => on // 2 => in int is_in_circle(const Circle &cir, const Point &p) { ld dis = abs(cir.p - p); if (dis > cir.r + eps) return 0; else if (dis < cir.r - eps) return 2; else return 1; } //???lc??????rc?????????????????? // 0 => out // 1 => on // 2 => in int circle_in_circle(const Circle &lc, const Circle &rc) { ld dis = abs(lc.p - rc.p); if (dis < rc.r - lc.r - eps) return 2; else if (dis > rc.r - lc.r + eps) return 0; else return 1; } // ?????¨??´???????????? vector<Point> is_lc(const Circle &c, const Line &l) { vector<Point> res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } // ?????¨??????????????¢ vector<Point> is_sc(const Circle &c, const Line &l) { vector<Point> v = is_lc(c, l), res; for (Point p : v) if (isis_sp(l, p)) res.push_back(p); return res; } // ?????¨????????\??? vector<Line> tangent_cp(const Circle &c, const Point &p) { vector<Line> ret; Point v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } Point v1 = v * Point(l / d, c.r / d); Point v2 = v * Point(l / d, -c.r / d); ret.push_back(Line(p, p + v1)); if (l < eps) return ret; ret.push_back(Line(p, p + v2)); return ret; } // ?????¨????????\??? vector<Line> tangent_cc(const Circle &c1, const Circle &c2) { vector<Line> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<Line> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { Point v = c2.p - c1.p; v /= abs(v); Point q1 = c1.p + v * Point(0, 1) * c1.r; Point q2 = c1.p + v * Point(0, -1) * c1.r; ret.push_back(Line(q1, q1 + v)); ret.push_back(Line(q2, q2 + v)); } return ret; } //??????????????????????????¢??? ld two_circle_area(const Circle &l, const Circle &r) { ld dis = abs(l.p - r.p); if (dis > l.r + r.r) return 0; else if (dis + r.r < l.r) { return r.r * r.r * pi; } else if (dis + l.r < r.r) { return l.r * l.r * pi; } else if (dis < eps) { const ld ar = min(l.r, r.r); return ar * ar * pi; } else { ld ans = (l.r) * (l.r) * acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) + (r.r) * (r.r) * acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) - sqrt(4 * dis * dis * l.r * l.r - (dis * dis + l.r * l.r - r.r * r.r) * (dis * dis + l.r * l.r - r.r * r.r)) / 2; return ans; } } /* ????§???¢ */ typedef vector<Point> Polygon; // ??¢??? ld area(const Polygon &p) { ld res = 0; int n = p.size(); REP(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } // ????§???¢????????¢?????? bool is_counter_clockwise(const Polygon &poly) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n]; angle += arg((c - b) / (b - a)); } return angle > eps; } // ?????????????????? // 0 => out // 1 => on // 2 => in int is_in_polygon(const Polygon &poly, const Point &p) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n]; if (isis_sp(Line(a, b), p)) return 1; angle += arg((b - p) / (a - p)); } return eq(angle, 0) ? 0 : 2; } //??????????????????2????????? enum { OUT, ON, IN }; int convex_contains(const Polygon &P, const Point &p) { const int n = P.size(); Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point int a = 0, b = n; while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b] int c = (a + b) / 2; if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c; else a = c; } else { if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c; else b = c; } } b %= n; if (cross(P[a] - p, P[b] - p) < 0) return 0; if (cross(P[a] - p, P[b] - p) > 0) return 2; return 1; } // ?????? // ???????????????????????¨????????????????????§??¨??? Polygon convex_hull(vector<Point> ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); Polygon ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } // ???????????? vector<Polygon> convex_cut(const Polygon &ps, const Line &l) { int n = ps.size(); Polygon Q; Polygon R; REP(i, n) { Point A = ps[i], B = ps[(i + 1) % n]; Line m = Line(A, B); if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) != 1) R.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) { Q.push_back(is_ll(l, m)); R.push_back(is_ll(l, m)); } } const vector<Polygon> polys{Q, R}; return polys; } /* ??¢??¬??????????????? */ void add_point(vector<Point> &ps, const Point p) { for (Point q : ps) if (abs(q - p) < eps) return; ps.push_back(p); } typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &g, const int from, const int to, const Weight weight) { g[from].push_back(Edge{from, to, weight}); } Graph segment_arrangement(const vector<Line> &s, const vector<Point> &p) { int n = p.size(), m = s.size(); Graph g(n); REP(i, m) { vector<pair<ld, int>> vec; REP(j, n) if (isis_sp(s[i], p[j])) vec.emplace_back(abs(s[i].a - p[j]), j); sort(ALL(vec)); REP(j, vec.size() - 1) { int from = vec[j].second, to = vec[j + 1].second; add_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to]))); } } return g; } Graph sennbunn_arrangement(const vector<Line> &s) { vector<Point> crss; for (int i = 0; i < static_cast<int>(s.size()); ++i) { for (int j = i + 1; j < static_cast<int>(s.size()); ++j) { if (isis_ss(s[i], s[j])) { crss.push_back(is_ll(s[i], s[j])); } } } for (int i = 0; i < static_cast<int>(s.size()); ++i) { crss.push_back(s[i][0]); crss.push_back(s[i][1]); } return segment_arrangement(s, crss); } // Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) { // int n = p.size(), m = c.size(); // Graph g(n); // REP(i, m) { // vector<pair<ld, int>> vec; // REP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps) // vec.emplace_back(arg(c[i].p - p[j]), j); // sort(ALL(vec)); // REP(j, vec.size() - 1) { // int from = vec[j].second, to = vec[j + 1].second; // ld angle = vec[j + 1].first - vec[j].first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // if (vec.size() >= 2) { // int from = vec.back().second, to = vec.front().first; // ld angle = vec.front().first - vec.back().first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // } // return g; //} /* ????????°?????? */ // ?????????????????¢?????¢??¬??????????????????????????????????????°??????????????? // ?????´?????????????¨?????????§????????´????????????????¨?????????§??????????????? // ?????° polygon ??????vector<int> // ??§??¨?????????????§???¢???????????§????????? vector<int> ??§??¨????????? // ????§???¢???i???????????????????????????????????????p????????????????????§????????? vector<vector<int>> polygon; vector<int> seg2p[1024][1024]; // Graph dual_graph(const vector<Line> &s, const vector<Point> &p) { // int N = p.size(); // polygon.clear(); // REP(i, 1024) REP(j, 1024) seg2p[i][j].clear(); // vector<vector<tuple<ld, int, bool>>> tup(N); // REP(i, s.size()) { // int a = -1, b = -1; // REP(j, N) if (abs(s[i].a - p[j]) < eps) a = j; // REP(j, N) if (abs(s[i].b - p[j]) < eps) b = j; // assert(a >= 0 && b >= 0); // tup[a].emplace_back(arg(s[i].b - s[i].a), b, false); // tup[b].emplace_back(arg(s[i].a - s[i].b), a, false); // } // REP(i, N) sort(ALL(tup[i])); // REP(i, N) { // REP(j, tup[i].size()) { // ld angle; int pos = j, from = i, to; bool flag; // tie(angle, to, flag) = tup[i][j]; // if (flag) continue; // vector<int> ps; // while (!flag) { // ps.push_back(from); // get<2>(tup[from][pos]) = true; // seg2p[from][to].push_back(polygon.size()); // seg2p[to][from].push_back(polygon.size()); // angle += pi + eps; // if (angle > pi) angle -= 2 * pi; // auto it = lower_bound(ALL(tup[to]), make_tuple(angle, //0, false)); if (it == tup[to].end()) it = tup[to].begin(); from = to; //tie(angle, to, flag) = *it; pos = it - tup[from].begin(); // } // polygon.push_back(ps); // } // } // Graph g(polygon.size()); // REP(i, N) REP(j, i) { // if (seg2p[i][j].size() == 2) { // int from = seg2p[i][j][0], to = seg2p[i][j][1]; // g[from].push_back(Edge{ from, to }); // g[to].push_back(Edge{ to, from }); // } // } // return g; //} vector<Circle> cs; vector<int> colors; char memo[1 << 24]; int getans(const vector<vector<int>> &edges, vector<int> &nums, bitset<24> &used) { if (memo[used.to_ulong()] != -1) return memo[used.to_ulong()]; vector<vector<int>> colorids(4); for (int i = 0; i < used.size(); ++i) { if (!used[i]) { if (!nums[i]) { colorids[colors[i]].push_back(i); } } } int ans = 0; for (int i = 0; i < 4; ++i) { for (int a = 0; a < colorids[i].size(); ++a) { int aid = colorids[i][a]; for (int b = a + 1; b < colorids[i].size(); ++b) { const int bid = colorids[i][b]; used[aid] = true; used[bid] = true; for (auto e : edges[aid]) { nums[e]--; } for (auto e : edges[bid]) { nums[e]--; } ans = max(ans, 2 + getans(edges, nums, used)); used[aid] = false; used[bid] = false; for (auto e : edges[aid]) { nums[e]++; } for (auto e : edges[bid]) { nums[e]++; } } } } return memo[used.to_ulong()] = ans; } int main() { while (1) { for (int i = 0; i < 1 << 24; ++i) { memo[i] = -1; } int N; cin >> N; if (!N) break; cs.clear(); colors.clear(); for (int i = 0; i < N; ++i) { int x, y, r, c; cin >> x >> y >> r >> c; c--; cs.emplace_back(Point(x, y), r); colors.push_back(c); } vector<vector<int>> edges(N); vector<int> nums(N); for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { if (two_circle_area(cs[i], cs[j]) > eps) { edges[i].push_back(j); nums[j]++; } } } bitset<24> bs; int ans = getans(edges, nums, bs); cout << ans << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; // < "D:\D_Download\Visual Studio // 2015\Projects\programing_contest_c++\Debug\a.txt" /* ??????????????¬ */ #include <complex> typedef long double ld; typedef complex<ld> Point; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() const ld pi = acos(-1.0); const ld dtop = pi / 180.; const ld ptod = 1. / dtop; namespace std { bool operator<(const Point &lhs, const Point &rhs) { if (lhs.real() < rhs.real() - eps) return true; if (lhs.real() > rhs.real() + eps) return false; return lhs.imag() < rhs.imag(); } } // namespace std // ????????\??? Point input_point() { ld x, y; cin >> x >> y; return Point(x, y); } // ???????????????????????? bool eq(const ld a, const ld b) { return (abs(a - b) < eps); } // ?????? ld dot(const Point &a, const Point &b) { return real(conj(a) * b); } // ?????? ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); } // ??´???????????? class Line { public: Point a, b; Line() : a(Point(0, 0)), b(Point(0, 0)) {} Line(Point a, Point b) : a(a), b(b) {} Point operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else { assert(false); return Point(); } } }; // ???????????? class Circle { public: Point p; ld r; Circle() : p(Point(0, 0)), r(0) {} Circle(Point p, ld r) : p(p), r(r) {} }; // CCW int ccw(const Point &a, const Point &b, const Point &c) { const Point nb(b - a); const Point nc(c - a); if (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶ if (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶ if (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶ if (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶ return 0; // a,c,b???????????´???????????¶ } /* ???????????? */ // ??´?????¨??´?????????????????? bool isis_ll(const Line &l, const Line &m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); } // ??´?????¨????????????????????? bool isis_ls(const Line &l, const Line &s) { return isis_ll(l, s) && (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps); } // ????????¨????????????????????? bool isis_ss(const Line &s, const Line &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } // ????????´???????????? bool isis_lp(const Line &l, const Point &p) { return (abs(cross(l.b - p, l.a - p)) < eps); } // ????????????????????? bool isis_sp(const Line &s, const Point &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } // ??????????¶? Point proj(const Line &l, const Point &p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } // ??´?????¨??´???????????? Point is_ll(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; assert(cross(sv, tv) != 0); return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } // ??´?????¨??´???????????? vector<Point> is_ll2(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; if (cross(sv, tv) != 0) return vector<Point>(1, is_ll(s, t)); else { vector<Point> ans; for (int k = 0; k < 2; ++k) { if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end()) ans.push_back(t[k]); if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end()) ans.push_back(s[k]); } return ans; } } // ????????¨??????????????? //???????????£????????¨???????????¨assert(false) Point is_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) { for (int k = 0; k < 2; ++k) { for (int l = 0; l < 2; ++l) { if (s[k] == t[l]) return s[k]; } } return is_ll(s, t); } else { //??????isis_ss????????? assert(false); return Point(0, 0); } } // ??´?????¨???????????¢ ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); } // ??´?????¨??´???????????¢ ld dist_ll(const Line &l, const Line &m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } // ??´?????¨??????????????¢ ld dist_ls(const Line &l, const Line &s) { return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b)); } // ????????¨???????????¢ ld dist_sp(const Line &s, const Point &p) { Point r = proj(s, p); return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p)); } // ????????¨??????????????¢ ld dist_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) return 0; return min( {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)}); } //??´?????¨??´????????????????????????????????? Line bisection(const Line &s, const Line &t) { const Point laglanju(is_ll(s, t)); const Point avec = !(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju; const Point bvec = !(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju; return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) / (abs(avec) + abs(bvec))); } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? Point inner_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0]) return vertics[0]; Line bi1( bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2]))); Line bi2( bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0]))); if (bi1[0] == bi2[0]) return bi1[0]; else { return is_ll(bi1, bi2); } } //???????????´????????????????????? //???????????´??????????????§???????????¨????¢?????????¨????????? vector<Point> ex_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (abs(vertics[0] - vertics[1]) < eps || abs(vertics[1] - vertics[2]) < eps || (abs(vertics[2] - vertics[0]) < eps)) return vector<Point>(); vector<Point> ecs; for (int i = 0; i < 3; ++i) { Line bi1( bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3]))); Line bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i]))); ecs.push_back(is_ll(bi1, bi2)); } return ecs; } // a,b:?????? // c:????????§?????? //???????????´?????????????????¢?????????????±?????????? vector<Point> same_dis(const vector<Line> &ls) { vector<Point> vertics; vertics.push_back(is_ll(ls[0], ls[2])); vertics.push_back(is_ll(ls[1], ls[2])); if (abs(vertics[0] - vertics[1]) < eps) return vector<Point>{vertics[0]}; Line bis(bisection(ls[0], ls[1])); vector<Point> ecs; Line abi(bisection(Line(vertics[0], vertics[1]), ls[0])); ecs.push_back(is_ll(bis, abi)); Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0])); ecs.push_back(is_ll(bis, bbi)); return ecs; } /* ??? */ // ?????¨???????????? vector<Point> is_cc(const Circle &c1, const Circle &c2) { vector<Point> res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); Point diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * Point(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs)); return res; } //??????????????????????????? // 0 => out // 1 => on // 2 => in int is_in_circle(const Circle &cir, const Point &p) { ld dis = abs(cir.p - p); if (dis > cir.r + eps) return 0; else if (dis < cir.r - eps) return 2; else return 1; } //???lc??????rc?????????????????? // 0 => out // 1 => on // 2 => in int circle_in_circle(const Circle &lc, const Circle &rc) { ld dis = abs(lc.p - rc.p); if (dis < rc.r - lc.r - eps) return 2; else if (dis > rc.r - lc.r + eps) return 0; else return 1; } // ?????¨??´???????????? vector<Point> is_lc(const Circle &c, const Line &l) { vector<Point> res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } // ?????¨??????????????¢ vector<Point> is_sc(const Circle &c, const Line &l) { vector<Point> v = is_lc(c, l), res; for (Point p : v) if (isis_sp(l, p)) res.push_back(p); return res; } // ?????¨????????\??? vector<Line> tangent_cp(const Circle &c, const Point &p) { vector<Line> ret; Point v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } Point v1 = v * Point(l / d, c.r / d); Point v2 = v * Point(l / d, -c.r / d); ret.push_back(Line(p, p + v1)); if (l < eps) return ret; ret.push_back(Line(p, p + v2)); return ret; } // ?????¨????????\??? vector<Line> tangent_cc(const Circle &c1, const Circle &c2) { vector<Line> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<Line> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { Point v = c2.p - c1.p; v /= abs(v); Point q1 = c1.p + v * Point(0, 1) * c1.r; Point q2 = c1.p + v * Point(0, -1) * c1.r; ret.push_back(Line(q1, q1 + v)); ret.push_back(Line(q2, q2 + v)); } return ret; } //??????????????????????????¢??? ld two_circle_area(const Circle &l, const Circle &r) { ld dis = abs(l.p - r.p); if (dis > l.r + r.r) return 0; else if (dis + r.r < l.r) { return r.r * r.r * pi; } else if (dis + l.r < r.r) { return l.r * l.r * pi; } else if (dis < eps) { const ld ar = min(l.r, r.r); return ar * ar * pi; } else { ld ans = (l.r) * (l.r) * acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) + (r.r) * (r.r) * acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) - sqrt(4 * dis * dis * l.r * l.r - (dis * dis + l.r * l.r - r.r * r.r) * (dis * dis + l.r * l.r - r.r * r.r)) / 2; return ans; } } /* ????§???¢ */ typedef vector<Point> Polygon; // ??¢??? ld area(const Polygon &p) { ld res = 0; int n = p.size(); REP(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } // ????§???¢????????¢?????? bool is_counter_clockwise(const Polygon &poly) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n]; angle += arg((c - b) / (b - a)); } return angle > eps; } // ?????????????????? // 0 => out // 1 => on // 2 => in int is_in_polygon(const Polygon &poly, const Point &p) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n]; if (isis_sp(Line(a, b), p)) return 1; angle += arg((b - p) / (a - p)); } return eq(angle, 0) ? 0 : 2; } //??????????????????2????????? enum { OUT, ON, IN }; int convex_contains(const Polygon &P, const Point &p) { const int n = P.size(); Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point int a = 0, b = n; while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b] int c = (a + b) / 2; if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c; else a = c; } else { if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c; else b = c; } } b %= n; if (cross(P[a] - p, P[b] - p) < 0) return 0; if (cross(P[a] - p, P[b] - p) > 0) return 2; return 1; } // ?????? // ???????????????????????¨????????????????????§??¨??? Polygon convex_hull(vector<Point> ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); Polygon ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } // ???????????? vector<Polygon> convex_cut(const Polygon &ps, const Line &l) { int n = ps.size(); Polygon Q; Polygon R; REP(i, n) { Point A = ps[i], B = ps[(i + 1) % n]; Line m = Line(A, B); if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) != 1) R.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) { Q.push_back(is_ll(l, m)); R.push_back(is_ll(l, m)); } } const vector<Polygon> polys{Q, R}; return polys; } /* ??¢??¬??????????????? */ void add_point(vector<Point> &ps, const Point p) { for (Point q : ps) if (abs(q - p) < eps) return; ps.push_back(p); } typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &g, const int from, const int to, const Weight weight) { g[from].push_back(Edge{from, to, weight}); } Graph segment_arrangement(const vector<Line> &s, const vector<Point> &p) { int n = p.size(), m = s.size(); Graph g(n); REP(i, m) { vector<pair<ld, int>> vec; REP(j, n) if (isis_sp(s[i], p[j])) vec.emplace_back(abs(s[i].a - p[j]), j); sort(ALL(vec)); REP(j, vec.size() - 1) { int from = vec[j].second, to = vec[j + 1].second; add_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to]))); } } return g; } Graph sennbunn_arrangement(const vector<Line> &s) { vector<Point> crss; for (int i = 0; i < static_cast<int>(s.size()); ++i) { for (int j = i + 1; j < static_cast<int>(s.size()); ++j) { if (isis_ss(s[i], s[j])) { crss.push_back(is_ll(s[i], s[j])); } } } for (int i = 0; i < static_cast<int>(s.size()); ++i) { crss.push_back(s[i][0]); crss.push_back(s[i][1]); } return segment_arrangement(s, crss); } // Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) { // int n = p.size(), m = c.size(); // Graph g(n); // REP(i, m) { // vector<pair<ld, int>> vec; // REP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps) // vec.emplace_back(arg(c[i].p - p[j]), j); // sort(ALL(vec)); // REP(j, vec.size() - 1) { // int from = vec[j].second, to = vec[j + 1].second; // ld angle = vec[j + 1].first - vec[j].first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // if (vec.size() >= 2) { // int from = vec.back().second, to = vec.front().first; // ld angle = vec.front().first - vec.back().first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // } // return g; //} /* ????????°?????? */ // ?????????????????¢?????¢??¬??????????????????????????????????????°??????????????? // ?????´?????????????¨?????????§????????´????????????????¨?????????§??????????????? // ?????° polygon ??????vector<int> // ??§??¨?????????????§???¢???????????§????????? vector<int> ??§??¨????????? // ????§???¢???i???????????????????????????????????????p????????????????????§????????? vector<vector<int>> polygon; vector<int> seg2p[1024][1024]; // Graph dual_graph(const vector<Line> &s, const vector<Point> &p) { // int N = p.size(); // polygon.clear(); // REP(i, 1024) REP(j, 1024) seg2p[i][j].clear(); // vector<vector<tuple<ld, int, bool>>> tup(N); // REP(i, s.size()) { // int a = -1, b = -1; // REP(j, N) if (abs(s[i].a - p[j]) < eps) a = j; // REP(j, N) if (abs(s[i].b - p[j]) < eps) b = j; // assert(a >= 0 && b >= 0); // tup[a].emplace_back(arg(s[i].b - s[i].a), b, false); // tup[b].emplace_back(arg(s[i].a - s[i].b), a, false); // } // REP(i, N) sort(ALL(tup[i])); // REP(i, N) { // REP(j, tup[i].size()) { // ld angle; int pos = j, from = i, to; bool flag; // tie(angle, to, flag) = tup[i][j]; // if (flag) continue; // vector<int> ps; // while (!flag) { // ps.push_back(from); // get<2>(tup[from][pos]) = true; // seg2p[from][to].push_back(polygon.size()); // seg2p[to][from].push_back(polygon.size()); // angle += pi + eps; // if (angle > pi) angle -= 2 * pi; // auto it = lower_bound(ALL(tup[to]), make_tuple(angle, //0, false)); if (it == tup[to].end()) it = tup[to].begin(); from = to; //tie(angle, to, flag) = *it; pos = it - tup[from].begin(); // } // polygon.push_back(ps); // } // } // Graph g(polygon.size()); // REP(i, N) REP(j, i) { // if (seg2p[i][j].size() == 2) { // int from = seg2p[i][j][0], to = seg2p[i][j][1]; // g[from].push_back(Edge{ from, to }); // g[to].push_back(Edge{ to, from }); // } // } // return g; //} vector<Circle> cs; vector<int> colors; char memo[1 << 24]; int getans(const vector<vector<int>> &edges, vector<int> &nums, bitset<24> &used) { if (memo[used.to_ulong()] != -1) return memo[used.to_ulong()]; vector<vector<int>> colorids(4); for (int i = 0; i < nums.size(); ++i) { if (!used[i]) { if (!nums[i]) { colorids[colors[i]].push_back(i); } } } int ans = 0; for (int i = 0; i < 4; ++i) { for (int a = 0; a < colorids[i].size(); ++a) { int aid = colorids[i][a]; for (int b = a + 1; b < colorids[i].size(); ++b) { const int bid = colorids[i][b]; used[aid] = true; used[bid] = true; for (auto e : edges[aid]) { nums[e]--; } for (auto e : edges[bid]) { nums[e]--; } ans = max(ans, 2 + getans(edges, nums, used)); used[aid] = false; used[bid] = false; for (auto e : edges[aid]) { nums[e]++; } for (auto e : edges[bid]) { nums[e]++; } } } } return memo[used.to_ulong()] = ans; } int main() { while (1) { for (int i = 0; i < 1 << 24; ++i) { memo[i] = -1; } int N; cin >> N; if (!N) break; cs.clear(); colors.clear(); for (int i = 0; i < N; ++i) { int x, y, r, c; cin >> x >> y >> r >> c; c--; cs.emplace_back(Point(x, y), r); colors.push_back(c); } vector<vector<int>> edges(N); vector<int> nums(N); for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { if (two_circle_area(cs[i], cs[j]) > eps) { edges[i].push_back(j); nums[j]++; } } } bitset<24> bs; int ans = getans(edges, nums, bs); cout << ans << endl; } return 0; }
[["-", 0, 7, 15, 16, 12, 2, 63, 118, 28, 22], ["+", 0, 7, 15, 16, 12, 2, 63, 118, 28, 22]]
1
5,618
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define fr first #define sc second #define mp make_pair typedef long long int64; typedef pair<int, int> iP; typedef pair<iP, int> iiP; const int INF = 2 << 28; const double EPS = 1e-10; struct disk { int x, y, r, color; }; int n; map<int, int> dp; vector<disk> disks; int rec(int used) { if (dp[used]) return dp[used]; vector<int> idx; for (int i = n - 1; i >= 0; i--) { if (!(used >> i & 1)) { bool judge = true; for (int j = i - 1; j >= 0; j--) { if (!(used >> j & 1)) { if (pow(abs(disks[i].x - disks[j].x), 2) + pow(abs(disks[i].y - disks[j].y), 2) < pow(disks[i].r + disks[j].r, 2)) judge = false; } } if (judge) idx.push_back(i); } } int ret = 0; if (idx.size() < 2) return 0; else { for (int i = 0; i < idx.size(); i++) { for (int j = i + 1; j < idx.size(); j++) { if (disks[idx[i]].color == disks[idx[j]].color) { ret = max(ret, rec(used | (1 << idx[i] | 1 << idx[j])) + 2); } } } return dp[used] = ret; } } int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) { int x, y, c, r; cin >> x >> y >> r >> c; disks.push_back((disk){x, y, r, c}); } cout << rec(0) << endl; disks.clear(); } }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define fr first #define sc second #define mp make_pair typedef long long int64; typedef pair<int, int> iP; typedef pair<iP, int> iiP; const int INF = 2 << 28; const double EPS = 1e-10; struct disk { int x, y, r, color; }; int n; map<int, int> dp; vector<disk> disks; int rec(int used) { if (dp[used]) return dp[used]; vector<int> idx; for (int i = n - 1; i >= 0; i--) { if (!(used >> i & 1)) { bool judge = true; for (int j = i - 1; j >= 0; j--) { if (!(used >> j & 1)) { if (pow(abs(disks[i].x - disks[j].x), 2) + pow(abs(disks[i].y - disks[j].y), 2) < pow(disks[i].r + disks[j].r, 2)) judge = false; } } if (judge) idx.push_back(i); } } int ret = 0; if (idx.size() < 2) return 0; else { for (int i = 0; i < idx.size(); i++) { for (int j = i + 1; j < idx.size(); j++) { if (disks[idx[i]].color == disks[idx[j]].color) { ret = max(ret, rec(used | (1 << idx[i] | 1 << idx[j])) + 2); } } } return dp[used] = ret; } } int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) { int x, y, c, r; cin >> x >> y >> r >> c; disks.push_back((disk){x, y, r, c}); } cout << rec(0) << endl; disks.clear(); dp.clear(); } }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
502
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef pair<int, int> P; typedef pair<ll, ll> PL; int n, ans = 0; int x[25], y[25], r[25], c[25]; bool memo[1 << 24]; bool overlap(int i, int j) { return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) < (r[i] + r[j]) * (r[i] + r[j]); } void dfs(VI f) { int x = 0; REP(i, n) x += f[i] * (1 << i); if (memo[x]) return; memo[x] = 1; int res = 0; REP(i, n) res += f[i]; ans = max(ans, res); VI top(n); REP(i, n) { if (f[i]) continue; bool g = 1; REP(j, i) { if (f[j]) continue; if (overlap(i, j)) g = 0; } top[i] = (!f[i] & g); } // REP(i,n) printf("%d ", f[i]); // cout << endl; REP(i, n) REP(j, i) { if (!top[i] || !top[j] || c[i] != c[j]) continue; f[i] = f[j] = 1; dfs(f); f[i] = f[j] = 0; } } int main() { ifstream ifs("in.txt"); while (ifs >> n && n) { VI f(n); ans = 0; REP(i, 1 << n) memo[i] = 0; REP(i, n) ifs >> x[i] >> y[i] >> r[i] >> c[i]; dfs(f); cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef pair<int, int> P; typedef pair<ll, ll> PL; int n, ans = 0; int x[25], y[25], r[25], c[25]; bool memo[1 << 24]; bool overlap(int i, int j) { return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) < (r[i] + r[j]) * (r[i] + r[j]); } void dfs(VI f) { int x = 0; REP(i, n) x += f[i] * (1 << i); if (memo[x]) return; memo[x] = 1; int res = 0; REP(i, n) res += f[i]; ans = max(ans, res); VI top(n); REP(i, n) { if (f[i]) continue; bool g = 1; REP(j, i) { if (f[j]) continue; if (overlap(i, j)) g = 0; } top[i] = (!f[i] & g); } // REP(i,n) printf("%d ", f[i]); // cout << endl; REP(i, n) REP(j, i) { if (!top[i] || !top[j] || c[i] != c[j]) continue; f[i] = f[j] = 1; dfs(f); f[i] = f[j] = 0; } } int main() { ifstream ifs("in.txt"); while (cin >> n && n) { VI f(n); ans = 0; REP(i, 1 << n) memo[i] = 0; REP(i, n) cin >> x[i] >> y[i] >> r[i] >> c[i]; dfs(f); cout << ans << endl; } return 0; }
[["-", 0, 52, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 31, 22], ["-", 0, 16, 31, 16, 31, 16, 31, 16, 31, 22], ["+", 0, 16, 31, 16, 31, 16, 31, 16, 31, 22]]
1
532
#include "bits/stdc++.h" using namespace std; #define mod 1000000007 #define all(c) begin(c), end(c) template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } using ll = long long; using vi = vector<int>; using vs = vector<string>; int dd[] = {0, 1, 0, -1, 0}; //→↓←↑ void solve() { int N; while (cin >> N, N) { vector<int> x(N); vector<int> y(N); vector<int> r(N); vector<int> c(N); for (int i = 0; i < N; i++) { cin >> x[i] >> y[i] >> r[i] >> c[i]; } vector<int> G[N]; // vector<int> P(N); /* for (int i = 0; i < N; i++) { P[i] = -1; }*/ for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int xx = x[i] - x[j]; int yy = y[i] - y[j]; int rr = r[i] + r[j]; if (xx * xx + yy * yy < rr * rr) { G[j].emplace_back(i); } } } vector<int> dp(1 << N, 0); int ans = 0; vector<bool> ok(N, false); for (int S = 0; S < 1 << N; S++) { if (S != 0 && dp[S] == 0) continue; for (int i = 0; i < N; i++) { if (S >> i & 1) continue; for (int j = i + 1; j < N; j++) { if (S >> j & 1) continue; if (c[i] != c[j]) continue; bool flag = true; for (int v : G[i]) { if ((S >> v & 1) == 0) { flag = false; break; } } if (!flag) continue; for (int v : G[j]) { if ((S >> v & 1) == 0) { flag = false; break; } } if (!flag) continue; dp[S | (1 << i) | (1 << j)] = dp[S] + 2; ans = max(ans, dp[S + 2]); // ok[i] = true; // ok[j] = true; } } // cout << dp[S] << endl; } cout << ans << endl; } } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include "bits/stdc++.h" using namespace std; #define mod 1000000007 #define all(c) begin(c), end(c) template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } using ll = long long; using vi = vector<int>; using vs = vector<string>; int dd[] = {0, 1, 0, -1, 0}; //→↓←↑ void solve() { int N; while (cin >> N, N) { vector<int> x(N); vector<int> y(N); vector<int> r(N); vector<int> c(N); for (int i = 0; i < N; i++) { cin >> x[i] >> y[i] >> r[i] >> c[i]; } vector<int> G[N]; // vector<int> P(N); /* for (int i = 0; i < N; i++) { P[i] = -1; }*/ for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int xx = x[i] - x[j]; int yy = y[i] - y[j]; int rr = r[i] + r[j]; if (xx * xx + yy * yy < rr * rr) { G[j].emplace_back(i); } } } vector<int> dp(1 << N, 0); int ans = 0; vector<bool> ok(N, false); for (int S = 0; S < 1 << N; S++) { if (S != 0 && dp[S] == 0) continue; for (int i = 0; i < N; i++) { if (S >> i & 1) continue; for (int j = i + 1; j < N; j++) { if (S >> j & 1) continue; if (c[i] != c[j]) continue; bool flag = true; for (int v : G[i]) { if ((S >> v & 1) == 0) { flag = false; break; } } if (!flag) continue; for (int v : G[j]) { if ((S >> v & 1) == 0) { flag = false; break; } } if (!flag) continue; dp[S | (1 << i) | (1 << j)] = dp[S] + 2; ans = max(ans, dp[S] + 2); // ok[i] = true; // ok[j] = true; } } // cout << dp[S] << endl; } cout << ans << endl; } } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
[["+", 3, 4, 0, 16, 31, 69, 341, 342, 0, 73], ["-", 12, 2, 3, 4, 0, 69, 341, 342, 0, 73]]
1
607
loop { a,l = gets.split.map{|s|s.to_i} if [a,l] == [0,0] then exit! end a = [a] loop { v = a.last.to_s.rjust(l,'0').chars.sort.join min = v.to_i max = v.reverse.to_i r = max - min i = a.index(r) puts i if i != nil puts [i, r, a.length-i].join(' ') break end a << r } }
loop { a,l = gets.split.map{|s|s.to_i} if [a,l] == [0,0] then break end a = [a] loop { v = a.last.to_s.rjust(l,'0').chars.sort.join min = v.to_i max = v.reverse.to_i r = max - min i = a.index(r) if i != nil puts [i, r, a.length-i].join(' ') break end a << r } }
[["-", 8, 734, 0, 121, 64, 749, 0, 652, 735, 22], ["+", 8, 734, 0, 121, 64, 749, 0, 94, 0, 94], ["-", 0, 652, 196, 196, 8, 734, 0, 652, 735, 22], ["-", 196, 196, 8, 734, 0, 652, 3, 4, 0, 22]]
4
120
def sort_MxMn(SeriesA): global max_a, min_a listA = list(str(SeriesA)) while len(listA) < b: listA.append("0") listA = [int(x) for x in listA] max_a = sorted(listA, reverse=True) min_a = sorted(listA) def translate(listEx): num = 0 for i in range(b): num += listEx[i]*(10 ** (b-i-1)) return num for k in range(10): a, b = map(int, input().split()) if a + b ==0: break c = [] for i in range(10): c.append(a) sort_MxMn(a) a = translate(max_a) - translate(min_a) if (a in c) == True: print("{} {} {}".format(c.index(a), a, i - c.index(a) + 1)) break
def sort_MxMn(SeriesA): global max_a, min_a listA = list(str(SeriesA)) while len(listA) < b: listA.append("0") listA = [int(x) for x in listA] max_a = sorted(listA, reverse=True) min_a = sorted(listA) def translate(listEx): num = 0 for i in range(b): num += listEx[i]*(10 ** (b-i-1)) return num for k in range(100000): a, b = map(int, input().split()) if a + b ==0: break c = [] for i in range(100000): c.append(a) sort_MxMn(a) a = translate(max_a) - translate(min_a) if (a in c) == True: print("{} {} {}".format(c.index(a), a, i - c.index(a) + 1)) break
[["-", 0, 656, 0, 7, 12, 652, 3, 4, 0, 612], ["+", 0, 656, 0, 7, 12, 652, 3, 4, 0, 612], ["-", 8, 196, 0, 7, 12, 652, 3, 4, 0, 612], ["+", 8, 196, 0, 7, 12, 652, 3, 4, 0, 612]]
5
204
import sys readline = sys.stdin.readline while True: A, L = (int(x) for x in readline().split()) if A == 0 and L == 0: break cnt = 0 past = {A: 0} while True: A = sorted(str(A).rjust(int(L), '0')) minA = int("".join(A)) maxA = int("".join(reversed(A))) A = maxA - minA try: pre = past[A] print("%d %d %d" % (cnt, A, cnt - pre + 1)) break except: cnt += 1 past[A] = cnt
import sys readline = sys.stdin.readline while True: A, L = (int(x) for x in readline().split()) if A == 0 and L == 0: break cnt = 0 past = {A: 0} while True: A = sorted(str(A).rjust(int(L), '0')) minA = int("".join(A)) maxA = int("".join(reversed(A))) A = maxA - minA try: pre = past[A] print("%d %d %d" % (pre, A, cnt - pre + 1)) break except: cnt += 1 past[A] = cnt
[["-", 0, 652, 3, 4, 0, 657, 12, 660, 0, 22], ["+", 0, 652, 3, 4, 0, 657, 12, 660, 0, 22]]
5
146
#include <stdio.h> int main() { int n; int i, j, k; int t, f; // int l, r, b; int field[100][100][100]; int left[6][6] = {{0, 4, 2, 5, 3, 0}, {3, 0, 6, 1, 0, 4}, {5, 1, 0, 0, 6, 2}, {2, 6, 0, 0, 1, 5}, {4, 0, 1, 6, 0, 3}, {0, 3, 5, 2, 4, 0}}; // int dice[6][2] = {{5, 4}, {6, 4}, {6, 5}, {6, 5}, {6, 4}, {5, 4}}; // u = 0, r = 1, d = 2, l = 3 int dice[6][6][2] = {{{-1, -1}, {0, 3}, {1, 3}, {3, 2}, {2, 1}, {-1, -1}}, {{0, 1}, {-1, -1}, {3, 0}, {1, 2}, {-1, -1}, {2, 3}}, {{0, 3}, {1, 0}, {-1, -1}, {-1, -1}, {3, 2}, {2, 1}}, {{0, 1}, {3, 0}, {-1, -1}, {-1, -1}, {1, 2}, {2, 3}}, {{0, 3}, {-1, -1}, {1, 0}, {3, 2}, {-1, -1}, {2, 1}}, {{-1, -1}, {0, 1}, {3, 0}, {1, 2}, {2, 3}, {-1, -1}}}; int result[6]; int tmp2; while (1) { scanf("%d", &n); if (n == 0) { break; } for (i = 0; i < 100; i++) { for (j = 0; j < 100; j++) { for (k = 0; k < 100; k++) { field[i][j][k] = 0; } } } for (i = 0; i < 6; i++) { result[i] = 0; } for (i = 0; i < n; i++) { int x, y, z; x = y = 50; z = 99; scanf("%d %d", &t, &f); // b = 7 - f; // l = left[t-1][f-1]; // r = 7 - l; while (1) { fail:; while (z > 0 && field[x][y][z - 1] == 0) { z--; // field[x][y][z+1] = 0; // field[x][y][z] = t; } for (j = 0; j < 2; j++) { if (z == 0) { goto fallend; } int tmp = dice[t - 1][f - 1][j]; if (tmp == 0) { if (field[x][y + 1][z - 1] == 0) { // field[x][y][z] = 0; // field[x][y+1][z-1] = f; y++; z--; tmp2 = f; f = 7 - t; t = tmp2; goto fail; } } else if (tmp == 1) { if (field[x + 1][y][z - 1] == 0) { // field[x][y][z] = 0; t = left[t - 1][f - 1]; // field[x+1][y][z-1] = t; x++; z--; goto fail; } } else if (tmp == 2) { if (field[x][y - 1][z - 1] == 0) { // field[x][y][z] = 0; // field[x][y+1][z-1] = 7 - f; y--; z--; tmp2 = t; t = 7 - f; f = tmp2; goto fail; } } else { if (field[x - 1][y][z - 1] == 0) { // field[x][y][z] = 0; t = 7 - left[t - 1][f - 1]; // field[x-1][y][z-1] = t; x--; z--; goto fail; } } } break; } fallend: field[x][y][z] = t; // printf("%d %d %d %d\n", x, y, z, t); } for (i = 0; i < 100; i++) { for (j = 0; j < 100; j++) { for (k = 0; k < 100; k++) { if (k == 99 || field[i][j][k + 1] == 0) { int tmpres = field[i][j][k]; result[tmpres - 1]++; } } } } for (i = 0; i < 6; i++) { printf("%d", result[i]); if (i != 5) { printf(" "); } } printf("\n"); } return 0; }
#include <stdio.h> int main() { int n; int i, j, k; int t, f; // int l, r, b; int field[100][100][100]; int left[6][6] = {{0, 4, 2, 5, 3, 0}, {3, 0, 6, 1, 0, 4}, {5, 1, 0, 0, 6, 2}, {2, 6, 0, 0, 1, 5}, {4, 0, 1, 6, 0, 3}, {0, 3, 5, 2, 4, 0}}; // int dice[6][2] = {{5, 4}, {6, 4}, {6, 5}, {6, 5}, {6, 4}, {5, 4}}; // u = 0, r = 1, d = 2, l = 3 int dice[6][6][2] = {{{-1, -1}, {0, 3}, {1, 0}, {3, 2}, {2, 1}, {-1, -1}}, {{0, 1}, {-1, -1}, {3, 0}, {1, 2}, {-1, -1}, {2, 3}}, {{0, 3}, {1, 0}, {-1, -1}, {-1, -1}, {3, 2}, {2, 1}}, {{0, 1}, {3, 0}, {-1, -1}, {-1, -1}, {1, 2}, {2, 3}}, {{0, 3}, {-1, -1}, {1, 0}, {3, 2}, {-1, -1}, {2, 1}}, {{-1, -1}, {0, 1}, {3, 0}, {1, 2}, {2, 3}, {-1, -1}}}; int result[6]; int tmp2; while (1) { scanf("%d", &n); if (n == 0) { break; } for (i = 0; i < 100; i++) { for (j = 0; j < 100; j++) { for (k = 0; k < 100; k++) { field[i][j][k] = 0; } } } for (i = 0; i < 6; i++) { result[i] = 0; } for (i = 0; i < n; i++) { int x, y, z; x = y = 50; z = 99; scanf("%d %d", &t, &f); // b = 7 - f; // l = left[t-1][f-1]; // r = 7 - l; while (1) { fail:; while (z > 0 && field[x][y][z - 1] == 0) { z--; // field[x][y][z+1] = 0; // field[x][y][z] = t; } for (j = 0; j < 2; j++) { if (z == 0) { goto fallend; } int tmp = dice[t - 1][f - 1][j]; if (tmp == 0) { if (field[x][y + 1][z - 1] == 0) { // field[x][y][z] = 0; // field[x][y+1][z-1] = f; y++; z--; tmp2 = f; f = 7 - t; t = tmp2; goto fail; } } else if (tmp == 1) { if (field[x + 1][y][z - 1] == 0) { // field[x][y][z] = 0; t = left[t - 1][f - 1]; // field[x+1][y][z-1] = t; x++; z--; goto fail; } } else if (tmp == 2) { if (field[x][y - 1][z - 1] == 0) { // field[x][y][z] = 0; // field[x][y+1][z-1] = 7 - f; y--; z--; tmp2 = t; t = 7 - f; f = tmp2; goto fail; } } else { if (field[x - 1][y][z - 1] == 0) { // field[x][y][z] = 0; t = 7 - left[t - 1][f - 1]; // field[x-1][y][z-1] = t; x--; z--; goto fail; } } } break; } fallend: field[x][y][z] = t; // printf("%d %d %d %d\n", x, y, z, t); } for (i = 0; i < 100; i++) { for (j = 0; j < 100; j++) { for (k = 0; k < 100; k++) { if (k == 99 || field[i][j][k + 1] == 0) { int tmpres = field[i][j][k]; result[tmpres - 1]++; } } } } for (i = 0; i < 6; i++) { printf("%d", result[i]); if (i != 5) { printf(" "); } } printf("\n"); } return 0; }
[["-", 49, 50, 51, 83, 0, 83, 0, 83, 0, 13], ["+", 49, 50, 51, 83, 0, 83, 0, 83, 0, 13]]
0
994
//転がす方向を決める処理と実際に転がす処理を分けると楽ちん。 // vec == 3は下面じゃないからね!!! #include <iostream> using namespace std; int n; int t, f; int dy[4] = {1, 0, -1, 0}; int dx[4] = {0, 1, 0, -1}; int tfrbl[7][4] = {{0}, {3, 5, 4, 2}, {3, 1, 4, 6}, {6, 5, 1, 2}, {1, 5, 6, 2}, {3, 6, 4, 1}, {3, 2, 4, 5}}; //側面右回り // tfrblのどれを下にするか(f=0,r=1,b=2,l=3で指定)、そしたら転がす。 //仕様がこんがらがる。補正の補正の…うわぁぁぁ。 // vec = 盤面におけるサイコロの移動方向、zure = // 今、前面がtfrbl[t][zure]となっている。 void goron(int vec, int zure, int &t, int &f) { int p = tfrbl [t] [(vec + zure + 2) % 4]; // tの変化は、転がす方向とずれに影響(というより、下にする面の値に影響) int q; // fの変化は、転がす方向にのみ影響 if (vec == 2) q = t; if (vec == 1 || vec == 3) q = f; if (vec == 0) q = 7 - t; t = p; f = q; } int main() { while (cin >> n) { if (!n) break; int high[300][300] = {0}, cy = 114, cx = 114; int num[300][300] = {0}; while (n--) { cin >> t >> f; int py = cy, px = cx; while (high[py][px]) { int i, index = -1; int zure; //例:t = 1, f = 4ならzure = 2になる for (zure = 0; zure < 4; zure++) { if (tfrbl[t][zure] == f) break; } // i, index : 盤面におけるサイコロの移動方向 for (i = 0; i < 4; i++) { //転がる条件(下にする面の値は、tと転がす方向だけでなく向きにも影響される。) if (high[py + dy[i]][px + dx[i]] < high[py][px] && tfrbl[t][(i + zure) % 4] >= 4) if (index == -1 || tfrbl[t][(i + zure) % 4] > tfrbl[t][(index + zure) % 4]) { index = i; } } // cout << index << endl; if (index == -1) break; //転がす( dy[i], dx[i] //)進むために、tfrbl[(index+zure)%4]が下になるように転がす。 //同じt,fにおいて、同じ面を下にして転がしても、実際に転がす方向によって、次のt,fが変わるので要注意。 goron(index, zure, t, f); py += dy[index]; px += dx[index]; } high[py][px]++; num[py][px] = t; // cout << py << " " << px << " " << t << " " << f << endl; } int count[7] = {0}; for (int i = 0; i < 300; i++) { for (int j = 0; j < 300; j++) { count[num[i][j]]++; } } for (int i = 1; i < 6; i++) { cout << count[i] << " "; } cout << count[6] << endl; } return 0; }
//転がす方向を決める処理と実際に転がす処理を分けると楽ちん。 // vec == 3は下面じゃないからね!!! #include <iostream> using namespace std; int n; int t, f; int dy[4] = {1, 0, -1, 0}; int dx[4] = {0, 1, 0, -1}; int tfrbl[7][4] = {{0}, {3, 5, 4, 2}, {3, 1, 4, 6}, {6, 5, 1, 2}, {1, 5, 6, 2}, {3, 6, 4, 1}, {3, 2, 4, 5}}; //側面右回り // tfrblのどれを下にするか(f=0,r=1,b=2,l=3で指定)、そしたら転がす。 //仕様がこんがらがる。補正の補正の…うわぁぁぁ。 // vec = 盤面におけるサイコロの移動方向、zure = // 今、前面がtfrbl[t][zure]となっている。 void goron(int vec, int zure, int &t, int &f) { int p = tfrbl [t] [(vec + zure + 2) % 4]; // tの変化は、転がす方向とずれに影響(というより、下にする面の値に影響) int q; // fの変化は、転がす方向にのみ影響 if (vec == 0) q = t; if (vec == 1 || vec == 3) q = f; if (vec == 2) q = 7 - t; t = p; f = q; } int main() { while (cin >> n) { if (!n) break; int high[300][300] = {0}, cy = 114, cx = 114; int num[300][300] = {0}; while (n--) { cin >> t >> f; int py = cy, px = cx; while (high[py][px]) { int i, index = -1; int zure; //例:t = 1, f = 4ならzure = 2になる for (zure = 0; zure < 4; zure++) { if (tfrbl[t][zure] == f) break; } // i, index : 盤面におけるサイコロの移動方向 for (i = 0; i < 4; i++) { //転がる条件(下にする面の値は、tと転がす方向だけでなく向きにも影響される。) if (high[py + dy[i]][px + dx[i]] < high[py][px] && tfrbl[t][(i + zure) % 4] >= 4) if (index == -1 || tfrbl[t][(i + zure) % 4] > tfrbl[t][(index + zure) % 4]) { index = i; } } // cout << index << endl; if (index == -1) break; //転がす( dy[i], dx[i] //)進むために、tfrbl[(index+zure)%4]が下になるように転がす。 //同じt,fにおいて、同じ面を下にして転がしても、実際に転がす方向によって、次のt,fが変わるので要注意。 goron(index, zure, t, f); py += dy[index]; px += dx[index]; } high[py][px]++; num[py][px] = t; // cout << py << " " << px << " " << t << " " << f << endl; } int count[7] = {0}; for (int i = 0; i < 300; i++) { for (int j = 0; j < 300; j++) { count[num[i][j]]++; } } for (int i = 1; i < 6; i++) { cout << count[i] << " "; } cout << count[6] << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
595
#include <iostream> #include <string.h> using namespace std; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; int tb[7][5] = { {}, {3, 5, 4, 2, 6}, {3, 1, 4, 6, 5}, {6, 5, 1, 2, 4}, {1, 5, 6, 2, 3}, {3, 6, 4, 1, 2}, {4, 5, 3, 2, 1}, }; int main() { int n; while (cin >> n && n) { const int M = 110; int d[M * 3][M * 3]; int a[M * 3][M * 3]; memset(d, 0, sizeof d); memset(a, 0, sizeof a); for (int i = 0; i < n; i++) { int t, f; cin >> t >> f; int x = M, y = M; while (1) { int dir; for (int j = 0; j < 4; j++) { if (tb[t][j] == f) dir = j; } int tj = -1, Max = t; for (int j = 0; j < 4; j++) { if (tb[t][(j + dir) % 4] >= 4 && tb[t][(j + dir) % 4] >= Max) { if (d[x + dx[j]][y + dy[j]] < d[x][y]) { tj = j; Max = tb[t][(j + dir) % 4]; } } } if (tj == -1) { d[x][y]++; a[x][y] = t; break; } x += dx[tj], y += dy[tj]; if (tj == 0) { f = t; t = tb[t][(tj + 2 + dir) % 4]; } if (tj == 1) { t = tb[t][(tj + 2 + dir) % 4]; } if (tj == 2) { f = tb[t][4]; t = tb[t][(tj + 2 + dir) % 4]; } if (tj == 3) { t = tb[t][(tj + 2 + dir) % 4]; } } } int ans[7]; memset(ans, 0, sizeof ans); for (int i = 0; i < M * 3; i++) for (int j = 0; j < M * 3; j++) { ans[a[i][j]]++; } for (int i = 1; i <= 6; i++) { if (i == 6) cout << ans[i] << endl; else cout << ans[i] << " "; } } return 0; }
#include <iostream> #include <string.h> using namespace std; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; int tb[7][5] = { {}, {3, 5, 4, 2, 6}, {3, 1, 4, 6, 5}, {6, 5, 1, 2, 4}, {1, 5, 6, 2, 3}, {3, 6, 4, 1, 2}, {4, 5, 3, 2, 1}, }; int main() { int n; while (cin >> n && n) { const int M = 110; int d[M * 3][M * 3]; int a[M * 3][M * 3]; memset(d, 0, sizeof d); memset(a, 0, sizeof a); for (int i = 0; i < n; i++) { int t, f; cin >> t >> f; int x = M, y = M; while (1) { int dir; for (int j = 0; j < 4; j++) { if (tb[t][j] == f) dir = j; } int tj = -1, Max = 0; for (int j = 0; j < 4; j++) { if (tb[t][(j + dir) % 4] >= 4 && tb[t][(j + dir) % 4] >= Max) { if (d[x + dx[j]][y + dy[j]] < d[x][y]) { tj = j; Max = tb[t][(j + dir) % 4]; } } } if (tj == -1) { d[x][y]++; a[x][y] = t; break; } x += dx[tj], y += dy[tj]; if (tj == 0) { f = t; t = tb[t][(tj + 2 + dir) % 4]; } if (tj == 1) { t = tb[t][(tj + 2 + dir) % 4]; } if (tj == 2) { f = tb[t][4]; t = tb[t][(tj + 2 + dir) % 4]; } if (tj == 3) { t = tb[t][(tj + 2 + dir) % 4]; } } } int ans[7]; memset(ans, 0, sizeof ans); for (int i = 0; i < M * 3; i++) for (int j = 0; j < M * 3; j++) { ans[a[i][j]]++; } for (int i = 1; i <= 6; i++) { if (i == 6) cout << ans[i] << endl; else cout << ans[i] << " "; } } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 22], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
654
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> //#include<cctype> #include <climits> #include <iostream> #include <map> #include <string> #include <vector> //#include<list> #include <algorithm> #include <deque> #include <queue> //#include<numeric> #include <complex> #include <utility> //#include<memory> #include <cassert> #include <functional> #include <random> #include <set> #include <stack> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; enum Dir { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT }; int trans[6] = {-1, -1, 3, 1, 2, 0}; int memo[6][4] = {{3, 5, 4, 2}, {3, 1, 4, 6}, {1, 2, 6, 5}, {1, 5, 6, 2}, {1, 3, 6, 4}, {3, 5, 4, 2}}; class Dice { public: Dice() { d.resize(6); } Dice(int top, int front) { d.resize(6); set(top, front); } int find(int num) const { for (int i = 0; i < 6; i++) if (num == d[i]) return i; assert(0); return -1; } int get(int dir) const { return d[dir]; } void set(int top, int front) { d[TOP] = top; d[BOTTOM] = 7 - top; d[FRONT] = front; d[BACK] = 7 - front; int index = 0; for (; index < 4; index++) { if (memo[top - 1][index] == front) break; } d[RIGHT] = memo[top - 1][(index + 1) % 4]; d[LEFT] = 7 - d[RIGHT]; } void rotate(int dir) { vector<int> nd = d; if (dir == 0) { nd[TOP] = d[LEFT]; nd[LEFT] = d[BOTTOM]; nd[BOTTOM] = d[RIGHT]; nd[RIGHT] = d[TOP]; } if (dir == 1) { nd[TOP] = d[FRONT]; nd[FRONT] = d[BOTTOM]; nd[BOTTOM] = d[BACK]; nd[BACK] = d[TOP]; } if (dir == 2) { nd[TOP] = d[RIGHT]; nd[RIGHT] = d[BOTTOM]; nd[BOTTOM] = d[LEFT]; nd[LEFT] = d[TOP]; } if (dir == 3) { nd[TOP] = d[BACK]; nd[BACK] = d[BOTTOM]; nd[BOTTOM] = d[FRONT]; nd[FRONT] = d[TOP]; } d = nd; } private: vector<int> d; }; const int MAX = 111; int field[MAX][MAX][MAX]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n) { if (n == 0) break; memset(field, 0, sizeof(field)); for (int i = 0; i < n; i++) { int t, f; cin >> t >> f; Dice dice(t, f); int y = MAX / 2, x = MAX / 2; for (int h = MAX - 1; h >= 0;) { if (h == 0) { field[h][y][x] = dice.get(TOP); break; } if (field[h - 1][y][x] == 0) { h--; continue; } bool finish = true; for (int num = 6; num >= 4; num--) { int d = dice.find(num); if (d == BOTTOM || d == TOP) continue; int dir = trans[d]; int ny = y + dy[dir], nx = x + dx[dir]; if (field[h - 1][ny][nx] == 0) { dice.rotate(dir); h--; y = ny; x = nx; finish = false; break; } } if (finish) { field[h][y][x] = dice.get(TOP); break; } } } vector<int> ans(6); for (int y = 0; y < MAX; y++) for (int x = 0; x < MAX; x++) { for (int h = MAX - 1; h >= 0; h--) { if (field[h][y][x] > 0) { ans[field[h][y][x] - 1]++; break; } } } for (int i = 0; i < 6; i++) { cout << ans[i]; if (i < 5) cout << " "; } cout << endl; } return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> //#include<cctype> #include <climits> #include <iostream> #include <map> #include <string> #include <vector> //#include<list> #include <algorithm> #include <deque> #include <queue> //#include<numeric> #include <complex> #include <utility> //#include<memory> #include <cassert> #include <functional> #include <random> #include <set> #include <stack> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; enum Dir { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT }; int trans[6] = {-1, -1, 3, 1, 2, 0}; int memo[6][4] = {{3, 5, 4, 2}, {3, 1, 4, 6}, {1, 2, 6, 5}, {1, 5, 6, 2}, {1, 3, 6, 4}, {2, 4, 5, 3}}; class Dice { public: Dice() { d.resize(6); } Dice(int top, int front) { d.resize(6); set(top, front); } int find(int num) const { for (int i = 0; i < 6; i++) if (num == d[i]) return i; assert(0); return -1; } int get(int dir) const { return d[dir]; } void set(int top, int front) { d[TOP] = top; d[BOTTOM] = 7 - top; d[FRONT] = front; d[BACK] = 7 - front; int index = 0; for (; index < 4; index++) { if (memo[top - 1][index] == front) break; } d[RIGHT] = memo[top - 1][(index + 1) % 4]; d[LEFT] = 7 - d[RIGHT]; } void rotate(int dir) { vector<int> nd = d; if (dir == 0) { nd[TOP] = d[LEFT]; nd[LEFT] = d[BOTTOM]; nd[BOTTOM] = d[RIGHT]; nd[RIGHT] = d[TOP]; } if (dir == 1) { nd[TOP] = d[FRONT]; nd[FRONT] = d[BOTTOM]; nd[BOTTOM] = d[BACK]; nd[BACK] = d[TOP]; } if (dir == 2) { nd[TOP] = d[RIGHT]; nd[RIGHT] = d[BOTTOM]; nd[BOTTOM] = d[LEFT]; nd[LEFT] = d[TOP]; } if (dir == 3) { nd[TOP] = d[BACK]; nd[BACK] = d[BOTTOM]; nd[BOTTOM] = d[FRONT]; nd[FRONT] = d[TOP]; } d = nd; } private: vector<int> d; }; const int MAX = 111; int field[MAX][MAX][MAX]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n) { if (n == 0) break; memset(field, 0, sizeof(field)); for (int i = 0; i < n; i++) { int t, f; cin >> t >> f; Dice dice(t, f); int y = MAX / 2, x = MAX / 2; for (int h = MAX - 1; h >= 0;) { if (h == 0) { field[h][y][x] = dice.get(TOP); break; } if (field[h - 1][y][x] == 0) { h--; continue; } bool finish = true; for (int num = 6; num >= 4; num--) { int d = dice.find(num); if (d == BOTTOM || d == TOP) continue; int dir = trans[d]; int ny = y + dy[dir], nx = x + dx[dir]; if (field[h - 1][ny][nx] == 0) { dice.rotate(dir); h--; y = ny; x = nx; finish = false; break; } } if (finish) { field[h][y][x] = dice.get(TOP); break; } } } vector<int> ans(6); for (int y = 0; y < MAX; y++) for (int x = 0; x < MAX; x++) { for (int h = MAX - 1; h >= 0; h--) { if (field[h][y][x] > 0) { ans[field[h][y][x] - 1]++; break; } } } for (int i = 0; i < 6; i++) { cout << ans[i]; if (i < 5) cout << " "; } cout << endl; } return 0; }
[["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13], ["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 21], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 21]]
1
1,108
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1e8; int f[6][6] = {{-1, 3, 5, 4, 2, -1}, {4, -1, 1, 6, -1, 3}, {2, 6, -1, -1, 1, 5}, {5, 1, -1, -1, 6, 2}, {3, -1, 6, 1, -1, 4}, {-1, 4, 2, 5, 3, -1}}; int main() { int n; while (cin >> n, n) { vvp in(110, vp(110)); rep(i, n) { int a, b; cin >> a >> b; if (!i) { in[55][55] = pii(a, 1); continue; } int c = f[a - 1][b - 1]; int x = 55, y = 55; while (1) { // cout<<a<<" "<<b<<" "<<c<<endl; // cout<<x<<" "<<y<<endl; int ma = -1; if (in[x + 1][y].second < in[x][y].second && b > 3) ma = max(ma, b); if (in[x][y + 1].second < in[x][y].second && c > 3) ma = max(ma, c); if (in[x - 1][y].second < in[x][y].second && 7 - b > 3) ma = max(ma, 7 - b); if (in[x][y - 1].second < in[x][y].second && 7 - c > 3) ma = max(ma, 7 - c); if (ma == -1) break; int na, nb, nc; if (b == ma) { na = 7 - b, nb = a, nc = c; x++; } else if (c == ma) { na = 7 - c, nb = b, nc = a; y++; } else if (7 - b == ma) { na = b, nb = 7 - a, nc = c; x--; } else { na = c, nb = b, nc = 7 - a; y--; } a = na; b = nb; c = nc; } in[x][y].second++; in[x][y].first = a; /* vi out(7); rep(k,110)rep(j,110)out[in[k][j].first]++; rep(k,5)cout<<out[k+1]<<" "; cout<<out[6]<<endl;*/ } vi out(7); rep(i, 110) rep(j, 110) out[in[i][j].first]++; rep(i, 5) cout << out[i + 1] << " "; cout << out[6] << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1e8; int f[6][6] = {{-1, 3, 5, 2, 4, -1}, {4, -1, 1, 6, -1, 3}, {2, 6, -1, -1, 1, 5}, {5, 1, -1, -1, 6, 2}, {3, -1, 6, 1, -1, 4}, {-1, 4, 2, 5, 3, -1}}; int main() { int n; while (cin >> n, n) { vvp in(110, vp(110)); rep(i, n) { int a, b; cin >> a >> b; if (!i) { in[55][55] = pii(a, 1); continue; } int c = f[a - 1][b - 1]; int x = 55, y = 55; while (1) { // cout<<a<<" "<<b<<" "<<c<<endl; // cout<<x<<" "<<y<<endl; int ma = -1; if (in[x + 1][y].second < in[x][y].second && b > 3) ma = max(ma, b); if (in[x][y + 1].second < in[x][y].second && c > 3) ma = max(ma, c); if (in[x - 1][y].second < in[x][y].second && 7 - b > 3) ma = max(ma, 7 - b); if (in[x][y - 1].second < in[x][y].second && 7 - c > 3) ma = max(ma, 7 - c); if (ma == -1) break; int na, nb, nc; if (b == ma) { na = 7 - b, nb = a, nc = c; x++; } else if (c == ma) { na = 7 - c, nb = b, nc = a; y++; } else if (7 - b == ma) { na = b, nb = 7 - a, nc = c; x--; } else { na = c, nb = b, nc = 7 - a; y--; } a = na; b = nb; c = nc; } in[x][y].second++; in[x][y].first = a; /* vi out(7); rep(k,110)rep(j,110)out[in[k][j].first]++; rep(k,5)cout<<out[k+1]<<" "; cout<<out[6]<<endl;*/ } vi out(7); rep(i, 110) rep(j, 110) out[in[i][j].first]++; rep(i, 5) cout << out[i + 1] << " "; cout << out[6] << endl; } }
[["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13], ["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 21], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 21], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13]]
1
710
#include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; struct dice { int top; int bottom; int front; int back; int left; int right; }; void rotate_front(dice *d) { int tmp = d->top; d->top = d->back; d->back = d->bottom; d->bottom = d->front; d->front = tmp; } void rotate_back(dice *d) { int tmp = d->top; d->top = d->front; d->front = d->bottom; d->bottom = d->back; d->back = tmp; } void rotate_right(dice *d) { int tmp = d->top; d->top = d->left; d->left = d->bottom; d->bottom = d->right; d->right = tmp; } void rotate_left(dice *d) { int tmp = d->top; d->top = d->right; d->right = d->bottom; d->bottom = d->left; d->left = tmp; } // t=top, f=front void decide_all_aspects(dice *d, int t, int f) { d->top = t; d->front = f; d->bottom = 7 - t; d->back = 7 - f; if (t == 1) { if (f == 2) d->right = 3; if (f == 3) d->right = 5; if (f == 5) d->right = 4; if (f == 4) d->right = 2; } if (t == 2) { if (f == 1) d->right = 4; if (f == 4) d->right = 6; if (f == 6) d->right = 3; if (f == 3) d->right = 1; } if (t == 3) { if (f == 1) d->right = 2; if (f == 2) d->right = 6; if (f == 6) d->right = 5; if (f == 5) d->right = 1; } if (t == 4) { if (f == 1) d->right = 5; if (f == 5) d->right = 6; if (f == 6) d->right = 2; if (f = -2) d->right = 1; } if (t == 5) { if (f == 1) d->right = 3; if (f == 3) d->right = 6; if (f == 6) d->right = 4; if (f == 4) d->right = 1; } if (t == 6) { if (f == 2) d->right = 4; if (f == 4) d->right = 5; if (f == 5) d->right = 3; if (f == 3) d->right = 2; } d->left = 7 - d->right; } int n; int t[101], f[101]; int fie[101][101][51]; int siz[101][101]; void roll(dice *d) { int z = siz[50][50]; int nx = 50, ny = 50; while (1) { int move = -1, mob = -1; for (int i = 0; i < 4; i++) { int nnx = nx + dx[i], nny = ny + dy[i]; if (i == 0 && z > siz[nny][nnx] && d->right > 3) { if (mob < d->right) { move = 0; mob = d->right; } } if (i == 1 && z > siz[nny][nnx] && d->back > 3) { if (mob < d->back) { move = 1; mob = d->back; } } if (i == 2 && z > siz[nny][nnx] && d->left > 3) { if (mob < d->left) { move = 2; mob = d->left; } } if (i == 3 && z > siz[nny][nnx] && d->front > 3) { if (mob < d->front) { move = 3; mob = d->front; } } } if (move == -1) { z = siz[ny][nx]; fie[ny][nx][z] = d->top; siz[ny][nx]++; break; } if (move >= 0) { nx += dx[move]; ny += dy[move]; z = siz[ny][nx]; if (move == 0) rotate_right(d); if (move == 1) rotate_back(d); if (move == 2) rotate_left(d); if (move == 3) rotate_front(d); } } } int res[7]; int main(void) { while (1) { scanf("%d", &n); if (n == 0) break; memset(fie, 0, sizeof(fie)); memset(siz, 0, sizeof(siz)); for (int i = 0; i < n; i++) { scanf("%d%d", &t[i], &f[i]); dice d; decide_all_aspects(&d, t[i], f[i]); roll(&d); } memset(res, 0, sizeof(res)); for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) { if (siz[i][j] > 0) res[fie[i][j][siz[i][j] - 1]]++; } } for (int i = 1; i <= 6; i++) { printf("%d%c", res[i], i == 6 ? '\n' : ' '); } } return 0; }
#include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; struct dice { int top; int bottom; int front; int back; int left; int right; }; void rotate_front(dice *d) { int tmp = d->top; d->top = d->back; d->back = d->bottom; d->bottom = d->front; d->front = tmp; } void rotate_back(dice *d) { int tmp = d->top; d->top = d->front; d->front = d->bottom; d->bottom = d->back; d->back = tmp; } void rotate_right(dice *d) { int tmp = d->top; d->top = d->left; d->left = d->bottom; d->bottom = d->right; d->right = tmp; } void rotate_left(dice *d) { int tmp = d->top; d->top = d->right; d->right = d->bottom; d->bottom = d->left; d->left = tmp; } // t=top, f=front void decide_all_aspects(dice *d, int t, int f) { d->top = t; d->front = f; d->bottom = 7 - t; d->back = 7 - f; if (t == 1) { if (f == 2) d->right = 3; if (f == 3) d->right = 5; if (f == 5) d->right = 4; if (f == 4) d->right = 2; } if (t == 2) { if (f == 1) d->right = 4; if (f == 4) d->right = 6; if (f == 6) d->right = 3; if (f == 3) d->right = 1; } if (t == 3) { if (f == 1) d->right = 2; if (f == 2) d->right = 6; if (f == 6) d->right = 5; if (f == 5) d->right = 1; } if (t == 4) { if (f == 1) d->right = 5; if (f == 5) d->right = 6; if (f == 6) d->right = 2; if (f == 2) d->right = 1; } if (t == 5) { if (f == 1) d->right = 3; if (f == 3) d->right = 6; if (f == 6) d->right = 4; if (f == 4) d->right = 1; } if (t == 6) { if (f == 2) d->right = 4; if (f == 4) d->right = 5; if (f == 5) d->right = 3; if (f == 3) d->right = 2; } d->left = 7 - d->right; } int n; int t[101], f[101]; int fie[101][101][51]; int siz[101][101]; void roll(dice *d) { int z = siz[50][50]; int nx = 50, ny = 50; while (1) { int move = -1, mob = -1; for (int i = 0; i < 4; i++) { int nnx = nx + dx[i], nny = ny + dy[i]; if (i == 0 && z > siz[nny][nnx] && d->right > 3) { if (mob < d->right) { move = 0; mob = d->right; } } if (i == 1 && z > siz[nny][nnx] && d->back > 3) { if (mob < d->back) { move = 1; mob = d->back; } } if (i == 2 && z > siz[nny][nnx] && d->left > 3) { if (mob < d->left) { move = 2; mob = d->left; } } if (i == 3 && z > siz[nny][nnx] && d->front > 3) { if (mob < d->front) { move = 3; mob = d->front; } } } if (move == -1) { z = siz[ny][nx]; fie[ny][nx][z] = d->top; siz[ny][nx]++; break; } if (move >= 0) { nx += dx[move]; ny += dy[move]; z = siz[ny][nx]; if (move == 0) rotate_right(d); if (move == 1) rotate_back(d); if (move == 2) rotate_left(d); if (move == 3) rotate_front(d); } } } int res[7]; int main(void) { while (1) { scanf("%d", &n); if (n == 0) break; memset(fie, 0, sizeof(fie)); memset(siz, 0, sizeof(siz)); for (int i = 0; i < n; i++) { scanf("%d%d", &t[i], &f[i]); dice d; decide_all_aspects(&d, t[i], f[i]); roll(&d); } memset(res, 0, sizeof(res)); for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) { if (siz[i][j] > 0) res[fie[i][j][siz[i][j] - 1]]++; } } for (int i = 1; i <= 6; i++) { printf("%d%c", res[i], i == 6 ? '\n' : ' '); } } return 0; }
[["-", 64, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["-", 64, 9, 0, 57, 15, 339, 51, 11, 12, 13], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
1,305
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define REP(n) rep(i, n) #define all(n) n.begin(), n.end() const int TO = 4, BO = 5, F = 0, B = 1, R = 2, L = 3; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int OFS = 100; struct dice { int die[6]; }; void printd(dice d) { cout << d.die[TO] << endl; cout << d.die[F] << " " << d.die[R] << " " << d.die[B] << " " << d.die[L] << endl; cout << d.die[BO] << endl; } int right(int t, int f) { if (t == 1) { if (f == 2) return 3; if (f == 3) return 4; if (f == 4) return 2; if (f == 5) return 4; } if (t == 2) { if (f == 1) return 4; if (f == 4) return 6; if (f == 6) return 3; if (f == 3) return 1; } if (t == 3) { if (f == 1) return 2; if (f == 2) return 6; if (f == 6) return 5; if (f == 5) return 1; } if (t == 4) { if (f == 1) return 5; if (f == 5) return 6; if (f == 6) return 2; if (f == 2) return 1; } if (t == 5) { if (f == 1) return 3; if (f == 3) return 6; if (f == 6) return 4; if (f == 4) return 1; } if (t == 6) { if (f == 2) return 4; if (f == 4) return 5; if (f == 5) return 3; if (f == 3) return 2; } } void roll(dice *d, int vec) { int t; if (vec == F) { t = d->die[F]; d->die[F] = d->die[TO]; d->die[TO] = d->die[B]; d->die[B] = d->die[BO]; d->die[BO] = t; } if (vec == R) { t = d->die[R]; d->die[R] = d->die[TO]; d->die[TO] = d->die[L]; d->die[L] = d->die[BO]; d->die[BO] = t; } if (vec == L) { t = d->die[L]; d->die[L] = d->die[TO]; d->die[TO] = d->die[R]; d->die[R] = d->die[BO]; d->die[BO] = t; } if (vec == B) { t = d->die[B]; d->die[B] = d->die[TO]; d->die[TO] = d->die[F]; d->die[F] = d->die[BO]; d->die[BO] = t; } }; const int W = 210; typedef pair<int, int> pp; int main() { int n; while (cin >> n && n) { int tops[W][W] = {}, h[W][W] = {}; REP(n) { int t, f; cin >> t >> f; dice tmp; tmp.die[TO] = t; tmp.die[F] = f; tmp.die[R] = right(t, f); tmp.die[L] = 7 - tmp.die[R]; tmp.die[B] = 7 - tmp.die[F]; tmp.die[BO] = 7 - tmp.die[TO]; int x = OFS; int y = OFS; int nowh = h[x][y]; int rollf = 1; // cout << "initialized " << endl; while (rollf) { // printd(tmp); rollf = 0; vector<pp> r; r.push_back(pp(-tmp.die[F], F)); r.push_back(pp(-tmp.die[R], R)); r.push_back(pp(-tmp.die[B], B)); r.push_back(pp(-tmp.die[L], L)); sort(all(r)); rep(j, 4) if (nowh > h[x + dx[r[j].second]][y + dy[r[j].second]] && -r[j].first >= 4) { roll(&tmp, r[j].second); rollf = 1; x += dx[r[j].second]; y += dy[r[j].second]; nowh = h[x][y]; break; } // cout << x << " " << y << endl; } h[x][y]++; tops[x][y] = tmp.die[TO]; } // rep(j,30){rep(i,30) cout << tops[i + 85][j + 85]; cout << endl;} // cout << "rolled " << endl; int ans[7] = {}; rep(i, W) rep(j, W) ans[tops[i][j]]++; cout << ans[1] << " " << ans[2] << " " << ans[3] << " " << ans[4] << " " << ans[5] << " " << ans[6] << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define REP(n) rep(i, n) #define all(n) n.begin(), n.end() const int TO = 4, BO = 5, F = 0, B = 1, R = 2, L = 3; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int OFS = 100; struct dice { int die[6]; }; void printd(dice d) { cout << d.die[TO] << endl; cout << d.die[F] << " " << d.die[R] << " " << d.die[B] << " " << d.die[L] << endl; cout << d.die[BO] << endl; } int right(int t, int f) { if (t == 1) { if (f == 2) return 3; if (f == 3) return 5; if (f == 4) return 2; if (f == 5) return 4; } if (t == 2) { if (f == 1) return 4; if (f == 4) return 6; if (f == 6) return 3; if (f == 3) return 1; } if (t == 3) { if (f == 1) return 2; if (f == 2) return 6; if (f == 6) return 5; if (f == 5) return 1; } if (t == 4) { if (f == 1) return 5; if (f == 5) return 6; if (f == 6) return 2; if (f == 2) return 1; } if (t == 5) { if (f == 1) return 3; if (f == 3) return 6; if (f == 6) return 4; if (f == 4) return 1; } if (t == 6) { if (f == 2) return 4; if (f == 4) return 5; if (f == 5) return 3; if (f == 3) return 2; } } void roll(dice *d, int vec) { int t; if (vec == F) { t = d->die[F]; d->die[F] = d->die[TO]; d->die[TO] = d->die[B]; d->die[B] = d->die[BO]; d->die[BO] = t; } if (vec == R) { t = d->die[R]; d->die[R] = d->die[TO]; d->die[TO] = d->die[L]; d->die[L] = d->die[BO]; d->die[BO] = t; } if (vec == L) { t = d->die[L]; d->die[L] = d->die[TO]; d->die[TO] = d->die[R]; d->die[R] = d->die[BO]; d->die[BO] = t; } if (vec == B) { t = d->die[B]; d->die[B] = d->die[TO]; d->die[TO] = d->die[F]; d->die[F] = d->die[BO]; d->die[BO] = t; } }; const int W = 210; typedef pair<int, int> pp; int main() { int n; while (cin >> n && n) { int tops[W][W] = {}, h[W][W] = {}; REP(n) { int t, f; cin >> t >> f; dice tmp; tmp.die[TO] = t; tmp.die[F] = f; tmp.die[R] = right(t, f); tmp.die[L] = 7 - tmp.die[R]; tmp.die[B] = 7 - tmp.die[F]; tmp.die[BO] = 7 - tmp.die[TO]; int x = OFS; int y = OFS; int nowh = h[x][y]; int rollf = 1; // cout << "initialized " << endl; while (rollf) { // printd(tmp); rollf = 0; vector<pp> r; r.push_back(pp(-tmp.die[F], F)); r.push_back(pp(-tmp.die[R], R)); r.push_back(pp(-tmp.die[B], B)); r.push_back(pp(-tmp.die[L], L)); sort(all(r)); rep(j, 4) if (nowh > h[x + dx[r[j].second]][y + dy[r[j].second]] && -r[j].first >= 4) { roll(&tmp, r[j].second); rollf = 1; x += dx[r[j].second]; y += dy[r[j].second]; nowh = h[x][y]; break; } // cout << x << " " << y << endl; } h[x][y]++; tops[x][y] = tmp.die[TO]; } // rep(j,30){rep(i,30) cout << tops[i + 85][j + 85]; cout << endl;} // cout << "rolled " << endl; int ans[7] = {}; rep(i, W) rep(j, W) ans[tops[i][j]]++; cout << ans[1] << " " << ans[2] << " " << ans[3] << " " << ans[4] << " " << ans[5] << " " << ans[6] << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 57, 64, 37, 0, 13], ["+", 0, 57, 64, 9, 0, 57, 64, 37, 0, 13]]
1
1,251
#include <iostream> using namespace std; #define VAR 210 struct S { int U, D, R, L, F, B, h, x, y; }; int main() { int field[VAR][VAR][VAR]; int n; int t, f; int tmp; int cnt[7]; S dice; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < VAR; i++) { for (int j = 0; j < VAR; j++) { for (int k = 0; k < VAR; k++) { field[i][j][k] = -1; } } } for (int i = 0; i < n; i++) { cin >> t >> f; dice.U = t; dice.D = 7 - t; dice.F = f; dice.B = 7 - f; if (t == 1 && f == 2) dice.R = 3, dice.L = 4; if (t == 1 && f == 3) dice.R = 5, dice.L = 2; if (t == 1 && f == 4) dice.R = 2, dice.L = 5; if (t == 1 && f == 5) dice.R = 4, dice.L = 3; if (t == 2 && f == 1) dice.R = 4, dice.L = 3; if (t == 2 && f == 3) dice.R = 1, dice.L = 6; if (t == 2 && f == 4) dice.R = 6, dice.L = 1; if (t == 2 && f == 6) dice.R = 3, dice.L = 4; if (t == 3 && f == 1) dice.R = 2, dice.L = 5; if (t == 3 && f == 2) dice.R = 6, dice.L = 1; if (t == 3 && f == 5) dice.R = 1, dice.L = 6; if (t == 3 && f == 6) dice.R = 5, dice.L = 2; if (t == 4 && f == 1) dice.R = 5, dice.L = 2; if (t == 4 && f == 2) dice.R = 1, dice.L = 6; if (t == 4 && f == 5) dice.R = 6, dice.L = 1; if (t == 4 && f == 6) dice.R = 2, dice.L = 5; if (t == 5 && f == 1) dice.R = 3, dice.L = 4; if (t == 5 && f == 3) dice.R = 6, dice.L = 1; if (t == 5 && f == 4) dice.R = 1, dice.L = 6; if (t == 5 && f == 6) dice.R = 4, dice.L = 3; if (t == 6 && f == 2) dice.R = 4, dice.L = 3; if (t == 6 && f == 3) dice.R = 2, dice.L = 5; if (t == 6 && f == 4) dice.R = 5, dice.L = 2; if (t == 6 && f == 5) dice.R = 3, dice.L = 4; dice.h = VAR - 1; dice.x = VAR / 2, dice.y = VAR / 2; while (1) { while (dice.h != 0 && field[dice.h - 1][dice.y][dice.x] == -1) dice.h--; int dir = -1; int dirmax = -1; if (dice.h != 0 && field[dice.h - 1][dice.y][dice.x + 1] == -1) { if (dirmax < dice.R && dice.R >= 4) { dir = 3; dirmax = dice.R; } } if (dice.h != 0 && field[dice.h - 1][dice.y][dice.x - 1] == -1) { if (dirmax < dice.L && dice.L >= 4) { dir = 4; dirmax = dice.L; } } if (dice.h != 0 && field[dice.h - 1][dice.y + 1][dice.x] == -1) { if (dirmax < dice.B && dice.B >= 4) { dir = 2; dirmax = dice.B; } } if (dice.h != 0 && field[dice.h - 1][dice.y - 1][dice.x] == -1) { if (dirmax < dice.F && dice.F >= 4) { dir = 1; dirmax = dice.F; } } if (dir == -1) { field[dice.h][dice.y][dice.x] = dice.U; break; } if (dir == 1) { dice.y--; tmp = dice.U; dice.U = dice.B; dice.B = dice.D; dice.D = dice.F; dice.F = tmp; } if (dir == 2) { dice.y++; tmp = dice.U; dice.U = dice.F; dice.F = dice.D; dice.D = dice.B; dice.B = tmp; } if (dir == 3) { dice.x++; tmp = dice.U; dice.U = dice.L; dice.L = dice.D; dice.D = dice.R; dice.R = tmp; } if (dir == 4) { dice.x--; tmp = dice.U; dice.U = dice.R; dice.R = dice.D; dice.D = dice.L; dice.L = tmp; } } } for (int i = 0; i < 7; i++) cnt[i] = 0; for (int i = 0; i < VAR; i++) { for (int j = 0; j < VAR; j++) { for (int k = 0; k <= VAR; k++) { if (k == VAR || field[k][i][j] == -1) { if (k != 0) { cnt[field[k - 1][i][j]]++; } break; } } } } for (int i = 1; i < 6; i++) cout << cnt[i] << " "; cout << cnt[6] << endl; } }
#include <iostream> using namespace std; #define VAR 100 struct S { int U, D, R, L, F, B, h, x, y; }; int main() { int field[VAR][VAR][VAR]; int n; int t, f; int tmp; int cnt[7]; S dice; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < VAR; i++) { for (int j = 0; j < VAR; j++) { for (int k = 0; k < VAR; k++) { field[i][j][k] = -1; } } } for (int i = 0; i < n; i++) { cin >> t >> f; dice.U = t; dice.D = 7 - t; dice.F = f; dice.B = 7 - f; if (t == 1 && f == 2) dice.R = 3, dice.L = 4; if (t == 1 && f == 3) dice.R = 5, dice.L = 2; if (t == 1 && f == 4) dice.R = 2, dice.L = 5; if (t == 1 && f == 5) dice.R = 4, dice.L = 3; if (t == 2 && f == 1) dice.R = 4, dice.L = 3; if (t == 2 && f == 3) dice.R = 1, dice.L = 6; if (t == 2 && f == 4) dice.R = 6, dice.L = 1; if (t == 2 && f == 6) dice.R = 3, dice.L = 4; if (t == 3 && f == 1) dice.R = 2, dice.L = 5; if (t == 3 && f == 2) dice.R = 6, dice.L = 1; if (t == 3 && f == 5) dice.R = 1, dice.L = 6; if (t == 3 && f == 6) dice.R = 5, dice.L = 2; if (t == 4 && f == 1) dice.R = 5, dice.L = 2; if (t == 4 && f == 2) dice.R = 1, dice.L = 6; if (t == 4 && f == 5) dice.R = 6, dice.L = 1; if (t == 4 && f == 6) dice.R = 2, dice.L = 5; if (t == 5 && f == 1) dice.R = 3, dice.L = 4; if (t == 5 && f == 3) dice.R = 6, dice.L = 1; if (t == 5 && f == 4) dice.R = 1, dice.L = 6; if (t == 5 && f == 6) dice.R = 4, dice.L = 3; if (t == 6 && f == 2) dice.R = 4, dice.L = 3; if (t == 6 && f == 3) dice.R = 2, dice.L = 5; if (t == 6 && f == 4) dice.R = 5, dice.L = 2; if (t == 6 && f == 5) dice.R = 3, dice.L = 4; dice.h = VAR - 1; dice.x = VAR / 2, dice.y = VAR / 2; while (1) { while (dice.h != 0 && field[dice.h - 1][dice.y][dice.x] == -1) dice.h--; int dir = -1; int dirmax = -1; if (dice.h != 0 && field[dice.h - 1][dice.y][dice.x + 1] == -1) { if (dirmax < dice.R && dice.R >= 4) { dir = 3; dirmax = dice.R; } } if (dice.h != 0 && field[dice.h - 1][dice.y][dice.x - 1] == -1) { if (dirmax < dice.L && dice.L >= 4) { dir = 4; dirmax = dice.L; } } if (dice.h != 0 && field[dice.h - 1][dice.y + 1][dice.x] == -1) { if (dirmax < dice.B && dice.B >= 4) { dir = 2; dirmax = dice.B; } } if (dice.h != 0 && field[dice.h - 1][dice.y - 1][dice.x] == -1) { if (dirmax < dice.F && dice.F >= 4) { dir = 1; dirmax = dice.F; } } if (dir == -1) { field[dice.h][dice.y][dice.x] = dice.U; break; } if (dir == 1) { dice.y--; tmp = dice.U; dice.U = dice.B; dice.B = dice.D; dice.D = dice.F; dice.F = tmp; } if (dir == 2) { dice.y++; tmp = dice.U; dice.U = dice.F; dice.F = dice.D; dice.D = dice.B; dice.B = tmp; } if (dir == 3) { dice.x++; tmp = dice.U; dice.U = dice.L; dice.L = dice.D; dice.D = dice.R; dice.R = tmp; } if (dir == 4) { dice.x--; tmp = dice.U; dice.U = dice.R; dice.R = dice.D; dice.D = dice.L; dice.L = tmp; } } } for (int i = 0; i < 7; i++) cnt[i] = 0; for (int i = 0; i < VAR; i++) { for (int j = 0; j < VAR; j++) { for (int k = 0; k <= VAR; k++) { if (k == VAR || field[k][i][j] == -1) { if (k != 0) { cnt[field[k - 1][i][j]]++; } break; } } } } for (int i = 1; i < 6; i++) cout << cnt[i] << " "; cout << cnt[6] << endl; } }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
1
1,416
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { List<Rec> list = new ArrayList<Rec>(); for (int h = 1; h <= 150; h++) { for (int w = h + 1; w <= 150; w++) { list.add(new Rec(h, w)); } } Collections.sort(list); Scanner sc = new Scanner(System.in); while (true) { int h = sc.nextInt(); int w = sc.nextInt(); if (h == 0) break; int x = h * h + w * w; int i = list.indexOf(new Rec(h, w)); Rec l = list.get(i++); System.out.println(l.h + " " + l.w); } } } class Rec implements Comparable<Rec> { int h, w; int x; Rec(int h, int w) { this.h = h; this.w = w; x = h * h + w * w; } @Override public int compareTo(Rec o) { if (x == o.x) { if (h == o.h) return 0; else if (h < o.h) return 1; else return -1; } else if (x > o.x) return 1; else return -1; // return Integer.compare(x, o.x); } @Override public boolean equals(Object obj) { return ((Rec)obj).h == h && ((Rec)obj).w == w; } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { List<Rec> list = new ArrayList<Rec>(); for (int h = 1; h <= 150; h++) { for (int w = h + 1; w <= 150; w++) { list.add(new Rec(h, w)); } } Collections.sort(list); Scanner sc = new Scanner(System.in); while (true) { int h = sc.nextInt(); int w = sc.nextInt(); if (h == 0) break; int x = h * h + w * w; int i = list.indexOf(new Rec(h, w)); Rec l = list.get(i + 1); System.out.println(l.h + " " + l.w); } } } class Rec implements Comparable<Rec> { int h, w; int x; Rec(int h, int w) { this.h = h; this.w = w; x = h * h + w * w; } @Override public int compareTo(Rec o) { if (x == o.x) { if (h == o.h) return 0; else if (h > o.h) return 1; else return -1; } else if (x > o.x) return 1; else return -1; // return Integer.compare(x, o.x); } @Override public boolean equals(Object obj) { return ((Rec)obj).h == h && ((Rec)obj).w == w; } }
[["-", 49, 200, 51, 492, 3, 4, 0, 27, 0, 29], ["+", 49, 200, 51, 492, 3, 4, 0, 16, 17, 72], ["+", 49, 200, 51, 492, 3, 4, 0, 16, 12, 499], ["-", 0, 57, 75, 57, 15, 15, 0, 16, 17, 18], ["+", 0, 57, 75, 57, 15, 15, 0, 16, 17, 47]]
3
377
m, M, X, Y, x, y, i, j; main() { while (scanf("%d%d", &x, &y), x || y) { m = x * x + y * y, M = 90000; for (i = 150; i; i--) for (j = i + 1; j <= 150; j++, y = i * i + j * j) if (m == y && x < i) M = y, X = i, Y = j; else if (m < y && M >= y) M = y, X = i, Y = j; printf("%d %d\n", X, Y); } return 0; }
m, M, X, Y, x, y, i, j; main() { while (scanf("%d%d", &x, &y), x || y) { m = x * x + y * y, M = 90000; for (i = 150; i; i--) for (j = i + 1; j <= 150; j++) { y = i * i + j * j; if (m == y && x < i) M = y, X = i, Y = j; else if (m < y && M >= y) M = y, X = i, Y = j; } printf("%d %d\n", X, Y); } return 0; }
[["-", 8, 9, 0, 7, 8, 7, 26, 34, 0, 21], ["+", 0, 52, 8, 9, 0, 7, 8, 7, 0, 25], ["+", 8, 9, 0, 7, 8, 7, 8, 9, 0, 45], ["-", 0, 52, 8, 9, 0, 7, 8, 7, 0, 25], ["+", 0, 7, 8, 7, 8, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 7, 8, 7, 8, 9, 0, 46]]
0
153
#include <stdio.h> int tai(int x, int y) { x = x * x + y * y; return x; } int main() { int h, w, h1, w1; while (1) { int ha = 151, wa = 151; scanf("%d %D", &h1, &w1); if ((h1 == 0) && (w1 == 0)) { return 0; } for (h = 1; h < 150; h++) { for (w = h + 1; w <= 150; w++) { if (((tai(w, h) > tai(w1, h1)) && (tai(h, w) < tai(ha, wa))) || ((tai(w, h) > tai(w1, h1)) && (tai(h, w) == tai(ha, wa)) && (h < ha)) || ((tai(w, h) == tai(w1, h1)) && (tai(h, w) < tai(ha, wa)) && (h1 < h)) || ((tai(w, h) == tai(w1, h1)) && (tai(h, w) == tai(ha, wa)) && (h1 < h) && (h < ha))) { ha = h; wa = w; } } } printf("%d %d\n", ha, wa); } return 0; }
#include <stdio.h> int tai(int x, int y) { x = x * x + y * y; return x; } int main() { int h, w, h1, w1; while (1) { int ha = 151, wa = 151; scanf("%d %d", &h1, &w1); if ((h1 == 0) && (w1 == 0)) { return 0; } for (h = 1; h < 150; h++) { for (w = h + 1; w <= 150; w++) { if (((tai(w, h) > tai(w1, h1)) && (tai(h, w) < tai(ha, wa))) || ((tai(w, h) > tai(w1, h1)) && (tai(h, w) == tai(ha, wa)) && (h < ha)) || ((tai(w, h) == tai(w1, h1)) && (tai(h, w) < tai(ha, wa)) && (h1 < h)) || ((tai(w, h) == tai(w1, h1)) && (tai(h, w) == tai(ha, wa)) && (h1 < h) && (h < ha))) { ha = h; wa = w; } } } printf("%d %d\n", ha, wa); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
307
#include <math.h> #include <stdio.h> int search(int, int); int main(void) { int h, w, dSq, d, ans; for (; scanf("%d %d", &h, &w), h; printf("%d %d\n", ans / 101, ans % 101)) { dSq = h * h + w * w; if (ans = search(h + 1, dSq)) continue; for (d = dSq + 1; !(ans = search(1, d++));) ; } return 0; } int search(int ini, int d) { int h, w; for (h = ini; h * h * 2 < d; h++) { w = sqrt(d - h * h); if (w * w == d - h * h) return h * 101 + w; } return 0; }
#include <math.h> #include <stdio.h> int search(int, int); int main(void) { int h, w, dSq, d, ans; for (; scanf("%d %d", &h, &w), h; printf("%d %d\n", ans / 151, ans % 151)) { dSq = h * h + w * w; if (ans = search(h + 1, dSq)) continue; for (d = dSq + 1; !(ans = search(1, d++));) ; } return 0; } int search(int ini, int d) { int h, w; for (h = ini; h * h * 2 < d; h++) { w = sqrt(d - h * h); if (w * w == d - h * h) return h * 151 + w; } return 0; }
[["-", 0, 7, 26, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 7, 26, 2, 3, 4, 0, 16, 12, 13], ["-", 0, 57, 64, 37, 0, 16, 31, 16, 12, 13], ["+", 0, 57, 64, 37, 0, 16, 31, 16, 12, 13]]
0
185
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; using namespace std; const int MAX = 151; const int INF = INT_MAX; int main() { int H, W; while (cin >> H >> W && W) { int dia = H * H + W * W; pair<int, int> ans = {MAX, MAX}; bool check = false; int dia_tmp = 0; REP(h, 1, MAX) { REP(w, h + 1, MAX) { if (h > H && h != H) { dia_tmp = h * h + W * W; if (dia == dia_tmp) { if (h < ans.first) { check = true; ans = {h, w}; } } } } } int dia_now = 0; if (!check) { REP(h, 1, MAX) { REP(w, h + 1, MAX) { dia_tmp = h * h + w * w; dia_now = ans.first * ans.first + ans.second * ans.second; if (dia < dia_tmp && dia_tmp < dia_now) { ans = {h, w}; } } } } cout << ans.first << " " << ans.second << endl; } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; using namespace std; const int MAX = 151; const int INF = INT_MAX; int main() { int H, W; while (cin >> H >> W && W) { int dia = H * H + W * W; pair<int, int> ans = {MAX, MAX}; bool check = false; int dia_tmp = 0; REP(h, 1, MAX) { REP(w, h + 1, MAX) { if (h > H && h != H) { dia_tmp = h * h + w * w; if (dia == dia_tmp) { if (h < ans.first) { check = true; ans = {h, w}; } } } } } int dia_now = 0; if (!check) { REP(h, 1, MAX) { REP(w, h + 1, MAX) { dia_tmp = h * h + w * w; dia_now = ans.first * ans.first + ans.second * ans.second; if (dia < dia_tmp && dia_tmp < dia_now) { ans = {h, w}; } } } } cout << ans.first << " " << ans.second << endl; } return 0; }
[["-", 0, 1, 0, 11, 12, 16, 12, 16, 31, 22], ["+", 0, 1, 0, 11, 12, 16, 12, 16, 31, 22], ["-", 0, 1, 0, 11, 12, 16, 12, 16, 12, 22], ["+", 0, 1, 0, 11, 12, 16, 12, 16, 12, 22]]
1
308
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin(a), end(a) #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(5) typedef vector<int> vi; typedef vector<string> vs; typedef pair<short, short> P; typedef complex<double> Point; typedef long long ll; const int INF = 114514810; const int MOD = 100000007; const double EPS = 1e-10; const double PI = acos(-1.0); struct edge { int from, to, cost; bool operator<(const edge &e) const { return cost < e.cost; }; bool operator>(const edge &e) const { return cost > e.cost; } }; struct rec { int w, h, t; }; int main() { int w, h, t; while (cin >> h >> w, w) { t = h * h + w * w; int tar = t; while (1) { FOR(i, 1, 151) FOR(j, i + 1, 151) { if (i * i + j * j == tar) { if (tar == t && i == h) continue; else { cout << i << " " << j << endl; goto end; } } else if (i * i + j * j > tar) break; } tar++; } end:; } }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin(a), end(a) #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(5) typedef vector<int> vi; typedef vector<string> vs; typedef pair<short, short> P; typedef complex<double> Point; typedef long long ll; const int INF = 114514810; const int MOD = 100000007; const double EPS = 1e-10; const double PI = acos(-1.0); struct edge { int from, to, cost; bool operator<(const edge &e) const { return cost < e.cost; }; bool operator>(const edge &e) const { return cost > e.cost; } }; struct rec { int w, h, t; }; int main() { int w, h, t; while (cin >> h >> w, w) { t = h * h + w * w; int tar = t; while (1) { FOR(i, 1, 151) FOR(j, i + 1, 151) { if (i * i + j * j == tar) { if (tar == t && i <= h) continue; else { cout << i << " " << j << endl; goto end; } } else if (i * i + j * j > tar) break; } tar++; } end:; } }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 60], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 19]]
1
299
#include <cstdio> int m, M, X, Y, x, y, i, j; main() { while (scanf("%d%d", &x, &y), x || y) { m = x * x + y * y, M = 90000; for (i = 150; i; i--) for (j = i + 1; j <= 150; j++, y = i * i + j * j) if (m == y && x < i) M = y, X = i, Y = j; else if (m < y && M >= y) M = y, X = i, Y = j; printf("%d %d\n", X, Y); } return 0; }
#include <cstdio> int m, M, X, Y, x, y, i, j; main() { while (scanf("%d%d", &x, &y), x || y) { m = x * x + y * y, M = 90000; for (i = 150; i; i--) for (j = i + 1; j <= 150; j++) { y = i * i + j * j; if (m == y && x < i) M = y, X = i, Y = j; else if (m < y && M >= y) M = y, X = i, Y = j; } printf("%d %d\n", X, Y); } return 0; }
[["-", 8, 9, 0, 7, 8, 7, 26, 34, 0, 21], ["+", 0, 52, 8, 9, 0, 7, 8, 7, 0, 25], ["+", 8, 9, 0, 7, 8, 7, 8, 9, 0, 45], ["-", 0, 52, 8, 9, 0, 7, 8, 7, 0, 25], ["+", 0, 7, 8, 7, 8, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 7, 8, 7, 8, 9, 0, 46]]
1
155
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { vector<pair<int, pair<int, int>>> list; for (int i = 1; i <= 100; ++i) { for (int j = i + 1; j <= 100; ++j) { int v = i * i + j * j; list.push_back(pair<int, pair<int, int>>(v, pair<int, int>(i, j))); } } sort(list.begin(), list.end()); int h, w; while (cin >> h >> w, (h | w)) { int v = h * h + w * w; pair<int, pair<int, int>> p(v, pair<int, int>(h, w)); for (auto v : list) { if (p < v) { cout << v.second.first << " " << v.second.second << endl; break; } } } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { vector<pair<int, pair<int, int>>> list; for (int i = 1; i <= 150; ++i) { for (int j = i + 1; j <= 150; ++j) { int v = i * i + j * j; list.push_back(pair<int, pair<int, int>>(v, pair<int, int>(i, j))); } } sort(list.begin(), list.end()); int h, w; while (cin >> h >> w, (h | w)) { int v = h * h + w * w; pair<int, pair<int, int>> p(v, pair<int, int>(h, w)); for (auto v : list) { if (p < v) { cout << v.second.first << " " << v.second.second << endl; break; } } } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
1
234
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long LL; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define FOREACH(i, n) \ for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(v) ((int)v.size()) #define min_3(a, b, c) min(a, min(b, c)) #define max_3(a, b, c) max(a, max(b, c)) #define debug(x) cerr << #x << " = " << (x) << endl; #define PI 3.1415926535897 #define INF 1000000000 //#define INF 9 int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int h, w; int i, j; int minh = 0, minw = 0; ; int taikaku; long long min = 999999999999999; while (1) { cin >> h >> w; if (h == 0 && w == 0) break; min = 99999999999999999; taikaku = h * h + w * w; for (i = 1; i <= 150 && i * i < taikaku; i++) { j = i + 1; while (1) { // 925 int x = i * i + j * j; if (x == taikaku) { if (x < min && h < i) { min = x; minh = i; minw = j; break; } } if (x > taikaku) { if (i != h || j != w) { if (min > x) { min = x; minh = i; minw = j; } } break; } j++; } } cout << "-" << minh << " " << minw << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long LL; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define FOREACH(i, n) \ for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(v) ((int)v.size()) #define min_3(a, b, c) min(a, min(b, c)) #define max_3(a, b, c) max(a, max(b, c)) #define debug(x) cerr << #x << " = " << (x) << endl; #define PI 3.1415926535897 #define INF 1000000000 //#define INF 9 int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int h, w; int i, j; int minh = 0, minw = 0; ; int taikaku; long long min = 999999999999999; while (1) { cin >> h >> w; if (h == 0 && w == 0) break; min = 99999999999999999; taikaku = h * h + w * w; for (i = 1; i <= 150 && i * i < taikaku; i++) { j = i + 1; while (1) { // 925 int x = i * i + j * j; if (x == taikaku) { if (x < min && h < i) { min = x; minh = i; minw = j; break; } } if (x > taikaku) { if (i != h || j != w) { if (min > x) { min = x; minh = i; minw = j; } } break; } j++; } } cout << minh << " " << minw << endl; } return 0; }
[["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["-", 0, 16, 31, 16, 31, 16, 31, 16, 17, 151]]
1
362
#include <algorithm> #include <iostream> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); ++i) #define rep(i, n) reps(i, 0, n) struct Rect { int h, w; int d() const { return h * h + w * w; } bool operator<(const Rect &r) const { return d() < r.d() || (d() == r.d() && h < r.h); } }; int main() { Rect s; while (cin >> s.h >> s.w, s.h && s.w) { Rect ans{1000, 1000}; reps(i, 1, 151) { reps(j, i + 1, 151) { Rect tmp{i, j}; if (s < tmp) { tmp = min(tmp, ans); } } } cout << ans.h << ' ' << ans.w << endl; } }
#include <algorithm> #include <iostream> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); ++i) #define rep(i, n) reps(i, 0, n) struct Rect { int h, w; int d() const { return h * h + w * w; } bool operator<(const Rect &r) const { return d() < r.d() || (d() == r.d() && h < r.h); } }; int main() { Rect s; while (cin >> s.h >> s.w, s.h && s.w) { Rect ans{1000, 1000}; reps(i, 1, 151) { reps(j, i + 1, 151) { Rect tmp{i, j}; if (s < tmp) { ans = min(tmp, ans); } } } cout << ans.h << ' ' << ans.w << endl; } }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22]]
1
197
#include <iostream> #define l(a, b) a *a + b *b #define INF 150 using namespace std; int main() { int h, w, mh, mw; while (cin >> h >> w && h | w) { mh = mw = INF; for (int i = INF; i > 0; i--) { for (int j = INF; j > i; j--) { if ((l(i, j) > l(h, w)) || (l(i, j) == l(h, w) && i > h)) { if (l(i, j) < l(mh, mw) || (l(i, j) == l(h, w) && i < mh)) { mh = i, mw = j; } } } } cout << mh << " " << mw << endl; } }
#include <iostream> #define l(a, b) a *a + b *b #define INF 150 using namespace std; int main() { int h, w, mh, mw; while (cin >> h >> w && h | w) { mh = mw = INF; for (int i = INF; i > 0; i--) { for (int j = INF; j > i; j--) { if ((l(i, j) > l(h, w)) || (l(i, j) == l(h, w) && i > h)) { if (l(i, j) < l(mh, mw) || (l(i, j) == l(mh, mw) && i < mh)) { mh = i, mw = j; } } } } cout << mh << " " << mw << endl; } }
[["-", 0, 16, 31, 16, 12, 2, 3, 4, 0, 22], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 22]]
1
182
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++) #define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--) #define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--) #define DEBUG(C) cerr << #C << " = " << C << endl; using LL = long long; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<LL>; using VVL = vector<VL>; using VD = vector<double>; using VVD = vector<VD>; using PII = pair<int, int>; using PDD = pair<double, double>; using PLL = pair<LL, LL>; using VPII = vector<PII>; #define ALL(a) begin((a)), end((a)) #define RALL(a) rbegin((a)), rend((a)) #define SORT(a) sort(ALL((a))) #define RSORT(a) sort(RALL((a))) #define REVERSE(a) reverse(ALL((a))) #define MP make_pair #define FORE(a, b) for (auto &&a : (b)) #define FIND(s, e) ((s).find(e) != (s).end()) #define EB emplace_back const int INF = 1e9; const int MOD = INF + 7; const LL LLINF = 1e18; map<PII, int> mp; void solve(int h, int w) { int n = h * h + w * w; pair<PII, int> ans = MP(MP(INF, INF), INF); FORE(el, mp) { if (MP(h, w) == el.first) continue; if (n > el.second) continue; if (n == el.second && h >= el.first.first) continue; if (el.second < ans.second) { ans = el; } else if (el.second == ans.second && ans.first.first < el.first.first) { ans = el; } } cout << ans.first.first << " " << ans.first.second << endl; } signed main(void) { int n, m, p, a, b, c, x, y, z, q; string s; REP(h, 150) FOR(w, h + 1, 150) { mp[MP(h + 1, w + 1)] = pow(h + 1, 2) + pow(w + 1, 2); } while (cin >> n >> m, n && m) { solve(n, m); // return 0; } }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++) #define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--) #define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--) #define DEBUG(C) cerr << #C << " = " << C << endl; using LL = long long; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<LL>; using VVL = vector<VL>; using VD = vector<double>; using VVD = vector<VD>; using PII = pair<int, int>; using PDD = pair<double, double>; using PLL = pair<LL, LL>; using VPII = vector<PII>; #define ALL(a) begin((a)), end((a)) #define RALL(a) rbegin((a)), rend((a)) #define SORT(a) sort(ALL((a))) #define RSORT(a) sort(RALL((a))) #define REVERSE(a) reverse(ALL((a))) #define MP make_pair #define FORE(a, b) for (auto &&a : (b)) #define FIND(s, e) ((s).find(e) != (s).end()) #define EB emplace_back const int INF = 1e9; const int MOD = INF + 7; const LL LLINF = 1e18; map<PII, int> mp; void solve(int h, int w) { int n = h * h + w * w; pair<PII, int> ans = MP(MP(INF, INF), INF); FORE(el, mp) { if (MP(h, w) == el.first) continue; if (n > el.second) continue; if (n == el.second && h >= el.first.first) continue; if (el.second < ans.second) { ans = el; } else if (el.second == ans.second && ans.first.first > el.first.first) { ans = el; } } cout << ans.first.first << " " << ans.first.second << endl; } signed main(void) { int n, m, p, a, b, c, x, y, z, q; string s; REP(h, 150) FOR(w, h + 1, 150) { mp[MP(h + 1, w + 1)] = pow(h + 1, 2) + pow(w + 1, 2); } while (cin >> n >> m, n && m) { solve(n, m); // return 0; } }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47]]
1
535
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> VI; typedef vector<VI> VVI; const double PI = 3.14159265358979323846; const double EPS = 1e-12; const int INF = numeric_limits<int>::max() / 2; const int NEG_INF = numeric_limits<int>::min() / 2; int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { int h, w; cin >> h >> w; if (h == 0) break; int len = h * h + w * w; bool flag = false; int resh = 300, resw = 300; for (int hei = 1; hei < 250; hei++) { for (int wid = hei + 1; wid < 255; wid++) { if (len < hei * hei + wid * wid && hei * hei + wid * wid < resh * resh + resw * resw) { resh = hei; resw = wid; } else if (len == hei * hei + wid * wid && hei < h) { flag = true; resh = hei; resw = wid; } if (flag) break; } if (flag) break; } cout << resh << " " << resw << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> VI; typedef vector<VI> VVI; const double PI = 3.14159265358979323846; const double EPS = 1e-12; const int INF = numeric_limits<int>::max() / 2; const int NEG_INF = numeric_limits<int>::min() / 2; int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { int h, w; cin >> h >> w; if (h == 0) break; int len = h * h + w * w; bool flag = false; int resh = 300, resw = 300; for (int hei = 1; hei < 250; hei++) { for (int wid = hei + 1; wid < 255; wid++) { if (len < hei * hei + wid * wid && hei * hei + wid * wid < resh * resh + resw * resw) { resh = hei; resw = wid; } else if (len == hei * hei + wid * wid && hei > h) { flag = true; resh = hei; resw = wid; } if (flag) break; } if (flag) break; } cout << resh << " " << resw << endl; } }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47]]
1
273
#include <algorithm> #include <iostream> #include <utility> #include <vector> using namespace std; #define vi vector<int> int main() { int i, j; int h, w; pair<int, pair<int, int>> p[22500]; int n = 0; for (i = 1; i <= 150; i++) for (j = 1; j <= 150; j++) { p[n].first = i * i + j * j; p[n].second.first = i; p[n].second.second = j; n++; } sort(p, p + 22500); while (1) { cin >> w >> h; if (h == 0 && w == 0) break; for (i = 0; i < 22500; i++) if (p[i].second.first == h && p[i].second.second == w) { cout << p[i + 1].second.first << " " << p[i + 1].second.second << endl; break; } } }
#include <algorithm> #include <iostream> #include <utility> #include <vector> using namespace std; #define vi vector<int> int main() { int i, j; int h, w; pair<int, pair<int, int>> p[22500]; int n = 0; for (i = 1; i <= 150; i++) for (j = i + 1; j <= 150; j++) { p[n].first = i * i + j * j; p[n].second.first = i; p[n].second.second = j; n++; } sort(p, p + 22500); while (1) { cin >> h >> w; if (h == 0 && w == 0) break; for (i = 0; i < 22500; i++) if (p[i].second.first == h && p[i].second.second == w) { cout << p[i + 1].second.first << " " << p[i + 1].second.second << endl; break; } } }
[["+", 0, 7, 8, 7, 10, 11, 12, 16, 31, 22], ["+", 0, 7, 8, 7, 10, 11, 12, 16, 17, 72], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 152], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
224
#include <iostream> using namespace std; int main() { int h, w; while (1) { cin >> h >> w; if (h == 0 && w == 0) break; int area = h * h + w * w; int minh = 200, minw = 200, mina = 100000000; for (int i = 1; i <= 150; i++) { for (int j = i; j <= 150; j++) { int newarea = i * i + j * j; if (area > newarea || (area == newarea && i <= h)) continue; if (newarea < mina || (newarea == mina && i < minh)) { minh = i; minw = j; mina = newarea; } } } cout << minh << " " << minw << endl; } }
#include <iostream> using namespace std; int main() { int h, w; while (1) { cin >> h >> w; if (h == 0 && w == 0) break; int area = h * h + w * w; int minh = 200, minw = 200, mina = 100000000; for (int i = 1; i <= 150; i++) { for (int j = i + 1; j <= 150; j++) { int newarea = i * i + j * j; if (area > newarea || (area == newarea && i <= h)) continue; if (newarea < mina || (newarea == mina && i < minh)) { minh = i; minw = j; mina = newarea; } } } cout << minh << " " << minw << endl; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
168
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; struct Tuple { double d; int h, w; Tuple(double d = 0.0, int h = 0, int w = 0) : d(d), h(h), w(w) {} bool operator<(const Tuple &t) const { if (d == t.d) { if (h == t.h) { return w < t.w; } else { return h < t.h; } } else { return d < t.d; } } bool operator==(const Tuple &t) const { return (d == t.d && h == t.h && w == t.w); } }; int h, w, size_; double d; vector<Tuple> t; int main() { cin.tie(0); ios::sync_with_stdio(false); for (int hh = 1; hh <= 150; ++hh) { for (int ww = 1; ww <= 150; ++ww) { t.push_back({sqrt(hh * hh + ww * ww), hh, ww}); } } sort(t.begin(), t.end()); size_ = t.size(); while (cin >> h >> w, h | w) { d = sqrt(h * h + w * w); for (int i = 0; i < size_; ++i) { if (Tuple(d, h, w) == t[i]) { cout << t[i + 1].h << " " << t[i + 1].w << endl; break; } } } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; struct Tuple { double d; int h, w; Tuple(double d = 0.0, int h = 0, int w = 0) : d(d), h(h), w(w) {} bool operator<(const Tuple &t) const { if (d == t.d) { if (h == t.h) { return w < t.w; } else { return h < t.h; } } else { return d < t.d; } } bool operator==(const Tuple &t) const { return (d == t.d && h == t.h && w == t.w); } }; int h, w, size_; double d; vector<Tuple> t; int main() { cin.tie(0); ios::sync_with_stdio(false); for (int hh = 1; hh <= 150; ++hh) { for (int ww = hh + 1; ww <= 150; ++ww) { t.push_back({sqrt(hh * hh + ww * ww), hh, ww}); } } sort(t.begin(), t.end()); size_ = t.size(); while (cin >> h >> w, h | w) { d = sqrt(h * h + w * w); for (int i = 0; i < size_; ++i) { if (Tuple(d, h, w) == t[i]) { cout << t[i + 1].h << " " << t[i + 1].w << endl; break; } } } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 31, 22], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72]]
1
356
#include <bits/stdc++.h> using namespace std; int main(void) { while (1) { int h, w; cin >> h >> w; if (h == 0) break; bool flag = 0; int ans_i = 200, ans_j = 200; int temp = 2000000000; for (int i = 1; i < 200; i++) { for (int j = i + 1; j < 200; j++) { if (h * h + w * w == i * i + j * j) { if (i == h && j == w) continue; flag = 1; ans_i = i; ans_j = j; break; } } if (flag == 1) break; } if (flag == 1) { cout << ans_i << " " << ans_j << endl; continue; } else { for (int i = 1; i < 200; i++) { for (int j = i + 1; j < 200; j++) { if (temp > i * i + j * j && h * h + w * w < i * i + j * j) { ans_i = i; ans_j = j; temp = i * i + j * j; } } } cout << ans_i << " " << ans_j << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { while (1) { int h, w; cin >> h >> w; if (h == 0) break; bool flag = 0; int ans_i = 200, ans_j = 200; int temp = 2000000000; for (int i = h + 1; i < 200; i++) { for (int j = i + 1; j < 200; j++) { if (h * h + w * w == i * i + j * j) { if (i == h && j == w) continue; flag = 1; ans_i = i; ans_j = j; break; } } if (flag == 1) break; } if (flag == 1) { cout << ans_i << " " << ans_j << endl; continue; } else { for (int i = 1; i < 200; i++) { for (int j = i + 1; j < 200; j++) { if (temp > i * i + j * j && h * h + w * w < i * i + j * j) { ans_i = i; ans_j = j; temp = i * i + j * j; } } } cout << ans_i << " " << ans_j << endl; } } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 31, 22], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72]]
1
267
#include <iostream> using namespace std; int main() { int w, h; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; int d = h * h + w * w; int H = 150; int W = 150; int D = 150 * 150 * 2; for (int i = 1; i <= 150; i++) { for (int j = i + 1; j <= 150; j++) { int z = i * i + j * j; if (z > d) { if (z < D) { H = i; W = j; D = z; } break; } } } for (int i = h + 1; i < 150; i++) { bool flg = false; for (int j = i + 1; j <= 150; j++) { int x = i * i + j * j; if (x == d) { H = i; W = j; D = x; flg = true; } if (x >= d) break; } if (flg) break; } cout << H << ' ' << W << endl; } return 0; }
#include <iostream> using namespace std; int main() { int w, h; while (1) { cin >> h >> w; if (w == 0 && h == 0) break; int d = h * h + w * w; int H = 150; int W = 150; int D = 150 * 150 * 2; for (int i = 1; i <= 150; i++) { for (int j = i + 1; j <= 150; j++) { int z = i * i + j * j; if (z > d) { if (z < D) { H = i; W = j; D = z; } break; } } } for (int i = h + 1; i < 150; i++) { bool flg = false; for (int j = i + 1; j <= 150; j++) { int x = i * i + j * j; if (x == d) { H = i; W = j; D = x; flg = true; } if (x >= d) break; } if (flg) break; } cout << H << ' ' << W << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["-", 0, 52, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
251
#include <algorithm> #include <iostream> #define rng(x1, y1, x2, y2) \ (sum[y2][x2] + sum[y1][x1] - sum[y2][x1] - sum[y1][x2]) using namespace std; int H, W, ra, o, a[40][40], sum[40][40], dp[40][40][40][40]; int solve(int x1, int y1, int x2, int y2) { if (dp[y1][x1][y2][x2] != -1) return dp[y1][x1][y2][x2]; int rng_ = rng(x1, y1, x2, y2); int ret = 1048576 + rng_; for (int i = x1 + 1; i < x2; i++) { int petl = rng(x1, y1, i, y2); int petr = rng(i, y1, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, i, y2); int resr = solve(i, y1, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } for (int i = y1 + 1; i < y2; i++) { int petl = rng(x1, y1, x2, i); int petr = rng(x1, i, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, x2, i); int resr = solve(x1, i, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } return dp[y1][x1][y2][x2] = ret; } int main() { while (scanf("%d%d", &H, &W, &ra), H) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) scanf("%d", &a[i][j]); } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + a[i][j]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = i + 1; k <= H; k++) { for (int l = j + 1; l <= W; l++) dp[i][j][k][l] = -1; } } } o = sum[H][W] - ra; int ret = solve(0, 0, W, H); printf("%d %d\n", (ret >> 20), ((ret - o) & 1048575)); } }
#include <algorithm> #include <iostream> #define rng(x1, y1, x2, y2) \ (sum[y2][x2] + sum[y1][x1] - sum[y2][x1] - sum[y1][x2]) using namespace std; int H, W, ra, o, a[40][40], sum[40][40], dp[40][40][40][40]; int solve(int x1, int y1, int x2, int y2) { if (dp[y1][x1][y2][x2] != -1) return dp[y1][x1][y2][x2]; int rng_ = rng(x1, y1, x2, y2); int ret = 1048576 + rng_; for (int i = x1 + 1; i < x2; i++) { int petl = rng(x1, y1, i, y2); int petr = rng(i, y1, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, i, y2); int resr = solve(i, y1, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } for (int i = y1 + 1; i < y2; i++) { int petl = rng(x1, y1, x2, i); int petr = rng(x1, i, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, x2, i); int resr = solve(x1, i, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } return dp[y1][x1][y2][x2] = ret; } int main() { while (scanf("%d%d%d", &H, &W, &ra), H) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) scanf("%d", &a[i][j]); } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + a[i][j]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = i + 1; k <= H; k++) { for (int l = j + 1; l <= W; l++) dp[i][j][k][l] = -1; } } } o = sum[H][W] - ra; int ret = solve(0, 0, W, H); printf("%d %d\n", (ret >> 20), ((ret - o) & 1048575)); } }
[["-", 51, 34, 31, 2, 3, 4, 0, 5, 0, 6], ["+", 51, 34, 31, 2, 3, 4, 0, 5, 0, 6]]
1
676
#include <algorithm> #include <iostream> #pragma warning(disable : 4996) #define rng(x1, y1, x2, y2) \ (sum[y2][x2] + sum[y1][x1] - sum[y2][x1] - sum[y1][x2]) using namespace std; int H, W, ra, o, a[40][40], sum[40][40], dp[40][40][40][40]; inline int solve(int x1, int y1, int x2, int y2) { if (!dp[y1][x1][y2][x2]) return dp[y1][x1][y2][x2]; int ret = 1048576 + rng(x1, y1, x2, y2); for (int i = x1 + 1; i < x2; i++) { int petl = rng(x1, y1, i, y2), petr = rng(i, y1, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, i, y2); int resr = solve(i, y1, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } for (int i = y1 + 1; i < y2; i++) { int petl = rng(x1, y1, x2, i), petr = rng(x1, i, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, x2, i); int resr = solve(x1, i, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } return dp[y1][x1][y2][x2] = ret; } int main() { while (scanf("%d%d%d", &H, &W, &ra), H) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) scanf("%d", &a[i][j]); } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + a[i][j]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = i + 1; k <= H; k++) { for (int l = j + 1; l <= W; l++) dp[i][j][k][l] = 0; } } } o = sum[H][W] - ra; int ret = solve(0, 0, W, H); printf("%d %d\n", (ret >> 20), ((ret - o) & 1048575)); } }
#include <algorithm> #include <iostream> #pragma warning(disable : 4996) #define rng(x1, y1, x2, y2) \ (sum[y2][x2] + sum[y1][x1] - sum[y2][x1] - sum[y1][x2]) using namespace std; int H, W, ra, o, a[40][40], sum[40][40], dp[40][40][40][40]; inline int solve(int x1, int y1, int x2, int y2) { if (dp[y1][x1][y2][x2]) return dp[y1][x1][y2][x2]; int ret = 1048576 + rng(x1, y1, x2, y2); for (int i = x1 + 1; i < x2; i++) { int petl = rng(x1, y1, i, y2), petr = rng(i, y1, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, i, y2); int resr = solve(i, y1, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } for (int i = y1 + 1; i < y2; i++) { int petl = rng(x1, y1, x2, i), petr = rng(x1, i, x2, y2); if (min(petl, petr) >= o) { int resl = solve(x1, y1, x2, i); int resr = solve(x1, i, x2, y2); int res = ((resl + resr) >> 20 << 20) + min(resl & 1048575, resr & 1048575); ret = max(ret, res); } } return dp[y1][x1][y2][x2] = ret; } int main() { while (scanf("%d%d%d", &H, &W, &ra), H) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) scanf("%d", &a[i][j]); } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + a[i][j]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = i + 1; k <= H; k++) { for (int l = j + 1; l <= W; l++) dp[i][j][k][l] = 0; } } } o = sum[H][W] - ra; int ret = solve(0, 0, W, H); printf("%d %d\n", (ret >> 20), ((ret - o) & 1048575)); } }
[["-", 8, 9, 0, 57, 15, 339, 51, 91, 17, 111]]
1
671
#include <algorithm> #include <map> #include <stdio.h> #include <string.h> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define INF (1 << 28) inline void cmax(int &a, int b) { if (a < b) a = b; } int n, m, S, f[32][32], _sum[32][32]; inline int sum(int x1, int y1, int x2, int y2) { return _sum[x2 + 1][y2 + 1] - _sum[x2 + 1][y1] - _sum[x1][y2 + 1] + _sum[x1][y1]; } int dp[32][32][32][32]; int solve(int lb) { for (int w = 0; w < n; w++) rep(x1, n - w) { const int x2 = x1 + w; for (int h = 0; h < m; h++) rep(y1, m - h) { const int y2 = y1 + h; if (sum(x1, y1, x2, y2) < lb) { dp[x1][x2][y1][y2] = -INF; continue; } dp[x1][x2][y1][y2] = 1; for (int i = x1; i < x2; i++) { cmax(dp[x1][x2][y1][y2], dp[x1][i][y1][y2] + dp[i + 1][x2][y1][y2]); } for (int i = y1; i < y2; i++) { cmax(dp[x1][x2][y1][y2], dp[x1][x2][y1][i] + dp[x1][x2][i + 1][y2]); } } } return dp[0][n - 1][0][m - 1]; } int main() { for (;;) { scanf("%d%d%d", &n, &m, &S); if (n == 0) return 0; rep(i, n) rep(j, m) scanf("%d", f[i] + j); rep(i, n) rep(j, m) { _sum[i + 1][j + 1] = f[i][j] + _sum[i + 1][j] + _sum[i][j + 1] - _sum[i][j]; } const int tot = sum(0, 0, n - 1, m - 1); const int ans = solve(tot - S); int l = 0, r = S + 1; while (r - l > 1) { const int mid = (l + r) / 2; if (solve(tot - S + mid) >= ans) l = mid; else r = mid; } printf("%d %d\n", ans, l); } }
#include <algorithm> #include <map> #include <stdio.h> #include <string.h> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define INF (1 << 28) inline void cmax(int &a, int b) { if (a < b) a = b; } int n, m, S, f[32][32], _sum[40][40]; inline int sum(int x1, int y1, int x2, int y2) { return _sum[x2 + 1][y2 + 1] - _sum[x2 + 1][y1] - _sum[x1][y2 + 1] + _sum[x1][y1]; } int dp[32][32][32][32]; int solve(int lb) { for (int w = 0; w < n; w++) rep(x1, n - w) { const int x2 = x1 + w; for (int h = 0; h < m; h++) rep(y1, m - h) { const int y2 = y1 + h; if (sum(x1, y1, x2, y2) < lb) { dp[x1][x2][y1][y2] = -INF; continue; } dp[x1][x2][y1][y2] = 1; for (int i = x1; i < x2; i++) { cmax(dp[x1][x2][y1][y2], dp[x1][i][y1][y2] + dp[i + 1][x2][y1][y2]); } for (int i = y1; i < y2; i++) { cmax(dp[x1][x2][y1][y2], dp[x1][x2][y1][i] + dp[x1][x2][i + 1][y2]); } } } return dp[0][n - 1][0][m - 1]; } int main() { for (;;) { scanf("%d%d%d", &n, &m, &S); if (n == 0) return 0; rep(i, n) rep(j, m) scanf("%d", f[i] + j); rep(i, n) rep(j, m) { _sum[i + 1][j + 1] = f[i][j] + _sum[i + 1][j] + _sum[i][j + 1] - _sum[i][j]; } const int tot = sum(0, 0, n - 1, m - 1); const int ans = solve(tot - S); int l = 0, r = S + 1; while (r - l > 1) { const int mid = (l + r) / 2; if (solve(tot - S + mid) >= ans) l = mid; else r = mid; } printf("%d %d\n", ans, l); } }
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13]]
1
642
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef pair<int, int> pii; #define rep(i, j) for (int i = 0; i < (j); i++) #define reps(i, j, k) for (int i = j; i <= k; i++) #define in(i, j, k) ((i) >= (j) && (i) <= (k)) #define N 32 #define INF (1e8) const char dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; pii dp[N + 1][N + 1][N + 1][N + 1]; int h, w, s, sum, l; int u[N][N]; pii add(pii a, pii b) { pii r = make_pair(a.first + b.first, min(a.second, b.second)); return r; } pii dfs(int x0, int y0, int x1, int y1) { if (dp[x0][y0][x1][y1].first > 0) return dp[x0][y0][x1][y1]; pii r; int p = 0; for (int i = x0; i < x1; i++) for (int j = y0; j < y1; j++) p += u[i][j]; r = make_pair(1, p); for (int i = x0 + 1; i < x1; i++) { pii t = add(dfs(x0, y0, i, y1), dfs(i, y0, x1, y1)); if (t.second >= l) r = max(r, t); } for (int i = y0 + 1; i < y1; i++) { pii t = add(dfs(x0, y0, x1, i), dfs(x0, i, x1, y1)); if (t.second >= l) r = max(r, t); } return dp[x0][y0][x1][y1] = r; } int main() { for (;;) { scanf("%d%d%d", &h, &w, &s); if (!(h * w * s)) break; sum = 0; memset(dp, -1, sizeof(dp)); rep(i, h) rep(j, w) { scanf("%d", u[i] + j); sum += u[i][j]; } l = sum - s; pii ans = dfs(0, 0, h, w); printf("%d %d\n", ans.first, l - ans.second); } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef pair<int, int> pii; #define rep(i, j) for (int i = 0; i < (j); i++) #define reps(i, j, k) for (int i = j; i <= k; i++) #define in(i, j, k) ((i) >= (j) && (i) <= (k)) #define N 32 #define INF (1e8) const char dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; pii dp[N + 1][N + 1][N + 1][N + 1]; int h, w, s, sum, l; int u[N][N]; pii add(pii a, pii b) { pii r = make_pair(a.first + b.first, min(a.second, b.second)); return r; } pii dfs(int x0, int y0, int x1, int y1) { if (dp[x0][y0][x1][y1].first > 0) return dp[x0][y0][x1][y1]; pii r; int p = 0; for (int i = x0; i < x1; i++) for (int j = y0; j < y1; j++) p += u[i][j]; r = make_pair(1, p); for (int i = x0 + 1; i < x1; i++) { pii t = add(dfs(x0, y0, i, y1), dfs(i, y0, x1, y1)); if (t.second >= l) r = max(r, t); } for (int i = y0 + 1; i < y1; i++) { pii t = add(dfs(x0, y0, x1, i), dfs(x0, i, x1, y1)); if (t.second >= l) r = max(r, t); } return dp[x0][y0][x1][y1] = r; } int main() { for (;;) { scanf("%d%d%d", &h, &w, &s); if (!(h * w * s)) break; sum = 0; memset(dp, -1, sizeof(dp)); rep(i, h) rep(j, w) { scanf("%d", u[i] + j); sum += u[i][j]; } l = sum - s; pii ans = dfs(0, 0, h, w); printf("%d %d\n", ans.first, ans.second - l); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 22]]
1
569
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; final int INF = Integer.MAX_VALUE; final int MIN = Integer.MIN_VALUE; static void solve() { int n = ni(); for (int i = 0; i < n; i++) { int y = ni(); int m = ni(); int d = ni(); System.out.println(countSum(y, m, d)); } } static int countSum(int y, int m, int d) { int sum = 0; for (int i = 1; i < y; i++) { sum += (y % 3 == 0) ? 200 : 195; } if (y % 3 == 0) { sum += (m - 1) * 20; } else { for (int i = 1; i < m; i++) { sum += (m % 2 == 0) ? 19 : 20; } } sum += d; return 196471 - sum; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G - S + "ms"); } private static boolean eof() { if (lenbuf == -1) return true; int lptr = ptrbuf; while (lptr < lenbuf) if (!isSpaceChar(inbuf[lptr++])) return false; try { is.mark(1000); while (true) { int b = is.read(); if (b == -1) { is.reset(); return true; } else if (!isSpaceChar(b)) { is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= //126); } private static int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o)); } }
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; final int INF = Integer.MAX_VALUE; final int MIN = Integer.MIN_VALUE; static void solve() { int n = ni(); for (int i = 0; i < n; i++) { int y = ni(); int m = ni(); int d = ni(); System.out.println(countSum(y, m, d)); } } static int countSum(int y, int m, int d) { int sum = 0; for (int i = 1; i < y; i++) { sum += (i % 3 == 0) ? 200 : 195; } if (y % 3 == 0) { sum += (m - 1) * 20; } else { for (int i = 1; i < m; i++) { sum += (i % 2 == 0) ? 19 : 20; } } sum += d; return 196471 - sum; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G - S + "ms"); } private static boolean eof() { if (lenbuf == -1) return true; int lptr = ptrbuf; while (lptr < lenbuf) if (!isSpaceChar(inbuf[lptr++])) return false; try { is.mark(1000); while (true) { int b = is.read(); if (b == -1) { is.reset(); return true; } else if (!isSpaceChar(b)) { is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= //126); } private static int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o)); } }
[["-", 12, 510, 15, 23, 0, 16, 31, 16, 31, 22], ["+", 12, 510, 15, 23, 0, 16, 31, 16, 31, 22]]
3
1,162
#include <iostream> using namespace std; int main() { while (true) { int h, w; cin >> h >> w; if (h == 0 && w == 0) break; int d = h * h + w * w; int H = 150; int W = 150; int D = 150 * 150 + 150 * 150; for (int y = 1; y <= 150; y++) { for (int x = y + 1; x <= 150; x++) { int z = y * y + x * x; if (z <= d) { continue; } else { if (z < D) { int H = y; int W = x; int D = z; } break; } } } for (int y = h + 1; y < 150; y++) { bool flg = false; for (int x = y + 1; x <= 150; x++) { int z = y * y + x * x; if (z == d) { int H = y; int W = x; int D = z; flg = true; } if (z >= d) break; } if (flg) break; } cout << H << ' ' << W << endl; } return 0; }
#include <iostream> using namespace std; int main() { while (true) { int h, w; cin >> h >> w; if (h == 0 && w == 0) break; int d = h * h + w * w; int H = 150; int W = 150; int D = 150 * 150 + 150 * 150; for (int y = 1; y <= 150; y++) { for (int x = y + 1; x <= 150; x++) { int z = y * y + x * x; if (z <= d) { continue; } else { if (z < D) { H = y; W = x; D = z; } break; } } } for (int y = h + 1; y < 150; y++) { bool flg = false; for (int x = y + 1; x <= 150; x++) { int z = y * y + x * x; if (z == d) { H = y; W = x; D = z; flg = true; } if (z >= d) break; } if (flg) break; } cout << H << ' ' << W << endl; } return 0; }
[["-", 0, 9, 0, 57, 64, 9, 0, 43, 39, 40], ["-", 8, 9, 0, 57, 64, 9, 0, 43, 39, 40]]
1
264
#include <bits/stdc++.h> using namespace std; int main() { int w, h; int H, W; int min; while (1) { cin >> h >> w; if (w == 0 && h == 0) break; int length = w * w + h * h; min = 150 * 150; bool flag = false; for (int i = 1; i <= 150; i++) { for (int j = 1; j <= 150; j++) { int temp = i * i + j * j; if (temp > length) { if (temp < min) { H = i; W = j; min = temp; } } else if (temp == length) { if (i > h) { flag = true; H = i; W = j; min = temp; } } if (flag) break; } if (flag) break; } cout << H << " " << W << endl; ; } }
#include <bits/stdc++.h> using namespace std; int main() { int w, h; int H, W; int min; while (1) { cin >> h >> w; if (w == 0 && h == 0) break; int length = w * w + h * h; min = 150 * 150; bool flag = false; for (int i = 1; i <= 150; i++) { for (int j = i + 1; j <= 150; j++) { int temp = i * i + j * j; if (temp > length) { if (temp < min) { H = i; W = j; min = temp; } } else if (temp == length) { if (i > h) { flag = true; H = i; W = j; min = temp; } } if (flag) break; } if (flag) break; } cout << H << " " << W << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 31, 22], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 35]]
1
200
#include <algorithm> #include <iostream> #include <string> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define all(in) in.begin(), in.end() using namespace std; typedef long long ll; typedef int Def; typedef pair<Def, Def> pii; typedef vector<Def> vi; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10; int main() { int n, m; while (cin >> n >> m, n) { int t = n * n + m * m; for (int i = t;; i++) { loop(w, 1, 160) loop(h, 1, 160) { int tt = h * h + w * w; if (i == tt) { if (h >= w) continue; if (i == t && h <= n) continue; cout << h << " " << w << endl; goto end; } } } end:; } }
#include <algorithm> #include <iostream> #include <string> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define all(in) in.begin(), in.end() using namespace std; typedef long long ll; typedef int Def; typedef pair<Def, Def> pii; typedef vector<Def> vi; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10; int main() { int n, m; while (cin >> n >> m, n) { int t = n * n + m * m; for (int i = t;; i++) { loop(h, 1, 160) loop(w, 1, 160) { int tt = h * h + w * w; if (i == tt) { if (h >= w) continue; if (i == t && h <= n) continue; cout << h << " " << w << endl; goto end; } } } end:; } }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
208
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1186&lang=jp> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ int H, W; vector<pair<int, pii>> rect; void init() { for (int h = 1; h < 500; h++) { for (int w = h + 1; w < 500; w++) { rect.push_back({h * h + w * w, {h, w}}); } } sort(rect.begin(), rect.end()); } void solve() { pair<int, pii> info = {H * H + W * W, {INF, INF}}; auto p = upper_bound(rect.begin(), rect.end(), info)->second; cout << p.first << " " << p.second << endl; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init(); while (cin >> H >> W, H) { solve(); } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1186&lang=jp> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ int H, W; vector<pair<int, pii>> rect; void init() { for (int h = 1; h < 500; h++) { for (int w = h + 1; w < 500; w++) { rect.push_back({h * h + w * w, {h, w}}); } } sort(rect.begin(), rect.end()); } void solve() { pair<int, pii> info = {H * H + W * W, {H, W}}; auto p = upper_bound(rect.begin(), rect.end(), info)->second; cout << p.first << " " << p.second << endl; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init(); while (cin >> H >> W, H) { solve(); } return 0; }
[["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 22], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 22]]
1
547
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int h, w; void solve() { int length2 = h * h + w * w; int minHeight = 150; int minWidth; int minLength = 255000; for (int height = 1; height <= 150; height++) { for (int width = height + 1; width <= 150; width++) { int l = height * height + width * width; if (length2 < l || (length2 == l && h > height)) { if (minLength > l || (minLength == l && minHeight > height)) { minHeight = height; minWidth = width; minLength = l; } } } } cout << minHeight << " " << minWidth << endl; } int main() { while (true) { cin >> h >> w; if (h == 0 && w == 0) break; solve(); } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int h, w; void solve() { int length2 = h * h + w * w; int minHeight = 150; int minWidth; int minLength = 255000; for (int height = 1; height <= 150; height++) { for (int width = height + 1; width <= 150; width++) { int l = height * height + width * width; if (length2 < l || (length2 == l && h < height)) { if (minLength > l || (minLength == l && minHeight > height)) { minHeight = height; minWidth = width; minLength = l; } } } } cout << minHeight << " " << minWidth << endl; } int main() { while (true) { cin >> h >> w; if (h == 0 && w == 0) break; solve(); } return 0; }
[["-", 51, 16, 12, 23, 0, 16, 12, 16, 17, 47], ["+", 51, 16, 12, 23, 0, 16, 12, 16, 17, 18]]
1
189
#include <algorithm> #include <assert.h> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define EXIST(s, e) ((s).find(e) != (s).end()) #define ALL(s) begin(s), end(s) #define VIEW(dat) \ for (auto v : dat) { \ cout << v << endl; \ } #define clr(a) memset((a), 0, sizeof(a)) #define nclr(a) memset((a), -1, sizeof(a)) #define pb push_back #define INRANGE(x, s, e) ((s) <= (x) && (x) < (e)) #define MP(a, b) make_pair((a), (b)) const int INF = 1 << 24; using namespace std; string valOf(int n) { stringstream ss; ss << n; return ss.str(); } int valOf(string s) { return atoi(s.c_str()); } int valOf(char s) { return (unsigned int)(s - '0'); } int gt(int h, int w) { return h * h + w * w; } int main() { // ifstream cin("in.txt"); int h, w, t; while (cin >> h >> w, h || w) { int r = INF, H = 0, W = 0; t = gt(h, w); for (int i = 150; i > 0; --i) { for (int j = 150; j > 0; --j) { if (t == gt(i, j) && i > h && i != j) { r = gt(i, j); H = i; W = j; } else if (t < gt(i, j)) { if (r == gt(i, j)) { H = min(H, i); W = j; } else if (r > gt(i, j)) { r = gt(i, j); H = i; W = j; } } } } cout << H << " " << W << endl; } }
#include <algorithm> #include <assert.h> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define EXIST(s, e) ((s).find(e) != (s).end()) #define ALL(s) begin(s), end(s) #define VIEW(dat) \ for (auto v : dat) { \ cout << v << endl; \ } #define clr(a) memset((a), 0, sizeof(a)) #define nclr(a) memset((a), -1, sizeof(a)) #define pb push_back #define INRANGE(x, s, e) ((s) <= (x) && (x) < (e)) #define MP(a, b) make_pair((a), (b)) const int INF = 1 << 24; using namespace std; string valOf(int n) { stringstream ss; ss << n; return ss.str(); } int valOf(string s) { return atoi(s.c_str()); } int valOf(char s) { return (unsigned int)(s - '0'); } int gt(int h, int w) { return h * h + w * w; } int main() { // ifstream cin("in.txt"); int h, w, t; while (cin >> h >> w, h || w) { int r = INF, H = 0, W = 0; t = gt(h, w); for (int i = 150; i > 0; --i) { for (int j = 150; j > 0; --j) { if (t == gt(i, j) && i > h && i < j) { r = gt(i, j); H = i; W = j; } else if (t < gt(i, j) && i < j) { if (r == gt(i, j)) { H = min(H, i); W = j; } else if (r > gt(i, j)) { r = gt(i, j); H = i; W = j; } } } } cout << H << " " << W << endl; } }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 79], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18], ["+", 75, 76, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 22]]
1
410
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; struct Rec { int w, h, v; Rec(int _w, int _h) : w(_w), h(_h) { v = w * w + h * h; } bool operator<(const Rec &a) const { if (v != a.v) return v < a.v; return h < a.h; } }; int ind[1000][1000]; vector<Rec> rec; int main(void) { for (int i = 1; i < 1000; i++) for (int j = 1; j < i; j++) rec.push_back(Rec(i, j)); sort(rec.begin(), rec.end()); for (int i = 0; i < rec.size(); i++) ind[rec[i].h][rec[i].w] = i; int h, w; while (scanf("%d %d", &h, &w) != -1) { int tarInd = ind[h][w]; printf("%d %d\n", rec[tarInd + 1].h, rec[tarInd + 1].w); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; struct Rec { int w, h, v; Rec(int _w, int _h) : w(_w), h(_h) { v = w * w + h * h; } bool operator<(const Rec &a) const { if (v != a.v) return v < a.v; return h < a.h; } }; int ind[1000][1000]; vector<Rec> rec; int main(void) { for (int i = 1; i < 1000; i++) for (int j = 1; j < i; j++) rec.push_back(Rec(i, j)); sort(rec.begin(), rec.end()); for (int i = 0; i < rec.size(); i++) ind[rec[i].h][rec[i].w] = i; int h, w; while (scanf("%d %d", &h, &w), h) { int tarInd = ind[h][w]; printf("%d %d\n", rec[tarInd + 1].h, rec[tarInd + 1].w); } return 0; }
[["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 79], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
267
#include <iostream> #include <map> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define mp(a, b) make_pair(a, b) int main() { multimap<pair<int, int>, pair<int, int>> m; rep(i, 110) rep(j, i) { if (i == 0 || j == 0) continue; // m.emplace(mp(i*i + j*j, i*j), mp(j, i)); m.insert(mp(mp(i * i + j * j, i * j), mp(j, i))); } int h, w; while (cin >> h >> w, h | w) { pair<int, int> ans = m.upper_bound(mp(h * h + w * w, h * w))->second; cout << ans.first << " " << ans.second << '\n'; } return 0; }
#include <iostream> #include <map> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define mp(a, b) make_pair(a, b) int main() { multimap<pair<int, int>, pair<int, int>> m; rep(i, 150) rep(j, i) { if (i == 0 || j == 0) continue; // m.emplace(mp(i*i + j*j, i*j), mp(j, i)); m.insert(mp(mp(i * i + j * j, i * j), mp(j, i))); } int h, w; while (cin >> h >> w, h | w) { pair<int, int> ans = m.upper_bound(mp(h * h + w * w, h * w))->second; cout << ans.first << ' ' << ans.second << '\n'; } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 16, 31, 16, 31, 16, 12, 103, 0, 104]]
1
176
S = sorted([[i,j] for i in range(1,100) for j in range(i,151)], key = lambda x:x[0]**2+x[1]**2) while True: h,w = list(map(int,input().split())) if h == 0: break hi,wi = S[S.index([h,w])+1] print(hi,wi)
S = sorted([[i,j] for i in range(1,100) for j in range(i+1,151)], key = lambda x:x[0]**2+x[1]**2) while True: h,w = list(map(int,input().split())) if h == 0: break hi,wi = S[S.index([h,w])+1] print(hi,wi)
[["+", 0, 659, 12, 652, 3, 4, 0, 657, 17, 72], ["+", 0, 659, 12, 652, 3, 4, 0, 657, 12, 612]]
5
102
#include <iostream> #include <utility> #include <vector> using namespace std; int main() { int m, t, p, r, i, j, k; while (1) { cin >> m >> t >> p >> r; if (m == 0) break; int ac[t] = {}, wa[t][p + 1], actime[t] = {}; for (i = 0; i < t; i++) { for (j = 0; j < p; j++) wa[i][j] = 0; } for (i = 0; i < r; i++) { int M, T, P, R; cin >> M >> T >> P >> R; if (R != 0) wa[T - 1][P]++; else { ac[T - 1]++; actime[T - 1] += wa[T - 1][P] * 20 + M; } } // cout << actime[0] << actime[1] << endl; bool flag[t] = {}; int bscore = -1, bfinaltime = -1; for (j = 0; j < t; j++) { int team = -1, score = -1, finaltime = 300 * 20; for (i = 0; i < t; i++) { if (flag[i] == true) continue; if (score < ac[i]) { team = i; score = ac[i]; finaltime = actime[i]; } else if (score == ac[i] && finaltime >= actime[i]) { team = i; finaltime = actime[i]; } } if (j != 0) { if (bscore == score && bfinaltime == finaltime) cout << "="; else cout << ","; } cout << team + 1; flag[team] = true; bscore = score; bfinaltime = finaltime; } cout << endl; } return 0; }
#include <iostream> #include <utility> #include <vector> using namespace std; int main() { int m, t, p, r, i, j, k; while (1) { cin >> m >> t >> p >> r; if (m == 0) break; int ac[t] = {}, wa[t][p + 1], actime[t] = {}; for (i = 0; i < t; i++) { for (j = 0; j < p + 1; j++) wa[i][j] = 0; } for (i = 0; i < r; i++) { int M, T, P, R; cin >> M >> T >> P >> R; if (R != 0) wa[T - 1][P]++; else { ac[T - 1]++; actime[T - 1] += wa[T - 1][P] * 20 + M; } } // cout << actime[0] << actime[1] << endl; bool flag[t] = {}; int bscore = -1, bfinaltime = -1; for (j = 0; j < t; j++) { int team = -1, score = -1, finaltime = 300 * 20; for (i = 0; i < t; i++) { if (flag[i] == true) continue; if (score < ac[i]) { team = i; score = ac[i]; finaltime = actime[i]; } else if (score == ac[i] && finaltime >= actime[i]) { team = i; finaltime = actime[i]; } } if (j != 0) { if (bscore == score && bfinaltime == finaltime) cout << "="; else cout << ","; } cout << team + 1; flag[team] = true; bscore = score; bfinaltime = finaltime; } cout << endl; } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
396
#include <bits/stdc++.h> #define REP(a, b, c) for (int a = b; a < (int)c; a++) #define REPE(a, b, c) for (int a = b; a <= (int)c; a++) #define REPD(a, b, c) for (int a = b; a > (int)c; a--) #define rep(a, b) REP(a, 0, b) #define repd(a, b) REPD(a, b, 0) class _datas { public: int m; int t; int p; int j; }; class _score { public: int p; int t; _score(int p, int t) { this->p = p; this->t = t; } }; typedef long long int ll; using namespace std; int main() { while (true) { int M, T, P, R; cin >> M >> T >> P >> R; if (M == 0) break; vector<_datas> datas(R); rep(i, R) cin >> datas[i].m >> datas[i].t >> datas[i].p >> datas[i].j; // rep(i,R) cout << datas[i].m << " "<< datas[i].t << " "<< datas[i].p << " // "<< datas[i].j << " " << endl; vector<_score> scores(T + 1, _score(0, 0)); REPE(i, 1, T) { vector<int> miss_cnt(P, 0); rep(j, R) { _datas data = datas[j]; if (data.t == i) { if (data.j == 0) { scores[i].p++; scores[i].t += data.m + miss_cnt[data.p] * 20; } else { miss_cnt[data.p]++; } } } } /*cout << "score" << endl; REPE(i,1,T){ cout<<i<<":" << scores[i].p << " " << scores[i].t << endl; }*/ _score b_score = _score(0, 0); REPE(i, 1, T) { int max_idx = 1; REPE(j, 2, T) { if (scores[j].p > scores[max_idx].p || (scores[j].p == scores[max_idx].p && scores[j].t <= scores[max_idx].t)) { max_idx = j; } } if (i != 1) { if (b_score.p == scores[max_idx].p && b_score.t == scores[max_idx].t) cout << "="; else cout << ","; } cout << max_idx; b_score = scores[max_idx]; scores[max_idx] = _score(-1, 0); } cout << endl; } return 0; }
#include <bits/stdc++.h> #define REP(a, b, c) for (int a = b; a < (int)c; a++) #define REPE(a, b, c) for (int a = b; a <= (int)c; a++) #define REPD(a, b, c) for (int a = b; a > (int)c; a--) #define rep(a, b) REP(a, 0, b) #define repd(a, b) REPD(a, b, 0) class _datas { public: int m; int t; int p; int j; }; class _score { public: int p; int t; _score(int p, int t) { this->p = p; this->t = t; } }; typedef long long int ll; using namespace std; int main() { while (true) { int M, T, P, R; cin >> M >> T >> P >> R; if (M == 0) break; vector<_datas> datas(R); rep(i, R) cin >> datas[i].m >> datas[i].t >> datas[i].p >> datas[i].j; // rep(i,R) cout << datas[i].m << " "<< datas[i].t << " "<< datas[i].p << " // "<< datas[i].j << " " << endl; vector<_score> scores(T + 1, _score(0, 0)); REPE(i, 1, T) { vector<int> miss_cnt(P + 1, 0); rep(j, R) { _datas data = datas[j]; if (data.t == i) { if (data.j == 0) { scores[i].p++; scores[i].t += data.m + miss_cnt[data.p] * 20; } else { miss_cnt[data.p]++; } } } } /*cout << "score" << endl; REPE(i,1,T){ cout<<i<<":" << scores[i].p << " " << scores[i].t << endl; }*/ _score b_score = _score(0, 0); REPE(i, 1, T) { int max_idx = 1; REPE(j, 2, T) { if (scores[j].p > scores[max_idx].p || (scores[j].p == scores[max_idx].p && scores[j].t <= scores[max_idx].t)) { max_idx = j; } } if (i != 1) { if (b_score.p == scores[max_idx].p && b_score.t == scores[max_idx].t) cout << "="; else cout << ","; } cout << max_idx; b_score = scores[max_idx]; scores[max_idx] = _score(-1, 0); } cout << endl; } return 0; }
[["+", 0, 43, 49, 50, 51, 4, 0, 16, 17, 72], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13]]
1
479
#include <iostream> #include <string> using namespace std; int main() { int solved[51]; int time[51][11]; int team[51]; int m, t, p, r; while (true) { cin >> m >> t >> p >> r; if (m == 0 && t == 0 && p == 0 && r == 0) break; for (int i = 0; i < t; i++) { solved[i] = 0; for (int j = 0; j < p; j++) time[i][j] = 0; team[i] = i + 1; } for (int i = 0; i < r; i++) { int a[4]; for (int j = 0; j < 4; j++) cin >> a[j]; if (a[3] == 0) solved[a[1] - 1] += 1000 - time[a[1] - 1][a[2] - 1] - a[0]; else time[a[1] - 1][a[2] - 1] += 20; } for (int i = 0; i < t - 1; i++) { for (int j = t - 1; j > i; j--) { if (solved[j] > solved[j - 1] || (solved[j] == solved[j - 1] && team[j] > team[j - 1])) { int tmp = solved[j]; solved[j] = solved[j - 1]; solved[j - 1] = tmp; tmp = team[j]; team[j] = team[j - 1]; team[j - 1] = tmp; } } } cout << team[0]; for (int i = 1; i < t; i++) { if (solved[i] == solved[i - 1]) cout << "="; else cout << ","; cout << team[i]; } cout << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { int solved[50]; int time[50][10]; int team[50]; int m, t, p, r; while (true) { cin >> m >> t >> p >> r; if (m == 0 && t == 0 && p == 0 && r == 0) break; for (int i = 0; i < t; i++) { solved[i] = 0; for (int j = 0; j < p; j++) time[i][j] = 0; team[i] = i + 1; } for (int i = 0; i < r; i++) { int a[4]; for (int j = 0; j < 4; j++) cin >> a[j]; if (a[3] == 0) solved[a[1] - 1] += 50000 - time[a[1] - 1][a[2] - 1] - a[0]; else time[a[1] - 1][a[2] - 1] += 20; } for (int i = 0; i < t - 1; i++) { for (int j = t - 1; j > i; j--) { if (solved[j] > solved[j - 1] || (solved[j] == solved[j - 1] && team[j] > team[j - 1])) { int tmp = solved[j]; solved[j] = solved[j - 1]; solved[j - 1] = tmp; tmp = team[j]; team[j] = team[j - 1]; team[j - 1] = tmp; } } } cout << team[0]; for (int i = 1; i < t; i++) { if (solved[i] == solved[i - 1]) cout << "="; else cout << ","; cout << team[i]; } cout << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 64, 1, 0, 11, 12, 16, 31, 16, 31, 13], ["+", 64, 1, 0, 11, 12, 16, 31, 16, 31, 13]]
1
443
#include <bits/stdc++.h> #define f first #define sf second.first #define ss second.second using namespace std; typedef pair<int, int> P; typedef pair<int, P> iP; int main() { int t, p, r, b; while (cin >> b >> t >> p >> r && t) { iP a[51]; int k[11][51] = {}; a[0] = iP(-1, P(-1, -1)); for (int i = 1; i <= t; i++) a[i] = iP(0, P(0, i)); for (int i = 0; i < r; i++) { int ti, pi, m, s; cin >> m >> ti >> pi >> s; if (!s) a[ti].f++, a[ti].sf -= m + k[pi][ti] * 20; else k[pi][ti]++; } sort(a + 1, a + t + 1); for (int i = t; i > 0; i--) { cout << a[i].ss; if (i != 1) if (a[i + 1].f == a[i].f && a[i + 1].sf == a[i].sf) cout << "="; else cout << ","; } cout << endl; } return 0; }
#include <bits/stdc++.h> #define f first #define sf second.first #define ss second.second using namespace std; typedef pair<int, int> P; typedef pair<int, P> iP; int main() { int t, p, r, b; while (cin >> b >> t >> p >> r && t) { iP a[51]; int k[11][51] = {}; a[0] = iP(-1, P(-1, -1)); for (int i = 1; i <= t; i++) a[i] = iP(0, P(0, i)); for (int i = 0; i < r; i++) { int ti, pi, m, s; cin >> m >> ti >> pi >> s; if (!s) a[ti].f++, a[ti].sf -= m + k[pi][ti] * 20; else k[pi][ti]++; } sort(a + 1, a + t + 1); for (int i = t; i > 0; i--) { cout << a[i].ss; if (i != 1) if (a[i - 1].f == a[i].f && a[i - 1].sf == a[i].sf) cout << "="; else cout << ","; } cout << endl; } return 0; }
[["-", 31, 118, 28, 69, 341, 342, 0, 16, 17, 72], ["+", 31, 118, 28, 69, 341, 342, 0, 16, 17, 33]]
1
305
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; class team { public: int num; // number of team int cnt; // solved problems int tim; // total consumed time team(void){}; team(int _num, int _cnt, int _tim) : num(_num), cnt(_cnt), tim(_tim){}; friend bool operator==(const team &a, const team &b) { return a.cnt == b.cnt && a.tim == b.tim; } friend bool operator<(const team &a, const team &b) { if (a.cnt != b.cnt) return a.cnt > b.cnt; else if (a.tim != b.tim) return a.tim < b.tim; else return a.num < b.num; } }; class record { public: int tim; // elapsed time int num; // number of team int pro; // problem number int jud; // judgement of submission record(void){}; record(int _tim, int _num, int _pro, int _jud) : tim(_tim), num(_num), pro(_pro), jud(_jud){}; }; team tm[55]; record rc[2005]; bitset<15> sl[55]; // if team i solved problem j signed main(void) { int m, t, p, r; while (cin >> m >> t >> p >> r, m || t || p || r) { for (int i = 1; i <= p; ++i) sl[i].reset(); for (int i = 1; i <= r; ++i) { int ti, ni, pi, ji; cin >> ti >> ni >> pi >> ji; rc[i] = record(ti, ni, pi, ji); if (ji == 0) sl[ni].set(pi); // team ni solved pi } for (int i = 1; i <= t; ++i) tm[i] = team(i, sl[i].count(), 0); for (int i = 1; i <= r; ++i) { int ti = rc[i].tim; int ni = rc[i].num; int pi = rc[i].pro; int ji = rc[i].jud; if (ji == 0) tm[ni].tim += ti; else if (sl[ni][pi]) tm[ni].tim += 20; } sort(tm + 1, tm + 1 + t); int stk[55], tot = 0; for (int i = 1, j = 1; i <= t; i = j) { while (j <= t && tm[i] == tm[j]) stk[++tot] = j++; cout << tm[stk[tot--]].num; while (tot) cout << "=" << tm[stk[tot--]].num; if (j <= t) cout << ","; else cout << endl; } } // system("pause"); }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; class team { public: int num; // number of team int cnt; // solved problems int tim; // total consumed time team(void){}; team(int _num, int _cnt, int _tim) : num(_num), cnt(_cnt), tim(_tim){}; friend bool operator==(const team &a, const team &b) { return a.cnt == b.cnt && a.tim == b.tim; } friend bool operator<(const team &a, const team &b) { if (a.cnt != b.cnt) return a.cnt > b.cnt; else if (a.tim != b.tim) return a.tim < b.tim; else return a.num < b.num; } }; class record { public: int tim; // elapsed time int num; // number of team int pro; // problem number int jud; // judgement of submission record(void){}; record(int _tim, int _num, int _pro, int _jud) : tim(_tim), num(_num), pro(_pro), jud(_jud){}; }; team tm[55]; record rc[2005]; bitset<15> sl[55]; // if team i solved problem j signed main(void) { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int m, t, p, r; while (cin >> m >> t >> p >> r, m || t || p || r) { for (int i = 1; i <= t; ++i) sl[i].reset(); for (int i = 1; i <= r; ++i) { int ti, ni, pi, ji; cin >> ti >> ni >> pi >> ji; rc[i] = record(ti, ni, pi, ji); if (ji == 0) sl[ni].set(pi); // team ni solved pi } for (int i = 1; i <= t; ++i) tm[i] = team(i, sl[i].count(), 0); for (int i = 1; i <= r; ++i) { int ti = rc[i].tim; int ni = rc[i].num; int pi = rc[i].pro; int ji = rc[i].jud; if (ji == 0) tm[ni].tim += ti; else if (sl[ni][pi]) tm[ni].tim += 20; } sort(tm + 1, tm + 1 + t); int stk[55], tot = 0; for (int i = 1, j = 1; i <= t; i = j) { while (j <= t && tm[i] == tm[j]) stk[++tot] = j++; cout << tm[stk[tot--]].num; while (tot) cout << "=" << tm[stk[tot--]].num; if (j <= t) cout << ","; else cout << endl; } } // system("pause"); }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22]]
1
638
#include <stdio.h> #include <string.h> typedef struct { int id; int ac; int time; int pt[300]; int ok[20]; } Player; int M, T, P, R; void makeScore(int n, Player *A) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= P; j++) { if (A[i].ok[j] == 0) { if (A[i].pt[j]) A[i].time += (20 * A[i].pt[j]); } } } } void makeRank(int n, Player *A) { int i, j; Player temp; for (int i = 0; i < n; i++) for (int j = n; j > i + 1; j--) { if (A[j].ac > A[j - 1].ac) { temp = A[j]; A[j] = A[j - 1]; A[j - 1] = temp; } else if (A[j].ac == A[j - 1].ac) { if (A[j].time < A[j - 1].time) { temp = A[j]; A[j] = A[j - 1]; A[j - 1] = temp; } else if (A[j].time == A[j - 1].time) { if (A[j].id > A[j - 1].id) { temp = A[j]; A[j] = A[j - 1]; A[j - 1] = temp; } } } } } void Print(int n, Player *A) { int i; for (i = 1; i <= n; i++) { printf("%d", A[i].id); if (A[i].ac == A[i + 1].ac && A[i].time == A[i + 1].time && i != n) printf("="); else if (i != n) printf(","); } printf("\n"); } int main() { int i; int time, team, num, ok; Player plr[100]; while (scanf("%d%d%d%d", &M, &T, &P, &R) && M || T || R || P) { for (int i = T; i >= 1; i--) { memset(plr[i].pt, 0, sizeof(plr[i].pt)); memset(plr[i].ok, -1, sizeof(plr[i].ok)); plr[i].ac = plr[i].time = 0; plr[i].id = i; } for (i = 0; i < R; i++) { scanf("%d%d%d%d", &time, &team, &num, &ok); if (ok == 0) { plr[team].ac++; plr[team].time = time; plr[team].ok[num] = 0; } else { plr[team].pt[num]++; } } makeScore(T, plr); makeRank(T, plr); Print(T, plr); } }
#include <stdio.h> #include <string.h> typedef struct { int id; int ac; int time; int pt[300]; int ok[20]; } Player; int M, T, P, R; void makeScore(int n, Player *A) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= P; j++) { if (A[i].ok[j] == 0) { if (A[i].pt[j]) A[i].time += (20 * A[i].pt[j]); } } } } void makeRank(int n, Player *A) { int i, j; Player temp; for (int i = 0; i < n; i++) for (int j = n; j > i + 1; j--) { if (A[j].ac > A[j - 1].ac) { temp = A[j]; A[j] = A[j - 1]; A[j - 1] = temp; } else if (A[j].ac == A[j - 1].ac) { if (A[j].time < A[j - 1].time) { temp = A[j]; A[j] = A[j - 1]; A[j - 1] = temp; } else if (A[j].time == A[j - 1].time) { if (A[j].id > A[j - 1].id) { temp = A[j]; A[j] = A[j - 1]; A[j - 1] = temp; } } } } } void Print(int n, Player *A) { int i; for (i = 1; i <= n; i++) { printf("%d", A[i].id); if (A[i].ac == A[i + 1].ac && A[i].time == A[i + 1].time && i != n) printf("="); else if (i != n) printf(","); } printf("\n"); } int main() { int i; int time, team, num, ok; Player plr[100]; while (scanf("%d%d%d%d", &M, &T, &P, &R) && M || T || R || P) { for (int i = T; i >= 1; i--) { memset(plr[i].pt, 0, sizeof(plr[i].pt)); memset(plr[i].ok, -1, sizeof(plr[i].ok)); plr[i].ac = plr[i].time = 0; plr[i].id = i; } for (i = 0; i < R; i++) { scanf("%d%d%d%d", &time, &team, &num, &ok); if (ok == 0) { plr[team].ac++; plr[team].time += time; plr[team].ok[num] = 0; } else { plr[team].pt[num]++; } } makeScore(T, plr); makeRank(T, plr); Print(T, plr); } }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 107]]
1
727
#include <iostream> using namespace std; int main() { while (true) { int M, T, P, R; cin >> M >> T >> P >> R; if (M == 0 && T == 0 && P == 0 && R == 0) break; int CP[T], TT[T], PT[T][P]; //正解数、時間計、ペナルティ for (int i = 0; i < T; i++) { CP[i] = TT[i] = 0; for (int j = 0; j < P; j++) { PT[i][j] = 0; } } for (int i = 0; i < R; i++) { //各チームの正解数、時間計を求める int m, t, p, j; cin >> m >> t >> p >> j; if (j == 0) { //正解 CP[t - 1]++; TT[t - 1] += (m + PT[t - 1][p - 1]); } else { //不正解 PT[t - 1][p - 1] += 20; } } int t_now, t_pre = -1; //今のチーム、1つ前のチーム bool flag[50]; //処理済みフラグ for (int i = 0; i < T; i++) { flag[i] = false; } for (int i = 0; i < T; i++) { t_now = -1; for (int j = 0; j < T; j++) { //今のチームを求める if (flag[j]) continue; if (t_now == -1) { t_now = j; } else { if (CP[j] > CP[t_now] || (CP[j] == CP[t_now] && TT[j] <= TT[t_now])) { t_now = j; } } } if (t_pre != -1) { if (CP[t_now] == CP[t_pre] && TT[t_now] == TT[t_pre]) { cout << '='; } else { cout << ', '; } } cout << t_now + 1; flag[t_now] = true; t_pre = t_now; } cout << endl; } return 0; }
#include <iostream> using namespace std; int main() { while (true) { int M, T, P, R; cin >> M >> T >> P >> R; if (M == 0 && T == 0 && P == 0 && R == 0) break; int CP[T], TT[T], PT[T][P]; //正解数、時間計、ペナルティ for (int i = 0; i < T; i++) { CP[i] = TT[i] = 0; for (int j = 0; j < P; j++) { PT[i][j] = 0; } } for (int i = 0; i < R; i++) { //各チームの正解数、時間計を求める int m, t, p, j; cin >> m >> t >> p >> j; if (j == 0) { //正解 CP[t - 1]++; TT[t - 1] += (m + PT[t - 1][p - 1]); } else { //不正解 PT[t - 1][p - 1] += 20; } } int t_now, t_pre = -1; //今のチーム、1つ前のチーム bool flag[50]; //処理済みフラグ for (int i = 0; i < T; i++) { flag[i] = false; } for (int i = 0; i < T; i++) { t_now = -1; for (int j = 0; j < T; j++) { //今のチームを求める if (flag[j]) continue; if (t_now == -1) { t_now = j; } else { if (CP[j] > CP[t_now] || (CP[j] == CP[t_now] && TT[j] <= TT[t_now])) { t_now = j; } } } if (t_pre != -1) { if (CP[t_now] == CP[t_pre] && TT[t_now] == TT[t_pre]) { cout << '='; } else { cout << ','; } } cout << t_now + 1; flag[t_now] = true; t_pre = t_now; } cout << endl; } return 0; }
[]
1
435
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() using namespace std; typedef long long ll; typedef pair<int, int> P; const double EPS = 1e-10; const ll INF = 1000000000; const ll MAX = 100010; int main() { int M, T, P, R; while (cin >> M >> T >> P >> R, M + T + P + R) { pair<pair<int, int>, int> v[60] = {}; int wa[60][11] = {}; int pn[60] = {}; for (int i = 0; i < R; i++) { int m, t, p, j; cin >> m >> t >> p >> j; t--; if (j) { wa[t][p]++; continue; } pn[t] += wa[t][p] * 20; v[t].first.first--; v[t].first.second = m; } for (int i = 0; i < T; i++) { v[i].first.second += pn[i]; v[i].second = -(i + 1); } sort(v, v + T); for (int i = 0; i < T; i++) { cout << -v[i].second; if (i == T - 1) { cout << endl; continue; } if (v[i].first == v[i + 1].first) { cout << "="; } else { cout << ","; } } } }
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() using namespace std; typedef long long ll; typedef pair<int, int> P; const double EPS = 1e-10; const ll INF = 1000000000; const ll MAX = 100010; int main() { int M, T, P, R; while (cin >> M >> T >> P >> R, M + T + P + R) { pair<pair<int, int>, int> v[60] = {}; int wa[60][11] = {}; int pn[60] = {}; for (int i = 0; i < R; i++) { int m, t, p, j; cin >> m >> t >> p >> j; t--; if (j != 0) { wa[t][p]++; continue; } pn[t] += wa[t][p] * 20; v[t].first.first--; v[t].first.second += m; } for (int i = 0; i < T; i++) { v[i].first.second += pn[i]; v[i].second = -(i + 1); } sort(v, v + T); for (int i = 0; i < T; i++) { cout << -v[i].second; if (i == T - 1) { cout << endl; continue; } if (v[i].first == v[i + 1].first) { cout << "="; } else { cout << ","; } } } }
[["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 79], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 107]]
1
342
#include <algorithm> #include <cstdio> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) typedef pair<int, int> pii; typedef pair<pii, int> piii; piii mk(int a, int b, int c) { return piii(pii(a, b), c); } int main() { A:; int teemwrong[55][11] = {0}; int teemcost[11] = {0}; int teemac[11] = {0}; int M, T, P, R; cin >> M >> T >> P >> R; if (M == 0 && T == 0 && R == 0 && P == 0) return 0; rep(i, R) { int m, t, p, j; cin >> m >> t >> p >> j; if (j == 0) { teemac[t]++; teemcost[t] += m + teemwrong[t][p] * 20; } else { teemwrong[t][p]++; } } vector<piii> rank; reps(i, 1, T + 1) { rank.push_back(mk(teemac[i], -teemcost[i], i)); } sort(rank.begin(), rank.end()); reverse(rank.begin(), rank.end()); rep(i, rank.size()) { if (i != 0) { if (rank[i].first == rank[i - 1].first) printf("="); else printf(","); } printf("%d", rank[i].second); } puts(""); goto A; }
#include <algorithm> #include <cstdio> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) typedef pair<int, int> pii; typedef pair<pii, int> piii; piii mk(int a, int b, int c) { return piii(pii(a, b), c); } int main() { A:; int teemwrong[55][11] = {0}; int teemcost[55] = {0}; int teemac[55] = {0}; int M, T, P, R; cin >> M >> T >> P >> R; if (M == 0 && T == 0 && R == 0 && P == 0) return 0; rep(i, R) { int m, t, p, j; cin >> m >> t >> p >> j; if (j == 0) { teemac[t]++; teemcost[t] += m + teemwrong[t][p] * 20; } else { teemwrong[t][p]++; } } vector<piii> rank; reps(i, 1, T + 1) { rank.push_back(mk(teemac[i], -teemcost[i], i)); } sort(rank.begin(), rank.end()); reverse(rank.begin(), rank.end()); rep(i, rank.size()) { if (i != 0) { if (rank[i].first == rank[i - 1].first) printf("="); else printf(","); } printf("%d", rank[i].second); } puts(""); goto A; }
[["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13]]
1
378
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int M, T, P, R; int m[2000], t[2000], p[2000], j[2000]; struct team { int n, s, t, w[2000]; }; bool comp(const team &t1, const team &t2) { if (t1.t == t2.t && t1.s == t2.s) return t1.n > t2.n; if (t1.s == t2.s) return t1.t < t2.t; return t1.s > t2.s; }; team dat[2001]; int main(void) { while (1) { scanf("%d%d%d%d", &M, &T, &P, &R); if (M + T + R + P == 0) break; memset(dat, 0, sizeof(dat)); for (int i = 0; i < T; i++) dat[i].n = i + 1; for (int i = 0; i < R; i++) { scanf("%d%d%d%d", &m[i], &t[i], &p[i], &j[i]); if (j[i] == 1) { dat[t[i] - 1].w[p[i]]++; } else { dat[t[i] - 1].s++; dat[t[i] - 1].t += m[i] + dat[t[i] - 1].w[p[i]] * 20; } } sort(dat, dat + T, comp); printf("%d", dat[0].n); for (int i = 1; i < T; i++) { if (dat[i - 1].s == dat[i].s && dat[i - 1].t == dat[i].t) printf("="); else printf(","); printf("%d", dat[i].n); } printf("\n"); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int M, T, P, R; int m[2000], t[2000], p[2000], j[2000]; struct team { int n, s, t, w[2000]; }; bool comp(const team &t1, const team &t2) { if (t1.t == t2.t && t1.s == t2.s) return t1.n > t2.n; if (t1.s == t2.s) return t1.t < t2.t; return t1.s > t2.s; }; team dat[2001]; int main(void) { while (1) { scanf("%d%d%d%d", &M, &T, &P, &R); if (M + T + R + P == 0) break; memset(dat, 0, sizeof(dat)); for (int i = 0; i < T; i++) dat[i].n = i + 1; for (int i = 0; i < R; i++) { scanf("%d%d%d%d", &m[i], &t[i], &p[i], &j[i]); if (j[i] != 0) { dat[t[i] - 1].w[p[i]]++; } else { dat[t[i] - 1].s++; dat[t[i] - 1].t += m[i] + dat[t[i] - 1].w[p[i]] * 20; } } sort(dat, dat + T, comp); printf("%d", dat[0].n); for (int i = 1; i < T; i++) { if (dat[i - 1].s == dat[i].s && dat[i - 1].t == dat[i].t) printf("="); else printf(","); printf("%d", dat[i].n); } printf("\n"); } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 79], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
463
class Array def sb sort_by.with_index{ |e, index| [yield(e), index] } end end while true do mm, tt, pp, rr = gets.split.map(&:to_i) break if mm + tt + pp + rr == 0 wa = Array.new(tt, Array.new(pp, 0)) solved = Array.new(tt, 0) time = Array.new(tt, 0) rr.times do m, t, p, j = gets.split.map(&:to_i) t -= 1 p -= 1 if j == 0 solved[t] += 1 time[t] += m + wa[t][p] * 20 else wa[t][p] += 1 end end teams = (0...tt).to_a.reverse result = teams.sb{|x| time[x]}.sb{|x| - solved[x]} tt.times do |i| t = result[i] if i != 0 s = result[i-1] print time[s] == time[t] && solved[s] == solved[t] ? '=' : ',' end print t + 1 end puts end
class Array def sb sort_by.with_index{ |e, index| [yield(e), index] } end end while true do mm, tt, pp, rr = gets.split.map(&:to_i) break if mm + tt + pp + rr == 0 wa = Array.new(tt).map{Array.new(pp,0)} solved = Array.new(tt, 0) time = Array.new(tt, 0) rr.times do m, t, p, j = gets.split.map(&:to_i) t -= 1 p -= 1 if j == 0 solved[t] += 1 time[t] += m + wa[t][p] * 20 else wa[t][p] += 1 end end teams = (0...tt).to_a.reverse result = teams.sb{|x| time[x]}.sb{|x| - solved[x]} tt.times do |i| t = result[i] if i != 0 s = result[i-1] print time[s] == time[t] && solved[s] == solved[t] ? '=' : ',' end print t + 1 end puts end
[["-", 8, 170, 0, 662, 12, 652, 3, 4, 0, 21], ["+", 0, 662, 12, 652, 486, 652, 3, 4, 0, 25], ["+", 0, 89, 8, 170, 0, 662, 12, 652, 17, 131], ["+", 0, 89, 8, 170, 0, 662, 12, 652, 735, 22], ["+", 8, 170, 0, 662, 12, 652, 196, 196, 0, 45], ["-", 8, 170, 0, 662, 12, 652, 3, 4, 0, 25], ["+", 8, 170, 0, 662, 12, 652, 196, 196, 0, 46]]
4
255
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1187&lang=jp """ import sys from sys import stdin input = stdin.readline class Team(object): def __init__(self, id): self.id = id self.correct = 0 # ??£?§£??° self.time = 0 # ???????????? self.penalty = 0 # ??????????????£ self.status = {} # ?????£?§£?????¶??? key: ????????????, value: ????????£????????° def judge(self, m, p, j): if j == 0: self.correct += 1 self.time = m if p in self.status: self.penalty += 20 * self.status[p] del self.status[p] else: if p in self.status: self.status[p] += 1 else: self.status[p] = 1 def calc_result(scores): # ???????????????????????¢????????§?????????????????£?§£??°?????????????????????????????????????????§???????????????????????????l result = [] # ??????????????¨??????[??£?§£??°*(-1), ????????????, ???????????????*(-1)] ??????????????§?¨?????????? for i, s in enumerate(scores[1:], start=1): if s is not None: result.append([-s.correct, s.time+s.penalty, s.id*-1]) # sort()??????????????????????????£????????????????????????????????£?§£??°??¨??????????????????-1??????????????? else: result.append([0, 0, -i]) result.sort() return result def main(args): # M, T, P, R = 300, 10, 8, 5, # data = [[50, 5, 2, 1,], [70, 5, 2, 0], [75, 1, 1, 0], [100, 3, 1, 0], [150, 3, 2, 0]] while True: M, T, P, R = map(int, input().split()) if M == 0 and T == 0 and P == 0 and R == 0: break data = [[int(i) for i in input().split()] for x in range(R)] scores = [None] * (T+1) for d in data: m, t, p, j = d if scores[t] == None: team = Team(t) scores[t] = team scores[t].judge(m, p, j) # ??????????????? result = calc_result(scores) # ????????????????????????????????? prev_corrent = result[0][0] prev_time = result[0][1] final_output = str(result[0][2]*-1) for r in result[1:]: if r[0] == prev_corrent and r[1] == prev_time: final_output += '=' final_output += str(r[2]*-1) else: final_output += ',' final_output += str(r[2]*-1) prev_corrent = r[0] prev_time = r[1] print(final_output) if __name__ == '__main__': main(sys.argv[1:])
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1187&lang=jp """ import sys from sys import stdin input = stdin.readline class Team(object): def __init__(self, id): self.id = id self.correct = 0 # ??£?§£??° self.time = 0 # ???????????? self.penalty = 0 # ??????????????£ self.status = {} # ?????£?§£?????¶??? key: ????????????, value: ????????£????????° def judge(self, m, p, j): if j == 0: self.correct += 1 self.time += m if p in self.status: self.penalty += 20 * self.status[p] del self.status[p] else: if p in self.status: self.status[p] += 1 else: self.status[p] = 1 def calc_result(scores): # ???????????????????????¢????????§?????????????????£?§£??°?????????????????????????????????????????§???????????????????????????l result = [] # ??????????????¨??????[??£?§£??°*(-1), ????????????, ???????????????*(-1)] ??????????????§?¨?????????? for i, s in enumerate(scores[1:], start=1): if s is not None: result.append([-s.correct, s.time+s.penalty, s.id*-1]) # sort()??????????????????????????£????????????????????????????????£?§£??°??¨??????????????????-1??????????????? else: result.append([0, 0, -i]) result.sort() return result def main(args): # M, T, P, R = 300, 10, 8, 5, # data = [[50, 5, 2, 1,], [70, 5, 2, 0], [75, 1, 1, 0], [100, 3, 1, 0], [150, 3, 2, 0]] while True: M, T, P, R = map(int, input().split()) if M == 0 and T == 0 and P == 0 and R == 0: break data = [[int(i) for i in input().split()] for x in range(R)] scores = [None] * (T+1) for d in data: m, t, p, j = d if scores[t] == None: team = Team(t) scores[t] = team scores[t].judge(m, p, j) # ??????????????? result = calc_result(scores) # ????????????????????????????????? prev_corrent = result[0][0] prev_time = result[0][1] final_output = str(result[0][2]*-1) for r in result[1:]: if r[0] == prev_corrent and r[1] == prev_time: final_output += '=' final_output += str(r[2]*-1) else: final_output += ',' final_output += str(r[2]*-1) prev_corrent = r[0] prev_time = r[1] print(final_output) if __name__ == '__main__': main(sys.argv[1:])
[["-", 0, 57, 64, 196, 0, 1, 0, 662, 0, 32], ["+", 0, 57, 64, 196, 0, 1, 0, 677, 17, 107]]
5
492