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 <bits/stdc++.h> using namespace std; int main() { int a, b; string op; cin >> a; while (1) { cin >> op; if (op == "=") break; cin >> b; if (op == "+") a = a + b; if (op == "-") a = a - b; if (op == "*") a = a * b; if (op == "/") a = a / b; cout << a << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; string op; cin >> a; while (1) { cin >> op; if (op == "=") break; cin >> b; if (op == "+") a = a + b; if (op == "-") a = a - b; if (op == "*") a = a * b; if (op == "/") a = a / b; } cout << a << endl; return 0; }
[["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46], ["-", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
1
113
#include <iostream> using namespace std; int main(void) { int in; char TDN; int YJSNPI; cin >> in; cin >> TDN; YJSNPI = in; while (1) { if (TDN == '=') { cout << YJSNPI << endl; break; } cin >> in; if (TDN == '*') YJSNPI += in; if (TDN == '+') YJSNPI += in; if (TDN == '/') YJSNPI /= in; if (TDN == '-') YJSNPI -= in; cin >> TDN; } return 0; }
#include <iostream> using namespace std; int main(void) { int in; char TDN; int YJSNPI; cin >> in; cin >> TDN; YJSNPI = in; while (1) { if (TDN == '=') { cout << YJSNPI << endl; break; } cin >> in; if (TDN == '*') YJSNPI *= in; if (TDN == '+') YJSNPI += in; if (TDN == '/') YJSNPI /= in; if (TDN == '-') YJSNPI -= in; cin >> TDN; } return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 17, 107], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 17, 108]]
1
117
#include <cstdio> using namespace std; int in(void) { int x; scanf("%d", &x); return (x); } int main() { char symbol = '+'; int ans = 0; do { if (symbol == '+') ans += in(); if (symbol == '-') ans -= in(); if (symbol == '*') ans *= in(); if (symbol == '/') ans /= in(); scanf(" %c", &symbol); } while (symbol != '='); printf("%d\n", symbol); return (0); }
#include <cstdio> using namespace std; int in(void) { int x; scanf("%d", &x); return (x); } int main() { char symbol = '+'; int ans = 0; do { if (symbol == '+') ans += in(); if (symbol == '-') ans -= in(); if (symbol == '*') ans *= in(); if (symbol == '/') ans /= in(); scanf(" %c", &symbol); } while (symbol != '='); printf("%d\n", ans); return (0); }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
142
nums = [] oprs = [] loop do t = gets.strip if t == "=" break elsif t.match /^\d+$/ nums.push t.to_i else oprs.push t.to_sym end end p nums.inject do |a, b| a.method(oprs.shift).call(b) end
nums = [] oprs = [] loop do t = gets.strip if t == "=" break elsif t.match /^\d+$/ nums.push t.to_i else oprs.push t.to_sym end end p nums.inject{|a, b| a.method(oprs.shift).call(b) }
[["-", 36, 36, 0, 493, 0, 652, 196, 737, 0, 170], ["+", 0, 652, 3, 4, 0, 652, 196, 196, 0, 45], ["-", 36, 36, 0, 493, 0, 652, 196, 737, 0, 444], ["+", 0, 652, 3, 4, 0, 652, 196, 196, 0, 46]]
4
70
a=$<.map(&:strip) r=a[0] c=1 while 2*c<a.size r=eval(r+a[2*c-1]+a[2*c]).to_s c+=1 end p r
a=$<.map(&:strip) r=a[0] c=1 while 2*c<a.size r=eval(r+a[2*c-1]+a[2*c]).to_s c+=1 end puts r
[["-", 36, 36, 36, 36, 0, 493, 0, 652, 735, 22], ["+", 36, 36, 36, 36, 0, 493, 0, 652, 735, 22]]
4
56
a = int(input()) while True: b = input(); if b == '=': break c = int(input()) if b == '+': a += c elif b == '-': a -= c elif b == '*': a *= c elif b == '/': a /= c print(a)
a = int(input()) while True: b = input(); if b == '=': break c = int(input()) if b == '+': a += c elif b == '-': a -= c elif b == '*': a *= c elif b == '/': a //= c print(a)
[["-", 75, 665, 64, 196, 0, 1, 0, 677, 17, 90], ["+", 75, 665, 64, 196, 0, 1, 0, 677, 17, 691]]
5
77
# AOJ 0588: Simple Calculator # Python3 2018.6.30 bal4u a = int(input()) while True: op = input().strip() if op == '=': break b = int(input()) if op == '+': a += b elif s == '-': a -= b elif s == '*': a *= b else: a //= b print(a)
# AOJ 0588: Simple Calculator # Python3 2018.6.30 bal4u a = int(input()) while True: op = input().strip() if op == '=': break b = int(input()) if op == '+': a += b elif op == '-': a -= b elif op == '*': a *= b else: a //= b print(a)
[["-", 8, 196, 0, 57, 75, 665, 15, 666, 0, 22], ["+", 8, 196, 0, 57, 75, 665, 15, 666, 0, 22]]
5
77
#include <stdio.h> #include <string.h> #define SWAP(type, a, b) \ do { \ type temp = a; \ a = b; \ b = temp; \ } while (0) typedef struct { char name[5]; int many; } P; int main() { int i, j, n, b, cnt[5], flg, c; char nametmp[5]; scanf("%d", &n); P a[5][n]; for (i = 0; i < 5; i++) cnt[i] = 0; for (i = 0; i < n; i++) { scanf("%s %d", nametmp, &b); c = strlen(nametmp) - 1; for (j = 0, flg = 0; j < cnt[c]; j++) { if (strcmp(nametmp, a[c][j].name) == 0) { a[c][j].many += b; flg = 1; break; } } if (flg == 0) { strcpy(a[c][cnt[c]].name, nametmp); a[c][cnt[c]].many = b; cnt[c]++; } } for (c = 0; c < 5; c++) { for (i = 0; i < cnt[c] - 1; i++) { for (j = cnt[c] - 1; j < i; j--) { if (strcmp(a[c][j].name, a[c][j - 1].name) < 0) { SWAP(P, a[c][j], a[c][j - 1]); } } } } for (c = 0; c < 5; c++) { for (j = 0; j < cnt[c]; j++) { printf("%s %d\n", a[c][j].name, a[c][j].many); } } return 0; }
#include <stdio.h> #include <string.h> #define SWAP(type, a, b) \ do { \ type temp = a; \ a = b; \ b = temp; \ } while (0) typedef struct { char name[5]; int many; } P; int main() { int i, j, n, b, cnt[5], flg, c; char nametmp[5]; scanf("%d", &n); P a[5][n]; for (i = 0; i < 5; i++) cnt[i] = 0; for (i = 0; i < n; i++) { scanf("%s %d", nametmp, &b); c = strlen(nametmp) - 1; for (j = 0, flg = 0; j < cnt[c]; j++) { if (strcmp(nametmp, a[c][j].name) == 0) { a[c][j].many += b; flg = 1; break; } } if (flg == 0) { strcpy(a[c][cnt[c]].name, nametmp); a[c][cnt[c]].many = b; cnt[c]++; } } for (c = 0; c < 5; c++) { for (i = 0; i < cnt[c] - 1; i++) { for (j = cnt[c] - 1; j > i; j--) { if (strcmp(a[c][j].name, a[c][j - 1].name) < 0) { SWAP(P, a[c][j], a[c][j - 1]); } } } } for (c = 0; c < 5; c++) { for (j = 0; j < cnt[c]; j++) { printf("%s %d\n", a[c][j].name, a[c][j].many); } } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 47]]
0
416
#include <cmath> #include <cstring> #include <iostream> #include <string> using namespace std; int k[16000000]; int n, a, z, cnt; string S; int main() { memset(k, 0, sizeof(k)); cin >> n; for (int i = 0; i < n; i++) { cin >> S; z = 0; for (int j = 0; j < S.size(); j++) { z += (int)(S[j] - 'A' + 1) * pow(27, S.size() - j - 1); } cin >> a; k[z] += a; } for (int i = 0; i < 16000000; i++) { if (k[i] >= 1) { for (int j = 4; j >= 0; j--) { z = (int)(i / (int)pow(27, j)) % 27; } if (z >= 1) { cout << (char)(z + 'A' - 1); } cout << ' '; cout << k[i] << endl; } } return 0; }
#include <cmath> #include <cstring> #include <iostream> #include <string> using namespace std; int k[16000000]; int n, a, z, cnt; string S; int main() { memset(k, 0, sizeof(k)); cin >> n; for (int i = 0; i < n; i++) { cin >> S; z = 0; for (int j = 0; j < S.size(); j++) { z += (int)(S[j] - 'A' + 1) * pow(27, S.size() - j - 1); } cin >> a; k[z] += a; } for (int i = 0; i < 16000000; i++) { if (k[i] >= 1) { for (int j = 4; j >= 0; j--) { z = (int)(i / (int)pow(27, j)) % 27; if (z >= 1) { cout << (char)(z + 'A' - 1); } } cout << ' '; cout << k[i] << endl; } } return 0; }
[["-", 0, 57, 64, 9, 0, 7, 8, 9, 0, 46], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
1
245
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; bool cmp(const string &a, const string &b) { if (a.size() != b.size()) return a.size() < b.size(); for (int i = 0; i < a.size(); i++) { if (a[i] == b[i]) continue; return a[i] < b[i]; } return false; } int main() { map<string, int> ans; vector<string> vec; int n; cin >> n; for (int i = 0; i < n; i++) { string name; int val; cin >> name >> val; ans[name] += val; vec.push_back(name); } sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); sort(vec.begin(), vec.end(), cmp); for (int i = 0; i < vec.size(); i++) cout << vec[i] << ans[vec[i]] << endl; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; bool cmp(const string &a, const string &b) { if (a.size() != b.size()) return a.size() < b.size(); for (int i = 0; i < a.size(); i++) { if (a[i] == b[i]) continue; return a[i] < b[i]; } return false; } int main() { map<string, int> ans; vector<string> vec; int n; cin >> n; for (int i = 0; i < n; i++) { string name; int val; cin >> name >> val; ans[name] += val; vec.push_back(name); } sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); sort(vec.begin(), vec.end(), cmp); for (int i = 0; i < vec.size(); i++) cout << vec[i] << " " << ans[vec[i]] << endl; return 0; }
[["+", 0, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 7, 8, 1, 0, 16, 31, 16, 17, 151]]
1
267
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; bool cmp(const pair<string, int> &x, const pair<string, int> &y) { if (x.first.size() != y.first.size()) { return x.first < y.first; } return x.first.size() < y.first.size(); } int main() { map<string, int> m; string pn; int n, c; scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> pn >> c; if (m.count(pn)) m[pn] += c; else m.insert(make_pair(pn, c)); } vector<pair<string, int>> v; for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) { v.push_back(*it); } sort(v.begin(), v.end(), cmp); for (vector<pair<string, int>>::iterator it = v.begin(); it != v.end(); it++) { cout << it->first << " " << it->second << endl; } return (0); }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; bool cmp(const pair<string, int> &x, const pair<string, int> &y) { if (x.first.size() == y.first.size()) { return x.first < y.first; } return x.first.size() < y.first.size(); } int main() { map<string, int> m; string pn; int n, c; scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> pn >> c; if (m.count(pn)) m[pn] += c; else m.insert(make_pair(pn, c)); } vector<pair<string, int>> v; for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) { v.push_back(*it); } sort(v.begin(), v.end(), cmp); for (vector<pair<string, int>>::iterator it = v.begin(); it != v.end(); it++) { cout << it->first << " " << it->second << endl; } return (0); }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 79], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
292
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int memo[201][201]; int d, n; int t[200]; int a[200], b[200], c[200]; int rec(int k, int prev) { if (memo[k][prev] != -1) return memo[k][prev]; if (k == d) return 0; int best = 0; for (int next = 0; next < n; next++) { if (t[k] >= a[next] && t[k] <= b[next]) best = max(best, rec(k + 1, next) + abs(c[next] - c[prev])); } return memo[k][prev] = best; } int main() { for (int i = 0; i <= 200; i++) for (int j = 0; j <= 200; j++) memo[i][j] = -1; cin >> d >> n; for (int i = 0; i < d; i++) cin >> t[i]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i]; int ans = 0; for (int i = 0; i < n; i++) { if (t[0] >= a[i] && t[0] <= b[i]) ans = max(rec(0, i), ans); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int memo[201][201]; int d, n; int t[200]; int a[200], b[200], c[200]; int rec(int k, int prev) { if (memo[k][prev] != -1) return memo[k][prev]; if (k == d) return 0; int best = 0; for (int next = 0; next < n; next++) { if (t[k] >= a[next] && t[k] <= b[next]) best = max(best, rec(k + 1, next) + abs(c[next] - c[prev])); } return memo[k][prev] = best; } int main() { for (int i = 0; i <= 200; i++) for (int j = 0; j <= 200; j++) memo[i][j] = -1; cin >> d >> n; for (int i = 0; i < d; i++) cin >> t[i]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i]; int ans = 0; for (int i = 0; i < n; i++) { if (t[0] >= a[i] && t[0] <= b[i]) ans = max(rec(1, i), ans); } cout << ans << endl; return 0; }
[["-", 12, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 12, 2, 3, 4, 0, 2, 3, 4, 0, 13]]
1
338
#include <bits/stdc++.h> using namespace std; int dp[250][150], T[250], x[250][3], D, N, MAX; main() { memset(dp, -99999, sizeof(dp)); cin >> D >> N; for (int i = 1; i <= D; i++) { cin >> T[i]; } for (int i = 1; i <= N; i++) { cin >> x[i][0] >> x[i][1] >> x[i][2]; if (T[1] >= x[i][0] && T[1] <= x[i][1]) { dp[1][x[i][2]] = 0; } } for (int i = 2; i <= D; i++) { for (int j = 1; j <= N; j++) { if (T[i] >= x[j][0] && T[i] <= x[j][1]) { for (int k = 0; k <= 100; k++) { dp[i][x[j][2]] = max(dp[i][x[j][2]], dp[i - 1][k] + abs(x[j][2] - k)); } } } } for (int i = 0; i <= 100; i++) { MAX = max(MAX, dp[D][i]); } cout << MAX << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[250][150], T[250], x[250][3], D, N, MAX; main() { memset(dp, -999999, sizeof(dp)); cin >> D >> N; for (int i = 1; i <= D; i++) { cin >> T[i]; } for (int i = 1; i <= N; i++) { cin >> x[i][0] >> x[i][1] >> x[i][2]; if (T[1] >= x[i][0] && T[1] <= x[i][1]) { dp[1][x[i][2]] = 0; } } for (int i = 2; i <= D; i++) { for (int j = 1; j <= N; j++) { if (T[i] >= x[j][0] && T[i] <= x[j][1]) { for (int k = 0; k <= 100; k++) { dp[i][x[j][2]] = max(dp[i][x[j][2]], dp[i - 1][k] + abs(x[j][2] - k)); } } } } for (int i = 0; i <= 100; i++) { MAX = max(MAX, dp[D][i]); } cout << MAX << endl; return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
340
#include <algorithm> #include <iostream> using namespace std; int dp[300][110], l[300], r[300], p[300], n, m, a[300]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> l[i] >> r[i] >> p[i]; for (int i = 0; i < 33000; i++) dp[i / 110][i % 110] = -1000000000; for (int i = 0; i < m; i++) { if (l[i] <= a[0] && a[i] <= r[0]) { dp[0][p[i]] = max(dp[0][p[i]], 0); } } for (int i = 1; i < n; i++) { for (int j = 0; j <= 100; j++) { for (int k = 0; k < m; k++) { if (l[k] <= a[i] && a[i] <= r[k]) { dp[i][p[k]] = max(dp[i][p[k]], dp[i - 1][j] + abs(j - p[k])); } } } } int maxn = 0; for (int i = 0; i <= 100; i++) maxn = max(maxn, dp[n - 1][i]); cout << maxn << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int dp[300][110], l[300], r[300], p[300], n, m, a[300]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> l[i] >> r[i] >> p[i]; for (int i = 0; i < 33000; i++) dp[i / 110][i % 110] = -1000000000; for (int i = 0; i < m; i++) { if (l[i] <= a[0] && a[0] <= r[i]) { dp[0][p[i]] = max(dp[0][p[i]], 0); } } for (int i = 1; i < n; i++) { for (int j = 0; j <= 100; j++) { for (int k = 0; k < m; k++) { if (l[k] <= a[i] && a[i] <= r[k]) { dp[i][p[k]] = max(dp[i][p[k]], dp[i - 1][j] + abs(j - p[k])); } } } } int maxn = 0; for (int i = 0; i <= 100; i++) maxn = max(maxn, dp[n - 1][i]); cout << maxn << endl; return 0; }
[["-", 51, 16, 12, 16, 31, 69, 341, 342, 0, 22], ["+", 51, 16, 12, 16, 31, 69, 341, 342, 0, 13], ["-", 51, 16, 12, 16, 12, 69, 341, 342, 0, 13], ["+", 51, 16, 12, 16, 12, 69, 341, 342, 0, 22]]
1
362
#include <bits/stdc++.h> using namespace std; int t[200]; int a[200], b[200], c[200]; int dp[200][200]; int main() { int d, n; cin >> d >> n; for (int i = 0; i < d; ++i) { cin >> t[i]; } for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i] >> c[i]; } for (int i = 1; i < d; ++i) { for (int j = 0; j < n; ++j) { // i??\??? if (a[j] <= t[i] && t[i] <= b[j]) { for (int k = 0; k < n; ++k) { if (a[k] < t[i - 1] && t[i - 1] <= b[k]) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(c[j] - c[k])); } } } } } int ans = 0; for (int i = 0; i < n; ++i) { ans = max(ans, dp[d - 1][i]); } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; int t[200]; int a[200], b[200], c[200]; int dp[200][200]; int main() { int d, n; cin >> d >> n; for (int i = 0; i < d; ++i) { cin >> t[i]; } for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i] >> c[i]; } for (int i = 1; i < d; ++i) { for (int j = 0; j < n; ++j) { // i??\??? if (a[j] <= t[i] && t[i] <= b[j]) { for (int k = 0; k < n; ++k) { if (a[k] <= t[i - 1] && t[i - 1] <= b[k]) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(c[j] - c[k])); } } } } } int ans = 0; for (int i = 0; i < n; ++i) { ans = max(ans, dp[d - 1][i]); } printf("%d\n", ans); }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 19]]
1
301
#include <algorithm> #include <climits> #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 int long long typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pii> vpii; #define fst first #define scd second #define PB push_back #define MP make_pair #define rep(i, x) for (int i = 0; i < (x); ++i) #define rep1(i, x) for (int i = 1; i <= (x); ++i) #define rrep(i, x) for (int i = (x)-1; i >= 0; --i) #define rrep1(i, x) for (int i = (x); i >= 1; --i) #define FOR(i, a, x) for (int i = (a); i < (x); ++i) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define omajinai \ ios::sync_with_stdio(false); \ cin.tie(0) template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> T get() { T a; cin >> a; return a; } template <typename T> T rev(T a) { reverse(all(a)); return a; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { rep(i, vec.size()) is >> vec[i]; return is; } template <typename T> vector<T> &sort(vector<T> &a) { sort(all(a)); return a; } const int inf = 1e9; const ll linf = 3e18; const double eps = 1e-9; int T[200], A[200], B[200], C[200]; int dp[201][200]; signed main() { int D, N; cin >> D >> N; rep(i, D) { cin >> T[i]; } rep(i, N) { cin >> A[i] >> B[i] >> C[i]; } for (int i = 1; i <= D; ++i) { rep(j, N) { if (T[i - 1] < A[j] || T[i - 1] > B[j]) continue; rep(k, N) { if (T[i] < A[k] || T[i] > B[k]) continue; chmax(dp[i][k], dp[i - 1][j] + abs(C[j] - C[k])); } } } int ans = 0; rep(i, D + 1) rep(j, N) chmax(ans, dp[i][j]); cout << ans << endl; }
#include <algorithm> #include <climits> #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 int long long typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pii> vpii; #define fst first #define scd second #define PB push_back #define MP make_pair #define rep(i, x) for (int i = 0; i < (x); ++i) #define rep1(i, x) for (int i = 1; i <= (x); ++i) #define rrep(i, x) for (int i = (x)-1; i >= 0; --i) #define rrep1(i, x) for (int i = (x); i >= 1; --i) #define FOR(i, a, x) for (int i = (a); i < (x); ++i) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define omajinai \ ios::sync_with_stdio(false); \ cin.tie(0) template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> T get() { T a; cin >> a; return a; } template <typename T> T rev(T a) { reverse(all(a)); return a; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { rep(i, vec.size()) is >> vec[i]; return is; } template <typename T> vector<T> &sort(vector<T> &a) { sort(all(a)); return a; } const int inf = 1e9; const ll linf = 3e18; const double eps = 1e-9; int T[200], A[200], B[200], C[200]; int dp[201][200]; signed main() { int D, N; cin >> D >> N; rep(i, D) { cin >> T[i]; } rep(i, N) { cin >> A[i] >> B[i] >> C[i]; } for (int i = 1; i < D; ++i) { rep(j, N) { if (T[i - 1] < A[j] || T[i - 1] > B[j]) continue; rep(k, N) { if (T[i] < A[k] || T[i] > B[k]) continue; chmax(dp[i][k], dp[i - 1][j] + abs(C[j] - C[k])); } } } int ans = 0; rep(i, D + 1) rep(j, N) chmax(ans, dp[i][j]); cout << ans << endl; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18]]
1
593
#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; const ll MOD = 1e9 + 7; double EPS = 1e-10; int dp[210][210]; int main() { int d, n; cin >> d >> n; vi t(d); rep(i, d) cin >> t[i]; vi a(n), b(n), c(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; rep(i, 210) rep(j, 210) dp[i][j] = -1; for (int i = 0; i < n; i++) { if (a[i] <= t[0] && t[i] <= b[i]) { dp[0][i] = 0; } } for (int i = 1; i < d; i++) { for (int j = 0; j < n; j++) { if (a[j] <= t[i] && t[i] <= b[j]) { for (int k = 0; k < n; k++) { if (dp[i - 1][k] != -1) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(c[k] - c[j])); } } } } } int ans = 0; rep(i, 210) { ans = max(ans, dp[d - 1][i]); } cout << ans << 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; const ll MOD = 1e9 + 7; double EPS = 1e-10; int dp[210][210]; int main() { int d, n; cin >> d >> n; vi t(d); rep(i, d) cin >> t[i]; vi a(n), b(n), c(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; rep(i, 210) rep(j, 210) dp[i][j] = -1; for (int i = 0; i < n; i++) { if (a[i] <= t[0] && t[0] <= b[i]) { dp[0][i] = 0; } } for (int i = 1; i < d; i++) { for (int j = 0; j < n; j++) { if (a[j] <= t[i] && t[i] <= b[j]) { for (int k = 0; k < n; k++) { if (dp[i - 1][k] != -1) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(c[k] - c[j])); } } } } } int ans = 0; rep(i, 210) { ans = max(ans, dp[d - 1][i]); } cout << ans << endl; }
[["-", 51, 16, 12, 16, 31, 69, 341, 342, 0, 22], ["+", 51, 16, 12, 16, 31, 69, 341, 342, 0, 13]]
1
480
#include <bits/stdc++.h> #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = 1; i <= n; i++) #define drep(i, n) for (int i = n; i >= 0; i--) #define INF 100000005 #define MAX 100000001 #define dmp make_pair #define dpb push_back #define fi first #define se second using namespace std; //__gcd(a,b), __builtin_popcount(a); int a[201], b[201][3]; pair<int, int> dp[201][2]; int main() { int d, n, ans = 0; scanf("%d%d", &d, &n); rrep(i, d) scanf("%d", &a[i]); rrep(i, n) scanf("%d%d%d", &b[i][0], &b[i][1], &b[i][2]); int t1, t2; for (int i = 1; i <= d; i++) { t1 = INF; t2 = 0; for (int j = 1; j <= n; j++) { if (b[j][0] <= a[i] && a[i] <= b[j][1]) { t1 = min(t1, b[j][2]); t2 = max(t2, b[j][2]); } } dp[i][0].se = t1; dp[i][1].se = t2; dp[i][0].fi = dp[i - 1][1].fi + abs(dp[i - 1][1].se - t1); dp[i][0].fi = max(dp[i][0].fi, dp[i - 1][1].fi + abs(dp[i - 1][0].se - t1)); dp[i][1].fi = dp[i - 1][0].fi + abs(t2 - dp[i - 1][0].se); dp[i][1].fi = max(dp[i][1].fi, dp[i - 1][1].fi + abs(t2 - dp[i - 1][1].se)); if (i == 1) { dp[i][0].fi = 0; dp[i][1].fi = 0; } } t1 = dp[d][0].fi; t2 = dp[d][1].fi; ans = max(t1, t2); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = 1; i <= n; i++) #define drep(i, n) for (int i = n; i >= 0; i--) #define INF 100000005 #define MAX 100000001 #define dmp make_pair #define dpb push_back #define fi first #define se second using namespace std; //__gcd(a,b), __builtin_popcount(a); int a[201], b[201][3]; pair<int, int> dp[201][2]; int main() { int d, n, ans = 0; scanf("%d%d", &d, &n); rrep(i, d) scanf("%d", &a[i]); rrep(i, n) scanf("%d%d%d", &b[i][0], &b[i][1], &b[i][2]); int t1, t2; for (int i = 1; i <= d; i++) { t1 = INF; t2 = 0; for (int j = 1; j <= n; j++) { if (b[j][0] <= a[i] && a[i] <= b[j][1]) { t1 = min(t1, b[j][2]); t2 = max(t2, b[j][2]); } } dp[i][0].se = t1; dp[i][1].se = t2; dp[i][0].fi = dp[i - 1][1].fi + abs(dp[i - 1][1].se - t1); dp[i][0].fi = max(dp[i][0].fi, dp[i - 1][0].fi + abs(dp[i - 1][0].se - t1)); dp[i][1].fi = dp[i - 1][0].fi + abs(t2 - dp[i - 1][0].se); dp[i][1].fi = max(dp[i][1].fi, dp[i - 1][1].fi + abs(t2 - dp[i - 1][1].se)); if (i == 1) { dp[i][0].fi = 0; dp[i][1].fi = 0; } } t1 = dp[d][0].fi; t2 = dp[d][1].fi; ans = max(t1, t2); printf("%d\n", ans); return 0; }
[["-", 0, 16, 31, 118, 28, 69, 341, 342, 0, 13], ["+", 0, 16, 31, 118, 28, 69, 341, 342, 0, 13]]
1
558
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main() { int d, n; cin >> d >> n; int dp[205][105]; for (int i = 0; i < 205; i++) for (int j = 0; j < 205; j++) dp[i][j] = 1e7; int temp[205]; int a[205], b[205], c[205]; for (int i = 1; i <= d; i++) { cin >> temp[i]; } for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; if (a[i] <= temp[1] && temp[1] <= b[i]) { dp[1][c[i]] = min(dp[1][c[i]], 0); } } for (int i = 2; i <= d; i++) { for (int j = 1; j <= n; j++) { if (a[j] <= temp[i] && temp[i] <= b[j]) { for (int k = 0; k <= 100; k++) dp[i][c[j]] = min(dp[i][c[j]], dp[i - 1][k] + abs(k - c[j])); } } } int val = 1e7; for (int k = 0; k <= 100; k++) val = min(val, dp[d][k]); cout << val << endl; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main() { int d, n; cin >> d >> n; int dp[205][105]; for (int i = 0; i < 205; i++) for (int j = 0; j < 105; j++) dp[i][j] = -1e7; int temp[205]; int a[205], b[205], c[205]; for (int i = 1; i <= d; i++) { cin >> temp[i]; } for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; if (a[i] <= temp[1] && temp[1] <= b[i]) { dp[1][c[i]] = max(dp[1][c[i]], 0); } } for (int i = 2; i <= d; i++) { for (int j = 1; j <= n; j++) { if (a[j] <= temp[i] && temp[i] <= b[j]) { for (int k = 0; k <= 100; k++) dp[i][c[j]] = max(dp[i][c[j]], dp[i - 1][k] + abs(k - c[j])); } } } int val = -1e7; for (int k = 0; k <= 100; k++) val = max(val, dp[d][k]); cout << val << endl; }
[["-", 8, 9, 0, 7, 8, 7, 15, 16, 12, 13], ["+", 8, 9, 0, 7, 8, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 7, 8, 1, 0, 11, 12, 13], ["+", 0, 7, 8, 7, 8, 1, 0, 11, 12, 13], ["-", 64, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 64, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["-", 0, 7, 8, 1, 0, 11, 12, 2, 63, 22], ["+", 0, 7, 8, 1, 0, 11, 12, 2, 63, 22], ["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
360
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main(void) { int d, n, t, a, b, c, dp[202][202]; vector<int> T, A, B, C; cin >> d >> n; T.clear(); A.clear(); B.clear(); C.clear(); for (int i = 0; i < d; i++) { cin >> t; T.push_back(t); } for (int i = 0; i < n; i++) { cin >> a >> b >> c; A.push_back(a); B.push_back(b); C.push_back(c); } for (int i = 0; i < 202; i++) for (int j = 0; j < 202; j++) dp[i][j] = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (A[i] <= T[0] && T[0] <= B[i] && A[j] <= T[1] && T[1] <= B[j]) dp[0][j] = max(dp[0][j], abs(C[i] - C[j])); } } for (int i = 1; i <= d; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (A[k] <= T[i] && T[i] <= B[k] && dp[i - 1][j] >= 0) dp[i][k] = max(dp[i][k], dp[i - 1][j] + abs(C[j] - C[k])); } } } int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, dp[d - 1][i]); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main(void) { int d, n, t, a, b, c, dp[202][202]; vector<int> T, A, B, C; cin >> d >> n; T.clear(); A.clear(); B.clear(); C.clear(); for (int i = 0; i < d; i++) { cin >> t; T.push_back(t); } for (int i = 0; i < n; i++) { cin >> a >> b >> c; A.push_back(a); B.push_back(b); C.push_back(c); } for (int i = 0; i < 202; i++) for (int j = 0; j < 202; j++) dp[i][j] = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (A[i] <= T[0] && T[0] <= B[i] && A[j] <= T[1] && T[1] <= B[j]) dp[0][j] = max(dp[0][j], abs(C[i] - C[j])); } } for (int i = 1; i <= d; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (A[k] <= T[i + 1] && T[i + 1] <= B[k] && dp[i - 1][j] >= 0) dp[i][k] = max(dp[i][k], dp[i - 1][j] + abs(C[j] - C[k])); } } } int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, dp[d - 2][i]); } cout << ans << endl; return 0; }
[["+", 31, 16, 12, 69, 341, 342, 0, 16, 17, 72], ["+", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13], ["+", 12, 16, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 12, 16, 31, 69, 341, 342, 0, 16, 12, 13], ["-", 0, 69, 28, 69, 341, 342, 0, 16, 12, 13], ["+", 0, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
469
#include <fstream> #include <iostream> #include <math.h> using namespace std; int T[200], A[200], B[200], C[200], D, N; int Cs[200][200], CsNumber[200], valueCs[200][200]; inline void readinput() { // ifstream fin("input_2012_4.dat"); cin >> D >> N; for (int i = 0; i < D; i++) cin >> T[i]; for (int i = 0; i < N; i++) cin >> A[i] >> B[i] >> C[i]; } inline void output_2d(int X[200][200]) { for (int i = 0; i < D; i++) { for (int j = 0; j < N; j++) { cout << X[i][j]; if (j != N - 1) cout << " "; else cout << endl; } } } inline void putCs() { int idx; for (int i = 0; i < D; i++) { idx = 0; for (int j = 0; j < N; j++) { if (A[j] <= T[i] && T[i] <= B[j]) { Cs[i][idx] = C[j]; idx += 1; } CsNumber[i] = idx; } } } inline int solver() { int max, maxidx, buf; // initialize for (int i = 0; i < 200; i++) { for (int j = 0; j < 200; j++) valueCs[i][j] = 0; } for (int i = D - 1; i > 0; i--) { // D-1 to 1 for (int j1 = 0; j1 < CsNumber[i - 1]; j1++) { max = 0; for (int j2 = 0; j2 < CsNumber[i]; j2++) { buf = fabs(Cs[i - 1][j1] - Cs[i][j2]) + valueCs[i][j2]; if (max < buf) { max = buf; maxidx = j2; } } valueCs[i - 1][j1] += max; } } max = 0; for (int j = 0; j < CsNumber[0]; j++) { buf = valueCs[0][j]; if (max < buf) max = buf; } return max; } int main() { readinput(); putCs(); cout << "answer: " << solver() << endl; // cout << "--- Cs ---" << endl; // output_2d(Cs); // cout << "--- valueCs ---" << endl; // output_2d(valueCs); return 0; }
#include <fstream> #include <iostream> #include <math.h> using namespace std; int T[200], A[200], B[200], C[200], D, N; int Cs[200][200], CsNumber[200], valueCs[200][200]; inline void readinput() { // ifstream fin("input_2012_4.dat"); cin >> D >> N; for (int i = 0; i < D; i++) cin >> T[i]; for (int i = 0; i < N; i++) cin >> A[i] >> B[i] >> C[i]; } inline void output_2d(int X[200][200]) { for (int i = 0; i < D; i++) { for (int j = 0; j < N; j++) { cout << X[i][j]; if (j != N - 1) cout << " "; else cout << endl; } } } inline void putCs() { int idx; for (int i = 0; i < D; i++) { idx = 0; for (int j = 0; j < N; j++) { if (A[j] <= T[i] && T[i] <= B[j]) { Cs[i][idx] = C[j]; idx += 1; } CsNumber[i] = idx; } } } inline int solver() { int max, maxidx, buf; // initialize for (int i = 0; i < 200; i++) { for (int j = 0; j < 200; j++) valueCs[i][j] = 0; } for (int i = D - 1; i > 0; i--) { // D-1 to 1 for (int j1 = 0; j1 < CsNumber[i - 1]; j1++) { max = 0; for (int j2 = 0; j2 < CsNumber[i]; j2++) { buf = fabs(Cs[i - 1][j1] - Cs[i][j2]) + valueCs[i][j2]; if (max < buf) { max = buf; maxidx = j2; } } valueCs[i - 1][j1] += max; } } max = 0; for (int j = 0; j < CsNumber[0]; j++) { buf = valueCs[0][j]; if (max < buf) max = buf; } return max; } int main() { readinput(); putCs(); cout << solver() << endl; // cout << "--- Cs ---" << endl; // output_2d(Cs); // cout << "--- valueCs ---" << endl; // output_2d(valueCs); return 0; }
[["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151]]
1
549
/// // File: 0579.go // Author: ymiyamoto // // Created on Mon Mar 19 18:46:50 2018 // package main import ( "fmt" ) var D, N int type cloth struct { A, B, C int } func abs(a int) int { if a > 0 { return a } return -a } func max(a, b int) int { if a < b { return b } return a } func main() { fmt.Scan(&D, &N) thermals := make([]int, D+1) for i := 1; i <= D; i++ { fmt.Scan(&thermals[i]) } clothes := make([]cloth, N) for i := 0; i < N; i++ { fmt.Scan(&clothes[i].A, &clothes[i].B, &clothes[i].C) } dp := make([][]int, D+1) for i := range dp { dp[i] = make([]int, N) } for i := 0; i < D; i++ { for j := 0; j < N; j++ { for k := 0; k < N; k++ { t1 := thermals[i] t2 := thermals[i+1] if clothes[j].A <= t2 && t2 <= clothes[j].B && clothes[k].A <= t1 && t1 <= clothes[k].B { dp[i+1][j] = max(dp[i+1][j], dp[i][k]+abs(clothes[j].C-clothes[k].C)) } } } } val := 0 for _, v := range dp[D] { val = max(val, v) } fmt.Println(val) }
/// // File: 0579.go // Author: ymiyamoto // // Created on Mon Mar 19 18:46:50 2018 // package main import ( "fmt" ) var D, N int type cloth struct { A, B, C int } func abs(a int) int { if a > 0 { return a } return -a } func max(a, b int) int { if a < b { return b } return a } func main() { fmt.Scan(&D, &N) thermals := make([]int, D+1) for i := 1; i <= D; i++ { fmt.Scan(&thermals[i]) } clothes := make([]cloth, N) for i := 0; i < N; i++ { fmt.Scan(&clothes[i].A, &clothes[i].B, &clothes[i].C) } dp := make([][]int, D+1) for i := range dp { dp[i] = make([]int, N) } for i := 1; i < D; i++ { for j := 0; j < N; j++ { for k := 0; k < N; k++ { t1 := thermals[i] t2 := thermals[i+1] if clothes[j].A <= t2 && t2 <= clothes[j].B && clothes[k].A <= t1 && t1 <= clothes[k].B { dp[i+1][j] = max(dp[i+1][j], dp[i][k]+abs(clothes[j].C-clothes[k].C)) } } } } val := 0 for _, v := range dp[D] { val = max(val, v) } fmt.Println(val) }
[["-", 0, 7, 0, 430, 10, 431, 12, 432, 0, 433], ["+", 0, 7, 0, 430, 10, 431, 12, 432, 0, 433]]
7
410
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> typedef unsigned long long ull; const int MAX_N = 50; int N, K; int x1[MAX_N], y1[MAX_N], d1[MAX_N], x2[MAX_N], y2[MAX_N], d2[MAX_N]; int countKind(int x, int y, int d) { int ans = 0; x++; y++; d++; for (int i = 0; i < N; i++) { if (x1[i] <= x && x <= x2[i] && y1[i] <= y && y <= y2[i] && d1[i] <= d && d <= d2[i]) ans++; } return ans; } using namespace std; int main() { ull v = 0; cin >> N >> K; vector<int> X(2 * N), Y(2 * N), D(2 * N); for (int i = 0; i < N; i++) { cin >> x1[i] >> y1[i] >> d1[i] >> x2[i] >> y2[i] >> d2[i]; X[i] = x1[i]; X[i + N] = x2[i]; Y[i] = y1[i]; Y[i + N] = y2[i]; D[i] = d1[i]; D[i + N] = d2[i]; } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); sort(D.begin(), D.end()); for (int i = 0; i < 2 * N - 1; i++) { for (int j = 0; j < 2 * N - 1; j++) { for (int k = 0; k < 2 * N - 1; k++) { v += (countKind(X[i], Y[j], D[k]) >= K ? (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]) * (D[k + 1] - D[k]) : 0); } } } printf("%llu\n", v); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> typedef unsigned long long ull; const int MAX_N = 50; int N, K; int x1[MAX_N], y1[MAX_N], d1[MAX_N], x2[MAX_N], y2[MAX_N], d2[MAX_N]; int countKind(int x, int y, int d) { int ans = 0; x++; y++; d++; for (int i = 0; i < N; i++) { if (x1[i] <= x && x <= x2[i] && y1[i] <= y && y <= y2[i] && d1[i] <= d && d <= d2[i]) ans++; } return ans; } using namespace std; int main() { ull v = 0; cin >> N >> K; vector<int> X(2 * N), Y(2 * N), D(2 * N); for (int i = 0; i < N; i++) { cin >> x1[i] >> y1[i] >> d1[i] >> x2[i] >> y2[i] >> d2[i]; X[i] = x1[i]; X[i + N] = x2[i]; Y[i] = y1[i]; Y[i + N] = y2[i]; D[i] = d1[i]; D[i + N] = d2[i]; } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); sort(D.begin(), D.end()); for (int i = 0; i < 2 * N - 1; i++) { for (int j = 0; j < 2 * N - 1; j++) { for (int k = 0; k < 2 * N - 1; k++) { v += (countKind(X[i], Y[j], D[k]) >= K ? (ull)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]) * (D[k + 1] - D[k]) : 0); } } } printf("%llu\n", v); return 0; }
[["+", 64, 16, 31, 16, 31, 2, 63, 23, 0, 22], ["+", 64, 16, 31, 16, 31, 2, 63, 23, 0, 25], ["+", 64, 16, 31, 16, 31, 2, 3, 4, 0, 24]]
1
496
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> X1(N); vector<int> Y1(N); vector<int> D1(N); vector<int> X2(N); vector<int> Y2(N); vector<int> D2(N); for (int i = 0; i < N; i++) { cin >> X1[i] >> Y1[i] >> D1[i] >> X2[i] >> Y2[i] >> D2[i]; } //ソート用vector vector<int> X(2 * N); vector<int> Y(2 * N); vector<int> D(2 * N); for (int i = 0; i < N; i++) { X[2 * i] = X1[i]; X[2 * i + 1] = X2[i]; Y[2 * i] = Y1[i]; Y[2 * i + 1] = Y2[i]; D[2 * i] = D1[i]; D[2 * i + 1] = D2[i]; } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); sort(D.begin(), D.end()); long long int ans = 0; int kasanari; for (int i = 0; i < 2 * N - 1; i++) { // X for (int j = 0; j < 2 * N - 1; j++) { // Y for (int k = 0; k < 2 * N - 1; k++) { // D kasanari = 0; for (int l = 0; l < N; l++) { //実際の直方体 if ((X1[l] <= X[i] && X[i + 1] <= X2[l]) && (Y1[l] <= Y[j] && Y[j + 1] <= Y2[l]) && (D1[l] <= D[k] && D[k + 1] <= D2[l])) kasanari++; } if (kasanari >= K) { ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]) * (D[k + 1] - D[k]); } } } } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> X1(N); vector<int> Y1(N); vector<int> D1(N); vector<int> X2(N); vector<int> Y2(N); vector<int> D2(N); for (int i = 0; i < N; i++) { cin >> X1[i] >> Y1[i] >> D1[i] >> X2[i] >> Y2[i] >> D2[i]; } //ソート用vector vector<int> X(2 * N); vector<int> Y(2 * N); vector<int> D(2 * N); for (int i = 0; i < N; i++) { X[2 * i] = X1[i]; X[2 * i + 1] = X2[i]; Y[2 * i] = Y1[i]; Y[2 * i + 1] = Y2[i]; D[2 * i] = D1[i]; D[2 * i + 1] = D2[i]; } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); sort(D.begin(), D.end()); long long int ans = 0; int kasanari; for (int i = 0; i < 2 * N - 1; i++) { // X for (int j = 0; j < 2 * N - 1; j++) { // Y for (int k = 0; k < 2 * N - 1; k++) { // D kasanari = 0; for (int l = 0; l < N; l++) { //実際の直方体 if ((X1[l] <= X[i] && X[i + 1] <= X2[l]) && (Y1[l] <= Y[j] && Y[j + 1] <= Y2[l]) && (D1[l] <= D[k] && D[k + 1] <= D2[l])) kasanari++; } if (kasanari >= K) { ans += (long long int)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]) * (D[k + 1] - D[k]); } } } } cout << ans << endl; return 0; }
[["+", 31, 16, 31, 74, 39, 77, 39, 86, 0, 96], ["+", 31, 16, 31, 74, 39, 77, 39, 86, 39, 40], ["+", 0, 11, 12, 16, 31, 16, 31, 74, 0, 25], ["+", 12, 16, 31, 16, 31, 74, 51, 23, 0, 24]]
1
534
/** * @brief 最近点対の発見について扱います * @date 2016/03/22 */ //**************************************** // 必要なヘッダファイルのインクルード //**************************************** #include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> //**************************************** // オブジェクト形式マクロの定義 //**************************************** //#define GEOMETRY_BEGIN namespace geom { //#define GEOMETRY_END } //**************************************** // 型シノニム //**************************************** using elem_t = long double; using index_t = std::int32_t; using indices_t = std::vector<index_t>; //**************************************** // 構造体の定義 //**************************************** struct point { elem_t x, y; point() : x(0), y(0) {} point(elem_t x, elem_t y) : x(x), y(y) {} point &operator+=(const point &p) { x += p.x; y += p.y; return *this; } point &operator-=(const point &p) { x -= p.x; y -= p.y; return *this; } point &operator*=(const elem_t d) { x *= d; y *= d; return *this; } point operator+(const point &p) const { return point(*this) += p; } point operator-(const point &p) const { return point(*this) -= p; } point operator*(const elem_t &d) const { return point(*this) *= d; } }; struct segment { point ps; // 線分の始点 point pd; // 線分の終点 }; namespace limits { constexpr auto pi = 3.141592653589793238; constexpr auto eps = 1e-10; constexpr auto inf = 1e12; } // namespace limits /** * @brief 与えられた3点a, b, cをa -> b -> cと進むとき、 * @details a -> bで時計方向に折れてb -> c(cw) * a -> bで反時計方向に折れてb -> c(ccw) * a -> bで逆を向いてaを通り越してb -> c (back) * a -> bでそのままb -> c(front) * a -> bで逆を向いてb -> cまたは、b == c(on) */ enum struct orient { cw = +1, ccw = -1, back = -2, front = +2, on = 0, }; //**************************************** // 型シノニムその2 //**************************************** using vector_t = point; using polygon_t = std::vector<point>; using segments_t = std::vector<segment>; //**************************************** // 関数の定義 //**************************************** /** * @brief 2つのベクトルa, bからなる行列A = (a, * b)の行列式(determinant)を返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return elem_t det(A) 行列式|(a, b)| */ static constexpr elem_t det(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; } /** * @brief 2つのベクトルa, bのクロス積(cross product)a x bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return elem_t a x b クロス積a x b */ static constexpr elem_t cross(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; // return det(a, b); } /** * @brief 2つのベクトルa, bのドット積(dot product)a・bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return elem_t a・b ドット積a・b */ static constexpr elem_t dot(const vector_t &a, const vector_t &b) { return a.x * b.x + a.y * b.y; } /** * @brief ベクトルvのノルム(norm・大きさの2乗)を返します * @param const vector_t& v * @return elem_t norm(v); */ static constexpr elem_t norm(const vector_t &v) { return v.x * v.x + v.y * v.y; // return dot(v, v); } /** * @brief ベクトルvの大きさ|v|(原点からの距離)を返します * @param const vector_t& v * @return elem_t sqrt(norm(v)) */ static inline elem_t abs(const vector_t &v) { return std::sqrt(norm(v)); } //**************************************** // 構造体の定義 //**************************************** struct cmp_x { bool operator()(const point &pi, const point &pj) { return pi.x < pj.x; } }; struct cmp_y { bool operator()(const point &pi, const point &pj) { return pi.y < pj.y; } }; //**************************************** // 関数の定義 //**************************************** elem_t closest_pair(point *P, index_t n) { if (n <= 1) { // |P| <= 1か否かを判定する return limits::inf; // |P| <= 1ならば、再帰の基底 } // |P| > 1ならば、再帰呼び出しによって、以下のように分割統治法を実行する // 分割: // 以下の条件を満たすように、点集合Pを2つの集合PLとPRに分割する垂直線lを求める // |PL|=ceil(|P|/2), // |PR|=floor(|P|/2)であり、PLの点はすべてlの上または左に、 // PRの点はすべてlの上または右にある index_t m = n / 2; elem_t lx = P[m].x; // NOTE: // このとき、PRおよびPLはxを昇順に含む(presortingによって保証済み) // 統治: PをPLとPRに分割した後、1回はPLにおける最近点対を求めるために、 // もう1回はPRにおける最近点対を求めるために、2回の再帰呼び出しを行う // PLとPRに対して求めた最近点対間距離をδLとδRとし、δ = min(δL, δR)と置く elem_t delta = std::min(closest_pair(P, m), closest_pair(P + m, n - m)); // 結合: // 最近点対は、上の再帰呼び出しの一方で見つかった距離がδの点対か、1点がPLに属し、 // もう1点がPRに属する点対のどちらかである. // アルゴリズムは、一方がPLに属し、 // 他方がPRに属する点対で、距離がδ未満のものが存在するか否かを判定する // 距離がδ未満の点対を構成する2つの点は、それぞれ、直線lからδ単位距離以内に存在する // したがって、このような2点は直線lを中心とする、幅2δの垂直帯領域に存在する // このような点対を、存在するならば、以下の要領で発見する // // 1. // 配列Y(P)から、2δ幅の垂直帯領域を除く領域に属するすべての点を除去してできる、 // 配列Y'を生成する. 配列Y'はYと同様、y座標に関してソートされている // // 2. // 配列Y'の各点pについて、pからδ単位未満の距離にあるY'の点を発見しようと努める // このとき、Y'においてpに続く5個の点だけに候補対象を限定できる // pからこれら7個の点までの距離を計算し、Y'のすべての点対について発見できた // 最近点対の距離δ'を記憶する // // 3. δ' < // δならば、この垂直帯領域は再帰呼び出しが発見したものより近い点対を確かに含む // そこで、この点対と距離δ'を返す. // そうでなければ、再帰呼び出しが発見した最近点対と距離δを返す // NOTE: // 配列Yと配列Yのプレソーティングを避けるため、ソート済み配列PLとPRをマージしてソート済み配列Yを構成する(Pの再構成) std::inplace_merge(P, P + m, P + n, cmp_y()); polygon_t Y_; // 直線lから距離がδ未満の点を入れていく elem_t delta_ = limits::inf; for (index_t i = 0; i < n; i++) { // 直線lよりδ以上離れている(幅2δの垂直帯領域に存在しない)点は考慮から外す if (std::fabs(P[i].x - lx) >= delta) { continue; } index_t limit = Y_.size(); // NOTE: Y'の要素数は高々5個である for (index_t j = 0; j < limit; j++) { // Y'に入っている点を、末尾からy座標の差がδ以上になるまで確認する point p = Y_[limit - j - 1]; if ((P[i].y - p.y) >= delta) { break; } delta_ = std::min(delta_, abs(P[i] - p)); } Y_.push_back(P[i]); } return std::min(delta, delta_); } elem_t closest_pair(polygon_t &P) { std::sort(P.begin(), P.end(), cmp_x()); // xに関して、あらかじめソートしておく(presorting) return closest_pair(P.data(), P.size()); // 最初の再帰呼び出し } //**************************************** // エントリポイント //**************************************** int main() { using namespace std; int n; cin >> n; polygon_t P(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; P[i].x = x, P[i].y = y; } cout << closest_pair(P) << endl; return 0; }
/** * @brief 最近点対の発見について扱います * @date 2016/03/22 */ //**************************************** // 必要なヘッダファイルのインクルード //**************************************** #include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> //**************************************** // オブジェクト形式マクロの定義 //**************************************** //#define GEOMETRY_BEGIN namespace geom { //#define GEOMETRY_END } //**************************************** // 型シノニム //**************************************** using elem_t = long double; using index_t = std::int32_t; using indices_t = std::vector<index_t>; //**************************************** // 構造体の定義 //**************************************** struct point { elem_t x, y; point() : x(0), y(0) {} point(elem_t x, elem_t y) : x(x), y(y) {} point &operator+=(const point &p) { x += p.x; y += p.y; return *this; } point &operator-=(const point &p) { x -= p.x; y -= p.y; return *this; } point &operator*=(const elem_t d) { x *= d; y *= d; return *this; } point operator+(const point &p) const { return point(*this) += p; } point operator-(const point &p) const { return point(*this) -= p; } point operator*(const elem_t &d) const { return point(*this) *= d; } }; struct segment { point ps; // 線分の始点 point pd; // 線分の終点 }; namespace limits { constexpr auto pi = 3.141592653589793238; constexpr auto eps = 1e-10; constexpr auto inf = 1e12; } // namespace limits /** * @brief 与えられた3点a, b, cをa -> b -> cと進むとき、 * @details a -> bで時計方向に折れてb -> c(cw) * a -> bで反時計方向に折れてb -> c(ccw) * a -> bで逆を向いてaを通り越してb -> c (back) * a -> bでそのままb -> c(front) * a -> bで逆を向いてb -> cまたは、b == c(on) */ enum struct orient { cw = +1, ccw = -1, back = -2, front = +2, on = 0, }; //**************************************** // 型シノニムその2 //**************************************** using vector_t = point; using polygon_t = std::vector<point>; using segments_t = std::vector<segment>; //**************************************** // 関数の定義 //**************************************** /** * @brief 2つのベクトルa, bからなる行列A = (a, * b)の行列式(determinant)を返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return elem_t det(A) 行列式|(a, b)| */ static constexpr elem_t det(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; } /** * @brief 2つのベクトルa, bのクロス積(cross product)a x bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return elem_t a x b クロス積a x b */ static constexpr elem_t cross(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; // return det(a, b); } /** * @brief 2つのベクトルa, bのドット積(dot product)a・bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return elem_t a・b ドット積a・b */ static constexpr elem_t dot(const vector_t &a, const vector_t &b) { return a.x * b.x + a.y * b.y; } /** * @brief ベクトルvのノルム(norm・大きさの2乗)を返します * @param const vector_t& v * @return elem_t norm(v); */ static constexpr elem_t norm(const vector_t &v) { return v.x * v.x + v.y * v.y; // return dot(v, v); } /** * @brief ベクトルvの大きさ|v|(原点からの距離)を返します * @param const vector_t& v * @return elem_t sqrt(norm(v)) */ static inline elem_t abs(const vector_t &v) { return std::sqrt(norm(v)); } //**************************************** // 構造体の定義 //**************************************** struct cmp_x { bool operator()(const point &pi, const point &pj) { return pi.x < pj.x; } }; struct cmp_y { bool operator()(const point &pi, const point &pj) { return pi.y < pj.y; } }; //**************************************** // 関数の定義 //**************************************** elem_t closest_pair(point *P, index_t n) { if (n <= 1) { // |P| <= 1か否かを判定する return limits::inf; // |P| <= 1ならば、再帰の基底 } // |P| > 1ならば、再帰呼び出しによって、以下のように分割統治法を実行する // 分割: // 以下の条件を満たすように、点集合Pを2つの集合PLとPRに分割する垂直線lを求める // |PL|=ceil(|P|/2), // |PR|=floor(|P|/2)であり、PLの点はすべてlの上または左に、 // PRの点はすべてlの上または右にある index_t m = n / 2; elem_t lx = P[m].x; // NOTE: // このとき、PRおよびPLはxを昇順に含む(presortingによって保証済み) // 統治: PをPLとPRに分割した後、1回はPLにおける最近点対を求めるために、 // もう1回はPRにおける最近点対を求めるために、2回の再帰呼び出しを行う // PLとPRに対して求めた最近点対間距離をδLとδRとし、δ = min(δL, δR)と置く elem_t delta = std::min(closest_pair(P, m), closest_pair(P + m, n - m)); // 結合: // 最近点対は、上の再帰呼び出しの一方で見つかった距離がδの点対か、1点がPLに属し、 // もう1点がPRに属する点対のどちらかである. // アルゴリズムは、一方がPLに属し、 // 他方がPRに属する点対で、距離がδ未満のものが存在するか否かを判定する // 距離がδ未満の点対を構成する2つの点は、それぞれ、直線lからδ単位距離以内に存在する // したがって、このような2点は直線lを中心とする、幅2δの垂直帯領域に存在する // このような点対を、存在するならば、以下の要領で発見する // // 1. // 配列Y(P)から、2δ幅の垂直帯領域を除く領域に属するすべての点を除去してできる、 // 配列Y'を生成する. 配列Y'はYと同様、y座標に関してソートされている // // 2. // 配列Y'の各点pについて、pからδ単位未満の距離にあるY'の点を発見しようと努める // このとき、Y'においてpに続く5個の点だけに候補対象を限定できる // pからこれら7個の点までの距離を計算し、Y'のすべての点対について発見できた // 最近点対の距離δ'を記憶する // // 3. δ' < // δならば、この垂直帯領域は再帰呼び出しが発見したものより近い点対を確かに含む // そこで、この点対と距離δ'を返す. // そうでなければ、再帰呼び出しが発見した最近点対と距離δを返す // NOTE: // 配列Yと配列Yのプレソーティングを避けるため、ソート済み配列PLとPRをマージしてソート済み配列Yを構成する(Pの再構成) std::inplace_merge(P, P + m, P + n, cmp_y()); polygon_t Y_; // 直線lから距離がδ未満の点を入れていく elem_t delta_ = limits::inf; for (index_t i = 0; i < n; i++) { // 直線lよりδ以上離れている(幅2δの垂直帯領域に存在しない)点は考慮から外す if (std::fabs(P[i].x - lx) >= delta) { continue; } index_t limit = Y_.size(); // NOTE: Y'の要素数は高々5個である for (index_t j = 0; j < limit; j++) { // Y'に入っている点を、末尾からy座標の差がδ以上になるまで確認する point p = Y_[limit - j - 1]; if ((P[i].y - p.y) >= delta) { break; } delta_ = std::min(delta_, norm(P[i] - p)); } Y_.push_back(P[i]); } return std::min(delta, delta_); } elem_t closest_pair(polygon_t &P) { std::sort(P.begin(), P.end(), cmp_x()); // xに関して、あらかじめソートしておく(presorting) return closest_pair(P.data(), P.size()); // 最初の再帰呼び出し } //**************************************** // エントリポイント //**************************************** int main() { using namespace std; int n; cin >> n; polygon_t P(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; P[i].x = x, P[i].y = y; } cout << closest_pair(P) << endl; return 0; }
[["-", 0, 11, 12, 2, 3, 4, 0, 2, 63, 22], ["+", 0, 11, 12, 2, 3, 4, 0, 2, 63, 22]]
1
961
/** * @brief 最近点対の発見について扱います * @note 関連URL: * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0585 * @date 2016/03/22 */ //**************************************** // 必要なヘッダファイルのインクルード //**************************************** #include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> //**************************************** // オブジェクト形式マクロの定義 //**************************************** //#define GEOMETRY_BEGIN namespace geom { //#define GEOMETRY_END } //**************************************** // 型シノニム //**************************************** using coord_t = long double; using index_t = std::int32_t; using indices_t = std::vector<index_t>; //**************************************** // 構造体の定義 //**************************************** struct point { coord_t x, y; point() : x(0), y(0) {} point(coord_t x, coord_t y) : x(x), y(y) {} point &operator+=(const point &p) { x += p.x; y += p.y; return *this; } point &operator-=(const point &p) { x -= p.x; y -= p.y; return *this; } point &operator*=(const coord_t d) { x *= d; y *= d; return *this; } point operator+(const point &p) const { return point(*this) += p; } point operator-(const point &p) const { return point(*this) -= p; } point operator*(const coord_t d) const { return point(*this) *= d; } }; struct segment { point ps; // 線分の始点 point pd; // 線分の終点 }; namespace limits { constexpr auto pi = 3.141592653589793238; constexpr auto eps = 1e-10; constexpr auto inf = 1e12; } // namespace limits /** * @brief 与えられた3点a, b, cをa -> b -> cと進むとき、 * @details a -> bで時計方向に折れてb -> c(cw) * a -> bで反時計方向に折れてb -> c(ccw) * a -> bで逆を向いてaを通り越してb -> c (back) * a -> bでそのままb -> c(front) * a -> bで逆を向いてb -> cまたは、b == c(on) */ enum struct orientation { cw = -1, ccw = +1, back = +2, front = -2, on = 0, }; /** * @brief 述語オブジェクト */ struct cmp_x { bool operator()(const point &pi, const point &pj) { return pi.x < pj.x; } }; struct cmp_y { bool operator()(const point &pi, const point &pj) { return pi.y < pj.y; } }; //**************************************** // 型シノニムその2 //**************************************** using vector_t = point; using polygon_t = std::vector<point>; using segments_t = std::vector<segment>; //**************************************** // 関数の定義 //**************************************** /** * @brief 2つのベクトルa, bからなる行列A = (a, * b)の行列式(determinant)を返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return coord_t det(A) 行列式|(a, b)| */ static constexpr coord_t det(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; } /** * @brief 2つのベクトルa, bのクロス積(cross product)a x bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return coord_t a x b クロス積a x b */ static constexpr coord_t cross(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; // return det(a, b); } /** * @brief 2つのベクトルa, bのドット積(dot product)a・bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return coord_t a・b ドット積a・b */ static constexpr coord_t dot(const vector_t &a, const vector_t &b) { return a.x * b.x + a.y * b.y; } /** * @brief ベクトルvのノルム(norm・大きさの2乗)を返します * @param const vector_t& v * @return coord_t norm(v); */ static constexpr coord_t norm(const vector_t &v) { return v.x * v.x + v.y * v.y; // return dot(v, v); } /** * @brief ベクトルvの大きさ|v|(原点からの距離)を返します * @param const vector_t& v * @return coord_t sqrt(norm(v)) */ static inline coord_t abs(const vector_t &v) { return std::sqrt(norm(v)); } /** * @brief 点pから線分sに下ろした垂線と線分sの交点を返します * @param const segment& s 線分s * @param const point& p 点p * @return 垂線と線分の交点 */ static inline point proj(const segment &s, const point &p) { vector_t base = s.pd - s.ps; vector_t hypo = p - s.ps; coord_t r = dot(hypo, base) / norm(base); return s.ps + base * r; } /** * @brief 点pと線対称の位置に存在する点を返します * @param cosnt segment& s 線分s * @param const point& p 点p * @param pと線対称な点 */ static inline point reflect(const segment &s, const point p) { return p + (proj(s, p) - p) * 2.0; } /** * @brief COMBINED-TOLERANCE-COMPAREより少ない労力で行える近似的な判定 */ static inline bool approximate_combined_tolerance_compare(coord_t x, coord_t y) noexcept { return std::fabs(x - y) <= limits::eps * (std::fabs(x) + std::fabs(y) + 1.0); } /** * @brief 手続きAPPROXIMATE-COMBINED-TOLERANCE-COMPAREの短い名前 */ static inline bool eq(coord_t x, coord_t y) { return approximate_combined_tolerance_compare(x, y); } /** * @brief 述語オブジェクト */ struct cmp_xy { bool operator()(const point &pi, const point &pj) { return pi.x < pj.x || (eq(pi.x, pj.x) && pi.y < pj.y); } }; /** * @brinf 2点(p1, p2)のp0に関する偏角(polar angle)から、 * p0から見た2つのベクトルp0p1↑, p0p2↑の方向を返す */ static inline orientation ccw(point p0, point p1, point p2) { p1 -= p0; p2 -= p0; if (cross(p1, p2) > limits::eps) { return orientation::ccw; } // クロス積(p1-p0)x(p2-p0)が正の場合、ccw ...(*1) if (cross(p1, p2) < -limits::eps) { return orientation::cw; } // クロス積(p1-p0)x(p2-p0)が負の場合、cw ...(*2) // (*1), // (*2)に当てはまらないとき、p2は直線p0p1|上(線分p0p1↑上とは限らない)に存在する if (dot(p1, p2) < -limits::eps) { return orientation::back; } // ドット積(p1-p0)・(p2-p0)が負の場合、p2->p0->p1(back) ...(*3) // (*3)に当てはまらないとき、p2はp0->p1->p2またはp0->p2->p1の位置に存在する if (eq(norm(p1), norm(p2))) { return orientation::front; } // p0p2↑の大きさがp0p1↑の大きさより大きい場合、p0->p1->p2(front) ...(*4) // (*4)に当てはまらないとき、p0->p2->p1(on) return orientation::on; } //**************************************** // 関数の定義 //**************************************** /** * @brief 分割統治アルゴリズムによって最近点対を発見します * * @param point*P 点の集合P * @param index_t n 集合Pの要素数n */ coord_t closest_pair(point *P, index_t n) { if (n <= 1) { // |P| <= 1か否かを判定する return limits::inf; // |P| <= 1ならば、再帰の基底 } // |P| > 1ならば、再帰呼び出しによって、以下のように分割統治法を実行する // 分割: // 以下の条件を満たすように、点集合Pを2つの集合PLとPRに分割する垂直線lを求める // |PL|=ceil(|P|/2), // |PR|=floor(|P|/2)であり、PLの点はすべてlの上または左に、 // PRの点はすべてlの上または右にある index_t m = n / 2; coord_t lx = P[m].x; // NOTE: // このとき、PRおよびPLはxを昇順に含む(presortingによって保証済み) // 統治: PをPLとPRに分割した後、1回はPLにおける最近点対を求めるために、 // もう1回はPRにおける最近点対を求めるために、2回の再帰呼び出しを行う // PLとPRに対して求めた最近点対間距離をδLとδRとし、δ = min(δL, δR)と置く coord_t delta = std::min(closest_pair(P, m), closest_pair(P + m, n - m)); // 結合: // 最近点対は、上の再帰呼び出しの一方で見つかった距離がδの点対か、1点がPLに属し、 // もう1点がPRに属する点対のどちらかである. // アルゴリズムは、一方がPLに属し、 // 他方がPRに属する点対で、距離がδ未満のものが存在するか否かを判定する // 距離がδ未満の点対を構成する2つの点は、それぞれ、直線lからδ単位距離以内に存在する // したがって、このような2点は直線lを中心とする、幅2δの垂直帯領域に存在する // このような点対を、存在するならば、以下の要領で発見する // // 1. // 配列Y(P)から、2δ幅の垂直帯領域を除く領域に属するすべての点を除去してできる、 // 配列Y'を生成する. 配列Y'はYと同様、y座標に関してソートされている // // 2. // 配列Y'の各点pについて、pからδ単位未満の距離にあるY'の点を発見しようと努める // このとき、Y'においてpに続く5個の点だけに候補対象を限定できる // pからこれら5個の点までの距離を計算し、Y'のすべての点対について発見できた // 最近点対の距離δ'を記憶する // // 3. δ' < // δならば、この垂直帯領域は再帰呼び出しが発見したものより近い点対を確かに含む // そこで、この点対と距離δ'を返す. // そうでなければ、再帰呼び出しが発見した最近点対と距離δを返す // NOTE: // 配列Yと配列Yのプレソーティングを避けるため、ソート済み配列PLとPRをマージしてソート済み配列Yを構成する(Pの再構成) std::inplace_merge(P, P + m, P + n, cmp_y()); polygon_t Y_; // 直線lから距離がδ未満の点を入れていく for (index_t i = 0; i < n; i++) { // 直線lよりδ以上離れている(幅2δの垂直帯領域に存在しない)点は考慮から外す if (std::fabs(P[i].x - lx) >= delta) { continue; } index_t limit = Y_.size(); // NOTE: Y'の要素数は高々5個である for (index_t j = 0; j < limit; j++) { // Y'に入っている点を、末尾からy座標の差がδ以上になるまで確認する point p = Y_[limit - j - 1]; if ((P[i].y - p.y) >= delta) { break; } delta = std::min(delta, abs(P[i] - p)); } Y_.push_back(P[i]); } return delta; } /** * @brief 分割統治アルゴリズムによって最近点対を発見します * * @param polygon_t&P 点の集合P */ coord_t closest_pair(polygon_t &P) { std::sort(P.begin(), P.end(), cmp_x()); // xに関して、あらかじめソートしておく(presorting) return closest_pair(P.data(), P.size()); // 最初の再帰呼び出し } //**************************************** // エントリポイント //**************************************** int main() { using namespace std; int n; cin >> n; polygon_t P(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; P[i].x = x, P[i].y = y; } cout << closest_pair(P) << endl; return 0; }
/** * @brief 最近点対の発見について扱います * @note 関連URL: * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0585 * @date 2016/03/22 */ //**************************************** // 必要なヘッダファイルのインクルード //**************************************** #include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> //**************************************** // オブジェクト形式マクロの定義 //**************************************** //#define GEOMETRY_BEGIN namespace geom { //#define GEOMETRY_END } //**************************************** // 型シノニム //**************************************** using coord_t = long double; using index_t = std::int32_t; using indices_t = std::vector<index_t>; //**************************************** // 構造体の定義 //**************************************** struct point { coord_t x, y; point() : x(0), y(0) {} point(coord_t x, coord_t y) : x(x), y(y) {} point &operator+=(const point &p) { x += p.x; y += p.y; return *this; } point &operator-=(const point &p) { x -= p.x; y -= p.y; return *this; } point &operator*=(const coord_t d) { x *= d; y *= d; return *this; } point operator+(const point &p) const { return point(*this) += p; } point operator-(const point &p) const { return point(*this) -= p; } point operator*(const coord_t d) const { return point(*this) *= d; } }; struct segment { point ps; // 線分の始点 point pd; // 線分の終点 }; namespace limits { constexpr auto pi = 3.141592653589793238; constexpr auto eps = 1e-10; constexpr auto inf = 1e12; } // namespace limits /** * @brief 与えられた3点a, b, cをa -> b -> cと進むとき、 * @details a -> bで時計方向に折れてb -> c(cw) * a -> bで反時計方向に折れてb -> c(ccw) * a -> bで逆を向いてaを通り越してb -> c (back) * a -> bでそのままb -> c(front) * a -> bで逆を向いてb -> cまたは、b == c(on) */ enum struct orientation { cw = -1, ccw = +1, back = +2, front = -2, on = 0, }; /** * @brief 述語オブジェクト */ struct cmp_x { bool operator()(const point &pi, const point &pj) { return pi.x < pj.x; } }; struct cmp_y { bool operator()(const point &pi, const point &pj) { return pi.y < pj.y; } }; //**************************************** // 型シノニムその2 //**************************************** using vector_t = point; using polygon_t = std::vector<point>; using segments_t = std::vector<segment>; //**************************************** // 関数の定義 //**************************************** /** * @brief 2つのベクトルa, bからなる行列A = (a, * b)の行列式(determinant)を返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return coord_t det(A) 行列式|(a, b)| */ static constexpr coord_t det(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; } /** * @brief 2つのベクトルa, bのクロス積(cross product)a x bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return coord_t a x b クロス積a x b */ static constexpr coord_t cross(const vector_t &a, const vector_t &b) { return a.x * b.y - a.y * b.x; // return det(a, b); } /** * @brief 2つのベクトルa, bのドット積(dot product)a・bを返します * @param const vector_t& a ベクトルa * @param const vector_t& b ベクトルb * @return coord_t a・b ドット積a・b */ static constexpr coord_t dot(const vector_t &a, const vector_t &b) { return a.x * b.x + a.y * b.y; } /** * @brief ベクトルvのノルム(norm・大きさの2乗)を返します * @param const vector_t& v * @return coord_t norm(v); */ static constexpr coord_t norm(const vector_t &v) { return v.x * v.x + v.y * v.y; // return dot(v, v); } /** * @brief ベクトルvの大きさ|v|(原点からの距離)を返します * @param const vector_t& v * @return coord_t sqrt(norm(v)) */ static inline coord_t abs(const vector_t &v) { return std::sqrt(norm(v)); } /** * @brief 点pから線分sに下ろした垂線と線分sの交点を返します * @param const segment& s 線分s * @param const point& p 点p * @return 垂線と線分の交点 */ static inline point proj(const segment &s, const point &p) { vector_t base = s.pd - s.ps; vector_t hypo = p - s.ps; coord_t r = dot(hypo, base) / norm(base); return s.ps + base * r; } /** * @brief 点pと線対称の位置に存在する点を返します * @param cosnt segment& s 線分s * @param const point& p 点p * @param pと線対称な点 */ static inline point reflect(const segment &s, const point p) { return p + (proj(s, p) - p) * 2.0; } /** * @brief COMBINED-TOLERANCE-COMPAREより少ない労力で行える近似的な判定 */ static inline bool approximate_combined_tolerance_compare(coord_t x, coord_t y) noexcept { return std::fabs(x - y) <= limits::eps * (std::fabs(x) + std::fabs(y) + 1.0); } /** * @brief 手続きAPPROXIMATE-COMBINED-TOLERANCE-COMPAREの短い名前 */ static inline bool eq(coord_t x, coord_t y) { return approximate_combined_tolerance_compare(x, y); } /** * @brief 述語オブジェクト */ struct cmp_xy { bool operator()(const point &pi, const point &pj) { return pi.x < pj.x || (eq(pi.x, pj.x) && pi.y < pj.y); } }; /** * @brinf 2点(p1, p2)のp0に関する偏角(polar angle)から、 * p0から見た2つのベクトルp0p1↑, p0p2↑の方向を返す */ static inline orientation ccw(point p0, point p1, point p2) { p1 -= p0; p2 -= p0; if (cross(p1, p2) > limits::eps) { return orientation::ccw; } // クロス積(p1-p0)x(p2-p0)が正の場合、ccw ...(*1) if (cross(p1, p2) < -limits::eps) { return orientation::cw; } // クロス積(p1-p0)x(p2-p0)が負の場合、cw ...(*2) // (*1), // (*2)に当てはまらないとき、p2は直線p0p1|上(線分p0p1↑上とは限らない)に存在する if (dot(p1, p2) < -limits::eps) { return orientation::back; } // ドット積(p1-p0)・(p2-p0)が負の場合、p2->p0->p1(back) ...(*3) // (*3)に当てはまらないとき、p2はp0->p1->p2またはp0->p2->p1の位置に存在する if (eq(norm(p1), norm(p2))) { return orientation::front; } // p0p2↑の大きさがp0p1↑の大きさより大きい場合、p0->p1->p2(front) ...(*4) // (*4)に当てはまらないとき、p0->p2->p1(on) return orientation::on; } //**************************************** // 関数の定義 //**************************************** /** * @brief 分割統治アルゴリズムによって最近点対を発見します * * @param point*P 点の集合P * @param index_t n 集合Pの要素数n */ coord_t closest_pair(point *P, index_t n) { if (n <= 1) { // |P| <= 1か否かを判定する return limits::inf; // |P| <= 1ならば、再帰の基底 } // |P| > 1ならば、再帰呼び出しによって、以下のように分割統治法を実行する // 分割: // 以下の条件を満たすように、点集合Pを2つの集合PLとPRに分割する垂直線lを求める // |PL|=ceil(|P|/2), // |PR|=floor(|P|/2)であり、PLの点はすべてlの上または左に、 // PRの点はすべてlの上または右にある index_t m = n / 2; coord_t lx = P[m].x; // NOTE: // このとき、PRおよびPLはxを昇順に含む(presortingによって保証済み) // 統治: PをPLとPRに分割した後、1回はPLにおける最近点対を求めるために、 // もう1回はPRにおける最近点対を求めるために、2回の再帰呼び出しを行う // PLとPRに対して求めた最近点対間距離をδLとδRとし、δ = min(δL, δR)と置く coord_t delta = std::min(closest_pair(P, m), closest_pair(P + m, n - m)); // 結合: // 最近点対は、上の再帰呼び出しの一方で見つかった距離がδの点対か、1点がPLに属し、 // もう1点がPRに属する点対のどちらかである. // アルゴリズムは、一方がPLに属し、 // 他方がPRに属する点対で、距離がδ未満のものが存在するか否かを判定する // 距離がδ未満の点対を構成する2つの点は、それぞれ、直線lからδ単位距離以内に存在する // したがって、このような2点は直線lを中心とする、幅2δの垂直帯領域に存在する // このような点対を、存在するならば、以下の要領で発見する // // 1. // 配列Y(P)から、2δ幅の垂直帯領域を除く領域に属するすべての点を除去してできる、 // 配列Y'を生成する. 配列Y'はYと同様、y座標に関してソートされている // // 2. // 配列Y'の各点pについて、pからδ単位未満の距離にあるY'の点を発見しようと努める // このとき、Y'においてpに続く5個の点だけに候補対象を限定できる // pからこれら5個の点までの距離を計算し、Y'のすべての点対について発見できた // 最近点対の距離δ'を記憶する // // 3. δ' < // δならば、この垂直帯領域は再帰呼び出しが発見したものより近い点対を確かに含む // そこで、この点対と距離δ'を返す. // そうでなければ、再帰呼び出しが発見した最近点対と距離δを返す // NOTE: // 配列Yと配列Yのプレソーティングを避けるため、ソート済み配列PLとPRをマージしてソート済み配列Yを構成する(Pの再構成) std::inplace_merge(P, P + m, P + n, cmp_y()); polygon_t Y_; // 直線lから距離がδ未満の点を入れていく for (index_t i = 0; i < n; i++) { // 直線lよりδ以上離れている(幅2δの垂直帯領域に存在しない)点は考慮から外す if (std::fabs(P[i].x - lx) >= delta) { continue; } index_t limit = Y_.size(); // NOTE: Y'の要素数は高々5個である for (index_t j = 0; j < limit; j++) { // Y'に入っている点を、末尾からy座標の差がδ以上になるまで確認する point p = Y_[limit - j - 1]; if ((P[i].y - p.y) >= delta) { break; } delta = std::min(delta, norm(P[i] - p)); } Y_.push_back(P[i]); } return delta; } /** * @brief 分割統治アルゴリズムによって最近点対を発見します * * @param polygon_t&P 点の集合P */ coord_t closest_pair(polygon_t &P) { std::sort(P.begin(), P.end(), cmp_x()); // xに関して、あらかじめソートしておく(presorting) return closest_pair(P.data(), P.size()); // 最初の再帰呼び出し } //**************************************** // エントリポイント //**************************************** int main() { using namespace std; int n; cin >> n; polygon_t P(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; P[i].x = x, P[i].y = y; } cout << closest_pair(P) << endl; return 0; }
[["-", 0, 11, 12, 2, 3, 4, 0, 2, 63, 22], ["+", 0, 11, 12, 2, 3, 4, 0, 2, 63, 22]]
1
1,284
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 1000000000000000000ll #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < EPS) #define MAX_N 100000000 int N; P A[MAX_N]; bool comp(P a, P b) { return a.sec < b.sec; } ll closest_pair(P *a, int n) { if (n <= 1) return INF; int m = n / 2; ll x = (ll)a[m].fi; ll d = min(closest_pair(a, m), closest_pair(a, n - m)); inplace_merge(a, a + m, a + n, comp); vector<P> b; for (int i = 0; i < n; i++) { if (abs((ll)a[i].fi - (ll)x) >= d) continue; for (int j = 0; j < b.size(); j++) { ll dx = (ll)a[i].fi - (ll)b[b.size() - 1 - j].fi; ll dy = (ll)a[i].sec - (ll)b[b.size() - 1 - j].sec; if (dy >= d) break; d = min(d, dx * dx + dy * dy); } b.pb(a[i]); } return d; } int main() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i].fi >> A[i].sec; sort(A, A + N); cout << closest_pair(A, N) << endl; return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<short, short> P; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 1000000000000000000ll #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < EPS) #define MAX_N 10000000 int N; P A[MAX_N]; bool comp(P a, P b) { return a.sec < b.sec; } ll closest_pair(P *a, int n) { if (n <= 1) return INF; int m = n / 2; ll x = (ll)a[m].fi; ll d = min(closest_pair(a, m), closest_pair(a, n - m)); inplace_merge(a, a + m, a + n, comp); vector<P> b; for (int i = 0; i < n; i++) { if (abs((ll)a[i].fi - (ll)x) >= d) continue; for (int j = 0; j < b.size(); j++) { ll dx = (ll)a[i].fi - (ll)b[b.size() - 1 - j].fi; ll dy = (ll)a[i].sec - (ll)b[b.size() - 1 - j].sec; if (dy >= d) break; d = min(d, dx * dx + dy * dy); } b.pb(a[i]); } return d; } int main() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i].fi >> A[i].sec; sort(A, A + N); cout << closest_pair(A, N) << endl; return 0; }
[["-", 0, 134, 39, 344, 3, 347, 0, 77, 39, 40], ["+", 39, 344, 3, 347, 0, 77, 39, 86, 0, 133], ["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
1
446
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; #define MAX 500000 typedef pair<int, int> P; int N; P A[MAX]; bool compare_y(P a, P b) { return a.second < b.second; } int closest_pair(P *a, int n) { if (n <= 1) return INT_MAX; int m = n / 2; int x = a[m].first; int d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (fabs(a[i].first - x) >= d) continue; for (int j = 0; j < b.size(); j++) { int dx = a[i].first - b[b.size() - j - 1].first; int dy = a[i].second - b[b.size() - j - 1].second; if (dy >= d) break; d = min(d, dx * dx + dy * dy); } b.push_back(a[i]); } return d; } main() { cin >> N; assert(2 <= N && N <= 500000); for (int i = 0; i < N; i++) { scanf("%d %d", &A[i].first, A[i].second); } sort(A, A + N); // printf("%.10lf\n", closest_pair(A, N)); printf("%d\n", closest_pair(A, N)); }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; #define MAX 500000 typedef pair<int, int> P; int N; P A[MAX]; bool compare_y(P a, P b) { return a.second < b.second; } int closest_pair(P *a, int n) { if (n <= 1) return INT_MAX; int m = n / 2; int x = a[m].first; int d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<P> b; for (int i = 0; i < n; i++) { if (fabs(a[i].first - x) >= d) continue; for (int j = 0; j < b.size(); j++) { int dx = a[i].first - b[b.size() - j - 1].first; int dy = a[i].second - b[b.size() - j - 1].second; if (dy >= d) break; d = min(d, dx * dx + dy * dy); } b.push_back(a[i]); } return d; } main() { cin >> N; assert(2 <= N && N <= 500000); for (int i = 0; i < N; i++) { scanf("%d %d", &A[i].first, &A[i].second); } sort(A, A + N); // printf("%.10lf\n", closest_pair(A, N)); printf("%d\n", closest_pair(A, N)); }
[["+", 0, 1, 0, 2, 3, 4, 0, 66, 17, 67]]
1
369
#include <stdio.h> int main(void) { int n, m, k, s; scanf("%d %d", &n, &m); s = m; int max = s; int i[10000][2] = {}; for (k = 0; k < n; k++) { scanf("%d %d", &i[k][0], &i[k][1]); s += i[k][0]; s -= i[k][1]; if (s < 0) { printf("0\n"); break; } if (max < s) max = s; } printf("%d\n", max); return 0; }
#include <stdio.h> int main(void) { int n, m, k, s; scanf("%d %d", &n, &m); s = m; int max = s; int i[10000][2] = {}; for (k = 0; k < n; k++) { scanf("%d %d", &i[k][0], &i[k][1]); s += i[k][0]; s -= i[k][1]; if (s < 0) { max = 0; break; } if (max < s) max = s; } printf("%d\n", max); return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 2, 63, 22], ["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 25]]
0
153
/*AOJ No.5687 Tunnel*/ #include <stdio.h> int main() { int n; /*time*/ int tunnel; int i; int max; int incar; int outcar; char check = 1; scanf("%d", &n); scanf("%d", &tunnel); max = tunnel; for (i = 0; i < n; i++) { scanf("%d %d", &incar, &outcar); tunnel += incar; tunnel -= outcar; if (tunnel < 0) { check = 0; } else if (tunnel > max) { max = tunnel; } } if (tunnel == 1) { printf("%d\n", max); } else { printf("0\n"); } return 0; }
#include <stdio.h> int main() { int n; int tunnel; int i; int max; int incar; int outcar; char check = 1; scanf("%d", &n); scanf("%d", &tunnel); max = tunnel; for (i = 0; i < n; i++) { scanf("%d %d", &incar, &outcar); tunnel += incar; tunnel -= outcar; if (tunnel < 0) { check = 0; } else if (tunnel > max) { max = tunnel; } } if (check == 1) { printf("%d\n", max); } else { printf("0\n"); } return 0; }
[["-", 8, 9, 0, 57, 15, 23, 0, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 31, 22]]
0
150
#include <stdio.h> int main(void) { int n, m, i, j, count[10001], carin, carout, in, out, box, zero; scanf("%d", &n); scanf("%d", &m); for (i = 1; i <= n; i++) { scanf("%d %d", &in, &out); if (i == 1) { count[i] = m + in - out; } else count[i] = count[i - 1] + in - out; if (count[i] < 0) { zero = 0; } if (i == 1 && m >= count[i]) box = m; else if (i == 1) box = count[i]; if (i > 1 && box <= count[i]) box = count[i]; else box = box; } if (zero == 0) printf("0\n"); else if (box > 0) printf("%d\n", box); return 0; }
#include <stdio.h> int main(void) { int n, m, i, j, count[10001], carin, carout, in, out, box, zero = 1; scanf("%d", &n); scanf("%d", &m); for (i = 1; i <= n; i++) { scanf("%d %d", &in, &out); if (i == 1) { count[i] = m + in - out; } else count[i] = count[i - 1] + in - out; if (count[i] < 0) { zero = 0; } if (i == 1 && m >= count[i]) box = m; else if (i == 1) box = count[i]; if (i > 1 && box <= count[i]) box = count[i]; else box = box; } if (zero == 0) printf("0\n"); else if (box > 0) printf("%d\n", box); return 0; }
[["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
0
224
#include <algorithm> #include <iostream> #include <math.h> #include <stack> #include <stdio.h> #include <vector> using namespace std; int main() { int n, m, sum, in, out, max; bool FLG = true; scanf("%d", &n); scanf("%d", &m); sum = m; max = m; for (int i = 0; i < n; i++) { scanf("%d %d", &in, &out); sum = m + in - out; if (sum < 0) { printf("0\n"); FLG = false; break; } max = (max >= sum) ? max : sum; } if (FLG) printf("%d\n", max); return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <stack> #include <stdio.h> #include <vector> using namespace std; int main() { int n, m, sum, in, out, max; bool FLG = true; scanf("%d", &n); scanf("%d", &m); sum = m; max = m; for (int i = 0; i < n; i++) { scanf("%d %d", &in, &out); sum += (in - out); if (sum < 0) { printf("0\n"); FLG = false; break; } max = (max >= sum) ? max : sum; } if (FLG) printf("%d\n", max); return 0; }
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["-", 0, 1, 0, 11, 12, 16, 31, 16, 31, 22], ["-", 0, 1, 0, 11, 12, 16, 31, 16, 17, 72], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 107], ["+", 8, 9, 0, 1, 0, 11, 12, 23, 0, 24], ["+", 8, 9, 0, 1, 0, 11, 12, 23, 0, 25]]
1
156
#include <bits/stdc++.h> using namespace std; int main() { int n, m, ans, inc, outc; cin >> n >> m; ans = m; while (n--) { cin >> inc >> outc; m = ans; m += inc - outc; if (m < 0) { cout << 0 << endl; return 0; } ans = max(m, ans); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, ans, inc, outc; cin >> n >> m; ans = m; while (n--) { cin >> inc >> outc; m += inc - outc; if (m < 0) { cout << 0 << endl; return 0; } ans = max(m, ans); } cout << ans << endl; return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 22], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["-", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22]]
1
91
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int maxn = 0; cin >> n; cin >> m; int *in = new int[n]; int *out = new int[n]; int *S = new int[n + 1]; for (int i = 0; i < n; ++i) cin >> in[i] >> out[i]; S[0] = m; for (int i = 0; i < n; ++i) { S[i + 1] = m + in[i] - out[i]; m = S[i + 1]; if (S[i + 1] < 0) { cout << "0" << endl; return 0; } if (S[maxn] < S[i + 1]) { maxn = i; } } cout << S[maxn] << endl; delete[] in; delete[] out; delete[] S; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int maxn = 0; cin >> n; cin >> m; int *in = new int[n]; int *out = new int[n]; int *S = new int[n + 1]; for (int i = 0; i < n; ++i) cin >> in[i] >> out[i]; S[0] = m; for (int i = 0; i < n; ++i) { S[i + 1] = m + in[i] - out[i]; m = S[i + 1]; if (S[i + 1] < 0) { cout << "0" << endl; return 0; } if (S[maxn] < S[i + 1]) { maxn = i + 1; } } cout << S[maxn] << endl; delete[] in; delete[] out; delete[] S; return 0; }
[["+", 64, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
210
#include <algorithm> #include <iostream> using namespace std; int main(void) { int n, cnt, in, out, mx; cin >> n >> cnt; mx = cnt; while (n--) { cin >> in >> out; cnt += in - out; mx = max(mx, cnt); if (cnt < 0) { cout << 0 << endl; return 0; } } cout << cnt << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main(void) { int n, cnt, in, out, mx; cin >> n >> cnt; mx = cnt; while (n--) { cin >> in >> out; cnt += in - out; mx = max(mx, cnt); if (cnt < 0) { cout << 0 << endl; return 0; } } cout << mx << endl; return 0; }
[["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22]]
1
90
#include <cstdio> using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); int ans = m; for (int i = 0; i < n; i++) { int a, b; scanf("%d %d", &a, &b); m += (a - b); if (m < 0) return (!printf("0\n")); else ans = m > ans ? m : ans; } return (!printf("%d\n", m)); }
#include <cstdio> using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); int ans = m; for (int i = 0; i < n; i++) { int a, b; scanf("%d %d", &a, &b); m += (a - b); if (m < 0) return (!printf("0\n")); else ans = m > ans ? m : ans; } return (!printf("%d\n", ans)); }
[["-", 0, 23, 0, 91, 28, 2, 3, 4, 0, 22], ["+", 0, 23, 0, 91, 28, 2, 3, 4, 0, 22]]
1
120
n=int(input()) ans=c=m=int(input()) for i in[0]*n: a,b=map(int,input().split()) if c<0:ans=0;break c+=a-b ans=max(ans,c)
n=int(input()) ans=c=m=int(input()) for i in[0]*n: a,b=map(int,input().split()) if c<0:ans=0;break c+=a-b ans=max(ans,c) print(ans)
[["+", 36, 36, 0, 656, 0, 1, 0, 652, 63, 22], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 24], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 22], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 25]]
5
68
n = int(input()) m = int(input()) maxd = m for i in range(n): inn, out = map(int, input().split()) m += inn - out if m > maxd: maxd = m elif m < 0: m = 0 break print(maxd)
n = int(input()) m = int(input()) maxd = m for i in range(n): inn, out = map(int, input().split()) m += inn - out if m > maxd: maxd = m elif m < 0: maxd = 0 break print(maxd)
[["-", 75, 665, 64, 196, 0, 1, 0, 662, 31, 22], ["+", 75, 665, 64, 196, 0, 1, 0, 662, 31, 22]]
5
69
#include <stdio.h> int main(void) { int scr[5]; int sum = 0; int i; for (i = 0; i < 5; i++) ; scanf("%d", &scr[i]); for (i = 0; i < 5; i++) { if (scr[i] < 40) scr[i] = 40; } for (i = 0; i < 5; i++) sum += scr[i]; printf("%d\n", sum / 5); return 0; }
#include <stdio.h> int main(void) { int scr[5]; int sum = 0; int i; for (i = 0; i < 5; i++) scanf("%d", &scr[i]); for (i = 0; i < 5; i++) { if (scr[i] < 40) scr[i] = 40; } for (i = 0; i < 5; i++) sum += scr[i]; printf("%d\n", sum / 5); return 0; }
[["-", 0, 14, 8, 9, 0, 7, 8, 1, 0, 35]]
0
116
#include <stdio.h> int main(void) { int a; int b; int c; int d; int x; int y; int z; scanf("%d\n%d\n%d\n%d\n%d\n", &a, &b, &c, &d, &z); if (a < 40) { a = 40; } else if (b < 40) { b = 40; } else if (c < 40) { c = 40; } else if (d < 40) { d = 40; } else if (z < 40) { z = 40; } y = a + b + c + d + z; x = y / 5; printf("%d\n", x); return 0; }
#include <stdio.h> int main(void) { int a; int b; int c; int d; int x; int y; int z; scanf("%d\n%d\n%d\n%d\n%d\n", &a, &b, &c, &d, &z); if (a < 40) { a = 40; } if (b < 40) { b = 40; } if (c < 40) { c = 40; } if (d < 40) { d = 40; } if (z < 40) { z = 40; } y = a + b + c + d + z; x = y / 5; printf("%d\n", x); return 0; }
[["-", 0, 14, 8, 9, 0, 57, 75, 76, 0, 95], ["-", 0, 57, 75, 76, 0, 57, 75, 76, 0, 95]]
0
156
#include <stdio.h> int i, j, k; int main() { for (; i < 5; i++) { scanf("%d", &j); k += j; } printf("%d\n", k / 5); return 0; }
#include <stdio.h> int i, j, k; int main() { for (; i < 5; i++) { scanf("%d", &j); k += j < 40 ? 40 : j; } printf("%d\n", k / 5); return 0; }
[["+", 0, 1, 0, 11, 12, 41, 15, 16, 17, 18], ["+", 0, 1, 0, 11, 12, 41, 15, 16, 12, 13], ["+", 8, 9, 0, 1, 0, 11, 12, 41, 0, 101], ["+", 8, 9, 0, 1, 0, 11, 12, 41, 64, 13], ["+", 8, 9, 0, 1, 0, 11, 12, 41, 0, 102], ["+", 8, 9, 0, 1, 0, 11, 12, 41, 75, 22]]
0
56
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { int a[10]; int sum = 0; for (int i = 0; i < 5; i++) { cin >> a[i]; if (a[i] < 40) a[i] = 40; sum += a[i]; } cout << sum << endl; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { int a[10]; int sum = 0; for (int i = 0; i < 5; i++) { cin >> a[i]; if (a[i] < 40) a[i] = 40; sum += a[i]; } cout << sum / 5 << endl; }
[["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 85], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
80
#include <stdio.h> int a, i, sum = 0; int main(void) { for (i = 0; i < 5; i++) { scanf("%d", &a); if (a < 40) { a = 40; } sum += a; } printf("%d\n", sum); }
#include <stdio.h> int a, i, sum = 0; int main(void) { for (i = 0; i < 5; i++) { scanf("%d", &a); if (a < 40) { a = 40; } sum += a; } sum /= 5; printf("%d\n", sum); }
[["+", 0, 14, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 17, 90], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
69
#include <iostream> using namespace std; int main() { int a; int sum = 0; for (int i = 0; i < 5; i++) { cin >> a; if (a < 40) sum += 40; else sum += a; } sum = sum / 5; cout << a << endl; return 0; }
#include <iostream> using namespace std; int main() { int a; int sum = 0; for (int i = 0; i < 5; i++) { cin >> a; if (a < 40) sum += 40; else sum += a; } sum = sum / 5; cout << sum << endl; return 0; }
[["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22]]
1
70
#include <stdio.h> int main() { int s = 0, x; for (int i = 0; i < 5; i++) { scanf("%d", &x); if (x < 40) x = 40; s += x; } printf("%d\n", x / 5); return 0; }
#include <stdio.h> int main() { int s = 0, x; for (int i = 0; i < 5; i++) { scanf("%d", &x); if (x < 40) x = 40; s += x; } printf("%d\n", s / 5); return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22]]
1
70
#include <stdio.h> int main(void) { int t, j, sa, shi, h; int ans; scanf("%d %d %d %d %d", &t, &j, &sa, &shi, &h); if (t < 40) { t = 40; } if (j < 40) { j = 40; } if (sa < 40) { sa = 40; } if (shi < 40) { shi = 40; } if (h < 40) { j = 40; } ans = (t + j + sa + shi + h) / 5; printf("%d\n", ans); return 0; }
#include <stdio.h> int main(void) { int t, j, sa, shi, h; int ans; scanf("%d %d %d %d %d", &t, &j, &sa, &shi, &h); if (t < 40) { t = 40; } if (j < 40) { j = 40; } if (sa < 40) { sa = 40; } if (shi < 40) { shi = 40; } if (h < 40) { h = 40; } ans = (t + j + sa + shi + h) / 5; printf("%d\n", ans); return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22]]
1
134
#include <algorithm> #include <bits/stdc++.h> #include <queue> #include <vector> using namespace std; int main() { int ave = 0; for (int i = 0; i < 6; i++) { int a; scanf("%d", &a); ave += max(40, a); } printf("%d\n", ave / 6); }
#include <algorithm> #include <bits/stdc++.h> #include <queue> #include <vector> using namespace std; int main() { int ave = 0; for (int i = 0; i < 5; i++) { int a; scanf("%d", &a); ave += max(40, a); } printf("%d\n", ave / 5); }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
73
#include <bits/stdc++.h> using namespace std; int main() { int sum = 0, x; for (int i = 0; i < 5; i++) { cin >> x; sum += x; } cout << sum / 5 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int sum = 0, x; for (int i = 0; i < 5; i++) { cin >> x; sum += max(40, x); } cout << sum / 5 << endl; return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 13], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 21], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 25]]
1
54
#include <bits/stdc++.h> using namespace std; int main() { int a = 0; int b; for (int c = 0; c < 5; c++) { scanf("%d", &b); if (b < 40) b = 40; a += b; } printf("%d\n", a); }
#include <bits/stdc++.h> using namespace std; int main() { int a = 0; int b; for (int c = 0; c < 5; c++) { scanf("%d", &b); if (b < 40) b = 40; a += b; } printf("%d\n", a / 5); }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 85], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
70
#include <stdio.h> int average(int a[]) { int i, sum = 0; for (i = 0; i < 5; i++) { if (a[i] < 40) { a[i] = 40; } sum = sum + a[i]; } return (sum / 5); } int main(void) { int a[5]; int s; int i; for (i = 0; i < 5; i++) { scanf("5d", &a[i]); } s = average(a); printf("%d\n", s); return (0); }
#include <stdio.h> int average(int a[]) { int i, sum = 0; for (i = 0; i < 5; i++) { if (a[i] < 40) { a[i] = 40; } sum = sum + a[i]; } return (sum / 5); } int main(void) { int a[5]; int s; int i; for (i = 0; i < 5; i++) { scanf("%d", &a[i]); } s = average(a); printf("%d\n", s); return (0); }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
137
#include <bits/stdc++.h> using namespace std; int main() { int data; int sum = 0; for (int i = 0; i < 5; i++) { cin >> data; if (data < 40) data = 40; sum += data; } cout << data / 5 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int data; int sum = 0; for (int i = 0; i < 5; i++) { cin >> data; if (data < 40) data = 40; sum += data; } cout << sum / 5 << endl; return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 16, 31, 22], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 31, 22]]
1
65
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; int main() { int sum = 0; FOR(i, 0, 5) { int in; cin >> in; sum += in; } cout << sum / 5 << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; int main() { int sum = 0; FOR(i, 0, 5) { int in; cin >> in; sum += max(in, 40); } cout << sum / 5 << endl; return 0; }
[["+", 0, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 21], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 13], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 25]]
1
99
#include <algorithm> #include <iostream> using namespace std; int main() { int d, ret = 0; for (int i = 0; i < 5; i++) { cin >> d; ret += max(ret, 40); } cout << ret / 5 << endl; }
#include <algorithm> #include <iostream> using namespace std; int main() { int d, ret = 0; for (int i = 0; i < 5; i++) { cin >> d; ret += max(d, 40); } cout << ret / 5 << endl; }
[["-", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22]]
1
58
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main(void) { int ans = 0, a; for (int i = 0; i < 5; i++) { cin >> a; ans += a; } cout << ans / 5 << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main(void) { int ans = 0, a; for (int i = 0; i < 5; i++) { cin >> a; ans += max(40, a); } cout << ans / 5 << endl; return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 13], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 21], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 25]]
1
59
#include <iostream> using namespace std; int main() { int sum = 0; // your code goes here for (int i = 0; i < 5; i++) { int r; cin >> r; sum += max(r, 40); } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main() { int sum = 0; // your code goes here for (int i = 0; i < 5; i++) { int r; cin >> r; sum += max(r, 40); } cout << sum / 5 << endl; return 0; }
[["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 85], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
59
#include <algorithm> #include <iostream> using namespace std; int main() { int sum = 0, x; while (cin >> x) sum += min(40, x); cout << sum / 5 << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int sum = 0, x; while (cin >> x) sum += max(40, x); cout << sum / 5 << endl; return 0; }
[["-", 0, 52, 8, 1, 0, 11, 12, 2, 63, 22], ["+", 0, 52, 8, 1, 0, 11, 12, 2, 63, 22]]
1
47
#include <bits/stdc++.h> using namespace std; int main() { int a = 0; for (int i = 0; i < 5; i++) { int p; scanf("%d", &p); a += min(8, p / 5); } printf("%d\n", a); return (0); }
#include <bits/stdc++.h> using namespace std; int main() { int a = 0; for (int i = 0; i < 5; i++) { int p; scanf("%d", &p); a += max(8, p / 5); } printf("%d\n", a); return (0); }
[["-", 8, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 2, 63, 22]]
1
72
#include <stdio.h> int main() { int i, j, N, M, A[1000], B[1000], score[1000], max; scanf("%d %d", &N, &M); for (i = 0; i < N; i++) { scanf("%d", &A[i]); score[i] = 0; } for (j = 0; j < M; j++) { scanf("%d", &B[j]); i = 0; while (A[i] > B[j]) { i++; } score[i]++; } max = 0; for (i = 0; i < N; i++) { if (score[max] < score[i]) max = i; } printf("%d\n", max); return 0; }
#include <stdio.h> int main() { int i, j, N, M, A[1000], B[1000], score[1000], max; scanf("%d %d", &N, &M); for (i = 0; i < N; i++) { scanf("%d", &A[i]); score[i] = 0; } for (j = 0; j < M; j++) { scanf("%d", &B[j]); i = 0; while (A[i] > B[j]) { i++; } score[i]++; } max = 0; for (i = 0; i < N; i++) { if (score[max] < score[i]) max = i; } printf("%d\n", max + 1); return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
185
#include <bits/stdc++.h> using namespace std; #define Rep(i, N) for (int i = 0; i < N; i++) typedef pair<int, int> Pi; #define F first #define S second int main() { int N, M; int cost[1005]; int cnt[1005] = {0}; cin >> N >> M; Rep(i, N) cin >> cost[i]; int data; while (M--) { cin >> data; Rep(i, N) { if (data >= cost[i]) { cnt[i]++; break; } } } cout << *max_element(cnt, cnt + N) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define Rep(i, N) for (int i = 0; i < N; i++) typedef pair<int, int> Pi; #define F first #define S second int main() { int N, M; int cost[1005]; int cnt[1005] = {0}; cin >> N >> M; Rep(i, N) cin >> cost[i]; int data; while (M--) { cin >> data; Rep(i, N) { if (data >= cost[i]) { cnt[i]++; break; } } } cout << max_element(cnt, cnt + N) - cnt + 1 << endl; return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 66, 17, 48], ["+", 0, 16, 31, 16, 12, 16, 31, 16, 17, 33], ["+", 0, 16, 31, 16, 12, 16, 31, 16, 12, 22], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
134
#include <algorithm> #include <iostream> #include <math.h> #include <sstream> #include <stdio.h> #include <string> #define FOR(i, a) for (int i = 0; i < a; i++) using namespace std; int main() { int n, m; cin >> n >> m; int a[n]; int b[m]; int ans[n]; int maxcount = 0; FOR(i, n) { cin >> a[i]; ans[i] = 0; } FOR(i, m) { cin >> b[i]; FOR(j, n) { if (a[j] <= b[i]) { ans[j]++; } } } int pos = 0; FOR(i, n) { if (maxcount < ans[i]) { maxcount = ans[i]; pos = i; } } cout << pos + 1 << endl; return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <sstream> #include <stdio.h> #include <string> #define FOR(i, a) for (int i = 0; i < a; i++) using namespace std; int main() { int n, m; cin >> n >> m; int a[n]; int b[m]; int ans[n]; int maxcount = 0; FOR(i, n) { cin >> a[i]; ans[i] = 0; } FOR(i, m) { cin >> b[i]; FOR(j, n) { if (a[j] <= b[i]) { ans[j]++; break; } } } int pos = 0; FOR(i, n) { if (maxcount < ans[i]) { maxcount = ans[i]; pos = i; } } cout << pos + 1 << endl; return 0; }
[["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 35]]
1
175
#include <stdio.h> typedef struct Competition { int cost, vote; } Competition; int main() { int i, j, n, m, cost, max_vote = 0, max_index; Competition sports[1000]; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { sports[i].vote = 0; scanf("%d", &sports[i].cost); } for (i = 0; i < m; i++) { scanf("%d", &cost); for (j = 0; j < n; j++) { if (sports[j].cost <= cost) { sports[j].vote++; if (max_vote < sports[j].vote) { max_vote = sports[j].vote; max_index = j; } break; } } } printf("%d\n", max_vote + 1); return 0; }
#include <stdio.h> typedef struct Competition { int cost, vote; } Competition; int main() { int i, j, n, m, cost, max_vote = 0, max_index; Competition sports[1000]; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { sports[i].vote = 0; scanf("%d", &sports[i].cost); } for (i = 0; i < m; i++) { scanf("%d", &cost); for (j = 0; j < n; j++) { if (sports[j].cost <= cost) { sports[j].vote++; if (max_vote < sports[j].vote) { max_vote = sports[j].vote; max_index = j; } break; } } } printf("%d\n", max_index + 1); return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22]]
1
199
#include <algorithm> #include <cmath> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define ALL(co) co.begin(), co.end() typedef long long LL; typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP; typedef vector<int> Array; typedef vector<vector<int>> Array2; typedef vector<int> LArray; const int INF = 1 << 29; const LL LINF = 1LL << 60; int itiv; inline int getInt() { return (cin >> itiv, itiv); } void readAll(Array &vec, int n) { for (int i = 0; i < n; i++) cin >> vec[i]; } inline bool between(int first, int last, int n) { return first <= n && n <= last; } inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; } inline bool inRange(int size, int n) { return 0 <= n && n < size; } int m, n; int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int dr[] = {0, -1, 0, 1}, dc[] = {-1, 0, 1, 0}; int main(void) { while (cin >> n >> m && n) { Array kyougi(n), sinsa(m); Array hyou(n); readAll(kyougi, n); readAll(kyougi, m); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (kyougi[j] <= sinsa[i]) { hyou[j]++; break; } } } cout << (max_element(ALL(hyou)) - hyou.begin()) + 1 << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define ALL(co) co.begin(), co.end() typedef long long LL; typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP; typedef vector<int> Array; typedef vector<vector<int>> Array2; typedef vector<int> LArray; const int INF = 1 << 29; const LL LINF = 1LL << 60; int itiv; inline int getInt() { return (cin >> itiv, itiv); } void readAll(Array &vec, int n) { for (int i = 0; i < n; i++) cin >> vec[i]; } inline bool between(int first, int last, int n) { return first <= n && n <= last; } inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; } inline bool inRange(int size, int n) { return 0 <= n && n < size; } int m, n; int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int dr[] = {0, -1, 0, 1}, dc[] = {-1, 0, 1, 0}; int main(void) { while (cin >> n >> m && n) { Array kyougi(n), sinsa(m); Array hyou(n); readAll(kyougi, n); readAll(sinsa, m); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (kyougi[j] <= sinsa[i]) { hyou[j]++; break; } } } cout << (max_element(ALL(hyou)) - hyou.begin()) + 1 << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
421
# coding: utf-8 N, M = list(map(int, input().split(' '))) A = [int(input()) for _ in range(N)] B = [int(input()) for _ in range(M)] scores = [0 for _ in range(N)] for j in range(M): for i in range(N): if A[i] <= B[j]: scores[i] += 1 break max_i, max_val = 0, 0 for i in range(1, N): if scores[i] > max_val: max_val = scores[i] max_i = i print(max_i + 1)
# coding: utf-8 N, M = list(map(int, input().split(' '))) A = [int(input()) for _ in range(N)] B = [int(input()) for _ in range(M)] scores = [0 for _ in range(N)] for j in range(M): for i in range(N): if A[i] <= B[j]: scores[i] += 1 break max_i, max_val = 0, 0 for i in range(N): if scores[i] > max_val: max_val = scores[i] max_i = i print(max_i + 1)
[["-", 0, 656, 0, 7, 12, 652, 3, 4, 0, 612], ["-", 0, 656, 0, 7, 12, 652, 3, 4, 0, 21]]
5
143
def main(): n,m = map(int,input().split()) A = [int(input()) for _ in range(n)] V = [0 for _ in range(n)] m = 0 ans = 0 for i in range(m): b = int(input()) for j in range(n): if A[j] <= b: V[j] += 1 if m < V[j]: m += 1 ans = j break print(ans + 1) main()
def main(): n,m = map(int,input().split()) A = [int(input()) for _ in range(n)] V = [0 for _ in range(n)] M = 0 ans = 0 for i in range(m): b = int(input()) for j in range(n): if A[j] <= b: V[j] += 1 if M < V[j]: M += 1 ans = j break print(ans + 1) main()
[["-", 0, 14, 8, 196, 0, 1, 0, 662, 31, 22], ["+", 0, 14, 8, 196, 0, 1, 0, 662, 31, 22], ["-", 0, 57, 64, 196, 0, 57, 15, 666, 0, 22], ["+", 0, 57, 64, 196, 0, 57, 15, 666, 0, 22], ["-", 0, 57, 64, 196, 0, 1, 0, 677, 31, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 677, 31, 22]]
5
118
import java.io.*; import java.util.StringTokenizer; class Main { static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; while ((line = br.readLine()) != null && !line.isEmpty()) { StringTokenizer st1 = new StringTokenizer(line); int w = Integer.parseInt(st1.nextToken()); int h = Integer.parseInt(st1.nextToken()); int n = Integer.parseInt(st1.nextToken()); int[] x = new int[n]; int[] y = new int[n]; for (int i = 0; i < n; i++) { line = br.readLine(); StringTokenizer st2 = new StringTokenizer(line); x[i] = Integer.parseInt(st2.nextToken()); y[i] = Integer.parseInt(st2.nextToken()); } int cnt = 0; for (int i = 0; i < n - 1; i++) { cnt += minRoad(x[i], y[i], x[i + 1], y[i + 1]); } out.println(cnt); } } private static int minRoad(int a, int b, int c, int d) { if (a == c) return Math.abs(b - d); if (b == d) return Math.abs(a - c); int x = a - c; int y = b - d; if (x * y > 0) { x = Math.abs(x); y = Math.abs(y); return Math.max(x, y); } x = Math.abs(x); y = Math.abs(y); return x + y; } }
import java.io.*; import java.util.StringTokenizer; class Main { static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; while ((line = br.readLine()) != null && !line.isEmpty()) { StringTokenizer st1 = new StringTokenizer(line); int w = Integer.parseInt(st1.nextToken()); int h = Integer.parseInt(st1.nextToken()); int n = Integer.parseInt(st1.nextToken()); int[] x = new int[n]; int[] y = new int[n]; for (int i = 0; i < n; i++) { line = br.readLine(); StringTokenizer st2 = new StringTokenizer(line); x[i] = Integer.parseInt(st2.nextToken()); y[i] = Integer.parseInt(st2.nextToken()); } int cnt = 0; for (int i = 0; i < n - 1; i++) { cnt += minRoad(x[i], y[i], x[i + 1], y[i + 1]); } out.println(cnt); out.flush(); } } private static int minRoad(int a, int b, int c, int d) { if (a == c) return Math.abs(b - d); if (b == d) return Math.abs(a - c); int x = a - c; int y = b - d; if (x * y > 0) { x = Math.abs(x); y = Math.abs(y); return Math.max(x, y); } x = Math.abs(x); y = Math.abs(y); return x + y; } }
[["+", 0, 52, 8, 196, 0, 1, 0, 492, 500, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 492, 0, 131], ["+", 0, 52, 8, 196, 0, 1, 0, 492, 141, 22], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 24], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 25], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35]]
3
412
#include <stdio.h> int main(void) { int w, h, n; int X[1000], Y[1000]; int i, j, k; int ans, min, difx, dify; scanf("%d%d%d", &w, &h, &n); scanf("%d%d", &X[0], &Y[0]); ans = 0; for (i = 1; i < n; i++) { scanf("%d%d", &X[i], &Y[i]); if (X[i] - X[i - 1] > 0 && Y[i] - Y[i - 1] > 0) { difx = X[i] - X[i - 1]; dify = Y[i] - Y[i - 1]; if (difx > dify) ans += difx; else ans += dify; } else if (X[i] - X[i - 1] < 0 && Y[i] - Y[i - 1] < 0) { difx = X[i - 1] - X[i]; dify = Y[i - 1] - Y[i]; if (difx > dify) ans += difx; else ans += dify; } else { if (X[i] > X[i - 1]) ans += X[i] - X[i - 1]; else ans += X[i - 1] - X[i]; if (Y[i] > Y[i - 1]) ans += Y[i] - Y[i - 1]; else ans += Y[i - 1] - Y[i]; } printf("%d\n", ans); } return 0; }
#include <stdio.h> int main(void) { int w, h, n; int X[1000], Y[1000]; int i, j, k; int ans, min, difx, dify; scanf("%d%d%d", &w, &h, &n); scanf("%d%d", &X[0], &Y[0]); ans = 0; for (i = 1; i < n; i++) { scanf("%d%d", &X[i], &Y[i]); if (X[i] - X[i - 1] > 0 && Y[i] - Y[i - 1] > 0) { difx = X[i] - X[i - 1]; dify = Y[i] - Y[i - 1]; if (difx > dify) ans += difx; else ans += dify; } else if (X[i] - X[i - 1] < 0 && Y[i] - Y[i - 1] < 0) { difx = X[i - 1] - X[i]; dify = Y[i - 1] - Y[i]; if (difx > dify) ans += difx; else ans += dify; } else { if (X[i] > X[i - 1]) ans += X[i] - X[i - 1]; else ans += X[i - 1] - X[i]; if (Y[i] > Y[i - 1]) ans += Y[i] - Y[i - 1]; else ans += Y[i - 1] - Y[i]; } } printf("%d\n", ans); return 0; }
[["+", 0, 14, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 14, 8, 9, 0, 7, 8, 9, 0, 46]]
0
369
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, j) REP((i), 0, (j)) #define REP(i, j, k) for (int i = (j); (i) < (k); ++i) #define BW(a, x, b) ((a) <= (x) && (x) <= (b)) #define ALL(v) (v).begin(), (v).end() #define LENGTHOF(x) (sizeof(x) / sizeof(*(x))) #define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b) #define SQ(x) ((x) * (x)) #define Mod(x, mod) (((x)+(mod)%(mod)) #define MP make_pair #define PB push_back #define F first #define S second #define INF 1 << 30 #define EPS 1e-10 #define MOD 1000000007 typedef pair<int, int> pi; typedef pair<int, pi> pii; typedef vector<int> vi; typedef queue<int> qi; typedef long long ll; int dis(int x, int y, int nx, int ny) { if ((x <= nx && y <= nx) || (x >= nx && y >= nx)) { return max(abs(x - nx), abs(y - ny)); } else { return abs(x - nx) + abs(y - ny); } } int main() { int W, H, N; scanf("%d%d%d", &W, &H, &N); int X, Y; int res = 0; scanf("%d%d", &X, &Y); rep(i, N - 1) { int nX, nY; scanf("%d%d", &nX, &nY); res += dis(X, Y, nX, nY); X = nX; Y = nY; } printf("%d\n", res); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, j) REP((i), 0, (j)) #define REP(i, j, k) for (int i = (j); (i) < (k); ++i) #define BW(a, x, b) ((a) <= (x) && (x) <= (b)) #define ALL(v) (v).begin(), (v).end() #define LENGTHOF(x) (sizeof(x) / sizeof(*(x))) #define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b) #define SQ(x) ((x) * (x)) #define Mod(x, mod) (((x)+(mod)%(mod)) #define MP make_pair #define PB push_back #define F first #define S second #define INF 1 << 30 #define EPS 1e-10 #define MOD 1000000007 typedef pair<int, int> pi; typedef pair<int, pi> pii; typedef vector<int> vi; typedef queue<int> qi; typedef long long ll; int dis(int x, int y, int nx, int ny) { if ((x <= nx && y <= ny) || (x >= nx && y >= ny)) { return max(abs(x - nx), abs(y - ny)); } else { return abs(x - nx) + abs(y - ny); } } int main() { int W, H, N; scanf("%d%d%d", &W, &H, &N); int X, Y; int res = 0; scanf("%d%d", &X, &Y); rep(i, N - 1) { int nX, nY; scanf("%d%d", &nX, &nY); res += dis(X, Y, nX, nY); X = nX; Y = nY; } printf("%d\n", res); return 0; }
[["-", 51, 16, 31, 23, 0, 16, 12, 16, 12, 22], ["+", 51, 16, 31, 23, 0, 16, 12, 16, 12, 22], ["-", 51, 16, 12, 23, 0, 16, 12, 16, 12, 22], ["+", 51, 16, 12, 23, 0, 16, 12, 16, 12, 22]]
1
340
#include <stdio.h> #include <stdlib.h> int main() { int W = 0; int H = 0; int N = 0; int nowx, nowy; //現在位置 int x = 0, y = 0; //次の目的地 int ans = 0; scanf("%d %d %d", &W, &H, &N); // W,Hは使わない scanf("%d %d", &nowx, &nowy); //入力の最初は現座標に for (W = 1; W < N; W++) { scanf("%d %d", &x, &y); //次の座標 //ななめ移動 //右上 while ((nowx - x) < 0 && (nowy - y) < 0) { nowx++; nowy++; ans++; } //左下 while ((nowx - x) > 0 && (nowy - y) > 0) { nowx--; nowy--; ans++; } //上下左右に調整 ans += abs(nowx - x); nowx -= nowx - x; ans += abs(nowx - x); nowy -= nowy - y; } printf("%d\n", ans); return 0; }
#include <stdio.h> #include <stdlib.h> int main() { int W = 0; int H = 0; int N = 0; int nowx, nowy; //現在位置 int x = 0, y = 0; //次の目的地 int ans = 0; scanf("%d %d %d", &W, &H, &N); // W,Hは使わない scanf("%d %d", &nowx, &nowy); //入力の最初は現座標に for (W = 1; W < N; W++) { scanf("%d %d", &x, &y); //次の座標 //ななめ移動 //右上 while ((nowx - x) < 0 && (nowy - y) < 0) { nowx++; nowy++; ans++; } //左下 while ((nowx - x) > 0 && (nowy - y) > 0) { nowx--; nowy--; ans++; } //上下左右に調整 ans += abs(nowx - x); nowx -= nowx - x; ans += abs(nowy - y); nowy -= nowy - y; } printf("%d\n", ans); return 0; }
[["-", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 11, 12, 2, 3, 4, 0, 16, 12, 22], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 12, 22]]
1
211
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int w, h, n, x[20000], y[20000]; int sum = 0, dis; int i, j; scanf("%d %d %d", &w, &h, &n); for (i = 0; i <= n - 1; i++) { scanf("%d %d", &x[i], &y[i]); } for (i = 0; i <= n - 2; i++) { j = i + 1; if (x[i] == x[j]) { dis = abs(y[i] - y[j]); sum = sum + dis; continue; } else if (y[i] == y[j]) { dis = abs(x[i] - x[j]); sum = sum + dis; continue; } else if (((x[i] <= x[j]) && (y[i] <= y[j])) || (x[i] >= x[j]) && (y[i] >= y[j])) { dis = min(abs(x[i] - x[j]), abs(y[i] - y[j])); sum = sum + dis; continue; } else if (((x[i] <= x[j]) && (y[i] >= y[j])) || (x[i] >= x[j]) && (y[i] <= y[j])) { dis = abs(x[i] - x[j]) + abs(y[i] - y[j]); sum = sum + dis; continue; } } printf("%d\n", sum); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int w, h, n, x[20000], y[20000]; int sum = 0, dis; int i, j; scanf("%d %d %d", &w, &h, &n); for (i = 0; i <= n - 1; i++) { scanf("%d %d", &x[i], &y[i]); } for (i = 0; i <= n - 2; i++) { j = i + 1; if (x[i] == x[j]) { dis = abs(y[i] - y[j]); sum = sum + dis; continue; } else if (y[i] == y[j]) { dis = abs(x[i] - x[j]); sum = sum + dis; continue; } else if (((x[i] <= x[j]) && (y[i] <= y[j])) || (x[i] >= x[j]) && (y[i] >= y[j])) { dis = max(abs(x[i] - x[j]), abs(y[i] - y[j])); sum = sum + dis; continue; } else if (((x[i] <= x[j]) && (y[i] >= y[j])) || (x[i] >= x[j]) && (y[i] <= y[j])) { dis = abs(x[i] - x[j]) + abs(y[i] - y[j]); sum = sum + dis; continue; } } printf("%d\n", sum); return 0; }
[["-", 64, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 64, 9, 0, 1, 0, 11, 12, 2, 63, 22]]
1
395
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < EPS) int main() { int n; int a, b, c; cin >> n; if (n == 2) { cin >> a >> b; int g = __gcd(a, b); for (int i = 1; i <= g / 2; i++) { if (g % i == 0) cout << i << endl; } cout << g << endl; return 0; } if (n == 3) { cin >> a >> b >> c; int g = __gcd(a, b); int h = __gcd(h, c); for (int i = 1; i <= h / 2; i++) { if (h % i == 0) cout << i << endl; } cout << h << endl; return 0; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < EPS) int main() { int n; int a, b, c; cin >> n; if (n == 2) { cin >> a >> b; int g = __gcd(a, b); for (int i = 1; i <= g / 2; i++) { if (g % i == 0) cout << i << endl; } cout << g << endl; return 0; } if (n == 3) { cin >> a >> b >> c; int g = __gcd(a, b); int h = __gcd(g, c); for (int i = 1; i <= h / 2; i++) { if (h % i == 0) cout << i << endl; } cout << h << endl; return 0; } return 0; }
[["-", 0, 43, 49, 50, 51, 2, 3, 4, 0, 22], ["+", 0, 43, 49, 50, 51, 2, 3, 4, 0, 22]]
1
274
#include <algorithm> #include <iostream> using namespace std; int main() { int n; cin >> n; int s[n]; sort(s, s + n); for (int i = 0; i < s[0]; i++) cin >> s[i]; for (int i = 1; i <= s[0]; i++) { if (n == 2 && s[0] % i == 0 && s[1] % i == 0) cout << i << endl; else if (n == 3 && s[0] % i == 0 && s[1] % i == 0 && s[2] % i == 0) cout << i << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int n; cin >> n; int s[n]; sort(s, s + n); for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 1; i <= s[0]; i++) { if (n == 2 && s[0] % i == 0 && s[1] % i == 0) cout << i << endl; else if (n == 3 && s[0] % i == 0 && s[1] % i == 0 && s[2] % i == 0) cout << i << endl; } return 0; }
[["-", 8, 9, 0, 7, 15, 16, 12, 69, 28, 22], ["-", 0, 7, 15, 16, 12, 69, 341, 342, 0, 70], ["-", 0, 7, 15, 16, 12, 69, 341, 342, 0, 13], ["-", 0, 7, 15, 16, 12, 69, 341, 342, 0, 73], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
1
152
n = int(input()) values = list(map(int, input().split())) minv = min(vs)+1 print(1) i = 2 ng_value = [1]*minv while i < minv: if not 0 in [1 if values[k] % i == 0 else 0 for k in range(n)]: print(i) else: for j in range(i, minv, i): ng_value[j] = 0 b = True for j in range(i+1, minv): if ng_value[j]: i = j b = False break if b: break
n = int(input()) values = list(map(int, input().split())) minv = min(values)+1 print(1) i = 2 ng_value = [1]*minv while i < minv: if not 0 in [1 if values[k] % i == 0 else 0 for k in range(n)]: print(i) else: for j in range(i, minv, i): ng_value[j] = 0 b = True for j in range(i+1, minv): if ng_value[j]: i = j b = False break if b: break
[["-", 0, 662, 12, 657, 31, 652, 3, 4, 0, 22], ["+", 0, 662, 12, 657, 31, 652, 3, 4, 0, 22]]
5
134
input() n=sorted(list(map(int,input().split()))) m=n[0] a={} for x in range(1,int(m**.5)+1): if m%x==0:a|={x,m//x} for c in sorted(list(a)): for k in n[1:]: if k%c:break else:print(c)
input() n=sorted(list(map(int,input().split()))) m=n[0] a=set() for x in range(1,int(m**.5)+1): if m%x==0:a|={x,m//x} for c in sorted(list(a)): for k in n[1:]: if k%c:break else:print(c)
[["-", 0, 656, 0, 1, 0, 662, 12, 680, 0, 45], ["-", 0, 656, 0, 1, 0, 662, 12, 680, 0, 46], ["+", 0, 656, 0, 1, 0, 662, 12, 652, 63, 22], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 24], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 25]]
5
98
import java.io.IOException; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } private void run() throws IOException { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Pair[] p = new Pair[n]; for (int i = 0; i < n; i++) p[i] = new Pair(scanner.nextInt(), scanner.nextInt()); Arrays.sort(p); int ans = 1 << 25; for (int i = 1; i < n - 1; i++) for (int j = i; j >= 0; j--) { int dx = p[i + 1].x - p[j].x; int dy = p[i + 1].y - p[j].y; if (dx * dy >= ans) break; ans = Math.min(ans, dx * dx + dy * dy); } System.out.println(ans); } class Pair implements Comparable<Pair> { int x, y; @Override public String toString() { return "Pair [x=" + x + ", y=" + y + "]"; } public Pair(int x, int y) { super(); this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return x - o.x; } } }
import java.io.IOException; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } private void run() throws IOException { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Pair[] p = new Pair[n]; for (int i = 0; i < n; i++) p[i] = new Pair(scanner.nextInt(), scanner.nextInt()); Arrays.sort(p); int ans = 1 << 25; for (int i = 0; i < n - 1; i++) for (int j = i; j >= 0; j--) { int dx = p[i + 1].x - p[j].x; int dy = p[i + 1].y - p[j].y; if (dx * dx >= ans) break; ans = Math.min(ans, dx * dx + dy * dy); } System.out.println(ans); } class Pair implements Comparable<Pair> { int x, y; @Override public String toString() { return "Pair [x=" + x + ", y=" + y + "]"; } public Pair(int x, int y) { super(); this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return x - o.x; } } }
[["-", 8, 196, 0, 7, 502, 503, 49, 200, 51, 499], ["+", 8, 196, 0, 7, 502, 503, 49, 200, 51, 499], ["-", 0, 57, 15, 15, 0, 16, 31, 16, 12, 22], ["+", 0, 57, 15, 15, 0, 16, 31, 16, 12, 22]]
3
329
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; import java.util.*; public class Main { int INF = 1 << 28; // long INF = 1L << 62; double EPS = 1e-10; void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int[] a = new int[n], b = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { int c = sc.nextInt(); for (int j = 0; j < n; j++) if (a[j] <= c) { b[i]++; break; } } int ans = -1; for (int i = 0; i < n; i++) { if (ans < 0 || b[ans] < b[i]) { ans = i; } } System.out.println(ans + 1); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new Main().run(); } }
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; import java.util.*; public class Main { int INF = 1 << 28; // long INF = 1L << 62; double EPS = 1e-10; void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int[] a = new int[n], b = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { int c = sc.nextInt(); for (int j = 0; j < n; j++) if (a[j] <= c) { b[j]++; break; } } int ans = -1; for (int i = 0; i < n; i++) { if (ans < 0 || b[ans] < b[i]) { ans = i; } } // debug(b); System.out.println(ans + 1); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new Main().run(); } }
[["-", 64, 196, 0, 1, 0, 27, 0, 504, 71, 22], ["+", 64, 196, 0, 1, 0, 27, 0, 504, 71, 22]]
3
299
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; //??¶???????????¨ int b[] = new int[m]; //???????????? int cnt[] = new int[n]; //???????????? int max = 0, idx = 0; //?????§?????¨???????????????????????? for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); cnt[i] = 0; //???????????????????????? } for (int i = 0; i < m; i++) { b[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { for (int k = 0; k < n; k++) { if (a[k] <= b[i]) { cnt[k]++; break; } } } for (int i = 0; i < n; i++) { if (max < a[i]) { max = a[i]; idx = i; } } System.out.println(idx); } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; //??¶???????????¨ int b[] = new int[m]; //???????????? int cnt[] = new int[n]; //???????????? int max = 0, idx = 0; //?????§?????¨???????????????????????? for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); cnt[i] = 0; //???????????????????????? } for (int i = 0; i < m; i++) { b[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { for (int k = 0; k < n; k++) { if (a[k] <= b[i]) { cnt[k]++; break; } } } for (int i = 0; i < n; i++) { if (max < cnt[i]) { max = cnt[i]; idx = i; } } System.out.println(idx + 1); } }
[["-", 36, 36, 0, 493, 0, 454, 0, 525, 0, 48], ["+", 36, 36, 0, 493, 0, 454, 0, 522, 141, 22], ["-", 0, 57, 15, 15, 0, 16, 12, 504, 516, 22], ["+", 0, 57, 15, 15, 0, 16, 12, 504, 516, 22], ["-", 64, 196, 0, 1, 0, 11, 12, 504, 516, 22], ["+", 64, 196, 0, 1, 0, 11, 12, 504, 516, 22], ["+", 0, 1, 0, 492, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 492, 3, 4, 0, 16, 12, 499]]
3
262
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N, M, max = 0; N = sc.nextInt(); M = sc.nextInt(); int n[] = new int[N]; int m[] = new int[M]; int vote[] = new int[N]; for (int i = 0; i < N; i++) { n[i] = sc.nextInt(); vote[i] = 0; } for (int i = 0; i < M; i++) { m[i] = sc.nextInt(); } for (int i = 0; i < M; i++) { for (int k = 0; k < N; k++) { if (m[i] >= n[k]) { vote[k]++; break; } } } for (int i = 0; i < N; i++) { if (max < vote[i]) max = i; // System.out.println(vote[i]); } System.out.println(max + 1); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N, M, max = 0; N = sc.nextInt(); M = sc.nextInt(); int n[] = new int[N]; int m[] = new int[M]; int vote[] = new int[N]; for (int i = 0; i < N; i++) { n[i] = sc.nextInt(); vote[i] = 0; } for (int i = 0; i < M; i++) { m[i] = sc.nextInt(); } for (int i = 0; i < M; i++) { for (int k = 0; k < N; k++) { if (m[i] >= n[k]) { vote[k]++; break; } } } for (int i = 0; i < N; i++) { if (vote[max] < vote[i]) max = i; // System.out.println(vote[i]); } System.out.println(max + 1); } }
[["+", 0, 57, 15, 15, 0, 16, 31, 504, 516, 22], ["+", 0, 57, 15, 15, 0, 16, 31, 504, 0, 70], ["+", 0, 57, 15, 15, 0, 16, 31, 504, 0, 73]]
3
249
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int member = in.nextInt(); int cAve = in.nextInt(); int[] a = new int[member]; int[] b = new int[cAve]; int[] c = new int[member]; for (int i = 0; i < member; i++) { a[i] = in.nextInt(); c[i] = 0; } for (int i = 0; i < cAve; i++) { b[i] = in.nextInt(); } for (int i = 0; i < cAve; i++) { for (int j = 0; j < member; j++) { if (a[j] <= b[i]) { c[j]++; break; } } } int max = 1; for (int i = 0; i < member; i++) { if (c[max - 1] < c[i + 1]) { max = i + 2; } } System.out.println(max); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int member = in.nextInt(); int cAve = in.nextInt(); int[] a = new int[member]; int[] b = new int[cAve]; int[] c = new int[member]; for (int i = 0; i < member; i++) { a[i] = in.nextInt(); c[i] = 0; } for (int i = 0; i < cAve; i++) { b[i] = in.nextInt(); } for (int i = 0; i < cAve; i++) { for (int j = 0; j < member; j++) { if (a[j] <= b[i]) { c[j]++; break; } } } int max = 1; for (int i = 0; i < member; i++) { if (c[max - 1] < c[i]) { max = i + 1; } } System.out.println(max); } }
[["-", 15, 15, 0, 16, 12, 504, 71, 16, 17, 72], ["-", 15, 15, 0, 16, 12, 504, 71, 16, 12, 499], ["-", 64, 196, 0, 1, 0, 11, 12, 16, 12, 499], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 12, 499]]
3
255
#include <stdio.h> #include <stdlib.h> int main(void) { int vote[1000]; int list[1000]; int i, f; memset(vote, NULL, sizeof(vote)); int a = 0, b = 0, c = 0; scanf("%d %d", &i, &f); for (a = 0; a < i; a++) { scanf("%d", &list[a]); } for (a = 0; a < f; a++) { scanf("%d", &c); for (b = 0; b < i; b++) { if (list[b] <= c) { vote[b]++; break; } } } b = 0, c = 0; for (a = 0; a < i; a++) { if (vote[a] > b) { b = vote[a]; c = a; } } printf("%d\n", c); return 0; }
#include <stdio.h> #include <stdlib.h> int main(void) { int vote[1000]; int list[1000]; int i, f; memset(vote, NULL, sizeof(vote)); int a = 0, b = 0, c = 0; scanf("%d %d", &i, &f); for (a = 0; a < i; a++) { scanf("%d", &list[a]); } for (a = 0; a < f; a++) { scanf("%d", &c); for (b = 0; b < i; b++) { if (list[b] <= c) { vote[b]++; break; } } } b = 0, c = 0; for (a = 0; a < i; a++) { if (vote[a] > b) { b = vote[a]; c = a + 1; } } printf("%d\n", c); return 0; }
[["+", 64, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
0
211
#include <stdio.h> int main() { int i, j, n, m, max = 0; int game[1000], judge[1000], ticket[1000] = {0}; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) scanf("%d", &game[i]); for (i = 0; i < m; i++) scanf("%d", &judge[i]); for (i = 0; i < m; i++) { for (j = 0; j < n; i++) { if (game[j] <= judge[i]) { ticket[j]++; break; } } } for (i = 1; i < n; i++) { if (ticket[max] < ticket[i]) max = i; } printf("%d\n", max + 1); return 0; }
#include <stdio.h> int main() { int i, j, n, m, max = 0; int game[1000], judge[1000], ticket[1000] = {0}; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) scanf("%d", &game[i]); for (i = 0; i < m; i++) scanf("%d", &judge[i]); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { if (game[j] <= judge[i]) { ticket[j]++; break; } } } for (i = 1; i < n; i++) { if (ticket[max] < ticket[i]) max = i; } printf("%d\n", max + 1); return 0; }
[["-", 0, 7, 8, 9, 0, 7, 26, 27, 28, 22], ["+", 0, 7, 8, 9, 0, 7, 26, 27, 28, 22]]
0
204
#include <stdio.h> int main(void) { int n, m, max, cs[1024], b[1024], cnt[1024]; int i, j; scanf("%d %d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d", &cs[i]); } for (i = 1; i <= m; i++) { scanf("%d", &b[i]); } for (i = 1; i <= m; i++) { for (j = 1; b[i] < cs[j]; j++) { } cnt[j]++; } max = cnt[1]; for (i = 1; i <= n; i++) { if (cnt[max] < cnt[i]) { max = i; } } printf("%d\n", max); return (0); }
#include <stdio.h> int main(void) { int n, m, max, cs[1024], b[1024], cnt[1024] = {0}; int i, j; scanf("%d %d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d", &cs[i]); } for (i = 1; i <= m; i++) { scanf("%d", &b[i]); } for (i = 1; i <= m; i++) { for (j = 1; b[i] < cs[j]; j++) { } cnt[j]++; } max = cnt[1]; for (i = 1; i <= n; i++) { if (cnt[max] < cnt[i]) { max = i; } } printf("%d\n", max); return (0); }
[["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 45], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 46]]
0
202
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, j) REP((i), 0, (j)) #define REP(i, j, k) for (int i = (j); (i) < (k); ++i) #define BW(a, x, b) ((a) <= (x) && (x) <= (b)) #define ALL(v) (v).begin(), (v).end() #define LENGTHOF(x) (sizeof(x) / sizeof(*(x))) #define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b) #define SQ(x) ((x) * (x)) #define Mod(x, mod) (((x)+(mod)%(mod)) #define MP make_pair #define PB push_back #define F first #define S second #define INF 1 << 30 #define EPS 1e-10 #define MOD 1000000007 typedef pair<int, int> pi; typedef pair<int, pi> pii; typedef vector<int> vi; typedef queue<int> qi; typedef long long ll; int main() { int N, M, A[1024], c[1024]; scanf("%d%d", &N, &M); memset(c, 0, sizeof(c)); rep(i, N) scanf("%d", A + i); rep(i, M) { int B; scanf("%d", &B); int p = 0; rep(j, N) if (B >= A[j]) { p = j; break; } c[p]++; } int res = 0; rep(i, N) if (c[res] <= c[i]) res = i; printf("%d\n", res); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, j) REP((i), 0, (j)) #define REP(i, j, k) for (int i = (j); (i) < (k); ++i) #define BW(a, x, b) ((a) <= (x) && (x) <= (b)) #define ALL(v) (v).begin(), (v).end() #define LENGTHOF(x) (sizeof(x) / sizeof(*(x))) #define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b) #define SQ(x) ((x) * (x)) #define Mod(x, mod) (((x)+(mod)%(mod)) #define MP make_pair #define PB push_back #define F first #define S second #define INF 1 << 30 #define EPS 1e-10 #define MOD 1000000007 typedef pair<int, int> pi; typedef pair<int, pi> pii; typedef vector<int> vi; typedef queue<int> qi; typedef long long ll; int main() { int N, M, A[1024], c[1024]; scanf("%d%d", &N, &M); memset(c, 0, sizeof(c)); rep(i, N) scanf("%d", A + i); rep(i, M) { int B; scanf("%d", &B); int p = 0; rep(j, N) if (B >= A[j]) { p = j; break; } c[p]++; } // cout << "ok\n"; int res = 0; rep(i, N) if (c[res] <= c[i]) res = i; printf("%d\n", res + 1); return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
309
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int a[1000], b[1000], cnt[1000], mx; int main() { int n, m, ans; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < m; ++i) cin >> b[i]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++i) { if (b[i] >= a[j]) { ++cnt[j]; break; } } } for (int i = 0; i < n; ++i) { if (mx <= cnt[i]) { mx = cnt[i]; ans = i + 1; } } cout << ans << endl; return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int a[1000], b[1000], cnt[1000], mx; int main() { int n, m, ans; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < m; ++i) cin >> b[i]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (b[i] >= a[j]) { ++cnt[j]; break; } } } for (int i = 0; i < n; ++i) { if (mx < cnt[i]) { mx = cnt[i]; ans = i + 1; } } cout << ans << endl; return 0; }
[["-", 0, 7, 8, 9, 0, 7, 26, 27, 28, 22], ["+", 0, 7, 8, 9, 0, 7, 26, 27, 28, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
1
192
#include <algorithm> #include <cstdio> #include <utility> using namespace std; typedef pair<int, int> P; int main() { int n, m; int count[1001] = {}; int a[1001]; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", a + i); } for (int i = 0; i < m; i++) { int b; scanf("%d", &b); for (int j = 0; j < n; j++) { if (a[i] <= b) { ++count[1 + j]; } } } int max_v = 0, ans; for (int i = 1; i <= n; i++) { if (count[i] > max_v) { max_v = count[i]; ans = i; } } printf("%d\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <utility> using namespace std; typedef pair<int, int> P; int main() { int n, m; int count[1001] = {}; int a[1001]; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", a + i); } for (int i = 0; i < m; i++) { int b; scanf("%d", &b); for (int j = 0; j < n; j++) { if (a[j] <= b) { ++count[1 + j]; break; } } } int max_v = 0, ans; for (int i = 1; i <= n; i++) { if (count[i] > max_v) { max_v = count[i]; ans = i; } } printf("%d\n", ans); return 0; }
[["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94]]
1
207
#include <stdio.h> int main() { // initializing phase int N, M; int i, j; int A[1000], B[1000]; int votes[1000]; int max; for (i = 0; i < 1000; i++) votes[i] = 0; // data reading phase scanf("%d%d", &N, &M); for (i = 0; i < N; i++) scanf("%d", &A[i]); for (i = 0; i < M; i++) scanf("%d", &B[i]); // voting phase for (i = 0; i < M; i++) { for (j = 0; j < N; j++) if (A[j] < B[i]) break; votes[j]++; } // searching maximum phase max = 0; for (i = 0; i < N; i++) if (votes[max] < votes[i]) max = i; // writing answer phase printf("%d\n", max + 1); return 0; }
#include <stdio.h> int main() { // initializing phase int N, M; int i, j; int A[1000], B[1000]; int votes[1000]; int max; for (i = 0; i < 1000; i++) votes[i] = 0; // data reading phase scanf("%d%d", &N, &M); for (i = 0; i < N; i++) scanf("%d", &A[i]); for (i = 0; i < M; i++) scanf("%d", &B[i]); // voting phase for (i = 0; i < M; i++) { for (j = 0; j < N; j++) if (A[j] <= B[i]) break; votes[j]++; } // searching maximum phase max = 0; for (i = 0; i < N; i++) if (votes[max] < votes[i]) max = i; // writing answer phase printf("%d\n", max + 1); return 0; }
[["-", 0, 7, 8, 57, 15, 339, 51, 16, 17, 18], ["+", 0, 7, 8, 57, 15, 339, 51, 16, 17, 19]]
1
224
#include <iostream> #define MAX 1000 using namespace std; class Contest { public: Contest() {} Contest(int _id, int _cost, int _vote) { id = _id; cost = _cost; vote = _vote; } bool compare_cost(int _cost) { return cost < _cost; } bool compare_vote(int _vote) { return vote < _vote; } void output_id() { cout << id << endl; } void voting(int _vote) { vote += _vote; } int getVote() { return vote; } private: int id, cost, vote; }; int main() { int n, m; cin >> n >> m; Contest *contests = new Contest[MAX]; int cost; for (int i = 0; i < n; i += 1) { cin >> cost; contests[i] = Contest(i + 1, cost, 0); } for (int i = 0; i < m; i += 1) { cin >> cost; for (int j = 0; j < n; j += 1) { if (contests[j].compare_cost(cost)) { contests[j].voting(1); break; } } } Contest contest = contests[0]; for (int i = 1; i < n; i += 1) { if (contest.compare_vote(contests[i].getVote())) { contest = contests[i]; } } contest.output_id(); delete[] contests; return 0; }
#include <iostream> #define MAX 1000 using namespace std; class Contest { public: Contest() {} Contest(int _id, int _cost, int _vote) { id = _id; cost = _cost; vote = _vote; } bool compare_cost(int _cost) { return cost <= _cost; } bool compare_vote(int _vote) { return vote < _vote; } void output_id() { cout << id << endl; } void voting(int _vote) { vote += _vote; } int getVote() { return vote; } private: int id, cost, vote; }; int main() { int n, m; cin >> n >> m; Contest *contests = new Contest[MAX]; int cost; for (int i = 0; i < n; i += 1) { cin >> cost; contests[i] = Contest(i + 1, cost, 0); } for (int i = 0; i < m; i += 1) { cin >> cost; for (int j = 0; j < n; j += 1) { if (contests[j].compare_cost(cost)) { contests[j].voting(1); break; } } } Contest contest = contests[0]; for (int i = 1; i < n; i += 1) { if (contest.compare_vote(contests[i].getVote())) { contest = contests[i]; } } contest.output_id(); delete[] contests; return 0; }
[["-", 0, 14, 8, 9, 0, 37, 0, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 17, 19]]
1
309
#include <iostream> #include <vector> using namespace std; int main() { int n, m; int in; vector<int> a, b, sum; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> in; a.push_back(in); sum.push_back(0); } for (int i = 0; i < m; i++) { cin >> in; b.push_back(in); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (a[j] < b[i]) { sum[j]++; break; } } } int max = 0, num; for (int i = 0; i < n; i++) { if (max < sum[i]) { max = sum[i]; num = i; } } cout << num + 1 << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int n, m; int in; vector<int> a, b, sum; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> in; a.push_back(in); sum.push_back(0); } for (int i = 0; i < m; i++) { cin >> in; b.push_back(in); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (a[j] <= b[i]) { sum[j]++; break; } } } int max = 0, num; for (int i = 0; i < n; i++) { if (max < sum[i]) { max = sum[i]; num = i; } } cout << num + 1 << endl; return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
209
#include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <vector> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define debug(x) cout << "debug " << x << endl; using namespace std; int main() { int n, m; while (cin >> n >> m) { int cost[1005], vote[1005], ma = 0, ans; rep(i, n) { cin >> cost[i]; } int inp; rep(i, m) { cin >> inp; rep(j, n) { if (cost[j] <= inp) { vote[j]++; if (ma < vote[j]) { ma = vote[j]; ans = j + 1; } break; } } } cout << ans << endl; } }
#include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <vector> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define debug(x) cout << "debug " << x << endl; using namespace std; int main() { int n, m; while (cin >> n >> m) { int cost[1005], vote[1005] = {0}, ma = 0, ans; rep(i, n) { cin >> cost[i]; } int inp; rep(i, m) { cin >> inp; rep(j, n) { if (cost[j] <= inp) { vote[j]++; if (ma < vote[j]) { ma = vote[j]; ans = j + 1; } break; } } } cout << ans << endl; } }
[["+", 0, 52, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 45], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 46]]
1
173
#include <iostream> using namespace std; int main(void) { int act, ctt; int activi[1000] = {0}; int judge[1000] = {0}; int res[1000] = {0}; int buff, idxbf = 1; cin >> act >> ctt; for (int i = 0; i < act; i++) cin >> activi[i]; for (int i = 0; i < ctt; i++) cin >> judge[i]; for (int i = 0; i < ctt; i++) { for (int j = 0; j < act; j++) { if (activi[j] <= judge[i]) { res[j]++; break; } } } buff = res[0]; for (int i = 0; i < act; i++) { if (buff < res[i]) { buff = res[i]; idxbf = i; } } cout << idxbf + 1 << endl; return 0; }
#include <iostream> using namespace std; int main(void) { int act, ctt; int activi[1000] = {0}; int judge[1000] = {0}; int res[1000] = {0}; int buff, idxbf = 0; cin >> act >> ctt; for (int i = 0; i < act; i++) cin >> activi[i]; for (int i = 0; i < ctt; i++) cin >> judge[i]; for (int i = 0; i < ctt; i++) { for (int j = 0; j < act; j++) { if (activi[j] <= judge[i]) { res[j]++; break; } } } buff = res[0]; for (int i = 0; i < act; i++) { if (buff < res[i]) { buff = res[i]; idxbf = i; } } cout << idxbf + 1 << endl; return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
213
#include <iostream> using namespace std; int main(void) { int act, ctt; int activi[1000] = {0}; int res[1000] = {0}; int buff, idxbf = 1; cin >> act >> ctt; for (int i = 0; i < act; i++) cin >> activi[i]; for (int i = 0; i < ctt; i++) { cin >> buff; for (int j = 0; j < act; j++) { if (activi[j] <= buff) { res[j]++; break; } } } buff = res[0]; for (int i = 0; i < act; i++) { if (buff < res[i]) { buff = res[i]; idxbf = i; } } cout << idxbf + 1 << endl; return 0; }
#include <iostream> using namespace std; int main(void) { int act, ctt; int activi[1000] = {0}; int res[1000] = {0}; int buff, idxbf = 0; cin >> act >> ctt; for (int i = 0; i < act; i++) cin >> activi[i]; for (int i = 0; i < ctt; i++) { cin >> buff; for (int j = 0; j < act; j++) { if (activi[j] <= buff) { res[j]++; break; } } } buff = res[0]; for (int i = 0; i < act; i++) { if (buff < res[i]) { buff = res[i]; idxbf = i; } } cout << idxbf + 1 << endl; return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
183
#include <algorithm> #include <cstring> #include <iostream> using namespace std; #define MAX_N 1050 #define MAX_M 1050 int N, M, A[MAX_N + 1], B[MAX_M + 1], V[MAX_N + 1]; int main() { memset(V, 0, sizeof(V)); cin >> N >> M; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= M; i++) cin >> B[i]; for (int i = 1; i <= M; i++) { for (int j = 1; i <= N; j++) { if (A[j] < B[i]) { V[j]++; break; } } } cout << distance(V + 1, max_element(V, V + N)) << endl; return 0; }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; #define MAX_N 1050 #define MAX_M 1050 int N, M, A[MAX_N + 1], B[MAX_M + 1], V[MAX_N + 1]; int main() { memset(V, 0, sizeof(V)); cin >> N >> M; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= M; i++) cin >> B[i]; for (int i = 1; i <= M; i++) { for (int j = 1; i <= N; j++) { if (A[j] <= B[i]) { V[j]++; break; } } } cout << distance(V, max_element(V, V + N)) << endl; return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["-", 31, 16, 12, 2, 3, 4, 0, 16, 17, 72], ["-", 31, 16, 12, 2, 3, 4, 0, 16, 12, 13]]
1
185
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) long long dp[101][21]; int main() { int n; cin >> n; rep(i, 101) rep(j, 21) dp[i][j] = 0; int hand[100]; rep(i, n) { cin >> hand[i]; } dp[0][hand[0]] = 1; reps(i, 1, n - 1) { rep(j, 20 + 1) { int pls = j + hand[i]; int mns = j - hand[i]; if (pls <= 20) dp[i][pls] += dp[i - 1][j]; if (mns >= 0) dp[i][mns] += dp[i - 1][j]; } } printf("%d\n", dp[n - 2][hand[n - 1]]); }
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) long long dp[101][21]; int main() { int n; cin >> n; rep(i, 101) rep(j, 21) dp[i][j] = 0; int hand[100]; rep(i, n) { cin >> hand[i]; } dp[0][hand[0]] = 1; reps(i, 1, n - 1) { rep(j, 20 + 1) { int pls = j + hand[i]; int mns = j - hand[i]; if (pls <= 20) dp[i][pls] += dp[i - 1][j]; if (mns >= 0) dp[i][mns] += dp[i - 1][j]; } } printf("%lld\n", dp[n - 2][hand[n - 1]]); }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
231
#include <stdio.h> long long dp[21][100]; int main() { int m, n; int i, j; scanf("%d", &n); for (i = 0; i < 21; i++) for (j = 0; j < n; j++) dp[i][j] = 0; for (i = 0; i < n - 1; i++) { scanf("%d", &m); if (i == 0) { dp[m][1] = 1; continue; } for (j = 0; j <= 20; j++) { if (dp[j][i] != 0) { if (j + m <= 20) { dp[j + m][i + 1] += dp[j][i]; } if (j - m >= 0) { dp[j - m][i + 1] += dp[j][i]; } } } } scanf("%d", &m); printf("%I64d", dp[m][n - 1]); return 0; }
#include <stdio.h> long long dp[21][100]; int main() { int m, n; int i, j; scanf("%d", &n); for (i = 0; i < 21; i++) for (j = 0; j < n; j++) dp[i][j] = 0; for (i = 0; i < n - 1; i++) { scanf("%d", &m); if (i == 0) { dp[m][1] = 1; continue; } for (j = 0; j <= 20; j++) { if (dp[j][i] != 0) { if (j + m <= 20) { dp[j + m][i + 1] += dp[j][i]; } if (j - m >= 0) { dp[j - m][i + 1] += dp[j][i]; } } } } scanf("%d", &m); printf("%lld\n", dp[m][n - 1]); return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
240
#include <iostream> using namespace std; int dp[101][21]; int main(void) { int n; cin >> n; int num[101]; for (int i = 0; i < n; i++) { cin >> num[i]; } dp[0][num[0]] = 1; for (int j = 1; j < n; j++) { for (int i = 0; i < 21; i++) { if (i + num[j] <= 20) dp[j][i + num[j]] += dp[j][i]; if (i - num[j] >= 0) dp[j][i - num[j]] += dp[j][i]; } } cout << dp[n - 2][num[n - 1]] << endl; return 0; }
#include <iostream> using namespace std; long long dp[101][21]; int main(void) { int n; cin >> n; int num[101]; for (int i = 0; i < n; i++) { cin >> num[i]; } dp[0][num[0]] = 1; for (int j = 1; j < n; j++) { for (int i = 0; i < 21; i++) { if (i + num[j] <= 20) dp[j][i + num[j]] += dp[j - 1][i]; if (i - num[j] >= 0) dp[j][i - num[j]] += dp[j - 1][i]; } } cout << dp[n - 2][num[n - 1]] << endl; return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 0, 30, 0, 43, 39, 86, 0, 96], ["+", 12, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 12, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
189
#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; static const double EPS = 1e-8; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; int main() { int n; while (~scanf("%d", &n)) { ll dp[101][21]; memset(dp, 0, sizeof(dp)); int first; scanf("%d", &first); dp[0][first] = 1; for (int i = 0; i < n - 2; i++) { int num; scanf("%d", &num); for (int from = 0; from <= 20; from++) { if (dp[i][from] > 0) { if (from + num <= 20) { dp[i + 1][from + num] += dp[i][from]; } if (from - num >= 0) { dp[i + 1][from - num] += dp[i][from]; } } } } int target_num; scanf("%d", &target_num); printf("%lld\n", dp[n - 1][target_num]); } }
#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; static const double EPS = 1e-8; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; int main() { int n; while (~scanf("%d", &n)) { ll dp[101][21]; memset(dp, 0, sizeof(dp)); int first; scanf("%d", &first); dp[0][first] = 1; for (int i = 0; i < n - 2; i++) { int num; scanf("%d", &num); for (int from = 0; from <= 20; from++) { if (dp[i][from] > 0) { if (from + num <= 20) { dp[i + 1][from + num] += dp[i][from]; } if (from - num >= 0) { dp[i + 1][from - num] += dp[i][from]; } } } } int target_num; scanf("%d", &target_num); printf("%lld\n", dp[n - 2][target_num]); } }
[["-", 0, 69, 28, 69, 341, 342, 0, 16, 12, 13], ["+", 0, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
329
#include <cstdio> #include <vector> using namespace std; int main() { int i; int n; int target; long long unsigned int num[21]; long long unsigned int tmp[21]; vector<int> l; scanf("%d", &n); for (i = 0; i < 21; i++) { num[i] = 0; tmp[i] = 0; } num[0] = 1; l.push_back(0); for (i = 0; i < n - 1; i++) { int j; int d; scanf("%d", &d); vector<int>::iterator it = l.begin(); while (it != l.end()) { if ((*it) + d >= 0 && (*it) + d < 21) { tmp[(*it) + d] += num[*it]; } if (d == 0) break; if ((*it) - d >= 0 && (*it) - d < 21) { tmp[(*it) - d] += num[*it]; } it++; } l.clear(); for (j = 0; j < 21; j++) { if (tmp[j]) l.push_back(j); num[j] = tmp[j]; tmp[j] = 0; } } scanf("%d", &target); printf("%llu\n", num[target]); }
#include <cstdio> #include <vector> using namespace std; int main() { int i; int n; int target; long long unsigned int num[21]; long long unsigned int tmp[21]; vector<int> l; scanf("%d", &n); for (i = 0; i < 21; i++) { num[i] = 0; tmp[i] = 0; } num[0] = 1; l.push_back(0); for (i = 0; i < n - 1; i++) { int j; int d; scanf("%d", &d); vector<int>::iterator it = l.begin(); while (it != l.end()) { if ((*it) + d >= 0 && (*it) + d < 21) { tmp[(*it) + d] += num[*it]; } if (i == 0 && d == 0) break; if ((*it) - d >= 0 && (*it) - d < 21) { tmp[(*it) - d] += num[*it]; } it++; } l.clear(); for (j = 0; j < 21; j++) { if (tmp[j]) l.push_back(j); num[j] = tmp[j]; tmp[j] = 0; } } scanf("%d", &target); printf("%llu\n", num[target]); }
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 60], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98]]
1
321
#include <iostream> #include <string.h> const int SIZE = 20 + 1; int main() { int n, i, ii, m, r; int gp[SIZE], gp_[SIZE], *gpp, *gpp_, *tmpp; memset(gp, 0, sizeof(gp)); gpp = gp; gpp_ = gp_; std::cin >> n; std::cin >> m; gpp[m] = 1; for (i = 2; i < n; i++) { memset(gpp_, 0, sizeof(gp_)); std::cin >> m; for (ii = 0; ii < SIZE; ii++) { if (r = gpp[ii]) { if (ii + m < SIZE) gpp_[ii + m] += r; if (ii - m >= 0) gpp_[ii - m] += r; } } tmpp = gpp; gpp = gpp_; gpp_ = tmpp; } std::cin >> m; std::cout << gpp[m] << '\n'; return 0; }
#include <iostream> #include <string.h> const int SIZE = 20 + 1; int main() { long int n, i, ii, m, r; long int gp[SIZE], gp_[SIZE], *gpp, *gpp_, *tmpp; memset(gp, 0, sizeof(gp)); gpp = gp; gpp_ = gp_; std::cin >> n; std::cin >> m; gpp[m] = 1; for (i = 2; i < n; i++) { memset(gpp_, 0, sizeof(gp_)); std::cin >> m; for (ii = 0; ii < SIZE; ii++) { if (r = gpp[ii]) { if (ii + m < SIZE) gpp_[ii + m] += r; if (ii - m >= 0) gpp_[ii - m] += r; } } tmpp = gpp; gpp = gpp_; gpp_ = tmpp; } std::cin >> m; std::cout << gpp[m] << '\n'; return 0; }
[["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96]]
1
215
#include <iostream> #include <vector> int main() { const int max = 20, min = 0; int N; std::cin >> N; std::vector<std::vector<u_int>> dp(N - 1, std::vector<u_int>(max + 1, 0)); std::vector<int> num(N); for (int i = 0; i < N; ++i) { std::cin >> num[i]; } //1項目を反映 ++dp[0][num[0]]; for (int i = 0; i < N - 2; ++i) { for (int j = 0; j < max + 1; ++j) { //次項を足した場合 if (j + num[i + 1] <= max) { dp[i + 1][j + num[i + 1]] += dp[i][j]; } //次項を引いた場合 if (j - num[i + 1] >= min) { dp[i + 1][j - num[i + 1]] += dp[i][j]; } } } std::cout << dp[N - 2][num[N - 1]] << std::endl; return 0; }
#include <iostream> #include <vector> int main() { const int max = 20, min = 0; int N; std::cin >> N; std::vector<std::vector<unsigned long int>> dp( N - 1, std::vector<unsigned long int>(max + 1, 0)); std::vector<int> num(N); for (int i = 0; i < N; ++i) { std::cin >> num[i]; } //1項目を反映 ++dp[0][num[0]]; for (int i = 0; i < N - 2; ++i) { for (int j = 0; j < max + 1; ++j) { //次項を足した場合 if (j + num[i + 1] <= max) { dp[i + 1][j + num[i + 1]] += dp[i][j]; } //次項を引いた場合 if (j - num[i + 1] >= min) { dp[i + 1][j - num[i + 1]] += dp[i][j]; } } } std::cout << dp[N - 2][num[N - 1]] << std::endl; return 0; }
[["-", 39, 343, 141, 344, 3, 347, 0, 77, 39, 78], ["+", 141, 344, 3, 347, 0, 77, 39, 86, 0, 87], ["+", 141, 344, 3, 347, 0, 77, 39, 86, 0, 96], ["+", 141, 344, 3, 347, 0, 77, 39, 86, 39, 40], ["-", 63, 343, 141, 346, 3, 347, 0, 77, 39, 78], ["+", 141, 346, 3, 347, 0, 77, 39, 86, 0, 87], ["+", 141, 346, 3, 347, 0, 77, 39, 86, 0, 96], ["+", 141, 346, 3, 347, 0, 77, 39, 86, 39, 40]]
1
254
#dp[i][j]...i番で作れるjの通り数 #出力はdp[n][m] n = int(input()) A = list(map(int,input().split())) m = A.pop() maxN = sum(A) dp = [[0 for i in range(maxN + 1)] for _ in A] for i in range(len(A)): for j in range(maxN + 1): if i == 0: dp[0][A[i]] = 1 else: if j - A[i] >= 0: dp[i][j] += dp[i - 1][j - A[i]] if j + A[i] <= maxN: dp[i][j] += dp[i - 1][j + A[i]] print(dp[len(A) - 1][m])
#dp[i][j]...i番で作れるjの通り数 #出力はdp[n][m] n = int(input()) A = list(map(int,input().split())) m = A.pop() maxN = min(sum(A), 20) dp = [[0 for i in range(maxN + 1)] for _ in A] for i in range(len(A)): for j in range(maxN + 1): if i == 0: dp[0][A[i]] = 1 else: if j - A[i] >= 0: dp[i][j] += dp[i - 1][j - A[i]] if j + A[i] <= maxN: dp[i][j] += dp[i - 1][j + A[i]] print(dp[len(A) - 1][m])
[["+", 0, 656, 0, 1, 0, 662, 12, 652, 63, 22], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 24], ["+", 12, 652, 3, 4, 0, 652, 3, 4, 0, 25], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 21], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 612]]
5
179
#include <algorithm> #include <cstring> #include <iostream> #include <queue> using namespace std; #define MAX 1005 char JOI[MAX][MAX]; int h, w, n; int dx[] = {0, -1, 0, 1}; int dy[] = {1, 0, -1, 0}; pair<int, int> goal[10]; bool check(int a, int b) { return (a >= 1 && a <= h && b >= 1 && b <= w); } int step[MAX][MAX]; int bfs(const pair<int, int> &st, const pair<int, int> &en) { queue<pair<int, int>> q; q.push(st); memset(step, -1, sizeof(step)); step[st.first][st.second] = 0; int stp = 0; while (!q.empty()) { pair<int, int> S = q.front(); q.pop(); int x = S.first; int y = S.second; stp = step[x][y]; for (int i = 0; i < 4; i++) { int cx = x + dx[i]; int cy = y + dy[i]; if (check(cx, cy)) { if (JOI[cx][cy] != 'X' && step[cx][cy] == -1) { if (cx == en.first && cy == en.second) return stp + 1; q.push(make_pair(cx, cy)); step[cx][cy] = stp + 1; } } } } return stp; } int main() { cin >> h >> w >> n; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> JOI[i][j]; if (JOI[i][j] == 'S') goal[0] = make_pair(i, j); else if (JOI[i][j] >= '0' && JOI[i][j] <= '9') { int x = JOI[i][j] = '0'; goal[x] = make_pair(i, j); } } } int ans = 0; for (int i = 0; i < n; i++) { ans += bfs(goal[i], goal[i + 1]); } cout << ans << endl; }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> using namespace std; #define MAX 1005 char JOI[MAX][MAX]; int h, w, n; int dx[] = {0, -1, 0, 1}; int dy[] = {1, 0, -1, 0}; pair<int, int> goal[10]; bool check(int a, int b) { return (a >= 1 && a <= h && b >= 1 && b <= w); } int step[MAX][MAX]; int bfs(const pair<int, int> &st, const pair<int, int> &en) { queue<pair<int, int>> q; q.push(st); memset(step, -1, sizeof(step)); step[st.first][st.second] = 0; int stp = 0; while (!q.empty()) { pair<int, int> S = q.front(); q.pop(); int x = S.first; int y = S.second; stp = step[x][y]; for (int i = 0; i < 4; i++) { int cx = x + dx[i]; int cy = y + dy[i]; if (check(cx, cy)) { if (JOI[cx][cy] != 'X' && step[cx][cy] == -1) { if (cx == en.first && cy == en.second) return stp + 1; q.push(make_pair(cx, cy)); step[cx][cy] = stp + 1; } } } } } int main() { cin >> h >> w >> n; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> JOI[i][j]; if (JOI[i][j] == 'S') goal[0] = make_pair(i, j); else if (JOI[i][j] >= '0' && JOI[i][j] <= '9') { int x = JOI[i][j] - '0'; goal[x] = make_pair(i, j); } } } int ans = 0; for (int i = 0; i < n; i++) { ans += bfs(goal[i], goal[i + 1]); } cout << ans << endl; }
[["-", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["-", 0, 30, 0, 14, 8, 9, 0, 37, 0, 22], ["-", 0, 30, 0, 14, 8, 9, 0, 37, 0, 35], ["-", 64, 9, 0, 43, 49, 50, 51, 11, 17, 32], ["+", 64, 9, 0, 43, 49, 50, 51, 16, 17, 33]]
1
541