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
import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * Problem 05: Split Up! */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; while ((line = br.readLine()) != null && !line.isEmpty()) { int n = parseInt(line); if (n == 0) break; int[] a = new int[n]; int sum = 0; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { sum += a[i] = parseInt(st.nextToken()); } int min = MAX_VALUE; for (int i = 0; i < 1 << n; i++) { int A = 0; for (int j = 0; j < n; j++) { if ((i & 1 << j) != 1) A += a[j]; } min = Math.min(min, Math.abs(sum - A - A)); } System.out.println(min); } } }
import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * Problem 05: Split Up! */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; while ((line = br.readLine()) != null && !line.isEmpty()) { int n = parseInt(line); if (n == 0) break; int[] a = new int[n]; int sum = 0; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { sum += a[i] = parseInt(st.nextToken()); } int min = MAX_VALUE; for (int i = 0; i < 1 << n; i++) { int A = 0; for (int j = 0; j < n; j++) { if ((i & 1 << j) != 0) A += a[j]; } min = Math.min(min, Math.abs(sum - A - A)); } System.out.println(min); } } }
[["-", 8, 196, 0, 57, 15, 15, 0, 16, 12, 499], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 12, 499]]
3
283
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; int[] a = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); sum += a[i]; } Arrays.sort(a); boolean[] f = new boolean[sum + 1]; f[0] = true; int max = 0; for (int i = 0; i < n; i++) { for (int j = max; j >= 0; j--) { if (f[j]) f[j + a[i]] = true; } max += a[i]; } for (int i = 0;; i++) { if (f[sum / 2 + i] || f[sum / 2 - i]) { System.out.println(i + sum % 2); break; } } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; int[] a = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); sum += a[i]; } Arrays.sort(a); boolean[] f = new boolean[sum + 1]; f[0] = true; int max = 0; for (int i = 0; i < n; i++) { for (int j = max; j >= 0; j--) { if (f[j]) f[j + a[i]] = true; } max += a[i]; } for (int i = 0;; i++) { if (f[sum / 2 + i] || f[sum / 2 - i]) { System.out.println(i * 2 + sum % 2); break; } } } } }
[["+", 0, 492, 3, 4, 0, 16, 31, 16, 17, 48], ["+", 0, 492, 3, 4, 0, 16, 31, 16, 12, 499]]
3
248
#include <iostream> #include <queue> #define abs(a) ((a) > 0 ? (a) : -(a)) using namespace std; long diffmin, n, p[20]; void dfs(int layer, long a, long b) { if (layer < n) { dfs(layer + 1, a + p[layer], b); dfs(layer + 1, a, b + p[layer]); } else { if (diffmin > abs(a - b)) { diffmin = abs(a - b); } return; } } int main() { long all = 0, party0 = 0, party1 = 0; cin >> n; while (n) { for (int i = 0; i < n; i++) { long input; cin >> input; p[i] = input; all += input; } diffmin = all; dfs(0, 0, 0); cout << diffmin; cin >> n; party0 = party1 = 0; all = 0; } return 0; }
#include <iostream> #include <queue> #define abs(a) ((a) > 0 ? (a) : -(a)) using namespace std; long diffmin, n, p[20]; void dfs(int layer, long a, long b) { if (layer < n) { dfs(layer + 1, a + p[layer], b); dfs(layer + 1, a, b + p[layer]); } else { if (diffmin > abs(a - b)) { diffmin = abs(a - b); } return; } } int main() { long all = 0, party0 = 0, party1 = 0; cin >> n; while (n) { for (int i = 0; i < n; i++) { long input; cin >> input; p[i] = input; all += input; } diffmin = all; dfs(0, 0, 0); cout << diffmin << endl; cin >> n; party0 = party1 = 0; all = 0; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
202
#include <algorithm> #include <iostream> using namespace std; int RegNum; //登録者数 int RegATK[20]; //登録者戦闘力 int RegATKAll; //登録者戦闘力合計 int PartyATK[2]; //パーティー戦闘力 int GetMinATK(int n, int PAATK) { if (n == 0) return abs(RegATKAll - (2 * PAATK)); return min(GetMinATK(n - 1, PAATK + RegATK[n]), GetMinATK(n - 1, PAATK)); } int main() { while (1) { //変数初期化 PartyATK[0] = 0; PartyATK[1] = 0; RegATKAll = 0; cin >> RegNum; //登録者数代入 if (RegNum == 0) break; // 0ならbreak for (int i = 0; i < RegNum; ++i) { //戦闘力代入 cin >> RegATK[i]; RegATKAll += RegATK[i]; } cout << GetMinATK(RegNum, 0) << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int RegNum; //登録者数 int RegATK[20]; //登録者戦闘力 int RegATKAll; //登録者戦闘力合計 int PartyATK[2]; //パーティー戦闘力 int GetMinATK(int n, int PAATK) { if (n == 0) return abs(RegATKAll - (2 * PAATK)); return min(GetMinATK(n - 1, PAATK + RegATK[n]), GetMinATK(n - 1, PAATK)); } int main() { while (1) { //変数初期化 PartyATK[0] = 0; PartyATK[1] = 0; RegATKAll = 0; cin >> RegNum; //登録者数代入 if (RegNum == 0) break; // 0ならbreak for (int i = 0; i < RegNum; ++i) { //戦闘力代入 cin >> RegATK[i]; RegATKAll += RegATK[i]; } cout << GetMinATK(RegNum - 1, 0) << endl; } return 0; }
[["+", 31, 16, 12, 2, 3, 4, 0, 16, 17, 33], ["+", 31, 16, 12, 2, 3, 4, 0, 16, 12, 13]]
1
176
#include <cstdio> #include <iostream> #include <vector> using namespace std; int n, u[21]; vector<int> a, b; int minsa = 100000; void kakunin() { cout << "ateam" << endl; for (int i = 0; i < a.size(); i++) cout << a[i] << " "; cout << endl << "bteam" << endl; for (int i = 0; i < b.size(); i++) cout << b[i] << " "; cout << endl << "----------------------" << endl; ; } void sa() { // kakunin(); int sum = 0; for (int i = 0; i < a.size(); i++) sum += a[i]; for (int i = 0; i < b.size(); i++) sum -= b[i]; if (sum < 0) sum *= -1; if (minsa > sum) minsa = sum; } void check(int x) { // kakunin(); int flag = 0; if (x == n) { sa(); flag = 1; } else if (flag == 0) { a.push_back(u[x]); check(x + 1); a.pop_back(); b.push_back(u[x]); check(x + 1); b.pop_back(); } } int main() { while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) cin >> u[i]; check(0); a.clear(); b.clear(); cout << minsa << endl; } return 0; }
#include <cstdio> #include <iostream> #include <vector> using namespace std; int n, u[21]; vector<int> a, b; int minsa = 100000; void kakunin() { cout << "ateam" << endl; for (int i = 0; i < a.size(); i++) cout << a[i] << " "; cout << endl << "bteam" << endl; for (int i = 0; i < b.size(); i++) cout << b[i] << " "; cout << endl << "----------------------" << endl; ; } void sa() { // kakunin(); int sum = 0; for (int i = 0; i < a.size(); i++) sum += a[i]; for (int i = 0; i < b.size(); i++) sum -= b[i]; if (sum < 0) sum *= -1; if (minsa > sum) minsa = sum; } void check(int x) { // kakunin(); int flag = 0; if (x == n) { sa(); flag = 1; } else if (flag == 0) { a.push_back(u[x]); check(x + 1); a.pop_back(); b.push_back(u[x]); check(x + 1); b.pop_back(); } } int main() { while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) cin >> u[i]; minsa = 1000000; check(0); a.clear(); b.clear(); cout << minsa << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
362
#include <bits/stdc++.h> using namespace std; typedef set<int> S; int main() { int n; while (cin >> n, n) { int a[20], sum = 0; for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; S mp[21]; S::iterator it; mp[0].insert(0); for (int i = 0; i < n; i++) for (it = mp[i].begin(); it != mp[i].end(); it++) mp[i + 1].insert((*it) + a[i]), mp[i + 1].insert(*it); int ans = 1e9; for (it = mp[n].begin(); it != mp[n].end(); it++) ans = min(ans, sum - (*it)); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef set<int> S; int main() { int n; while (cin >> n, n) { int a[20], sum = 0; for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; S mp[21]; S::iterator it; mp[0].insert(0); for (int i = 0; i < n; i++) for (it = mp[i].begin(); it != mp[i].end(); it++) mp[i + 1].insert((*it) + a[i]), mp[i + 1].insert(*it); int ans = 1e9; for (it = mp[n].begin(); it != mp[n].end(); it++) ans = min(ans, abs(sum - 2 * (*it))); cout << ans << endl; } return 0; }
[["+", 0, 11, 12, 2, 3, 4, 0, 2, 63, 22], ["+", 12, 2, 3, 4, 0, 2, 3, 4, 0, 24], ["+", 0, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["+", 0, 2, 3, 4, 0, 16, 12, 16, 17, 48], ["+", 3, 4, 0, 16, 12, 16, 12, 23, 0, 25]]
1
220
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, sum; vector<int> a; while (true) { cin >> n; if (n == 0) { break; } a = vector<int>(n); for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } int min_ = (1 << 29); for (int i = 0; i < (1 << n); i++) { int sum2 = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum2 += a[j]; } } min_ = min(abs(2 * sum2 - sum), min_); } cout << min_ << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, sum; vector<int> a; while (true) { cin >> n; if (n == 0) { break; } a = vector<int>(n); sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } int min_ = (1 << 29); for (int i = 0; i < (1 << n); i++) { int sum2 = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum2 += a[j]; } } min_ = min(abs(2 * sum2 - sum), min_); } cout << min_ << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
183
#include <algorithm> #include <cmath> #include <iostream> using namespace std; const int INF = 1e+9; int n, ans; int a[21]; void dfs(int diff, int k); int main() { while (cin >> n) { for (int i = 0; i < n; i++) { cin >> a[i]; } ans = INF; dfs(0, 0); cout << ans << endl; } } void dfs(int diff, int k) { if (k == n) { ans = min(ans, abs(diff)); } else { dfs(diff + a[k], k + 1); dfs(diff - a[k], k + 1); } }
#include <algorithm> #include <cmath> #include <iostream> using namespace std; const int INF = 1e+9; int n, ans; int a[21]; void dfs(int diff, int k); int main() { while (cin >> n && n != 0) { for (int i = 0; i < n; i++) { cin >> a[i]; } ans = INF; dfs(0, 0); cout << ans << endl; } } void dfs(int diff, int k) { if (k == n) { ans = min(ans, abs(diff)); } else { dfs(diff + a[k], k + 1); dfs(diff - a[k], k + 1); } }
[["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 98], ["+", 0, 52, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 52, 15, 339, 51, 16, 12, 16, 17, 79], ["+", 0, 52, 15, 339, 51, 16, 12, 16, 12, 13]]
1
153
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int n, a[23]; vector<int> v; void f(int k, int sum) { if (k == n) v.push_back(sum); else { f(k + 1, sum); f(k + 1, sum + a[k]); } return; } int main() { while (cin >> n, n) { v.clear(); int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } f(0, 0); int ans = n; for (int i = 0; i < v.size(); i++) { int v1 = v[i], v2 = sum - v[i]; ans = min(ans, abs(v1 - v2)); } cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int n, a[23]; vector<int> v; void f(int k, int sum) { if (k == n) v.push_back(sum); else { f(k + 1, sum); f(k + 1, sum + a[k]); } return; } int main() { while (cin >> n, n) { v.clear(); int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } f(0, 0); int ans = sum; for (size_t i = 0; i < v.size(); i++) { int v1 = v[i], v2 = sum - v[i]; ans = min(ans, abs(v1 - v2)); } cout << ans << endl; } }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 22], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 22], ["-", 0, 52, 8, 9, 0, 7, 10, 43, 39, 40], ["+", 0, 52, 8, 9, 0, 7, 10, 43, 39, 40]]
1
204
#include <iostream> #define INF (1 << 21) using namespace std; int n, t[20], mi, a[20]; void rec(int p) { int a1, a2; if (p == n) { a1 = 0; a2 = 0; for (int i = 0; i < n; i++) { if (t[i] == 1) a1 += a[i]; else a2 += a[i]; } if (a1 - a2 > 0 && a1 - a2 < mi) mi = a1 - a2; else if (a2 - a1 > 0 && a2 - a1 < mi) mi = a2 - a1; return; } for (int k = 1; k <= 2; k++) { t[p] = k; rec(p + 1); } } int main() { int i, j; while (1) { cin >> n; if (n == 0) break; for (i = 0; i < n; i++) { cin >> a[i]; t[i] = 0; } mi = INF; rec(0); cout << mi << endl; } return 0; }
#include <iostream> #define INF (1 << 21) using namespace std; int n, t[20], mi, a[20]; void rec(int p) { int a1, a2; if (p == n) { a1 = 0; a2 = 0; for (int i = 0; i < n; i++) { if (t[i] == 1) a1 += a[i]; else a2 += a[i]; } if (a1 - a2 >= 0 && a1 - a2 < mi) mi = a1 - a2; else if (a2 - a1 > 0 && a2 - a1 < mi) mi = a2 - a1; return; } for (int k = 1; k <= 2; k++) { t[p] = k; rec(p + 1); } } int main() { int i, j; while (1) { cin >> n; if (n == 0) break; for (i = 0; i < n; i++) { cin >> a[i]; t[i] = 0; } mi = INF; rec(0); cout << mi << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 20]]
1
242
#include <cstring> #include <iostream> using namespace std; bool table[1000001]; int main() { while (true) { int n, a, sum = 0; cin >> n; if (n == 0) { break; } memset(table, 0, sizeof(bool) * 1000001); table[0] = true; while (n--) { cin >> a; for (int i = sum; i >= 0; --i) { if (table[i]) { table[i + a] = true; } } sum += a; } int answer = sum / 2; for (; answer >= 9; --answer) { if (table[answer]) { break; } } cout << (sum - answer * 2) << endl; } return 0; }
#include <cstring> #include <iostream> using namespace std; bool table[1000001]; int main() { while (true) { int n, a, sum = 0; cin >> n; if (n == 0) { break; } memset(table, 0, sizeof(bool) * 1000001); table[0] = true; while (n--) { cin >> a; for (int i = sum; i >= 0; --i) { if (table[i]) { table[i + a] = true; } } sum += a; } int answer = sum / 2; for (; answer >= 0; --answer) { if (table[answer]) { break; } } cout << (sum - answer * 2) << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
164
def solve(a,i,sa,sb): if 2*sa >= all: return 2*sa-all if 2*sb >= all: return 2*sb-all return min(solve(a,i+1,sa+a[i],sb),solve(a,i+1,sa,sb+a[i])) while True: n = eval(input()) if n == 0: break a = sorted(map(int,input().split())) all = sum(a) print(solve(a,0,a[0],0))
def solve(a,i,sa,sb): if 2*sa >= all: return 2*sa-all if 2*sb >= all: return 2*sb-all return min(solve(a,i+1,sa+a[i],sb),solve(a,i+1,sa,sb+a[i])) while True: n = eval(input()) if n == 0: break a = sorted(map(int,input().split())) all = sum(a) print(solve(a,1,a[0],0))
[["-", 0, 652, 3, 4, 0, 652, 3, 4, 0, 612], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 612]]
5
133
def solve(a,i,sa,sb): if 2*sa >= all: return 2*sa-all if 2*sb >= all: return 2*sb-all return min(solve(a,i+1,sa+a[i],sb),solve(a,i+1,sa,sb+a[i])) while True: n = eval(input()) if n == 0: break a = sorted(map(int,input().split())) all = sum(a) print(solve(a,0,a[0],0))
def solve(a,i,sa,sb): if 2*sa >= all: return 2*sa-all if 2*sb >= all: return 2*sb-all return min(solve(a,i+1,sa+a[i],sb),solve(a,i+1,sa,sb+a[i])) while True: n = eval(input()) if n == 0: break a = sorted(map(int,input().split()))[::-1] all = sum(a) print(solve(a,1,a[0],0))
[["+", 8, 196, 0, 1, 0, 662, 12, 206, 0, 70], ["+", 0, 1, 0, 662, 12, 206, 206, 663, 0, 102], ["+", 0, 662, 12, 206, 206, 663, 0, 664, 17, 33], ["+", 0, 662, 12, 206, 206, 663, 0, 664, 28, 612], ["+", 8, 196, 0, 1, 0, 662, 12, 206, 0, 73], ["-", 0, 652, 3, 4, 0, 652, 3, 4, 0, 612], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 612]]
5
133
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> using namespace std; typedef long long ll; typedef pair<int, int> P; static const double EPS = 1e-8; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; int H, W; char stage[20][20]; string ghost; bool visited[20][20][20][20][10]; int gTime = 0; int rx, ry; class State { public: int _sx, _sy, _gx, _gy, _time; State(int sx, int sy, int gx, int gy, int time) : _sx(sx), _sy(sy), _gx(gx), _gy(gy), _time(time) {} bool operator<(const State &s) const { return _time < s._time; } bool operator>(const State &s) const { return _time > s._time; } }; void bfs(int sx, int sy, int gx, int gy) { State init(sx, sy, gx, gy, 0); priority_queue<State, vector<State>, greater<State>> que; que.push(init); while (!que.empty()) { State s = que.top(); que.pop(); int pattern_i = s._time % ghost.size(); if (ghost[pattern_i] == '5') { // nothing to do } else if (ghost[pattern_i] == '8' && s._gy - 1 >= 0) { s._gy--; } else if (ghost[pattern_i] == '6' && s._gx + 1 < W) { s._gx++; } else if (ghost[pattern_i] == '4' && s._gx - 1 >= 0) { s._gx--; } else if (ghost[pattern_i] == '2' && s._gy + 1 < H) { s._gy++; } if (!visited[s._sx][s._sy][s._gx][s._gy][pattern_i]) { visited[s._sx][s._sy][s._gx][s._gy][pattern_i] = true; if (s._sx == s._gx && s._sy == s._gy && gTime > s._time) { gTime = s._time; rx = s._gx; ry = s._gy; return; } que.push(State(sx, sy, s._gx, s._gy, s._time + 1)); } for (int i = 0; i < 4; i++) { int dx = tx[i] + s._sx; int dy = ty[i] + s._sy; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; if (visited[dx][dy][s._gx][s._gy][pattern_i]) continue; visited[dx][dy][s._gx][s._gy][pattern_i] = true; if (dx == s._gx && dy == s._gy && gTime > s._time) { gTime = s._time; rx = s._gx; ry = s._gy; return; } que.push(State(dx, dy, s._gx, s._gy, s._time + 1)); } } } int main() { while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; int sx, sy, gx, gy; memset(visited, false, sizeof(visited)); gTime = INF; for (int y = 0; y < H; y++) { char line[32]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (stage[y][x] == 'A') { sx = x; sy = y; } if (stage[y][x] == 'B') { gx = x; gy = y; } } } cin >> ghost; rx = -1; ry = -1; bfs(sx, sy, gx, gy); if (gTime == INF) { printf("Impossible\n"); } else { printf("%d %d %d\n", gTime + 1, ry, rx); } } }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> using namespace std; typedef long long ll; typedef pair<int, int> P; static const double EPS = 1e-8; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; int H, W; char stage[20][20]; string ghost; bool visited[20][20][20][20][10]; int gTime = 0; int rx, ry; class State { public: int _sx, _sy, _gx, _gy, _time; State(int sx, int sy, int gx, int gy, int time) : _sx(sx), _sy(sy), _gx(gx), _gy(gy), _time(time) {} bool operator<(const State &s) const { return _time < s._time; } bool operator>(const State &s) const { return _time > s._time; } }; void bfs(int sx, int sy, int gx, int gy) { State init(sx, sy, gx, gy, 0); priority_queue<State, vector<State>, greater<State>> que; que.push(init); while (!que.empty()) { State s = que.top(); que.pop(); int pattern_i = s._time % ghost.size(); if (ghost[pattern_i] == '5') { // nothing to do } else if (ghost[pattern_i] == '8' && s._gy - 1 >= 0) { s._gy--; } else if (ghost[pattern_i] == '6' && s._gx + 1 < W) { s._gx++; } else if (ghost[pattern_i] == '4' && s._gx - 1 >= 0) { s._gx--; } else if (ghost[pattern_i] == '2' && s._gy + 1 < H) { s._gy++; } if (!visited[s._sx][s._sy][s._gx][s._gy][pattern_i]) { visited[s._sx][s._sy][s._gx][s._gy][pattern_i] = true; if (s._sx == s._gx && s._sy == s._gy && gTime > s._time) { gTime = s._time; rx = s._gx; ry = s._gy; return; } que.push(State(sx, sy, s._gx, s._gy, s._time + 1)); } for (int i = 0; i < 4; i++) { int dx = tx[i] + s._sx; int dy = ty[i] + s._sy; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; if (visited[dx][dy][s._gx][s._gy][pattern_i]) continue; visited[dx][dy][s._gx][s._gy][pattern_i] = true; if (dx == s._gx && dy == s._gy && gTime > s._time) { gTime = s._time; rx = s._gx; ry = s._gy; return; } que.push(State(dx, dy, s._gx, s._gy, s._time + 1)); } } } int main() { while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; int sx, sy, gx, gy; memset(visited, false, sizeof(visited)); gTime = INF; for (int y = 0; y < H; y++) { char line[32]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (stage[y][x] == 'A') { sx = x; sy = y; } if (stage[y][x] == 'B') { gx = x; gy = y; } } } cin >> ghost; rx = -1; ry = -1; bfs(sx, sy, gx, gy); if (gTime == INF) { printf("impossible\n"); } else { printf("%d %d %d\n", gTime + 1, ry, rx); } } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
1,004
#include <iostream> #include <queue> using namespace std; typedef pair<pair<int, int>, int> pa; #define ff first.first #define fs first.second int h, w, sx, sy, gx, gy; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; int co[21][21]; char ma[21][21]; bool visited[21][21]; string pat; int m = 99999; int main() { while (cin >> h >> w, h) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { co[i][j] = m; cin >> ma[i][j]; if (ma[i][j] == 'A') sx = j, sy = i; else if (ma[i][j] == 'B') gx = j, gy = i; } } queue<pa> q; q.push(make_pair(make_pair(sx, sy), 0)); while (q.size()) { pa p = q.front(); q.pop(); if (co[p.fs][p.ff] != m) continue; co[p.fs][p.ff] = p.second; for (int i = 0; i < 4; i++) { int nx = p.ff + dx[i]; int ny = p.fs + dy[i]; if (nx >= 0 && nx < w && ny >= 0 & ny < h && ma[ny][nx] != '#' && co[ny][nx] == m) q.push(make_pair(make_pair(nx, ny), p.second + 1)); } } cin >> pat; for (int j = 0, go = 0;; j++, go++) { int i = j % pat.length(); if (co[gy][gx] <= go) { cout << go << " " << gy << " " << gx << endl; break; } if (go > 100000) { cout << "impossible" << endl; break; } if (pat[i] == '8') gy--; else if (pat[i] == '6') gx++; else if (pat[i] == '4') gx--; else if (pat[i] == '2') gy++; gy = max(0, min(h - 1, gy)); gx = max(0, min(w - 1, gx)); } } }
#include <iostream> #include <queue> using namespace std; typedef pair<pair<int, int>, int> pa; #define ff first.first #define fs first.second int h, w, sx, sy, gx, gy; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; int co[21][21]; char ma[21][21]; bool visited[21][21]; string pat; int m = 999999; int main() { while (cin >> h >> w, h) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { co[i][j] = m; cin >> ma[i][j]; if (ma[i][j] == 'A') sx = j, sy = i; else if (ma[i][j] == 'B') gx = j, gy = i; } } queue<pa> q; q.push(make_pair(make_pair(sx, sy), 0)); while (q.size()) { pa p = q.front(); q.pop(); if (co[p.fs][p.ff] != m) continue; co[p.fs][p.ff] = p.second; for (int i = 0; i < 4; i++) { int nx = p.ff + dx[i]; int ny = p.fs + dy[i]; if (nx >= 0 && nx < w && ny >= 0 & ny < h && ma[ny][nx] != '#' && co[ny][nx] == m) q.push(make_pair(make_pair(nx, ny), p.second + 1)); } } cin >> pat; for (int j = 0, go = 0;; j++, go++) { int i = j % pat.length(); if (co[gy][gx] <= go) { cout << go << " " << gy << " " << gx << endl; break; } if (go > 100000) { cout << "impossible" << endl; break; } if (pat[i] == '8') gy--; else if (pat[i] == '6') gx++; else if (pat[i] == '4') gx--; else if (pat[i] == '2') gy++; gy = max(0, min(h - 1, gy)); gx = max(0, min(w - 1, gx)); } } }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13]]
1
584
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef pair<int, int> P; char s[20][21]; int d[20][20]; int dx[]{0, -1, 0, 0, 1}, dy[]{0, 0, 1, -1, 0}; int main() { int h, w; while (scanf("%d%d", &h, &w), h) { int ax, ay, bx, by; rep(i, h) { scanf("%s", s[i]); rep(j, w) { if (s[i][j] == 'A') ax = i, ay = j; if (s[i][j] == 'B') bx = i, by = j; } } string p; cin >> p; for (char &c : p) { if (c == '5') c = 0; if (c == '8') c = 1; if (c == '6') c = 2; if (c == '4') c = 3; if (c == '2') c = 4; } queue<P> que; memset(d, -1, sizeof(d)); d[0][0] = 0; que.push(P(ax, ay)); while (!que.empty()) { P p = que.front(); que.pop(); for (int i = 1; i <= 4; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#' && d[nx][ny] == -1) { d[nx][ny] = d[p.first][p.second] + 1; que.push(P(nx, ny)); } } } int x = bx, y = by; int t = 0; rep(i, 9000000) { for (char c : p) { if (d[x][y] != -1 && d[x][y] <= t) { printf("%d %d %d\n", t, x, y); goto g; } int nx = x + dx[c], ny = y + dy[c]; t++; if (0 <= nx && nx < h && 0 <= ny && ny < w) x = nx, y = ny; } } puts("impossible"); g:; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef pair<int, int> P; char s[20][21]; int d[20][20]; int dx[]{0, -1, 0, 0, 1}, dy[]{0, 0, 1, -1, 0}; int main() { int h, w; while (scanf("%d%d", &h, &w), h) { int ax, ay, bx, by; rep(i, h) { scanf("%s", s[i]); rep(j, w) { if (s[i][j] == 'A') ax = i, ay = j; if (s[i][j] == 'B') bx = i, by = j; } } string p; cin >> p; for (char &c : p) { if (c == '5') c = 0; if (c == '8') c = 1; if (c == '6') c = 2; if (c == '4') c = 3; if (c == '2') c = 4; } queue<P> que; memset(d, -1, sizeof(d)); d[ax][ay] = 0; que.push(P(ax, ay)); while (!que.empty()) { P p = que.front(); que.pop(); for (int i = 1; i <= 4; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#' && d[nx][ny] == -1) { d[nx][ny] = d[p.first][p.second] + 1; que.push(P(nx, ny)); } } } int x = bx, y = by; int t = 0; rep(i, 100) { for (char c : p) { if (d[x][y] != -1 && d[x][y] <= t) { printf("%d %d %d\n", t, x, y); goto g; } int nx = x + dx[c], ny = y + dy[c]; t++; if (0 <= nx && nx < h && 0 <= ny && ny < w) x = nx, y = ny; } } puts("impossible"); g:; } }
[["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 13], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 13], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
572
#include <iostream> #include <queue> #include <string> #include <vector> using namespace std; int dir[] = {0, 1, 0, -1}; int H, W, dist[25][25]; string s[25], t; int main() { while (cin >> H >> W, H) { int ax = -1, ay = -1, bx = -1, by = -1; for (int i = 0; i < H; i++) { cin >> s[i]; for (int j = 0; j < W; j++) { if (s[i][j] == 'A') ax = i, ay = j; if (s[i][j] == 'B') bx = i, by = j; } } cin >> t; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dist[i][j] = 999999999; } } queue<pair<int, int>> que; que.push(make_pair(ax, ay)); dist[ax][ay] = 0; while (!que.empty()) { pair<int, int> u = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int tx = u.first + dir[i], ty = u.second + dir[i ^ 1]; if (0 <= tx && tx < H && 0 <= ty && ty < W && s[tx][ty] != '#' && dist[tx][ty] == 999999999) { dist[tx][ty] = dist[u.first][u.second] + 1; que.push(make_pair(tx, ty)); } } } bool flag = false; for (int i = 0; i < 10000; i++) { if (dist[bx][by] <= i) { cout << i << ' ' << bx << ' ' << by << endl; flag = true; break; } int tx = bx, ty = by; if (t[i % t.size()] == '2') tx++; if (t[i % t.size()] == '4') ty--; if (t[i % t.size()] == '6') ty++; if (t[i % t.size()] == '8') tx--; if (0 <= tx && tx < H && 0 <= ty && ty < W) bx = tx, by = ty; } if (!flag) cout << "Impossible" << endl; } return 0; }
#include <iostream> #include <queue> #include <string> #include <vector> using namespace std; int dir[] = {0, 1, 0, -1}; int H, W, dist[25][25]; string s[25], t; int main() { while (cin >> H >> W, H) { int ax = -1, ay = -1, bx = -1, by = -1; for (int i = 0; i < H; i++) { cin >> s[i]; for (int j = 0; j < W; j++) { if (s[i][j] == 'A') ax = i, ay = j; if (s[i][j] == 'B') bx = i, by = j; } } cin >> t; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dist[i][j] = 999999999; } } queue<pair<int, int>> que; que.push(make_pair(ax, ay)); dist[ax][ay] = 0; while (!que.empty()) { pair<int, int> u = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int tx = u.first + dir[i], ty = u.second + dir[i ^ 1]; if (0 <= tx && tx < H && 0 <= ty && ty < W && s[tx][ty] != '#' && dist[tx][ty] == 999999999) { dist[tx][ty] = dist[u.first][u.second] + 1; que.push(make_pair(tx, ty)); } } } bool flag = false; for (int i = 0; i < 10000; i++) { if (dist[bx][by] <= i) { cout << i << ' ' << bx << ' ' << by << endl; flag = true; break; } int tx = bx, ty = by; if (t[i % t.size()] == '2') tx++; if (t[i % t.size()] == '4') ty--; if (t[i % t.size()] == '6') ty++; if (t[i % t.size()] == '8') tx--; if (0 <= tx && tx < H && 0 <= ty && ty < W) bx = tx, by = ty; } if (!flag) cout << "impossible" << endl; } return 0; }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
583
#include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define fr first #define sc second #define INFTY 1000000 typedef pair<int, int> Pt; typedef pair<pair<Pt, Pt>, int> P; Pt A, B; int h, w, dx[] = {0, 1, 0, -1, 0}, dy[] = {1, 0, -1, 0, 0}; char mas[20][20]; int used[20][20][20][20][11]; string pattern; pair<int, Pt> bfs(); bool judge(int, int); int main() { while (cin >> h >> w && h) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> mas[i][j]; if (mas[i][j] == 'A') A = Pt(i, j); else if (mas[i][j] == 'B') B = Pt(i, j); } } cin >> pattern; pair<int, Pt> ans = bfs(); if (ans.fr == -1) cout << "impossible" << endl; else cout << ans.fr << " " << ans.sc.fr << " " << ans.sc.sc << endl; } } pair<int, Pt> bfs() { map<char, int> tmp; tmp['5'] = 4, tmp['8'] = 3, tmp['6'] = 0, tmp['4'] = 2, tmp['2'] = 1; queue<P> que; memset(used, -1, sizeof(used)); que.push(P(make_pair(A, B), -1)); while (!que.empty()) { P p = que.front(); que.pop(); if (p.fr.fr == p.fr.sc) { return ( make_pair(used[p.fr.fr.fr][p.fr.fr.sc][p.fr.sc.fr][p.fr.sc.sc][p.sc], Pt(p.fr.fr.fr, p.fr.fr.sc))); } int cnt = (p.sc + 1) % pattern.size(); int bx = p.fr.sc.fr + dx[tmp[pattern[cnt]]]; int by = p.fr.sc.sc + dy[tmp[pattern[cnt]]]; if (!judge(bx, by)) bx = p.fr.sc.fr, by = p.fr.sc.sc; for (int i = 0; i < 5; i++) { int ax = p.fr.fr.fr + dx[i], ay = p.fr.fr.sc + dy[i]; if (!judge(ax, ay) || mas[ax][ay] == '#') ax = p.fr.fr.fr, ay = p.fr.fr.sc; if (used[ax][ay][bx][by][cnt] == -1) { que.push(P(make_pair(Pt(ax, ay), Pt(bx, by)), cnt)); used[ax][ay][bx][by][cnt] = used[p.fr.fr.fr][p.fr.fr.sc][p.fr.sc.fr][p.fr.sc.sc][p.sc] + 1; } } } return make_pair(-1, Pt(0, 0)); } bool judge(int x, int y) { return (x >= 0 && x < h && y >= 0 && y < w ? true : false); }
#include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define fr first #define sc second #define INFTY 1000000 typedef pair<int, int> Pt; typedef pair<pair<Pt, Pt>, int> P; Pt A, B; int h, w, dx[] = {0, 1, 0, -1, 0}, dy[] = {1, 0, -1, 0, 0}; char mas[20][20]; int used[20][20][20][20][11]; string pattern; pair<int, Pt> bfs(); bool judge(int, int); int main() { while (cin >> h >> w && h) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> mas[i][j]; if (mas[i][j] == 'A') A = Pt(i, j); else if (mas[i][j] == 'B') B = Pt(i, j); } } cin >> pattern; pair<int, Pt> ans = bfs(); if (ans.fr == -1) cout << "impossible" << endl; else cout << ans.fr << " " << ans.sc.fr << " " << ans.sc.sc << endl; } } pair<int, Pt> bfs() { map<char, int> tmp; tmp['5'] = 4, tmp['8'] = 3, tmp['6'] = 0, tmp['4'] = 2, tmp['2'] = 1; queue<P> que; memset(used, -1, sizeof(used)); que.push(P(make_pair(A, B), -1)); while (!que.empty()) { P p = que.front(); que.pop(); if (p.fr.fr == p.fr.sc) { return (make_pair( used[p.fr.fr.fr][p.fr.fr.sc][p.fr.sc.fr][p.fr.sc.sc][p.sc] + 1, Pt(p.fr.fr.fr, p.fr.fr.sc))); } int cnt = (p.sc + 1) % pattern.size(); int bx = p.fr.sc.fr + dx[tmp[pattern[cnt]]]; int by = p.fr.sc.sc + dy[tmp[pattern[cnt]]]; if (!judge(bx, by)) bx = p.fr.sc.fr, by = p.fr.sc.sc; for (int i = 0; i < 5; i++) { int ax = p.fr.fr.fr + dx[i], ay = p.fr.fr.sc + dy[i]; if (!judge(ax, ay) || mas[ax][ay] == '#') ax = p.fr.fr.fr, ay = p.fr.fr.sc; if (used[ax][ay][bx][by][cnt] == -1) { que.push(P(make_pair(Pt(ax, ay), Pt(bx, by)), cnt)); used[ax][ay][bx][by][cnt] = used[p.fr.fr.fr][p.fr.fr.sc][p.fr.sc.fr][p.fr.sc.sc][p.sc] + 1; } } } return make_pair(-1, Pt(0, 0)); } bool judge(int x, int y) { return (x >= 0 && x < h && y >= 0 && y < w ? true : false); }
[["+", 0, 23, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 23, 0, 2, 3, 4, 0, 16, 12, 13]]
1
864
#include <algorithm> #include <iostream> #include <queue> #include <stdio.h> #define INF 99999999 using namespace std; queue<int> qy, qx, qcnt; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int n, m, x, y, cnt, nx, ny, mini, ansx, ansy; int t[20][20], sx, sy, tx, ty, vd[20][20]; string in; int main() { while (cin >> n >> m && n) { for (int i = 0; i < n; i++) { cin >> in; for (int j = 0; j < (int)in.size(); j++) { if (in[j] == 'A') sx = j, sy = i; else if (in[j] == 'B') tx = j, ty = i; t[i][j] = (in[j] == '#'); } } for (int i = 0; i < 20; i++) for (int j = 0; j < 20; j++) vd[i][j] = INF; vd[sy][sx] = 1; qx.push(sx); qy.push(sy); qcnt.push(1); while (!qx.empty()) { x = qx.front(); qx.pop(); y = qy.front(); qy.pop(); cnt = qcnt.front(); qcnt.pop(); for (int i = 0; i < 4; i++) { nx = x + dx[i]; ny = y + dy[i]; if (!(0 <= nx && nx < m && 0 <= ny && ny < n)) continue; if (vd[ny][nx] > 0 || t[ny][nx] == 1) continue; vd[ny][nx] = cnt + 1; qx.push(nx); qy.push(ny); qcnt.push(cnt + 1); } } cin >> in; cnt = 0; mini = INF; for (int I = 0; I < n * m; I++) { for (int i = 0; i < (int)in.size(); i++) { cnt++; if (cnt >= vd[ty][tx]) { if (mini > cnt) { mini = cnt; ansy = ty; ansx = tx; } } if (in[i] == '5') continue; else if (in[i] == '8') { if (ty == 0) continue; ty--; } else if (in[i] == '6') { if (tx == m - 1) continue; tx++; } else if (in[i] == '4') { if (tx == 0) continue; tx--; } else { if (ty == n - 1) continue; ty++; } } // for } // while if (mini == INF) cout << "impossible" << endl; else cout << mini - 1 << ' ' << ansy << ' ' << ansx << endl; } return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <stdio.h> #define INF 99999999 using namespace std; queue<int> qy, qx, qcnt; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int n, m, x, y, cnt, nx, ny, mini, ansx, ansy; int t[20][20], sx, sy, tx, ty, vd[20][20]; string in; int main() { while (cin >> n >> m && n) { for (int i = 0; i < n; i++) { cin >> in; for (int j = 0; j < (int)in.size(); j++) { if (in[j] == 'A') sx = j, sy = i; else if (in[j] == 'B') tx = j, ty = i; t[i][j] = (in[j] == '#'); } } for (int i = 0; i < 20; i++) for (int j = 0; j < 20; j++) vd[i][j] = INF; vd[sy][sx] = 1; qx.push(sx); qy.push(sy); qcnt.push(1); while (!qx.empty()) { x = qx.front(); qx.pop(); y = qy.front(); qy.pop(); cnt = qcnt.front(); qcnt.pop(); for (int i = 0; i < 4; i++) { nx = x + dx[i]; ny = y + dy[i]; if (!(0 <= nx && nx < m && 0 <= ny && ny < n)) continue; if (vd[ny][nx] != INF || t[ny][nx] == 1) continue; vd[ny][nx] = cnt + 1; qx.push(nx); qy.push(ny); qcnt.push(cnt + 1); } } cin >> in; cnt = 0; mini = INF; for (int I = 0; I < n * m; I++) { for (int i = 0; i < (int)in.size(); i++) { cnt++; if (cnt >= vd[ty][tx]) { if (mini > cnt) { mini = cnt; ansy = ty; ansx = tx; } } if (in[i] == '5') continue; else if (in[i] == '8') { if (ty == 0) continue; ty--; } else if (in[i] == '6') { if (tx == m - 1) continue; tx++; } else if (in[i] == '4') { if (tx == 0) continue; tx--; } else { if (ty == n - 1) continue; ty++; } } // for } // while if (mini == INF) cout << "impossible" << endl; else cout << mini - 1 << ' ' << ansy << ' ' << ansx << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 47], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 79], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 22]]
1
697
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <vector> using namespace std; typedef complex<double> P; const double PI = acos(-1); const double EPS = 1e-9; struct circle /*{{{*/ { P o; double r; circle() {} circle(const P &p, double x) : o(p), r(x) {} // POJ 1418 Viva Confetti // POJ 2149 Inherit the Spheres inline bool intersects(const circle &c) const { return !contains(c) && !c.contains(*this) && abs(o - c.o) <= r + c.r; } pair<P, P> intersection(const circle &c) const { // assert(intersects(c)) const double d = abs(o - c.o); const double cos_ = abs(d * d + r * r - c.r * c.r) / (2 * d); const double sin_ = sqrt(r * r - cos_ * cos_); const P e = (c.o - o) / d; return make_pair(o + e * P(cos_, -sin_), o + e * P(cos_, sin_)); } inline bool contains(const circle &c) const { return abs(o - c.o) + c.r <= r; } }; /*}}}*/ bool solve() { int N; cin >> N; if (!N) return false; vector<circle> cs; for (int i = 0; i < N; ++i) { double x, y, r; cin >> x >> y >> r; cs.push_back(circle(P(x, y), r)); } double ans = 0; for (int i = 0; i < N; ++i) { vector<pair<double, int>> ps; const circle &c1 = cs[i]; ps.push_back(make_pair(-PI, 0)); ps.push_back(make_pair(PI, 0)); for (int j = 0; j < N; ++j) { if (i == j) continue; const circle &c2 = cs[j]; if (c2.contains(c1)) { ps.clear(); break; } if (c1.intersects(c2)) { const auto tmp = c1.intersection(c2); const double arg1 = arg(tmp.first - c1.o); const double arg2 = arg(tmp.second - c1.o); // cout << i << ' ' << j << ' ' << arg1 << ' ' << arg2 << endl; ps.push_back(make_pair(arg1, 1)); ps.push_back(make_pair(arg2, -1)); if (arg1 > arg2) { ps.push_back(make_pair(-PI, 1)); } } } sort(ps.begin(), ps.end()); int cur = 0; double prev = -PI; for (const auto &p : ps) { if (abs(prev - p.first) > EPS) { if (cur == 0) ans += c1.r * abs(p.first - prev); } cur += p.second; prev = p.first; } } printf("%.8f\n", ans); return true; } int main() { cin.tie(0); ios::sync_with_stdio(0); while (solve()) ; return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <vector> using namespace std; typedef complex<double> P; const double PI = acos(-1); const double EPS = 1e-9; struct circle /*{{{*/ { P o; double r; circle() {} circle(const P &p, double x) : o(p), r(x) {} // POJ 1418 Viva Confetti // POJ 2149 Inherit the Spheres inline bool intersects(const circle &c) const { return !contains(c) && !c.contains(*this) && abs(o - c.o) <= r + c.r; } pair<P, P> intersection(const circle &c) const { // assert(intersects(c)) const double d = abs(o - c.o); const double cos_ = (d * d + r * r - c.r * c.r) / (2 * d); const double sin_ = sqrt(r * r - cos_ * cos_); const P e = (c.o - o) / d; return make_pair(o + e * P(cos_, -sin_), o + e * P(cos_, sin_)); } inline bool contains(const circle &c) const { return abs(o - c.o) + c.r <= r; } }; /*}}}*/ bool solve() { int N; cin >> N; if (!N) return false; vector<circle> cs; for (int i = 0; i < N; ++i) { double x, y, r; cin >> x >> y >> r; cs.push_back(circle(P(x, y), r)); } double ans = 0; for (int i = 0; i < N; ++i) { vector<pair<double, int>> ps; const circle &c1 = cs[i]; ps.push_back(make_pair(-PI, 0)); ps.push_back(make_pair(PI, 0)); for (int j = 0; j < N; ++j) { if (i == j) continue; const circle &c2 = cs[j]; if (c2.contains(c1)) { ps.clear(); break; } if (c1.intersects(c2)) { const auto tmp = c1.intersection(c2); const double arg1 = arg(tmp.first - c1.o); const double arg2 = arg(tmp.second - c1.o); // cout << i << ' ' << j << ' ' << arg1 << ' ' << arg2 << endl; ps.push_back(make_pair(arg1, 1)); ps.push_back(make_pair(arg2, -1)); if (arg1 > arg2) { ps.push_back(make_pair(-PI, 1)); } } } sort(ps.begin(), ps.end()); int cur = 0; double prev = -PI; for (const auto &p : ps) { if (abs(prev - p.first) > EPS) { if (cur == 0) ans += c1.r * abs(p.first - prev); } cur += p.second; prev = p.first; } } printf("%.8f\n", ans); return true; } int main() { cin.tie(0); ios::sync_with_stdio(0); while (solve()) ; return 0; }
[["-", 0, 43, 49, 50, 51, 16, 31, 2, 63, 22]]
1
687
#include <algorithm> #include <cmath> #include <stack> #include <stdio.h> using namespace std; int b_row, b_col, memo[16][3][3]; char b[2]; bool rangeCheck(int row, int col) { if (row >= 0 && row <= 2 && col >= 0 && col <= 2) return true; else { return false; } } int translate(char ch) { switch (ch) { case 'A': return 0; case 'B': return 1; case 'C': return 2; case 'D': return 10; case 'E': return 11; case 'F': return 12; case 'G': return 20; case 'H': return 21; case 'I': return 22; } return -1; // must not reach here. } int main() { int n, tmp, energy_row, energy_col, row, col, div_row[4] = {-1, 0, 0, 1}, div_col[4] = {0, -1, 1, 0}; char start[2], energy[2]; while (true) { scanf("%d", &n); if (n == 0) break; for (int i = 0; i < 16; i++) { for (int p = 0; p < 3; p++) { for (int k = 0; k < 3; k++) memo[i][p][k] = 0; } } scanf("%s %s %s", start, energy, b); tmp = translate(b[0]); b_row = tmp / 10; b_col = tmp % 10; tmp = translate(energy[0]); energy_row = tmp / 10; energy_col = tmp % 10; tmp = translate(start[0]); row = tmp / 10, col = tmp % 10; memo[0][row][col] = 1; for (int loop = 0; loop <= n - 1; loop++) { for (int i = 0; i < 3; i++) { for (int k = 0; k < 3; k++) { if (i != b_row || k != b_col) { //????????§????????´??? for (int p = 0; p < 4; p++) { if (rangeCheck(i + div_row[p], k + div_col[p]) == true && (i + div_row[p] != b_row || k + div_col[p] != b_col)) { //?§????????????§??????????????????????????§?????? memo[loop + 1][i + div_row[p]][k + div_col[p]] += memo[loop][i][k]; } else { //?°?????????¨???1????????????????????????????????´?????????????????´??????????????¨????????´????????°??????????????? memo[loop + 1][i][k] = memo[loop][i][k]; } } } } } } printf("%.8lf\n", ((double)memo[n][energy_row][energy_col]) / ((double)pow(4, n))); } return 0; }
#include <algorithm> #include <cmath> #include <stack> #include <stdio.h> using namespace std; int b_row, b_col, memo[16][3][3]; char b[2]; bool rangeCheck(int row, int col) { if (row >= 0 && row <= 2 && col >= 0 && col <= 2) return true; else { return false; } } int translate(char ch) { switch (ch) { case 'A': return 0; case 'B': return 1; case 'C': return 2; case 'D': return 10; case 'E': return 11; case 'F': return 12; case 'G': return 20; case 'H': return 21; case 'I': return 22; } return -1; // must not reach here. } int main() { int n, tmp, energy_row, energy_col, row, col, div_row[4] = {-1, 0, 0, 1}, div_col[4] = {0, -1, 1, 0}; char start[2], energy[2]; while (true) { scanf("%d", &n); if (n == 0) break; for (int i = 0; i < 16; i++) { for (int p = 0; p < 3; p++) { for (int k = 0; k < 3; k++) memo[i][p][k] = 0; } } scanf("%s %s %s", start, energy, b); tmp = translate(b[0]); b_row = tmp / 10; b_col = tmp % 10; tmp = translate(energy[0]); energy_row = tmp / 10; energy_col = tmp % 10; tmp = translate(start[0]); row = tmp / 10, col = tmp % 10; memo[0][row][col] = 1; for (int loop = 0; loop <= n - 1; loop++) { for (int i = 0; i < 3; i++) { for (int k = 0; k < 3; k++) { if (i != b_row || k != b_col) { for (int p = 0; p < 4; p++) { if (rangeCheck(i + div_row[p], k + div_col[p]) == true && (i + div_row[p] != b_row || k + div_col[p] != b_col)) { memo[loop + 1][i + div_row[p]][k + div_col[p]] += memo[loop][i][k]; } else { memo[loop + 1][i][k] += memo[loop][i][k]; } } } } } } printf("%.8lf\n", ((double)memo[n][energy_row][energy_col]) / ((double)pow(4, n))); } return 0; }
[["-", 75, 76, 0, 9, 0, 1, 0, 11, 17, 32], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 17, 107]]
1
621
#include <iostream> #include <stdio.h> using namespace std; int main() { while (1) { int n, s, t, b, sum = 0; int ai[16][3][3] = {0}; char a, d, c; scanf("%d", &n); if (n == 0) break; cin >> a >> d >> c; s = a - 'A'; t = d - 'A'; b = c - 'A'; ai[0][s / 3][s % 3] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j < 3; ++j) { for (int l = 0; l < 3; ++l) { if (j - 1 >= 0 && (j - 1) * 3 + l != b) ai[i][j - 1][l] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; if (j + 1 < 3 && (j - 1) * 3 + l != b) ai[i][j + 1][l] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; if (l - 1 >= 0 && j * 3 + l - 1 != b) ai[i][j][l - 1] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; if (l + 1 < 3 && j * 3 + l + 1 != b) ai[i][j][l + 1] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; } } } for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { sum += ai[n][i][j]; } } printf("%f\n", (double)ai[n][t / 3][t % 3] / sum); } }
#include <iostream> #include <stdio.h> using namespace std; int main() { while (1) { int n, s, t, b, sum = 0; int ai[16][3][3] = {0}; char a, d, c; scanf("%d", &n); if (n == 0) break; cin >> a >> d >> c; s = a - 'A'; t = d - 'A'; b = c - 'A'; ai[0][s / 3][s % 3] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j < 3; ++j) { for (int l = 0; l < 3; ++l) { if (j - 1 >= 0 && (j - 1) * 3 + l != b) ai[i][j - 1][l] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; if (j + 1 < 3 && (j + 1) * 3 + l != b) ai[i][j + 1][l] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; if (l - 1 >= 0 && j * 3 + l - 1 != b) ai[i][j][l - 1] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; if (l + 1 < 3 && j * 3 + l + 1 != b) ai[i][j][l + 1] += ai[i - 1][j][l]; else ai[i][j][l] += ai[i - 1][j][l]; } } } for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { sum += ai[n][i][j]; } } printf("%f\n", (double)ai[n][t / 3][t % 3] / sum); } }
[["-", 31, 16, 31, 16, 31, 23, 0, 16, 17, 33], ["+", 31, 16, 31, 16, 31, 23, 0, 16, 17, 72]]
1
524
#include "stdio.h" #include <cstring> #include <iostream> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int main() { int n; double prob[2][3][3]; bool junk[3][3]; while (cin >> n, n) { char s, t, b; cin >> s >> t >> b; double ans = 0.0; for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) prob[i][j][k] = 0.0; prob[0][(s - 'A') / 3][(s - 'A') % 3] = 1.0; int gx = (t - 'A') / 3, gy = (t - 'A') % 3; memset(junk, false, sizeof(junk)); junk[(b - 'A') / 3][(b - 'A') % 3] = true; for (int i = 0; i < n; i++) { for (int x = 0; x < 3; x++) for (int y = 0; y < 3; y++) prob[1 - (i % 2)][x][y] = 0.0; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { for (int d = 0; d < n; d++) { int nx = x + dx[d], ny = y + dy[d]; if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || junk[nx][ny]) { nx = x, ny = y; } prob[1 - (i % 2)][nx][ny] += prob[i % 2][x][y] * 0.25; } } } } printf("%.8lf\n", prob[n % 2][gx][gy]); } }
#include "stdio.h" #include <cstring> #include <iostream> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int main() { int n; double prob[2][3][3]; bool junk[3][3]; while (cin >> n, n) { char s, t, b; cin >> s >> t >> b; double ans = 0.0; for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) prob[i][j][k] = 0.0; prob[0][(s - 'A') / 3][(s - 'A') % 3] = 1.0; int gx = (t - 'A') / 3, gy = (t - 'A') % 3; memset(junk, false, sizeof(junk)); junk[(b - 'A') / 3][(b - 'A') % 3] = true; for (int i = 0; i < n; i++) { for (int x = 0; x < 3; x++) for (int y = 0; y < 3; y++) prob[1 - (i % 2)][x][y] = 0.0; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { for (int d = 0; d < 4; d++) { int nx = x + dx[d], ny = y + dy[d]; if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || junk[nx][ny]) { nx = x, ny = y; } prob[1 - (i % 2)][nx][ny] += prob[i % 2][x][y] * 0.25; } } } } printf("%.8lf\n", prob[n % 2][gx][gy]); } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
1
468
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; #define mp make_pair template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } double dp[20][10]; int n, s, t, b; int move(int pos, int x, int y) { if (pos < 3 && y == -1) return pos; if (pos > 6 && y == 1) return pos; if (pos % 3 == 0 && x == -1) return pos; if (pos % 3 == 2 && x == 1) return pos; pos += x + 3 * y; if (pos == b) return pos - x - 3 * y; return pos; } int main() { string ss, tt, bb; while (n = in()) { cin >> ss >> tt >> bb; s = ss[0] - 'A'; t = tt[0] - 'A'; b = bb[0] - 'A'; memset(dp, 0, sizeof(dp)); dp[0][s] = 1; int i, j; for (i = 1; i <= n; i++) { for (j = 0; j < 9; j++) { dp[i][move(j, 1, 0)] += dp[i - 1][j] / 4; dp[i][move(j, -1, 0)] += dp[i - 1][j] / 4; dp[i][move(j, 0, 1)] += dp[i - 1][j] / 4; dp[i][move(j, 0, -1)] += dp[i - 1][j] / 4; } } printf("%.12f\n", dp[n][t]); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; #define mp make_pair template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } double dp[20][10]; int n, s, t, b; int move(int pos, int x, int y) { if (pos < 3 && y == -1) return pos; if (pos >= 6 && y == 1) return pos; if (pos % 3 == 0 && x == -1) return pos; if (pos % 3 == 2 && x == 1) return pos; pos += x + 3 * y; if (pos == b) return pos - x - 3 * y; return pos; } int main() { string ss, tt, bb; while (n = in()) { cin >> ss >> tt >> bb; s = ss[0] - 'A'; t = tt[0] - 'A'; b = bb[0] - 'A'; memset(dp, 0, sizeof(dp)); dp[0][s] = 1; int i, j; for (i = 1; i <= n; i++) { for (j = 0; j < 9; j++) { dp[i][move(j, 1, 0)] += dp[i - 1][j] / 4; dp[i][move(j, -1, 0)] += dp[i - 1][j] / 4; dp[i][move(j, 0, 1)] += dp[i - 1][j] / 4; dp[i][move(j, 0, -1)] += dp[i - 1][j] / 4; } } printf("%.12f\n", dp[n][t]); } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 20]]
1
543
#include <iostream> #include <string.h> using namespace std; int n; char room[10] = "ABCDEFGHI"; char s, t, b; int off[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int x, y; double table[3][3]; double solve() { memset(&table[0][0], 0, sizeof(double) * 9); table[x][y] = 1.0; for (int k = 0; k < n; k++) { double temp[3][3]; memset(&temp[0][0], 0, sizeof(double) * 9); for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { if (table[j][i] != 0.0) { for (int m = 0; m < 4; m++) { int xx = i + off[m][0]; int yy = j + off[m][1]; int index = xx + yy * 3; xx = (xx >= 0 && xx < 3 && room[index] != b) ? xx : i; yy = (yy >= 0 && yy < 3 && room[index] != b) ? yy : j; temp[yy][xx] += table[j][i] / 4.0; } } } } memcpy(&table[0][0], &temp[0][0], sizeof(double) * 9); } for (int i = 0; i < 9; i++) { if (room[i] == t) return table[i / 3][i % 3]; } return 0.0; } int main() { while (1) { cin >> n; if (n == 0) break; cin >> s >> t >> b; for (int i = 0; i < 9; i++) { if (room[i] == s) { x = i % 3; y = i / 3; } } printf("%.8f\n", solve()); } return 0; }
#include <iostream> #include <string.h> using namespace std; int n; char room[10] = "ABCDEFGHI"; char s, t, b; int off[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int x, y; double table[3][3]; double solve() { memset(&table[0][0], 0, sizeof(double) * 9); table[y][x] = 1.0; for (int k = 0; k < n; k++) { double temp[3][3]; memset(&temp[0][0], 0, sizeof(double) * 9); for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { if (table[j][i] != 0.0) { for (int m = 0; m < 4; m++) { int xx = i + off[m][0]; int yy = j + off[m][1]; int index = xx + yy * 3; xx = (xx >= 0 && xx < 3 && room[index] != b) ? xx : i; yy = (yy >= 0 && yy < 3 && room[index] != b) ? yy : j; temp[yy][xx] += table[j][i] / 4.0; } } } } memcpy(&table[0][0], &temp[0][0], sizeof(double) * 9); } for (int i = 0; i < 9; i++) { if (room[i] == t) return table[i / 3][i % 3]; } return 0.0; } int main() { while (1) { cin >> n; if (n == 0) break; cin >> s >> t >> b; for (int i = 0; i < 9; i++) { if (room[i] == s) { x = i % 3; y = i / 3; } } printf("%.8f\n", solve()); } return 0; }
[["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
476
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define EPS 1e-9 #define INF MOD #define MOD 1000000007LL #define fir first #define iss istringstream #define sst stringstream #define ite iterator #define ll long long #define mp make_pair #define rep(i, n) rep2(i, 0, n) #define rep2(i, m, n) for (int i = m; i < n; i++) #define pi pair<int, int> #define pb push_back #define sec second #define sh(i) (1LL << i) #define sz size() #define vi vector<int> #define vc vector #define vl vector<ll> #define vs vector<string> int n, dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0}; char s, b, j; map<pair<int, char>, double> M; char code(int y, int x) { return 'A' + y * 3 + x; } pi dec(char c) { return pi((c - 'A') / 3, (c - 'A') % 3); } double f(int n, char c) { if (n == 0) return c == b; if (M.find(pi(n, c)) != M.end()) return M[pi(n, c)]; double &res = M[pi(n, c)] = 0; pi p = dec(c); int y = p.fir, x = p.sec; rep(i, 4) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || 2 < ny || nx < 0 || 2 < nx || code(ny, nx) == j) res += f(n - 1, c) / 4; else res += f(n - 1, code(ny, nx)) / 4; } return res; } int main() { while (cin >> n && n) { cin >> s >> b >> j; cout << f(n, s) << endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define EPS 1e-9 #define INF MOD #define MOD 1000000007LL #define fir first #define iss istringstream #define sst stringstream #define ite iterator #define ll long long #define mp make_pair #define rep(i, n) rep2(i, 0, n) #define rep2(i, m, n) for (int i = m; i < n; i++) #define pi pair<int, int> #define pb push_back #define sec second #define sh(i) (1LL << i) #define sz size() #define vi vector<int> #define vc vector #define vl vector<ll> #define vs vector<string> int n, dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0}; char s, b, j; map<pair<int, char>, double> M; char code(int y, int x) { return 'A' + y * 3 + x; } pi dec(char c) { return pi((c - 'A') / 3, (c - 'A') % 3); } double f(int n, char c) { if (n == 0) return c == b; if (M.find(pi(n, c)) != M.end()) return M[pi(n, c)]; double &res = M[pi(n, c)] = 0; pi p = dec(c); int y = p.fir, x = p.sec; rep(i, 4) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || 2 < ny || nx < 0 || 2 < nx || code(ny, nx) == j) res += f(n - 1, c) / 4; else res += f(n - 1, code(ny, nx)) / 4; } return res; } int main() { while (cin >> n && n) { M.clear(); cin >> s >> b >> j; cout << f(n, s) << endl; } }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
441
#include <cstring> #include <iomanip> #include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { long double room[3][3][n + 1]; memset(room, 0, sizeof(room)); char s, t, b; cin >> s >> t >> b; int y = (s - 'A') / 3; int x = (s - 'A') % 3; int by = (b - 'A') / 3; int bx = (b - 'A') % 3; int gy = (t - 'A') / 3; int gx = (t - 'A') % 3; room[y][x][0] = 1.0; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, -1, 1, 0}; for (int i = 0; i < n; i++) { for (int k = 0; k < 3; k++) { for (int l = 0; l < 3; l++) { for (int j = 0; j < 4; j++) { int nx = l + dx[j]; int ny = k + dy[j]; if (nx < 0 || 2 < nx || ny < 0 || 2 < ny || (bx == l && by == k)) { room[k][l][i + 1] += room[k][l][i] * 0.25; } else { room[ny][nx][i + 1] += room[k][l][i] * 0.25; } } } } } cout.precision(9); cout << fixed << room[gy][gx][n] << endl; } return 0; }
#include <cstring> #include <iomanip> #include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { long double room[3][3][n + 1]; memset(room, 0, sizeof(room)); char s, t, b; cin >> s >> t >> b; int y = (s - 'A') / 3; int x = (s - 'A') % 3; int by = (b - 'A') / 3; int bx = (b - 'A') % 3; int gy = (t - 'A') / 3; int gx = (t - 'A') % 3; room[y][x][0] = 1.0; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, -1, 1, 0}; for (int i = 0; i < n; i++) { for (int k = 0; k < 3; k++) { for (int l = 0; l < 3; l++) { for (int j = 0; j < 4; j++) { int nx = l + dx[j]; int ny = k + dy[j]; if (nx < 0 || 2 < nx || ny < 0 || 2 < ny || (bx == nx && by == ny)) { room[k][l][i + 1] += room[k][l][i] * 0.25; } else { room[ny][nx][i + 1] += room[k][l][i] * 0.25; } } } } } cout.precision(9); cout << fixed << room[gy][gx][n] << endl; } return 0; }
[["-", 51, 16, 12, 23, 0, 16, 31, 16, 12, 22], ["+", 51, 16, 12, 23, 0, 16, 31, 16, 12, 22], ["-", 51, 16, 12, 23, 0, 16, 12, 16, 12, 22], ["+", 51, 16, 12, 23, 0, 16, 12, 16, 12, 22]]
1
388
#include <cstdio> #include <iostream> using namespace std; const int MAX = 16; const int NUM = 9; double f[MAX][NUM]; int main() { int n, s, t, b; char ts, tt, tb; while (cin >> n, n) { cin >> ts >> tt >> tb; s = ts - 'A'; t = tt - 'A'; b = tb - 'A'; fill((double *)f, (double *)f + MAX + NUM, 0); f[0][s] = 1.0; for (int i = 0; i < n; i++) { for (int j = 0; j < NUM; j++) { if (j >= 3 && (j - 3) != b) f[i + 1][j - 3] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; if (j < 6 && (j + 3) != b) f[i + 1][j + 3] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; if (j % 3 != 0 && (j - 1) != b) f[i + 1][j - 1] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; if (j % 3 != 2 && (j + 1) != b) f[i + 1][j + 1] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; } } printf("%.8lf\n", f[n][t]); } return 0; }
#include <cstdio> #include <iostream> using namespace std; const int MAX = 16; const int NUM = 9; double f[MAX][NUM]; int main() { int n, s, t, b; char ts, tt, tb; while (cin >> n, n) { cin >> ts >> tt >> tb; s = ts - 'A'; t = tt - 'A'; b = tb - 'A'; fill((double *)f, (double *)(f + MAX), 0); f[0][s] = 1.0; for (int i = 0; i < n; i++) { for (int j = 0; j < NUM; j++) { if (j >= 3 && (j - 3) != b) f[i + 1][j - 3] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; if (j < 6 && (j + 3) != b) f[i + 1][j + 3] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; if (j % 3 != 0 && (j - 1) != b) f[i + 1][j - 1] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; if (j % 3 != 2 && (j + 1) != b) f[i + 1][j + 1] += f[i][j] * 0.25; else f[i + 1][j] += f[i][j] * 0.25; } } printf("%.8lf\n", f[n][t]); } return 0; }
[["+", 0, 2, 3, 4, 0, 74, 51, 23, 0, 24], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 22], ["+", 0, 2, 3, 4, 0, 74, 51, 23, 0, 25]]
1
407
loop{ n = gets.to_i() if n == 0 then break end s,t,b = gets.split room_path = { "A"=>["B","D"], "B"=>["A","C","E"], "C"=>["B","F"], "D"=>["A","E","G"], "E"=>["D","B","F","H"], "F"=>["E","C","I"], "G"=>["H","D"], "H"=>["E","G","I"], "I"=>["F","H"]} room_path[b].each{|r| room_path[r].select!{|item| item != b} } room = Hash.new(0.0) room[s] = 1.0 n.times do troom = Hash.new(0.0) room_path.each_key{|key| troom[key] = room[key] } room_path.each{|key,path| room[key] = ((4.-path.size())/4) * troom[key] path.each{|r| room[key] += troom[r]/4 } } end p room[t] }
loop{ n = gets.to_i() if n == 0 then break end s,t,b = gets.split room_path = { "A"=>["B","D"], "B"=>["A","C","E"], "C"=>["B","F"], "D"=>["A","E","G"], "E"=>["D","B","F","H"], "F"=>["E","C","I"], "G"=>["H","D"], "H"=>["E","G","I"], "I"=>["F","H"]} room_path[b].each{|r| room_path[r].select!{|item| item != b} } room = Hash.new(0.0) room[s] = 1.0 n.times do troom = Hash.new(0.0) room_path.each_key{|key| troom[key] = room[key] } room_path.each{|key,path| room[key] = ((4.0-path.size())/4) * troom[key] path.each{|r| room[key] += troom[r]/4 } } end p room[t] }
[["-", 31, 739, 0, 738, 31, 739, 0, 652, 486, 612], ["-", 31, 739, 0, 738, 31, 739, 0, 652, 17, 131], ["+", 31, 739, 0, 738, 31, 739, 0, 738, 31, 531]]
4
306
#include <stdio.h> #include <string.h> int line_max; char buffer[2000]; char bunsyou[200][2000]; int cx, cy; void a(void) { cx = 0; } void e(void) { cx = strlen(bunsyou[cy]); } void p(void) { if (cy > 0) cy--; cx = 0; } void n(void) { if (cy < line_max - 1) cy++; cx = 0; } void f(void) { if (cx < strlen(bunsyou[cy])) cx++; else if (cy < line_max - 1) { cy++; cx = 0; } } void b(void) { if (cx > 0) cx--; else if (cy > 0) { cy--; cx = strlen(bunsyou[cy]); } } void d(void) { int i, length; if (cx < strlen(bunsyou[cy])) { length = strlen(&bunsyou[cy][cx]) - 1; memmove(&bunsyou[cy][cx], &bunsyou[cy][cx + 1], length); bunsyou[cy][cx + length] = 0; } else if (cy < line_max - 1) { strcat(bunsyou[cy], bunsyou[cy + 1]); for (i = cy + 2; i < line_max; i++) { strcpy(bunsyou[i - 1], bunsyou[i]); } line_max--; } } void k(void) { int i; if (cx >= strlen(bunsyou[cy])) { if (cy < line_max - 1) { strcat(bunsyou[cy], bunsyou[cy + 1]); for (i = cy + 2; i < line_max; i++) { strcpy(bunsyou[i - 1], bunsyou[i]); } line_max--; buffer[0] = '\n'; buffer[1] = 0; } } else { strcpy(buffer, &bunsyou[cy][cx]); bunsyou[cy][cx] = 0; } } void y(void) { int i, length; if (buffer[0] == 0) return; if (buffer[0] == '\n') { for (i = line_max; i > cy + 1; i--) { strcpy(bunsyou[i - 1], bunsyou[i]); } line_max++; strcpy(bunsyou[cy + 1], &bunsyou[cy][cx]); bunsyou[cy][cx] = 0; cy++; cx = 0; } else { length = strlen(buffer); memmove(&bunsyou[cy][cx + length], &bunsyou[cy][cx], strlen(&bunsyou[cy][cx]) + 1); memmove(&bunsyou[cy][cx], buffer, length); cx += length; } } int main(void) { char *cp; char cmd[10]; int i; line_max = 0; while (1) { fgets(bunsyou[line_max], sizeof(bunsyou[line_max]), stdin); cp = strchr(bunsyou[line_max], '\n'); if (cp) *cp = 0; if (strcmp(bunsyou[line_max], "END_OF_TEXT") == 0) { bunsyou[line_max][0] = 0; break; } else { line_max++; } } while (1) { scanf("%s", cmd); if (cmd[0] == '-') break; switch (cmd[0]) { case 'a': a(); break; case 'e': e(); break; case 'p': p(); break; case 'n': n(); break; case 'f': f(); break; case 'b': b(); break; case 'd': d(); break; case 'k': k(); break; case 'y': y(); break; } } for (i = 0; i < line_max; i++) puts(bunsyou[i]); return 0; }
#include <stdio.h> #include <string.h> int line_max; char buffer[2000]; char bunsyou[200][2000]; int cx, cy; void a(void) { cx = 0; } void e(void) { cx = strlen(bunsyou[cy]); } void p(void) { if (cy > 0) cy--; cx = 0; } void n(void) { if (cy < line_max - 1) cy++; cx = 0; } void f(void) { if (cx < strlen(bunsyou[cy])) cx++; else if (cy < line_max - 1) { cy++; cx = 0; } } void b(void) { if (cx > 0) cx--; else if (cy > 0) { cy--; cx = strlen(bunsyou[cy]); } } void d(void) { int i, length; if (cx < strlen(bunsyou[cy])) { length = strlen(&bunsyou[cy][cx]) - 1; memmove(&bunsyou[cy][cx], &bunsyou[cy][cx + 1], length); bunsyou[cy][cx + length] = 0; } else if (cy < line_max - 1) { strcat(bunsyou[cy], bunsyou[cy + 1]); for (i = cy + 2; i < line_max; i++) { strcpy(bunsyou[i - 1], bunsyou[i]); } line_max--; } } void k(void) { int i; if (cx >= strlen(bunsyou[cy])) { if (cy < line_max - 1) { strcat(bunsyou[cy], bunsyou[cy + 1]); for (i = cy + 2; i < line_max; i++) { strcpy(bunsyou[i - 1], bunsyou[i]); } line_max--; buffer[0] = '\n'; buffer[1] = 0; } } else { strcpy(buffer, &bunsyou[cy][cx]); bunsyou[cy][cx] = 0; } } void y(void) { int i, length; if (buffer[0] == 0) return; if (buffer[0] == '\n') { for (i = line_max; i > cy + 1; i--) { strcpy(bunsyou[i], bunsyou[i - 1]); } line_max++; strcpy(bunsyou[cy + 1], &bunsyou[cy][cx]); bunsyou[cy][cx] = 0; cy++; cx = 0; } else { length = strlen(buffer); memmove(&bunsyou[cy][cx + length], &bunsyou[cy][cx], strlen(&bunsyou[cy][cx]) + 1); memmove(&bunsyou[cy][cx], buffer, length); cx += length; } } int main(void) { char *cp; char cmd[10]; int i; line_max = 0; while (1) { fgets(bunsyou[line_max], sizeof(bunsyou[line_max]), stdin); cp = strchr(bunsyou[line_max], '\n'); if (cp) *cp = 0; if (strcmp(bunsyou[line_max], "END_OF_TEXT") == 0) { bunsyou[line_max][0] = 0; break; } else { line_max++; } } while (1) { scanf("%s", cmd); if (cmd[0] == '-') break; switch (cmd[0]) { case 'a': a(); break; case 'e': e(); break; case 'p': p(); break; case 'n': n(); break; case 'f': f(); break; case 'b': b(); break; case 'd': d(); break; case 'k': k(); break; case 'y': y(); break; } } for (i = 0; i < line_max; i++) puts(bunsyou[i]); return 0; }
[["-", 0, 2, 3, 4, 0, 69, 71, 16, 17, 33], ["-", 0, 2, 3, 4, 0, 69, 71, 16, 12, 13], ["+", 0, 2, 3, 4, 0, 69, 71, 16, 17, 33], ["+", 0, 2, 3, 4, 0, 69, 71, 16, 12, 13]]
0
883
#include <iostream> #include <list> #include <vector> #define POS_MAX 10000 #define GRID 40 #define GRID_NUM 250 // POS_MAX / GRID struct pos_t { pos_t() {} pos_t(int _x, int _y) : x(_x), y(_y) {} pos_t(std::istream *is) { (*is) >> x >> y; } int x, y; }; void process(const int AN, const int BN, const int R) { std::vector<pos_t> as(AN), bs(BN); for (int i = 0; i < AN; ++i) as[i] = pos_t(&std::cin); for (int i = 0; i < BN; ++i) bs[i] = pos_t(&std::cin); std::list<pos_t *> bs_grid[GRID_NUM][GRID_NUM]; for (int i = 0; i < BN; ++i) { int gx(bs[i].x / GRID), gy(bs[i].y / GRID); bs_grid[gx][gy].push_back(&bs[i]); } int sum(0); for (int i = 0; i < AN; ++i) { pos_t &a = as[i]; int gx(as[i].x / GRID), gy(as[i].y / GRID); for (int dx = -1; dx <= 1; ++dx) { int tx(gx + dx); if (tx < 0 or tx >= GRID_NUM) continue; for (int dy = -1; dy <= 1; ++dy) { int ty(gy + dy); if (ty < 0 or ty >= GRID_NUM) continue; std::list<pos_t *> &g = bs_grid[tx][ty]; std::list<pos_t *>::iterator it; for (it = g.begin(); it != g.end(); ++it) { int dx((*it)->x - a.x), dy((*it)->y - a.y); if (dx * dx + dy * dy < 16 * R * R) ++sum; } } } } std::cout << sum << std::endl; } int main() { int AN, BN, R; do { std::cin >> AN >> BN >> R; if (AN > 0 and BN > 0 and R > 0) process(AN, BN, R); } while (AN > 0); return 0; }
#include <iostream> #include <list> #include <vector> #define POS_MAX 10000 #define GRID 40 #define GRID_NUM 250 // POS_MAX / GRID struct pos_t { pos_t() {} pos_t(int _x, int _y) : x(_x), y(_y) {} pos_t(std::istream *is) { (*is) >> x >> y; } int x, y; }; void process(const int AN, const int BN, const int R) { std::vector<pos_t> as(AN), bs(BN); for (int i = 0; i < AN; ++i) as[i] = pos_t(&std::cin); for (int i = 0; i < BN; ++i) bs[i] = pos_t(&std::cin); std::list<pos_t *> bs_grid[GRID_NUM][GRID_NUM]; for (int i = 0; i < BN; ++i) { int gx(bs[i].x / GRID), gy(bs[i].y / GRID); bs_grid[gx][gy].push_back(&bs[i]); } int sum(0); for (int i = 0; i < AN; ++i) { pos_t &a = as[i]; int gx(as[i].x / GRID), gy(as[i].y / GRID); for (int dx = -1; dx <= 1; ++dx) { int tx(gx + dx); if (tx < 0 or tx >= GRID_NUM) continue; for (int dy = -1; dy <= 1; ++dy) { int ty(gy + dy); if (ty < 0 or ty >= GRID_NUM) continue; std::list<pos_t *> &g = bs_grid[tx][ty]; std::list<pos_t *>::iterator it; for (it = g.begin(); it != g.end(); ++it) { int dx((*it)->x - a.x), dy((*it)->y - a.y); if (dx * dx + dy * dy <= 16 * R * R) ++sum; } } } } std::cout << sum << std::endl; } int main() { int AN, BN, R; do { std::cin >> AN >> BN >> R; if (AN > 0 and BN > 0 and R > 0) process(AN, BN, R); } while (AN > 0); return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
526
#include <bits/stdc++.h> using namespace std; int A, B, R; vector<int> P[11000]; bool check(int x, int y, int x1, int y1) { if ((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y) > 16 * R * R) return false; return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> A >> B >> R && (A || B || R)) { for (int i = 0; i <= 10000; i++) P[i].clear(); for (int i = 0; i < A; i++) { int x, y; cin >> x >> y; P[x].push_back(y); } for (int i = 0; i <= 10000; i++) sort(P[i].begin(), P[i].end()); int res = 0; for (int i = 0; i < B; i++) { int x, y; cin >> x >> y; // cout << x << " " << y << " : " << endl; for (int nx = x - 4 * R; nx <= x + 4 * R; nx++) { int id = lower_bound(P[nx].begin(), P[nx].end(), y - 4 * R) - P[nx].begin(); for (int j = id; j < P[nx].size(); j++) { // cout << nx << " "<< P[nx][j] << endl; if (P[nx][j] > y + 4 * R) break; if (check(nx, P[nx][j], x, y)) res++; } } } cout << res << endl; } }
#include <bits/stdc++.h> using namespace std; int A, B, R; vector<int> P[11000]; bool check(int x, int y, int x1, int y1) { if ((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y) > 16 * R * R) return false; return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> A >> B >> R && (A || B || R)) { for (int i = 0; i <= 10000; i++) P[i].clear(); for (int i = 0; i < A; i++) { int x, y; cin >> x >> y; P[x].push_back(y); } for (int i = 0; i <= 10000; i++) sort(P[i].begin(), P[i].end()); int res = 0; for (int i = 0; i < B; i++) { int x, y; cin >> x >> y; // cout << x << " " << y << " : " << endl; for (int nx = max(0, x - 4 * R); nx <= x + 4 * R; nx++) { int id = lower_bound(P[nx].begin(), P[nx].end(), y - 4 * R) - P[nx].begin(); for (int j = id; j < P[nx].size(); j++) { // cout << nx << " "<< P[nx][j] << endl; if (P[nx][j] > y + 4 * R) break; if (check(nx, P[nx][j], x, y)) res++; } } } cout << res << endl; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 2, 63, 22], ["+", 10, 43, 49, 50, 51, 2, 3, 4, 0, 24], ["+", 10, 43, 49, 50, 51, 2, 3, 4, 0, 13], ["+", 10, 43, 49, 50, 51, 2, 3, 4, 0, 21], ["+", 10, 43, 49, 50, 51, 2, 3, 4, 0, 25]]
1
378
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define watch(a) \ { cout << #a << " = " << a << endl; } template <class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } typedef long long ll; int const inf = 1 << 29; int const Max = 10010; set<int> ys[Max]; constexpr int sq(int x) { return x * x; } int main() { for (int N, M, R; cin >> N >> M >> R && N;) { rep(i, Max) ys[i].clear(); rep(i, N) { int x, y; scanf("%d%d", &x, &y); ys[x].insert(y); } int ans = 0; for (; M--;) { int x, y; scanf("%d%d", &x, &y); REP(px, max(0, x - 4 * R), min(Max, x + 4 * R + 1)) { int py = y - 4 * R; auto iter = ys[px].lower_bound(py); while (iter != ys[px].end() && *iter <= y + 4 * R) { ans += sq(px - x) + sq(*iter - y) < 16 * R * R; iter++; } } } cout << ans << endl; } return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define watch(a) \ { cout << #a << " = " << a << endl; } template <class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } typedef long long ll; int const inf = 1 << 29; int const Max = 10010; set<int> ys[Max]; constexpr int sq(int x) { return x * x; } int main() { for (int N, M, R; cin >> N >> M >> R && N;) { rep(i, Max) ys[i].clear(); rep(i, N) { int x, y; scanf("%d%d", &x, &y); ys[x].insert(y); } int ans = 0; for (; M--;) { int x, y; scanf("%d%d", &x, &y); REP(px, max(0, x - 4 * R), min(Max, x + 4 * R + 1)) { const int py = y - 4 * R; auto iter = ys[px].lower_bound(py); while (iter != ys[px].end() && *iter <= y + 4 * R) { ans += sq(px - x) + sq(*iter - y) <= 16 * R * R; iter++; } } } cout << ans << endl; } return 0; }
[["+", 12, 83, 0, 83, 0, 42, 0, 11, 31, 22], ["-", 0, 83, 0, 27, 28, 11, 12, 16, 17, 18], ["+", 0, 83, 0, 27, 28, 11, 12, 16, 17, 19]]
1
424
#include <bits/stdc++.h> #define F 110000 using namespace std; vector<int> v[F]; int a, b, x, y, r, i; int D(int t) { int e = 0; while (e * e <= r * r - t * t) e++; return e - 1; } int main() { while (cin >> a >> b >> r && a + b) for (int i = 0; i < F; i++) v[i].clear(); r *= 4; for (i = 0; i < a; i++) { cin >> x >> y; v[x].push_back(y); } for (i = 0; i < a; i++) { sort(v[i].begin(), v[i].end()); } int n = 0; while (b--) { cin >> x >> y; for (i = max(0, x - r); i <= min(F - 100, x + r); i++) { n += lower_bound(v[i].begin(), v[i].end(), y + D(x - i) + 1) - lower_bound(v[i].begin(), v[i].end(), y - D(x - i)); } cout << n << endl; } return 0; }
#include <bits/stdc++.h> #define F 110000 using namespace std; vector<int> v[F]; int a, b, x, y, r, i; int D(int t) { int e = 0; while (e * e <= r * r - t * t) e++; return e - 1; } int main() { while (cin >> a >> b >> r && a + b) { for (int i = 0; i < F; i++) v[i].clear(); r *= 4; for (i = 0; i < a; i++) { cin >> x >> y; v[x].push_back(y); } for (i = 0; i < a; i++) sort(v[i].begin(), v[i].end()); int n = 0; while (b--) { cin >> x >> y; for (i = max(0, x - r); i <= min(F - 100, x + r); i++) n += lower_bound(v[i].begin(), v[i].end(), y + D(x - i) + 1) - lower_bound(v[i].begin(), v[i].end(), y - D(x - i)); } cout << n << endl; } return 0; }
[["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 45], ["-", 0, 14, 8, 9, 0, 7, 8, 9, 0, 45], ["-", 0, 14, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 52, 8, 9, 0, 7, 8, 9, 0, 45]]
1
302
#include <algorithm> #include <cassert> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i) #define FOREACH(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() inline int sq(int x) { return x * x; } vector<int> ships[10000]; int main() { int an, bn, r; while (scanf("%d%d%d", &an, &bn, &r), an | bn) { REP(x, 10000) { ships[x].clear(); } REP(i, an) { int x, y; scanf("%d%d", &x, &y); ships[x].push_back(y); } REP(x, 10000) { sort(ALL(ships[x])); } int ans = 0; REP(i, bn) { int x, y; scanf("%d%d", &x, &y); FOREQ(xx, max(0, x - 3 * r), min(x + 3 * r, 9999)) { vector<int>::iterator first = lower_bound(ALL(ships[xx]), y - 3 * r); vector<int>::iterator last = upper_bound(ALL(ships[xx]), y + 3 * r); for (vector<int>::iterator it = first; it != last; ++it) { int yy = *it; if (sq(x - xx) + sq(y - yy) <= sq(3 * r)) { ++ans; } } } } printf("%d\n", ans); } }
#include <algorithm> #include <cassert> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i) #define FOREACH(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() inline int sq(int x) { return x * x; } vector<int> ships[10000]; int main() { int an, bn, r; while (scanf("%d%d%d", &an, &bn, &r), an | bn) { REP(x, 10000) { ships[x].clear(); } REP(i, an) { int x, y; scanf("%d%d", &x, &y); ships[x].push_back(y); } REP(x, 10000) { sort(ALL(ships[x])); } int ans = 0; REP(i, bn) { int x, y; scanf("%d%d", &x, &y); FOREQ(xx, max(0, x - 4 * r), min(x + 4 * r, 9999)) { vector<int>::iterator first = lower_bound(ALL(ships[xx]), y - 4 * r); vector<int>::iterator last = upper_bound(ALL(ships[xx]), y + 4 * r); for (vector<int>::iterator it = first; it != last; ++it) { int yy = *it; if (sq(x - xx) + sq(y - yy) <= sq(4 * r)) { ++ans; } } } } printf("%d\n", ans); } }
[["-", 0, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["+", 0, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["-", 51, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["+", 51, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["-", 51, 16, 12, 2, 3, 4, 0, 16, 31, 13], ["+", 51, 16, 12, 2, 3, 4, 0, 16, 31, 13]]
1
376
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; int main() { int an, bn, r, x, y; while (scanf("%d%d%d", &an, &bn, &r), an != 0) { r *= 4; vector<vector<double>> pos(10000); for (int i = 0; i < an; ++i) { scanf("%d%d", &x, &y); pos[x].push_back(y); } for (int i = 0; i < 10000; ++i) { sort(pos[i].begin(), pos[i].end()); } int cnt = 0; for (int i = 0; i < bn; ++i) { scanf("%d%d", &x, &y); int last = min(y + r, 9999); for (int j = max(0, y - r); j <= last; ++j) { double d = sqrt(r * r - (y - j) * (y - j)); cnt += upper_bound(pos[j].begin(), pos[j].end(), y + d) - lower_bound(pos[j].begin(), pos[j].end(), y - d); } } printf("%d\n", cnt); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; int main() { int an, bn, r, x, y; while (scanf("%d%d%d", &an, &bn, &r), an != 0) { r *= 4; vector<vector<double>> pos(10000); for (int i = 0; i < an; ++i) { scanf("%d%d", &x, &y); pos[x].push_back(y); } for (int i = 0; i < 10000; ++i) { sort(pos[i].begin(), pos[i].end()); } int cnt = 0; for (int i = 0; i < bn; ++i) { scanf("%d%d", &x, &y); int last = min(x + r, 9999); for (int j = max(0, x - r); j <= last; ++j) { double d = sqrt(r * r - (x - j) * (x - j)); cnt += upper_bound(pos[j].begin(), pos[j].end(), y + d) - lower_bound(pos[j].begin(), pos[j].end(), y - d); } } printf("%d\n", cnt); } }
[["-", 49, 50, 51, 2, 3, 4, 0, 16, 31, 22], ["+", 49, 50, 51, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 16, 12, 16, 31, 23, 0, 16, 31, 22], ["+", 0, 16, 12, 16, 31, 23, 0, 16, 31, 22], ["-", 0, 16, 12, 16, 12, 23, 0, 16, 31, 22], ["+", 0, 16, 12, 16, 12, 23, 0, 16, 31, 22]]
1
298
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) const double EPS = 1e-10; const double PI = acos(-1.0); const int MAX = 10000; int main() { int an, bn, r; while (cin >> an >> bn >> r, an && bn) { static vvi a(MAX, vi()); REP(i, an) { int x, y; cin >> x >> y; a[x].push_back(y); } REP(i, MAX) { sort(ALL(a[i])); } ll ans = 0; REP(i, bn) { int xb, yb; cin >> xb >> yb; FOR(dx, -2 * r, 2 * r + 1) { int x = xb + dx; if (x >= 0 && x < MAX) { vi::iterator it = lower_bound(ALL(a[x]), yb - 2 * r); for (; it != a[x].end() && *it - yb <= 2 * r; it++) { int dy = *it - yb; if (sqrt((double)(dx * dx + dy * dy)) < 4 * r + EPS) { ans++; } } } } } cout << ans << endl; } }
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) const double EPS = 1e-10; const double PI = acos(-1.0); const int MAX = 10000; int main() { int an, bn, r; while (cin >> an >> bn >> r, an && bn) { vvi a(MAX, vi()); REP(i, an) { int x, y; cin >> x >> y; a[x].push_back(y); } REP(i, MAX) { sort(ALL(a[i])); } ll ans = 0; REP(i, bn) { int xb, yb; cin >> xb >> yb; FOR(dx, -4 * r, 4 * r + 1) { int x = xb + dx; if (x >= 0 && x < MAX) { vi::iterator it = lower_bound(ALL(a[x]), yb - 4 * r); for (; it != a[x].end() && *it - yb <= 4 * r; it++) { int dy = *it - yb; if (sqrt((double)(dx * dx + dy * dy)) < 4 * r + EPS) { ans++; } } } } } cout << ans << endl; } }
[["-", 0, 52, 8, 9, 0, 43, 0, 114, 0, 115], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 13], ["-", 0, 2, 3, 4, 0, 16, 31, 16, 31, 13], ["+", 0, 2, 3, 4, 0, 16, 31, 16, 31, 13], ["-", 51, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["+", 51, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["-", 0, 7, 15, 16, 12, 16, 12, 16, 31, 13], ["+", 0, 7, 15, 16, 12, 16, 12, 16, 31, 13]]
1
444
#include <cmath> #include <iostream> using namespace std; const double eps = 1.0e-9; int main() { while (true) { unsigned long long int n, r, ans = 0; cin >> n; if (n == 0) break; unsigned long long int tmp = n / 2LL; unsigned long long int rn = (long long int)(sqrt(n / 2) + eps) + 1; // cout << " RN : " << rn << endl; for (unsigned long long int i = 2; i <= rn; ++i) { if (n % (2 * (i - 1)) == 0) tmp += n / (2 * (i - 1)); else tmp += n / (2 * (i - 1)) + 1; } tmp *= 2; tmp -= rn * rn; ans = 8 * (n + tmp); cout << ans << endl; } return 0; }
#include <cmath> #include <iostream> using namespace std; const double eps = 1.0e-9; int main() { while (true) { unsigned long long int n, r, ans = 0; cin >> n; if (n == 0) break; unsigned long long int tmp = n / 2LL; unsigned long long int rn = (long long int)ceil(sqrt(n / 2) - eps); // cout << " RN : " << rn << endl; for (unsigned long long int i = 2; i <= rn; ++i) { if (n % (2 * (i - 1)) == 0) tmp += n / (2 * (i - 1)); else tmp += n / (2 * (i - 1)) + 1; } tmp *= 2; tmp -= rn * rn; ans = 8 * (n + tmp); cout << ans << endl; } return 0; }
[["+", 0, 43, 49, 50, 51, 74, 51, 2, 63, 22], ["-", 51, 16, 31, 74, 51, 23, 0, 16, 17, 72], ["+", 51, 74, 51, 2, 3, 4, 0, 16, 17, 33], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 17, 72], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13]]
1
180
while 1: n=int(input()) if n==0:break a=0;i=1;b=n//2 while i*i<b:a+=((b-1)/i+1)-i-1;i+=1 a=int((a+b-1)*2+i) print(8*(a+n))
while 1: n=int(input()) if n==0:break a=0;i=1;b=n//2 while i*i<b:a+=((b-1)//i+1)-i-1;i+=1 a=(a+b-1)*2+i print(8*(a+n))
[["-", 31, 657, 31, 23, 0, 657, 31, 657, 17, 85], ["+", 31, 657, 31, 23, 0, 657, 31, 657, 17, 676], ["-", 8, 196, 0, 1, 0, 662, 12, 652, 63, 22], ["-", 3, 4, 0, 657, 31, 657, 31, 23, 0, 24], ["-", 0, 1, 0, 662, 12, 652, 3, 4, 0, 25]]
5
84
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null && !line.isEmpty()) { int k = Integer.parseInt(line); if (k == 0) { break; } int sum = 0; StringTokenizer st = new StringTokenizer(br.readLine()); while (st.hasMoreTokens()) { sum += Integer.parseInt(st.nextToken()); } sb.append(sum / (k - 1) + '\n'); } System.out.print(sb.toString()); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null && !line.isEmpty()) { int k = Integer.parseInt(line); if (k == 0) { break; } int sum = 0; StringTokenizer st = new StringTokenizer(br.readLine()); while (st.hasMoreTokens()) { sum += Integer.parseInt(st.nextToken()); } sb.append(sum / (k - 1)).append('\n'); } System.out.print(sb.toString()); } }
[["-", 0, 1, 0, 492, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 492, 500, 492, 3, 4, 0, 25], ["+", 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]]
3
188
#include <stdio.h> int main() { int k, c, i, total, kai, t; for (t = 0; t < 100; t++) { scanf("%d", &k); if (k == 0) break; total = 0; for (i = 0; i < (k * k - k) / 2; i++) { scanf("%d", &c); total += c; } kai = total / 2; printf("%d\n", kai); } return 0; }
#include <stdio.h> int main() { int k, c, i, total, kai, t; for (t = 0; t < 100; t++) { scanf("%d", &k); if (k == 0) break; total = 0; for (i = 0; i < (k * k - k) / 2; i++) { scanf("%d", &c); total += c; } kai = total / (k - 1); printf("%d\n", kai); } return 0; }
[["-", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 31, 22], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 17, 33], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 12, 13], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 25]]
0
114
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 29; typedef int Weight; typedef vector<Weight> Array; typedef vector<Array> Matrix; // ツ経ツ路ツつ「ツづァツづ按つ「ツづつォ const int M = 20; Weight best[1 << M][M]; Weight shortestHamiltonPath(Matrix w, int s) { int n = w.size(); int N = 1 << n; REP(S, N) REP(i, n) best[S][i] = INF; best[1 << s][s] = 0; REP(S, N) REP(i, n) if (S & (1 << i)) REP(j, n) best[S | (1 << j)][j] = min(best[S | (1 << j)][j], best[S][i] + w[i][j]); Weight res = INF; REP(i, n) { res = min(res, best[N - 1][i] + w[i][s]); } return res; } int main() { int n; while (cin >> n, n) { map<string, int> mp[n]; map<string, int> mi; REP(i, n) { int k; cin >> k; REP(j, k) { string s; int v; cin >> s >> v; mp[i][s] = v; if (mi.count(s) == 0) mi[s] = v; else if (mi[s] > v) mi[s] = v; } } int q; cin >> q; int mask[n]; memset(mask, 0, sizeof(mask)); int ans1 = 0; bool dame = 0; REP(i, q) { string s; cin >> s; if (mi.count(s) == 0) { dame = 1; continue; } ans1 += mi[s]; REP(j, n) { if (mp[j][s] == mi[s]) mask[j] |= 1 << i; } } int m; cin >> m; Matrix g(n + 1, Array(n + 1, INF)); REP(i, m) { int s, t, d; cin >> s >> t >> d; g[s][t] = d; g[t][s] = d; } if (dame) { cout << "impossible" << endl; continue; } // REP(i, n+1) { // REP(j, n+1) { // cout << g[i][j] << " "; // } // cout << endl; // }cout << endl; int res = INF; REP(S, 1 << n) { int buy = 0; int num = 0; REP(i, n) { if (S >> i & 1) { buy |= mask[i]; num++; } } if (buy != (1 << q) - 1) continue; Matrix sub(num + 1, Array(num + 1, INF)); int y = 0; REP(i, n) { if (!(S >> i & 1)) continue; sub[y + 1][0] = g[i + 1][0]; sub[0][y + 1] = g[0][i + 1]; int x = 0; REP(j, n) { if (!(S >> j & 1)) continue; sub[y + 1][x + 1] = g[i + 1][j + 1]; x++; } y++; } // REP(i, num+1) { // REP(j, num+1) { // cout << sub[i][j] << " "; // } // cout << endl; // }cout << endl; res = min(res, shortestHamiltonPath(sub, 0)); } if (res == INF) { cout << "impossible" << endl; } else { cout << ans1 << " " << res << endl; } } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 29; typedef int Weight; typedef vector<Weight> Array; typedef vector<Array> Matrix; // ツ経ツ路ツつ「ツづァツづ按つ「ツづつォ const int M = 20; Weight best[1 << M][M]; Weight shortestHamiltonPath(Matrix w, int s) { int n = w.size(); int N = 1 << n; REP(S, N) REP(i, n) best[S][i] = INF; best[1 << s][s] = 0; REP(S, N) REP(k, n) REP(i, n) if (S & (1 << i)) REP(j, n) best[S | (1 << j)][j] = min(best[S | (1 << j)][j], best[S][i] + w[i][j]); Weight res = INF; REP(i, n) { res = min(res, best[N - 1][i] + w[i][s]); } return res; } int main() { int n; while (cin >> n, n) { map<string, int> mp[n]; map<string, int> mi; REP(i, n) { int k; cin >> k; REP(j, k) { string s; int v; cin >> s >> v; mp[i][s] = v; if (mi.count(s) == 0) mi[s] = v; else if (mi[s] > v) mi[s] = v; } } int q; cin >> q; int mask[n]; memset(mask, 0, sizeof(mask)); int ans1 = 0; bool dame = 0; REP(i, q) { string s; cin >> s; if (mi.count(s) == 0) { dame = 1; continue; } ans1 += mi[s]; REP(j, n) { if (mp[j][s] == mi[s]) mask[j] |= 1 << i; } } int m; cin >> m; Matrix g(n + 1, Array(n + 1, INF)); REP(i, m) { int s, t, d; cin >> s >> t >> d; g[s][t] = d; g[t][s] = d; } if (dame) { cout << "impossible" << endl; continue; } // REP(i, n+1) { // REP(j, n+1) { // cout << g[i][j] << " "; // } // cout << endl; // }cout << endl; int res = INF; REP(S, 1 << n) { int buy = 0; int num = 0; REP(i, n) { if (S >> i & 1) { buy |= mask[i]; num++; } } if (buy != (1 << q) - 1) continue; Matrix sub(num + 1, Array(num + 1, INF)); int y = 0; REP(i, n) { if (!(S >> i & 1)) continue; sub[y + 1][0] = g[i + 1][0]; sub[0][y + 1] = g[0][i + 1]; int x = 0; REP(j, n) { if (!(S >> j & 1)) continue; sub[y + 1][x + 1] = g[i + 1][j + 1]; x++; } y++; } // REP(i, num+1) { // REP(j, num+1) { // cout << sub[i][j] << " "; // } // cout << endl; // }cout << endl; res = min(res, shortestHamiltonPath(sub, 0)); } if (res == INF) { cout << "impossible" << endl; } else { cout << ans1 << " " << res << endl; } } }
[["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35], ["+", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24]]
1
848
#include <algorithm> #include <climits> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; struct Tag { int pos, pat, cost; Tag() : pos(0), pat(0), cost(0) {} Tag(int p, int pat, int c) : pos(p), pat(pat), cost(c) {} bool operator<(const Tag &other) const { return cost > other.cost; } }; struct Dict { map<string, int> dict; int id; Dict() : id(0) {} int get(const string &str) { if (dict.count(str)) return dict[str]; return dict[str] = id++; } }; int main() { cin.tie(0); ios::sync_with_stdio(0); while (true) { int N; cin >> N; if (!N) break; map<string, int> best_price; vector<map<string, int>> store(N + 1); for (int i = 1; i <= N; ++i) { int K; cin >> K; while (K--) { string name; int price; cin >> name >> price; store[i][name] = price; if (best_price.count(name) == 0 || best_price[name] > price) { best_price[name] = price; } } } int Q; cin >> Q; bool impossible = false; int ans_value = 0; vector<int> mask(N + 1, 0); Dict dict; for (int i = 0; i < Q; ++i) { string name; cin >> name; if (best_price.count(name) == 0) { impossible = true; } const int id = dict.get(name); ans_value += best_price[name]; for (int j = 1; j <= N; ++j) { if (store[j].count(name) && store[j][name] == best_price[name]) { mask[j] |= (1 << id); } } } int M; cin >> M; vector<vector<pair<int, int>>> road(N + 1); while (M--) { int S, T, D; cin >> S >> T >> D; road[S].push_back(make_pair(T, D)); road[T].push_back(make_pair(S, D)); } if (true || impossible) { cout << "impossible" << endl; } else { ++N; vector<vector<int>> memo(N, vector<int>(1 << N, INT_MAX)); priority_queue<Tag> q; q.push(Tag(0, 0, 0)); while (!q.empty()) { Tag t = q.top(); q.pop(); if (memo[t.pos][t.pat] < t.cost) continue; memo[t.pos][t.pat] = t.cost; if (t.pos == 0 && t.pat == (1 << Q) - 1) break; for (vector<pair<int, int>>::const_iterator it = road[t.pos].begin(); it != road[t.pos].end(); ++it) { const int npos = it->first; const int npat = t.pat | mask[npos]; const int ncost = t.cost + it->second; if (memo[npos][npat] > ncost) { memo[npos][npat] = ncost; q.push(Tag(npos, npat, ncost)); } } } cout << ans_value << ' ' << memo[0][(1 << Q) - 1] << endl; } } return 0; }
#include <algorithm> #include <climits> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; struct Tag { int pos, pat, cost; Tag() : pos(0), pat(0), cost(0) {} Tag(int p, int pat, int c) : pos(p), pat(pat), cost(c) {} bool operator<(const Tag &other) const { return cost > other.cost; } }; struct Dict { map<string, int> dict; int id; Dict() : id(0) {} int get(const string &str) { if (dict.count(str)) return dict[str]; return dict[str] = id++; } }; int main() { cin.tie(0); ios::sync_with_stdio(0); while (true) { int N; cin >> N; if (!N) break; map<string, int> best_price; vector<map<string, int>> store(N + 1); for (int i = 1; i <= N; ++i) { int K; cin >> K; while (K--) { string name; int price; cin >> name >> price; store[i][name] = price; if (best_price.count(name) == 0 || best_price[name] > price) { best_price[name] = price; } } } int Q; cin >> Q; bool impossible = false; int ans_value = 0; vector<int> mask(N + 1, 0); Dict dict; for (int i = 0; i < Q; ++i) { string name; cin >> name; if (best_price.count(name) == 0) { impossible = true; } const int id = dict.get(name); ans_value += best_price[name]; for (int j = 1; j <= N; ++j) { if (store[j].count(name) && store[j][name] == best_price[name]) { mask[j] |= (1 << id); } } } int M; cin >> M; vector<vector<pair<int, int>>> road(N + 1); while (M--) { int S, T, D; cin >> S >> T >> D; road[S].push_back(make_pair(T, D)); road[T].push_back(make_pair(S, D)); } if (impossible) { cout << "impossible" << endl; } else { ++N; vector<vector<int>> memo(N, vector<int>(1 << Q, INT_MAX)); priority_queue<Tag> q; q.push(Tag(0, 0, 0)); while (!q.empty()) { const Tag t = q.top(); q.pop(); if (memo[t.pos][t.pat] < t.cost) continue; memo[t.pos][t.pat] = t.cost; if (t.pos == 0 && t.pat == (1 << Q) - 1) break; for (vector<pair<int, int>>::const_iterator it = road[t.pos].begin(); it != road[t.pos].end(); ++it) { const int npos = it->first; const int npat = t.pat | mask[npos]; const int ncost = t.cost + it->second; if (memo[npos][npat] > ncost) { memo[npos][npat] = ncost; q.push(Tag(npos, npat, ncost)); } } } cout << ans_value << ' ' << memo[0][(1 << Q) - 1] << endl; } } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 146], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 106], ["-", 51, 4, 0, 2, 3, 4, 0, 16, 12, 22], ["+", 51, 4, 0, 2, 3, 4, 0, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 43, 0, 153, 0, 154]]
1
815
import java.util.*; public class Main { static Scanner kbd = new Scanner(System.in); public static void main(String[] args) { while (kbd.hasNext()) { int n = kbd.nextInt(); if (n != 0) { int[] p = new int[n]; int i; for (i = 0; i < n; i++) { p[i] = kbd.nextInt(); } int s = 0; i = 0; while (i < n) { while (i + 1 < n && p[i] + 1 == p[i + 1]) i++; if (s == 1) System.out.print(p[s]); else System.out.print(p[s] + "-" + p[i]); i++; s = i; if (i < n) System.out.print(" "); } System.out.println(); } } } }
import java.util.*; public class Main { static Scanner kbd = new Scanner(System.in); public static void main(String[] args) { while (kbd.hasNext()) { int n = kbd.nextInt(); if (n != 0) { int[] p = new int[n]; int i; for (i = 0; i < n; i++) { p[i] = kbd.nextInt(); } int s = 0; i = 0; while (i < n) { while (i + 1 < n && p[i] + 1 == p[i + 1]) i++; if (s == i) System.out.print(p[s]); else System.out.print(p[s] + "-" + p[i]); i++; s = i; if (i < n) System.out.print(" "); } System.out.println(); } } } }
[["-", 8, 196, 0, 57, 15, 15, 0, 16, 12, 499], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 12, 22]]
3
217
n, p, i, a[51]; main() { for (; ~scanf("%d", &n), n;) { for (p = i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 1; i <= n; i++) { if (a[i - 1] + 1 != a[i] || n) { if (i - p - 1) printf(n - i ? "%d-%d " : "%d-%d\n", a[p], a[i - 1]); else printf(n - i ? "%d " : "%d\n", a[p]); p = i; } } } exit(0); }
n, p, i, a[51]; main() { for (; ~scanf("%d", &n), n;) { for (p = i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 1; i <= n; i++) { if (a[i - 1] + 1 != a[i] || n - i == 0) { if (i - p - 1) printf(n - i ? "%d-%d " : "%d-%d\n", a[p], a[i - 1]); else printf(n - i ? "%d " : "%d\n", a[p]); p = i; } } } exit(0); }
[["+", 15, 23, 0, 16, 12, 16, 31, 16, 17, 33], ["+", 15, 23, 0, 16, 12, 16, 31, 16, 12, 22], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 17, 60], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13]]
0
166
n, p, i, a[51]; main() { for (; ~scanf("%d", &n), n;) { for (a[n] = p = i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < n; i++) { if (a[i] + 1 != a[i + 1]) { if (i - p + 1) printf(n - i - 1 ? "%d-%d " : "%d-%d\n", a[p], a[i]); else printf(n - i - 1 ? "%d " : "%d\n", a[p]); p = i + 1; } } } exit(0); }
n, p, i, a[51]; main() { for (; ~scanf("%d", &n), n;) { for (a[n] = p = i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < n; i++) { if (a[i] + 1 != a[i + 1]) { if (i - p) printf(n - i - 1 ? "%d-%d " : "%d-%d\n", a[p], a[i]); else printf(n - i - 1 ? "%d " : "%d\n", a[p]); p = i + 1; } } } exit(0); }
[["-", 64, 9, 0, 57, 15, 23, 0, 16, 17, 72], ["-", 64, 9, 0, 57, 15, 23, 0, 16, 12, 13]]
0
173
n, p, i, a[51]; main() { for (; ~scanf("%d", &n), n;) { for (a[n] = p = i = 0; i < n; i++) scanf("%d", a + i); for (i = 1; i <= n; i++) (a[i - 1] - a[i] + 1) && printf(i - p - 1 ? n - i ? "%d-%d " : "%d-%d\n" : n - i ? "%d " : "%d\n", a[p], a[i - 1]), p = i; } exit(0); }
n, p, i, a[51]; main() { for (; ~scanf("%d", &n), n;) { for (a[n] = p = i = 0; i < n; i++) scanf("%d", a + i); for (i = 1; i <= n; i++) if (a[i - 1] - a[i] + 1) printf(i - p - 1 ? n - i ? "%d-%d " : "%d-%d\n" : n - i ? "%d " : "%d\n", a[p], a[i - 1]), p = i; } exit(0); }
[["+", 0, 7, 8, 9, 0, 7, 8, 57, 0, 121], ["-", 0, 7, 8, 1, 0, 34, 31, 16, 17, 98]]
0
152
#include <stdio.h> int n, p, a[50]; int main() { while (~scanf("%d", &n), n) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); p = 0; for (int i = 1; i < n; i++) { if (a[i - 1] + 1 != a[i]) { if (p - i != 1) printf("%d-%d ", a[p], a[i - 1]); else printf("%d ", a[p]); p = i; } } if (n - p != 1) printf("%d-%d\n", a[p], a[n - 1]); else printf("%d\n", a[p]); } return 0; }
#include <stdio.h> int n, p, a[50]; int main() { while (~scanf("%d", &n), n) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); p = 0; for (int i = 1; i < n; i++) { if (a[i - 1] + 1 != a[i]) { if (i - p != 1) printf("%d-%d ", a[p], a[i - 1]); else printf("%d ", a[p]); p = i; } } if (n - p != 1) printf("%d-%d\n", a[p], a[n - 1]); else printf("%d\n", a[p]); } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 33], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 33], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 22]]
1
189
#include "bits/stdc++.h" using namespace std; //#define int long long #define DEBUG 1 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() #define dump(o) \ if (DEBUG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DEBUG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; static const int MOD = 1e9 + 7; signed main() { for (int n; cin >> n && n;) { vector<int> a(n); string ans = ""; rep(i, 0, n) cin >> a[i]; rep(i, 0, n) { int cnt = 0; rep(j, i, n - 1) { if (a[j] + 1 == a[j + 1]) cnt++; else break; } if (cnt >= 2) { ans += to_string(a[i]) + "-" + to_string(a[i + cnt]) + " "; } else { rep(j, i, i + cnt + 1) { ans += to_string(a[j]) + " "; } } i += cnt; } cout << ans.substr(0, ans.size() - 1) << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; //#define int long long #define DEBUG 1 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() #define dump(o) \ if (DEBUG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DEBUG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; static const int MOD = 1e9 + 7; signed main() { for (int n; cin >> n && n;) { vector<int> a(n); string ans = ""; rep(i, 0, n) cin >> a[i]; rep(i, 0, n) { int cnt = 0; rep(j, i, n - 1) { if (a[j] + 1 == a[j + 1]) cnt++; else break; } if (cnt >= 1) { ans += to_string(a[i]) + "-" + to_string(a[i + cnt]) + " "; } else { rep(j, i, i + cnt + 1) { ans += to_string(a[j]) + " "; } } i += cnt; } cout << ans.substr(0, ans.size() - 1) << endl; } return 0; }
[["-", 0, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 0, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
295
#include <cstdio> #include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { int x[n]; int a = 0; for (int i = 0; i < n; i++) { cin >> x[i]; if (i > 0) { if (x[i] - 1 != x[i - 1]) { if (a != i - 1) { printf("%d-%d ", x[a], x[i - 1]); } else { cout << x[i - 1]; } a = i; } } } if (a != n - 1) { printf("%d-%d", x[a], x[n - 1]); } else { cout << x[n - 1]; } cout << endl; } }
#include <cstdio> #include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { int x[n]; int a = 0; for (int i = 0; i < n; i++) { cin >> x[i]; if (i > 0) { if (x[i] - 1 != x[i - 1]) { if (a != i - 1) { printf("%d-%d ", x[a], x[i - 1]); } else { cout << x[i - 1] << " "; } a = i; } } } if (a != n - 1) { printf("%d-%d", x[a], x[n - 1]); } else { cout << x[n - 1]; } cout << endl; } }
[["+", 75, 76, 0, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 9, 0, 1, 0, 16, 12, 5, 0, 62], ["+", 0, 9, 0, 1, 0, 16, 12, 5, 0, 6]]
1
177
/* * File: main.cpp * Author: 成诺 * * Created on 2011年8月27日, 下午12:34 */ #include <cstdio> #include <cstdlib> using namespace std; int main(int argc, char **argv) { int n; while (1 == scanf("%d", &n) && n) { int just; int t; bool need = false; for (int i = 0; i < n; ++i, just = t) { scanf("%d", &t); if (i == 0) printf("%d", t); else if (t == just + 1) { need = true; continue; } else { if (need) printf("-%d", just); printf(" %d", t); } } if (need) printf("-%d", just); printf("\n"); } return EXIT_SUCCESS; }
/* * File: main.cpp * Author: 成诺 * * Created on 2011年8月27日, 下午12:34 */ #include <cstdio> #include <cstdlib> using namespace std; int main(int argc, char **argv) { int n; while (1 == scanf("%d", &n) && n) { int just; int t; bool need = false; for (int i = 0; i < n; ++i, just = t) { scanf("%d", &t); if (i == 0) printf("%d", t); else if (t == just + 1) { need = true; continue; } else { if (need) printf("-%d", just); printf(" %d", t); need = false; } } if (need) printf("-%d", just); printf("\n"); } return EXIT_SUCCESS; }
[["+", 75, 76, 0, 9, 0, 1, 0, 11, 31, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 17, 32], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 12, 147], ["+", 0, 57, 75, 76, 0, 9, 0, 1, 0, 35]]
1
164
var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); while (true) { var n = Arr.shift() - 0; if (n == 0) break; var s = []; var sum = 0; for (var i = 0; i < n; i++) { var arr = (Arr.shift()).split(" ").map(Number); s.push(arr); sum += arr[0]; } s.sort(function(a, b) { a = a[1]; b = b[1]; return b - a; }); var result = "YES" for (var i = 0; i < s.length; i++) { var v = s[i]; if (sum > v[1]) { result = "NO"; break; } sum -= v[0]; } console.log(result); }
var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); while (true) { var n = Arr.shift() - 0; if (n == 0) break; var s = []; var sum = 0; for (var i = 0; i < n; i++) { var arr = (Arr.shift()).split(" ").map(Number); s.push(arr); sum += arr[0]; } s.sort(function(a, b) { a = a[1]; b = b[1]; return b - a; }); var result = "Yes" for (var i = 0; i < s.length; i++) { var v = s[i]; if (sum > v[1]) { result = "No"; break; } sum -= v[0]; } console.log(result); }
[["-", 8, 556, 0, 198, 0, 200, 51, 557, 0, 491], ["+", 8, 556, 0, 198, 0, 200, 51, 557, 0, 491], ["-", 64, 556, 0, 1, 0, 11, 12, 557, 0, 491], ["+", 64, 556, 0, 1, 0, 11, 12, 557, 0, 491]]
2
226
import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); a: while (true) { int N = sc.nextInt(); if (N == 0) break; TreeMap<Integer, Integer> map = new TreeMap<>(); for (int i = 0; i < N; i++) { int a = sc.nextInt(); int b = sc.nextInt(); if (map.get(b) != null) map.put(b, a + map.get(b)); map.put(b, a); } int c = 0; for (Entry<Integer, Integer> m : map.entrySet()) { c += m.getValue(); if (c > m.getKey()) { System.out.println("No"); continue a; } } System.out.println("Yes"); } } }
import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); a: while (true) { int N = sc.nextInt(); if (N == 0) break; TreeMap<Integer, Integer> map = new TreeMap<>(); for (int i = 0; i < N; i++) { int a = sc.nextInt(); int b = sc.nextInt(); if (map.get(b) != null) { map.put(b, a + map.get(b)); } else { map.put(b, a); } } int c = 0; for (Entry<Integer, Integer> m : map.entrySet()) { c += m.getValue(); if (c > m.getKey()) { System.out.println("No"); continue a; } } System.out.println("Yes"); } } }
[["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 95], ["+", 0, 7, 8, 196, 0, 57, 75, 196, 0, 45], ["+", 0, 7, 8, 196, 0, 57, 75, 196, 0, 46]]
3
229
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Main { private static void solve() { final Scanner scanner = new Scanner(System.in); while (true) { final int n = scanner.nextInt(); if (n == 0) { break; } int[][] islands = new int[n][2]; for (int i = 0; i < n; i++) { islands[i][0] = scanner.nextInt(); islands[i][1] = scanner.nextInt(); } Arrays.sort(islands, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { if (o1[1] == o2[1]) { return o1[0] == o2[0] ? 0 : (o1[0] > o2[0] ? 1 : -1); } else { return o1[1] > o2[1] ? 1 : -1; } } }); int sum = 0; boolean possible = true; for (int i = 0; i < n; i++) { sum += islands[i][0]; if (sum > islands[i][1]) { possible = false; } } System.out.println(possible ? "YES" : "NO"); } } public static void main(String... args) { solve(); } }
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Main { private static void solve() { final Scanner scanner = new Scanner(System.in); while (true) { final int n = scanner.nextInt(); if (n == 0) { break; } int[][] islands = new int[n][2]; for (int i = 0; i < n; i++) { islands[i][0] = scanner.nextInt(); islands[i][1] = scanner.nextInt(); } Arrays.sort(islands, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { if (o1[1] == o2[1]) { return o1[0] == o2[0] ? 0 : (o1[0] > o2[0] ? 1 : -1); } else { return o1[1] > o2[1] ? 1 : -1; } } }); int sum = 0; boolean possible = true; for (int i = 0; i < n; i++) { sum += islands[i][0]; if (sum > islands[i][1]) { possible = false; } } System.out.println(possible ? "Yes" : "No"); } } public static void main(String... args) { solve(); } }
[["-", 0, 492, 3, 4, 0, 510, 64, 5, 0, 491], ["+", 0, 492, 3, 4, 0, 510, 64, 5, 0, 491], ["-", 0, 492, 3, 4, 0, 510, 75, 5, 0, 491], ["+", 0, 492, 3, 4, 0, 510, 75, 5, 0, 491]]
3
315
import java.util.Scanner; public class Main { public static void main(String arg[]) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); if (n == 0) return; int a[] = new int[n]; int b[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); b[i] = in.nextInt(); } for (int i = 0; i < n; i++) { int m = i; for (int j = i; j < n; j++) { if (a[j] < a[m]) m = j; } int tmp = b[i]; b[i] = b[m]; b[m] = tmp; tmp = a[i]; a[i] = a[m]; a[m] = tmp; } boolean flag = true; int T = 0; int W = 0; for (int i = 0; i < n; i++) { T += a[i]; W = b[i]; if (T > W) flag = false; } if (flag) System.out.println("Yes"); else System.out.println("No"); } } }
import java.util.Scanner; public class Main { public static void main(String arg[]) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); if (n == 0) return; int a[] = new int[n]; int b[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); b[i] = in.nextInt(); } for (int i = 0; i < n; i++) { int m = i; for (int j = i; j < n; j++) { if (b[j] < b[m]) m = j; } int tmp = b[i]; b[i] = b[m]; b[m] = tmp; tmp = a[i]; a[i] = a[m]; a[m] = tmp; } boolean flag = true; int T = 0; int W = 0; for (int i = 0; i < n; i++) { T += a[i]; W = b[i]; if (T > W) flag = false; } if (flag) System.out.println("Yes"); else System.out.println("No"); } } }
[["-", 0, 57, 15, 15, 0, 16, 31, 504, 516, 22], ["+", 0, 57, 15, 15, 0, 16, 31, 504, 516, 22], ["-", 0, 57, 15, 15, 0, 16, 12, 504, 516, 22], ["+", 0, 57, 15, 15, 0, 16, 12, 504, 516, 22]]
3
306
#include <stdio.h> #include <stdlib.h> int N, I[30][2], i, h; int c(const void *a, const void *b) { int *x = (int *)a; int *y = (int *)b; return x[1] == y[1] ? x[0] - y[0] : x[1] - y[1]; } int main() { for (; scanf("%d", &N), N;) { for (i = 0; i < N; i++) scanf("%d%d", &I[i][0], &I[i][1]); qsort(I, N, sizeof(int[2]), c); for (i = h = 0; i < N && h + I[i][0] <= I[i][1];) h += I[i++][0]; puts(i == N ? "YES" : "NO"); } return 0; }
#include <stdio.h> #include <stdlib.h> int N, I[30][2], i, h; int c(const void *a, const void *b) { int *x = (int *)a; int *y = (int *)b; return x[1] == y[1] ? x[0] - y[0] : x[1] - y[1]; } int main() { for (; scanf("%d", &N), N;) { for (i = 0; i < N; i++) scanf("%d%d", &I[i][0], &I[i][1]); qsort(I, N, sizeof(int[2]), c); for (i = h = 0; i < N && h + I[i][0] <= I[i][1];) h += I[i++][0]; puts(i == N ? "Yes" : "No"); } return 0; }
[["-", 0, 2, 3, 4, 0, 41, 64, 5, 0, 6], ["+", 0, 2, 3, 4, 0, 41, 64, 5, 0, 6], ["-", 0, 2, 3, 4, 0, 41, 75, 5, 0, 6], ["+", 0, 2, 3, 4, 0, 41, 75, 5, 0, 6]]
0
224
#include <algorithm> #include <cstring> #include <ctype.h> #include <iostream> #include <math.h> #include <queue> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define INF 114514810 #define ll long long int main() { while (1) { int n; cin >> n; if (n == 0) return 0; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(b, a)); } sort(begin(v), end(v)); int w = 0; for (int i = 0; i < n; i++) { w += v[i].second; if (w > v[i].first) { cout << "NO" << endl; goto next; } } cout << "YES" << endl; next:; } }
#include <algorithm> #include <cstring> #include <ctype.h> #include <iostream> #include <math.h> #include <queue> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define INF 114514810 #define ll long long int main() { while (1) { int n; cin >> n; if (n == 0) return 0; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(b, a)); } sort(begin(v), end(v)); int w = 0; for (int i = 0; i < n; i++) { w += v[i].second; if (w > v[i].first) { cout << "No" << endl; goto next; } } cout << "Yes" << endl; next:; } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
208
#include <algorithm> #include <stdio.h> #include <utility> int main(void) { int n, sum; bool NGflag; // first... テ」ツ?敕」ツ?ョテ・ツウツカテ」ツ?ョテ・ツョツ敕」ツ?ョテゥツ?? // second... // テ」ツ?敕」ツ?ョテ・ツウツカテ」ツ?ョテヲツゥツ凝」ツ?ョティツ?静、ツケツ?・ツ環? std::pair<int, int> bridge[30]; while (true) { scanf("%d", &n); if (n == 0) { break; } // テ・ツ?・テ・ツ環崚」ツつ津・ツ渉療」ツ?妥・ツ渉姪」ツつ? for (int i = 0; i < n; i++) { scanf("%d%d", &bridge[i].first, &bridge[i].second); } std::sort(&bridge[0], &bridge[n]); // ティツィツ暗ァツョツ? sum = 0; NGflag = false; for (int i = 0; i < n; i++) { sum += bridge[i].first; if (sum > bridge[i].second) { NGflag = true; break; } } // テ・ツ?コテ・ツ環? if (NGflag) { printf("No\n"); } else { printf("Yes\n"); } } return 0; }
#include <algorithm> #include <stdio.h> #include <utility> int main(void) { int n, sum; bool NGflag; // first... // テ」ツ?敕」ツ?ョテ・ツウツカテ」ツ?ョテヲツゥツ凝」ツ?ョティツ?静、ツケツ?・ツ環? // second... テ」ツ?敕」ツ?ョテ・ツウツカテ」ツ?ョテ・ツョツ敕」ツ?ョテゥツ?? std::pair<int, int> bridge[30]; while (true) { scanf("%d", &n); if (n == 0) { break; } // テ・ツ?・テ・ツ環崚」ツつ津・ツ渉療」ツ?妥・ツ渉姪」ツつ? for (int i = 0; i < n; i++) { scanf("%d%d", &bridge[i].second, &bridge[i].first); } std::sort(&bridge[0], &bridge[n]); // ティツィツ暗ァツョツ? sum = 0; NGflag = false; for (int i = 0; i < n; i++) { sum += bridge[i].second; if (sum > bridge[i].first) { NGflag = true; break; } } // テ・ツ?コテ・ツ環? if (NGflag) { printf("No\n"); } else { printf("Yes\n"); } } return 0; }
[["-", 0, 2, 3, 4, 0, 66, 28, 118, 119, 120], ["+", 0, 2, 3, 4, 0, 66, 28, 118, 119, 120], ["-", 8, 9, 0, 1, 0, 11, 12, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 11, 12, 118, 119, 120], ["-", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120], ["+", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120]]
1
202
#include <algorithm> #include <iostream> using namespace std; int n; pair<int, int> d[25]; int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> d[i].first >> d[i].second; sort(d, d + n); int s = 0, f = 0; for (int i = 0; i < n; i++) { s += d[i].second; if (d[i].first < s) { f = 1; break; } } cout << (f ? "No" : "Yes") << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int n; pair<int, int> d[25]; int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> d[i].second >> d[i].first; sort(d, d + n); int s = 0, f = 0; for (int i = 0; i < n; i++) { s += d[i].second; if (d[i].first < s) { f = 1; break; } } cout << (f ? "No" : "Yes") << endl; } return 0; }
[["-", 8, 1, 0, 16, 31, 16, 12, 118, 119, 120], ["+", 8, 1, 0, 16, 31, 16, 12, 118, 119, 120], ["-", 0, 7, 8, 1, 0, 16, 12, 118, 119, 120], ["+", 0, 7, 8, 1, 0, 16, 12, 118, 119, 120]]
1
149
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; bool check(int n) { map<int, vector<P>> mp; // b - a, {(a, b)} rep(i, n) { int a, b; cin >> a >> b; mp[b - a].emplace_back(P(a, b)); } int sm = 0; for (auto pp : mp) { auto v = pp.second; sort(all(v)); for (auto p : v) { sm += p.first; if (sm > p.second) { return false; } } } return true; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; while (cin >> n, n) { cout << (check(n) ? "Yes" : "No") << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; bool check(int n) { map<int, vector<P>> mp; // b - a, {(a, b)} rep(i, n) { int a, b; cin >> a >> b; // mp[b - a].emplace_back(P(a, b)); mp[b].emplace_back(P(a, b)); } int sm = 0; for (auto pp : mp) { auto v = pp.second; sort(all(v)); for (auto p : v) { sm += p.first; if (sm > p.second) { return false; } } } return true; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; while (cin >> n, n) { cout << (check(n) ? "Yes" : "No") << endl; } }
[["-", 63, 118, 28, 69, 341, 342, 0, 16, 17, 33], ["-", 63, 118, 28, 69, 341, 342, 0, 16, 12, 22]]
1
237
#include <bits/stdc++.h> #define LL long long #define ULL unsigned long long #define REP(i, n) for (int i = 0; i < (n); i++) #define REP2(i, x, n) for (int i = x; i < (n); i++) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { int N; cin >> N; if (N == 0) { return 0; } map<int, int> mp; REP(i, N) { int a, b; cin >> a >> b; mp.insert(make_pair(b, a)); } int total{0}; bool flag = true; for (auto &x : mp) { total += x.second; if (total > x.first) { flag = false; } } cout << (flag ? "Yes" : "No") << endl; } return 0; }
#include <bits/stdc++.h> #define LL long long #define ULL unsigned long long #define REP(i, n) for (int i = 0; i < (n); i++) #define REP2(i, x, n) for (int i = x; i < (n); i++) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { int N; cin >> N; if (N == 0) { return 0; } multimap<int, int> mp; REP(i, N) { int a, b; cin >> a >> b; mp.insert(make_pair(b, a)); } int total{0}; bool flag = true; for (auto &x : mp) { total += x.second; if (total > x.first) { flag = false; } } cout << (flag ? "Yes" : "No") << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 39, 344, 141, 78], ["+", 0, 52, 8, 9, 0, 43, 39, 344, 141, 78]]
1
173
#include <algorithm> #include <iostream> using namespace std; int n, z[25], h[25], wa, f; int main() { while (1) { wa = 0; f = 0; cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> z[i] >> h[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (z[j] > z[j + 1]) { swap(z[j], z[j + 1]); swap(h[j], h[j + 1]); } } } for (int i = 0; i < n; i++) { wa += z[i]; if (wa > h[i]) { cout << "No" << endl; f = 1; break; } } if (f != 1) cout << "Yes" << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int n, z[25], h[25], wa, f; int main() { while (1) { wa = 0; f = 0; cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> z[i] >> h[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { if (h[j] > h[j + 1]) { swap(z[j], z[j + 1]); swap(h[j], h[j + 1]); } } } for (int i = 0; i < n; i++) { wa += z[i]; if (wa > h[i]) { cout << "No" << endl; f = 1; break; } } if (f != 1) cout << "Yes" << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["-", 0, 57, 15, 339, 51, 16, 12, 69, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 69, 28, 22]]
1
232
// 8-11.cpp : 螳壻ケ画而蛻カ蜿ー蠎皮畑遞句コ冗噪蜈・蜿」轤ケ縲?// #include <algorithm> #include <iomanip> #include <iostream> #include <string> using namespace std; struct island { int v, t; }; struct island a[1000]; bool cmp(struct island a, struct island b) { return a.t < b.t; } int main() { int n; while (cin >> n && n != 0) { for (int i = 1; i <= n; i++) cin >> a[i].v >> a[i].t; sort(a + 1, a + n + 1, cmp); int f = 1; long long sum = 0; for (int i = 1; i <= n; i++) { sum += a[i].v; if (sum > a[i].t) f = 0; } if (f == 1) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
// 8-11.cpp : 螳壻ケ画而蛻カ蜿ー蠎皮畑遞句コ冗噪蜈・蜿」轤ケ縲?// #include <algorithm> #include <iomanip> #include <iostream> #include <string> using namespace std; struct island { int v, t; }; struct island a[1000]; bool cmp(struct island a, struct island b) { return a.t < b.t; } int main() { int n; while (cin >> n && n != 0) { for (int i = 1; i <= n; i++) cin >> a[i].v >> a[i].t; sort(a + 1, a + n + 1, cmp); int f = 1; long long sum = 0; for (int i = 1; i <= n; i++) { sum += a[i].v; if (sum > a[i].t) f = 0; } if (f == 1) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
195
#include <algorithm> #include <iostream> using namespace std; struct island { int bridge; int treasure; }; bool operator<(island i1, island i2) { return i1.treasure < i2.treasure; } int main() { int input, value; bool flag; while (1) { cin >> input; if (input == 0) { break; } island table[input]; for (int i = 0; i < input; i++) { cin >> table[i].treasure >> table[i].bridge; } sort(table, table + input); value = 0; flag = true; for (int i = 0; i < input; i++) { value += table[i].treasure; if (value >= table[i].bridge) { flag = false; break; } } if (flag) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
#include <algorithm> #include <iostream> using namespace std; struct island { int bridge; int treasure; }; bool operator<(island i1, island i2) { // if(i1.treasure == i2.treasure){ return i1.bridge < i2.bridge; // } // return i1.treasure < i2.treasure; } int main() { int input, value; bool flag; while (1) { cin >> input; if (input == 0) { break; } island table[input]; for (int i = 0; i < input; i++) { cin >> table[i].treasure >> table[i].bridge; } sort(table, table + input); value = 0; flag = true; for (int i = 0; i < input; i++) { value += table[i].treasure; if (value > table[i].bridge) { flag = false; break; } } if (flag) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
[["-", 8, 9, 0, 37, 0, 16, 31, 118, 119, 120], ["+", 8, 9, 0, 37, 0, 16, 31, 118, 119, 120], ["-", 8, 9, 0, 37, 0, 16, 12, 118, 119, 120], ["+", 8, 9, 0, 37, 0, 16, 12, 118, 119, 120], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47]]
1
201
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<string> ans; int main() { while (1) { int n; cin >> n; if (n == 0) break; int it[25]; int bl[25]; int sindex[25]; for (int i = 0; i < n; i++) { cin >> it[i] >> bl[i]; sindex[i] = i; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (it[sindex[i]] > it[sindex[j]]) { int tind = sindex[i]; sindex[i] = sindex[j]; sindex[j] = tind; } } } int tsum = 0; bool tans = true; for (int i = 0; i < n; i++) { tsum += it[sindex[i]]; if (tsum > bl[sindex[i]]) tans = false; } if (tans) { ans.push_back("Yes"); } else { ans.push_back("No"); } } for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<string> ans; int main() { while (1) { int n; cin >> n; if (n == 0) break; int it[25]; int bl[25]; int sindex[25]; for (int i = 0; i < n; i++) { cin >> it[i] >> bl[i]; sindex[i] = i; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (bl[sindex[i]] > bl[sindex[j]]) { int tind = sindex[i]; sindex[i] = sindex[j]; sindex[j] = tind; } } } int tsum = 0; bool tans = true; for (int i = 0; i < n; i++) { tsum += it[sindex[i]]; if (tsum > bl[sindex[i]]) tans = false; } if (tans) { ans.push_back("Yes"); } else { ans.push_back("No"); } } for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["-", 0, 57, 15, 339, 51, 16, 12, 69, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 69, 28, 22]]
1
284
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; bool myComparison(const pair<int, int> &a, const pair<int, int> &b) { return a.second < b.second; } int main() { int n = 0; vector<int> re; while (cin >> n, n != 0) { vector<pair<int, int>> v; for (int i = 0; i < n; i++) { int t1, t2; cin >> t1 >> t2; v.push_back(make_pair(t1, t2)); } sort(v.begin(), v.end(), myComparison); int flag = 0; int load = 0; for (int i = 0; i < v.size(); i++) { load += v[i].first; if (load > v[i].second) { flag = 1; break; } } if (flag == 0) { re.push_back(1); } else { re.push_back(0); } } for (int i = 0; i < re.size(); i++) { if (re[i] == 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } //ワーシャルフロイド法
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; bool myComparison(const pair<int, int> &a, const pair<int, int> &b) { return a.second < b.second; } int main() { int n = 0; vector<int> re; while (cin >> n, n != 0) { vector<pair<int, int>> v; for (int i = 0; i < n; i++) { int t1, t2; cin >> t1 >> t2; v.push_back(make_pair(t1, t2)); } sort(v.begin(), v.end(), myComparison); int flag = 0; int load = 0; for (int i = 0; i < v.size(); i++) { load += v[i].first; if (load > v[i].second) { flag = 1; break; } } if (flag == 0) { re.push_back(1); } else { re.push_back(0); } } for (int i = 0; i < re.size(); i++) { if (re[i] == 1) { cout << "Yes" << endl; } else { cout << "No" << endl; } } } //ワーシャルフロイド法
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
276
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct islands { int t; int w; bool operator>(const islands &left) const { return w >= left.w; } }; int main() { int n, i; vector<islands> island; while (1) { cin >> n; if (n == 0) break; for (i = 0; i < n; i++) { islands I; cin >> I.t >> I.w; island.push_back(I); } sort(island.begin(), island.end(), greater<islands>()); int sum = 0; int flag = 0; for (i = n - 1; i >= 0; i--) { sum += island[i].t; if (island[i].w < sum) { cout << "NO" << endl; flag = 1; break; } } if (flag != 1) cout << "YES" << endl; island.clear(); } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct islands { int t; int w; bool operator>(const islands &left) const { return w > left.w; } }; int main() { int n, i; vector<islands> island; while (1) { cin >> n; if (n == 0) break; for (i = 0; i < n; i++) { islands I; cin >> I.t >> I.w; island.push_back(I); } sort(island.begin(), island.end(), greater<islands>()); int sum = 0; int flag = 0; for (i = n - 1; i >= 0; i--) { sum += island[i].t; if (island[i].w < sum) { cout << "No" << endl; flag = 1; break; } } if (flag != 1) cout << "Yes" << endl; island.clear(); } return 0; }
[["-", 0, 14, 8, 9, 0, 37, 0, 16, 17, 20], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 17, 47], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
218
#include <algorithm> #include <functional> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> #define PI 3.14159265 #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define all(x) (x).begin(), (x).end() typedef long long ll; #define SZ(x) ((int)(x).size()) using namespace std; int main() { int n, flag; int me = 0; ll mi, now; int w[25], v[25]; while (1) { cin >> n; if (n == 0) break; REP(i, n) { cin >> w[i] >> v[i]; } REP(j, n) { mi = 100000000; REP(i, n) { if (v[i] >= 0) { if (mi > v[i]) { mi = v[i]; now = i; } } } if (me + w[now] > v[now]) { cout << "No" << endl; flag = 0; break; } else { me = me + w[now]; v[now] = -1; flag = 1; } } if (flag == 1) { cout << "Yes" << endl; } } return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> #define PI 3.14159265 #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define all(x) (x).begin(), (x).end() typedef long long ll; #define SZ(x) ((int)(x).size()) using namespace std; int main() { int n, flag; int me = 0; ll mi, now; int w[25], v[25]; while (1) { me = 0; cin >> n; if (n == 0) break; REP(i, n) { cin >> w[i] >> v[i]; } REP(j, n) { mi = 100000000; REP(i, n) { if (v[i] >= 0) { if (mi > v[i]) { mi = v[i]; now = i; } } } if (me + w[now] > v[now]) { cout << "No" << endl; flag = 0; break; } else { me = me + w[now]; v[now] = -1; flag = 1; } } if (flag == 1) { cout << "Yes" << endl; } } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
251
#include <cstdlib> #include <iostream> using namespace std; struct island { int tre; int bri; }; int compare(const island *a, const island *b) { if ((*a).bri == (*b).bri) return (*a).tre - (*b).tre; else return (*a).bri - (*b).bri; } island isls[25]; int main() { for (;;) { int isl; cin >> isl; if (isl == 0) break; for (int i = 0; i < isl; i++) { cin >> isls[i].tre >> isls[i].bri; } qsort(isls, isl, sizeof(island), (int (*)(const void *, const void *))compare); int now = 0; bool pass = true; for (int i = 0; i < isl; i++) { now += isls[i].tre; if (isls[i].bri < now) { pass = false; break; } } if (pass) cout << "Yes"; else cout << "No"; } }
#include <cstdlib> #include <iostream> using namespace std; struct island { int tre; int bri; }; int compare(const island *a, const island *b) { if ((*a).bri == (*b).bri) return (*a).tre - (*b).tre; else return (*a).bri - (*b).bri; } island isls[25]; int main() { for (;;) { int isl; cin >> isl; if (isl == 0) break; for (int i = 0; i < isl; i++) { cin >> isls[i].tre >> isls[i].bri; } qsort(isls, isl, sizeof(island), (int (*)(const void *, const void *))compare); int now = 0; bool pass = true; for (int i = 0; i < isl; i++) { now += isls[i].tre; if (isls[i].bri < now) { pass = false; break; } } if (pass) cout << "Yes\n"; else cout << "No\n"; } }
[["+", 0, 57, 64, 1, 0, 16, 12, 5, 0, 44], ["+", 75, 76, 0, 1, 0, 16, 12, 5, 0, 44]]
1
247
#include <algorithm> #include <functional> #include <iostream> #include <vector> using namespace std; #define pii pair<int, int> int main() { int n; while (cin >> n, n) { vector<pii> v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back(pii(b, a)); } sort(v.begin(), v.end()); int x = 0; int i; for (i = 0; i < n; i++) { x += v[i].second; if (x > v[i].first) break; } if (i == n) cout << "YES" << endl; else cout << "NO" << endl; } }
#include <algorithm> #include <functional> #include <iostream> #include <vector> using namespace std; #define pii pair<int, int> int main() { int n; while (cin >> n, n) { vector<pii> v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back(pii(b, a)); } sort(v.begin(), v.end()); int x = 0; int i; for (i = 0; i < n; i++) { x += v[i].second; if (x > v[i].first) break; } if (i == n) cout << "Yes" << endl; else cout << "No" << endl; } }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
162
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; typedef struct { int weight, bridge; } sima; bool lessw(const sima &Left, const sima &Right) { return Left.weight < Right.weight; } bool lessb(const sima &Left, const sima &Right) { return Left.bridge < Right.bridge; } vector<sima> tou; int n; int main() { while (cin >> n) { if (n == 0) break; tou.clear(); for (int i = 0; i < n; i++) { int w, b; cin >> w >> b; sima t = {w, b}; tou.push_back(t); } int cuw = 0, difmin = 0; stable_sort(tou.begin(), tou.end(), lessb); stable_sort(tou.begin(), tou.end(), lessw); int out = 0; for (int i = 0; i < n; i++) { cuw += tou[i].weight; if (cuw > tou[i].bridge) { out = 1; break; } } if (out == 1) puts("No"); else puts("Yes"); } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; typedef struct { int weight, bridge; } sima; bool lessw(const sima &Left, const sima &Right) { return Left.weight < Right.weight; } bool lessb(const sima &Left, const sima &Right) { return Left.bridge < Right.bridge; } vector<sima> tou; int n; int main() { while (cin >> n) { if (n == 0) break; tou.clear(); for (int i = 0; i < n; i++) { int w, b; cin >> w >> b; sima t = {w, b}; tou.push_back(t); } int cuw = 0, difmin = 0; stable_sort(tou.begin(), tou.end(), lessw); stable_sort(tou.begin(), tou.end(), lessb); int out = 0; for (int i = 0; i < n; i++) { cuw += tou[i].weight; if (cuw > tou[i].bridge) { out = 1; break; } } if (out == 1) puts("No"); else puts("Yes"); } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
267
#include <algorithm> #include <stdio.h> using namespace std; pair<int, int> t[25]; int main() { int a; while (scanf("%d", &a), a) { for (int i = 0; i < a; i++) { scanf("%d%d", &t[i].first, &t[i].second); } std::sort(t, t + a); int now = 0; bool ok = true; for (int i = 0; i < a; i++) { now += t[i].first; if (now > t[i].second) ok = false; } printf(ok ? "Yes\n" : "No\n"); } }
#include <algorithm> #include <stdio.h> using namespace std; pair<int, int> t[25]; int main() { int a; while (scanf("%d", &a), a) { for (int i = 0; i < a; i++) { scanf("%d%d", &t[i].second, &t[i].first); } std::sort(t, t + a); int now = 0; bool ok = true; for (int i = 0; i < a; i++) { now += t[i].second; if (now > t[i].first) ok = false; } printf(ok ? "Yes\n" : "No\n"); } }
[["-", 0, 2, 3, 4, 0, 66, 28, 118, 119, 120], ["+", 0, 2, 3, 4, 0, 66, 28, 118, 119, 120], ["-", 8, 9, 0, 1, 0, 11, 12, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 11, 12, 118, 119, 120], ["-", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120], ["+", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120]]
1
159
#include <cassert> #include <climits> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <sstream> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <algorithm> #include <cctype> #include <cmath> #include <list> #include <math.h> #include <memory.h> #include <memory> #include <queue> #include <stack> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<P, P> P2; typedef pair<int, pair<P, P>> PPP; const int INF = 1 << 30; const double EPS = 1e-10; const double PI = 3.14159; P bridge[50]; int main() { int n; while (cin >> n && n) { for (int i = 0; i < n; i++) { cin >> bridge[i].second >> bridge[i].first; } sort(bridge, bridge + n); int sum = 0; int p = 0; while (p < n && sum + bridge[p].second <= bridge[p].first) { sum += bridge[p].second; p++; } if (p == n) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
#include <cassert> #include <climits> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <sstream> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <algorithm> #include <cctype> #include <cmath> #include <list> #include <math.h> #include <memory.h> #include <memory> #include <queue> #include <stack> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<P, P> P2; typedef pair<int, pair<P, P>> PPP; const int INF = 1 << 30; const double EPS = 1e-10; const double PI = 3.14159; P bridge[50]; int main() { int n; while (cin >> n && n) { for (int i = 0; i < n; i++) { cin >> bridge[i].second >> bridge[i].first; } sort(bridge, bridge + n); int sum = 0; int p = 0; while (p < n && sum + bridge[p].second <= bridge[p].first) { sum += bridge[p].second; p++; } if (p == n) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
252
#include <algorithm> #include <map> #include <stdio.h> int main() { int n, i, s; std::pair<int, int> b[25]; while (scanf("%d", &n), n) { for (i = 0; i < n; ++i) scanf("%d%d", &b[i].second, &b[i].first); std::sort(b, b + n); for (i = s = 0; i < n; ++i) { s += b[i].second; if (s > b[i].first) { puts("NO"); goto E; } } puts("YES"); E:; } return 0; }
#include <algorithm> #include <map> #include <stdio.h> int main() { int n, i, s; std::pair<int, int> b[25]; while (scanf("%d", &n), n) { for (i = 0; i < n; ++i) scanf("%d%d", &b[i].second, &b[i].first); std::sort(b, b + n); for (i = s = 0; i < n; ++i) { s += b[i].second; if (s > b[i].first) { puts("No"); goto E; } } puts("Yes"); E:; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
157
while True: n=eval(input()) if n==0:break p=[0]*n l=[[0]*31 for i in range(n)] a=sorted([input().split() for i in range(n)]) for j in range(n): for t in a[j][2:]: l[j][int(t)]=1 for t in range(1,30): c=0 for j in range(n): if l[j][t]:c+=1 for j in range(n): if l[j][t]: p[j]+=n-c+1 print(min(p),a[p.index(min(p))][0])
while True: n=eval(input()) if n==0:break p=[0]*n l=[[0]*31 for i in range(n)] a=sorted([input().split() for i in range(n)]) for j in range(n): for t in a[j][2:]: l[j][int(t)]=1 for t in range(30): c=0 for j in range(n): if l[j][t]: c+=1 for j in range(n): if l[j][t]: p[j]+=n-c+1 print(min(p),a[p.index(min(p))][0])
[["-", 8, 196, 0, 7, 12, 652, 3, 4, 0, 612], ["-", 8, 196, 0, 7, 12, 652, 3, 4, 0, 21]]
5
176
#include <bits/stdc++.h> using namespace std; #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) typedef long long ll; typedef pair<int, int> pii; #define all(X) (X).begin(), (X).end() #define pb push_back #define eb emplace_back #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define X first #define Y second #define reps(X, Y, Z) for (int(X) = (Y); (X) <= (Z); ++(X)) #define sz size() #define in(i, j, k) ((i) >= (j) && (j) <= (k)) #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) int main() { vector<int> a(6); while (1) { rep(i, 6) cin >> a[i]; if (!accumulate(all(a), 0)) break; rep(i, 3) a[i] += a[i + 3]; int d, re = 0, res; d = min({a[0], a[1], a[2]}); rep(i, min(d, 3)) { res = d - i; rep(j, 3) res += max(0, a[j] - d + i) / 3; re = max(re, res); } cout << re << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) typedef long long ll; typedef pair<int, int> pii; #define all(X) (X).begin(), (X).end() #define pb push_back #define eb emplace_back #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define X first #define Y second #define reps(X, Y, Z) for (int(X) = (Y); (X) <= (Z); ++(X)) #define sz size() #define in(i, j, k) ((i) >= (j) && (j) <= (k)) #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) int main() { vector<int> a(6); while (1) { rep(i, 6) cin >> a[i]; if (!accumulate(all(a), 0)) break; rep(i, 3) a[i] += a[i + 3]; int d, re = 0, res; d = min({a[0], a[1], a[2]}); rep(i, min(d + 1, 3)) { res = d - i; rep(j, 3) res += max(0, a[j] - d + i) / 3; re = max(re, res); } cout << re << endl; } return 0; }
[["+", 3, 4, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 12, 13]]
1
243
#include <bits/stdc++.h> using namespace std; typedef long long ll; int A, B, C; int main() { int a, b, c, d, e, f; while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) { A = a + d; B = b + e; C = c + f; // cout<< A << " "<< B << " " << C << endl; int n = min(A, min(B, C)); int res = 0; res = max(res, A / 3 + B / 3 + C / 3); for (int i = n; i > min(-1, n - 4); i--) { res = max(res, i + (A - i) / 3 + (B - i) / 3 + (C - i) / 3); } cout << res << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int A, B, C; int main() { int a, b, c, d, e, f; while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) { A = a + d; B = b + e; C = c + f; // cout<< A << " "<< B << " " << C << endl; int n = min(A, min(B, C)); int res = 0; res = max(res, A / 3 + B / 3 + C / 3); for (int i = n; i > max(-1, n - 4); i--) { res = max(res, i + (A - i) / 3 + (B - i) / 3 + (C - i) / 3); } cout << res << endl; } }
[["-", 8, 9, 0, 7, 15, 16, 12, 2, 63, 22], ["+", 8, 9, 0, 7, 15, 16, 12, 2, 63, 22]]
1
189
#include <iostream> #include <vector> using namespace std; vector<string> split(const string &str, const string &delim) { vector<string> res; size_t current = 0, found, delimlen = delim.size(); while ((found = str.find(delim, current)) != string::npos) { res.push_back(string(str, current, found - current)); current = found + delimlen; } res.push_back(string(str, current, str.size() - current)); return res; } void solve(string expr) { bool answer = true; vector<string> strings = split(expr, "|"); for (int i = 0; i < strings.size(); i++) { strings[i] = strings[i].substr(1, strings[i].size() - 2); // cout << strings[i] << endl; vector<string> sstrings = split(strings[i], "&"); bool t[3]; char c[3]; for (int i = 0; i < 3; i++) { if (sstrings[i].size() == 1) { c[i] = sstrings[i][0]; t[i] = false; } else { c[i] = sstrings[i][1]; t[i] = true; } // cout << c[i] << t[i] << endl; } if ((c[0] == c[1] && t[0] != t[1]) || (c[0] == c[2] && t[0] != t[2]) || (c[1] == c[2] && t[1] != t[2])) { answer = false; break; } } cout << (answer ? "yes" : "no") << endl; } int main() { while (true) { string input; cin >> input; if (input == "#") { break; } solve(input); } }
#include <iostream> #include <vector> using namespace std; vector<string> split(const string &str, const string &delim) { vector<string> res; size_t current = 0, found, delimlen = delim.size(); while ((found = str.find(delim, current)) != string::npos) { res.push_back(string(str, current, found - current)); current = found + delimlen; } res.push_back(string(str, current, str.size() - current)); return res; } void solve(string expr) { bool answer = false; vector<string> strings = split(expr, "|"); for (int i = 0; i < strings.size(); i++) { strings[i] = strings[i].substr(1, strings[i].size() - 2); // cout << strings[i] << endl; vector<string> sstrings = split(strings[i], "&"); bool t[3]; char c[3]; for (int i = 0; i < 3; i++) { if (sstrings[i].size() == 1) { c[i] = sstrings[i][0]; t[i] = false; } else { c[i] = sstrings[i][1]; t[i] = true; } // cout << c[i] << t[i] << endl; } if (!((c[0] == c[1] && t[0] != t[1]) || (c[0] == c[2] && t[0] != t[2]) || (c[1] == c[2] && t[1] != t[2]))) { answer = true; // cout << "false" << endl; break; } } cout << (answer ? "yes" : "no") << endl; } int main() { while (true) { string input; cin >> input; if (input == "#") { break; } solve(input); } }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 146], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 147], ["+", 8, 9, 0, 57, 15, 339, 51, 91, 17, 111], ["+", 28, 23, 0, 16, 31, 16, 31, 23, 0, 24], ["+", 0, 7, 8, 9, 0, 57, 15, 339, 0, 25], ["-", 0, 57, 64, 9, 0, 1, 0, 11, 12, 147], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 146]]
1
421
while gets.chomp!='#' puts $_.split('|').all?{|e|a=e[1..e.size-2].split('&');a.select{|e|e[0,1]!='~'&&a.index('~'+e)}!=[]}?'no':'yes'end
while(l=gets.chomp)!='#' puts l.split('|').all?{|e|a=e[1..e.size-2].split('&');a.select{|e|e[0,1]!='~'&&a.index('~'+e)}!=[]}?'no':'yes'end
[["+", 0, 493, 0, 89, 15, 738, 31, 739, 0, 24], ["+", 0, 89, 15, 738, 31, 739, 0, 662, 31, 22], ["+", 0, 89, 15, 738, 31, 739, 0, 662, 0, 32], ["+", 0, 493, 0, 89, 15, 738, 31, 739, 0, 25], ["-", 3, 4, 0, 754, 15, 652, 486, 652, 486, 744], ["+", 3, 4, 0, 754, 15, 652, 486, 652, 486, 22]]
4
85
#include <algorithm> #include <stdio.h> using namespace std; int d[51000]; int e[51000]; int f[51000]; int row[51000]; int col[51000]; int main() { int a, b, c; while (scanf("%d%d%d", &a, &b, &c), a) { for (int i = 0; i < a; i++) row[i] = 1; for (int j = 0; j < b; j++) col[j] = 1; for (int i = 0; i < c; i++) scanf("%d%d%d", d + i, e + i, f + i); int R = a; int C = b; long long ret = 0; for (int i = c - 1; i >= 0; i--) { if (d[i]) { if (!col[e[i]]) continue; col[e[i]] = 0; R--; if (f[i]) ret += C; } else { if (!row[e[i]]) continue; row[e[i]] = 0; C--; if (f[i]) ret += R; } } printf("%lld\n", ret); } }
#include <algorithm> #include <stdio.h> using namespace std; int d[51000]; int e[51000]; int f[51000]; int row[51000]; int col[51000]; int main() { int a, b, c; while (scanf("%d%d%d", &a, &b, &c), a) { for (int i = 0; i < a; i++) row[i] = 1; for (int j = 0; j < b; j++) col[j] = 1; for (int i = 0; i < c; i++) scanf("%d%d%d", d + i, e + i, f + i); int R = a; int C = b; long long ret = 0; for (int i = c - 1; i >= 0; i--) { if (d[i]) { if (!col[e[i]]) continue; col[e[i]] = 0; C--; if (f[i]) ret += R; } else { if (!row[e[i]]) continue; row[e[i]] = 0; R--; if (f[i]) ret += C; } } printf("%lld\n", ret); } }
[["-", 0, 57, 64, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 27, 28, 22], ["-", 64, 9, 0, 57, 64, 1, 0, 11, 12, 22], ["+", 64, 9, 0, 57, 64, 1, 0, 11, 12, 22], ["-", 75, 76, 0, 9, 0, 1, 0, 27, 28, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 27, 28, 22], ["-", 0, 9, 0, 57, 64, 1, 0, 11, 12, 22], ["+", 0, 9, 0, 57, 64, 1, 0, 11, 12, 22]]
1
278
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; int order[100000][3]; bool check_r[100000]; bool check_c[100000]; int main() { int r, c, q; while (true) { cin >> r >> c >> q; if (r == 0 && c == 0 && q == 0) { break; } for (int i = 0; i < 100000; i++) { check_r[i] = false; check_c[i] = false; } for (int i = 0; i < q; i++) { cin >> order[i][0] >> order[i][1] >> order[i][2]; } long long int r_rest = r; long long int c_rest = c; long long int ans = 0; for (int i = q - 1; i >= 0; i--) { if (order[i][0] == 0) { if (!check_c[order[i][1]]) { if (order[i][2] == 1) { ans += r_rest; } check_c[order[i][1]] = true; c_rest--; } } else { if (!check_r[order[i][1]]) { if (order[i][2] == 1) { ans += c_rest; } check_r[order[i][1]] = true; r_rest--; } } } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; int order[100000][3]; bool check_r[100000]; bool check_c[100000]; int main() { int r, c, q; while (true) { cin >> r >> c >> q; if (r == 0 && c == 0 && q == 0) { break; } for (int i = 0; i < 100000; i++) { check_r[i] = false; check_c[i] = false; } for (int i = 0; i < q; i++) { cin >> order[i][0] >> order[i][1] >> order[i][2]; } long long int r_rest = c; long long int c_rest = r; long long int ans = 0; for (int i = q - 1; i >= 0; i--) { if (order[i][0] == 0) { if (!check_c[order[i][1]]) { if (order[i][2] == 1) { ans += r_rest; } check_c[order[i][1]] = true; c_rest--; } } else { if (!check_r[order[i][1]]) { if (order[i][2] == 1) { ans += c_rest; } check_r[order[i][1]] = true; r_rest--; } } } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 22], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 22]]
1
321
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <sstream> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++) #define all(c) (c).begin(), (c).end() #define mp make_pair #define pb push_back using namespace std; typedef vector<int> vi; typedef pair<int, int> pi; typedef long long ll; int h, w, q; int a[50000], b[50000], o[50000]; bool row[50000], col[50000]; int main() { while (cin >> h >> w >> q, h) { rep(i, q) cin >> a[q - i - 1] >> b[q - i - 1] >> o[q - i - 1]; memset(row, 0, sizeof(row)); memset(col, 0, sizeof(col)); ll ans = 0; int nrow = 0, ncol = 0; rep(i, q) { if (a[i] == 0) { if (row[b[i]]) continue; row[b[i]] = 1; nrow++; if (o[i]) ans += h - ncol; } else { if (col[b[i]]) continue; col[b[i]] = 1; ncol++; if (o[i]) ans += w - nrow; } } cout << ans << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <sstream> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++) #define all(c) (c).begin(), (c).end() #define mp make_pair #define pb push_back using namespace std; typedef vector<int> vi; typedef pair<int, int> pi; typedef long long ll; int h, w, q; int a[50000], b[50000], o[50000]; bool row[50000], col[50000]; int main() { while (cin >> h >> w >> q, h) { rep(i, q) cin >> a[q - i - 1] >> b[q - i - 1] >> o[q - i - 1]; memset(row, 0, sizeof(row)); memset(col, 0, sizeof(col)); ll ans = 0; int nrow = 0, ncol = 0; rep(i, q) { if (a[i] == 0) { if (row[b[i]]) continue; row[b[i]] = 1; nrow++; if (o[i]) ans += w - ncol; } else { if (col[b[i]]) continue; col[b[i]] = 1; ncol++; if (o[i]) ans += h - nrow; } } cout << ans << endl; } return 0; }
[["-", 0, 57, 64, 1, 0, 11, 12, 16, 31, 22], ["+", 0, 57, 64, 1, 0, 11, 12, 16, 31, 22]]
1
309
#include <cstdio> #include <cstring> using namespace std; typedef long long int64; const int MAX_W = 50000; const int MAX_Q = 50000; int H, W, Q; int ts[MAX_Q], as[MAX_Q], cmd[MAX_Q]; bool visited[2][MAX_W]; bool init() { scanf("%d%d%d", &H, &W, &Q); for (int i = 0; i < Q; i++) { scanf("%d%d%d", ts + i, as + i, cmd + i); } memset(visited, false, sizeof(visited)); return H > 0; } int64 solve() { int64 ret = 0; int64 count[2]; count[0] = count[1] = 0; for (int i = Q - 1; i >= 0; i--) { int t = ts[i], x = as[i]; if (visited[t][x]) { continue; } visited[t][x] = true; count[t]++; if (cmd[i] == 1) { ret += (t == 0 ? H : W) - count[1 - t]; } } return ret; } int main() { while (init()) { printf("%lld\n", solve()); } return 0; }
#include <cstdio> #include <cstring> using namespace std; typedef long long int64; const int MAX_W = 50000; const int MAX_Q = 50000; int H, W, Q; int ts[MAX_Q], as[MAX_Q], cmd[MAX_Q]; bool visited[2][MAX_W]; bool init() { scanf("%d%d%d", &H, &W, &Q); for (int i = 0; i < Q; i++) { scanf("%d%d%d", ts + i, as + i, cmd + i); } memset(visited, false, sizeof(visited)); return H > 0; } int64 solve() { int64 ret = 0; int64 count[2]; count[0] = count[1] = 0; for (int i = Q - 1; i >= 0; i--) { int t = ts[i], x = as[i]; if (visited[t][x]) { continue; } visited[t][x] = true; count[t]++; if (cmd[i] == 1) { ret += (t == 0 ? W : H) - count[1 - t]; } } return ret; } int main() { while (init()) { printf("%lld\n", solve()); } return 0; }
[["-", 0, 11, 12, 16, 31, 23, 0, 41, 64, 22], ["-", 0, 11, 12, 16, 31, 23, 0, 41, 0, 102], ["+", 0, 11, 12, 16, 31, 23, 0, 41, 0, 102], ["+", 0, 11, 12, 16, 31, 23, 0, 41, 75, 22]]
1
285
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; struct P { int point; string name; P() {} P(int point, string name) : point{point}, name{name} {} bool operator<(const P &p) const { if (point != p.point) { return point > p.point; } return name < p.name; } }; bool c(int X, int K, int L, const vector<P> &info, const vector<int> &p) { int sum = 0; int rnk = K - X; for (int i = 0; i < X; i++) { if (p[i] < rnk) { rnk = min(rnk + 1, K); continue; } if (info[p[i]].name < info[rnk].name) { sum += info[rnk].point - info[p[i]].point; } else { sum += info[rnk].point - info[p[i]].point + 1; } if (sum > L) return 0; } return (sum <= L); } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M, K, L; while (cin >> N >> M >> K >> L, N > 0) { string name; int point; vector<P> info; for (int i = 0; i < N; i++) { cin >> name >> point; info.emplace_back(point, name); } sort(info.begin(), info.end()); map<string, int> num; for (int i = 0; i < N; i++) { num[info[i].name] = i; } vector<int> p(M); string fav; for (int i = 0; i < M; i++) { cin >> fav; p[i] = num[fav]; } sort(p.begin(), p.end()); int l = 0, r = min(K, M); while (r - l > 1) { int mid = (l + r) / 2; if (c(mid, K, L, info, p)) { l = mid; } else { r = mid; } } if (c(r, K, L, info, p)) { cout << r << endl; } else { cout << l << endl; } } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; struct P { int point; string name; P() {} P(int point, string name) : point{point}, name{name} {} bool operator<(const P &p) const { if (point != p.point) { return point > p.point; } return name < p.name; } }; bool c(int X, int K, int L, const vector<P> &info, const vector<int> &p) { int sum = 0; int rnk = K - X; for (int i = 0; i < X; i++) { if (p[i] <= rnk) { rnk = min(rnk + 1, K); continue; } if (info[p[i]].name < info[rnk].name) { sum += info[rnk].point - info[p[i]].point; } else { sum += info[rnk].point - info[p[i]].point + 1; } if (sum > L) return 0; } return (sum <= L); } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M, K, L; while (cin >> N >> M >> K >> L, N > 0) { string name; int point; vector<P> info; for (int i = 0; i < N; i++) { cin >> name >> point; info.emplace_back(point, name); } sort(info.begin(), info.end()); map<string, int> num; for (int i = 0; i < N; i++) { num[info[i].name] = i; } vector<int> p(M); string fav; for (int i = 0; i < M; i++) { cin >> fav; p[i] = num[fav]; } sort(p.begin(), p.end()); int l = 0, r = min(K, M); while (r - l > 1) { int mid = (l + r) / 2; if (c(mid, K, L, info, p)) { l = mid; } else { r = mid; } } if (c(r, K, L, info, p)) { cout << r << endl; } else { cout << l << endl; } } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
542
#include <algorithm> #include <iostream> #include <numeric> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; int main() { for (int N; cin >> N, N;) { vector<P> v; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(b, a)); } sort(v.begin(), v.end()); vector<int> w1, w2; for (int i = 0; i < N; i++) { w1.push_back(v[i].first); w2.push_back(v[i].second); } partial_sum(w2.begin(), w2.end(), w2.begin()); bool f = true; for (int i = 0; i < N; i++) if (w1[i] < w2[i]) f = false; cout << (f ? "YES" : "NO") << endl; } }
#include <algorithm> #include <iostream> #include <numeric> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; int main() { for (int N; cin >> N, N;) { vector<P> v; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(b, a)); } sort(v.begin(), v.end()); vector<int> w1, w2; for (int i = 0; i < N; i++) { w1.push_back(v[i].first); w2.push_back(v[i].second); } partial_sum(w2.begin(), w2.end(), w2.begin()); bool f = true; for (int i = 0; i < N; i++) if (w1[i] < w2[i]) f = false; cout << (f ? "Yes" : "No") << endl; } }
[["-", 31, 16, 12, 23, 0, 41, 64, 5, 0, 6], ["+", 31, 16, 12, 23, 0, 41, 64, 5, 0, 6], ["-", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6], ["+", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6]]
1
223
#include <iostream> #include <set> using namespace std; int main(void) { int n; int temp1, temp2; int i1; int ok; while (1) { cin >> n; if (n == 0) break; set<pair<int, int>> w; int sum; for (i1 = 0; i1 < n; i1++) { cin >> temp1 >> temp2; w.insert(make_pair(temp2, temp1)); } sum = 0; ok = 1; set<pair<int, int>>::iterator it; for (it = w.begin(); it != w.end(); it++) { sum += it->second; if (sum > it->first) ok = -1; } if (ok == -1) cout << "No" << endl; else cout << "Yes" << endl; } return 0; }
#include <iostream> #include <set> using namespace std; int main(void) { int n; int temp1, temp2; int i1; int ok; while (1) { cin >> n; if (n == 0) break; multiset<pair<int, int>> w; int sum; for (i1 = 0; i1 < n; i1++) { cin >> temp1 >> temp2; w.insert(make_pair(temp2, temp1)); } sum = 0; ok = 1; set<pair<int, int>>::iterator it; for (it = w.begin(); it != w.end(); it++) { sum += it->second; if (sum > it->first) ok = -1; } if (ok == -1) cout << "No" << endl; else cout << "Yes" << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 39, 344, 141, 78], ["+", 0, 52, 8, 9, 0, 43, 39, 344, 141, 78]]
1
182
#include <algorithm> #include <cstdlib> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define INF 100000000 typedef long long int lli; typedef pair<int, int> P; int main() { int n; while (cin >> n && n) { vector<P> bridge(n); for (int i = 0; i < n; ++i) { cin >> bridge[i].first; // weight cin >> bridge[i].second; // limit } for (int i = 0; i < n; ++i) { for (int j = n - 1; j - 1 >= i; --j) { if (bridge[j].second < bridge[j - 1].second) { swap(bridge[j], bridge[j - 1]); } else if (bridge[j].second == bridge[j - 1].second && bridge[j].first < bridge[j - 1].first) { swap(bridge[j], bridge[j - 1]); } } } bool f = true; int w = 0; for (int i = 0; i < n; ++i) { if (w + bridge[i].second <= bridge[i].second) { w += bridge[i].first; } else { f = false; } } if (f) { cout << "Yes\n"; } else { cout << "No\n"; } } return 0; }
#include <algorithm> #include <cstdlib> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define INF 100000000 typedef long long int lli; typedef pair<int, int> P; int main() { int n; while (cin >> n && n) { vector<P> bridge(n); for (int i = 0; i < n; ++i) { cin >> bridge[i].first; // weight cin >> bridge[i].second; // limit } for (int i = 0; i < n; ++i) { for (int j = n - 1; j - 1 >= i; --j) { if (bridge[j].second < bridge[j - 1].second) { swap(bridge[j], bridge[j - 1]); } else if (bridge[j].second == bridge[j - 1].second && bridge[j].first < bridge[j - 1].first) { swap(bridge[j], bridge[j - 1]); } } } bool f = true; int w = 0; for (int i = 0; i < n; ++i) { if (w + bridge[i].first <= bridge[i].second) { w += bridge[i].first; } else { f = false; } } if (f) { cout << "Yes\n"; } else { cout << "No\n"; } } return 0; }
[["-", 15, 339, 51, 16, 31, 16, 12, 118, 119, 120], ["+", 15, 339, 51, 16, 31, 16, 12, 118, 119, 120]]
1
321
class Q1052 def doIt n = gets.to_i island = Struct.new("Island", :treasure, :bridge) while n > 0 ary = Array.new for i in 0..n-1 t = gets.split(" ").map{|x| x.to_i} is = island.new(t[0], t[1]) ary.push is end ary.sort!{|a, b| a.treasure <=> b.treasure} sum = 0 bOK = 1 for i in 0..n-1 sum += ary[i].treasure if ary[i].bridge < sum bOK = 0 break end end if bOK == 1 then puts "Yes" else puts "No" end n = gets.to_i end end end Q1052.new.doIt
class Q1052 def doIt n = gets.to_i island = Struct.new("Island", :treasure, :bridge) while n > 0 ary = Array.new for i in 0..n-1 t = gets.split(" ").map{|x| x.to_i} is = island.new(t[0], t[1]) ary.push is end ary.sort!{|a, b| a.bridge <=> b.bridge} sum = 0 bOK = 1 for i in 0..n-1 sum += ary[i].treasure if ary[i].bridge < sum bOK = 0 break end end if bOK == 1 then puts "Yes" else puts "No" end n = gets.to_i end end end Q1052.new.doIt
[["-", 196, 196, 8, 734, 0, 738, 31, 652, 735, 22], ["+", 196, 196, 8, 734, 0, 738, 31, 652, 735, 22], ["-", 196, 196, 8, 734, 0, 738, 12, 652, 735, 22], ["+", 196, 196, 8, 734, 0, 738, 12, 652, 735, 22]]
4
163
while True: n = eval(input()) if n == 0: break info = [] for i in range(n): lu, w = list(map(int, input().split())) info.append((w, lu)) info.sort() lu_sum = 0 flag = True for w, lu in info: lu_sum += lu if lu_sum > w: flag = False print(("YES" if flag else "NO"))
while True: n = eval(input()) if n == 0: break info = [] for i in range(n): lu, w = list(map(int, input().split())) info.append((w, lu)) info.sort() lu_sum = 0 flag = True for w, lu in info: lu_sum += lu if lu_sum > w: flag = False print(("Yes" if flag else "No"))
[["-", 3, 4, 0, 23, 0, 41, 0, 557, 0, 6], ["+", 3, 4, 0, 23, 0, 41, 0, 557, 0, 6]]
5
101
while(1): n=int(input()) if n==0: break A=[[0,0] for i in range(n)] for i in range(n): [a,b]=list(map(int,input().split())) A[i]=[a,b] A.sort(key= lambda x: x[0]) s=0 for i in range(n): if s+A[i][0]>A[i][1]: print('No') break else: s+=A[i][0] else: print('Yes')
while(1): n=int(input()) if n==0: break A=[[0,0] for i in range(n)] for i in range(n): [a,b]=list(map(int,input().split())) A[i]=[a,b] A.sort(key= lambda x: x[1]) s=0 for i in range(n): if s+A[i][0]>A[i][1]: print('No') break else: s+=A[i][0] else: print('Yes')
[["-", 3, 4, 0, 653, 51, 670, 8, 206, 206, 612], ["+", 3, 4, 0, 653, 51, 670, 8, 206, 206, 612]]
5
144
while True: n = int(input()) if n == 0: break z = sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda x: x[1]) cnt = total = 0 for a, b in z: total += a if total > b: cnt = 1 break print("Yes" if cnt else "No")
while True: n = int(input()) if n == 0: break z = sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda x: x[1]) cnt = total = 0 for a, b in z: total += a if total > b: cnt = 1 break print("No" if cnt else "Yes")
[["-", 0, 652, 3, 4, 0, 41, 0, 557, 0, 6], ["+", 0, 652, 3, 4, 0, 41, 0, 557, 0, 6]]
5
92
def solve(l): have = 0 for m, w in l: have += w if m < have: return "NO" return "YES" while True: n = eval(input()) if n == 0: break l = sorted([tuple(reversed(list(map(int, input().split())))) for i in range(n)]) print(solve(l))
def solve(l): have = 0 for m, w in l: have += w if m < have: return "No" return "Yes" while True: n = eval(input()) if n == 0: break l = sorted([tuple(reversed(list(map(int, input().split())))) for i in range(n)]) print(solve(l))
[["-", 0, 57, 64, 196, 0, 37, 0, 557, 0, 6], ["+", 0, 57, 64, 196, 0, 37, 0, 557, 0, 6], ["-", 0, 14, 8, 196, 0, 37, 0, 557, 0, 6], ["+", 0, 14, 8, 196, 0, 37, 0, 557, 0, 6]]
5
91
#include <algorithm> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #define Epsilon 0.000001 using namespace std; double cal1(double x, double y) { return sqrt(x * x + y * y); } double cal2(double a, double b, double c, double d) { return a * b + c * d; } int main() { double D, px, py, vx, vy; double d; while (1) { scanf("%lf", &D); if (D == 0 || D < Epsilon) break; scanf("%lf%lf%lf%lf", &px, &py, &vx, &vy); double a = cal1(px, py); double b = cal1(vx, vy); if (fabs(py * vx - vy * px) < Epsilon) { double x1 = a * vx / b, y1 = a * vy / b; if (fabs(x1 - px) < Epsilon && fabs(y1 - py) < Epsilon) { d = 2 - a; } else { d = b; } } else { d = D + 1; } if (d > D || fabs(d - D) < Epsilon) printf("impossible\n"); else printf("%.8lf\n", d); } // bool check( double a, double b ) { // return a < b || fabs( a - b ) < 1e-9; // } // int main() { // double D; // while ( cin >> D ) { // if ( D < 1e-9 ) break; // double px, py, vx, vy; // cin >> px >> py >> vx >> vy; // double a = get( -px, -py, vx, vy ) / get_abs( px, py ) / get_abs( // vx, vy ); double t = D + 1; if ( fabs( a - 1.0 ) < 1e-9 ) { // t = get_abs( px, py ); // } else if ( fabs( a + 1.0 ) < 1e-9 ) { // t = 2.0 - get_abs( px, py ); // } // if ( check( t, D ) ) { // printf("%.10f\n", t); // } else { // cout << impossible << endl; // } // } // return 0; // } return 0; }
#include <algorithm> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #define Epsilon 0.000001 using namespace std; double cal1(double x, double y) { return sqrt(x * x + y * y); } double cal2(double a, double b, double c, double d) { return a * b + c * d; } int main() { double D, px, py, vx, vy; double d; while (1) { scanf("%lf", &D); if (D == 0 || D < Epsilon) break; scanf("%lf%lf%lf%lf", &px, &py, &vx, &vy); double a = cal1(px, py); double b = cal1(vx, vy); // printf("%lf %lf\n", a, b); if (fabs(py * vx - vy * px) < Epsilon) { double x1 = a * vx / b, y1 = a * vy / b; if (fabs(x1 - px) < Epsilon && fabs(y1 - py) < Epsilon) { d = 2 - a; } else { d = a; } } else { d = D + 1; } if (d > D || fabs(d - D) < Epsilon) printf("impossible\n"); else printf("%.8lf\n", d); } return 0; }
[["-", 75, 76, 0, 9, 0, 1, 0, 11, 12, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 12, 22]]
1
295
#include <algorithm> #include <complex> #include <vector> using namespace std; const double EPS = 1e-7; typedef complex<double> P; /* “_ */ typedef vector<P> G; /* ‘½ŠpŒ` */ struct L { P p, q; L(P p, P q) : p(p), q(q) {} }; /* ü•ª */ // “àÏEŠOÏEŠp“x double dot(P a, P b) { return real(conj(a) * b); } double cross(P a, P b) { return imag(conj(a) * b); } double angle(P a, P b) { return arg(b - a); } // complex‚ɑ召ŠÖŒW‚ª•K—v‚ȏꍇ namespace std { bool operator<(const P &a, const P &b) { return make_pair(real(a), imag(a)) < make_pair(real(b), imag(b)); } } // namespace std // ’¼ül‚Æ’¼üm‚ªŒð·‚·‚é‚©‚ð”»’è‚·‚éiŒð·‚µ‚È‚¢ê‡‚Í•½sj bool intersLL(L l, L m) { return abs(cross(l.q - l.p, m.q - m.p)) > EPS || // non-parallel abs(cross(l.q - l.p, m.p - l.p)) < EPS; // same line } // ’¼ül‚Ɛü•ªs‚ªŒð·‚·‚é‚©‚ð”»’è‚·‚é bool intersLS(L l, L s) { return abs(cross(l.q - l.p, s.p - l.p) * cross(l.q - l.p, s.q - l.p)) < EPS; } // ü•ªa‚Ɛü•ªb‚ªŒð·‚·‚é‚©‚ð”»’è‚·‚é bool intersSS(L a, L b) { if (abs(imag((a.q - a.p) / (b.q - b.p))) < EPS) return false; return cross(a.q - a.p, b.p - a.p) * cross(a.q - a.p, b.q - a.p) < 0 && cross(b.q - b.p, a.p - b.p) * cross(b.q - b.p, a.q - b.p) < 0; } // “_p‚ª’¼ülã‚É‘¶Ý‚·‚é‚©‚ð”»’è bool intersLP(L l, P p) { return abs(imag((p - l.p) / (l.q - l.p))) < EPS; } // “_p‚ªü•ªlã‚É‘¶Ý‚·‚é‚©‚ð”»’è‚·‚éi¸“x‚¤‚j bool intersSP(L l, P p) { // return abs( abs(l.p-p)+abs(l.q-p)-abs(l.q-l.p) ) < EPS; // // ¸“x‚ð‹C‚É‚µ‚È‚¢‚Ȃ炱‚êH if (intersLP(l, p)) { if ((real(l.p) - real(p)) * (real(l.q) - real(p)) <= 0) { if ((imag(l.p) - imag(p)) * (imag(l.q) - imag(p)) <= 0) { return true; } } } return false; } // ’¼üa‚Æ’¼üb‚ÌŒð“_‚ðŒvŽZ‚·‚éDæ‚ÉŒð·”»’肪•K{D // Œð·”»’èiintersLL or intersLS or intersSSj P crossPointLL(L a, L b) { double A = cross(a.q - a.p, b.q - b.p); double B = cross(a.q - a.p, a.q - b.p); return b.p + B / A * (b.q - b.p); } // ’¼üa‚Æ’¼üb‚ª“¯ˆê‚Ì‚à‚Ì‚©‚ð”»’è‚·‚é bool sameLine(L a, L b) { return intersLP(a, b.p) && intersLP(a, b.q); } // ’¼ül‚Æ“_p‚Ì‹——£‚ð‹‚ß‚é double distLP(L l, P p) { return abs(cross(l.q - l.p, p - l.p)) / abs(l.q - l.p); } // ’¼ül‚Æ’¼üm‚Ì‹——£‚ð‹‚ß‚éi•Às‚ÌŽžj double distLL(L l, L m) { return sameLine(l, m) ? 0.0 : distLP(l, m.p); } // ’¼ül‚Ɛü•ªs‚Ì‹——£‚ð‹‚ß‚é double distLS(L l, L s) { if (intersLS(l, s)) return 0.0; return min(distLP(l, s.p), distLP(l, s.q)); } // ü•ªs‚Æ“_p‚Ì‹——£‚ð‹‚ß‚é double distSP(L s, P p) { if (dot(s.q - s.p, p - s.p) < -EPS) return abs(p - s.p); if (dot(s.p - s.q, p - s.q) < -EPS) return abs(p - s.q); return abs(cross(s.q - s.p, p - s.p)) / abs(s.q - s.p); } // ü•ªs‚Ɛü•ªt‚Ì‹——£‚ð‹‚ß‚é double distSS(L s, L t) { if (intersSS(s, t)) return 0; return min(min(distSP(s, t.p), distSP(s, t.q)), min(distSP(t, s.p), distSP(t, s.q))); } // ü•ªa‚Ɛü•ªb‚ªƒI[ƒo[ƒ‰ƒbƒv‚µ‚Ä‚¢‚é‚©‚ð”»’è‚·‚é bool overlapSS(L a, L b) { if (intersSP(a, b.p) && intersLP(a, b.q)) return true; if (intersSP(a, b.q) && intersLP(a, b.p)) return true; if (intersSP(b, a.p) && intersLP(b, a.q)) return true; if (intersSP(b, a.q) && intersLP(b, a.p)) return true; return false; } // ü•ª‚̏W‡res’†‚Őü•ªl‚Əd‚È‚éü•ª‚ðƒ}[ƒW‚µCres‚ðXV‚·‚é void mergeLine(vector<L> &res, L l) { int n = res.size(); vector<L>::iterator it = res.begin(); while (it != res.end()) { if (overlapSS((*it), l)) { P dir(l.q - l.p); double m = 0.0, M = 1.0; m = min(m, real((it->p - l.p) / dir)); M = max(M, real((it->p - l.p) / dir)); m = min(m, real((it->q - l.p) / dir)); M = max(M, real((it->q - l.p) / dir)); l = L(l.p + m * dir, l.p + M * dir); it = res.erase(it); } else { it++; } } res.push_back(l); } // ’¼ü‚̏W‡res’†‚ŏd•¡‚·‚é’¼ü‚ðíœ‚µCres‚ðXV‚·‚é // Šp“xƒ\[ƒg‚ðŽÀ‘•‚·‚ê‚΍‚‘¬‰»}‚ê‚é‚ªŽ¸”s‚·‚éEEE void deleteSameLine(vector<L> &res) { int n = res.size(); for (int i = 0; i < n; i++) { if (imag(res[i].p) > imag(res[i].q)) { P temp = res[i].p; res[i].p = res[i].q; res[i].q = temp; } } // sort(res.begin(), res.end(), LessAngle); for (int i = 0; i < n; i++) { vector<L>::iterator it = res.begin() + i + 1; while (it < res.end()) { if (sameLine( res[i], (*it))) { /* ‚±‚±‚ð•Ï‚¦‚ê‚Ήž—p‚ªŒø‚­ */ it = res.erase(it); n--; } else { it++; } } } } #include <cstdio> #include <iostream> int main() { double D; while (cin >> D) { if (D == 0) break; double px, py, vx, vy; cin >> px >> py >> vx >> vy; P mikoto = P(px, py); P vec = P(vx, vy); P aim = P(0.0, 0.0); L line = L(mikoto, mikoto + vec); if (intersLP(line, aim)) { double dist = abs(mikoto - aim); double ans; if (dot(mikoto, vec) > 0) { ans = dist; } else { ans = 2.0 - dist; } if (D > ans) { printf("%.8f\n", ans); } else { cout << "impossible" << endl; } } else { cout << "impossible" << endl; } } return 0; }
#include <algorithm> #include <complex> #include <vector> using namespace std; const double EPS = 1e-7; typedef complex<double> P; /* “_ */ typedef vector<P> G; /* ‘½ŠpŒ` */ struct L { P p, q; L(P p, P q) : p(p), q(q) {} }; /* ü•ª */ // “àÏEŠOÏEŠp“x double dot(P a, P b) { return real(conj(a) * b); } double cross(P a, P b) { return imag(conj(a) * b); } double angle(P a, P b) { return arg(b - a); } // complex‚ɑ召ŠÖŒW‚ª•K—v‚ȏꍇ namespace std { bool operator<(const P &a, const P &b) { return make_pair(real(a), imag(a)) < make_pair(real(b), imag(b)); } } // namespace std // ’¼ül‚Æ’¼üm‚ªŒð·‚·‚é‚©‚ð”»’è‚·‚éiŒð·‚µ‚È‚¢ê‡‚Í•½sj bool intersLL(L l, L m) { return abs(cross(l.q - l.p, m.q - m.p)) > EPS || // non-parallel abs(cross(l.q - l.p, m.p - l.p)) < EPS; // same line } // ’¼ül‚Ɛü•ªs‚ªŒð·‚·‚é‚©‚ð”»’è‚·‚é bool intersLS(L l, L s) { return abs(cross(l.q - l.p, s.p - l.p) * cross(l.q - l.p, s.q - l.p)) < EPS; } // ü•ªa‚Ɛü•ªb‚ªŒð·‚·‚é‚©‚ð”»’è‚·‚é bool intersSS(L a, L b) { if (abs(imag((a.q - a.p) / (b.q - b.p))) < EPS) return false; return cross(a.q - a.p, b.p - a.p) * cross(a.q - a.p, b.q - a.p) < 0 && cross(b.q - b.p, a.p - b.p) * cross(b.q - b.p, a.q - b.p) < 0; } // “_p‚ª’¼ülã‚É‘¶Ý‚·‚é‚©‚ð”»’è bool intersLP(L l, P p) { return abs(imag((p - l.p) / (l.q - l.p))) < EPS; } // “_p‚ªü•ªlã‚É‘¶Ý‚·‚é‚©‚ð”»’è‚·‚éi¸“x‚¤‚j bool intersSP(L l, P p) { // return abs( abs(l.p-p)+abs(l.q-p)-abs(l.q-l.p) ) < EPS; // // ¸“x‚ð‹C‚É‚µ‚È‚¢‚Ȃ炱‚êH if (intersLP(l, p)) { if ((real(l.p) - real(p)) * (real(l.q) - real(p)) <= 0) { if ((imag(l.p) - imag(p)) * (imag(l.q) - imag(p)) <= 0) { return true; } } } return false; } // ’¼üa‚Æ’¼üb‚ÌŒð“_‚ðŒvŽZ‚·‚éDæ‚ÉŒð·”»’肪•K{D // Œð·”»’èiintersLL or intersLS or intersSSj P crossPointLL(L a, L b) { double A = cross(a.q - a.p, b.q - b.p); double B = cross(a.q - a.p, a.q - b.p); return b.p + B / A * (b.q - b.p); } // ’¼üa‚Æ’¼üb‚ª“¯ˆê‚Ì‚à‚Ì‚©‚ð”»’è‚·‚é bool sameLine(L a, L b) { return intersLP(a, b.p) && intersLP(a, b.q); } // ’¼ül‚Æ“_p‚Ì‹——£‚ð‹‚ß‚é double distLP(L l, P p) { return abs(cross(l.q - l.p, p - l.p)) / abs(l.q - l.p); } // ’¼ül‚Æ’¼üm‚Ì‹——£‚ð‹‚ß‚éi•Às‚ÌŽžj double distLL(L l, L m) { return sameLine(l, m) ? 0.0 : distLP(l, m.p); } // ’¼ül‚Ɛü•ªs‚Ì‹——£‚ð‹‚ß‚é double distLS(L l, L s) { if (intersLS(l, s)) return 0.0; return min(distLP(l, s.p), distLP(l, s.q)); } // ü•ªs‚Æ“_p‚Ì‹——£‚ð‹‚ß‚é double distSP(L s, P p) { if (dot(s.q - s.p, p - s.p) < -EPS) return abs(p - s.p); if (dot(s.p - s.q, p - s.q) < -EPS) return abs(p - s.q); return abs(cross(s.q - s.p, p - s.p)) / abs(s.q - s.p); } // ü•ªs‚Ɛü•ªt‚Ì‹——£‚ð‹‚ß‚é double distSS(L s, L t) { if (intersSS(s, t)) return 0; return min(min(distSP(s, t.p), distSP(s, t.q)), min(distSP(t, s.p), distSP(t, s.q))); } // ü•ªa‚Ɛü•ªb‚ªƒI[ƒo[ƒ‰ƒbƒv‚µ‚Ä‚¢‚é‚©‚ð”»’è‚·‚é bool overlapSS(L a, L b) { if (intersSP(a, b.p) && intersLP(a, b.q)) return true; if (intersSP(a, b.q) && intersLP(a, b.p)) return true; if (intersSP(b, a.p) && intersLP(b, a.q)) return true; if (intersSP(b, a.q) && intersLP(b, a.p)) return true; return false; } // ü•ª‚̏W‡res’†‚Őü•ªl‚Əd‚È‚éü•ª‚ðƒ}[ƒW‚µCres‚ðXV‚·‚é void mergeLine(vector<L> &res, L l) { int n = res.size(); vector<L>::iterator it = res.begin(); while (it != res.end()) { if (overlapSS((*it), l)) { P dir(l.q - l.p); double m = 0.0, M = 1.0; m = min(m, real((it->p - l.p) / dir)); M = max(M, real((it->p - l.p) / dir)); m = min(m, real((it->q - l.p) / dir)); M = max(M, real((it->q - l.p) / dir)); l = L(l.p + m * dir, l.p + M * dir); it = res.erase(it); } else { it++; } } res.push_back(l); } // ’¼ü‚̏W‡res’†‚ŏd•¡‚·‚é’¼ü‚ðíœ‚µCres‚ðXV‚·‚é // Šp“xƒ\[ƒg‚ðŽÀ‘•‚·‚ê‚΍‚‘¬‰»}‚ê‚é‚ªŽ¸”s‚·‚éEEE void deleteSameLine(vector<L> &res) { int n = res.size(); for (int i = 0; i < n; i++) { if (imag(res[i].p) > imag(res[i].q)) { P temp = res[i].p; res[i].p = res[i].q; res[i].q = temp; } } // sort(res.begin(), res.end(), LessAngle); for (int i = 0; i < n; i++) { vector<L>::iterator it = res.begin() + i + 1; while (it < res.end()) { if (sameLine( res[i], (*it))) { /* ‚±‚±‚ð•Ï‚¦‚ê‚Ήž—p‚ªŒø‚­ */ it = res.erase(it); n--; } else { it++; } } } } #include <cstdio> #include <iostream> int main() { double D; while (cin >> D) { if (D == 0) break; double px, py, vx, vy; cin >> px >> py >> vx >> vy; P mikoto = P(px, py); P vec = P(vx, vy); P aim = P(0.0, 0.0); L line = L(mikoto, mikoto + vec); if (intersLP(line, aim)) { double dist = abs(mikoto - aim); double ans; if (dot(mikoto, vec) < 0 + EPS) { ans = dist; } else { ans = 2.0 - dist; } if (D > ans) { printf("%.8f\n", ans); } else { cout << "impossible" << endl; } } else { cout << "impossible" << endl; } } return 0; }
[["-", 64, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 72], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 22]]
1
1,651
// AOJ1053 Accelerated Railgun #include <complex> #include <iostream> using namespace std; typedef complex<double> P; double dot(P a, P b) { return real(conj(a) * b); } double cross(P a, P b) { return imag(conj(a) * b); } int main() { double d; while (cin >> d) { if (d == 0.0) break; double px, py, vx, vy; cin >> px >> py >> vx >> vy; P p = P(px, py), v = P(vx, vy); if (cross(p, v) < 1e-8) { double L = 0.0; if (dot(p, v) > 0) L = 2.0 - abs(p); else L = abs(p); if (L < d - 1e-8) { printf("%.8lf\n", L); continue; } } puts("impossible"); } }
// AOJ1053 Accelerated Railgun #include <complex> #include <iostream> using namespace std; typedef complex<double> P; double dot(P a, P b) { return real(conj(a) * b); } double cross(P a, P b) { return imag(conj(a) * b); } int main() { double d; while (cin >> d) { if (d == 0.0) break; double px, py, vx, vy; cin >> px >> py >> vx >> vy; P p = P(px, py), v = P(vx, vy); if (abs(cross(p, v)) < 1e-8) { double L = 0.0; if (dot(p, v) > 0) L = 2.0 - abs(p); else L = abs(p); if (L < d - 1e-8) { printf("%.8lf\n", L); continue; } } puts("impossible"); } }
[["+", 0, 57, 15, 339, 51, 16, 31, 2, 63, 22], ["+", 15, 339, 51, 16, 31, 2, 3, 4, 0, 24], ["+", 31, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
1
198
#include <cmath> #include <iomanip> #include <iostream> #define PI (3.1415926535897932384626433832795) #define SQ(x) ((x) * (x)) #define LEN(x, y) (sqrt(SQ(x) + SQ(y))) using namespace std; bool eq(double a, double b) { return fabs(a - b) < 1e-8; } double prod(double x1, double y1, double x2, double y2) { return x1 * x2 + y1 * y2; } int main() { double D; while (cin >> D, D != 0.0) { double px, py, vx, vy; cin >> px >> py >> vx >> vy; double lp = LEN(px, py); double p = prod(px, py, vx, vy) / lp / LEN(vx, vy); cout << fixed << setprecision(8); if (eq(p, -1)) { if (lp < D) { cout << lp << endl; continue; } } else if (eq(p, 1)) { if (2 - lp < D) { cout << (2 - lp) << endl; continue; } } cout << "imposible" << endl; } return 0; }
#include <cmath> #include <iomanip> #include <iostream> #define PI (3.1415926535897932384626433832795) #define SQ(x) ((x) * (x)) #define LEN(x, y) (sqrt(SQ(x) + SQ(y))) using namespace std; bool eq(double a, double b) { return fabs(a - b) < 1e-8; } double prod(double x1, double y1, double x2, double y2) { return x1 * x2 + y1 * y2; } int main() { double D; while (cin >> D, D != 0.0) { double px, py, vx, vy; cin >> px >> py >> vx >> vy; double lp = LEN(px, py); double p = prod(px, py, vx, vy) / lp / LEN(vx, vy); cout << fixed << setprecision(8); if (eq(p, -1)) { if (lp < D) { cout << lp << endl; continue; } } else if (eq(p, 1)) { if (2 - lp < D) { cout << (2 - lp) << endl; continue; } } cout << "impossible" << endl; } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
228
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; const double EPS = 1.0e-10; class Point { public: double y, x; Point() { y = x = 0.0; } Point(double y0, double x0) { y = y0; x = x0; } Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); } Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); } Point operator*(double a) const { return Point(y * a, x * a); } Point operator/(double a) const { return Point(y / a, x / a); } double length() const { return sqrt(y * y + x * x); } double dist(const Point &p) const { return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2)); } double dot(const Point &p) const { return y * p.y + x * p.x; // |a|*|b|*cosθ } double cross(const Point &p) const { return x * p.y - y * p.x; // |a|*|b|*sinθ } }; int main() { for (;;) { double d; cin >> d; if (d < EPS) return 0; Point p, v; cin >> p.x >> p.y >> v.x >> v.y; if (p.cross(v) < EPS) { double ret; if (p.dot(v) > 0) ret = 2.0 - p.length(); else ret = p.length(); if (ret < d - EPS) printf("%.10f\n", ret); else cout << "impossible" << endl; } else { cout << "impossible" << endl; } } }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; const double EPS = 1.0e-10; class Point { public: double y, x; Point() { y = x = 0.0; } Point(double y0, double x0) { y = y0; x = x0; } Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); } Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); } Point operator*(double a) const { return Point(y * a, x * a); } Point operator/(double a) const { return Point(y / a, x / a); } double length() const { return sqrt(y * y + x * x); } double dist(const Point &p) const { return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2)); } double dot(const Point &p) const { return y * p.y + x * p.x; // |a|*|b|*cos慮 } double cross(const Point &p) const { return x * p.y - y * p.x; // |a|*|b|*sin慮 } }; int main() { for (;;) { double d; cin >> d; if (d < EPS) return 0; Point p, v; cin >> p.x >> p.y >> v.x >> v.y; if (abs(p.cross(v)) < EPS) { double ret; if (p.dot(v) > 0) ret = 2.0 - p.length(); else ret = p.length(); if (ret < d - EPS) printf("%.10f\n", ret); else cout << "impossible" << endl; } else { cout << "impossible" << endl; } } }
[["+", 0, 57, 15, 339, 51, 16, 31, 2, 63, 22], ["+", 15, 339, 51, 16, 31, 2, 3, 4, 0, 24], ["+", 31, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
1
429
#include <bits/stdc++.h> using namespace std; #define EPS 1e-6 #define INF 1e8 typedef complex<double> P; double dot(P a, P b) { return real(conj(a) * b); } double cross(P a, P b) { return imag(conj(a) * b); } struct Point { double x, y; }; int main() { double D; while (true) { Point v, p; scanf("%lf", &D); if (D == 0.0) break; scanf("%lf%lf%lf%lf", &p.x, &p.y, &v.x, &v.y); P np = P(p.x, p.y), nv = P(v.x, v.y); double dist = INF; if (cross(np, nv) < EPS) { if (dot(np, nv) < -EPS) { dist = abs(np); } else { dist = 2 - abs(np); } } if (dist <= D - EPS) { printf("%.8f\n", dist); } else { printf("impossible\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define EPS 1e-6 #define INF 1e8 typedef complex<double> P; double dot(P a, P b) { return real(conj(a) * b); } double cross(P a, P b) { return imag(conj(a) * b); } struct Point { double x, y; }; int main() { double D; while (true) { Point v, p; scanf("%lf", &D); if (D == 0.0) break; scanf("%lf%lf%lf%lf", &p.x, &p.y, &v.x, &v.y); P np = P(p.x, p.y), nv = P(v.x, v.y); double dist = INF; if (abs(cross(np, nv)) < EPS) { if (dot(np, nv) < -EPS) { dist = abs(np); } else { dist = 2 - abs(np); } } if (dist < D - EPS) { printf("%.8f\n", dist); } else { printf("impossible\n"); } } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 31, 2, 63, 22], ["+", 15, 339, 51, 16, 31, 2, 3, 4, 0, 24], ["+", 15, 339, 51, 16, 31, 2, 3, 4, 0, 25], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
1
250
#include <bits/stdc++.h> using namespace std; using Int = long long; struct UnionFind { Int n; vector<Int> r, p; UnionFind(Int n) : n(n), r(n, 1), p(n) { iota(p.begin(), p.end(), 0); } Int find(Int x) { if (x == p[x]) return x; return p[x] = find(p[x]); } void unite(Int x, Int y) { x = find(x); y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y); r[x] += r[y]; p[y] = x; } }; signed main() { cin.tie(0); ios::sync_with_stdio(0); Int n; while (cin >> n, n) { UnionFind uf(n); vector<Int> a(n), x(n), b(n), y(n); for (Int i = 0; i < n; i++) { cin >> a[i] >> x[i] >> b[i] >> y[i]; uf.unite(i, a[i]); uf.unite(i, b[i]); } vector<vector<Int>> v(n); for (Int i = 0; i < n; i++) { Int k = uf.find(i); v[k].emplace_back(x[i]); v[k].emplace_back(y[i]); } Int ans = 1; for (Int i = 0; i < n; i++) { if (v[i].empty()) continue; auto &u = v[i]; sort(u.begin(), u.end()); Int k = 0; while (k < (Int)u.size() && u[0] == u[k]) k++; k /= 2; ans *= k; ans %= 10007; // cout<<k<<endl; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using Int = long long; struct UnionFind { Int n; vector<Int> r, p; UnionFind(Int n) : n(n), r(n, 1), p(n) { iota(p.begin(), p.end(), 0); } Int find(Int x) { if (x == p[x]) return x; return p[x] = find(p[x]); } void unite(Int x, Int y) { x = find(x); y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y); r[x] += r[y]; p[y] = x; } }; signed main() { cin.tie(0); ios::sync_with_stdio(0); Int n; while (cin >> n, n) { UnionFind uf(n); vector<Int> a(n), x(n), b(n), y(n); for (Int i = 0; i < n; i++) { cin >> a[i] >> x[i] >> b[i] >> y[i]; uf.unite(i, a[i]); uf.unite(i, b[i]); } vector<vector<Int>> v(n); for (Int i = 0; i < n; i++) { Int k = uf.find(i); v[k].emplace_back(x[i]); v[k].emplace_back(y[i]); } Int ans = 1; for (Int i = 0; i < n; i++) { if (v[i].empty()) continue; auto &u = v[i]; sort(u.rbegin(), u.rend()); Int k = 0; while (k < (Int)u.size() && u[0] == u[k]) k++; k /= 2; ans *= k; ans %= 10007; // cout<<k<<endl; } cout << ans << endl; } return 0; }
[["-", 0, 2, 3, 4, 0, 2, 63, 118, 119, 120], ["+", 0, 2, 3, 4, 0, 2, 63, 118, 119, 120]]
1
472
#include <iostream> #include <map> #include <stack> #include <vector> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; void visit(const Graph &g, int v, vector<vector<int>> &scc, stack<int> &S, vector<bool> &inS, vector<int> &low, vector<int> &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->dst; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(vector<int>()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, vector<vector<int>> &scc) { const int n = g.size(); vector<int> num(n), low(n); stack<int> S; vector<bool> inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } int main() { int n; while (cin >> n && n) { Graph G(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { int b, f; cin >> b >> f; G[i].push_back(Edge(i, b, f)); } } VVI scc; int res = 1; stronglyConnectedComponents(G, scc); for (auto it = scc.begin(); it != scc.end(); ++it) { VI V = *it; int m = V.size(); int maxf = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < 2; ++j) { maxf = max(maxf, G[i][j].weight); } } int cnt = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < 2; ++j) { if (maxf == G[i][j].weight) { cnt++; } } } res = (res * (cnt / 2)) % 10007; } cout << res << endl; } return 0; }
#include <iostream> #include <map> #include <stack> #include <vector> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; void visit(const Graph &g, int v, vector<vector<int>> &scc, stack<int> &S, vector<bool> &inS, vector<int> &low, vector<int> &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->dst; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(vector<int>()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, vector<vector<int>> &scc) { const int n = g.size(); vector<int> num(n), low(n); stack<int> S; vector<bool> inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } int main() { int n; while (cin >> n && n) { Graph G(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { int b, f; cin >> b >> f; G[i].push_back(Edge(i, b, f)); } } VVI scc; int res = 1; stronglyConnectedComponents(G, scc); for (auto it = scc.begin(); it != scc.end(); ++it) { VI V = *it; int m = V.size(); int maxf = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < 2; ++j) { maxf = max(maxf, G[V[i]][j].weight); } } int cnt = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < 2; ++j) { if (maxf == G[V[i]][j].weight) { cnt++; } } } res = (res * (cnt / 2)) % 10007; } cout << res << endl; } return 0; }
[["+", 28, 69, 28, 69, 341, 342, 0, 69, 28, 22], ["+", 28, 69, 341, 342, 0, 69, 341, 342, 0, 70], ["+", 28, 69, 341, 342, 0, 69, 341, 342, 0, 73], ["+", 12, 118, 28, 69, 28, 69, 341, 342, 0, 70]]
1
807