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 <iomanip> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) const int N = 10, ONE_KUURU = 12; vector<double> E; void init() { E.assign(N + 1, 0.0); vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1)); dp[1][0] = 1.0; for (int i = 1; i <= N; ++i) { E[i] += E[i - 1]; rep(j, ONE_KUURU) { dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j)); dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j)); E[i] += dp[i][j] * (1.0 / (1 << j)); } } } int main() { init(); for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\n') ; return 0; }
#include <iomanip> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) const int N = 100000, ONE_KUURU = 12; vector<double> E; void init() { E.assign(N + 1, 0.0); vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1)); dp[1][0] = 1.0; for (int i = 1; i <= N; ++i) { E[i] += E[i - 1]; rep(j, ONE_KUURU) { dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j)); dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j)); E[i] += dp[i][j] * (1.0 / (1 << j)); } } } int main() { init(); for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\n') ; return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13]]
1
247
#include <cstdio> #include <iostream> using namespace std; #define NMAX 100000 double dp[NMAX + 1]; int main() { int n = NMAX; for (int i = 0; i < n + 1; i++) { dp[i] = 0.0; } for (int i = 0; i < n; i++) { long long int foo = 1; int bar = 1; for (int j = i + 1; j < n + 1 and j < i + 40; j++) { dp[j] += (1.0 - dp[i]) / foo; bar *= 2; foo *= bar; } } while (cin >> n and n > 0) { double ans = 0.0; for (int i = 1; i < n + 1; i++) { ans += dp[i]; } printf("%.8f\n", ans); } return 0; }
#include <cstdio> #include <iostream> using namespace std; #define NMAX 100000 double dp[NMAX + 1]; int main() { int n = NMAX; for (int i = 0; i < n + 1; i++) { dp[i] = 0.0; } for (int i = 0; i < n; i++) { long long int foo = 1; long long int bar = 1; for (int j = i + 1; j < n + 1 and foo < 1e08; j++) { dp[j] += (1.0 - dp[i]) / foo; bar *= 2; foo *= bar; } } while (cin >> n and n > 0) { double ans = 0.0; for (int i = 1; i < n + 1; i++) { ans += dp[i]; } printf("%.8f\n", ans); } return 0; }
[["+", 0, 7, 8, 9, 0, 43, 39, 86, 0, 96], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 31, 22], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 31, 22], ["-", 0, 7, 15, 16, 12, 16, 12, 16, 31, 22], ["-", 0, 7, 15, 16, 12, 16, 12, 16, 17, 72], ["-", 0, 7, 15, 16, 12, 16, 12, 16, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
188
#include <bits/stdc++.h> #define N 100010 using namespace std; bool mem[N][50]; int used[N][50]; double dfs(int day, int cnt, double p) { if (day == 0) return 0; if (cnt >= 50) return 0; if (used[day][cnt]) return mem[day][cnt]; used[day][cnt] = 1; double res = 0; res += p * (1 + dfs(day - 1, cnt + 1, p / 2)); if (cnt) res += (1 - p) * dfs(day - 1, 0, 1); return mem[day][cnt] = res; } int main() { int n; while (cin >> n, n) printf("%f\n", dfs(n, 0, 1)); return 0; }
#include <bits/stdc++.h> #define N 100010 using namespace std; double mem[N][50]; bool used[N][50]; double dfs(int day, int cnt, double p) { if (day == 0) return 0; if (cnt >= 50) return 0; if (used[day][cnt]) return mem[day][cnt]; used[day][cnt] = 1; double res = 0; res += p * (1 + dfs(day - 1, cnt + 1, p / 2)); if (cnt) res += (1 - p) * dfs(day - 1, 0, 1); return mem[day][cnt] = res; } int main() { int n; while (cin >> n, n) printf("%f\n", dfs(n, 0, 1)); return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40]]
1
187
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> #define _USE_MATH_DEFINES using namespace std; typedef unsigned long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<string, map<string, int>> Ps; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 30; const double EPS = 1e-9; double getb[200000]; double dp[100001][30]; int main() { int n; fill(&dp[0][0], &dp[199999][49] + 1, 0.0); dp[1][1] = 1.0; for (int i = 1; i < 100000; i++) { for (int j = 0; j < 29; j++) { dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j); dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j)); } } getb[0] = 0; for (int i = 1; i <= 100000; i++) { getb[i] = 0.0; for (int j = 1; j < 30; j++) { getb[i] += dp[i][j]; } getb[i] += getb[i - 1]; } while (cin >> n && n) { cout << fixed << setprecision(9) << getb[n] << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> #define _USE_MATH_DEFINES using namespace std; typedef unsigned long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<string, map<string, int>> Ps; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 30; const double EPS = 1e-9; double getb[200000]; double dp[100001][30]; int main() { int n; fill(&dp[0][0], &dp[100000][29] + 1, 0.0); dp[1][1] = 1.0; for (int i = 1; i < 100000; i++) { for (int j = 0; j < 29; j++) { dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j); dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j)); } } getb[0] = 0; for (int i = 1; i <= 100000; i++) { getb[i] = 0.0; for (int j = 1; j < 30; j++) { getb[i] += dp[i][j]; } getb[i] += getb[i - 1]; } while (cin >> n && n) { cout << fixed << setprecision(9) << getb[n] << endl; } return 0; }
[["-", 31, 66, 28, 69, 28, 69, 341, 342, 0, 13], ["+", 31, 66, 28, 69, 28, 69, 341, 342, 0, 13], ["-", 0, 16, 31, 66, 28, 69, 341, 342, 0, 13], ["+", 0, 16, 31, 66, 28, 69, 341, 342, 0, 13]]
1
363
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; double dp[100001][21]; int main() { int n; while (~scanf("%d", &n)) { if (n == 0) break; memset(dp, 0, sizeof(dp)); double exp = 0; dp[1][1] = 1.0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= 20; j++) { // dp[i][j] // i:day // j:success time exp += dp[i][j] * pow(0.5, j - 1); if (j == 1) { dp[i + 1][j + 1] += dp[j][j]; continue; } dp[i + 1][1] += dp[i][j] * (1.0 - pow(0.5, j - 1)); dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j - 1); } } printf("%.8lf\n", exp); } }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; double dp[100001][21]; int main() { int n; while (~scanf("%d", &n)) { if (n == 0) break; memset(dp, 0, sizeof(dp)); double exp = 0; dp[1][1] = 1.0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= 20; j++) { // dp[i][j] // i:day // j:success time exp += dp[i][j] * pow(0.5, j - 1); if (j == 1) { dp[i + 1][j + 1] += dp[i][j]; continue; } dp[i + 1][1] += dp[i][j] * (1.0 - pow(0.5, j - 1)); dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j - 1); } } printf("%.8lf\n", exp); } }
[["-", 0, 11, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 69, 28, 69, 341, 342, 0, 22]]
1
327
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main(void) { int n; double dp[100001] = {0}, p, w; dp[1] = 1.0; for (int i = 2; i < 100001; i++) { p = 1.0; w = 0.5; for (int j = 1; j < i && 0.0000000001 < p; j++) { dp[i] += p * (1 - w) * (j + dp[max(0, i - j - 1)]); p *= w; w /= 2; } dp[i] += i * p; } while (cin >> n, n) printf("%.3f\n", dp[n]); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main(void) { int n; double dp[100001] = {0}, p, w; dp[1] = 1.0; for (int i = 2; i < 100001; i++) { p = 1.0; w = 0.5; for (int j = 1; j < i && 0.00000000001 < p; j++) { dp[i] += p * (1 - w) * (j + dp[max(0, i - j - 1)]); p *= w; w /= 2; } dp[i] += i * p; } while (cin >> n, n) printf("%.8f\n", dp[n]); return 0; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 31, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 31, 13], ["-", 8, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 8, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
157
#include <cstdio> #include <cstring> #define EPS (1e-13) #define MAX(A, B) ((A) >= (B) ? (A) : (B)) double dp[1000001]; int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i <= 1000000; i++) dp[i] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) { double p = 1.0, win = 0.5; for (int j = 1; j < i && EPS < p; j++) { double Not = p * (1 - win); dp[i] += Not * (j + dp[MAX(0, n - 1 - j)]); p *= win; win /= 2; } dp[i] += p * i; } printf("%lf\n", dp[n]); } }
#include <cstdio> #include <cstring> #define EPS (1e-13) #define MAX(A, B) ((A) >= (B) ? (A) : (B)) double dp[1000001]; int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i <= 1000000; i++) dp[i] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) { double p = 1.0, win = 0.5; for (int j = 1; j < i && EPS < p; j++) { double Not = p * (1 - win); dp[i] += Not * (j + dp[MAX(0, i - j - 1)]); p *= win; win /= 2; } dp[i] += p * i; } printf("%lf\n", dp[n]); } }
[["-", 0, 2, 3, 4, 0, 16, 31, 16, 31, 22], ["-", 0, 2, 3, 4, 0, 16, 31, 16, 17, 33], ["-", 0, 2, 3, 4, 0, 16, 31, 16, 12, 13], ["+", 0, 2, 3, 4, 0, 16, 31, 16, 31, 22], ["+", 341, 342, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 341, 342, 0, 2, 3, 4, 0, 16, 12, 13]]
1
185
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; const int INF = 1 << 29; typedef pair<int, int> P; int n; const int MAX_N = 10001; const int MAX_A = 12; double dp[MAX_N + 1][MAX_A + 1] = {0}; //日にち、連続 double ans[MAX_N] = {0}; int main(void) { dp[0][0] = 1; double sum = 0; for (int i = 1; i < MAX_N; i++) { for (int j = 0; j < MAX_A; j++) { if (!dp[i][j]) continue; sum += dp[i][j] * 1.0 / (1 << j); dp[i + 1][j + 1] = dp[i][j] * 1.0 / (1 << j); dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j)); } ans[i] = sum; } while (cin >> n && n) { printf("%.8f\n", ans[n - 1]); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; const int INF = 1 << 29; typedef pair<int, int> P; int n; const int MAX_N = 100001; const int MAX_A = 10; double dp[MAX_N + 1][MAX_A + 1] = {0}; //日にち、連続 double ans[MAX_N] = {0}; int main(void) { dp[0][0] = 1; double sum = 0; for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_A; j++) { if (!dp[i][j]) continue; sum += dp[i][j] * 1.0 / (1 << j); dp[i + 1][j + 1] = dp[i][j] * 1.0 / (1 << j); dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j)); } ans[i] = sum; } while (cin >> n && n) { printf("%.8f\n", ans[n - 1]); } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
266
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; const int N = 1e5 + 10, N2 = 300; double dp[N][N2]; const double EPS = 1e-3; double p[N2]; double rec(int n, int i) { if (n == 0) return 0; double &res = dp[n][i]; if (res >= 0) return res; res = 0; if (i < N2 - 1) res += p[i] * (rec(n - 1, i + 1) + 1); res += (1 - p[i]) * (rec(n - 1, 0)); return res; } int main() { cout.setf(ios::fixed); cout.precision(10); p[0] = 1; rep(i, N2 - 1) { p[i + 1] = p[i] * .5; } rep(i, N) rep(j, N2) dp[i][j] = -1; rep(i, 10) rec(i * 10000, 0); int n; while (cin >> n) { cout << rec(n, 0) << endl; } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; const int N = 1e5 + 10, N2 = 50; double dp[N][N2]; const double EPS = 1e-3; double p[N2]; double rec(int n, int i) { if (n == 0) return 0; double &res = dp[n][i]; if (res >= 0) return res; res = 0; if (i < N2 - 1) res += p[i] * (rec(n - 1, i + 1) + 1); res += (1 - p[i]) * (rec(n - 1, 0)); return res; } int main() { cout.setf(ios::fixed); cout.precision(10); p[0] = 1; rep(i, N2 - 1) { p[i + 1] = p[i] * .5; } rep(i, N) rep(j, N2) dp[i][j] = -1; rep(i, 10) rec(i * 10000, 0); int n; while (cin >> n, n) { cout << rec(n, 0) << endl; } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 15, 339, 51, 34, 31, 16, 12, 22], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21]]
1
350
dp = Array.new(100001,0.0) dp[1] = 1 for i in 2...100 p = 1.0 w = 0.5 k = 1 while k<i && p>1e-13 dp[i] += (p*(1-w))*(k+dp[[0,i-k-1].max]) p *= w w /= 2 k += 1 end dp[i] += i*p end while true n = gets.to_i if n == 0 break end puts dp[n] end
dp = Array.new(100001,0.0) dp[1] = 1 for i in 2..100000 p = 1.0 w = 0.5 k = 1 while k<i && p>1e-13 dp[i] += (p*(1-w))*(k+dp[[0,i-k-1].max]) p *= w w /= 2 k += 1 end dp[i] += i*p end while true n = gets.to_i if n == 0 break end puts dp[n] end
[["-", 0, 493, 0, 88, 51, 267, 0, 475, 17, 389], ["-", 0, 493, 0, 88, 51, 267, 0, 475, 444, 612], ["+", 0, 493, 0, 88, 51, 267, 0, 475, 17, 757], ["+", 0, 493, 0, 88, 51, 267, 0, 475, 444, 612]]
4
110
dp=[.0]*100001 dp[1]=1. for i in range(2,100001): a,b,j=.5,1,1 while j<i and b> 1e-5: dp[i]+=b*(1-a)*(j+dp[i-j-1]) b*=a;a/=2;j+=1 dp[i]+=i*b while 1: n=int(input()) if n==0:break print(dp[n])
dp=[.0]*100001 dp[1]=1. for i in range(2,100001): a,b,j=.5,1,1 while j<i and b>1e-15: dp[i]+=b*(1-a)*(j+dp[i-j-1]) b*=a;a/=2;j+=1 dp[i]+=i*b while 1: n=int(input()) if n==0:break print(dp[n])
[["-", 8, 196, 0, 52, 15, 679, 12, 666, 0, 531], ["+", 8, 196, 0, 52, 15, 679, 12, 666, 0, 531]]
5
111
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define f first #define s second #define mp make_pair #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define rep(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(c) (c).begin(), (c).end() #define IN(x, s, g) ((x) >= (s) && (x) < (g)) #define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h))) #define print(x) printf("%d\n", x) using namespace std; typedef unsigned int uint; typedef long long ll; const int _dx[] = {0, 1, 0, -1}; const int _dy[] = {-1, 0, 1, 0}; int getInt() { int ret = 0, c; c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) { ret *= 10; ret += c - '0'; c = getchar(); } return ret; } enum direction { UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3 }; /* * 6 * 2 * 3 1 4 * 5 */ /* top and RIGHT */ int next_top[7][7][4]; int next_left[7][7][4]; int front[7][7]; void initDie() { front[1][3] = 2; front[1][2] = 4; front[1][4] = 5; front[1][5] = 3; front[2][1] = 3; front[2][3] = 6; front[2][4] = 1; front[2][6] = 4; front[3][1] = 5; front[3][2] = 1; front[3][5] = 6; front[3][6] = 2; for (int i = 4; i < 7; i++) { for (int j = 1; j < 7; j++) { if (i == j || i + j == 7) continue; int t = 7 - i; int l = 7 - j; front[i][j] = front[t][l]; } } for (int t = 1; t < 7; t++) { for (int l = 1; l < 7; l++) { if (t == l || t + l == 7) continue; next_top[t][l][UP] = 7 - front[t][l]; next_top[t][l][LEFT] = 7 - l; next_top[t][l][RIGHT] = l; next_top[t][l][DOWN] = front[t][l]; next_left[t][l][UP] = l; next_left[t][l][LEFT] = t; next_left[t][l][RIGHT] = 7 - t; next_left[t][l][DOWN] = l; } } } int b[10][10]; int memo[10][10][7][7]; int main() { int h, w; initDie(); while ((h = getInt()) + (w = getInt())) { REP(i, h) REP(j, w) b[i][j] = getInt(); int sx = getInt(); int sy = getInt(); int gy = getInt(); int gx = getInt(); typedef pair<int, pair<pair<int, int>, pair<int, int>>> data; priority_queue<data, vector<data>, greater<data>> pq; pq.push(mp(0, mp(mp(sx, sy), mp(1, 4)))); memset(memo, -1, sizeof(memo)); while (pq.size()) { data d = pq.top(); pq.pop(); int cost = d.f; int x = d.s.f.f; int y = d.s.f.s; int t = d.s.s.f; int l = d.s.s.s; // printf("cost = %d, (%d,%d) (%d,%d)\n",cost,x,y,t,l); if (x == gx && y == gy) { print(cost); break; } if (memo[x][y][t][l] != -1) continue; memo[x][y][t][l] = cost; REP(i, 4) { int xx = x + _dx[i]; int yy = y + _dy[i]; if (!ISIN(xx, yy, w, h)) continue; int tt = next_top[t][l][i]; int ll = next_left[t][l][i]; int cc = cost + (7 - tt) * b[yy][xx]; if (memo[xx][yy][tt][ll] == -1) pq.push(mp(cc, mp(mp(xx, yy), mp(tt, ll)))); } } } return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define f first #define s second #define mp make_pair #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define rep(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(c) (c).begin(), (c).end() #define IN(x, s, g) ((x) >= (s) && (x) < (g)) #define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h))) #define print(x) printf("%d\n", x) using namespace std; typedef unsigned int uint; typedef long long ll; const int _dx[] = {0, 1, 0, -1}; const int _dy[] = {-1, 0, 1, 0}; int getInt() { int ret = 0, c; c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) { ret *= 10; ret += c - '0'; c = getchar(); } return ret; } enum direction { UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3 }; /* * 6 * 2 * 3 1 4 * 5 */ /* top and RIGHT */ int next_top[7][7][4]; int next_left[7][7][4]; int front[7][7]; void initDie() { front[1][3] = 2; front[1][2] = 4; front[1][4] = 5; front[1][5] = 3; front[2][1] = 3; front[2][3] = 6; front[2][4] = 1; front[2][6] = 4; front[3][1] = 5; front[3][2] = 1; front[3][5] = 6; front[3][6] = 2; for (int i = 4; i < 7; i++) { for (int j = 1; j < 7; j++) { if (i == j || i + j == 7) continue; int t = 7 - i; int l = 7 - j; front[i][j] = front[t][l]; } } for (int t = 1; t < 7; t++) { for (int l = 1; l < 7; l++) { if (t == l || t + l == 7) continue; next_top[t][l][UP] = 7 - front[t][l]; next_top[t][l][LEFT] = 7 - l; next_top[t][l][RIGHT] = l; next_top[t][l][DOWN] = front[t][l]; next_left[t][l][UP] = l; next_left[t][l][LEFT] = t; next_left[t][l][RIGHT] = 7 - t; next_left[t][l][DOWN] = l; } } } int b[10][10]; int memo[10][10][7][7]; int main() { int h, w; initDie(); while ((h = getInt()) + (w = getInt())) { REP(i, h) REP(j, w) b[i][j] = getInt(); int sy = getInt(); int sx = getInt(); int gy = getInt(); int gx = getInt(); typedef pair<int, pair<pair<int, int>, pair<int, int>>> data; priority_queue<data, vector<data>, greater<data>> pq; pq.push(mp(0, mp(mp(sx, sy), mp(1, 4)))); memset(memo, -1, sizeof(memo)); while (pq.size()) { data d = pq.top(); pq.pop(); int cost = d.f; int x = d.s.f.f; int y = d.s.f.s; int t = d.s.s.f; int l = d.s.s.s; // printf("cost = %d, (%d,%d) (%d,%d)\n",cost,x,y,t,l); if (x == gx && y == gy) { print(cost); break; } if (memo[x][y][t][l] != -1) continue; memo[x][y][t][l] = cost; REP(i, 4) { int xx = x + _dx[i]; int yy = y + _dy[i]; if (!ISIN(xx, yy, w, h)) continue; int tt = next_top[t][l][i]; int ll = next_left[t][l][i]; int cc = cost + (7 - tt) * b[yy][xx]; if (memo[xx][yy][tt][ll] == -1) pq.push(mp(cc, mp(mp(xx, yy), mp(tt, ll)))); } } } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 49, 22], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 49, 22]]
1
1,096
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1 << 24; const int INF = 1 << 28; int r, c; int w, h; int seg[8 * MAX_N]; void init() { for (w = 1; w < r; w <<= 1) ; for (h = 1; h < c; h <<= 1) ; fill_n(seg, 8 * MAX_N, INF); } void update(int x, int y, int v, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (x < ax || bx <= x) return; if (y < ay || by <= y) return; if (bx - ax == 1 && by - ay == 1) { seg[k] = v; return; } if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; if (nxt == 0) { int m = (ax + bx) / 2; update(x, y, v, k * 2 + 1, ax, ay, m, by, nxt ^ 1); update(x, y, v, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; update(x, y, v, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); update(x, y, v, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]); // printf("seg[%d] = %d\n", k, seg[k]); // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); } int getMin(int lx, int ly, int rx, int ry, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (rx <= ax || bx <= lx) return INF; if (ry <= ay || by <= ly) return INF; if (lx <= ax && bx <= rx && ly <= ay && by <= ry) return seg[k]; // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; int vl, vr; if (nxt == 0) { int m = (ax + bx) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, m, by, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } return min(vl, vr); } int main() { int q; while (scanf("%d %d %d", &r, &c, &q), r | c | q) { init(); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int v; scanf("%d", &v); update(i, j, v); } } while (q--) { int r1, c1, r2, c2; scanf("%d %d %d %d", &r1, &c1, &r2, &c2); printf("%d\n", getMin(r1, c1, ++r2, ++c2)); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1 << 20; const int INF = INT_MAX; int r, c; int w, h; int seg[8 * MAX_N]; void init() { for (w = 1; w < r; w <<= 1) ; for (h = 1; h < c; h <<= 1) ; fill_n(seg, 8 * MAX_N, INF); } void update(int x, int y, int v, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (x < ax || bx <= x) return; if (y < ay || by <= y) return; if (bx - ax == 1 && by - ay == 1) { seg[k] = v; return; } if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; if (nxt == 0) { int m = (ax + bx) / 2; update(x, y, v, k * 2 + 1, ax, ay, m, by, nxt ^ 1); update(x, y, v, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; update(x, y, v, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); update(x, y, v, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]); // printf("seg[%d] = %d\n", k, seg[k]); // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); } int getMin(int lx, int ly, int rx, int ry, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (rx <= ax || bx <= lx) return INF; if (ry <= ay || by <= ly) return INF; if (lx <= ax && bx <= rx && ly <= ay && by <= ry) return seg[k]; // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; int vl, vr; if (nxt == 0) { int m = (ax + bx) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, m, by, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } return min(vl, vr); } int main() { int q; while (scanf("%d %d %d", &r, &c, &q), r | c | q) { init(); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int v; scanf("%d", &v); update(i, j, v); } } while (q--) { int r1, c1, r2, c2; scanf("%d %d %d %d", &r1, &c1, &r2, &c2); printf("%d\n", getMin(r1, c1, ++r2, ++c2)); } } return 0; }
[["-", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13], ["-", 0, 30, 0, 43, 49, 50, 51, 16, 31, 13], ["-", 0, 30, 0, 43, 49, 50, 51, 16, 17, 151], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 22]]
1
847
#include <algorithm> #include <math.h> #include <stdio.h> using namespace std; long long b[110000]; long long gz[550]; double eps = 1e-9; int main() { int a; while (scanf("%d", &a), a) { long double A = 1; int sz = 0; for (int i = 0; i < (a + 1) * a / 2; i++) { scanf("%lld", b + i); if (b[i] % 2 == 0) { A *= pow(b[i], 1.0 / a); gz[sz++] = b[i]; } else A *= pow(b[i], -1.0 / a / (a - 1)); } long long gs = (long long)(A + eps); printf("%lld\n", gs); std::sort(gz, gz + a); for (int i = 0; i < a; i++) { if (i) printf(" "); printf("%lld", gz[i] / gs); } printf("\n"); } }
#include <algorithm> #include <math.h> #include <stdio.h> using namespace std; long long b[110000]; long long gz[550]; double eps = 1e-9; int main() { int a; while (scanf("%d", &a), a) { long double A = 1; int sz = 0; for (int i = 0; i < (a + 1) * a / 2; i++) { scanf("%lld", b + i); if (b[i] % 2 == 0) { A *= pow(b[i], 1.0 / a); gz[sz++] = b[i]; } else A *= pow(b[i], -1.0 / a / (a - 1)); } long long gs = (long long)(A + 0.5); printf("%lld\n", gs); std::sort(gz, gz + a); for (int i = 0; i < a; i++) { if (i) printf(" "); printf("%lld", gz[i] / gs); } printf("\n"); } }
[["-", 49, 50, 51, 74, 51, 23, 0, 16, 12, 22], ["+", 49, 50, 51, 74, 51, 23, 0, 16, 12, 13]]
1
242
#include <algorithm> #include <cmath> #include <cstdio> using namespace std; long long a[100000]; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int cmp(const long long &a, const long long &b) { if (a % 2 == 0 && b % 2 == 1) return 1; else if (a % 2 == 1 && b % 2 == 0) return 0; else return a < b; } int main() { int N, n; while (1) { long long x, y, z, tmp, r1, r2, d2, m, p, q; scanf("%d", &n); if (n == 0) break; N = (n + 1) * n / 2; for (int i = 1; i <= N; i++) scanf("%lld", a + i); sort(a + 1, a + N + 1, cmp); x = a[n + 1]; y = a[1]; z = a[2]; tmp = gcd(y, z); r1 = y / tmp; r2 = z / tmp; d2 = x / r1 / r2; p = 1; q = (1 << 31); while (p <= q) { m = (p + q) >> 1; if (m * m == d2) break; else if (m * m < d2) p = m + 1; else q = m - 1; } tmp /= m; printf("%lld\n", tmp); for (int i = 1; i <= n; i++) { if (i < n) printf("%lld ", a[i] / tmp); else printf("%lld\n", a[i] / tmp); } } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> using namespace std; long long a[100000]; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int cmp(const long long &a, const long long &b) { if (a % 2 == 0 && b % 2 == 1) return 1; else if (a % 2 == 1 && b % 2 == 0) return 0; else return a < b; } int main() { int N, n; while (1) { long long x, y, z, tmp, r1, r2, d2, m, p, q; scanf("%d", &n); if (n == 0) break; N = (n + 1) * n / 2; for (int i = 1; i <= N; i++) scanf("%lld", a + i); sort(a + 1, a + N + 1, cmp); x = a[n + 1]; y = a[1]; z = a[2]; tmp = gcd(y, z); r1 = y / tmp; r2 = z / tmp; d2 = x / r1 / r2; p = 1; q = (1 << 31) - 1; while (p <= q) { m = (p + q) >> 1; if (m * m == d2) break; else if (m * m < d2) p = m + 1; else q = m - 1; } tmp /= m; printf("%lld\n", tmp); for (int i = 1; i <= n; i++) { if (i < n) printf("%lld ", a[i] / tmp); else printf("%lld\n", a[i] / tmp); } } return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
395
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<string> vstring; typedef vector<pint> vpint; struct Edge { int to, from, cost; }; #ifdef DEBUG #define debug cout #else stringstream __ss__; #define debug __ss__ #endif template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } #define rep(i, n) for (int i = 0; i < (n); ++i) #define repd(i, n) for (int i = (n)-1; i >= 0; i--) #define repn(i, m, n) for (int i = (m); i <= (n); ++i) #define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--) #define rep0(i, n) for (i = 0; i < (n); ++i) #define all(n) n.begin(), n.end() #define sz(n) ((int)(n).size()) #define IL for (;;) #define MP make_pair #define PB push_back #define SS stringstream #define X second #define Y first #define PUTLINE debug << "LINE:" << __LINE__ << endl; const int INF = 2147483647 / 3; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0}; Int a[300]; Int gcd(Int x, Int y) { if (x < y) return gcd(y, x); if (y == 0) return x; return gcd(y, x % y); } Int sqrt(Int x, Int m, Int n) { if (m + 1 <= n) return m; Int l = (m + n) / 2; if (x >= l * l) return sqrt(x, l, n); else return sqrt(x, m, l); } int main() { int n; IL { cin >> n; if (n == 0) break; n = n * (n + 1) / 2; rep(i, n) cin >> a[i]; sort(a, a + n); Int x = -1, y = -1, z = -1; rep(i, n) { if (a[i] % 2 == 0) { if (x < 0) x = a[i]; else if (y < 0) y = a[i]; } else if (z < 0) { z = a[i]; } } // cout<<x<<" "<<y<<" "<<z<<endl; Int g = gcd(x, z); x /= g; z /= g; y /= z; Int w = x * y; w = sqrt(w, 0, 1LL << 31); cout << w << endl; bool first = true; rep(i, n) { if (a[i] % 2 == 0) { if (!first) cout << " "; first = false; cout << a[i] / w; } } cout << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<string> vstring; typedef vector<pint> vpint; struct Edge { int to, from, cost; }; #ifdef DEBUG #define debug cout #else stringstream __ss__; #define debug __ss__ #endif template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } #define rep(i, n) for (int i = 0; i < (n); ++i) #define repd(i, n) for (int i = (n)-1; i >= 0; i--) #define repn(i, m, n) for (int i = (m); i <= (n); ++i) #define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--) #define rep0(i, n) for (i = 0; i < (n); ++i) #define all(n) n.begin(), n.end() #define sz(n) ((int)(n).size()) #define IL for (;;) #define MP make_pair #define PB push_back #define SS stringstream #define X second #define Y first #define PUTLINE debug << "LINE:" << __LINE__ << endl; const int INF = 2147483647 / 3; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0}; Int a[1000000]; Int gcd(Int x, Int y) { if (x < y) return gcd(y, x); if (y == 0) return x; return gcd(y, x % y); } Int sqrt(Int x, Int m, Int n) { if (m + 1 == n) return m; Int l = (m + n) / 2; if (x >= l * l) return sqrt(x, l, n); else return sqrt(x, m, l); } int main() { int n; IL { cin >> n; if (n == 0) break; n = n * (n + 1) / 2; rep(i, n) cin >> a[i]; sort(a, a + n); Int x = -1, y = -1, z = -1; rep(i, n) { if (a[i] % 2 == 0) { if (x < 0) x = a[i]; else if (y < 0) y = a[i]; } else if (z < 0) { z = a[i]; } } // cout<<x<<" "<<y<<" "<<z<<endl; Int g = gcd(x, z); x /= g; z /= g; y /= z; Int w = x * y; w = sqrt(w, 0, 1LL << 31); cout << w << endl; bool first = true; rep(i, n) { if (a[i] % 2 == 0) { if (!first) cout << " "; first = false; cout << a[i] / w; } } cout << endl; } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
729
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (;;) { int r = in.nextInt(), c = in.nextInt(); if ((r | c) == 0) return; System.out.println(r * c % 2 == 0 ? "Yes" : "No"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (;;) { int r = in.nextInt(), c = in.nextInt(); if ((r | c) == 0) return; System.out.println(r * c % 2 == 0 ? "yes" : "no"); } } }
[["-", 0, 492, 3, 4, 0, 510, 64, 5, 0, 491], ["+", 0, 492, 3, 4, 0, 510, 64, 5, 0, 491], ["-", 0, 492, 3, 4, 0, 510, 75, 5, 0, 491], ["+", 0, 492, 3, 4, 0, 510, 75, 5, 0, 491]]
3
94
c; main(r) { for (; scanf("%d%d", &r, &c), r;) puts(r & c % 2 ? "no" : "yes"); }
c; main(r) { for (; scanf("%d%d", &r, &c), r;) puts((r & c) % 2 ? "no" : "yes"); }
[["+", 3, 4, 0, 41, 15, 16, 31, 23, 0, 24], ["+", 3, 4, 0, 41, 15, 16, 31, 23, 0, 25]]
0
44
#include <iostream> using namespace std; int main() { int r, c; while (cin >> r >> c && r) { cout << "yes\0no" + (r & 1 || c & 1) * 4 << endl; } return 0; }
#include <iostream> using namespace std; int main() { int r, c; while (cin >> r >> c && r) { cout << "yes\0no" + (r & 1 && c & 1) * 4 << endl; } return 0; }
[["-", 12, 16, 12, 16, 31, 23, 0, 16, 17, 106], ["+", 12, 16, 12, 16, 31, 23, 0, 16, 17, 98]]
1
54
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int a, b; while (cin >> a >> b, a | b) { if ((a & 1) && (b & 1)) cout << "no" << endl; cout << "yes" << endl; } }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int a, b; while (cin >> a >> b, a | b) { if ((a & 1) && (b & 1)) cout << "no" << endl; else cout << "yes" << endl; } }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
67
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef vector<ll> V; typedef complex<double> Point; #define PI acos(-1.0) #define EPS 1e-10 const ll INF = 1e9 + 7; const ll MOD = 1e9 + 7; #define FOR(i, a, b) for (long long i = (a); i < (b); i++) #define REP(i, N) for (long long i = 0; i < (N); i++) #define ALL(s) (s).begin(), (s).end() #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) #define fi first #define se second int r, c; int main() { while (cin >> r >> c && r + c) { if (r % 2 && c % 2) { cout << "yes" << endl; } else cout << "no" << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef vector<ll> V; typedef complex<double> Point; #define PI acos(-1.0) #define EPS 1e-10 const ll INF = 1e9 + 7; const ll MOD = 1e9 + 7; #define FOR(i, a, b) for (long long i = (a); i < (b); i++) #define REP(i, N) for (long long i = 0; i < (N); i++) #define ALL(s) (s).begin(), (s).end() #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) #define fi first #define se second int r, c; int main() { while (cin >> r >> c && r + c) { if (r % 2 && c % 2) { cout << "no" << endl; } else cout << "yes" << endl; } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
188
#include <cstdio> int main() { int r, c; while (~scanf("%d%d", &r, &c) && (r || c)) { if (r >= 2 && c >= 2) { puts("yes"); } else { puts("no"); } } return 0; }
#include <cstdio> int main() { int r, c; while (~scanf("%d%d", &r, &c) && (r || c)) { if (r % 2 && c % 2) { puts("no"); } else { puts("yes"); } } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 20], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 109], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 20], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 109], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
69
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define INF 1 << 30 #define F first #define S second using namespace std; typedef pair<int, int> P; int main() { int r, c; while (true) { cin >> r >> c; if (r + c == 0) break; r % 2 == 0 ? cout << "yes" << endl : cout << "no" << endl; } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define INF 1 << 30 #define F first #define S second using namespace std; typedef pair<int, int> P; int main() { int r, c; while (true) { cin >> r >> c; if (r + c == 0) break; (r * c) % 2 == 0 ? cout << "yes" << endl : cout << "no" << endl; } return 0; }
[["+", 0, 41, 15, 16, 31, 16, 31, 23, 0, 24], ["+", 15, 16, 31, 16, 31, 23, 0, 16, 17, 48], ["+", 15, 16, 31, 16, 31, 23, 0, 16, 12, 22], ["+", 0, 41, 15, 16, 31, 16, 31, 23, 0, 25]]
1
126
#include <cstdio> main() { int a, b; for (; scanf("%d%d", &a, &b), a + b;) { puts((a * b) % 2 ? "yes" : "no"); } }
#include <cstdio> main() { int a, b; for (; scanf("%d%d", &a, &b), a + b;) { puts(!((a * b) % 2) ? "yes" : "no"); } }
[["+", 0, 2, 3, 4, 0, 41, 15, 91, 17, 111], ["+", 15, 91, 28, 23, 0, 16, 31, 23, 0, 24], ["+", 3, 4, 0, 41, 15, 91, 28, 23, 0, 25]]
1
54
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repi(i, a, b) for (int i = (a); i < (b); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; int r, c; int main() { while (cin >> r >> c, r || c) { if (r % 2 && c && 2) { printf("no\n"); } else { printf("yes\n"); } } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repi(i, a, b) for (int i = (a); i < (b); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; int r, c; int main() { while (cin >> r >> c, r || c) { if (r % 2 && c % 2) { printf("no\n"); } else { printf("yes\n"); } } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 109]]
1
219
while True: r, c = list(map(int, input().split())) if r == 0: break print("yes" if r%2+c%2 == 0 else "no")
while True: r,c=list(map(int,input().split())) if r==0:break print("yes" if r*c%2==0 else "no")
[["-", 0, 41, 0, 666, 0, 657, 31, 657, 17, 109], ["-", 0, 41, 0, 666, 0, 657, 31, 657, 12, 612], ["-", 3, 4, 0, 41, 0, 666, 0, 657, 17, 72], ["+", 0, 41, 0, 666, 0, 657, 31, 657, 17, 48]]
5
48
import java.awt.geom.*; import java.io.*; import java.math.*; import java.util.*; public class Main { static int INF = 2 << 27; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; Data[] data = new Data[n]; int[] time = new int[31]; for (int i = 0; i < n; i++) { Data tmp = new Data(); tmp.name = sc.next(); tmp.m = sc.nextInt(); tmp.d = new int[tmp.m]; for (int j = 0; j < tmp.m; j++) { tmp.d[j] = sc.nextInt(); time[tmp.d[j]]++; } data[i] = tmp; } for (int i = 0; i < n; i++) { Data tmp = data[i]; for (int j = 0; j < tmp.m; j++) { tmp.point += Math.max(1, n - time[tmp.d[j]] + 1); } } Arrays.sort(data); System.out.println(data[data.length - 1].point + " " + data[data.length - 1].name); } } static class Data implements Comparable<Data> { String name; int m; int[] d = new int[m]; int point; @Override public int compareTo(Data o) { if (this.point == o.point) return this.name.compareTo(o.name); return o.point - this.point; } } }
import java.awt.geom.*; import java.io.*; import java.math.*; import java.util.*; public class Main { static int INF = 2 << 27; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; Data[] data = new Data[n]; int[] time = new int[31]; for (int i = 0; i < n; i++) { Data tmp = new Data(); tmp.name = sc.next(); tmp.m = sc.nextInt(); tmp.d = new int[tmp.m]; for (int j = 0; j < tmp.m; j++) { tmp.d[j] = sc.nextInt(); time[tmp.d[j]]++; } data[i] = tmp; } for (int i = 0; i < n; i++) { Data tmp = data[i]; for (int j = 0; j < tmp.m; j++) { tmp.point += Math.max(1, n - time[tmp.d[j]] + 1); } } Arrays.sort(data); System.out.println(data[data.length - 1].point + " " + data[data.length - 1].name); } } static class Data implements Comparable<Data> { String name; int m; int[] d = new int[m]; int point; @Override public int compareTo(Data o) { if (this.point == o.point) return o.name.compareTo(this.name); return o.point - this.point; } } }
[["-", 0, 57, 64, 37, 0, 492, 500, 509, 500, 274], ["+", 0, 57, 64, 37, 0, 492, 500, 509, 500, 22], ["-", 64, 37, 0, 492, 3, 4, 0, 509, 500, 22], ["+", 64, 37, 0, 492, 3, 4, 0, 509, 500, 274]]
3
395
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; String names[] = new String[n]; int[] ns = new int[30]; boolean[][] act = new boolean[n][30]; for (int i = 0; i < n; i++) { String name = sc.next(); names[i] = name; int m = sc.nextInt(); for (int j = 0; j < m; j++) { int d = sc.nextInt(); act[i][d] = true; ns[d]++; } } int index = 0; int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int p = 0; for (int j = 0; j < 30; j++) { if (act[i][j]) { p += n - ns[j] + 1; } } if (min > p) { min = p; index = i; } else if (min == p) { if (names[i].compareTo(names[index]) > 0) { index = i; } } } System.out.println(min + " " + names[index]); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; String names[] = new String[n]; int[] ns = new int[30]; boolean[][] act = new boolean[n][30]; for (int i = 0; i < n; i++) { String name = sc.next(); names[i] = name; int m = sc.nextInt(); for (int j = 0; j < m; j++) { int d = sc.nextInt(); act[i][d] = true; ns[d]++; } } int index = 0; int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int p = 0; for (int j = 0; j < 30; j++) { if (act[i][j]) { p += n - ns[j] + 1; } } if (min > p) { min = p; index = i; } else if (min == p) { if (names[i].compareTo(names[index]) < 0) { index = i; } } } System.out.println(min + " " + names[index]); } } }
[["-", 64, 196, 0, 57, 15, 15, 0, 16, 17, 47], ["+", 64, 196, 0, 57, 15, 15, 0, 16, 17, 18]]
3
318
char names[20][16]; int M[20][30], S[30], P[20]; main(n, m, i, j, d) { for (; scanf("%d", &n), n; printf("%d %s\n", P[d], names[d])) { memset(names, 0, sizeof names), memset(M, 0, sizeof M), memset(S, 0, sizeof S), memset(P, 0, sizeof P); for (i = 0; i < n; i++) for (scanf("%s%d", names[i], &m), j = 0; j < m; j++) scanf("%d", &d), M[i][d]++, S[d]++; for (i = 0; i < n; i++) for (j = 0; j < 30; j++) if (M[i][j]) P[i] += 1 + n - S[j]; for (d = 0, m = 999, i = 0; i < n; i++) if (P[i] < m || (P[i] == m && strcmp(names[i], names[d]) > 0)) d = i, m = P[i]; } exit(0); }
char names[20][16]; int M[20][30], S[30], P[20]; main(n, m, i, j, d) { for (; scanf("%d", &n), n; printf("%d %s\n", P[d], names[d])) { memset(names, 0, sizeof names), memset(M, 0, sizeof M), memset(S, 0, sizeof S), memset(P, 0, sizeof P); for (i = 0; i < n; i++) for (scanf("%s%d", names[i], &m), j = 0; j < m; j++) scanf("%d", &d), M[i][d]++, S[d]++; for (i = 0; i < n; i++) for (j = 0; j < 30; j++) if (M[i][j]) P[i] += 1 + n - S[j]; for (d = 0, m = 999, i = 0; i < n; i++) if (P[i] < m || (P[i] == m && strcmp(names[i], names[d]) < 0)) d = i, m = P[i]; } exit(0); }
[["-", 0, 16, 12, 23, 0, 16, 12, 16, 17, 47], ["+", 0, 16, 12, 23, 0, 16, 12, 16, 17, 18]]
0
304
#include <stdio.h> #include <string.h> typedef struct { char name[12]; int disp_num; int disp_time[30]; int points; } kyara_t; int kyara_num; kyara_t kyara[20]; int all_disp_num[30]; int main(void) { int i, j; int min_id; while (1) { scanf("%d", &kyara_num); if (kyara_num == 0) break; memset(kyara, 0, sizeof(kyara)); memset(all_disp_num, 0, sizeof(all_disp_num)); for (i = 0; i < kyara_num; i++) { scanf("%s%d", kyara[i].name, &kyara[i].disp_num); for (j = 0; j < kyara[i].disp_num; j++) { scanf("%d", &kyara[i].disp_time[j]); } } for (i = 0; i < kyara_num; i++) { for (j = 0; j < kyara[i].disp_num; j++) { kyara[i].points += kyara_num; all_disp_num[kyara[i].disp_time[j]]++; } } for (i = 0; i < kyara_num; i++) { for (j = 0; j < kyara[i].disp_num; j++) { kyara[i].points -= all_disp_num[kyara[i].disp_time[j]] - 1; } } for (i = 1, min_id = 0; i < kyara_num; i++) { if (kyara[i].points < kyara[min_id].points) { min_id = i; } else if (kyara[i].points == kyara[min_id].points) { if (strcmp(kyara[i].name, kyara[min_id].name) > 0) { min_id = i; } } } printf("%d %s\n", kyara[min_id].points, kyara[min_id].name); } return 0; }
#include <stdio.h> #include <string.h> typedef struct { char name[12]; int disp_num; int disp_time[30]; int points; } kyara_t; int kyara_num; kyara_t kyara[20]; int all_disp_num[30]; int main(void) { int i, j; int min_id; while (1) { scanf("%d", &kyara_num); if (kyara_num == 0) break; memset(kyara, 0, sizeof(kyara)); memset(all_disp_num, 0, sizeof(all_disp_num)); for (i = 0; i < kyara_num; i++) { scanf("%s%d", kyara[i].name, &kyara[i].disp_num); for (j = 0; j < kyara[i].disp_num; j++) { scanf("%d", &kyara[i].disp_time[j]); } } for (i = 0; i < kyara_num; i++) { for (j = 0; j < kyara[i].disp_num; j++) { kyara[i].points += kyara_num; all_disp_num[kyara[i].disp_time[j]]++; } } for (i = 0; i < kyara_num; i++) { for (j = 0; j < kyara[i].disp_num; j++) { kyara[i].points -= all_disp_num[kyara[i].disp_time[j]] - 1; } } for (i = 1, min_id = 0; i < kyara_num; i++) { if (kyara[i].points < kyara[min_id].points) { min_id = i; } else if (kyara[i].points == kyara[min_id].points) { if (strcmp(kyara[i].name, kyara[min_id].name) < 0) { min_id = i; } } } printf("%d %s\n", kyara[min_id].points, kyara[min_id].name); } return 0; }
[["-", 64, 9, 0, 57, 15, 23, 0, 16, 17, 47], ["+", 64, 9, 0, 57, 15, 23, 0, 16, 17, 18]]
0
408
#include <iostream> using namespace std; int main() { int n; while (cin >> n) { string name[40]; int m[40] = {}; int d[40][40] = {}; // input for (int i = 0; i < n; i++) { cin >> name[i] >> m[i]; for (int j = 0; j < m[i]; j++) { int tmp; cin >> tmp; d[i][tmp] = 1; } } // point int sum[40] = {}; for (int i = 0; i < 40; i++) { for (int j = 0; j < n; j++) { sum[i] += d[j][i]; } } /* for(int i =0; i < 15; i++){ cout << sum[i] << " "; } cout << endl; */ int pt[40] = {}; for (int j = 0; j < 40; j++) { for (int i = 0; i < n; i++) { if (d[i][j] == 1) { pt[i] += n + 1 - sum[j]; } } } /* for(int i =0; i < n; i++){ cout << pt[i] << " "; } */ // min int min = 0; for (int i = 0; i < n; i++) { if (pt[i] < pt[min]) min = i; } for (int i = 0; i < n; i++) { if (pt[i] == pt[min]) if (name[i] < name[min]) min = i; } cout << pt[min] << " " << name[min] << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n; while (cin >> n && n) { string name[40]; int m[40] = {}; int d[40][40] = {}; // input for (int i = 0; i < n; i++) { cin >> name[i] >> m[i]; for (int j = 0; j < m[i]; j++) { int tmp; cin >> tmp; d[i][tmp] = 1; } } // point int sum[40] = {}; for (int i = 0; i < 40; i++) { for (int j = 0; j < n; j++) { sum[i] += d[j][i]; } } /* for(int i =0; i < 15; i++){ cout << sum[i] << " "; } cout << endl; */ int pt[40] = {}; for (int j = 0; j < 40; j++) { for (int i = 0; i < n; i++) { if (d[i][j] == 1) { pt[i] += n + 1 - sum[j]; } } } /* for(int i =0; i < n; i++){ cout << pt[i] << " "; } */ // min int min = 0; for (int i = 0; i < n; i++) { if (pt[i] < pt[min]) min = i; } for (int i = 0; i < n; i++) { if (pt[i] == pt[min]) if (name[i] < name[min]) min = i; } cout << pt[min] << " " << name[min] << endl; } return 0; }
[["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 98], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22]]
1
344
#include <cstdio> #include <iostream> #include <string> #include <utility> using namespace std; typedef struct { string name; int popularity; bool scene[30]; } character; int main() { while (1) { int n; character c[20]; for (int i = 0; i < 20; ++i) { c[i].popularity = 0; for (int j = 0; j < 30; ++j) c[i].scene[j] = false; } scanf(" %d", &n); if (n == 0) break; for (int i = 0; i < n; ++i) { int m; cin >> c[i].name; scanf(" %d", &m); for (int j = 0; j < m; ++j) { int a; scanf(" %d", &a); c[i].scene[a - 1] = true; } } for (int i = 0; i < 30; ++i) { //シーン数 int count = 0; for (int j = 0; j < n; ++j) { if (c[j].scene[i]) ++count; } for (int j = 0; j < n; ++j) { if (c[j].scene[i]) { c[j].popularity += n - count + 1; } } } for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { if (c[i].popularity > c[j].popularity) swap(c[i], c[j]); else if (c[i].popularity == c[j].popularity) { if (c[i].name > c[j].name) swap(c[i], c[j]); } } } cout << c[0].popularity << " " << c[0].name << endl; } }
#include <cstdio> #include <iostream> #include <string> #include <utility> using namespace std; typedef struct { string name; int popularity; bool scene[30]; } character; int main() { while (1) { int n; character c[20]; for (int i = 0; i < 20; ++i) { c[i].popularity = 0; for (int j = 0; j < 30; ++j) c[i].scene[j] = false; } scanf(" %d", &n); if (n == 0) break; for (int i = 0; i < n; ++i) { int m; cin >> c[i].name; scanf(" %d", &m); for (int j = 0; j < m; ++j) { int a; scanf(" %d", &a); c[i].scene[a] = true; } } for (int i = 0; i < 30; ++i) { //シーン数 int count = 0; for (int j = 0; j < n; ++j) { if (c[j].scene[i]) ++count; } for (int j = 0; j < n; ++j) { if (c[j].scene[i]) { c[j].popularity += n - count + 1; } } } for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { if (c[i].popularity > c[j].popularity) swap(c[i], c[j]); else if (c[i].popularity == c[j].popularity) { if (c[i].name > c[j].name) swap(c[i], c[j]); } } } cout << c[0].popularity << " " << c[0].name << endl; } }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
428
#include <algorithm> #include <iostream> #include <string> using namespace std; int d[50], n, a, b, v[50][50]; pair<int, string> S[30]; int main() { while (true) { cin >> n; if (n == 0) break; for (int i = 0; i < 2500; i++) { d[i % 50] = 0; v[i / 50][i % 50] = 0; S[i % 30] = make_pair(0, ""); } for (int i = 0; i < n; i++) { cin >> S[i].second >> a; for (int j = 0; j < a; j++) { cin >> b; v[i][b] = 1; d[b]++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < 50; j++) { if (v[i][j] == 0) continue; S[i].first += n + 1 - d[j]; } } sort(S, S + n); cout << S[0].second << ' ' << S[0].first << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int d[50], n, a, b, v[50][50]; pair<int, string> S[30]; int main() { while (true) { cin >> n; if (n == 0) break; for (int i = 0; i < 2500; i++) { d[i % 50] = 0; v[i / 50][i % 50] = 0; S[i % 30] = make_pair(0, ""); } for (int i = 0; i < n; i++) { cin >> S[i].second >> a; for (int j = 0; j < a; j++) { cin >> b; v[i][b] = 1; d[b]++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < 50; j++) { if (v[i][j] == 0) continue; S[i].first += n + 1 - d[j]; } } sort(S, S + n); cout << S[0].first << ' ' << S[0].second << endl; } return 0; }
[["-", 31, 16, 31, 16, 31, 16, 12, 118, 119, 120], ["+", 31, 16, 31, 16, 31, 16, 12, 118, 119, 120], ["-", 0, 1, 0, 16, 31, 16, 12, 118, 119, 120], ["+", 0, 1, 0, 16, 31, 16, 12, 118, 119, 120]]
1
278
#include <algorithm> #include <cmath> #include <queue> #include <stack> #include <stdio.h> #include <vector> typedef long long int ll; #define BIG_NUM 2000000000 using namespace std; struct Info { Info() { point = 0; } char name[11]; int point; }; struct Data { Data() { index = 0; } int table[31], index; }; int N; bool strCmp(char *base, char *comp) { int length1 = 0, length2 = 0; for (int i = 0; base[i] != '\0'; i++) length1++; for (int i = 0; comp[i] != '\0'; i++) length2++; if (length1 != length2) return false; for (int i = 0; base[i] != '\0'; i++) { if (base[i] != comp[i]) return false; } return true; } void strcpy(char *to, char *str) { for (int i = 0; str[i] != '\0'; i++) { to[i] = str[i]; to[i + 1] = '\0'; } } int strCmp2(char *left, char *right) { int i; if (strCmp(left, right)) return 3; for (i = 0; left[i] != '\0' && right[i] != '\0'; i++) { if (left[i] != right[i]) { if (left[i] < right[i]) return 1; else { return 2; } } } if (left[i] == '\0') return 1; else { return 2; } } void func() { Info info[N], work; Data data[31]; int num, tmp; for (int i = 0; i < N; i++) { scanf("%s %d", info[i].name, &num); for (int k = 0; k < num; k++) { scanf("%d", &tmp); data[tmp].table[data[tmp].index++] = i; } } for (int i = 1; i <= 30; i++) { tmp = N - (data[i].index - 1); for (int k = 0; k < data[i].index; k++) { info[data[i].table[k]].point += tmp; } } for (int i = 1; i <= N - 1; i++) { for (int k = N - 1; k >= i; k--) { if ((info[k].point < info[k - 1].point) || (info[k].point == info[k - 1].point && strCmp2(info[k].name, info[k - 1].name) == 1)) { strcpy(work.name, info[k - 1].name); work.point = info[k - 1].point; strcpy(info[k - 1].name, info[k].name); info[k - 1].point = info[k].point; strcpy(info[k].name, work.name); info[k].point = work.point; } } } printf("%d %s\n", info[0].point, info[0].name); } int main() { while (true) { scanf("%d", &N); if (N == 0) break; func(); } return 0; }
#include <algorithm> #include <cmath> #include <queue> #include <stack> #include <stdio.h> #include <vector> typedef long long int ll; #define BIG_NUM 2000000000 using namespace std; struct Info { Info() { point = 0; } char name[11]; int point; }; struct Data { Data() { index = 0; } int table[35], index; }; int N; bool strCmp(char *base, char *comp) { int length1 = 0, length2 = 0; for (int i = 0; base[i] != '\0'; i++) length1++; for (int i = 0; comp[i] != '\0'; i++) length2++; if (length1 != length2) return false; for (int i = 0; base[i] != '\0'; i++) { if (base[i] != comp[i]) return false; } return true; } void strcpy(char *to, char *str) { for (int i = 0; str[i] != '\0'; i++) { to[i] = str[i]; to[i + 1] = '\0'; } } int strCmp2(char *left, char *right) { int i; if (strCmp(left, right)) return 3; for (i = 0; left[i] != '\0' && right[i] != '\0'; i++) { if (left[i] != right[i]) { if (left[i] < right[i]) return 1; else { return 2; } } } if (left[i] == '\0') return 1; else { return 2; } } void func() { Info info[N], work; Data data[31]; int num, tmp; for (int i = 0; i < N; i++) { scanf("%s %d", info[i].name, &num); for (int k = 0; k < num; k++) { scanf("%d", &tmp); data[tmp].table[data[tmp].index++] = i; } } for (int i = 0; i <= 30; i++) { tmp = N - (data[i].index - 1); for (int k = 0; k < data[i].index; k++) { info[data[i].table[k]].point += tmp; } } for (int i = 1; i <= N - 1; i++) { for (int k = N - 1; k >= i; k--) { if ((info[k].point < info[k - 1].point) || (info[k].point == info[k - 1].point && strCmp2(info[k].name, info[k - 1].name) == 1)) { strcpy(work.name, info[k - 1].name); work.point = info[k - 1].point; strcpy(info[k - 1].name, info[k].name); info[k - 1].point = info[k].point; strcpy(info[k].name, work.name); info[k].point = work.point; } } } printf("%d %s\n", info[0].point, info[0].name); } int main() { while (true) { scanf("%d", &N); if (N == 0) break; func(); } return 0; }
[["-", 0, 122, 8, 123, 0, 124, 49, 80, 81, 13], ["+", 0, 122, 8, 123, 0, 124, 49, 80, 81, 13], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
770
#include <algorithm> #include <iostream> #include <set> #include <vector> using namespace std; bool compare(const pair<string, int> &a, const pair<string, int> &b) { if (a.second != b.second) return a.second < b.second; else return a.first > b.first; } int main(void) { int n; while (cin >> n, n) { vector<pair<string, int>> chr(n); vector<set<int>> data(31); for (int i = 0; i < n; ++i) { cin >> chr[i].first; int m; cin >> m; for (int j = 0, d; j < m; ++j) { cin >> d; data[d].insert(i); } } for (int i = 0; i <= 30; ++i) { set<int>::iterator it = data[i].begin(); for (; it != data[i].end(); ++it) { chr[*it].second += n - data[i].size() + 1; } } sort(chr.begin(), chr.end(), compare); cout << chr[0].second << " " << chr[0].first << endl; } return 0; }
#include <algorithm> #include <iostream> #include <set> #include <vector> using namespace std; bool compare(const pair<string, int> &a, const pair<string, int> &b) { if (a.second != b.second) return a.second < b.second; else return a.first < b.first; } int main(void) { int n; while (cin >> n, n) { vector<pair<string, int>> chr(n); vector<set<int>> data(31); for (int i = 0; i < n; ++i) { cin >> chr[i].first; int m; cin >> m; for (int j = 0, d; j < m; ++j) { cin >> d; data[d].insert(i); } } for (int i = 0; i <= 30; ++i) { set<int>::iterator it = data[i].begin(); for (; it != data[i].end(); ++it) { chr[*it].second += n - data[i].size() + 1; } } sort(chr.begin(), chr.end(), compare); cout << chr[0].second << " " << chr[0].first << endl; } return 0; }
[["-", 0, 57, 75, 76, 0, 37, 0, 16, 17, 47], ["+", 0, 57, 75, 76, 0, 37, 0, 16, 17, 18]]
1
291
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; struct Primal_Dual { typedef pair<int, int> Pi; struct edge { int to, cap, cost, rev; }; vector<vector<edge>> graph; vector<int> potential, min_cost, prevv, preve; Primal_Dual(int V) : graph(V) {} void add_edge(int from, int to, int cap, int cost) { graph[from].push_back((edge){to, cap, cost, (int)graph[to].size()}); graph[to].push_back((edge){from, 0, -cost, (int)graph[from].size() - 1}); } int min_cost_flow(int s, int t, int f) { int V = graph.size(), ret = 0; priority_queue<Pi, vector<Pi>, greater<Pi>> que; potential.assign(V, 0); prevv.assign(V, -1); preve.assign(V, -1); while (f > 0) { min_cost.assign(V, INF); que.push(Pi(0, s)); min_cost[s] = 0; while (!que.empty()) { Pi p = que.top(); que.pop(); if (min_cost[p.second] < p.first) continue; for (int i = 0; i < graph[p.second].size(); i++) { edge &e = graph[p.second][i]; int nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to]; if (e.cap > 0 && min_cost[e.to] > nextCost) { min_cost[e.to] = nextCost; prevv[e.to] = p.second, preve[e.to] = i; que.push(Pi(min_cost[e.to], e.to)); } } } if (min_cost[t] == INF) return -1; for (int v = 0; v < V; v++) potential[v] += min_cost[v]; int addflow = f; for (int v = t; v != s; v = prevv[v]) { addflow = min(addflow, graph[prevv[v]][preve[v]].cap); } f -= addflow; ret += addflow * potential[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = graph[prevv[v]][preve[v]]; e.cap -= addflow; graph[v][e.rev].cap += addflow; } } return (ret); } }; struct Data { int X, Y, C; bool operator<(const Data &e) const { return (Y < e.Y); } }; int main() { int N; while (scanf("%d", &N), N) { Primal_Dual MCF(N * 20 * 3); vector<vector<Data>> train(N); vector<int> nums; for (int i = 0; i < N - 1; i++) { int M; scanf("%d", &M); while (M--) { int x, y, c; scanf("%d %d %d", &x, &y, &c); nums.push_back(y); train[i + 1].push_back((Data){x, y, c}); } } int G; train[0].push_back((Data){0, 0, 0}); scanf("%d", &G); int ptr = 0; int prev = 0; vector<int> arras, now; now.push_back(0); for (int i = 1; i < train.size(); i++) { sort(train[i].begin(), train[i].end()); int buff = ptr; for (int j = 0; j < train[i].size(); j++) { if (train[i - 1][0].Y > train[i][j].X) continue; int low = 0, high = train[i - 1].size() - 1; while (high - low > 0) { int mid = (low + high + 1) >> 1; if (train[i][j].X >= train[i - 1][mid].Y) low = mid; else high = mid - 1; } if (j == 0 || train[i][j - 1].Y != train[i][j].Y) { MCF.add_edge(now[low], ++ptr, 1, train[i][j].C); MCF.add_edge(ptr, ptr + 1, 1, 0); ++ptr; arras.push_back(ptr); if (j != 0) MCF.add_edge(ptr - 2, ptr, INF, 0); } else { arras.push_back(ptr); MCF.add_edge(now[low], ptr - 1, 1, train[i][j].C); } } prev = buff; swap(arras, now); arras.clear(); } int ret = 0, buff = 0, tail = 0; for (tail = 1; tail <= G; tail++) { if ((buff = MCF.min_cost_flow(0, ptr, 1)) == -1) break; ret += buff; }; scanf("%d %d\n", tail - 1, ret); } }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; struct Primal_Dual { typedef pair<int, int> Pi; struct edge { int to, cap, cost, rev; }; vector<vector<edge>> graph; vector<int> potential, min_cost, prevv, preve; Primal_Dual(int V) : graph(V) {} void add_edge(int from, int to, int cap, int cost) { graph[from].push_back((edge){to, cap, cost, (int)graph[to].size()}); graph[to].push_back((edge){from, 0, -cost, (int)graph[from].size() - 1}); } int min_cost_flow(int s, int t, int f) { int V = graph.size(), ret = 0; priority_queue<Pi, vector<Pi>, greater<Pi>> que; potential.assign(V, 0); prevv.assign(V, -1); preve.assign(V, -1); while (f > 0) { min_cost.assign(V, INF); que.push(Pi(0, s)); min_cost[s] = 0; while (!que.empty()) { Pi p = que.top(); que.pop(); if (min_cost[p.second] < p.first) continue; for (int i = 0; i < graph[p.second].size(); i++) { edge &e = graph[p.second][i]; int nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to]; if (e.cap > 0 && min_cost[e.to] > nextCost) { min_cost[e.to] = nextCost; prevv[e.to] = p.second, preve[e.to] = i; que.push(Pi(min_cost[e.to], e.to)); } } } if (min_cost[t] == INF) return -1; for (int v = 0; v < V; v++) potential[v] += min_cost[v]; int addflow = f; for (int v = t; v != s; v = prevv[v]) { addflow = min(addflow, graph[prevv[v]][preve[v]].cap); } f -= addflow; ret += addflow * potential[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = graph[prevv[v]][preve[v]]; e.cap -= addflow; graph[v][e.rev].cap += addflow; } } return (ret); } }; struct Data { int X, Y, C; bool operator<(const Data &e) const { return (Y < e.Y); } }; int main() { int N; while (scanf("%d", &N), N) { Primal_Dual MCF(N * 20 * 3); vector<vector<Data>> train(N); vector<int> nums; for (int i = 0; i < N - 1; i++) { int M; scanf("%d", &M); while (M--) { int x, y, c; scanf("%d %d %d", &x, &y, &c); nums.push_back(y); train[i + 1].push_back((Data){x, y, c}); } } int G; train[0].push_back((Data){0, 0, 0}); scanf("%d", &G); int ptr = 0; int prev = 0; vector<int> arras, now; now.push_back(0); for (int i = 1; i < train.size(); i++) { sort(train[i].begin(), train[i].end()); int buff = ptr; for (int j = 0; j < train[i].size(); j++) { if (train[i - 1][0].Y > train[i][j].X) continue; int low = 0, high = train[i - 1].size() - 1; while (high - low > 0) { int mid = (low + high + 1) >> 1; if (train[i][j].X >= train[i - 1][mid].Y) low = mid; else high = mid - 1; } if (j == 0 || train[i][j - 1].Y != train[i][j].Y) { MCF.add_edge(now[low], ++ptr, 1, train[i][j].C); MCF.add_edge(ptr, ptr + 1, 1, 0); ++ptr; arras.push_back(ptr); if (j != 0) MCF.add_edge(ptr - 2, ptr, INF, 0); } else { arras.push_back(ptr); MCF.add_edge(now[low], ptr - 1, 1, train[i][j].C); } } prev = buff; swap(arras, now); arras.clear(); } int ret = 0, buff = 0, tail = 0; for (tail = 1; tail <= G; tail++) { if ((buff = MCF.min_cost_flow(0, ptr, 1)) == -1) break; ret += buff; }; printf("%d %d\n", tail - 1, ret); } }
[["-", 0, 52, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 2, 63, 22]]
1
1,225
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) FOR(i, 0, (n)) #define RREP(i, n) RFOR(i, 0, (n)) using namespace std; typedef pair<int, int> P; #define pb push_back #define mp make_pair #define ft first #define sd second #define INF 1000000000 #define MAX_N 100 #define MAX_V (MAX_N * 20 * 2 + 1) struct edge { int to, cap, cost, rev; }; int V; vector<edge> G[MAX_V]; vector<edge> G2[MAX_V]; int dist[MAX_V]; int prevv[MAX_V], preve[MAX_V]; void add_edge(int from, int to, int cap, int cost) { G[from].push_back((edge){to, cap, cost, (int)G[to].size()}); G[to].push_back((edge){from, 0, -cost, (int)G[from].size() - 1}); } int min_cost_flow(int s, int t, int f) { int res = 0; while (f > 0) { fill(dist, dist + V, INF); dist[s] = 0; bool update = true; while (update) { update = false; for (int v = 0; v < V; v++) { if (dist[v] == INF) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; prevv[e.to] = v; preve[e.to] = i; update = true; } } } } if (dist[t] == INF) { return -1; } int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * dist[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } bool used[MAX_V]; int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { memset(used, 0, sizeof(used)); int f = dfs(s, t, INF); if (f == 0) return flow; flow += f; } } void resetG(int ver) { V = ver; REP(i, V) G[i].clear(); } int n; int m[MAX_N]; int x[MAX_N][20]; int y[MAX_N][20]; int c[MAX_N][20]; int g; bool input() { cin >> n; REP(i, n - 1) { cin >> m[i]; REP(j, m[i]) cin >> x[i][j] >> y[i][j] >> c[i][j]; } if (n != 0) cin >> g; return n != 0; } int tim[MAX_V]; vector<int> soeji[MAX_N - 1][2]; int x2[20]; void solve() { REP(i, MAX_V) G[i].clear(); V = 0; REP(i, n - 1) { soeji[i][0].clear(); soeji[i][1].clear(); REP(j, m[i]) { x2[j] = x[i][j]; } sort(x2, x2 + m[i]); REP(j, m[i]) { if (j == 0 || x2[j - 1] < x2[j]) { soeji[i][0].pb(V); tim[V] = x2[j]; if (j > 0) add_edge(V - 1, V, INF, 0); V++; } } REP(j, m[i]) { x2[j] = y[i][j]; } sort(x2, x2 + m[i]); REP(j, m[i]) { if (j == 0 || x2[j - 1] < x2[j]) { soeji[i][1].pb(V); tim[V] = x2[j]; V++; } } } REP(i, n - 1) { REP(j, m[i]) { int k = 0; while (tim[soeji[i][0][k++]] != x[i][j]) ; int l = 0; while (tim[soeji[i][1][l++]] == y[i][j]) ; add_edge(soeji[i][0][k - 1], soeji[i][1][l - 1], 1, c[i][j]); } } REP(i, n - 2) { REP(j, soeji[i][1].size()) { REP(k, soeji[i + 1][0].size()) { if (tim[soeji[i][1][j]] <= tim[soeji[i + 1][0][k]]) { add_edge(soeji[i][1][j], soeji[i + 1][0][k], 1, 0); break; } } } } REP(i, soeji[n - 2][1].size()) { add_edge(soeji[n - 2][1][i], V, 1, 0); } V++; REP(i, V) G2[i] = G[i]; int f = min(g, max_flow(0, V - 1)); REP(i, V) G[i] = G2[i]; printf("%d %d\n", f, min_cost_flow(0, V - 1, f)); } int main() { cin.tie(0); while (input()) { solve(); } return 0; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) FOR(i, 0, (n)) #define RREP(i, n) RFOR(i, 0, (n)) using namespace std; typedef pair<int, int> P; #define pb push_back #define mp make_pair #define ft first #define sd second #define INF 1000000000 #define MAX_N 100 #define MAX_V (MAX_N * 20 * 2 + 1) struct edge { int to, cap, cost, rev; }; int V; vector<edge> G[MAX_V]; vector<edge> G2[MAX_V]; int dist[MAX_V]; int prevv[MAX_V], preve[MAX_V]; void add_edge(int from, int to, int cap, int cost) { G[from].push_back((edge){to, cap, cost, (int)G[to].size()}); G[to].push_back((edge){from, 0, -cost, (int)G[from].size() - 1}); } int min_cost_flow(int s, int t, int f) { int res = 0; while (f > 0) { fill(dist, dist + V, INF); dist[s] = 0; bool update = true; while (update) { update = false; for (int v = 0; v < V; v++) { if (dist[v] == INF) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; prevv[e.to] = v; preve[e.to] = i; update = true; } } } } if (dist[t] == INF) { return -1; } int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * dist[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } bool used[MAX_V]; int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { memset(used, 0, sizeof(used)); int f = dfs(s, t, INF); if (f == 0) return flow; flow += f; } } void resetG(int ver) { V = ver; REP(i, V) G[i].clear(); } int n; int m[MAX_N]; int x[MAX_N][20]; int y[MAX_N][20]; int c[MAX_N][20]; int g; bool input() { cin >> n; REP(i, n - 1) { cin >> m[i]; REP(j, m[i]) cin >> x[i][j] >> y[i][j] >> c[i][j]; } if (n != 0) cin >> g; return n != 0; } int tim[MAX_V]; vector<int> soeji[MAX_N - 1][2]; int x2[20]; void solve() { REP(i, MAX_V) G[i].clear(); V = 0; REP(i, n - 1) { soeji[i][0].clear(); soeji[i][1].clear(); REP(j, m[i]) { x2[j] = x[i][j]; } sort(x2, x2 + m[i]); REP(j, m[i]) { if (j == 0 || x2[j - 1] < x2[j]) { soeji[i][0].pb(V); tim[V] = x2[j]; if (j > 0) add_edge(V - 1, V, INF, 0); V++; } } REP(j, m[i]) { x2[j] = y[i][j]; } sort(x2, x2 + m[i]); REP(j, m[i]) { if (j == 0 || x2[j - 1] < x2[j]) { soeji[i][1].pb(V); tim[V] = x2[j]; V++; } } } REP(i, n - 1) { REP(j, m[i]) { int k = 0; while (tim[soeji[i][0][k++]] != x[i][j]) ; int l = 0; while (tim[soeji[i][1][l++]] != y[i][j]) ; add_edge(soeji[i][0][k - 1], soeji[i][1][l - 1], 1, c[i][j]); } } REP(i, n - 2) { REP(j, soeji[i][1].size()) { REP(k, soeji[i + 1][0].size()) { if (tim[soeji[i][1][j]] <= tim[soeji[i + 1][0][k]]) { add_edge(soeji[i][1][j], soeji[i + 1][0][k], 1, 0); break; } } } } REP(i, soeji[n - 2][1].size()) { add_edge(soeji[n - 2][1][i], V, 1, 0); } V++; REP(i, V) G2[i] = G[i]; int f = min(g, max_flow(0, V - 1)); REP(i, V) G[i] = G2[i]; printf("%d %d\n", f, min_cost_flow(0, V - 1, f)); } int main() { cin.tie(0); while (input()) { solve(); } return 0; }
[["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 60], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 79]]
1
1,557
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #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 REPA(i, s, e) for (int i = s; i <= e; i++) #define REPD(i, s, e) for (int i = s; i >= e; i--) typedef unsigned long long ULL; typedef vector<int> VI; typedef pair<int, int> PII; int n, g; const int MAX_N = 101; const int MAX_M = 21; int m[MAX_N], x[MAX_N][MAX_M], y[MAX_N][MAX_M], c[MAX_N][MAX_M]; const int MAX_V = 5000; // 最大ノード番号 int v; // 通算ノード番号 map<int, int> idx[2][MAX_N]; // 0は入ってくる側,1は出ていく側 struct edge { int to; // 行き先 int cap; // 流量 int cost; // コスト int rev; // 逆辺が格納されている番号 }; vector<edge> G[MAX_V], G2[MAX_V]; int dist[MAX_V]; // ノードまでの最小コスト int prevv[MAX_V]; // 直前のノード int preve[MAX_V]; // 直前のエッジ const int INF = 1000000000; void add_edge(int from, int to, int cap, int cost) { edge e; e.to = to, e.cap = cap, e.cost = cost, e.rev = G[to].size(); G[from].push_back(e); e.to = from, e.cap = 0, e.cost = -cost, e.rev = G[from].size() - 1; // 準方向の辺はすでにaddしたので1を引く必要があるのに注意 G[to].push_back(e); } // 最小費用流問題を解く PII min_cost_flow(int s, int t, int f) { for (int i = 0; i < v; i++) G2[i] = G[i]; int res = 0; // 流量が限界になるまで流す int a = 0; // ゴールについたクラス数 int b = f; // 最大まで流し切った時のために保存 while (f > 0) { // ダイクストラ法で最短距離を検索 priority_queue<PII, vector<PII>, greater<PII>> que; fill(dist, dist + v, INF); dist[s] = 0; que.push(PII(0, s)); while (!que.empty()) { PII p = que.top(); que.pop(); int nv = p.second; if (dist[nv] < p.first) continue; for (int i = 0; i < G2[nv].size(); i++) { edge e = G2[nv][i]; if (e.cap > 0 && dist[e.to] > dist[nv] + e.cost) { dist[e.to] = dist[nv] + e.cost; prevv[e.to] = nv; preve[e.to] = i; que.push(PII(dist[e.to], e.to)); } } } if (dist[t] == INF) return PII(a, res); a++; int d = f; for (int nv = t; nv != s; nv = prevv[nv]) { d = min(d, G2[prevv[nv]][preve[nv]].cap); } f -= d; res += d * dist[t]; for (int nv = t; nv != s; nv = prevv[nv]) { edge &e = G2[prevv[nv]][preve[nv]]; e.cap = -d; G2[nv][e.rev].cap += d; } } return PII(b, res); } int main() { while (cin >> n, n) { // データのクリア REP(i, MAX_V) G[i].clear(); REP(i, 2) REP(j, n) idx[i][j].clear(); // データの読み込み for (int i = 1; i < n; i++) { cin >> m[i]; REP(j, m[i]) cin >> x[i][j] >> y[i][j] >> c[i][j]; } cin >> g; // グラフの作成を開始 v = 0; idx[1][1][0] = v++; // 0番目の駅には0分の時点で着いている for (int q = 1; q < n; q++) { // q番目の駅から出ていく列車の到着する先にとの間にエッジを張る for (map<int, int>::iterator it = idx[1][q].begin(); it != idx[1][q].end(); ++it) { int time = it->first; // q番目の駅に到着した時間 for (int j = 0; j < m[q]; j++) { // q番目の駅から出発する列車を調査 if (x[q][j] >= time) { // 到着した時点でまだ出発していない if (idx[0][q + 1].count(y[q][j]) == 0) idx[0][q + 1][y[q][j]] = v++; // まだ登場していないノードならノードの番号を追加 add_edge(it->second, idx[0][q + 1][y[q][j]], 1, c[q][j]); } } } // 2つペアで管理されている駅上での発着をエッジで結ぶ for (map<int, int>::iterator it = idx[0][q + 1].begin(); it != idx[0][q + 1].end(); ++it) { int time = it->first; if (idx[1][q + 1].count(time) == 0) idx[1][q + 1][time] = v++; // まだ登場していないノードならノードの番号を追加 add_edge(it->second, idx[1][q + 1][time], 1, 0); // 流量を制限するためのノードなのでコストは不要 } } idx[0][n + 1][0] = v++; // ゴールのノードへエッジを張る for (map<int, int>::iterator it = idx[1][n].begin(); it != idx[1][n].end(); ++it) { add_edge(it->second, idx[0][n + 1][0], 1, 0); } int sp = 0; int gp = v - 1; PII res = min_cost_flow(sp, gp, g); if (res.first != 0) { cout << res.first << " " << res.second << endl; } else { cout << 0 << " " << 0 << endl; } } }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #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 REPA(i, s, e) for (int i = s; i <= e; i++) #define REPD(i, s, e) for (int i = s; i >= e; i--) typedef unsigned long long ULL; typedef vector<int> VI; typedef pair<int, int> PII; int n, g; const int MAX_N = 101; const int MAX_M = 21; int m[MAX_N], x[MAX_N][MAX_M], y[MAX_N][MAX_M], c[MAX_N][MAX_M]; const int MAX_V = 5000; // 最大ノード番号 int v; // 通算ノード番号 map<int, int> idx[2][MAX_N]; // 0は入ってくる側,1は出ていく側 struct edge { int to; // 行き先 int cap; // 流量 int cost; // コスト int rev; // 逆辺が格納されている番号 }; vector<edge> G[MAX_V], G2[MAX_V]; int dist[MAX_V]; // ノードまでの最小コスト int prevv[MAX_V]; // 直前のノード int preve[MAX_V]; // 直前のエッジ const int INF = 1000000000; void add_edge(int from, int to, int cap, int cost) { edge e; e.to = to, e.cap = cap, e.cost = cost, e.rev = G[to].size(); G[from].push_back(e); e.to = from, e.cap = 0, e.cost = -cost, e.rev = G[from].size() - 1; // 準方向の辺はすでにaddしたので1を引く必要があるのに注意 G[to].push_back(e); } // 最小費用流問題を解く PII min_cost_flow(int s, int t, int f) { for (int i = 0; i < v; i++) G2[i] = G[i]; int res = 0; // 流量が限界になるまで流す int a = 0; // ゴールについたクラス数 int b = f; // 最大まで流し切った時のために保存 while (f > 0) { // ダイクストラ法で最短距離を検索 priority_queue<PII, vector<PII>, greater<PII>> que; fill(dist, dist + v, INF); dist[s] = 0; que.push(PII(0, s)); while (!que.empty()) { PII p = que.top(); que.pop(); int nv = p.second; if (dist[nv] < p.first) continue; for (int i = 0; i < G2[nv].size(); i++) { edge &e = G2[nv][i]; if (e.cap > 0 && dist[e.to] > dist[nv] + e.cost) { dist[e.to] = dist[nv] + e.cost; prevv[e.to] = nv; preve[e.to] = i; que.push(PII(dist[e.to], e.to)); } } } if (dist[t] == INF) return PII(a, res); a++; int d = f; for (int nv = t; nv != s; nv = prevv[nv]) { d = min(d, G2[prevv[nv]][preve[nv]].cap); } f -= d; res += d * dist[t]; for (int nv = t; nv != s; nv = prevv[nv]) { edge &e = G2[prevv[nv]][preve[nv]]; e.cap = -d; G2[nv][e.rev].cap += d; } } return PII(b, res); } int main() { while (cin >> n, n) { // データのクリア REP(i, MAX_V) G[i].clear(); REP(i, 2) REP(j, MAX_N) idx[i][j].clear(); // データの読み込み for (int i = 1; i < n; i++) { cin >> m[i]; REP(j, m[i]) cin >> x[i][j] >> y[i][j] >> c[i][j]; } cin >> g; // グラフの作成を開始 v = 0; idx[1][1][0] = v++; // 0番目の駅には0分の時点で着いている for (int q = 1; q < n; q++) { // q番目の駅から出ていく列車の到着する先にとの間にエッジを張る for (map<int, int>::iterator it = idx[1][q].begin(); it != idx[1][q].end(); ++it) { int time = it->first; // q番目の駅に到着した時間 for (int j = 0; j < m[q]; j++) { // q番目の駅から出発する列車を調査 if (x[q][j] >= time) { // 到着した時点でまだ出発していない if (idx[0][q + 1].count(y[q][j]) == 0) idx[0][q + 1][y[q][j]] = v++; // まだ登場していないノードならノードの番号を追加 add_edge(it->second, idx[0][q + 1][y[q][j]], 1, c[q][j]); } } } // 2つペアで管理されている駅上での発着をエッジで結ぶ for (map<int, int>::iterator it = idx[0][q + 1].begin(); it != idx[0][q + 1].end(); ++it) { int time = it->first; if (idx[1][q + 1].count(time) == 0) idx[1][q + 1][time] = v++; // まだ登場していないノードならノードの番号を追加 add_edge(it->second, idx[1][q + 1][time], 1, 0); // 流量を制限するためのノードなのでコストは不要 } } idx[0][n + 1][0] = v++; // ゴールのノードへエッジを張る for (map<int, int>::iterator it = idx[1][n].begin(); it != idx[1][n].end(); ++it) { add_edge(it->second, idx[0][n + 1][0], 1, 0); } int sp = 0; int gp = v - 1; PII res = min_cost_flow(sp, gp, g); if (res.first != 0) { cout << res.first << " " << res.second << endl; } else { cout << 0 << " " << 0 << endl; } } }
[["+", 8, 9, 0, 43, 49, 50, 49, 352, 0, 67], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
1,337
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<vector<pii>> graph; const int INF = 1e9; inline int match(int cur, int in, const string &s) { string prv = s.substr(0, cur) + (char)(in + '0'); rep(i, cur + 1) { int len = cur + 1 - i; if (s.substr(0, len) == prv.substr(i, len)) return len; } return 0; } void dfs(int cur, vector<int> &vis, graph &g) { if (vis[cur]) return; vis[cur] = 1; for (pii e : g[cur]) dfs(e.second, vis, g); } bool BellmanFord(int s, int V, vector<int> &d, const graph &g) { d = vi(V, INF); d[s] = 0; rep(azu, V) { bool f = false; rep(i, V) for (pii e : g[i]) { if (d[i] < INF && d[e.second] > d[i] + e.first) { d[e.second] = d[i] + e.first; f = true; } } if (!f) return true; } return false; } int main() { cin.tie(0); ios::sync_with_stdio(0); int n, m, k, l; string pattern; while (cin >> n, n) { vi col(n); rep(i, n) cin >> col[i], col[i]--; cin >> m; graph base(n); rep(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; base[a].push_back(pii(c, b)); } cin >> k >> pattern; l = pattern.size(); vector<vi> trans(l, vi(4, 0)); rep(i, l) rep(j, 4) { trans[i][j] = match(i, j + 1, pattern); } int V = n * l; graph g(V), rg(V); rep(i, l) rep(j, n) { for (pii e : base[j]) { int to = e.second, nxt = trans[i][col[to]]; if (nxt == l) continue; g[i * n + j].push_back(pii(e.first, nxt * n + to)); rg[nxt * n + to].push_back(pii(e.first, i * n + j)); } } if (trans[0][col[0]] == l) { cout << -1 << endl; continue; } int S = trans[0][col[0]] * n; vi vis(V, 0), rvis(V, 0); dfs(S, vis, g); rep(i, l) dfs(i * n + n - 1, rvis, rg); rep(i, V) { if (vis[i] == 0 || rvis[i] == 0) g[i].clear(); rep(j, g[i].size()) { int to = g[i][j].second; if (vis[to] == 0 || rvis[to] == 0) { g[i].erase(g[i].begin() + j); j--; } } } vi h; if (!BellmanFord(S, V, h, g)) { cout << -1 << endl; continue; } vector<vi> d(V, vi(k, INF)); d[S][0] = 0; priority_queue<pii, vector<pii>, greater<pii>> q; q.push(pii(0, S)); while (q.size()) { pii p = q.top(); q.pop(); int v = p.second, c = p.first; for (pii e : g[v]) { int nv = e.second, nc = c + e.first + h[v] - h[nv]; bool f = true; rep(i, k) { if (d[nv][i] > nc) { if (f) q.push(pii(nc, nv)), f = false; swap(d[nv][i], nc); } } } } vi can; rep(i, l) rep(j, k) { if (d[i * n + n - 1][j] != INF) { can.push_back(d[i * n + n - 1][j] - h[S] + h[i * n + n - 1]); } } sort(can.begin(), can.end()); int ans = min((int)can.size(), k); long long sum = 0; rep(i, ans) sum += can[i]; cout << ans << " " << sum << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<vector<pii>> graph; const int INF = 1e9; inline int match(int cur, int in, const string &s) { string prv = s.substr(0, cur) + (char)(in + '0'); rep(i, cur + 1) { int len = cur + 1 - i; if (s.substr(0, len) == prv.substr(i, len)) return len; } return 0; } void dfs(int cur, vector<int> &vis, graph &g) { if (vis[cur]) return; vis[cur] = 1; for (pii e : g[cur]) dfs(e.second, vis, g); } bool BellmanFord(int s, int V, vector<int> &d, const graph &g) { d = vi(V, INF); d[s] = 0; rep(azu, V) { bool f = false; rep(i, V) for (pii e : g[i]) { if (d[i] < INF && d[e.second] > d[i] + e.first) { d[e.second] = d[i] + e.first; f = true; } } if (!f) return true; } return false; } int main() { cin.tie(0); ios::sync_with_stdio(0); int n, m, k, l; string pattern; while (cin >> n, n) { vi col(n); rep(i, n) cin >> col[i], col[i]--; cin >> m; graph base(n); rep(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; base[a].push_back(pii(c, b)); } cin >> k >> pattern; l = pattern.size(); vector<vi> trans(l, vi(4, 0)); rep(i, l) rep(j, 4) { trans[i][j] = match(i, j + 1, pattern); } int V = n * l; graph g(V), rg(V); rep(i, l) rep(j, n) { for (pii e : base[j]) { int to = e.second, nxt = trans[i][col[to]]; if (nxt == l) continue; g[i * n + j].push_back(pii(e.first, nxt * n + to)); rg[nxt * n + to].push_back(pii(e.first, i * n + j)); } } if (trans[0][col[0]] == l) { cout << "0 0" << endl; continue; } int S = trans[0][col[0]] * n; vi vis(V, 0), rvis(V, 0); dfs(S, vis, g); rep(i, l) dfs(i * n + n - 1, rvis, rg); rep(i, V) { if (vis[i] == 0 || rvis[i] == 0) g[i].clear(); rep(j, g[i].size()) { int to = g[i][j].second; if (vis[to] == 0 || rvis[to] == 0) { g[i].erase(g[i].begin() + j); j--; } } } vi h; if (!BellmanFord(S, V, h, g)) { cout << -1 << endl; continue; } vector<vi> d(V, vi(k, INF)); d[S][0] = 0; priority_queue<pii, vector<pii>, greater<pii>> q; q.push(pii(0, S)); while (q.size()) { pii p = q.top(); q.pop(); int v = p.second, c = p.first; for (pii e : g[v]) { int nv = e.second, nc = c + e.first + h[v] - h[nv]; bool f = true; rep(i, k) { if (d[nv][i] > nc) { if (f) q.push(pii(nc, nv)), f = false; swap(d[nv][i], nc); } } } } vi can; rep(i, l) rep(j, k) { if (d[i * n + n - 1][j] != INF) { can.push_back(d[i * n + n - 1][j] - h[S] + h[i * n + n - 1]); } } sort(can.begin(), can.end()); int ans = min((int)can.size(), k); long long sum = 0; rep(i, ans) sum += can[i]; cout << ans << " " << sum << endl; } }
[["-", 64, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
1,168
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; const int INF = 1 << 29; struct edge { int v, cost; }; // Šg’£‚µ‚½ƒOƒ‰ƒt‚É‚¨‚¯‚é’¸“_ struct Xnode { int u, t; // ƒR[ƒ“‚̔ԍ†, pttn // ‚̉½•¶Žš–ڂ܂Ń}ƒbƒ`‚µ‚½‚© bool operator<(const Xnode &X) const { return u < X.u || u == X.u && t < X.t; } }; // Šg’£‚µ‚½ƒOƒ‰ƒt‚É‚¨‚¯‚é•Ó struct Xedge { Xnode X; int cost; }; int n, color[100], K, len; vector<edge> G[100]; // Œ³‚̃Oƒ‰ƒt char pttn[11]; bool input() { scanf("%d", &n); if (n == 0) return false; rep(u, n) { G[u].clear(); scanf("%d", color + u); } int m; scanf("%d", &m); rep(i, m) { int u, v, cost; scanf("%d%d%d", &u, &v, &cost); u--; v--; G[u].push_back((edge){v, cost}); } scanf("%d%s", &K, pttn); len = strlen(pttn); return true; } vector<Xedge> XG[101][10], XG_rev[101][10]; // Šg’£‚µ‚½ƒOƒ‰ƒt, XG ‚Ì‹tƒOƒ‰ƒt void build_XG() { rep(u, n + 1) rep(t, max(len, 2)) { XG[u][t].clear(); XG_rev[u][t].clear(); } rep(u, n) rep(t, len) rep(i, G[u].size()) { int v = G[u][i].v, cost = G[u][i].cost; char tmp[12] = {}; strncpy(tmp, pttn, t); tmp[t] = color[v] + '0'; int t_next; for (t_next = t + 1; strncmp(pttn, tmp + (t + 1) - t_next, t_next); t_next--) ; if (t_next < len) { XG[u][t].push_back((Xedge){(Xnode){v, t_next}, cost}); XG_rev[v][t_next].push_back((Xedge){(Xnode){u, t}, cost}); } } // XG_rev ‚ÌŽn“_‚½‚¿ ( XG ‚̏I“_‚½‚¿ ) // ‚ðˆê‚‚ɂ܂Ƃ߂é rep(t, len) XG_rev[n][0].push_back((Xedge){(Xnode){n - 1, t}, 0}); // ƒ|ƒeƒ“ƒVƒƒƒ‹ŒvŽZ—p‚Ì super source rep(u, n) rep(t, len) XG[n][0].push_back((Xedge){(Xnode){u, t}, 0}); } bool vis[101][10]; // XG ‚É‚¨‚¢‚ÄŽn“_ (0, t0) ‚©‚ç’H‚è’…‚¯‚é Xnode // ‚̏W‡ bool dfs(int u, int t) { vis[u][t] = true; rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t; if (!vis[v][t_next]) dfs(v, t_next); } } bool vis_rev[101][10]; // XG ‚É‚¨‚¢‚ďI“_‚½‚¿ (n-1, t) (0 < t < len) // ‚©‚ç’H‚è’…‚¯‚é Xnode ‚̏W‡ bool dfs_rev(int v, int t) { vis_rev[v][t] = true; rep(i, XG_rev[v][t].size()) { const Xedge &e = XG_rev[v][t][i]; int u = e.X.u, t_prev = e.X.t; if (!vis_rev[u][t_prev]) dfs_rev(u, t_prev); } } // Bellman-Ford ‚É‚æ‚éŒë”»’è‚ð–h‚®‚½‚߂ɕK—v‚̂Ȃ¢ Xnode ‚ð XG // ‚©‚çíœ‚·‚é void delete_Xnodes() { rep(u, n + 1) rep(t, len) vis[u][t] = vis_rev[n][t] = false; int t0 = (pttn[0] == color[0] + '0' ? 1 : 0); dfs(0, t0); dfs_rev(n, 0); // rep(u,n+1) printf("%d ",vis[u][0]); puts(""); // rep(u,n+1) printf("%d ",vis_rev[u][0]); puts(""); rep(u, n) rep(t, len) { if (!vis[u][t] || !vis_rev[u][t]) { XG[u][t].clear(); } else { rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t; if (!vis[v][t_next] || !vis_rev[v][t_next]) { XG[u][t].erase(XG[u][t].begin() + i); i--; } } } } } int pot[101][10]; // Bellman-Ford –@‚ð—p‚¢‚ă|ƒeƒ“ƒVƒƒƒ‹‚ðŒvŽZ bool calc_potential() { rep(u, n + 1) rep(t, len) pot[u][t] = INF; pot[n][0] = 0; rep(c, n * len) { rep(u, n + 1) rep(t, len) { if (pot[u][t] == INF) continue; rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t, cost = e.cost; if (pot[u][t] + cost < pot[v][t_next]) { pot[v][t_next] = pot[u][t] + cost; if (c == n * len - 1) { puts("-1"); return false; } } } } } return true; } void Kth_shortest_path() { int nd[100][10] = {}, d[100][10][10]; int t0 = (pttn[0] == color[0] + '0' ? 1 : 0); priority_queue<pair<int, Xnode>> Q; Q.push(make_pair(0, (Xnode){0, t0})); while (!Q.empty()) { int d_now = -Q.top().first; int u = Q.top().second.u, t = Q.top().second.t; Q.pop(); if (nd[u][t] == K) continue; d[u][t][nd[u][t]++] = d_now; rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t, cost = e.cost; if (nd[v][t_next] < K) { int d_next = d_now + cost + pot[u][t] - pot[v][t_next]; Q.push(make_pair(-d_next, (Xnode){v, t_next})); } } } vector<int> ans; rep(t, len) rep(i, nd[n - 1][t]) ans.push_back(d[n - 1][t][i] - pot[0][t0] + pot[n - 1][t]); sort(ans.begin(), ans.end()); int n_ans = min((int)ans.size(), K), ans_sum = 0; rep(i, n_ans) ans_sum += ans[i]; printf("%d %d\n", n_ans, ans_sum); } int main() { while (input()) { build_XG(); delete_Xnodes(); if (!calc_potential()) continue; Kth_shortest_path(); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; const int INF = 1 << 29; struct edge { int v, cost; }; // Šg’£‚µ‚½ƒOƒ‰ƒt‚É‚¨‚¯‚é’¸“_ struct Xnode { int u, t; // ƒR[ƒ“‚̔ԍ†, pttn // ‚̉½•¶Žš–ڂ܂Ń}ƒbƒ`‚µ‚½‚© bool operator<(const Xnode &X) const { return u < X.u || u == X.u && t < X.t; } }; // Šg’£‚µ‚½ƒOƒ‰ƒt‚É‚¨‚¯‚é•Ó struct Xedge { Xnode X; int cost; }; int n, color[100], K, len; vector<edge> G[100]; // Œ³‚̃Oƒ‰ƒt char pttn[11]; bool input() { scanf("%d", &n); if (n == 0) return false; rep(u, n) { G[u].clear(); scanf("%d", color + u); } int m; scanf("%d", &m); rep(i, m) { int u, v, cost; scanf("%d%d%d", &u, &v, &cost); u--; v--; G[u].push_back((edge){v, cost}); } scanf("%d%s", &K, pttn); len = strlen(pttn); return true; } vector<Xedge> XG[101][10], XG_rev[101][10]; // Šg’£‚µ‚½ƒOƒ‰ƒt, XG ‚Ì‹tƒOƒ‰ƒt void build_XG() { rep(u, n + 1) rep(t, max(len, 2)) { XG[u][t].clear(); XG_rev[u][t].clear(); } rep(u, n) rep(t, len) rep(i, G[u].size()) { int v = G[u][i].v, cost = G[u][i].cost; char tmp[12] = {}; strncpy(tmp, pttn, t); tmp[t] = color[v] + '0'; int t_next; for (t_next = t + 1; strncmp(pttn, tmp + (t + 1) - t_next, t_next); t_next--) ; if (t_next < len) { XG[u][t].push_back((Xedge){(Xnode){v, t_next}, cost}); XG_rev[v][t_next].push_back((Xedge){(Xnode){u, t}, cost}); } } // XG_rev ‚ÌŽn“_‚½‚¿ ( XG ‚̏I“_‚½‚¿ ) // ‚ðˆê‚‚ɂ܂Ƃ߂é rep(t, len) XG_rev[n][0].push_back((Xedge){(Xnode){n - 1, t}, 0}); // ƒ|ƒeƒ“ƒVƒƒƒ‹ŒvŽZ—p‚Ì super source rep(u, n) rep(t, len) XG[n][0].push_back((Xedge){(Xnode){u, t}, 0}); } bool vis[101][10]; // XG ‚É‚¨‚¢‚ÄŽn“_ (0, t0) ‚©‚ç’H‚è’…‚¯‚é Xnode // ‚̏W‡ bool dfs(int u, int t) { vis[u][t] = true; rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t; if (!vis[v][t_next]) dfs(v, t_next); } } bool vis_rev[101][10]; // XG ‚É‚¨‚¢‚ďI“_‚½‚¿ (n-1, t) (0 < t < len) // ‚©‚ç’H‚è’…‚¯‚é Xnode ‚̏W‡ bool dfs_rev(int v, int t) { vis_rev[v][t] = true; rep(i, XG_rev[v][t].size()) { const Xedge &e = XG_rev[v][t][i]; int u = e.X.u, t_prev = e.X.t; if (!vis_rev[u][t_prev]) dfs_rev(u, t_prev); } } // Bellman-Ford ‚É‚æ‚éŒë”»’è‚ð–h‚®‚½‚߂ɕK—v‚̂Ȃ¢ Xnode ‚ð XG // ‚©‚çíœ‚·‚é void delete_Xnodes() { rep(u, n + 1) rep(t, len) vis[u][t] = vis_rev[u][t] = false; int t0 = (pttn[0] == color[0] + '0' ? 1 : 0); dfs(0, t0); dfs_rev(n, 0); rep(u, n) rep(t, len) { if (!vis[u][t] || !vis_rev[u][t]) { XG[u][t].clear(); } else { rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t; if (!vis[v][t_next] || !vis_rev[v][t_next]) { XG[u][t].erase(XG[u][t].begin() + i); i--; } } } } } int pot[101][10]; // Bellman-Ford –@‚ð—p‚¢‚ă|ƒeƒ“ƒVƒƒƒ‹‚ðŒvŽZ bool calc_potential() { rep(u, n + 1) rep(t, len) pot[u][t] = INF; pot[n][0] = 0; rep(c, n * len) { rep(u, n + 1) rep(t, len) { if (pot[u][t] == INF) continue; rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t, cost = e.cost; if (pot[u][t] + cost < pot[v][t_next]) { pot[v][t_next] = pot[u][t] + cost; if (c == n * len - 1) { puts("-1"); return false; } } } } } return true; } void Kth_shortest_path() { int nd[100][10] = {}, d[100][10][10]; int t0 = (pttn[0] == color[0] + '0' ? 1 : 0); priority_queue<pair<int, Xnode>> Q; Q.push(make_pair(0, (Xnode){0, t0})); while (!Q.empty()) { int d_now = -Q.top().first; int u = Q.top().second.u, t = Q.top().second.t; Q.pop(); if (nd[u][t] == K) continue; d[u][t][nd[u][t]++] = d_now; rep(i, XG[u][t].size()) { const Xedge &e = XG[u][t][i]; int v = e.X.u, t_next = e.X.t, cost = e.cost; if (nd[v][t_next] < K) { int d_next = d_now + cost + pot[u][t] - pot[v][t_next]; Q.push(make_pair(-d_next, (Xnode){v, t_next})); } } } vector<int> ans; rep(t, len) rep(i, nd[n - 1][t]) ans.push_back(d[n - 1][t][i] - pot[0][t0] + pot[n - 1][t]); sort(ans.begin(), ans.end()); int n_ans = min((int)ans.size(), K), ans_sum = 0; rep(i, n_ans) ans_sum += ans[i]; printf("%d %d\n", n_ans, ans_sum); } int main() { while (input()) { build_XG(); delete_Xnodes(); if (!calc_potential()) continue; Kth_shortest_path(); } return 0; }
[["-", 12, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 12, 11, 31, 69, 28, 69, 341, 342, 0, 22]]
1
1,698
#define _GLIBCXX_DEBUG #include <algorithm> #include <cassert> #include <deque> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define FOREACH(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define TIMES(i, n) for (int i = 0; i < (n); ++i) typedef long long Cost; const long long INF = 1000 * 1000 * 10 * 10 * 2 * 10LL; struct Edge { int from, to; Cost cost; Edge() {} Edge(int f, int t, Cost c) : from(f), to(t), cost(c) {} }; struct DistTag { int size; deque<Cost> buf, newbie; DistTag(int size) : size(size), buf(size, INF) { // newbie.reserve(size); } bool insert(Cost val) { if (buf.back() <= val) return false; deque<Cost>::iterator ins_pos = upper_bound(newbie.begin(), newbie.end(), val); newbie.insert(ins_pos, val); if (static_cast<int>(newbie.size()) > size) newbie.pop_back(); return true; } bool has(Cost val) { return buf.back() >= val; } void flush() { FOREACH(it, newbie) { deque<Cost>::iterator ins_pos = upper_bound(buf.begin(), buf.end(), *it); buf.insert(ins_pos, *it); } buf.resize(size); newbie.clear(); } }; struct State { int pos; Cost cost; State() {} State(int p, Cost c) : pos(p), cost(c) {} bool operator<(const State &other) const { return cost > other.cost; // Reversed } }; typedef vector<Edge> EdgeList; void check_reachable(int start, const vector<EdgeList> &graph, vector<unsigned char> &reachable) { queue<int> q; q.push(start); reachable[start] = 1; while (!q.empty()) { int cur = q.front(); q.pop(); FOREACH(it, graph[cur]) { if (!reachable[it->to]) { reachable[it->to] = 1; q.push(it->to); } } } } bool relax(const vector<EdgeList> &graph, vector<Cost> &dist) { bool updated = false; FOREACH(list_it, graph) { FOREACH(it, *list_it) { const Cost cost = dist[it->from] + it->cost; if (dist[it->to] > cost) { dist[it->to] = cost; updated = true; } } } return updated; } pair<int, Cost> search(int start, const vector<EdgeList> &graph, int N, int P, int K) { vector<DistTag> dist(graph.size(), DistTag(K)); dist[start].insert(0); TIMES(_, static_cast<int>(graph.size()) * K) { TIMES(i, static_cast<int>(graph.size())) { DistTag &dt = dist[i]; if (dt.newbie.size() == 0) continue; vector<int> tmp; FOREACH(it, graph[i]) { assert(it->from == i); // Corner case: loop edge if (it->to == i) { FOREACH(nit, dt.newbie) { tmp.push_back(*nit + it->cost); } } else { FOREACH(nit, dt.newbie) { // cout << "insert " << i << ' ' << it->to << endl; dist[it->to].insert(*nit + it->cost); } } } dt.flush(); FOREACH(tit, tmp) { dt.insert(*tit); } } } pair<int, Cost> res(0, 0); vector<Cost> costs; TIMES(i, P) { FOREACH(it, dist[(N - 1) * P + i].buf) { costs.push_back(*it); } } sort(costs.begin(), costs.end()); TIMES(i, min(K, static_cast<int>(costs.size()))) { if (costs[i] == INF) break; res.first++; res.second += costs[i]; } return res; } pair<int, Cost> solve(int N) { vector<int> color_of(N); TIMES(i, N) { cin >> color_of[i]; --color_of[i]; } int M; cin >> M; vector<Edge> edges(M); TIMES(i, M) { Edge &e = edges[i]; cin >> e.from >> e.to >> e.cost; --e.from; --e.to; } int K; cin >> K; vector<int> prohibited; { string str; cin >> str; FOREACH(it, str) { prohibited.push_back(*it - '0' - 1); } } const int P = prohibited.size(); vector<vector<int>> next_prohibited_pos(P, vector<int>(4, 0)); { int shadow = 0; TIMES(i, P) { next_prohibited_pos[i][prohibited[i]] = i + 1; if (i > 0) { TIMES(j, 4) { if (j != prohibited[i]) next_prohibited_pos[i][j] = next_prohibited_pos[shadow][j]; } shadow = next_prohibited_pos[shadow][prohibited[i]]; } } } const int start = 0 + next_prohibited_pos[0][color_of[0]]; vector<EdgeList> graph(N * P); FOREACH(it, edges) { TIMES(i, P) { // Cannot reach this state. if (i > 0 && prohibited[i - 1] != color_of[it->from]) continue; int np = next_prohibited_pos[i][color_of[it->to]]; if (np < P) { graph[it->from * P + i].push_back( Edge(it->from * P + i, it->to * P + np, it->cost)); // cout << it->from << ' ' << i << ' ' << it->to << ' ' << np << endl; } } } // Remove all unreachable nodes. // It causes fake negative loop detection. vector<unsigned char> reachable(N * P, 0); check_reachable(start, graph, reachable); TIMES(i, N * P) { if (!reachable[i]) graph[i].clear(); } // Check if it has negative loop vector<Cost> dist(N * P, INF); dist[start] = 0; TIMES(_, N * P) { relax(graph, dist); } vector<Cost> tmp_last(P); TIMES(i, P) { tmp_last[i] = dist[(N - 1) * P + i]; } TIMES(_, N * P) { relax(graph, dist); TIMES(i, P) { if (dist[(N - 1) * P + i] != tmp_last[i]) return make_pair(-1, -1); } } // Corner case: cannot start if (P == 1 && prohibited[0] == color_of[0]) return make_pair(0, 0); pair<int, Cost> res = search(start, graph, N, P, K); return res; } int main() { cin.tie(0); ios::sync_with_stdio(0); int N; while (cin >> N) { if (!N) break; pair<int, Cost> ans = solve(N); if (ans.first == -1) cout << -1 << endl; else cout << ans.first << ' ' << ans.second << endl; } return 0; }
//#define _GLIBCXX_DEBUG #include <algorithm> #include <cassert> #include <deque> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define FOREACH(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define TIMES(i, n) for (int i = 0; i < (n); ++i) typedef long long Cost; const long long INF = 1000 * 1000 * 10 * 10 * 2 * 10LL; struct Edge { int from, to; Cost cost; Edge() {} Edge(int f, int t, Cost c) : from(f), to(t), cost(c) {} }; struct DistTag { int size; deque<Cost> buf, newbie; DistTag(int size) : size(size), buf(size, INF) { // newbie.reserve(size); } bool insert(Cost val) { if (buf.back() <= val) return false; deque<Cost>::iterator ins_pos = upper_bound(newbie.begin(), newbie.end(), val); newbie.insert(ins_pos, val); if (static_cast<int>(newbie.size()) > size) newbie.pop_back(); return true; } bool has(Cost val) { return buf.back() >= val; } void flush() { FOREACH(it, newbie) { deque<Cost>::iterator ins_pos = upper_bound(buf.begin(), buf.end(), *it); buf.insert(ins_pos, *it); } buf.resize(size); newbie.clear(); } }; struct State { int pos; Cost cost; State() {} State(int p, Cost c) : pos(p), cost(c) {} bool operator<(const State &other) const { return cost > other.cost; // Reversed } }; typedef vector<Edge> EdgeList; void check_reachable(int start, const vector<EdgeList> &graph, vector<unsigned char> &reachable) { queue<int> q; q.push(start); reachable[start] = 1; while (!q.empty()) { int cur = q.front(); q.pop(); FOREACH(it, graph[cur]) { if (!reachable[it->to]) { reachable[it->to] = 1; q.push(it->to); } } } } bool relax(const vector<EdgeList> &graph, vector<Cost> &dist) { bool updated = false; FOREACH(list_it, graph) { FOREACH(it, *list_it) { const Cost cost = dist[it->from] + it->cost; if (dist[it->to] > cost) { dist[it->to] = cost; updated = true; } } } return updated; } pair<int, Cost> search(int start, const vector<EdgeList> &graph, int N, int P, int K) { vector<DistTag> dist(graph.size(), DistTag(K)); dist[start].insert(0); TIMES(_, static_cast<int>(graph.size()) * K) { TIMES(i, static_cast<int>(graph.size())) { DistTag &dt = dist[i]; if (dt.newbie.size() == 0) continue; vector<int> tmp; FOREACH(it, graph[i]) { assert(it->from == i); // Corner case: loop edge if (it->to == i) { FOREACH(nit, dt.newbie) { tmp.push_back(*nit + it->cost); } } else { FOREACH(nit, dt.newbie) { // cout << "insert " << i << ' ' << it->to << endl; dist[it->to].insert(*nit + it->cost); } } } dt.flush(); FOREACH(tit, tmp) { dt.insert(*tit); } } } pair<int, Cost> res(0, 0); vector<Cost> costs; TIMES(i, P) { FOREACH(it, dist[(N - 1) * P + i].buf) { costs.push_back(*it); } } sort(costs.begin(), costs.end()); TIMES(i, min(K, static_cast<int>(costs.size()))) { if (costs[i] == INF) break; res.first++; res.second += costs[i]; } return res; } pair<int, Cost> solve(int N) { vector<int> color_of(N); TIMES(i, N) { cin >> color_of[i]; --color_of[i]; } int M; cin >> M; vector<Edge> edges(M); TIMES(i, M) { Edge &e = edges[i]; cin >> e.from >> e.to >> e.cost; --e.from; --e.to; } int K; cin >> K; vector<int> prohibited; { string str; cin >> str; FOREACH(it, str) { prohibited.push_back(*it - '0' - 1); } } const int P = prohibited.size(); vector<vector<int>> next_prohibited_pos(P, vector<int>(4, 0)); { int shadow = 0; TIMES(i, P) { next_prohibited_pos[i][prohibited[i]] = i + 1; if (i > 0) { TIMES(j, 4) { if (j != prohibited[i]) next_prohibited_pos[i][j] = next_prohibited_pos[shadow][j]; } shadow = next_prohibited_pos[shadow][prohibited[i]]; } } } const int start = 0 + next_prohibited_pos[0][color_of[0]]; vector<EdgeList> graph(N * P); FOREACH(it, edges) { TIMES(i, P) { // Cannot reach this state. if (i > 0 && prohibited[i - 1] != color_of[it->from]) continue; int np = next_prohibited_pos[i][color_of[it->to]]; if (np < P) { graph[it->from * P + i].push_back( Edge(it->from * P + i, it->to * P + np, it->cost)); // cout << it->from << ' ' << i << ' ' << it->to << ' ' << np << endl; } } } // Remove all unreachable nodes. // It causes fake negative loop detection. vector<unsigned char> reachable(N * P, 0); check_reachable(start, graph, reachable); TIMES(i, N * P) { if (!reachable[i]) graph[i].clear(); } // Check if it has negative loop vector<Cost> dist(N * P, INF); dist[start] = 0; TIMES(_, N * P) { relax(graph, dist); } vector<Cost> tmp_last(P); TIMES(i, P) { tmp_last[i] = dist[(N - 1) * P + i]; } TIMES(_, N * P) { relax(graph, dist); TIMES(i, P) { if (dist[(N - 1) * P + i] != tmp_last[i]) return make_pair(-1, -1); } } // Corner case: cannot start if (P == 1 && prohibited[0] == color_of[0]) return make_pair(0, 0); pair<int, Cost> res = search(start, graph, N, P, K); return res; } int main() { cin.tie(0); ios::sync_with_stdio(0); int N; while (cin >> N) { if (!N) break; pair<int, Cost> ans = solve(N); if (ans.first == -1) cout << -1 << endl; else cout << ans.first << ' ' << ans.second << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 0, 148], ["-", 36, 36, 36, 36, 0, 30, 0, 58, 141, 22]]
1
1,623
function heron(a, b, c) { var s = (a + b + c) / 2; var S = Math.sqrt(s * (s - a) * (s - b) * (s - c)); return S; } var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); for (var i = 0; i < Arr.length; i++) { var ALX = Arr[i].split(" ").map(Number); var A = ALX[0]; var L = ALX[1]; var X = ALX[2]; var sum = 0; sum += heron(A, L, L); sum += (heron(A, (L + X) / 2, (L + X) / 2) * 2); console.log(sum.toFixed(10)); }
function heron(a, b, c) { var s = (a + b + c) / 2; var S = Math.sqrt(s * (s - a) * (s - b) * (s - c)); return S; } var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); for (var i = 0; i < Arr.length; i++) { var ALX = Arr[i].split(" ").map(Number); var A = ALX[0]; var L = ALX[1]; var X = ALX[2]; var sum = 0; sum += heron(A, L, L); sum += (heron(L, (L + X) / 2, (L + X) / 2) * 2); console.log(sum.toFixed(10)); }
[["-", 12, 23, 0, 16, 31, 2, 3, 3, 0, 22], ["+", 12, 23, 0, 16, 31, 2, 3, 3, 0, 22]]
2
211
#include <cmath> #include <iomanip> #include <iostream> using namespace std; long double f(int x, int y) { return x * sqrtl(y * y - x * x / 4) / 2; } int a, l, x; int main() { while (cin >> a >> l >> x) { cout << fixed << setprecision(20) << f(a, l) + f(l * 2, l + x) / 2 << endl; } }
#include <cmath> #include <iomanip> #include <iostream> using namespace std; long double f(int x, int y) { return x * sqrtl(y * y - x * x / 4.0L) / 2; } int a, l, x; int main() { while (cin >> a >> l >> x) { cout << fixed << setprecision(20) << f(a, l) + f(l * 2, l + x) / 2 << endl; } }
[["-", 12, 2, 3, 4, 0, 16, 12, 16, 12, 13], ["+", 12, 2, 3, 4, 0, 16, 12, 16, 12, 13]]
1
96
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; double calc(double a, double b, double c) { double s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } void func(double a, double l, double x) { double tmp = (l + x) / 2; printf("%.10lf\n", calc(a, l, l) + calc(tmp, tmp, l) * 2); } int main() { double a, l, x; while (scanf("%lf %lf %lf", a, l, x) != EOF) { func(a, l, x); } }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; double calc(double a, double b, double c) { double s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } void func(double a, double l, double x) { double tmp = (l + x) / 2; printf("%.10lf\n", calc(a, l, l) + calc(tmp, tmp, l) * 2); } int main() { double a, l, x; while (scanf("%lf %lf %lf", &a, &l, &x) != EOF) { func(a, l, x); } }
[["+", 51, 16, 31, 2, 3, 4, 0, 66, 17, 67]]
1
189
#!ruby -apl a,l,x=$F.map &:to_f k=2*l $_="%.8f"%[k*Math.sqrt(x*x+x*k)+a*Math.sqrt(k*k-a*a)]
#!ruby -apl a,l,x=$F.map &:to_f k=2*l $_="%.8f"%[(k*Math.sqrt(x*x+x*k)+a*Math.sqrt(k*k-a*a))/4]
[["-", 0, 493, 0, 662, 12, 771, 0, 557, 0, 6], ["+", 0, 493, 0, 662, 12, 771, 0, 557, 0, 6]]
4
25
import java.util.Arrays; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int S, D, M; static int[][] P, W; public static void main(String[] args) { while (sc.hasNext()) { S = sc.nextInt(); D = sc.nextInt(); M = sc.nextInt(); W = new int[S][]; P = new int[S][]; for (int i = 0; i < S; ++i) { int K = sc.nextInt(); W[i] = new int[K]; P[i] = new int[K]; for (int j = 0; j < K; ++j) { W[i][j] = sc.nextInt(); P[i][j] = sc.nextInt(); } } int[] dp = new int[M + 1]; Arrays.fill(dp, -1); dp[M] = 0; for (int i = 0; i < D; ++i) { int F = sc.nextInt(); for (int j = 0; j < P[F].length; ++j) { for (int k = 0; k <= M - P[i][j]; ++k) { if (dp[k + P[i][j]] == -1) continue; dp[k] = Math.max(dp[k], dp[k + P[i][j]] + W[i][j]); } } } int maxW = 0; int ansP = 0; for (int i = M; i >= 0; --i) { if (dp[i] > maxW) { maxW = dp[i]; ansP = M - i; } } System.out.println(maxW + " " + ansP); } } }
import java.util.Arrays; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int S, D, M; static int[][] P, W; public static void main(String[] args) { while (sc.hasNext()) { S = sc.nextInt(); D = sc.nextInt(); M = sc.nextInt(); W = new int[S][]; P = new int[S][]; for (int i = 0; i < S; ++i) { int K = sc.nextInt(); W[i] = new int[K]; P[i] = new int[K]; for (int j = 0; j < K; ++j) { W[i][j] = sc.nextInt(); P[i][j] = sc.nextInt(); } } int[] dp = new int[M + 1]; Arrays.fill(dp, -1); dp[M] = 0; for (int i = 0; i < D; ++i) { int F = sc.nextInt(); for (int j = 0; j < P[F].length; ++j) { for (int k = 0; k <= M - P[F][j]; ++k) { if (dp[k + P[F][j]] == -1) continue; dp[k] = Math.max(dp[k], dp[k + P[F][j]] + W[F][j]); } } } int maxW = 0; int ansP = 0; for (int i = M; i >= 0; --i) { if (dp[i] > maxW) { maxW = dp[i]; ansP = M - i; } } System.out.println(maxW + " " + ansP); } } }
[["-", 15, 16, 12, 16, 12, 504, 516, 504, 71, 22], ["+", 15, 16, 12, 16, 12, 504, 516, 504, 71, 22], ["-", 31, 504, 71, 16, 12, 504, 516, 504, 71, 22], ["+", 31, 504, 71, 16, 12, 504, 516, 504, 71, 22], ["-", 3, 4, 0, 16, 12, 504, 516, 504, 71, 22], ["+", 3, 4, 0, 16, 12, 504, 516, 504, 71, 22]]
3
427
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll dp[5555][301]; struct Okashi { ll e; ll c; }; void dbgprng(const vector<vector<Okashi>> &kind_of_okashi, ll s) { for (ll j = 0; j < s; j++) { for (ll i = 0; i < kind_of_okashi[j].size(); i++) { cout << "eff=" << kind_of_okashi[j][i].e << " cost=" << kind_of_okashi[j][i].c << endl; } cout << "===" << endl; } } int main(void) { ios::sync_with_stdio(false); cin.tie(0); ll s, d, m; while (cin >> s >> d >> m) { vector<Okashi> all_okashi; vector<vector<Okashi>> kind_of_okashi(s); for (ll i = 0; i < s; i++) { ll k; cin >> k; vector<Okashi> tmp(k); for (ll j = 0; j < k; j++) { ll w, p; cin >> w >> p; tmp[j].e = w; tmp[j].c = p; } kind_of_okashi[i] = tmp; } for (ll i = 0; i < d; i++) { ll f; cin >> f; for (ll j = 0; j < kind_of_okashi[f].size(); j++) { all_okashi.push_back(kind_of_okashi[f][j]); } } for (ll i = 0; i < 5555; i++) for (ll j = 0; j < 301; j++) dp[i][j] = 0; ll size = all_okashi.size(); for (ll i = 0; i < size; i++) { for (ll j = 0; j <= m; j++) { if (j - all_okashi[i].c < 0) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i][j - all_okashi[i].c] + all_okashi[i].e); } } } ll cost; ll org = dp[size][m]; for (ll i = m; i >= 0; i--) { if (dp[size][i] != org) { cost = i + 1; break; } } cout << dp[size][m] << " " << cost << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll dp[5555][301]; struct Okashi { ll e; ll c; }; void dbgprng(const vector<vector<Okashi>> &kind_of_okashi, ll s) { for (ll j = 0; j < s; j++) { for (ll i = 0; i < kind_of_okashi[j].size(); i++) { cout << "eff=" << kind_of_okashi[j][i].e << " cost=" << kind_of_okashi[j][i].c << endl; } cout << "===" << endl; } } int main(void) { ios::sync_with_stdio(false); cin.tie(0); ll s, d, m; while (cin >> s >> d >> m) { vector<Okashi> all_okashi; vector<vector<Okashi>> kind_of_okashi(s); for (ll i = 0; i < s; i++) { ll k; cin >> k; vector<Okashi> tmp(k); for (ll j = 0; j < k; j++) { ll w, p; cin >> w >> p; tmp[j].e = w; tmp[j].c = p; } kind_of_okashi[i] = tmp; } for (ll i = 0; i < d; i++) { ll f; cin >> f; for (ll j = 0; j < kind_of_okashi[f].size(); j++) { all_okashi.push_back(kind_of_okashi[f][j]); } } for (ll i = 0; i < 5555; i++) for (ll j = 0; j < 301; j++) dp[i][j] = 0; ll size = all_okashi.size(); for (ll i = 0; i < size; i++) { for (ll j = 0; j <= m; j++) { if (j - all_okashi[i].c < 0) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i][j - all_okashi[i].c] + all_okashi[i].e); } } } ll cost = 0; ll org = dp[size][m]; for (ll i = m; i >= 0; i--) { if (dp[size][i] != org) { cost = i + 1; break; } } cout << dp[size][m] << " " << cost << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
561
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; using ll = long long; #define all(c) (c).begin(), (c).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second const ll INF = 1e9; const ll MOD = 1e9 + 7; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using P = pair<int, int>; int s, d, m; int f[101]; int dp1[102][302]; int cost[102][302]; int dfs1(int idx, int p) { // printf("%d %d\n", idx, p); if (idx == d) return 0; if (dp1[idx][p] != -INF) return dp1[idx][p]; int res = 0; rep(i, p + 1) { res = max(res, dfs1(idx + 1, p - i) + cost[f[idx]][i]); } return dp1[idx][p] = res; } int main() { while (cin >> s >> d >> m && s) { rep(i, 102) rep(j, 302) dp1[i][j] = -INF; rep(i, 102) rep(j, 302) cost[i][j] = 0; rep(i, s) { int k; cin >> k; vector<int> ws, ps; rep(j, k) { int w, p; cin >> w >> p; ws.push_back(w); ps.push_back(p); } rep(a, k) for (int j = m + 1; j >= ps[a]; j--) { cost[a][j] = max(cost[a][j], cost[a][j - ps[a]] + ws[a]); } } rep(i, d) cin >> f[i]; int ans1 = dfs1(0, m); int ans2 = 0; rep(i, m + 1) { if (ans1 == dfs1(0, i)) { cout << ans1 << " " << i << endl; break; } } } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; using ll = long long; #define all(c) (c).begin(), (c).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second const ll INF = 1e9; const ll MOD = 1e9 + 7; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using P = pair<int, int>; int s, d, m; int f[101]; int dp1[102][302]; int cost[102][302]; int dfs1(int idx, int p) { // printf("%d %d\n", idx, p); if (idx == d) return 0; if (dp1[idx][p] != -INF) return dp1[idx][p]; int res = 0; rep(i, p + 1) { res = max(res, dfs1(idx + 1, p - i) + cost[f[idx]][i]); } return dp1[idx][p] = res; } int main() { while (cin >> s >> d >> m && s) { rep(i, 102) rep(j, 302) dp1[i][j] = -INF; rep(i, 102) rep(j, 302) cost[i][j] = 0; rep(i, s) { int k; cin >> k; vector<int> ws, ps; rep(j, k) { int w, p; cin >> w >> p; ws.push_back(w); ps.push_back(p); } rep(a, k) for (int j = m + 1; j >= ps[a]; j--) { cost[i][j] = max(cost[i][j], cost[i][j - ps[a]] + ws[a]); } } rep(i, d) cin >> f[i]; int ans1 = dfs1(0, m); int ans2 = 0; rep(i, m + 1) { if (ans1 == dfs1(0, i)) { cout << ans1 << " " << i << endl; break; } } } return 0; }
[["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22]]
1
512
#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" > "d:\d_download\visual ///studio 2015\projects\programing_contest_c++\debug\b.txt" int main() { int S, D, M; while (cin >> S >> D >> M) { map<pair<int, int>, int> nums; vector<vector<pair<int, int>>> sws(M); for (int i = 0; i < S; ++i) { int k; cin >> k; vector<pair<int, int>> sw(k); for (int j = 0; j < k; ++j) { int w, p; cin >> w >> p; sw[j] = make_pair(w, p); } sws[i] = sw; } map<int, int> mp; for (int i = 0; i < D; ++i) { int f; cin >> f; mp[f]++; } for (int i = 0; i < S; ++i) { for (int j = 0; j < sws[i].size(); ++j) { nums[sws[i][j]] += mp[i]; } } vector<vector<int>> memo(nums.size() + 1, vector<int>(M + 1, -1e8)); memo[0][0] = 0; int aa = 0; for (auto num : nums) { for (int nweight = 0; nweight <= M; ++nweight) { for (int i = 0; i <= num.second; ++i) { const int nextweight = nweight + num.first.second * i; const int nextvalue = memo[aa][nweight] + num.first.first * i; if (nextweight <= M) { memo[aa + 1][nextweight] = max(memo[aa + 1][nextweight], nextvalue); } } } aa++; } int ans = 0; int ans2 = 0; for (int i = 0; i <= M; ++i) { if (ans < memo[nums.size()][i]) { ans = memo[nums.size()][i]; ans2 = i; } } cout << ans << " " << ans2 << 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" > "d:\d_download\visual ///studio 2015\projects\programing_contest_c++\debug\b.txt" int main() { int S, D, M; while (cin >> S >> D >> M) { map<pair<int, int>, int> nums; vector<vector<pair<int, int>>> sws(S); for (int i = 0; i < S; ++i) { int k; cin >> k; vector<pair<int, int>> sw(k); for (int j = 0; j < k; ++j) { int w, p; cin >> w >> p; sw[j] = make_pair(w, p); } sws[i] = sw; } map<int, int> mp; for (int i = 0; i < D; ++i) { int f; cin >> f; mp[f]++; } for (int i = 0; i < S; ++i) { for (int j = 0; j < sws[i].size(); ++j) { nums[sws[i][j]] += mp[i]; } } vector<vector<int>> memo(nums.size() + 1, vector<int>(M + 1, -1e8)); memo[0][0] = 0; int aa = 0; for (auto num : nums) { for (int nweight = 0; nweight <= M; ++nweight) { for (int i = 0; i <= num.second; ++i) { const int nextweight = nweight + num.first.second * i; const int nextvalue = memo[aa][nweight] + num.first.first * i; if (nextweight <= M) { memo[aa + 1][nextweight] = max(memo[aa + 1][nextweight], nextvalue); } else { break; } } } aa++; } int ans = 0; int ans2 = 0; for (int i = 0; i <= M; ++i) { if (ans < memo[nums.size()][i]) { ans = memo[nums.size()][i]; ans2 = i; } } cout << ans << " " << ans2 << endl; } return 0; }
[["-", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46], ["+", 0, 7, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 8, 9, 0, 57, 75, 76, 0, 9, 0, 45], ["+", 0, 57, 75, 76, 0, 9, 0, 93, 0, 94]]
1
510
#include <bits/stdc++.h> using namespace std; #define mk make_pair main() { int s, d, m; while (cin >> s >> d >> m) { int dp[5001][101] = {}, pp = 0, ppp = 100000000, k, w, p, cc; vector<pair<int, int>> a[101], c; for (int i = 0; i < s; i++) { cin >> k; for (int j = 0; j < k; j++) { cin >> w >> p; a[i].push_back(mk(p, w)); } } for (int i = 0; i < d; i++) { cin >> cc; for (int j = 0; j < a[cc].size(); j++) c.push_back(a[cc][j]); } for (int i = 1; i <= c.size(); i++) for (int j = 0; j <= m; j++) { if (j < c[i - 1].first) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j - c[i - 1].first] + c[i - 1].second, dp[i - 1][j]); pp = max(pp, dp[i][j]); } for (int i = 0; i <= c.size(); i++) for (int j = 0; j <= m; j++) if (pp == dp[i][j]) ppp = min(ppp, j); cout << pp << ' ' << ppp << endl; } }
#include <bits/stdc++.h> using namespace std; #define mk make_pair main() { int s, d, m; while (cin >> s >> d >> m) { int dp[5001][301] = {}, pp = 0, ppp = 100000000, k, w, p, cc; vector<pair<int, int>> a[101], c; for (int i = 0; i < s; i++) { cin >> k; for (int j = 0; j < k; j++) { cin >> w >> p; a[i].push_back(mk(p, w)); } } for (int i = 0; i < d; i++) { cin >> cc; for (int j = 0; j < a[cc].size(); j++) c.push_back(a[cc][j]); } for (int i = 1; i <= c.size(); i++) for (int j = 0; j <= m; j++) { if (j < c[i - 1].first) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j - c[i - 1].first] + c[i - 1].second, dp[i - 1][j]); pp = max(pp, dp[i][j]); } for (int i = 0; i <= c.size(); i++) for (int j = 0; j <= m; j++) if (pp == dp[i][j]) ppp = min(ppp, j); cout << pp << ' ' << ppp << endl; } }
[["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13]]
1
383
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { int value, cost; }; Info info[100][50]; int item_num[100]; int dp[301]; int main() { int S, D, M; while (scanf("%d %d %d", &S, &D, &M) != EOF) { for (int i = 0; i < S; i++) { scanf("%d", &item_num[i]); for (int k = 0; k < item_num[i]; k++) { scanf("%d %d", &info[i][k].value, &info[i][k].cost); } } dp[0] = 0; for (int i = 1; i <= M; i++) dp[i] = -1; int tmp; for (int loop = 0; loop < D; loop++) { scanf("%d", &tmp); for (int k = 0; k < item_num[tmp]; k++) { for (int budget = M; budget - info[tmp][k].cost >= 0; budget--) { if (dp[budget - info[tmp][k].cost] >= 0) { dp[budget] = max(dp[budget], dp[budget - info[tmp][k].cost] + info[tmp][k].value); } } } } int max_effect = 0, min_budget; for (int budget = 0; budget <= M; budget++) { if (max_effect < dp[budget]) { max_effect = dp[budget]; min_budget = budget; } } printf("%d %d\n", max_effect, min_budget); } return 0; }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { int value, cost; }; Info info[100][50]; int item_num[100]; int dp[301]; int main() { int S, D, M; while (scanf("%d %d %d", &S, &D, &M) != EOF) { for (int i = 0; i < S; i++) { scanf("%d", &item_num[i]); for (int k = 0; k < item_num[i]; k++) { scanf("%d %d", &info[i][k].value, &info[i][k].cost); } } dp[0] = 0; for (int i = 1; i <= M; i++) dp[i] = -1; int tmp; for (int loop = 0; loop < D; loop++) { scanf("%d", &tmp); for (int k = 0; k < item_num[tmp]; k++) { for (int budget = M; budget - info[tmp][k].cost >= 0; budget--) { if (dp[budget - info[tmp][k].cost] >= 0) { dp[budget] = max(dp[budget], dp[budget - info[tmp][k].cost] + info[tmp][k].value); } } } } int max_effect = -1, min_budget; for (int budget = 0; budget <= M; budget++) { if (max_effect < dp[budget]) { max_effect = dp[budget]; min_budget = budget; } } printf("%d %d\n", max_effect, min_budget); } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
416
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { int value, cost; }; Info info[100][50]; int item_num[100], sell_count[100]; int dp[301], next_dp[301]; int main() { int S, D, M; while (scanf("%d %d %d", &S, &D, &M) != EOF) { for (int i = 0; i < S; i++) sell_count[i] = 0; for (int i = 0; i < S; i++) { scanf("%d", &item_num[i]); for (int k = 0; k < item_num[i]; k++) { scanf("%d %d", &info[i][k].value, &info[i][k].cost); } } int tmp; for (int i = 0; i < D; i++) { scanf("%d", &tmp); sell_count[tmp]++; } dp[0] = 0; next_dp[0] = 0; for (int i = 1; i <= M; i++) { dp[i] = -1; next_dp[i] = -1; } for (int i = 0; i < S; i++) { if (sell_count[i] == 0) continue; for (int k = 0; k < item_num[i]; k++) { for (int num = 1; num <= sell_count[i]; num++) { for (int budget = M; budget - num * info[i][k].cost >= 0; budget--) { if (dp[budget - num * info[i][k].cost] >= 0) { next_dp[budget] = max(next_dp[budget], dp[budget - num * info[i][k].cost] + num * info[i][k].value); } } } for (int budget = 0; budget <= M; budget++) dp[budget] = next_dp[budget]; } } int max_effect = 0, min_budget; for (int budget = 0; budget <= M; budget++) { if (max_effect < dp[budget]) { max_effect = dp[budget]; min_budget = budget; } } printf("%d %d\n", max_effect, min_budget); } return 0; }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { int value, cost; }; Info info[100][50]; int item_num[100], sell_count[100]; int dp[301], next_dp[301]; int main() { int S, D, M; while (scanf("%d %d %d", &S, &D, &M) != EOF) { for (int i = 0; i < S; i++) sell_count[i] = 0; for (int i = 0; i < S; i++) { scanf("%d", &item_num[i]); for (int k = 0; k < item_num[i]; k++) { scanf("%d %d", &info[i][k].value, &info[i][k].cost); } } int tmp; for (int i = 0; i < D; i++) { scanf("%d", &tmp); sell_count[tmp]++; } dp[0] = 0; next_dp[0] = 0; for (int i = 1; i <= M; i++) { dp[i] = -1; next_dp[i] = -1; } for (int i = 0; i < S; i++) { if (sell_count[i] == 0) continue; for (int k = 0; k < item_num[i]; k++) { for (int num = 1; num <= sell_count[i]; num++) { for (int budget = M; budget - num * info[i][k].cost >= 0; budget--) { if (dp[budget - num * info[i][k].cost] >= 0) { next_dp[budget] = max(next_dp[budget], dp[budget - num * info[i][k].cost] + num * info[i][k].value); } } } for (int budget = 0; budget <= M; budget++) dp[budget] = next_dp[budget]; } } int max_effect = -1, min_budget; for (int budget = 0; budget <= M; budget++) { if (max_effect < dp[budget]) { max_effect = dp[budget]; min_budget = budget; } } printf("%d %d\n", max_effect, min_budget); } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
547
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { int value, cost; }; Info info[100][50]; int item_num[100], sell_count[100]; int dp[301], next_dp[301]; int main() { int S, D, M; while (scanf("%d %d %d", &S, &D, &M) != EOF) { for (int i = 0; i < S; i++) sell_count[i] = 0; for (int i = 0; i < S; i++) { scanf("%d", &item_num[i]); for (int k = 0; k < item_num[i]; k++) { scanf("%d %d", &info[i][k].value, &info[i][k].cost); } } int tmp; for (int i = 0; i < D; i++) { scanf("%d", &tmp); sell_count[tmp]++; } dp[0] = 0; next_dp[0] = 0; for (int i = 1; i <= M; i++) { dp[i] = -1; next_dp[i] = -1; } vector<int> Updated; for (int i = 0; i < S; i++) { if (sell_count[i] == 0) continue; for (int k = 0; k < item_num[i]; k++) { for (int num = 1; num <= sell_count[i]; num++) { for (int budget = M; budget - num * info[i][k].cost >= 0; budget--) { if (dp[budget - num * info[i][k].cost] >= 0) { if (next_dp[budget] < dp[budget - num * info[i][k].cost] + num * info[i][k].value) { next_dp[budget] = dp[budget - num * info[i][k].cost] + num * info[i][k].value; Updated.push_back(budget); } } } } for (int p = 0; p < Updated.size(); p++) { dp[Updated[p]] = next_dp[Updated[p]]; } Updated.clear(); } } int max_effect = 0, min_budget; for (int budget = 0; budget <= M; budget++) { if (max_effect < dp[budget]) { max_effect = dp[budget]; min_budget = budget; } } printf("%d %d\n", max_effect, min_budget); } return 0; }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { int value, cost; }; Info info[100][50]; int item_num[100], sell_count[100]; int dp[301], next_dp[301]; int main() { int S, D, M; while (scanf("%d %d %d", &S, &D, &M) != EOF) { for (int i = 0; i < S; i++) sell_count[i] = 0; for (int i = 0; i < S; i++) { scanf("%d", &item_num[i]); for (int k = 0; k < item_num[i]; k++) { scanf("%d %d", &info[i][k].value, &info[i][k].cost); } } int tmp; for (int i = 0; i < D; i++) { scanf("%d", &tmp); sell_count[tmp]++; } dp[0] = 0; next_dp[0] = 0; for (int i = 1; i <= M; i++) { dp[i] = -1; next_dp[i] = -1; } vector<int> Updated; for (int i = 0; i < S; i++) { if (sell_count[i] == 0) continue; for (int k = 0; k < item_num[i]; k++) { for (int num = 1; num <= sell_count[i]; num++) { for (int budget = M; budget - num * info[i][k].cost >= 0; budget--) { if (dp[budget - num * info[i][k].cost] >= 0) { if (next_dp[budget] < dp[budget - num * info[i][k].cost] + num * info[i][k].value) { next_dp[budget] = dp[budget - num * info[i][k].cost] + num * info[i][k].value; Updated.push_back(budget); } } } } for (int p = 0; p < Updated.size(); p++) { dp[Updated[p]] = next_dp[Updated[p]]; } Updated.clear(); } } int max_effect = -1, min_budget; for (int budget = 0; budget <= M; budget++) { if (max_effect < dp[budget]) { max_effect = dp[budget]; min_budget = budget; } } printf("%d %d\n", max_effect, min_budget); } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
608
#include <cstdlib> #include <cstring> #include <iostream> using namespace std; int s, d, m; int k[101]; int w[101][51]; int p[101][51]; int f[101]; int dp[5001][301]; int main() { while (cin >> s >> d >> m) { memset(w, 0, sizeof(w)); memset(p, 0, sizeof(p)); memset(f, 0, sizeof(f)); memset(dp, -1, sizeof(dp)); for (int i = 0; i < s; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { cin >> w[i][j]; cin >> p[i][j]; } } for (int i = 0; i < d; i++) { cin >> f[i]; } dp[0][0] = 0; int cnt = 0; for (int i = 0; i < d; i++) { for (int l = 0; l < k[f[i]]; l++) { for (int j = 0; j <= m; j++) { if (j + p[f[i]][l] <= m && dp[cnt][j] != -1) { dp[cnt + 1][j + p[f[i]][l]] = max(dp[cnt + 1][j + p[f[i]][l]], dp[cnt][j] + w[f[i]][l]); } dp[cnt + 1][j] = max(dp[cnt + 1][j], dp[cnt][j]); } cnt++; } } int maxw = 0; int minp = 1 << 29; for (int i = 0; i <= m; i++) { if (maxw < dp[cnt][i]) { maxw = dp[cnt][i]; minp = i; } } cout << maxw << " " << minp << endl; } }
#include <cstdlib> #include <cstring> #include <iostream> using namespace std; int s, d, m; int k[101]; int w[101][51]; int p[101][51]; int f[101]; int dp[5001][301]; int main() { while (cin >> s >> d >> m) { memset(w, 0, sizeof(w)); memset(p, 0, sizeof(p)); memset(f, 0, sizeof(f)); memset(dp, -1, sizeof(dp)); for (int i = 0; i < s; i++) { cin >> k[i]; for (int j = 0; j < k[i]; j++) { cin >> w[i][j]; cin >> p[i][j]; } } for (int i = 0; i < d; i++) { cin >> f[i]; } dp[0][0] = 0; int cnt = 0; for (int i = 0; i < d; i++) { for (int l = 0; l < k[f[i]]; l++) { for (int j = 0; j <= m; j++) { if (j + p[f[i]][l] <= m && dp[cnt][j] != -1) { dp[cnt + 1][j + p[f[i]][l]] = max(dp[cnt + 1][j + p[f[i]][l]], dp[cnt][j] + w[f[i]][l]); } dp[cnt + 1][j] = max(dp[cnt + 1][j], dp[cnt][j]); } cnt++; } } int maxw = 0; int minp = 0; for (int i = 0; i <= m; i++) { if (maxw < dp[cnt][i]) { maxw = dp[cnt][i]; minp = i; } } cout << maxw << " " << minp << endl; } }
[["-", 8, 9, 0, 43, 49, 50, 51, 16, 31, 13], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 17, 151], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
471
#include <algorithm> #include <cstdio> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; void knapsack(int yosan, const vector<pair<int, int>> &a) { int n = a.size(); int dp[101] = {}; rep(i, n) { int value = a[i].first, price = a[i].second; for (int j = yosan; j >= price; j--) dp[j] = max(dp[j], dp[j - price] + value); } int ans = max_element(dp, dp + yosan + 1) - dp; printf("%d %d\n", dp[ans], ans); } int main() { for (int n, days, yosan; ~scanf("%d%d%d", &n, &days, &yosan);) { vector<pair<int, int>> kanmi[100]; rep(i, n) { int m; scanf("%d", &m); kanmi[i].resize(m); rep(j, m) scanf("%d%d", &kanmi[i][j].first, &kanmi[i][j].second); } vector<pair<int, int>> item; rep(t, days) { int id; scanf("%d", &id); item.insert(item.end(), kanmi[id].begin(), kanmi[id].end()); } knapsack(yosan, item); } return 0; }
#include <algorithm> #include <cstdio> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; void knapsack(int yosan, const vector<pair<int, int>> &a) { int n = a.size(); int dp[301] = {}; rep(i, n) { int value = a[i].first, price = a[i].second; for (int j = yosan; j >= price; j--) dp[j] = max(dp[j], dp[j - price] + value); } int ans = max_element(dp, dp + yosan + 1) - dp; printf("%d %d\n", dp[ans], ans); } int main() { for (int n, days, yosan; ~scanf("%d%d%d", &n, &days, &yosan);) { vector<pair<int, int>> kanmi[100]; rep(i, n) { int m; scanf("%d", &m); kanmi[i].resize(m); rep(j, m) scanf("%d%d", &kanmi[i][j].first, &kanmi[i][j].second); } vector<pair<int, int>> item; rep(t, days) { int id; scanf("%d", &id); item.insert(item.end(), kanmi[id].begin(), kanmi[id].end()); } knapsack(yosan, item); } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13]]
1
338
#include <iostream> #include <vector> using namespace std; const int INF = 1 << 28; int main() { int s, d, m; vector<int> w[100], p[100]; int dp[301]; while (cin >> s >> d >> m) { for (int i = 0; i < s; i++) { int k; cin >> k; w[i].resize(k), p[i].resize(k); for (int j = 0; j < k; j++) { cin >> w[i][j] >> p[i][j]; } } fill_n(dp, m, 0); for (int i = 0; i < d; i++) { int f; cin >> f; for (int j = 0; j < w[f].size(); j++) { for (int k = m; k - p[f][j] >= 0; k--) { dp[k] = max(dp[k], dp[k - p[f][j]] + w[f][j]); } } } int pos = 0; for (int i = 0; i <= m; i++) { if (dp[i] > dp[pos]) pos = i; } cout << dp[pos] << " " << pos << endl; } }
#include <iostream> #include <vector> using namespace std; const int INF = 1 << 28; int main() { int s, d, m; vector<int> w[100], p[100]; int dp[301]; while (cin >> s >> d >> m) { for (int i = 0; i < s; i++) { int k; cin >> k; w[i].resize(k), p[i].resize(k); for (int j = 0; j < k; j++) { cin >> w[i][j] >> p[i][j]; } } fill_n(dp, m + 1, 0); for (int i = 0; i < d; i++) { int f; cin >> f; for (int j = 0; j < w[f].size(); j++) { for (int k = m; k - p[f][j] >= 0; k--) { dp[k] = max(dp[k], dp[k - p[f][j]] + w[f][j]); } } } int pos = 0; for (int i = 0; i <= m; i++) { if (dp[i] > dp[pos]) pos = i; } cout << dp[pos] << " " << pos << endl; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
303
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef pair<int, int> pii; int dp1[50][301], dp2[101][301]; int main(void) { int s, d, m; while (cin >> s >> d >> m) { vector<pii> a[s]; for (int i = 0; i < s; i++) { int w, p, k; cin >> k; for (int j = 0; j < k; j++) { cin >> w >> p; a[i].push_back(pii(p, w)); } } vector<int> f(d); for (int i = 0; i < d; i++) cin >> f[i]; fill(dp1[0], dp1[50], 0); for (int i = 0; i < s; i++) { for (int j = 0; j < a[i].size(); j++) { for (int k = 300; k >= 0; k--) { if (k + a[i][j].first <= 300) { dp1[i][k + a[i][j].first] = max(dp1[i][k + a[i][j].first], dp1[i][k] + a[i][j].second); } } } } fill(dp2[0], dp2[101], 0); for (int i = 0; i < d; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (j + k <= 300) dp2[i + 1][j + k] = max(dp2[i + 1][j + k], dp2[i][j] + dp1[f[i]][k]); } } } int minp = (1 << 29), maxw = 0; for (int j = 0; j <= m; j++) { for (int i = 0; i <= d; i++) { if (maxw < dp2[i][j]) { maxw = dp2[i][j]; minp = j; } } } cout << maxw << " " << minp << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef pair<int, int> pii; int dp1[101][301], dp2[101][301]; int main(void) { int s, d, m; while (cin >> s >> d >> m) { vector<pii> a[s]; for (int i = 0; i < s; i++) { int w, p, k; cin >> k; for (int j = 0; j < k; j++) { cin >> w >> p; a[i].push_back(pii(p, w)); } } vector<int> f(d); for (int i = 0; i < d; i++) cin >> f[i]; fill(dp1[0], dp1[101], 0); for (int i = 0; i < s; i++) { for (int j = 0; j < a[i].size(); j++) { for (int k = 300; k >= 0; k--) { if (k + a[i][j].first <= 300) { dp1[i][k + a[i][j].first] = max(dp1[i][k + a[i][j].first], dp1[i][k] + a[i][j].second); } } } } fill(dp2[0], dp2[101], 0); for (int i = 0; i < d; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { if (j + k <= 300) dp2[i + 1][j + k] = max(dp2[i + 1][j + k], dp2[i][j] + dp1[f[i]][k]); } } } int minp = (1 << 29), maxw = -1; for (int j = 0; j <= m; j++) { for (int i = 0; i <= d; i++) { if (maxw < dp2[i][j]) { maxw = dp2[i][j]; minp = j; } } } cout << maxw << " " << minp << endl; } return 0; }
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 0, 2, 3, 4, 0, 69, 341, 342, 0, 13], ["+", 0, 2, 3, 4, 0, 69, 341, 342, 0, 13], ["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
515
// AOJ 1093: KND Runs for Sweets // 2017.11.16 bal4u@uu #include <math.h> #include <stdio.h> #include <stdlib.h> #define KAI 25 int x[101], y[101], v[101]; int n; int max; double fx(double a, double b) { int i; double d, max; max = 0; for (i = 0; i < n; i++) { d = hypot(a - x[i], b - y[i]) / v[i]; if (d > max) max = d; } return max; } double srchy(double x) { int i; double l, r, a, b; l = 0, r = max; i = KAI; while (i--) { a = (2 * l + r) / 3; b = (l + 2 * r) / 3; if (fx(x, a) < fx(x, b)) r = b; else l = a; } return fx(x, (l + r) / 2); } double srch3() { int i; double l, r, a, b; l = 0, r = max; i = KAI; while (i--) { a = (2 * l + r) / 3; b = (l + 2 * r) / 3; if (srchy(a) < srchy(b)) r = b; else l = a; } return srchy((l + r) / 2); } int main() { int i; while (scanf("%d", &n) && n > 0) { max = 0; for (i = 0; i < n; i++) { scanf("%d%d%d", x + i, y + i, v + i); if (x[i] > max) max = x[i]; if (y[i] > max) max = y[i]; } printf("%.8f\n", srch3()); } return 0; }
// AOJ 1093: KND Runs for Sweets // 2017.11.16 bal4u@uu #include <math.h> #include <stdio.h> #include <stdlib.h> #define KAI 35 int x[101], y[101], v[101]; int n; int max; double fx(double a, double b) { int i; double d, max; max = 0; for (i = 0; i < n; i++) { d = hypot(a - x[i], b - y[i]) / v[i]; if (d > max) max = d; } return max; } double srchy(double x) { int i; double l, r, a, b; l = 0, r = max; i = KAI; while (i--) { a = (2 * l + r) / 3; b = (l + 2 * r) / 3; if (fx(x, a) < fx(x, b)) r = b; else l = a; } return fx(x, (l + r) / 2); } double srch3() { int i; double l, r, a, b; l = 0, r = max; i = KAI; while (i--) { a = (2 * l + r) / 3; b = (l + 2 * r) / 3; if (srchy(a) < srchy(b)) r = b; else l = a; } return srchy((l + r) / 2); } int main() { int i; while (scanf("%d", &n) && n > 0) { max = 0; for (i = 0; i < n; i++) { scanf("%d%d%d", x + i, y + i, v + i); if (x[i] > max) max = x[i]; if (y[i] > max) max = y[i]; } printf("%.8f\n", srch3()); } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
0
416
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; double EPS = 1e-10; double add(double a, double b) { if (abs(a + b) < EPS * (abs(a) + abs(b))) return 0; return a + b; } struct point { double x, y; point() {} point(double x, double y) : x(x), y(y) {} point operator+(point p) { return point(add(x, p.x), add(y, p.y)); } point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); } point operator*(double d) { return point(x * d, y * d); } point operator/(double d) { return point(x / d, y / d); } }; double dist(point a, point b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); } int main(void) { int n, x, y, v; vector<point> P; vector<double> V; while (cin >> n, n) { P.clear(); V.clear(); for (int i = 0; i < n; i++) { cin >> x >> y >> v; P.push_back(point(x, y)); V.push_back(v); } double r = 0.90; point now(0, 0); for (double d = 10; d > EPS; d *= r) { int mx = 0; double mxd = 0; for (int j = 0; j < P.size(); j++) if (dist(now, P[j]) / V[j] > mxd) mx = j, mxd = dist(now, P[j]) / V[j]; now = now + ((P[mx] - now) / dist(P[mx], now) * d); } double ans = 0; for (int i = 0; i < P.size(); i++) { ans = max(ans, dist(now, P[i]) / V[i]); } printf("%.8f\n", ans); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; double EPS = 1e-8; double add(double a, double b) { if (abs(a + b) < EPS * (abs(a) + abs(b))) return 0; return a + b; } struct point { double x, y; point() {} point(double x, double y) : x(x), y(y) {} point operator+(point p) { return point(add(x, p.x), add(y, p.y)); } point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); } point operator*(double d) { return point(x * d, y * d); } point operator/(double d) { return point(x / d, y / d); } }; double dist(point a, point b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); } int main(void) { int n, x, y, v; vector<point> P; vector<double> V; while (cin >> n, n) { P.clear(); V.clear(); for (int i = 0; i < n; i++) { cin >> x >> y >> v; P.push_back(point(x, y)); V.push_back(v); } double r = 0.98; point now(0, 0); for (double d = 10; d > EPS; d *= r) { int mx = 0; double mxd = 0; for (int j = 0; j < P.size(); j++) if (dist(now, P[j]) / V[j] > mxd) mx = j, mxd = dist(now, P[j]) / V[j]; now = now + ((P[mx] - now) / dist(P[mx], now) * d); } double ans = 0; for (int i = 0; i < P.size(); i++) { ans = max(ans, dist(now, P[i]) / V[i]); } printf("%.8f\n", ans); } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
521
#include <complex> #include <iomanip> #include <iostream> #include <vector> using namespace std; typedef complex<double> Point; bool intersect(Point p1, double r1, Point p2, double r2) { double d = abs(p1 - p2); if (d < abs(r1 - r2)) return false; if (d > r1 + r2) return false; return true; } Point crosspoint(Point p1, double r1, Point p2, double r2) { double c = (r1 * r1 + norm(p1 - p2) - r2 * r2) / (2.0 * r1 * abs(p1 - p2)); return p1 + (p2 - p1) / abs(p1 - p2) * r1 * Point(c, sqrt(1.0 - c * c)); } int n; double x[109], y[109], v[109]; int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> v[i]; double l = 0.0, r = 2.0; for (int i = 0; i < 50; i++) { double m = (l + r) * 0.5; bool flag = false; for (int j = 0; j < n; j++) { bool ok = true; for (int k = 0; k < n; k++) { if (abs(Point(x[k] - x[j], y[k] - y[j])) > v[k] * m + 1.0e-7) ok = false; } if (ok) flag = true; } for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { if (intersect(Point(x[j], y[j]), v[j] * m, Point(x[k], y[k]), v[k] * m)) { Point p1 = crosspoint(Point(x[j], y[j]), v[j] * m, Point(x[k], y[k]), v[k] * m); Point p2 = crosspoint(Point(x[k], y[k]), v[k] * m, Point(x[j], y[j]), v[j] * m); bool ok1 = true, ok2 = true; for (int l = 0; l < n; l++) { if (abs(Point(x[l], y[l]) - p1) > v[l] * m + 1.0e-7) ok1 = false; if (abs(Point(x[l], y[l]) - p2) > v[l] * m + 1.0e-7) ok2 = false; } if (ok1 || ok2) flag = true; } } } if (flag) r = m; else l = m; } cout << fixed << setprecision(10) << l << endl; } return 0; }
#include <complex> #include <iomanip> #include <iostream> #include <vector> using namespace std; typedef complex<double> Point; bool intersect(Point p1, double r1, Point p2, double r2) { double d = abs(p1 - p2); if (d < abs(r1 - r2)) return false; if (d > r1 + r2) return false; return true; } Point crosspoint(Point p1, double r1, Point p2, double r2) { double c = (r1 * r1 + norm(p1 - p2) - r2 * r2) / (2.0 * r1 * abs(p1 - p2)); return p1 + (p2 - p1) / abs(p1 - p2) * r1 * Point(c, sqrt(1.0 - c * c)); } int n; double x[109], y[109], v[109]; int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> v[i]; double l = 0.0, r = 1000.0; for (int i = 0; i < 50; i++) { double m = (l + r) * 0.5; bool flag = false; for (int j = 0; j < n; j++) { bool ok = true; for (int k = 0; k < n; k++) { if (abs(Point(x[k] - x[j], y[k] - y[j])) > v[k] * m + 1.0e-7) ok = false; } if (ok) flag = true; } for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { if (intersect(Point(x[j], y[j]), v[j] * m, Point(x[k], y[k]), v[k] * m)) { Point p1 = crosspoint(Point(x[j], y[j]), v[j] * m, Point(x[k], y[k]), v[k] * m); Point p2 = crosspoint(Point(x[k], y[k]), v[k] * m, Point(x[j], y[j]), v[j] * m); bool ok1 = true, ok2 = true; for (int l = 0; l < n; l++) { if (abs(Point(x[l], y[l]) - p1) > v[l] * m + 1.0e-7) ok1 = false; if (abs(Point(x[l], y[l]) - p2) > v[l] * m + 1.0e-7) ok2 = false; } if (ok1 || ok2) flag = true; } } } if (flag) r = m; else l = m; } cout << fixed << setprecision(10) << l << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
652
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <queue> #include <set> #define REP(i, n) for (int i = 0; i < (int)(n); i++) inline int getInt() { int s; scanf("%d", &s); return s; } using namespace std; typedef complex<double> P; int main() { while (const int n = getInt()) { vector<P> pos(n); vector<double> v(n); REP(i, n) { const int x = getInt(); const int y = getInt(); v[i] = getInt(); pos[i] = P(x, y); } const double r = 0.7; P p(0, 0); auto calc = [&](P p) { auto mx = make_pair(0.0, 0); REP(i, n) mx = max(mx, make_pair(abs(p - pos[i]) / v[i], i)); return mx; }; for (double d = 1000; d > 1e-8; d *= r) { auto mx = calc(p).second; p += (pos[mx] - p) / abs(pos[mx] - p) * d; } printf("%.8f\n", calc(p).first); } return 0; }
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <queue> #include <set> #define REP(i, n) for (int i = 0; i < (int)(n); i++) inline int getInt() { int s; scanf("%d", &s); return s; } using namespace std; typedef complex<double> P; int main() { while (const int n = getInt()) { vector<P> pos(n); vector<double> v(n); REP(i, n) { const int x = getInt(); const int y = getInt(); v[i] = getInt(); pos[i] = P(x, y); } const double r = 0.99; P p(0, 0); auto calc = [&](P p) { auto mx = make_pair(0.0, 0); REP(i, n) mx = max(mx, make_pair(abs(p - pos[i]) / v[i], i)); return mx; }; for (double d = 1000; d > 1e-8; d *= r) { auto mx = calc(p).second; p += (pos[mx] - p) / abs(pos[mx] - p) * d; } printf("%.8f\n", calc(p).first); } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
276
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define inf (1 << 29) #define EPS (1e-10) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; // class Point { public: double x, y; Point(double x = -1, double y = -1) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } //※イケメンに限る bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; struct Segment { Point p1, p2; Segment(Point p1 = Point(-1, -1), Point p2 = Point(-1, -1)) : p1(p1), p2(p2) {} }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; ostream &operator<<(ostream &os, const Point &a) { os << "(" << a.x << "," << a.y << ")"; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double norm(Point a) { return a.x * a.x + a.y * a.y; } double abs(Point a) { return sqrt(norm(a)); } Point rotate(Point a, double rad) { return Point(cos(rad) * a.x - sin(rad) * a.y, sin(rad) * a.x + cos(rad) * a.y); } double toRad(double agl) { return agl * M_PI / 180.0; } // struct Node { Point p; double v; Node(Point p = Point(), double v = inf) : p(p), v(v) {} }; int N; void compute(vector<Node> &vec) { Point cur = vec[0].p; // cout << "(" << cur.x << "," << cur.y << ")\n"; double eps = 0.8, d = 10000; double mdist; while (d > EPS) { int index = -inf; mdist = -inf; rep(j, vec.size()) { double dist = abs(vec[j].p - cur) / vec[j].v; if (!equals(mdist, dist) && mdist < dist) { mdist = dist; index = j; } } Vector dept = vec[index].p - cur; dept = (dept / abs(dept)) * d; cur = cur + dept; d *= eps; } cout << setiosflags(ios::fixed) << setprecision(10) << mdist << endl; } int main() { while (cin >> N, N) { vector<Node> vec(N); rep(i, N) cin >> vec[i].p.x >> vec[i].p.y >> vec[i].v; compute(vec); } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define inf (1 << 29) #define EPS (1e-10) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; // class Point { public: double x, y; Point(double x = -1, double y = -1) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } //※イケメンに限る bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; struct Segment { Point p1, p2; Segment(Point p1 = Point(-1, -1), Point p2 = Point(-1, -1)) : p1(p1), p2(p2) {} }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; ostream &operator<<(ostream &os, const Point &a) { os << "(" << a.x << "," << a.y << ")"; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double norm(Point a) { return a.x * a.x + a.y * a.y; } double abs(Point a) { return sqrt(norm(a)); } Point rotate(Point a, double rad) { return Point(cos(rad) * a.x - sin(rad) * a.y, sin(rad) * a.x + cos(rad) * a.y); } double toRad(double agl) { return agl * M_PI / 180.0; } // struct Node { Point p; double v; Node(Point p = Point(), double v = inf) : p(p), v(v) {} }; int N; void compute(vector<Node> &vec) { Point cur = vec[0].p; // cout << "(" << cur.x << "," << cur.y << ")\n"; double eps = 0.99, d = 10000; double mdist; while (d > EPS) { int index = -inf; mdist = -inf; rep(j, vec.size()) { double dist = abs(vec[j].p - cur) / vec[j].v; if (!equals(mdist, dist) && mdist < dist) { mdist = dist; index = j; } } Vector dept = vec[index].p - cur; dept = (dept / abs(dept)) * d; cur = cur + dept; d *= eps; } cout << setiosflags(ios::fixed) << setprecision(10) << mdist << endl; } int main() { while (cin >> N, N) { vector<Node> vec(N); rep(i, N) cin >> vec[i].p.x >> vec[i].p.y >> vec[i].v; compute(vec); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
794
X, Y, V = *0..2 EPS = 1e-9 until (n = gets.to_i).zero? cafe = n.times.map{gets.split.map(&:to_f)} x, y = cafe.first d = 50.0 r = 0.9 until d < EPS farthermost = cafe.max_by{|c| Math.hypot(c[X] - x, c[Y] - y) / c[V]} angle = Math::atan2(farthermost[Y] - y, farthermost[X] - x) x += Math::cos(angle) * d y += Math::sin(angle) * d d *= r end puts "%.8f"%cafe.map{|c| Math.hypot(c[X] - x, c[Y] - y) / c[V]}.max end
X, Y, V = *0..2 EPS = 1e-9 until (n = gets.to_i).zero? cafe = n.times.map{gets.split.map(&:to_f)} x, y = cafe.first d = 50.0 r = 0.98 until d < EPS farthermost = cafe.max_by{|c| Math.hypot(c[X] - x, c[Y] - y) / c[V]} angle = Math::atan2(farthermost[Y] - y, farthermost[X] - x) x += Math::cos(angle) * d y += Math::sin(angle) * d d *= r end puts "%.8f"%cafe.map{|c| Math.hypot(c[X] - x, c[Y] - y) / c[V]}.max end
[["-", 0, 493, 0, 750, 8, 170, 0, 662, 12, 531], ["+", 0, 493, 0, 750, 8, 170, 0, 662, 12, 531]]
4
174
import java.util.Scanner; class Main { public static void main(String[] args) { final int X = 0; final int Y = 1; int p[][] = new int[50][2]; int times = 1; Scanner stdin = new Scanner(System.in); while (true) { int n = stdin.nextInt(); if (n == 0) { break; } for (int i = 0; i < n; i++) { p[i][X] = stdin.nextInt(); p[i][Y] = stdin.nextInt(); } double answer = 0; for (int i = 0; i < n - 1; i++) { answer += (double)(p[i + 1][X] - p[i][X]) * (p[i][Y] + p[i + 1][Y]) / 2; } answer += (double)(p[0][X] - p[n - 1][X]) * (p[n - 1][Y] + p[0][Y]) / 2; System.out.printf("%d %.1f\n", times, answer); } } }
import java.util.Scanner; class Main { public static void main(String[] args) { final int X = 0; final int Y = 1; int p[][] = new int[50][2]; int times = 1; Scanner stdin = new Scanner(System.in); while (true) { int n = stdin.nextInt(); if (n == 0) { break; } for (int i = 0; i < n; i++) { p[i][X] = stdin.nextInt(); p[i][Y] = stdin.nextInt(); } double answer = 0; for (int i = 0; i < n - 1; i++) { answer += (double)(p[i + 1][X] - p[i][X]) * (p[i][Y] + p[i + 1][Y]) / 2; } answer += (double)(p[0][X] - p[n - 1][X]) * (p[n - 1][Y] + p[0][Y]) / 2; System.out.printf("%d %.1f\n", times, answer); times++; } } }
[["+", 0, 52, 8, 196, 0, 1, 0, 27, 0, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 27, 0, 29], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35]]
3
269
#include <math.h> #include <stdio.h> int N, P[51][2], i, c, s; int main() { for (c = 1; scanf("%d", &N), N; c++) { scanf("%d%d", &P[0][0], &P[0][1]); for (i = 0, s = 0; i < N; i++) { if (i < N - 1) scanf("%d%d", &P[i + 1][0], &P[i + 1][1]); s += P[i][0] * P[(i + 1) % N][1] - P[(i + 1) % N][0] * P[i][1]; } printf("%d %.1f\n", c, abs(s / 2.0)); } return 0; }
#include <math.h> #include <stdio.h> int N, P[51][2], i, c, s; int main() { for (c = 1; scanf("%d", &N), N; c++) { scanf("%d%d", &P[0][0], &P[0][1]); for (i = 0, s = 0; i < N; i++) { if (i < N - 1) scanf("%d%d", &P[i + 1][0], &P[i + 1][1]); s += P[i][0] * P[(i + 1) % N][1] - P[(i + 1) % N][0] * P[i][1]; } printf("%d %.1f\n", c, abs(s) / 2.0); } return 0; }
[["+", 3, 4, 0, 16, 31, 2, 3, 4, 0, 25], ["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
0
197
#include <stdio.h> int main(void) { int n, x[100], y[100]; int va_x, va_y, vb_x, vb_y; int i, count = 1; double s = 0.0, sum = 0.0; scanf("%d", &n); do { sum = 0.0; for (i = 0; i < n; i++) { scanf("%d %d", &x[i], &y[i]); } for (i = 1; i < n - 1; i++) { va_x = x[i] - x[0]; va_y = y[i] - y[0]; vb_x = x[i + 1] - x[0]; vb_y = y[i + 1] - y[0]; s = ((double)va_x * vb_y - (double)va_y * vb_x) / 2; sum += s; } printf("%d %.1f\n\n", count++, -sum); scanf("%d", &n); } while (n != 0); return 0; }
#include <stdio.h> int main(void) { int n, x[100], y[100]; int va_x, va_y, vb_x, vb_y; int i, count = 1; double s = 0.0, sum = 0.0; scanf("%d", &n); do { sum = 0.0; for (i = 0; i < n; i++) { scanf("%d %d", &x[i], &y[i]); } for (i = 1; i < n - 1; i++) { va_x = x[i] - x[0]; va_y = y[i] - y[0]; vb_x = x[i + 1] - x[0]; vb_y = y[i + 1] - y[0]; s = ((double)va_x * vb_y - (double)va_y * vb_x) / 2; sum += s; } printf("%d %.1f\n", count++, -sum); scanf("%d", &n); } while (n != 0); return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
226
#include <stdio.h> int main() { int n, cnt = 1; scanf("%d", &n); while (n) { float s = 0, x0, y0, xa, xb, ya, yb; scanf("%d %d", &x0, &y0); xb = x0; yb = y0; for (int i = n - 1; i > 0; i--) { xa = xb; ya = yb; scanf("%d %d", &xb, &yb); s += (xb + xa) * (yb - ya); } s += (x0 + xb) * (y0 - yb); printf("%d %.1f\n", cnt++, s > 0 ? s / 2 : (-s) / 2); scanf("%d", &n); } return 0; }
#include <stdio.h> int main() { int n, cnt = 1; scanf("%d", &n); while (n) { float s = 0, x0, y0, xa, xb, ya, yb; scanf("%f %f", &x0, &y0); xb = x0; yb = y0; for (int i = n - 1; i > 0; i--) { xa = xb; ya = yb; scanf("%f %f", &xb, &yb); s += (xb + xa) * (yb - ya); } s += (x0 + xb) * (y0 - yb); printf("%d %.1f\n", cnt++, s > 0 ? s / 2 : (-s) / 2); scanf("%d", &n); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
175
#include <algorithm> #include <complex> #include <cstdio> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; double cross(complex<double> a, complex<double> b) { return (a * conj(b)).imag(); } int main() { int n, j = 1; while (cin >> n, n) { vector<complex<double>> v(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v[i] = complex<double>(a, b); } double sum = cross(v[0], v[n - 1]); for (int i = 1; i < n; i++) { sum += cross(v[i], v[i - 1]); } sum = abs(sum) / 2; printf("%d %.1f", j, sum); j++; } }
#include <algorithm> #include <complex> #include <cstdio> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; double cross(complex<double> a, complex<double> b) { return (a * conj(b)).imag(); } int main() { int n, j = 1; while (cin >> n, n) { vector<complex<double>> v(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v[i] = complex<double>(a, b); } double sum = cross(v[0], v[n - 1]); for (int i = 1; i < n; i++) { sum += cross(v[i], v[i - 1]); } sum = abs(sum) / 2; printf("%d %.1f\n", j, sum); j++; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
200
import java.awt.geom.*; import java.io.*; import java.math.*; import java.util.*; public class Main { static int INF = 2 << 27; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; int[] count = new int[3]; int[] sum = new int[3]; for (int i = 0; i < n; i++) { String[] in = sc.next().split(":"); if (in[0].equals("00")) { in[0] = "24"; } if (in[0].equals("01")) { in[0] = "25"; } int MM = sc.nextInt(); int time = Integer.parseInt(in[0]) * 60 + Integer.parseInt(in[1]); int TIMEA = Integer.parseInt(in[0]) * 60 + MM - time; int TIMEB = Integer.parseInt(in[0]) * 60 + MM + 60 - time; int DIS = (TIMEA < 0) ? TIMEB : Math.min(TIMEA, TIMEB); if (time >= 11 * 60 && time <= 14 * 60 + 59) { count[0]++; if (DIS <= 8) { sum[0]++; } } else if (time >= 18 * 60 && time <= 20 * 60 + 59) { count[1]++; if (DIS <= 8) { sum[1]++; } } else if (time >= 21 * 60 && time <= 1 * 60 + 59) { count[2]++; if (DIS <= 8) { sum[2]++; } } } double A = 0; double B = 0; double C = 0; if (count[0] != 0) A = (sum[0] / (double)count[0]) * 100; if (count[1] != 0) B = (sum[1] / (double)count[1]) * 100; if (count[2] != 0) C = (sum[2] / (double)count[2]) * 100; int AA = (int)A; int BB = (int)B; int CC = (int)C; System.out.println("lunch " + ((count[0] == 0) ? "no guest" : AA)); System.out.println("dinner " + ((count[1] == 0) ? "no guest" : BB)); System.out.println("midnight " + ((count[2] == 0) ? "no guest" : CC)); } } }
import java.awt.geom.*; import java.io.*; import java.math.*; import java.util.*; public class Main { static int INF = 2 << 27; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; int[] count = new int[3]; int[] sum = new int[3]; for (int i = 0; i < n; i++) { String[] in = sc.next().split(":"); if (in[0].equals("00")) { in[0] = "24"; } if (in[0].equals("01")) { in[0] = "25"; } int MM = sc.nextInt(); int time = Integer.parseInt(in[0]) * 60 + Integer.parseInt(in[1]); int TIMEA = Integer.parseInt(in[0]) * 60 + MM - time; int TIMEB = Integer.parseInt(in[0]) * 60 + MM + 60 - time; int DIS = (TIMEA < 0) ? TIMEB : Math.min(TIMEA, TIMEB); if (time >= 11 * 60 && time <= 14 * 60 + 59) { count[0]++; if (DIS <= 8) { sum[0]++; } } else if (time >= 18 * 60 && time <= 20 * 60 + 59) { count[1]++; if (DIS <= 8) { sum[1]++; } } else if (time >= 21 * 60 && time <= 25 * 60 + 59) { count[2]++; if (DIS <= 8) { sum[2]++; } } } double A = 0; double B = 0; double C = 0; if (count[0] != 0) A = (sum[0] / (double)count[0]) * 100; if (count[1] != 0) B = (sum[1] / (double)count[1]) * 100; if (count[2] != 0) C = (sum[2] / (double)count[2]) * 100; int AA = (int)A; int BB = (int)B; int CC = (int)C; System.out.println("lunch " + ((count[0] == 0) ? "no guest" : AA)); System.out.println("dinner " + ((count[1] == 0) ? "no guest" : BB)); System.out.println("midnight " + ((count[2] == 0) ? "no guest" : CC)); } } }
[["-", 0, 16, 12, 16, 12, 16, 31, 16, 31, 499], ["+", 0, 16, 12, 16, 12, 16, 31, 16, 31, 499]]
3
612
import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Problem A: It's our delight!! */ public class Main { static final int Lmin = 660; static final int Lmax = 900; static final int Dmin = 1080; static final int Dmax = 1260; static final int Mmin = 1260; static final int Mmax = 1560; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; while ((line = br.readLine()) != null && !line.isEmpty()) { int n = parseInt(line); if (n == 0) break; int L, D, M, Lsum, Dsum, Msum; L = D = M = 0; Lsum = Dsum = Msum = 0; for (int i = 0; i < n; i++) { words = br.readLine().split("[: ]"); int s = parseInt(words[0]) * 60 + parseInt(words[1]); int t = parseInt(words[0]) * 60 + parseInt(words[2]); if (t < s) { t += 60; } if (s < 120) { s += 1440; t += 1440; } if (Lmin <= s && s < Lmax) { Lsum++; if (s + 8 >= t) L++; } else if (Dmin <= s && s < Dmax) { Dsum++; if (s + 8 >= t) D++; } else if (Mmin <= s && s < Mmax) { Msum++; if (s + 8 >= t) M++; } } System.out.println("lunch " + (L == 0 ? "no guest" : L * 100 / Lsum)); System.out.println("dinner " + (D == 0 ? "no guest" : D * 100 / Dsum)); System.out.println("midnight " + (M == 0 ? "no guest" : M * 100 / Msum)); } } }
import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Problem A: It's our delight!! */ public class Main { static final int Lmin = 660; static final int Lmax = 900; static final int Dmin = 1080; static final int Dmax = 1260; static final int Mmin = 1260; static final int Mmax = 1560; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; while ((line = br.readLine()) != null && !line.isEmpty()) { int n = parseInt(line); if (n == 0) break; int L, D, M, Lsum, Dsum, Msum; L = D = M = 0; Lsum = Dsum = Msum = 0; for (int i = 0; i < n; i++) { words = br.readLine().split("[: ]"); int s = parseInt(words[0]) * 60 + parseInt(words[1]); int t = parseInt(words[0]) * 60 + parseInt(words[2]); if (t < s) { t += 60; } if (s < 120) { s += 1440; t += 1440; } if (Lmin <= s && s < Lmax) { Lsum++; if (s + 8 >= t) L++; } else if (Dmin <= s && s < Dmax) { Dsum++; if (s + 8 >= t) D++; } else if (Mmin <= s && s < Mmax) { Msum++; if (s + 8 >= t) M++; } } System.out.println("lunch " + (Lsum == 0 ? "no guest" : L * 100 / Lsum)); System.out.println("dinner " + (Dsum == 0 ? "no guest" : D * 100 / Dsum)); System.out.println("midnight " + (Msum == 0 ? "no guest" : M * 100 / Msum)); } } }
[["-", 0, 16, 12, 23, 0, 510, 15, 16, 31, 22], ["+", 0, 16, 12, 23, 0, 510, 15, 16, 31, 22]]
3
446
/* AOJ 1062 Title: @kankichi573 */ #include <stdio.h> #include <string.h> int denp[3][2]; char timezone[3][9] = {"lunch", "dinner", "midnight"}; void output() { int i; for (i = 0; i < 3; i++) { printf("%s ", &timezone[i][0]); if (denp[i][0] == 0) printf("no guest\n"); else printf("%d\n", denp[i][1] * 100 / denp[i][0]); } } main() { int N; int i, ret, hh, mm, MM, z, d; while (scanf("%d", &N) && (N)) { memset(denp, 0, sizeof(denp)); for (i = 0; i < N; i++) { scanf("%d:%d %d", &hh, &mm, &MM); if (hh >= 11 && hh <= 14) z = 0; else if (hh >= 18 && hh <= 20) z = 1; else if (hh > 21 || hh < 2) z = 2; else z = -1; if (z >= 0) { d = MM - mm; if (d < 0) d += 60; denp[z][0]++; if (d <= 8) denp[z][1]++; } } output(); } return (0); }
/* AOJ 1062 Title: @kankichi573 */ #include <stdio.h> #include <string.h> int denp[3][2]; char timezone[3][9] = {"lunch", "dinner", "midnight"}; void output() { int i; for (i = 0; i < 3; i++) { printf("%s ", &timezone[i][0]); if (denp[i][0] == 0) printf("no guest\n"); else printf("%d\n", denp[i][1] * 100 / denp[i][0]); } } main() { int N; int i, ret, hh, mm, MM, z, d; while (scanf("%d", &N) && (N)) { memset(denp, 0, sizeof(denp)); for (i = 0; i < N; i++) { scanf("%d:%d %d", &hh, &mm, &MM); if (hh >= 11 && hh <= 14) z = 0; else if (hh >= 18 && hh <= 20) z = 1; else if (hh >= 21 || hh < 2) z = 2; else z = -1; if (z >= 0) { d = MM - mm; if (d < 0) d += 60; denp[z][0]++; if (d <= 8) denp[z][1]++; } } output(); } return (0); }
[["-", 0, 57, 15, 23, 0, 16, 31, 16, 17, 47], ["+", 0, 57, 15, 23, 0, 16, 31, 16, 17, 20]]
0
315
#include <stdio.h> int main(void) { int l[2], d[2], s[2], n, h, m, t, i; while (scanf("%d", &n), n != 0) { l[0] = 0; l[1] = 0; d[0] = 0; d[1] = 0; s[0] = 0; s[1] = 0; for (i = 1; i <= n; i++) { scanf("%d:%d%d", &h, &m, &t); if (m > t) { t += 60; } if (11 <= h && h < 15) { l[1]++; if (t - m <= 8) { l[0]++; } } if (18 <= h && h < 21) { d[1]++; if (t - m <= 8) { d[0]++; } } if (22 < h || h < 2) { s[1]++; if (t - m <= 8) { s[0]++; } } } if (l[1]) { printf("lunch %d\n", l[0] * 100 / l[1]); } else { printf("lunch no guest\n"); } if (d[1]) { printf("dinner %d\n", d[0] * 100 / d[1]); } else { printf("dinner no guest\n"); } if (s[1]) { printf("midnight %d\n", s[0] * 100 / s[1]); } else { printf("midnight no guest\n"); } } return 0; }
#include <stdio.h> int main(void) { int l[2], d[2], s[2], n, h, m, t, i; while (scanf("%d", &n), n != 0) { l[0] = 0; l[1] = 0; d[0] = 0; d[1] = 0; s[0] = 0; s[1] = 0; for (i = 1; i <= n; i++) { scanf("%d:%d%d", &h, &m, &t); if (m > t) { t += 60; } if (11 <= h && h < 15) { l[1]++; if (t - m <= 8) { l[0]++; } } if (18 <= h && h < 21) { d[1]++; if (t - m <= 8) { d[0]++; } } if (21 <= h || h < 2) { s[1]++; if (t - m <= 8) { s[0]++; } } } if (l[1]) { printf("lunch %d\n", l[0] * 100 / l[1]); } else { printf("lunch no guest\n"); } if (d[1]) { printf("dinner %d\n", d[0] * 100 / d[1]); } else { printf("dinner no guest\n"); } if (s[1]) { printf("midnight %d\n", s[0] * 100 / s[1]); } else { printf("midnight no guest\n"); } } return 0; }
[["-", 0, 57, 15, 23, 0, 16, 31, 16, 31, 13], ["-", 0, 57, 15, 23, 0, 16, 31, 16, 17, 18], ["+", 0, 57, 15, 23, 0, 16, 31, 16, 31, 13], ["+", 0, 57, 15, 23, 0, 16, 31, 16, 17, 19]]
0
363
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <math.h> #include <stdio.h> #include <stdlib.h> #define P(type, x, y) fprintf(stdout, #x " %" #type "\n", x * 100 / y) int main() { int h, m, M, lunch, dinner, midnight, n, cl, cd, cm; for (; fscanf(stdin, "%d", &n), n != 0;) { lunch = dinner = midnight = cl = cd = cm = 0; while (n--) { fscanf(stdin, "%d%*c%d%d", &h, &m, &M); if (m > M) M += 60; if (11 <= h && h < 15) { cl++; if (M - m <= 8) lunch++; } else if (18 <= h && h < 21) { cd++; if (M - m <= 8) dinner++; } else if (h < 2 || 21 <= h) { cm++; if (M - m <= 8) midnight++; } } if (cl) P(d, lunch, cl); else puts("lunch no guest"); if (cd) P(d, dinner, cd); else puts("dinner no guest"); if (cl) P(d, midnight, cl); else puts("midnight no guest"); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <math.h> #include <stdio.h> #include <stdlib.h> #define P(type, x, y) fprintf(stdout, #x " %" #type "\n", x * 100 / y) int main() { int h, m, M, lunch, dinner, midnight, n, cl, cd, cm; for (; fscanf(stdin, "%d", &n), n != 0;) { lunch = dinner = midnight = cl = cd = cm = 0; while (n--) { fscanf(stdin, "%d%*c%d%d", &h, &m, &M); if (m > M) M += 60; if (11 <= h && h < 15) { cl++; if (M - m <= 8) lunch++; } else if (18 <= h && h < 21) { cd++; if (M - m <= 8) dinner++; } else if (h < 2 || 21 <= h) { cm++; if (M - m <= 8) midnight++; } } if (cl) P(d, lunch, cl); else puts("lunch no guest"); if (cd) P(d, dinner, cd); else puts("dinner no guest"); if (cm) P(d, midnight, cm); else puts("midnight no guest"); } return 0; }
[["-", 0, 7, 8, 9, 0, 57, 15, 23, 0, 22], ["+", 0, 7, 8, 9, 0, 57, 15, 23, 0, 22], ["-", 0, 57, 64, 1, 0, 2, 3, 4, 0, 22], ["+", 0, 57, 64, 1, 0, 2, 3, 4, 0, 22]]
0
264
#define zone(h) \ ((11 <= (h) && (h) < 15) ? 0 \ : (18 <= (h) && (h) < 21) ? 1 \ : (21 <= (h) && (h) < 26) ? 2 \ : 3) int a[3], b[3]; char s[9]; char *r(int i) { strcpy(s, "no guest"); if (a[i]) sprintf(s, "%d", b[i] * 100 / a[i]); return s; } main(n, h, m, H, M) { for (; scanf("%d", &n), n;) { a[0] = a[1] = a[2] = b[0] = b[1] = b[2] = 0; for (; n; n--) { scanf("%d:%d %d", &h, &m, &M); if (h < 2) h += 24; H = M < m ? h + 1 : h; if (zone(H) > 2) continue; a[zone(H)]++; if ((H * 60 + M) - (h * 60 + m) <= 8) b[zone(H)]++; } printf("lunch %s\n", r(0)); printf("dinner %s\n", r(1)); printf("midnight %s\n", r(2)); } return 0; }
#define zone(h) \ ((11 <= (h) && (h) < 15) ? 0 \ : (18 <= (h) && (h) < 21) ? 1 \ : (21 <= (h) && (h) < 26) ? 2 \ : 3) int a[3], b[3]; char s[9]; char *r(int i) { strcpy(s, "no guest"); if (a[i]) sprintf(s, "%d", b[i] * 100 / a[i]); return s; } main(n, h, m, H, M) { for (; scanf("%d", &n), n;) { a[0] = a[1] = a[2] = b[0] = b[1] = b[2] = 0; for (; n; n--) { scanf("%d:%d %d", &h, &m, &M); if (h < 2) h += 24; H = M < m ? h + 1 : h; if (zone(h) > 2) continue; a[zone(h)]++; if ((H * 60 + M) - (h * 60 + m) <= 8) b[zone(h)]++; } printf("lunch %s\n", r(0)); printf("dinner %s\n", r(1)); printf("midnight %s\n", r(2)); } return 0; }
[["-", 15, 23, 0, 16, 31, 2, 3, 4, 0, 22], ["+", 15, 23, 0, 16, 31, 2, 3, 4, 0, 22], ["-", 0, 27, 28, 69, 71, 2, 3, 4, 0, 22], ["+", 0, 27, 28, 69, 71, 2, 3, 4, 0, 22]]
0
277
#include <stdio.h> int main(void) { int menu; int hour, min, order_time; int hantei; int l_ac, d_ac, m_ac; int l_ord, d_ord, m_ord; int i; while (1) { scanf("%d", &menu); if (menu == 0) { break; } l_ord = d_ord = m_ord = l_ac = d_ac = m_ac = 0; for (i = 0; i < menu; i++) { scanf("%d%*c%d%d", &hour, &min, &order_time); if (11 <= hour && hour <= 12) { l_ord++; if (min > order_time) { hantei = order_time + 60 - min; } else { hantei = order_time - min; } if (hantei <= 8) { l_ac++; } } if (18 <= hour && hour <= 20) { d_ord++; if (min > order_time) { hantei = order_time + 60 - min; } else { hantei = order_time - min; } if (hantei <= 8) { d_ac++; } } if (21 <= hour && hour <= 23 || 0 <= hour && hour <= 1) { m_ord++; if (min > order_time) { hantei = order_time + 60 - min; } else { hantei = order_time - min; } if (hantei <= 8) { m_ac++; } } } printf("lunch "); if (l_ord == 0) { printf("no guest\n"); } else { printf("%d\n", l_ac * 100 / l_ord); } printf("dinner "); if (d_ord == 0) { printf("no guest\n"); } else { printf("%d\n", d_ac * 100 / d_ord); } printf("midnight "); if (m_ord == 0) { printf("no guest\n"); } else { printf("%d\n", m_ac * 100 / m_ord); } } return (0); }
#include <stdio.h> int main(void) { int menu; int hour, min, order_time; int hantei; int l_ac, d_ac, m_ac; int l_ord, d_ord, m_ord; int i; while (1) { scanf("%d", &menu); if (menu == 0) { break; } l_ord = d_ord = m_ord = l_ac = d_ac = m_ac = 0; for (i = 0; i < menu; i++) { scanf("%d%*c%d%d", &hour, &min, &order_time); if (11 <= hour && hour <= 14) { l_ord++; if (min > order_time) { hantei = order_time + 60 - min; } else { hantei = order_time - min; } if (hantei <= 8) { l_ac++; } } if (18 <= hour && hour <= 20) { d_ord++; if (min > order_time) { hantei = order_time + 60 - min; } else { hantei = order_time - min; } if (hantei <= 8) { d_ac++; } } if (21 <= hour && hour <= 23 || 0 <= hour && hour <= 1) { m_ord++; if (min > order_time) { hantei = order_time + 60 - min; } else { hantei = order_time - min; } if (hantei <= 8) { m_ac++; } } } printf("lunch "); if (l_ord == 0) { printf("no guest\n"); } else { printf("%d\n", l_ac * 100 / l_ord); } printf("dinner "); if (d_ord == 0) { printf("no guest\n"); } else { printf("%d\n", d_ac * 100 / d_ord); } printf("midnight "); if (m_ord == 0) { printf("no guest\n"); } else { printf("%d\n", m_ac * 100 / m_ord); } } return (0); }
[["-", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13]]
0
396
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, j) for (int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) typedef complex<double> P; int main() { int N; while (cin >> N && N) { int cnt[3], c[3]; memset(cnt, 0, sizeof(cnt)); memset(c, 0, sizeof(c)); REP(i, N) { int h, m, _m; scanf("%d%*c%d%d", &h, &m, &_m); int a = h * 60 + m, b = (_m > m ? a + _m - m : a + (60 - m) + _m); if (a >= 11 * 60 && a <= 14 * 60 + 59) ++c[0]; if (a >= 18 * 60 && a <= 20 * 60 + 59) ++c[1]; if (a >= 21 * 60 || a <= 1 * 60 + 59) ++c[2]; if (a >= 11 * 60 && a <= 14 * 60 + 59 && b - a <= 8) ++cnt[0]; if (a >= 18 * 60 && a <= 20 * 60 + 59 && b - a <= 8) ++cnt[1]; if ((a >= 21 * 60 || a <= 1 * 60 + 59) && b - a <= 8) ++cnt[2]; } cout << "lunch "; if (cnt[0] == 0) cout << "no guest" << endl; else cout << (int)(cnt[0] / (double)(c[0]) * 100) << endl; cout << "dinner "; if (cnt[1] == 0) cout << "no guest" << endl; else cout << (int)(cnt[1] / (double)(c[1]) * 100) << endl; cout << "midnight "; if (cnt[2] == 0) cout << "no guest" << endl; else cout << (int)(cnt[2] / (double)(c[2]) * 100) << endl; } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, j) for (int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) typedef complex<double> P; int main() { int N; while (cin >> N && N) { int cnt[3], c[3]; memset(cnt, 0, sizeof(cnt)); memset(c, 0, sizeof(c)); REP(i, N) { int h, m, _m; scanf("%d%*c%d%d", &h, &m, &_m); int a = h * 60 + m, b = (_m >= m ? a + _m - m : a + (60 - m) + _m); if (a >= 11 * 60 && a <= 14 * 60 + 59) ++c[0]; if (a >= 18 * 60 && a <= 20 * 60 + 59) ++c[1]; if (a >= 21 * 60 || a <= 1 * 60 + 59) ++c[2]; if (a >= 11 * 60 && a <= 14 * 60 + 59 && b - a <= 8) ++cnt[0]; if (a >= 18 * 60 && a <= 20 * 60 + 59 && b - a <= 8) ++cnt[1]; if ((a >= 21 * 60 || a <= 1 * 60 + 59) && b - a <= 8) ++cnt[2]; } cout << "lunch "; if (c[0] == 0) cout << "no guest" << endl; else cout << (int)(cnt[0] / (double)(c[0]) * 100) << endl; cout << "dinner "; if (c[1] == 0) cout << "no guest" << endl; else cout << (int)(cnt[1] / (double)(c[1]) * 100) << endl; cout << "midnight "; if (c[2] == 0) cout << "no guest" << endl; else cout << (int)(cnt[2] / (double)(c[2]) * 100) << endl; } return 0; }
[["-", 49, 50, 51, 23, 0, 41, 15, 16, 17, 47], ["+", 49, 50, 51, 23, 0, 41, 15, 16, 17, 20], ["-", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22]]
1
502
#include <algorithm> #include <cstdio> #include <vector> using namespace std; int main() { int n; const char *d[] = {"lunch", "dinner", "midnight"}; while (scanf("%d", &n), n) { int cnt[3] = {0}, acnt[3] = {0}; while (n--) { int t, m, r; scanf("%d%*c%d%d", &t, &m, &r); r -= m; if (r < 0) r += 60; m += t * 60; if (11 * 60 <= m && 15 * 60 > m) t = 0; else if (18 * 60 <= m && 21 * 60 > m) t = 1; else if (21 * 60 <= m || 2 * 60 > m) t = 2; else continue; acnt[t]++; if (r < 8) cnt[t]++; } for (int i = 0; i < 3; i++) { printf("%s ", d[i]); if (acnt[i] == 0) printf("no guest\n"); else printf("%d\n", 100 * cnt[i] / acnt[i]); } } }
#include <algorithm> #include <cstdio> #include <vector> using namespace std; int main() { int n; const char *d[] = {"lunch", "dinner", "midnight"}; while (scanf("%d", &n), n) { int cnt[3] = {0}, acnt[3] = {0}; while (n--) { int t, m, r; scanf("%d%*c%d%d", &t, &m, &r); r -= m; if (r < 0) r += 60; m += t * 60; if (11 * 60 <= m && 15 * 60 > m) t = 0; else if (18 * 60 <= m && 21 * 60 > m) t = 1; else if (21 * 60 <= m || 2 * 60 > m) t = 2; else continue; acnt[t]++; if (r <= 8) cnt[t]++; } for (int i = 0; i < 3; i++) { printf("%s ", d[i]); if (acnt[i] == 0) printf("no guest\n"); else printf("%d\n", 100 * cnt[i] / acnt[i]); } } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
268
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back int main() { int n; while (cin >> n) { if (n == 0) break; int l = 0, d = 0, m = 0; int L = 0, D = 0, M = 0; rep(i, n) { int a, b, c; char ch; cin >> a >> ch >> b >> c; if (c < b) c += 60; if (11 <= a && a <= 14) { L++; if (c - b <= 8) l++; } if (18 <= a && a <= 20) { D++; if (c - b <= 8) d++; } if (21 <= a && a <= 24) { M++; if (c - b <= 8) m++; } if (0 <= a && a <= 1) { M++; if (c - b <= 8) m++; } } cout << "lunch "; if (l == 0) cout << "no guest" << endl; else cout << (int)100 * l / L << endl; cout << "dinner "; if (d == 0) cout << "no guest" << endl; else cout << (int)100 * d / D << endl; cout << "midnight "; if (m == 0) cout << "no guest" << endl; else cout << (int)100 * m / M << endl; } }
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back int main() { int n; while (cin >> n) { if (n == 0) break; int l = 0, d = 0, m = 0; int L = 0, D = 0, M = 0; rep(i, n) { int a, b, c; char ch; cin >> a >> ch >> b >> c; if (c < b) c += 60; if (11 <= a && a <= 14) { L++; if (c - b <= 8) l++; } if (18 <= a && a <= 20) { D++; if (c - b <= 8) d++; } if (21 <= a && a <= 24) { M++; if (c - b <= 8) m++; } if (0 <= a && a <= 1) { M++; if (c - b <= 8) m++; } } cout << "lunch "; if (L == 0) cout << "no guest" << endl; else cout << (int)100 * l / L << endl; cout << "dinner "; if (D == 0) cout << "no guest" << endl; else cout << (int)100 * d / D << endl; cout << "midnight "; if (M == 0) cout << "no guest" << endl; else cout << (int)100 * m / M << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
1
341
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define pb push_back #define mp make_pair #define all(r) r.begin(), r.end() #define fi first #define se second #define println(X) cout << X << endl; #define DBG(X) cout << #X << " : " << X << endl; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; string str[] = {"linch ", "dinner ", "midnight "}; int main() { int n; while (cin >> n && n) { int h, m1, m2; char c; int a, b; vi v(3, 0), u(3, 0); while (n--) { cin >> h >> c >> m1 >> m2; a = b = h * 60; a += m1; b += m2 + (m1 > m2 ? 60 : 0); if (11 * 60 <= a && a <= 14 * 60 + 59) { u[0]++; if (b - a <= 8) v[0]++; } else if (18 * 60 <= a && a <= 20 * 60 + 59) { u[1]++; if (b - a <= 8) v[1]++; } else if (21 * 60 <= a || a <= 60 + 59) { u[2]++; if (b - a <= 8) v[2]++; // cout<<m1<<" "<<m2<<endl; } } rep(i, 3) { cout << str[i]; if (u[i] == 0) cout << "no guest" << endl; else cout << v[i] * 100 / u[i] << endl; } } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define pb push_back #define mp make_pair #define all(r) r.begin(), r.end() #define fi first #define se second #define println(X) cout << X << endl; #define DBG(X) cout << #X << " : " << X << endl; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; string str[] = {"lunch ", "dinner ", "midnight "}; int main() { int n; while (cin >> n && n) { int h, m1, m2; char c; int a, b; vi v(3, 0), u(3, 0); while (n--) { cin >> h >> c >> m1 >> m2; a = b = h * 60; a += m1; b += m2 + (m1 > m2 ? 60 : 0); if (11 * 60 <= a && a <= 14 * 60 + 59) { u[0]++; if (b - a <= 8) v[0]++; } else if (18 * 60 <= a && a <= 20 * 60 + 59) { u[1]++; if (b - a <= 8) v[1]++; } else if (21 * 60 <= a || a <= 60 + 59) { u[2]++; if (b - a <= 8) v[2]++; // cout<<m1<<" "<<m2<<endl; } } rep(i, 3) { cout << str[i]; if (u[i] == 0) cout << "no guest" << endl; else cout << v[i] * 100 / u[i] << endl; } } }
[["-", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6], ["+", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6]]
1
417
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define X real() #define Y imag() #define value(x, y, w, h) (x >= 0 && x < w && y >= 0 && y < h) #define all(r) (r).begin(), (r).end() #define gsort(st, en) sort((st), (en), greater<int>()) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) typedef long long ll; typedef deque<int> di; typedef deque<ll> dl; typedef map<string, int> dict; typedef complex<double> comd; typedef pair<int, int> pii; constexpr int imax = ((1 << 30) - 1) * 2 + 1; constexpr int inf = 100000000; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; double CalcDist(comd p1, comd p2) { return sqrt(pow(p1.X - p2.X, 2.0) + pow(p1.Y - p2.Y, 2.0)); } template <typename T> void out(deque<T> d) { for (size_t i = 0; i < d.size(); i++) { debug(d[i]); } } template <typename T> T ston(string &str, T n) { istringstream sin(str); T num; sin >> num; return num; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, h, m, m2, l, d, mn; int lc, dc, mnc; char c; while (cin >> n) { if (n == 0) break; l = d = mn = 0; lc = dc = mnc = 0; rep(_, n) { cin >> h >> c >> m >> m2; if (11 <= h && h <= 14) { ++lc; if (m2 - m >= 0 && m2 - m < 9) { ++l; } else if (m2 - m < 0 && 60 - m + m2 < 9) { ++l; } } else if (18 <= h && h <= 20) { ++dc; if (m2 - m >= 0 && m2 - m < 9) { ++d; } else if (m2 - m < 0 && 60 - m + m2 < 9) { ++d; } } else if ((21 <= h && h <= 24) || (0 <= h && h <= 1)) { ++mnc; if (m2 - m >= 0 && m2 - m < 9) { ++mn; } else if (m2 - m < 0 && 60 - m + m2 < 9) { ++mn; } } } cout << "lunch "; if (l != 0) cout << (int)(1.0 * l / lc * 100) << endl; else cout << "no guest" << endl; cout << "dinner "; if (d != 0) cout << (int)(1.0 * d / dc * 100) << endl; else cout << "no guest" << endl; cout << "midnight "; if (mn != 0) cout << (int)(1.0 * mn / mnc * 100) << endl; else cout << "no guest" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define X real() #define Y imag() #define value(x, y, w, h) (x >= 0 && x < w && y >= 0 && y < h) #define all(r) (r).begin(), (r).end() #define gsort(st, en) sort((st), (en), greater<int>()) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) typedef long long ll; typedef deque<int> di; typedef deque<ll> dl; typedef map<string, int> dict; typedef complex<double> comd; typedef pair<int, int> pii; constexpr int imax = ((1 << 30) - 1) * 2 + 1; constexpr int inf = 100000000; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; double CalcDist(comd p1, comd p2) { return sqrt(pow(p1.X - p2.X, 2.0) + pow(p1.Y - p2.Y, 2.0)); } template <typename T> void out(deque<T> d) { for (size_t i = 0; i < d.size(); i++) { debug(d[i]); } } template <typename T> T ston(string &str, T n) { istringstream sin(str); T num; sin >> num; return num; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, h, m, m2, l, d, mn; int lc, dc, mnc; char c; while (cin >> n) { if (n == 0) break; l = d = mn = 0; lc = dc = mnc = 0; rep(_, n) { cin >> h >> c >> m >> m2; if (11 <= h && h <= 14) { ++lc; if (m2 - m >= 0 && m2 - m < 9) { ++l; } else if (m2 - m < 0 && 60 - m + m2 < 9) { ++l; } } else if (18 <= h && h <= 20) { ++dc; if (m2 - m >= 0 && m2 - m < 9) { ++d; } else if (m2 - m < 0 && 60 - m + m2 < 9) { ++d; } } else if ((21 <= h && h <= 23) || (0 <= h && h <= 1)) { ++mnc; if (m2 - m >= 0 && m2 - m < 9) { ++mn; } else if (m2 - m < 0 && 60 - m + m2 < 9) { ++mn; } } } cout << "lunch "; if (lc != 0) cout << (int)(1.0 * l / lc * 100) << endl; else cout << "no guest" << endl; cout << "dinner "; if (dc != 0) cout << (int)(1.0 * d / dc * 100) << endl; else cout << "no guest" << endl; cout << "midnight "; if (mnc != 0) cout << (int)(1.0 * mn / mnc * 100) << endl; else cout << "no guest" << endl; } return 0; }
[["-", 51, 16, 31, 23, 0, 16, 12, 16, 12, 13], ["+", 51, 16, 31, 23, 0, 16, 12, 16, 12, 13], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
1
741
#include <bits/stdc++.h> using namespace std; int main() { string str[] = {"lunch", "dinner", "midnught"}; int a; while (scanf("%d", &a), a) { int b[3][2]{}; for (int c = 0; c < a; c++) { int d, e, f; scanf("%d%*c%d%d", &d, &e, &f); if (f < e) f += 60; if (f - e < 9) { if (d >= 11 && d <= 14) { b[0][0]++; b[0][1]++; } else if (d >= 18 && d <= 20) { b[1][0]++; b[1][1]++; } else if (d >= 21 || d <= 1) { b[2][0]++; b[2][1]++; } } else { if (d >= 11 && d <= 14) { b[0][1]++; } else if (d >= 18 && d <= 20) { b[1][1]++; } else if (d >= 21 || d <= 1) { b[2][1]++; } } } for (int q = 0; q < 3; q++) { cout << str[q] << " "; if (b[q][1]) { printf("%d\n", b[q][0] * 100 / b[q][1]); } else { puts("no guest"); } } } }
#include <bits/stdc++.h> using namespace std; int main() { string str[] = {"lunch", "dinner", "midnight"}; int a; while (scanf("%d", &a), a) { int b[3][2]{}; for (int c = 0; c < a; c++) { int d, e, f; scanf("%d%*c%d%d", &d, &e, &f); if (f < e) f += 60; if (f - e < 9) { if (d >= 11 && d <= 14) { b[0][0]++; b[0][1]++; } else if (d >= 18 && d <= 20) { b[1][0]++; b[1][1]++; } else if (d >= 21 || d <= 1) { b[2][0]++; b[2][1]++; } } else { if (d >= 11 && d <= 14) { b[0][1]++; } else if (d >= 18 && d <= 20) { b[1][1]++; } else if (d >= 21 || d <= 1) { b[2][1]++; } } } for (int q = 0; q < 3; q++) { cout << str[q] << " "; if (b[q][1]) { printf("%d\n", b[q][0] * 100 / b[q][1]); } else { puts("no guest"); } } } }
[["-", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6], ["+", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6]]
1
355
/* * Author: Zhang Jiangbin * Created Time: 2011/8/22/星期一 15:59:48 * File Name: a.cpp */ #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <vector> using namespace std; #define SZ(v) ((int)(v).size()) int main() { int n, a, b, c, d, l, m, ll, dd, mm, inter, d_; for (scanf("%d", &n); n; scanf("%d", &n)) { l = d = m = ll = dd = mm = 0; while (n--) { scanf("%d%*c%d%d", &a, &b, &d_); c = a; if (b > d_) { c = (c + 1) % 24; inter = d_ - b + 60; } else { inter = d_ - b; } if (a <= 5) a += 24; if (c <= 5) c += 24; if (11 <= a && c <= 14) { ll++; if (inter <= 8) l++; } else if (18 <= a && c <= 20) { dd++; if (inter <= 8) d++; } else if (21 <= a && c <= 25) { mm++; if (inter <= 8) m++; } } printf("lunch "); if (ll == 0) puts("no guest"); else printf("%d\n", int(1.0 * l / ll * 100)); printf("dinner "); if (dd == 0) puts("no guest"); else printf("%d\n", int(1.0 * d / dd * 100)); printf("midnight "); if (mm == 0) puts("no guest"); else printf("%d\n", int(1.0 * m / mm * 100)); } return 0; }
/* * Author: Zhang Jiangbin * Created Time: 2011/8/22/星期一 15:59:48 * File Name: a.cpp */ #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <vector> using namespace std; #define SZ(v) ((int)(v).size()) int main() { int n, a, b, c, d, l, m, ll, dd, mm, inter, d_; for (scanf("%d", &n); n; scanf("%d", &n)) { l = d = m = ll = dd = mm = 0; while (n--) { scanf("%d%*c%d%d", &a, &b, &d_); c = a; if (b > d_) { c = (c + 1) % 24; inter = d_ - b + 60; } else { inter = d_ - b; } if (a <= 5) a += 24; if (c <= 5) c += 24; if (11 <= a && a <= 14) { ll++; if (inter <= 8) l++; } else if (18 <= a && a <= 20) { dd++; if (inter <= 8) d++; } else if (21 <= a && a <= 25) { mm++; if (inter <= 8) m++; } } printf("lunch "); if (ll == 0) puts("no guest"); else printf("%d\n", int(1.0 * l / ll * 100)); printf("dinner "); if (dd == 0) puts("no guest"); else printf("%d\n", int(1.0 * d / dd * 100)); printf("midnight "); if (mm == 0) puts("no guest"); else printf("%d\n", int(1.0 * m / mm * 100)); } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22]]
1
375
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; #define all(c) (c).begin(), (c).end() #define pb push_back #define mp make_pair #define ERASE(v, i) (v).erase(remove(all(v), i), (v).end()) #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define each(it, c) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define debug(x) cerr << #x << " = " << (x) << endl; #define LINE cerr << "LINE: " << __LINE__ << endl; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> void preview(T a, T b) { for (T it = a; it != b; ++it) cerr << *it << " "; cerr << endl; } const int INF = 100000000; const double PI = acos(-1.0), EPS = 1e-10; int main() { for (int n; scanf(" %d", &n), n;) { int sums[3] = {0}; int ok[3] = {0}; rep(i, n) { int hh, mm, cc; scanf(" %d:%d %d", &hh, &mm, &cc); if (cc < mm) { cc = cc + 60 - mm; } else { cc = cc - mm; } if (11 <= hh && hh < 15) { sums[0]++; if (cc <= 8) ok[0]++; } else if (18 <= hh && hh < 21) { sums[1]++; if (cc <= 8) ok[1]++; } else if (hh < 2 || 21 <= hh) { sums[2]++; if (cc <= 8) ok[2]++; } } cout << "lunch "; if (sums[0]) cout << ok[0] * 100 / sums[0] << endl; else cout << "no guest" << endl; cout << "dinner "; if (sums[1]) cout << ok[1] * 100 / sums[1] << endl; else cout << "no guest" << endl; cout << "dinner "; if (sums[2]) cout << ok[2] * 100 / sums[2] << endl; else cout << "no guest" << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; #define all(c) (c).begin(), (c).end() #define pb push_back #define mp make_pair #define ERASE(v, i) (v).erase(remove(all(v), i), (v).end()) #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define each(it, c) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define debug(x) cerr << #x << " = " << (x) << endl; #define LINE cerr << "LINE: " << __LINE__ << endl; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> void preview(T a, T b) { for (T it = a; it != b; ++it) cerr << *it << " "; cerr << endl; } const int INF = 100000000; const double PI = acos(-1.0), EPS = 1e-10; int main() { for (int n; scanf(" %d", &n), n;) { int sums[3] = {0}; int ok[3] = {0}; rep(i, n) { int hh, mm, cc; scanf(" %d:%d %d", &hh, &mm, &cc); if (cc < mm) { cc = cc + 60 - mm; } else { cc = cc - mm; } if (11 <= hh && hh < 15) { sums[0]++; if (cc <= 8) ok[0]++; } else if (18 <= hh && hh < 21) { sums[1]++; if (cc <= 8) ok[1]++; } else if (hh < 2 || 21 <= hh) { sums[2]++; if (cc <= 8) ok[2]++; } } cout << "lunch "; if (sums[0]) cout << ok[0] * 100 / sums[0] << endl; else cout << "no guest" << endl; cout << "dinner "; if (sums[1]) cout << ok[1] * 100 / sums[1] << endl; else cout << "no guest" << endl; cout << "midnight "; if (sums[2]) cout << ok[2] * 100 / sums[2] << endl; else cout << "no guest" << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 16, 12, 5, 0, 6], ["+", 8, 9, 0, 1, 0, 16, 12, 5, 0, 6]]
1
526
#include <iostream> using namespace std; int main(void) { int n; while (cin >> n && n) { int l = 0, sl = 0, d = 0, sd = 0, m = 0, sm = 0; int hh, mm, MM; for (int i = 0; i < n; i++) { cin >> hh; cin.ignore(); // for ":" cin >> mm >> MM; if (mm > MM) MM += 60; bool overed; MM - mm <= 8 ? overed = false : overed = true; if (11 <= hh && hh <= 14) { sl++; if (!overed) l++; } else if (18 <= hh && hh <= 20) { sd++; if (!overed) d++; } else if ((21 <= hh && hh <= 24) || hh <= 1) { sm++; if (!overed) l++; } } cout << "lunch "; if (sl == 0) { cout << "no guest" << endl; } else { cout << 100 * l / sl << endl; } cout << "dinner "; if (sd == 0) { cout << "no guest" << endl; } else { cout << 100 * d / sd << endl; } cout << "midnight "; if (sm == 0) { cout << "no guest" << endl; } else { cout << 100 * m / sm << endl; } } }
#include <iostream> using namespace std; int main(void) { int n; while (cin >> n && n) { int l = 0, sl = 0, d = 0, sd = 0, m = 0, sm = 0; int hh, mm, MM; for (int i = 0; i < n; i++) { cin >> hh; cin.ignore(); // for ":" cin >> mm >> MM; if (mm > MM) MM += 60; bool overed; MM - mm <= 8 ? overed = false : overed = true; if (11 <= hh && hh <= 14) { sl++; if (!overed) l++; } else if (18 <= hh && hh <= 20) { sd++; if (!overed) d++; } else if ((21 <= hh && hh <= 24) || hh <= 1) { sm++; if (!overed) m++; } } cout << "lunch "; if (sl == 0) { cout << "no guest" << endl; } else { cout << 100 * l / sl << endl; } cout << "dinner "; if (sd == 0) { cout << "no guest" << endl; } else { cout << 100 * d / sd << endl; } cout << "midnight "; if (sm == 0) { cout << "no guest" << endl; } else { cout << 100 * m / sm << endl; } } }
[["-", 64, 9, 0, 57, 64, 1, 0, 27, 28, 22], ["+", 64, 9, 0, 57, 64, 1, 0, 27, 28, 22]]
1
300
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; int main() { int n; while (~scanf("%d", &n)) { if (n == 0) break; int L_count = 0; int L_ok = 0; int D_count = 0; int D_ok = 0; int M_count = 0; int M_ok = 0; for (int i = 0; i < n; i++) { int h, m, d; scanf("%d:%d %d", &h, &m, &d); int time = (d - m >= 0 ? d - m : (60 + d - m)); bool isok = (time <= 8 ? true : false); int t = h * 60 + m + time; if (11 * 60 <= t && t <= 14 * 60 + 59) { if (isok) L_ok++; L_count++; } else if (18 * 60 <= t && t <= 20 * 60 + 59) { if (isok) D_ok++; D_count++; } else if ((21 * 60 <= t && t <= 24 * 60) || (24 * 60 <= t && t <= (24 + 1) * 60 + 59) || (0 <= t && t <= 1 * 60 + 59)) { if (isok) M_ok++; M_count++; } } if (L_count == 0) { printf("lunch no guest\n"); } else { printf("lunch %.0lf\n", floor(((L_ok) / (double)L_count + EPS) * 100.0)); } if (D_count == 0) { printf("dinner no guest\n"); } else { printf("dinner %.0lf\n", floor(((D_ok) / (double)D_count + EPS) * 100.0)); } if (M_count == 0) { printf("midnight no guest\n"); } else { printf("midnight %.0lf\n", floor(((M_ok) / (double)M_count + EPS) * 100.0)); } } }
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; int main() { int n; while (~scanf("%d", &n)) { if (n == 0) break; int L_count = 0; int L_ok = 0; int D_count = 0; int D_ok = 0; int M_count = 0; int M_ok = 0; for (int i = 0; i < n; i++) { int h, m, d; scanf("%d:%d %d", &h, &m, &d); int time = (d - m >= 0 ? d - m : (60 + d - m)); bool isok = (time <= 8 ? true : false); int t = h * 60 + m; if (11 * 60 <= t && t <= 14 * 60 + 59) { if (isok) L_ok++; L_count++; } else if (18 * 60 <= t && t <= 20 * 60 + 59) { if (isok) D_ok++; D_count++; } else if ((21 * 60 <= t && t <= 24 * 60) || (24 * 60 <= t && t <= (24 + 1) * 60 + 59) || (0 <= t && t <= 1 * 60 + 59)) { if (isok) M_ok++; M_count++; } } if (L_count == 0) { printf("lunch no guest\n"); } else { printf("lunch %.0lf\n", floor(((L_ok) / (double)L_count + EPS) * 100.0)); } if (D_count == 0) { printf("dinner no guest\n"); } else { printf("dinner %.0lf\n", floor(((D_ok) / (double)D_count + EPS) * 100.0)); } if (M_count == 0) { printf("midnight no guest\n"); } else { printf("midnight %.0lf\n", floor(((M_ok) / (double)M_count + EPS) * 100.0)); } } }
[["-", 8, 9, 0, 43, 49, 50, 51, 16, 17, 72], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 12, 22]]
1
504
#include <bits/stdc++.h> using namespace std; enum { LUNCH, DINNER, MIDNIGHT }; int main() { const string times[] = {"lunch", "dinner", "midnight"}; for (int n; scanf("%d", &n) == 1 && n;) { array<int, 3> num, ok; num.fill(0); ok.fill(0); for (int i = 0; i < n; ++i) { int h, m; scanf("%d:%d", &h, &m); int M; scanf("%d", &M); int t = -1; if (11 <= h && h <= 14) { t = LUNCH; } else if (18 <= h && h <= 20) { t = DINNER; } else if (21 <= h && h <= 1) { t = MIDNIGHT; } else { continue; } ++num[t]; if ((M - m + 60) % 60 <= 8) ++ok[t]; } for (int t = 0; t < 3; ++t) { cout << times[t] << " "; if (num[t]) { cout << 100 * ok[t] / num[t] << "\n"; } else { cout << "no guest\n"; } } } return 0; }
#include <bits/stdc++.h> using namespace std; enum { LUNCH, DINNER, MIDNIGHT }; int main() { const string times[] = {"lunch", "dinner", "midnight"}; for (int n; scanf("%d", &n) == 1 && n;) { array<int, 3> num, ok; num.fill(0); ok.fill(0); for (int i = 0; i < n; ++i) { int h, m; scanf("%d:%d", &h, &m); int M; scanf("%d", &M); int t = -1; if (11 <= h && h <= 14) { t = LUNCH; } else if (18 <= h && h <= 20) { t = DINNER; } else if (21 <= h || h <= 1) { t = MIDNIGHT; } else { continue; } ++num[t]; if ((M - m + 60) % 60 <= 8) ++ok[t]; } for (int t = 0; t < 3; ++t) { cout << times[t] << " "; if (num[t]) { cout << 100 * ok[t] / num[t] << "\n"; } else { cout << "no guest\n"; } } } return 0; }
[["-", 75, 76, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 75, 76, 0, 57, 15, 339, 51, 16, 17, 106]]
1
287
#include <iostream> using namespace std; int main() { int n; for (; cin >> n, n;) { int ans[3] = {0, 0, 0}; int sum[3] = {0, 0, 0}; for (int i = 0; i < n; i++) { int hh, mm, m; char buf; cin >> hh >> buf >> mm >> m; // cout<<hh<<" "<<mm<<" "<<m<<endl; int time = hh * 60 + mm; int m_time; if (mm <= m) m_time = m - mm; else m_time = (60 - m) + mm; if (time >= 11 * 60 && time <= 14 * 60 + 59) { sum[0]++; if (m_time < 9) ans[0]++; } if (time >= 18 * 60 && time <= 20 * 60 + 59) { sum[1]++; if (m_time < 9) ans[1]++; } if ((time >= 21 * 60 && time <= 23 * 60 + 59) || (time >= 0 && time <= 1 * 60 + 59)) { sum[2]++; if (m_time < 9) ans[2]++; } } string s[3] = {"lunch", "dinner", "midnight"}; for (int i = 0; i < 3; i++) { cout << s[i] << " "; if (sum[i] > 0) cout << (int)(((double)ans[i] / sum[i]) * 100) << endl; else cout << "no guest" << endl; } } }
#include <iostream> using namespace std; int main() { int n; for (; cin >> n, n;) { int ans[3] = {0, 0, 0}; int sum[3] = {0, 0, 0}; for (int i = 0; i < n; i++) { int hh, mm, m; char buf; cin >> hh >> buf >> mm >> m; // cout<<hh<<" "<<mm<<" "<<m<<endl; int time = hh * 60 + mm; int m_time; if (mm <= m) m_time = m - mm; else m_time = (60 - mm) + m; if (time >= 11 * 60 && time <= 14 * 60 + 59) { sum[0]++; if (m_time < 9) ans[0]++; } if (time >= 18 * 60 && time <= 20 * 60 + 59) { sum[1]++; if (m_time < 9) ans[1]++; } if ((time >= 21 * 60 && time <= 23 * 60 + 59) || (time >= 0 && time <= 1 * 60 + 59)) { sum[2]++; if (m_time < 9) ans[2]++; } } string s[3] = {"lunch", "dinner", "midnight"}; for (int i = 0; i < 3; i++) { cout << s[i] << " "; if (sum[i] > 0) cout << (int)(((double)ans[i] / sum[i]) * 100) << endl; else cout << "no guest" << endl; } } }
[["-", 0, 11, 12, 16, 31, 23, 0, 16, 12, 22], ["+", 0, 11, 12, 16, 31, 23, 0, 16, 12, 22], ["-", 75, 76, 0, 1, 0, 11, 12, 16, 12, 22], ["+", 75, 76, 0, 1, 0, 11, 12, 16, 12, 22]]
1
342
while 1: n=int(input()) if n==0:break l=d=m=cl=cm=cd=0 for i in range(n): t,M=input().split() M=int(M) h,m=map(int,t.split(':')) if m>M:M+=60 a=M-m<=8 if 11<=h<15:cl+=1;l+=a elif 18<=h<21:cd+=1;d+=a elif 21<=h or h< 2:cm+=1;m+=a print('lunch', l*100//cl if cl else 'no guest') print('dinner', d*100//cd if cd else 'no guest') print('midnight', m*100//cm if cm else 'no guest')
while 1: n=int(input()) if n==0:break l=d=mi=cl=cm=cd=0 for i in range(n): t,M=input().split() M=int(M) h,m=map(int,t.split(":")) if m>M:M+=60 a=M-m<=8 if 11<=h<15:cl+=1;l+=a elif 18<=h<21:cd+=1;d+=a elif 21<=h or h< 2:cm+=1;mi+=a print('lunch', l*100//cl if cl else 'no guest') print('dinner', d*100//cd if cd else 'no guest') print('midnight', mi*100//cm if cm else 'no guest')
[["-", 0, 1, 0, 662, 12, 662, 12, 662, 31, 22], ["+", 0, 1, 0, 662, 12, 662, 12, 662, 31, 22], ["-", 3, 4, 0, 652, 3, 4, 0, 557, 0, 654], ["+", 3, 4, 0, 652, 3, 4, 0, 557, 0, 654], ["-", 3, 4, 0, 652, 3, 4, 0, 557, 0, 655], ["+", 3, 4, 0, 652, 3, 4, 0, 557, 0, 655], ["-", 75, 665, 64, 196, 0, 1, 0, 677, 31, 22], ["+", 75, 665, 64, 196, 0, 1, 0, 677, 31, 22], ["-", 3, 4, 0, 41, 0, 657, 31, 657, 31, 22], ["+", 3, 4, 0, 41, 0, 657, 31, 657, 31, 22]]
5
185
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; typedef pair<int, int> P; map<string, int> mp; int N, M; vector<P> t, A, B; int solve(int start) { int ai = 0, bi = 0, now = start, res = 0; int as = A.size(), bs = B.size(), cnt = 0; while (1) { int ag = (ai < as ? A[ai].first : 1e8); int bg = (bi < bs ? B[bi].first : 1e8); if (7 * 60 * 24 + start - 30 < ag) ag = 1e8; if (7 * 60 * 24 + start - 30 < bg) bg = 1e8; if (ag == 1e8 && bg == 1e8) break; if (ag < now) { ai++; continue; } if (bg < now) { bi++; continue; } if (ag + 30 <= bg) { res++; now = ag + 30; ai++; } else { res++; cnt++; now = bg + 30; bi++; } } if (cnt < M) return -1; else return res; } void init() { t.clear(); A.clear(); B.clear(); mp.clear(); } int main() { while (1) { cin >> N; if (N == 0) break; init(); for (int i = 0; i < N; i++) { string a; int b, c; cin >> a >> b >> c; b = (c / 100 * 60 + c % 100) + (b * 60 * 24); mp[a] = i; t.push_back(P(b % (7 * 60 * 24), i)); } cin >> M; for (int i = 0; i < M; i++) { string a; cin >> a; B.push_back(t[mp[a]]); t[mp[a]] = P(-1, -1); } for (int i = 0; i < (int)t.size(); i++) if (t[i].first != -1) A.push_back(t[i]); sort(A.begin(), A.end()); sort(B.begin(), B.end()); int ans = 0; for (int i = 0; i < 30; i++) ans = max(ans, solve(i)); cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; typedef pair<int, int> P; map<string, int> mp; int N, M; vector<P> t, A, B; int solve(int start) { int ai = 0, bi = 0, now = start, res = 0; int as = A.size(), bs = B.size(), cnt = 0; while (1) { int ag = (ai < as ? A[ai].first : 1e8); int bg = (bi < bs ? B[bi].first : 1e8); if (7 * 60 * 24 + start - 30 < ag) ag = 1e8; if (7 * 60 * 24 + start - 30 < bg) bg = 1e8; if (ag == 1e8 && bg == 1e8) break; if (ag < now) { ai++; continue; } if (bg < now) { bi++; continue; } if (ag + 30 <= bg) { res++; now = ag + 30; ai++; } else { res++; cnt++; now = bg + 30; bi++; } } if (cnt < M) return -1; else return res; } void init() { t.clear(); A.clear(); B.clear(); mp.clear(); } int main() { while (1) { cin >> N; if (N == 0) break; init(); for (int i = 0; i < N; i++) { string a; int b, c; cin >> a >> b >> c; b = (c / 100 * 60 + c % 100) + (b * 60 * 24); mp[a] = i; t.push_back(P(b % (7 * 60 * 24), i)); } cin >> M; for (int i = 0; i < M; i++) { string a; cin >> a; B.push_back(t[mp[a]]); t[mp[a]] = P(-1, -1); } for (int i = 0; i < (int)t.size(); i++) if (t[i].first != -1) A.push_back(t[i]); sort(A.begin(), A.end()); sort(B.begin(), B.end()); int ans = -1; for (int i = 0; i <= 30; i++) ans = max(ans, solve(i)); cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
565
//============================================================================ // Name : TopCoderCompetition.cpp // Author : taguchi // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; //#define P pair<int,int> #define rep(i, n) for (int i = 0; i < n; i++) #define pb(n) push_back(n) typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<string> vs; struct edge { int cost, to; }; vector<string> split(string &in, char delimiter) { vector<string> re; uint cur = 0, next; while ((next = in.find_first_of(delimiter, cur)) != string::npos) { re.pb(string(in, cur, next - cur)); cur = next + 1; } re.pb(string(in, cur, in.size() - cur)); return re; } vector<int> split_int(string &in, char delimiter) { vector<string> str = split(in, delimiter); vector<int> re; rep(i, str.size()) { re.pb(strtol(str[i].c_str(), NULL, 10)); } return re; } class Range { public: int a, b; Range(int an, int bn) : a(an), b(bn) {} bool operator()(int n) { return a <= n && n <= b; } bool isCross(Range x) { return x(a) || x(b); } bool contains(Range x) { return this->a <= x.a && x.b <= this->b; } }; class RangeHandler { public: list<Range> Ranges; bool insert(Range x) { if (Ranges.empty()) { Ranges.push_back(x); return true; } if (x.b < Ranges.front().a) { Ranges.push_front(x); return true; } if (Ranges.back().b < x.a) { Ranges.push_back(x); return true; } for (list<Range>::iterator i = Ranges.begin(); i != Ranges.end(); i++) { list<Range>::iterator l = i; l++; // cout << "dbg" << i->b << " " << i->a << endl; if (Range(i->b, l->a).contains(x)) { Ranges.insert(l, x); return true; } } return false; } }; int N, P; int main() { while (true) { int result = 0; cin >> N; if (N == 0) break; vector<pair<int, string>> Programs, Table; vector<pair<int, string>>::iterator ip; rep(i, N) { string name; cin >> name; int weekday, start; cin >> weekday >> start; start -= 600; int time = 1440 * weekday + (start / 100) * 60 + (start % 100); Programs.pb(make_pair(time, name)); } cin >> P; rep(i, P) { string name; cin >> name; for (ip = Programs.begin(); ip != Programs.end(); ip++) { if (ip->second == name) { Table.pb(*ip); Programs.erase(ip); break; } } } sort(Programs.begin(), Programs.end()); rep(i, Programs.size()) { Table.pb(Programs[i]); } RangeHandler rg; bool ans = true; rep(i, Table.size()) { if (rg.insert(Range(Table[i].first, Table[i].first + 30))) { ans &= true; result++; } else ans = false; if (i < P && !ans) { result = -1; break; } } cout << result << endl; } return 0; }
//============================================================================ // Name : TopCoderCompetition.cpp // Author : taguchi // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; //#define P pair<int,int> #define rep(i, n) for (int i = 0; i < n; i++) #define pb(n) push_back(n) typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<string> vs; struct edge { int cost, to; }; vector<string> split(string &in, char delimiter) { vector<string> re; uint cur = 0, next; while ((next = in.find_first_of(delimiter, cur)) != string::npos) { re.pb(string(in, cur, next - cur)); cur = next + 1; } re.pb(string(in, cur, in.size() - cur)); return re; } vector<int> split_int(string &in, char delimiter) { vector<string> str = split(in, delimiter); vector<int> re; rep(i, str.size()) { re.pb(strtol(str[i].c_str(), NULL, 10)); } return re; } class Range { public: int a, b; Range(int an, int bn) : a(an), b(bn) {} bool operator()(int n) { return a <= n && n <= b; } bool isCross(Range x) { return x(a) || x(b); } bool contains(Range x) { return this->a <= x.a && x.b <= this->b; } }; class RangeHandler { public: list<Range> Ranges; bool insert(Range x) { if (Ranges.empty()) { Ranges.push_back(x); return true; } if (x.b <= Ranges.front().a) { Ranges.push_front(x); return true; } if (Ranges.back().b <= x.a) { Ranges.push_back(x); return true; } for (list<Range>::iterator i = Ranges.begin(); i != Ranges.end(); i++) { list<Range>::iterator l = i; l++; // cout << "dbg" << i->b << " " << i->a << endl; if (Range(i->b, l->a).contains(x)) { Ranges.insert(l, x); return true; } } return false; } }; int N, P; int main() { while (true) { int result = 0; cin >> N; if (N == 0) break; vector<pair<int, string>> Programs, Table; vector<pair<int, string>>::iterator ip; rep(i, N) { string name; cin >> name; int weekday, start; cin >> weekday >> start; start -= 600; int time = 1440 * weekday + (start / 100) * 60 + (start % 100); Programs.pb(make_pair(time, name)); } cin >> P; rep(i, P) { string name; cin >> name; for (ip = Programs.begin(); ip != Programs.end(); ip++) { if (ip->second == name) { Table.pb(*ip); Programs.erase(ip); break; } } } sort(Programs.begin(), Programs.end()); rep(i, Programs.size()) { Table.pb(Programs[i]); } RangeHandler rg; bool ans = true; rep(i, Table.size()) { if (rg.insert(Range(Table[i].first, Table[i].first + 30))) { ans &= true; result++; } else ans = false; if (i < P && !ans) { result = -1; break; } } cout << result << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
835
#include <algorithm> #include <iostream> #include <map> #include <set> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) static const int MAX = 500; static const int L = 30; bool overlap(int a, int b) { if (a > b) swap(a, b); return a + 30 > b; } int getProgram(vector<int> v) { sort(v.begin(), v.end()); int dp[MAX]; int maxv = v.size() > 0 ? 1 : 0; for (int i = 0; i < v.size(); i++) dp[i] = 1; for (int i = 1; i < v.size(); i++) { for (int j = i - 1; j >= 0; j--) { if (!overlap(v[j], v[i])) { dp[i] = dp[j] + 1; maxv = max(maxv, dp[i]); break; } } } return maxv; } int solve(int N) { int in[MAX], w, s, P; map<string, int> M; string name; vector<int> F, T; rep(i, N) { cin >> name >> w >> s; M[name] = i; in[i] = w * 1440 + (s / 100) * 60 + s % 100; } cin >> P; rep(i, P) { cin >> name; F.push_back(in[M[name]]); } int ans = getProgram(F); if (ans == 0) return -1; rep(i, N) { bool f = true; for (int j = 0; j < F.size(); j++) { if (overlap(F[j], in[i])) { f = false; break; } } if (f) T.push_back(in[i]); } return ans + getProgram(T); } main() { int N; while (1) { cin >> N; if (N == 0) break; cout << solve(N) << endl; } }
#include <algorithm> #include <iostream> #include <map> #include <set> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) static const int MAX = 500; static const int L = 30; bool overlap(int a, int b) { if (a > b) swap(a, b); return a + 30 > b; } int getProgram(vector<int> v) { sort(v.begin(), v.end()); int dp[MAX]; int maxv = v.size() > 0 ? 1 : 0; for (int i = 0; i < v.size(); i++) dp[i] = 1; for (int i = 1; i < v.size(); i++) { for (int j = i - 1; j >= 0; j--) { if (!overlap(v[j], v[i])) { dp[i] = dp[j] + 1; maxv = max(maxv, dp[i]); break; } } } return maxv; } int solve(int N) { int in[MAX], w, s, P; map<string, int> M; string name; vector<int> F, T; rep(i, N) { cin >> name >> w >> s; M[name] = i; in[i] = w * 1440 + (s / 100) * 60 + s % 100; } cin >> P; rep(i, P) { cin >> name; F.push_back(in[M[name]]); } int ans = getProgram(F); if (ans != P) return -1; rep(i, N) { bool f = true; for (int j = 0; j < F.size(); j++) { if (overlap(F[j], in[i])) { f = false; break; } } if (f) T.push_back(in[i]); } return ans + getProgram(T); } main() { int N; while (1) { cin >> N; if (N == 0) break; cout << solve(N) << endl; } }
[["-", 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, 22]]
1
468
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // The House of Huge Family public class Main { class E { int s, t, id; long c; public E(int s, int t, long c, int id) { this.s = s; this.t = t; this.c = c; this.id = id; } } int n, m; List<E>[] adj; boolean isCon(int f1, int f2) { boolean[] u = new boolean[n]; List<Integer> l = new ArrayList<Integer>(); l.add(0); u[0] = true; int N = 1; while (!l.isEmpty()) { List<Integer> next = new ArrayList<Integer>(); for (int i : l) { N++; for (E e : adj[i]) if (!u[e.t] && e.id != f1 && e.id != f2) { u[e.t] = true; next.add(e.t); } } l = next; } return N == n; } @SuppressWarnings("unchecked") void run() { Scanner sc = new Scanner(System.in); for (;;) { n = sc.nextInt(); m = sc.nextInt(); if ((n | m) == 0) break; adj = new List[n]; for (int i = 0; i < n; i++) adj[i] = new ArrayList<E>(); int N = 0; long M = 0; List<Long> cost = new ArrayList<Long>(); while (m-- != 0) { int s = sc.nextInt(), t = sc.nextInt(); long c = sc.nextInt(); if (c <= 0) M += c; else { cost.add(c); adj[s].add(new E(s, t, c, N)); adj[t].add(new E(t, s, c, N)); N++; } } long min = 1L << 40; if (isCon(-1, -1)) min = 0; for (int i = 0; i < N; i++) if (isCon(i, -1)) min = Math.min(min, cost.get(i)); for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) if (isCon(i, j)) min = Math.min(min, cost.get(i) + cost.get(j)); System.out.println(M + min); } } public static void main(String[] args) { new Main().run(); } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // The House of Huge Family public class Main { class E { int s, t, id; long c; public E(int s, int t, long c, int id) { this.s = s; this.t = t; this.c = c; this.id = id; } } int n, m; List<E>[] adj; boolean isCon(int f1, int f2) { boolean[] u = new boolean[n]; List<Integer> l = new ArrayList<Integer>(); l.add(0); u[0] = true; int N = 0; while (!l.isEmpty()) { List<Integer> next = new ArrayList<Integer>(); for (int i : l) { N++; for (E e : adj[i]) if (!u[e.t] && e.id != f1 && e.id != f2) { u[e.t] = true; next.add(e.t); } } l = next; } return N == n; } @SuppressWarnings("unchecked") void run() { Scanner sc = new Scanner(System.in); for (;;) { n = sc.nextInt(); m = sc.nextInt(); if ((n | m) == 0) break; adj = new List[n]; for (int i = 0; i < n; i++) adj[i] = new ArrayList<E>(); int N = 0; long M = 0; List<Long> cost = new ArrayList<Long>(); while (m-- != 0) { int s = sc.nextInt(), t = sc.nextInt(); long c = sc.nextInt(); if (c <= 0) M += c; else { cost.add(c); adj[s].add(new E(s, t, c, N)); adj[t].add(new E(t, s, c, N)); N++; } } long min = 1L << 40; if (!isCon(-1, -1)) min = 0; for (int i = 0; i < N; i++) if (!isCon(i, -1)) min = Math.min(min, cost.get(i)); for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) if (!isCon(i, j)) min = Math.min(min, cost.get(i) + cost.get(j)); System.out.println(M + min); } } public static void main(String[] args) { new Main().run(); } }
[["-", 0, 195, 8, 196, 0, 503, 49, 200, 51, 499], ["+", 0, 195, 8, 196, 0, 503, 49, 200, 51, 499], ["+", 8, 196, 0, 57, 15, 15, 0, 91, 17, 111], ["+", 0, 7, 8, 57, 15, 15, 0, 91, 17, 111], ["+", 8, 7, 8, 57, 15, 15, 0, 91, 17, 111]]
3
620
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node> *_graph; bool *_used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node &e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d > 0) { Node &rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson &f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool *)_used, (bool *)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool *)_used, (bool *)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node> *graph() const { return _graph; } }; class UnionFindTree { private: int *par; int *rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); uft.unite(from, to); } int tree_count = 0; bool visited[101]; memset(visited, false, sizeof(visited)); for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!visited[uft.find(house_i)]) tree_count++; visited[uft.find(house_i)] = true; } int res = numeric_limits<int>::max(); if (tree_count >= 2) { res = 0; } else { for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%d\n", bonus + res); } }
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node> *_graph; bool *_used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node &e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d > 0) { Node &rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson &f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool *)_used, (bool *)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, flow)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool *)_used, (bool *)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node> *graph() const { return _graph; } }; class UnionFindTree { private: int *par; int *rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); uft.unite(from, to); } int tree_count = 0; bool visited[101]; memset(visited, false, sizeof(visited)); for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!visited[uft.find(house_i)]) tree_count++; visited[uft.find(house_i)] = true; } int res = numeric_limits<int>::max(); if (tree_count >= 2) { res = 0; } else { for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%d\n", bonus + res); } }
[["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22]]
1
1,124
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> //#include <utility> //#include <memory> //#include <functional> //#include <deque> //#include <cctype> //#include <ctime> //#include <numeric> //#include <list> //#include <iomanip> //#if __cplusplus >= 201103L //#include <array> //#include <tuple> //#include <initializer_list> //#include <forward_list> // //#define cauto const auto& //#else //#endif using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<vector<int>> vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long>> vvll, vvLL; #define VV(T) vector<vector<T>> template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; } #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define reep(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) reep((i), 0, (n)) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define F first #define S second #define mkp make_pair #define RALL(v) (v).rbegin(), (v).rend() #define DEBUG #ifdef DEBUG #define dump(x) cout << #x << " = " << (x) << endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; #else #define dump(x) #define debug(x) #endif #define LDcout(x, n) fixed << setprecision(n) << x #define MOD 1000000007LL #define EPS 1e-8 static const int INF = 1 << 24; // max flow (dinic) template <class V> class MaxFlow_dinic { public: struct edge { int to, reve; V cap; }; static const int MV = 1100; vector<edge> E[MV]; int itr[MV], lev[MV]; void add_edge(int x, int y, V cap, bool undir = false) { E[x].push_back((edge){y, (int)E[y].size(), cap}); E[y].push_back((edge){x, (int)E[x].size() - 1, undir ? cap : 0}); } void bfs(int cur) { memset(lev, 0xff, sizeof(lev)); queue<int> q; lev[cur] = 0; q.push(cur); while (q.size()) { int v = q.front(); q.pop(); for (__typeof(E[v].begin()) e = E[v].begin(); e != E[v].end(); e++) if (e->cap > 0 && lev[e->to] < 0) lev[e->to] = lev[v] + 1, q.push(e->to); } } V dfs(int from, int to, V cf) { if (from == to) { return cf; } for (; itr[from] < E[from].size(); itr[from]++) { edge *e = &E[from][itr[from]]; if (e->cap > 0 && lev[from] < lev[e->to]) { V f = dfs(e->to, to, min(cf, e->cap)); if (f > 0) { e->cap -= f; E[e->to][e->reve].cap += f; return f; } } } return 0; } V maxflow(int from, int to) { V fl = 0, tf; while (1) { bfs(from); if (lev[to] < 0) return fl; memset(itr, 0, sizeof(itr)); while ((tf = dfs(from, to, numeric_limits<V>::max())) > 0) fl += tf; } } }; void mainmain() { int n, m; while (cin >> n >> m, n || m) { ll ans = INT_MAX; vint a(m), b(m), c(m); rep(i, m) cin >> a[i] >> b[i] >> c[i]; reep(i, 1, n) { MaxFlow_dinic<ll> mf; int tmp = 0; rep(j, m) { if (c[j] < 0) tmp += c[j]; else mf.add_edge(a[j], b[j], (ll)c[j]); } ans = min(ans, tmp + mf.maxflow(0, i)); } cout << ans << endl; } } signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); mainmain(); }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> //#include <utility> //#include <memory> //#include <functional> //#include <deque> //#include <cctype> //#include <ctime> //#include <numeric> //#include <list> //#include <iomanip> //#if __cplusplus >= 201103L //#include <array> //#include <tuple> //#include <initializer_list> //#include <forward_list> // //#define cauto const auto& //#else //#endif using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<vector<int>> vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long>> vvll, vvLL; #define VV(T) vector<vector<T>> template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; } #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define reep(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) reep((i), 0, (n)) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define F first #define S second #define mkp make_pair #define RALL(v) (v).rbegin(), (v).rend() #define DEBUG #ifdef DEBUG #define dump(x) cout << #x << " = " << (x) << endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; #else #define dump(x) #define debug(x) #endif #define LDcout(x, n) fixed << setprecision(n) << x #define MOD 1000000007LL #define EPS 1e-8 static const int INF = 1 << 24; // max flow (dinic) template <class V> class MaxFlow_dinic { public: struct edge { int to, reve; V cap; }; static const int MV = 1100; vector<edge> E[MV]; int itr[MV], lev[MV]; void add_edge(int x, int y, V cap, bool undir = false) { E[x].push_back((edge){y, (int)E[y].size(), cap}); E[y].push_back((edge){x, (int)E[x].size() - 1, undir ? cap : 0}); } void bfs(int cur) { memset(lev, 0xff, sizeof(lev)); queue<int> q; lev[cur] = 0; q.push(cur); while (q.size()) { int v = q.front(); q.pop(); for (__typeof(E[v].begin()) e = E[v].begin(); e != E[v].end(); e++) if (e->cap > 0 && lev[e->to] < 0) lev[e->to] = lev[v] + 1, q.push(e->to); } } V dfs(int from, int to, V cf) { if (from == to) { return cf; } for (; itr[from] < E[from].size(); itr[from]++) { edge *e = &E[from][itr[from]]; if (e->cap > 0 && lev[from] < lev[e->to]) { V f = dfs(e->to, to, min(cf, e->cap)); if (f > 0) { e->cap -= f; E[e->to][e->reve].cap += f; return f; } } } return 0; } V maxflow(int from, int to) { V fl = 0, tf; while (1) { bfs(from); if (lev[to] < 0) return fl; memset(itr, 0, sizeof(itr)); while ((tf = dfs(from, to, numeric_limits<V>::max())) > 0) fl += tf; } } }; void mainmain() { int n, m; while (cin >> n >> m, n || m) { ll ans = INT_MAX; vint a(m), b(m), c(m); rep(i, m) cin >> a[i] >> b[i] >> c[i]; reep(i, 1, n) { MaxFlow_dinic<ll> mf; int tmp = 0; rep(j, m) { if (c[j] < 0) tmp += c[j]; else mf.add_edge(a[j], b[j], (ll)c[j], true); } ans = min(ans, tmp + mf.maxflow(0, i)); } cout << ans << endl; } } signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); mainmain(); }
[["+", 75, 76, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 75, 76, 0, 1, 0, 2, 3, 4, 0, 146]]
1
1,017
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; #define eps 1e-8 #define pi acos(-1.0) #define inf 1 << 30 #define linf 1LL << 60 #define pb push_back #define lc(x) (x << 1) #define rc(x) (x << 1 | 1) #define lowbit(x) (x & (-x)) #define ll long long #define maxn 110 int maz[maxn][maxn]; int n, m; int stoer_wagner(int n) { bool vis[maxn]; int node[maxn], dis[maxn]; int maxj, prev; int ans = inf; memset(dis, 0, sizeof(dis)); for (int i = 0; i < n; i++) node[i] = i; while (n > 1) { prev = 0; maxj = 1; memset(vis, false, sizeof(vis)); vis[node[0]] = true; for (int i = 1; i < n; i++) { dis[node[i]] = maz[node[0]][node[i]]; if (dis[node[i]] > dis[node[maxj]]) maxj = i; } for (int i = 1; i < n; i++) { if (i == n - 1) { //蜿ェ蜑ゥ譛?錘荳?クェ轤ケ蟆ア蠑?ァ句粋蟷カ轤ケ ans = (dis[node[maxj]] < ans ? dis[node[maxj]] : ans); for (int k = 0; k < n; k++) { //蜷亥ケカ轤ケ?梧峩譁ー霎ケ譚??邉サ maz[node[k]][node[prev]] += maz[node[k]][node[maxj]]; maz[node[prev]][node[k]] = maz[node[k]][node[prev]]; } node[maxj] = node[--n]; //郛ゥ轤ケ } prev = maxj; vis[node[maxj]] = true; maxj = -1; for (int k = 1; k < n; k++) { if (!vis[node[k]]) { dis[node[k]] += maz[node[prev]][node[k]]; if (maxj == -1 || dis[node[k]] > dis[node[maxj]]) maxj = k; } } } } return ans; } int main() { while (~scanf("%d%d", &n, &m)) { if (n == 0 && m == 0) break; memset(maz, 0, sizeof(maz)); int a, b, c, res = 0; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); if (c < 0) { res += -c; continue; } maz[a][b] += c; maz[b][a] += c; } printf("%d\n", stoer_wagner(n) + res); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; #define eps 1e-8 #define pi acos(-1.0) #define inf 1 << 30 #define linf 1LL << 60 #define pb push_back #define lc(x) (x << 1) #define rc(x) (x << 1 | 1) #define lowbit(x) (x & (-x)) #define ll long long #define maxn 110 int maz[maxn][maxn]; int n, m; int stoer_wagner(int n) { bool vis[maxn]; int node[maxn], dis[maxn]; int maxj, prev; int ans = inf; memset(dis, 0, sizeof(dis)); for (int i = 0; i < n; i++) node[i] = i; while (n > 1) { prev = 0; maxj = 1; memset(vis, false, sizeof(vis)); vis[node[0]] = true; for (int i = 1; i < n; i++) { dis[node[i]] = maz[node[0]][node[i]]; if (dis[node[i]] > dis[node[maxj]]) maxj = i; } for (int i = 1; i < n; i++) { if (i == n - 1) { //蜿ェ蜑ゥ譛?錘荳?クェ轤ケ蟆ア蠑?ァ句粋蟷カ轤ケ ans = (dis[node[maxj]] < ans ? dis[node[maxj]] : ans); for (int k = 0; k < n; k++) { //蜷亥ケカ轤ケ?梧峩譁ー霎ケ譚??邉サ maz[node[k]][node[prev]] += maz[node[k]][node[maxj]]; maz[node[prev]][node[k]] = maz[node[k]][node[prev]]; } node[maxj] = node[--n]; //郛ゥ轤ケ } prev = maxj; vis[node[maxj]] = true; maxj = -1; for (int k = 1; k < n; k++) { if (!vis[node[k]]) { dis[node[k]] += maz[node[prev]][node[k]]; if (maxj == -1 || dis[node[k]] > dis[node[maxj]]) maxj = k; } } } } return ans; } int main() { while (~scanf("%d%d", &n, &m)) { if (n == 0 && m == 0) break; memset(maz, 0, sizeof(maz)); int a, b, c, res = 0; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); if (c < 0) { res += c; continue; } maz[a][b] += c; maz[b][a] += c; } printf("%d\n", stoer_wagner(n) + res); } return 0; }
[["-", 64, 9, 0, 1, 0, 11, 12, 91, 17, 33]]
1
623
#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) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst, cap, rev; Edge(int u, int v, int c, int r) : src(u), dst(v), cap(c), rev(r) {} }; typedef vector<Edge> Node; typedef vector<Node> Graph; void add_edge(Graph &G, int a, int b, int c) { G[a].push_back(Edge(a, b, c, G[b].size())); G[b].push_back(Edge(b, a, 0, G[a].size() - 1)); } int dfs(Graph &G, int u, int t, int f, vector<bool> &used) { if (u == t) return f; used[u] = true; FORIT(e, G[u]) if (e->cap > 0 && !used[e->dst]) { int g = dfs(G, e->dst, t, min(f, e->cap), used); if (g > 0) { e->cap -= g; G[e->dst][e->rev].cap += g; return g; } } return 0; } int max_flow(Graph G, int s, int t) { int N = G.size(); int flow = 0; while (true) { vector<bool> used(N, false); int f = dfs(G, s, t, INF, used); if (f == 0) return flow; flow += f; } } int main() { int N, M; while (cin >> N >> M && N) { Graph G(N); int minus = 0; REP(i, M) { int a, b, c; cin >> a >> b >> c; if (c < 0) { minus += c; continue; } add_edge(G, a, b, c); } int ans = INF; FOR(t, 1, N) { ans = min(ans, max_flow(G, 0, t)); } cout << ans + minus << 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) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst, cap, rev; Edge(int u, int v, int c, int r) : src(u), dst(v), cap(c), rev(r) {} }; typedef vector<Edge> Node; typedef vector<Node> Graph; void add_edge(Graph &G, int a, int b, int c) { G[a].push_back(Edge(a, b, c, G[b].size())); G[b].push_back(Edge(b, a, c, G[a].size() - 1)); } int dfs(Graph &G, int u, int t, int f, vector<bool> &used) { if (u == t) return f; used[u] = true; FORIT(e, G[u]) if (e->cap > 0 && !used[e->dst]) { int g = dfs(G, e->dst, t, min(f, e->cap), used); if (g > 0) { e->cap -= g; G[e->dst][e->rev].cap += g; return g; } } return 0; } int max_flow(Graph G, int s, int t) { int N = G.size(); int flow = 0; while (true) { vector<bool> used(N, false); int f = dfs(G, s, t, INF, used); if (f == 0) return flow; flow += f; } } int main() { int N, M; while (cin >> N >> M && N) { Graph G(N); int minus = 0; REP(i, M) { int a, b, c; cin >> a >> b >> c; if (c <= 0) { minus += c; continue; } add_edge(G, a, b, c); } int ans = INF; FOR(t, 1, N) { ans = min(ans, max_flow(G, 0, t)); } cout << ans + minus << endl; } return 0; }
[["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
675
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <memory.h> #include <queue> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, m, n) for (int i = m; i < n; i++) typedef long long ll; typedef pair<int, int> P; const int INF = 1 << 29; const double EPS = 1e-10; const ll mod = 100000007; ll dp1[100010]; ll dp0[100010]; ll sum0[100010]; ll sum1[100010]; void maketable(ll *dp, ll *sum, int w) { memset(dp, 0, sizeof(dp)); memset(sum, 0, sizeof(sum)); dp[0] = 1; for (int i = 1; i < 100010; i++) { for (int j = 1; j <= w; j++) { if (i - j >= 0) dp[i] = (dp[i] + dp[i - j]) % mod; } } rep(i, 100010) { sum[i] = dp[i]; if (i > w) sum[i] = (sum[i] + sum[i - w]) % mod; } } int main() { string str; maketable(dp0, sum0, 3); maketable(dp1, sum1, 5); while (cin >> str && str != "0") { int pos = 0; ll res = 1; while (pos < (int)str.size()) { char ch = str[pos]; int len = 0; while (pos < (int)str.size() && str[pos] == ch) { pos++; len++; } if (ch == '8' || ch == '0') { res = (res * sum0[len]) % mod; } else { res = (res * sum1[len]) % mod; } } cout << res << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <memory.h> #include <queue> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, m, n) for (int i = m; i < n; i++) typedef long long ll; typedef pair<int, int> P; const int INF = 1 << 29; const double EPS = 1e-10; const ll mod = 100000007; ll dp1[100010]; ll dp0[100010]; ll sum0[100010]; ll sum1[100010]; void maketable(ll *dp, ll *sum, int w) { memset(dp, 0, sizeof(dp)); memset(sum, 0, sizeof(sum)); dp[0] = 1; for (int i = 1; i < 100010; i++) { for (int j = 1; j <= w; j++) { if (i - j >= 0) dp[i] = (dp[i] + dp[i - j]) % mod; } } rep(i, 100010) { sum[i] = dp[i]; if (i > w) sum[i] = (sum[i] + sum[i - w]) % mod; } } int main() { string str; maketable(dp0, sum0, 3); maketable(dp1, sum1, 5); while (cin >> str && str != "#") { int pos = 0; ll res = 1; while (pos < (int)str.size()) { char ch = str[pos]; int len = 0; while (pos < (int)str.size() && str[pos] == ch) { pos++; len++; } if (ch == '8' || ch == '0') { res = (res * sum0[len]) % mod; } else { res = (res * sum1[len]) % mod; } } cout << res << endl; } return 0; }
[["-", 15, 339, 51, 16, 12, 16, 12, 5, 0, 6], ["+", 15, 339, 51, 16, 12, 16, 12, 5, 0, 6]]
1
415
#include <iostream> enum { N = 100000, M = 100000007 }; int d[N + 1][2], t, i, j, l[2] = {3, 5}; main() { for (; t < 2; t++) { for (d[1][t] = i = 1; i++ < l[t];) d[i][t] = d[i - 1][t] * 2; for (i = l[t]; i++ < N;) for (d[i][t] = 1, j = 0; j++ < l[t];) d[i][t] = (d[i][t] + d[i - j][t]) % M; } for (std::string s; std::cin >> s, s != "#"; printf("%d\n", t)) for (s += (t = j = i = 1); i < s.size(); i++) s[i - 1] - s[i] ? (t = t * d[j][s[i - 1] - 56 && s[i - 1] - 48] % M), j = 1 : j++; }
#include <iostream> enum { N = 100000, M = 100000007 }; long long d[N + 1][2]; int t, i, j, l[2] = {3, 5}; main() { for (; t < 2; t++) { for (d[1][t] = i = 1; i++ < l[t];) d[i][t] = d[i - 1][t] * 2; for (i = l[t]; i++ < N;) for (d[i][t] = 1, j = 0; j++ < l[t];) d[i][t] = (d[i][t] + d[i - j][t]) % M; } for (std::string s; std::cin >> s, s != "#"; printf("%d\n", t)) for (s += (t = j = i = 1); i < s.size(); i++) s[i - 1] - s[i] ? (t = t * d[j][s[i - 1] - 56 && s[i - 1] - 48] % M), j = 1 : j++; }
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 0, 30, 0, 43, 39, 86, 0, 96], ["-", 36, 36, 36, 36, 0, 30, 0, 43, 0, 21], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 0, 35], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40]]
1
276
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (;;) { int n = in.nextInt(); int k = in.nextInt(); if ((n | k) == 0) return; int a[] = new int[n]; int max = 0; int cmax = 0; for (int i = 0; i < n; i++) a[i] = in.nextInt(); for (int i = 0; i < n - k + 1; i++) { int mul = 1; for (int j = i; j < i + k; j++) mul *= a[j]; max = Math.max(max, mul); } int ans = -1; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { int b[] = a.clone(); int tmp = b[i]; b[i] = b[j]; b[j] = tmp; for (int s = 0; s < n - k + 1; s++) { int mul = 1; for (int t = s; t < s + k; t++) mul *= b[t]; cmax = Math.max(cmax, mul); } ans = Math.max(ans, max - cmax); } System.out.println(ans >= 0 ? ans : "NO GAME"); } } static public void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (;;) { int n = in.nextInt(); int k = in.nextInt(); if ((n | k) == 0) return; int a[] = new int[n]; int max = 0; int cmax = 0; for (int i = 0; i < n; i++) a[i] = in.nextInt(); for (int i = 0; i < n - k + 1; i++) { int mul = 1; for (int j = i; j < i + k; j++) mul *= a[j]; max = Math.max(max, mul); } int ans = -1; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { int b[] = a.clone(); int tmp = b[i]; b[i] = b[j]; b[j] = tmp; for (int s = 0; s < n - k + 1; s++) { int mul = 1; for (int t = s; t < s + k; t++) mul *= b[t]; cmax = Math.max(cmax, mul); } ans = Math.max(ans, cmax - max); } System.out.println(ans >= 0 ? ans : "NO GAME"); } } static public void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
[["-", 0, 11, 12, 492, 3, 4, 0, 16, 31, 22], ["-", 0, 11, 12, 492, 3, 4, 0, 16, 17, 33], ["+", 0, 11, 12, 492, 3, 4, 0, 16, 17, 33], ["+", 0, 11, 12, 492, 3, 4, 0, 16, 12, 22]]
3
365
#include <bits/stdc++.h> using namespace std; #define REP(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) int main() { int N, K; while (cin >> N >> K && (N | K)) { int C[N]; rep(i, N) { cin >> C[i]; } int mx1 = 1; rep(i, N) { int mul = 1; REP(j, i, i + K) { if (j >= N) { mul = 0; break; } mul *= C[j]; } mx1 = max(mx1, mul); } int ans = -1 << 29; rep(i, N) REP(j, i + 1, N) { swap(C[i], C[j]); rep(k, N) { int mul = 1; REP(l, k, l + K) { if (l >= N) { mul = -1 << 29; break; } mul *= C[l]; } ans = max(ans, mul - mx1); } swap(C[i], C[j]); } if (ans < 0) { cout << 0 << endl; } else { cout << ans << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) int main() { int N, K; while (cin >> N >> K && (N | K)) { int C[N]; rep(i, N) { cin >> C[i]; } int mx1 = 1; rep(i, N) { int mul = 1; REP(j, i, i + K) { if (j >= N) { mul = 0; break; } mul *= C[j]; } mx1 = max(mx1, mul); } int ans = -1 << 29; rep(i, N) REP(j, i + 1, N) { swap(C[i], C[j]); rep(k, N) { int mul = 1; REP(l, k, k + K) { if (l >= N) { mul = -1 << 29; break; } mul *= C[l]; } ans = max(ans, mul - mx1); } swap(C[i], C[j]); } if (ans < 0) { cout << "NO GAME\n"; } else { cout << ans << endl; } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["-", 64, 9, 0, 1, 0, 16, 31, 16, 17, 151], ["-", 64, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["-", 0, 57, 64, 9, 0, 1, 0, 16, 12, 22], ["+", 64, 9, 0, 1, 0, 16, 12, 5, 0, 62], ["+", 64, 9, 0, 1, 0, 16, 12, 5, 0, 6], ["+", 64, 9, 0, 1, 0, 16, 12, 5, 0, 44]]
1
272