buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
#include <bits/stdc++.h> using namespace std; #define int long long #define stoi stoll using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define ALL(c) begin(c), end(c) #define RALL(c) rbegin(c), rend(c) #define ITR(i, b, e) for (auto i = (b); i != (e); ++i) #define FORE(x, c) for (auto &x : c) #define REPF(i, a, n) for (int i = a, i##len = (int)(n); i < i##len; ++i) #define REP(i, n) REPF(i, 0, n) #define REPR(i, n) for (int i = (int)(n); i >= 0; --i) #define SZ(c) ((int)c.size()) #define CONTAIN(c, x) (c.find(x) != end(c)) #define OUTOFRANGE(y, x, h, w) ((y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)) #define dump(...) const signed INF_ = 1001001001; const long long INF = 1001001001001001001LL; const int DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}, DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0}; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { ITR(i, begin(v), end(v)) is >> *i; return is; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T, class U> bool chmax(T &a, const U &b) { return a < b ? a = b, 1 : 0; } template <class T, class U> bool chmin(T &a, const U &b) { return a > b ? a = b, 1 : 0; } template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; struct { template <class T> operator T() { T x; cin >> x; return x; } } IN; struct before_main_function { before_main_function() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(15) << fixed; #define endl "\n" } } before_main_function; //------------------8<------------------------------------8<-------------------- void solve(int n) { vector<int> x(3 * n), y(3 * n); vector<vector<int>> rect(n); REP(i, n) { int l, t, r, b; cin >> l >> b >> r >> t; r--, b--; x[i * 2 + 0] = l; x[i * 2 + 1] = r; x[i * 2 + 2] = r + 1; y[i * 2 + 0] = t; y[i * 2 + 1] = b; y[i * 2 + 2] = b + 1; rect[i] = {l, t, r, b}; } sort(ALL(x)); sort(ALL(y)); auto getX = [&](int X) { return lower_bound(ALL(x), X) - begin(x); }; auto getY = [&](int Y) { return lower_bound(ALL(y), Y) - begin(y); }; vector<vector<int>> S(200, vector<int>(200, 0)); REP(i, n) { int l = getX(rect[i][0]) + 1; int t = getY(rect[i][1]) + 1; int r = getX(rect[i][2]) + 1; int r_ = getX(rect[i][2] + 1) + 1; int b = getY(rect[i][3]) + 1; int b_ = getY(rect[i][3] + 1) + 1; int num = 1 << (i + 1); S[t][l] += num; S[t][r_] -= num; S[b_][l] -= num; S[b_][r_] += num; } REP(i, 200) REPF(j, 1, 200) S[i][j] += S[i][j - 1]; REP(i, 200) REPF(j, 1, 200) S[j][i] += S[j - 1][i]; vector<vector<bool>> vis(200, vector<bool>(200, false)); int ans = 0; REP(i, 200) { REP(j, 200) { int num = S[i][j]; if (vis[i][j]) continue; ans++; queue<pii> q; q.push({i, j}); vis[i][j] = true; while (!q.empty()) { int iy = q.front().first; int ix = q.front().second; q.pop(); REP(dir, 4) { int niy = iy + DY[dir]; int nix = ix + DX[dir]; if (OUTOFRANGE(niy, nix, 200, 200)) continue; if (vis[niy][nix]) continue; if (S[niy][nix] == num) { vis[niy][nix] = true; q.push({niy, nix}); } } } } } cout << ans << endl; } signed main() { while (1) { int n; cin >> n; if (n == 0) break; solve(n); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define stoi stoll using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define ALL(c) begin(c), end(c) #define RALL(c) rbegin(c), rend(c) #define ITR(i, b, e) for (auto i = (b); i != (e); ++i) #define FORE(x, c) for (auto &x : c) #define REPF(i, a, n) for (int i = a, i##len = (int)(n); i < i##len; ++i) #define REP(i, n) REPF(i, 0, n) #define REPR(i, n) for (int i = (int)(n); i >= 0; --i) #define SZ(c) ((int)c.size()) #define CONTAIN(c, x) (c.find(x) != end(c)) #define OUTOFRANGE(y, x, h, w) ((y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)) #define dump(...) const signed INF_ = 1001001001; const long long INF = 1001001001001001001LL; const int DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}, DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0}; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { ITR(i, begin(v), end(v)) is >> *i; return is; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T, class U> bool chmax(T &a, const U &b) { return a < b ? a = b, 1 : 0; } template <class T, class U> bool chmin(T &a, const U &b) { return a > b ? a = b, 1 : 0; } template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; struct { template <class T> operator T() { T x; cin >> x; return x; } } IN; struct before_main_function { before_main_function() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(15) << fixed; #define endl "\n" } } before_main_function; //------------------8<------------------------------------8<-------------------- void solve(int n) { vector<int> x(3 * n), y(3 * n); vector<vector<int>> rect(n); REP(i, n) { int l, t, r, b; cin >> l >> b >> r >> t; r--, b--; x[i * 3 + 0] = l; x[i * 3 + 1] = r; x[i * 3 + 2] = r + 1; y[i * 3 + 0] = t; y[i * 3 + 1] = b; y[i * 3 + 2] = b + 1; rect[i] = {l, t, r, b}; } sort(ALL(x)); sort(ALL(y)); auto getX = [&](int X) { return lower_bound(ALL(x), X) - begin(x); }; auto getY = [&](int Y) { return lower_bound(ALL(y), Y) - begin(y); }; vector<vector<int>> S(200, vector<int>(200, 0)); REP(i, n) { int l = getX(rect[i][0]) + 1; int t = getY(rect[i][1]) + 1; int r = getX(rect[i][2]) + 1; int r_ = getX(rect[i][2] + 1) + 1; int b = getY(rect[i][3]) + 1; int b_ = getY(rect[i][3] + 1) + 1; int num = 1 << (i + 1); S[t][l] += num; S[t][r_] -= num; S[b_][l] -= num; S[b_][r_] += num; } REP(i, 200) REPF(j, 1, 200) S[i][j] += S[i][j - 1]; REP(i, 200) REPF(j, 1, 200) S[j][i] += S[j - 1][i]; vector<vector<bool>> vis(200, vector<bool>(200, false)); int ans = 0; REP(i, 200) { REP(j, 200) { int num = S[i][j]; if (vis[i][j]) continue; ans++; queue<pii> q; q.push({i, j}); vis[i][j] = true; while (!q.empty()) { int iy = q.front().first; int ix = q.front().second; q.pop(); REP(dir, 4) { int niy = iy + DY[dir]; int nix = ix + DX[dir]; if (OUTOFRANGE(niy, nix, 200, 200)) continue; if (vis[niy][nix]) continue; if (S[niy][nix] == num) { vis[niy][nix] = true; q.push({niy, nix}); } } } } } cout << ans << endl; } signed main() { while (1) { int n; cin >> n; if (n == 0) break; solve(n); } return 0; }
[["-", 31, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["+", 31, 69, 341, 342, 0, 16, 31, 16, 12, 13]]
1
1,242
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1337> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ ll solve(ll n) { ll res = 0; vector<ll> x1(n), x2(n), y1(n), y2(n); vector<ll> xs(2 * n), ys(2 * n); for (int i = 0; i < n; i++) { ll l, t, r, b; cin >> l >> t >> r >> b; l *= 2; t *= 2; r *= 2; b *= 2; x1[i] = l; y1[i] = b; x2[i] = r; y2[i] = t; xs[2 * i] = l; xs[2 * i + 1] = r; ys[2 * i] = b; ys[2 * i + 1] = t; } xs.push_back(-INF); xs.push_back(INF); ys.push_back(-INF); ys.push_back(INF); for (int i = 0; i < n; i++) { for (int j = -1; j <= 1; j += 2) { xs.push_back(x1[i] + j); xs.push_back(x2[i] + j); ys.push_back(y1[i] + j); ys.push_back(y2[i] + j); } } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); for (int i = 0; i < n; i++) { x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin() + 2; x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin() + 2; y1[i] = lower_bound(ys.begin(), ys.end(), y1[i]) - ys.begin() + 2; y2[i] = lower_bound(ys.begin(), ys.end(), y2[i]) - ys.begin() + 2; } vector<vector<int>> masu(xs.size() + 5, vector<int>(ys.size() + 5, 0)); for (int i = 0; i < n; i++) { for (ll j = x1[i]; j <= x2[i]; j++) { masu[j][y1[i]] = masu[j][y2[i]] = i + 1; } for (ll k = y1[i]; k <= y2[i]; k++) { masu[x1[i]][k] = masu[x2[i]][k] = i + 1; } } for (int i = 0; i < xs.size() + 5; i++) masu[i][0] = masu[i][ys.size() + 4] = -1; for (int j = 0; j < ys.size() + 5; j++) masu[0][j] = masu[xs.size() + 4][j] = -1; int d[5] = {1, 0, -1, 0, 1}; for (int i = 0; i < xs.size() + 5; i++) { for (int j = 0; j < ys.size() + 5; j++) { if (masu[i][j] != 0) continue; res++; queue<pii> q; q.push({i, j}); while (q.size()) { int x, y; tie(x, y) = q.front(); q.pop(); if (masu[x][y] != 0) continue; masu[x][y] = -1; for (int k = 0; k < 4; k++) { int nx = x + d[k], ny = y + d[k + 1]; q.push({nx, ny}); } } } } return res; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); ll n; while (cin >> n, n) { cout << "# " << solve(n) << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1337> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ ll solve(ll n) { ll res = 0; vector<ll> x1(n), x2(n), y1(n), y2(n); vector<ll> xs(2 * n), ys(2 * n); for (int i = 0; i < n; i++) { ll l, t, r, b; cin >> l >> t >> r >> b; l *= 2; t *= 2; r *= 2; b *= 2; x1[i] = l; y1[i] = b; x2[i] = r; y2[i] = t; xs[2 * i] = l; xs[2 * i + 1] = r; ys[2 * i] = b; ys[2 * i + 1] = t; } xs.push_back(-INF); xs.push_back(INF); ys.push_back(-INF); ys.push_back(INF); for (int i = 0; i < n; i++) { for (int j = -1; j <= 1; j += 2) { xs.push_back(x1[i] + j); xs.push_back(x2[i] + j); ys.push_back(y1[i] + j); ys.push_back(y2[i] + j); } } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); for (int i = 0; i < n; i++) { x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin() + 2; x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin() + 2; y1[i] = lower_bound(ys.begin(), ys.end(), y1[i]) - ys.begin() + 2; y2[i] = lower_bound(ys.begin(), ys.end(), y2[i]) - ys.begin() + 2; } vector<vector<int>> masu(xs.size() + 5, vector<int>(ys.size() + 5, 0)); for (int i = 0; i < n; i++) { for (ll j = x1[i]; j <= x2[i]; j++) { masu[j][y1[i]] = masu[j][y2[i]] = i + 1; } for (ll k = y1[i]; k <= y2[i]; k++) { masu[x1[i]][k] = masu[x2[i]][k] = i + 1; } } for (int i = 0; i < xs.size() + 5; i++) masu[i][0] = masu[i][ys.size() + 4] = -1; for (int j = 0; j < ys.size() + 5; j++) masu[0][j] = masu[xs.size() + 4][j] = -1; int d[5] = {1, 0, -1, 0, 1}; for (int i = 0; i < xs.size() + 5; i++) { for (int j = 0; j < ys.size() + 5; j++) { if (masu[i][j] != 0) continue; res++; queue<pii> q; q.push({i, j}); while (q.size()) { int x, y; tie(x, y) = q.front(); q.pop(); if (masu[x][y] != 0) continue; masu[x][y] = -1; for (int k = 0; k < 4; k++) { int nx = x + d[k], ny = y + d[k + 1]; q.push({nx, ny}); } } } } return res; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); ll n; while (cin >> n, n) { cout << solve(n) << endl; } return 0; }
[["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151]]
1
1,347
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define fo(i, n) for (int i = 0; i < (int)n; i++) #define rep(it, s) \ for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) #define mp(a, b) make_pair(a, b) #define pb(x) push_back(x) #define pii pair<int, int> int n, m; int par[100010]; long long w[100010]; int get(int a) { if (a == par[a]) return a; int p = par[a]; p = get(p); w[a] += w[par[a]]; par[a] = p; return par[a]; } void connect(int a, int b, int c) { int i = get(a); int j = get(b); par[i] = j; w[i] = -(w[j] - c - w[i]); } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); while (scanf("%d%d", &n, &m) && n != 0) { for (int i = 0; i < n; i++) par[i] = i; for (int i = 0; i < n; i++) w[i] = 0; for (int i = 0; i < m; i++) { char c[5]; scanf("%s", c); if (c[0] == '!') { int a, b, d; scanf("%d%d%d", &a, &b, &d); a--; b--; if (get(a) == get(b)) continue; connect(a, b, d); } else { int a, b; scanf("%d%d", &a, &b); a--; b--; if (get(a) != get(b)) { printf("UNKNOWN\n"); } else { printf("%lld\n", w[a] - w[b]); } } } } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define fo(i, n) for (int i = 0; i < (int)n; i++) #define rep(it, s) \ for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) #define mp(a, b) make_pair(a, b) #define pb(x) push_back(x) #define pii pair<int, int> int n, m; int par[100010]; long long w[100010]; int get(int a) { if (a == par[a]) return a; int p = par[a]; p = get(p); w[a] += w[par[a]]; par[a] = p; return par[a]; } void connect(int a, int b, int c) { int i = get(a); int j = get(b); par[i] = j; w[i] = -(w[a] - c - w[b]); } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); while (scanf("%d%d", &n, &m) && n != 0) { for (int i = 0; i < n; i++) par[i] = i; for (int i = 0; i < n; i++) w[i] = 0; for (int i = 0; i < m; i++) { char c[5]; scanf("%s", c); if (c[0] == '!') { int a, b, d; scanf("%d%d%d", &a, &b, &d); a--; b--; if (get(a) == get(b)) continue; connect(a, b, d); } else { int a, b; scanf("%d%d", &a, &b); a--; b--; if (get(a) != get(b)) { printf("UNKNOWN\n"); } else { printf("%lld\n", w[a] - w[b]); } } } } return 0; }
[["-", 0, 16, 31, 16, 31, 69, 341, 342, 0, 22], ["+", 0, 16, 31, 16, 31, 69, 341, 342, 0, 22], ["-", 28, 23, 0, 16, 12, 69, 341, 342, 0, 22], ["+", 28, 23, 0, 16, 12, 69, 341, 342, 0, 22]]
1
444
#include <bits/stdc++.h> using namespace std; const double eps = 0.0000000001; bool eq(double a, double b) { return (fabs(a - b) < eps); } struct Point { double x, y, z; Point operator+(Point p) { return (Point){x + p.x, y + p.y, z + p.z}; } Point operator-(Point p) { return (Point){x - p.x, y - p.y, z - p.z}; } Point operator*(double k) { return (Point){x * k, y * k, z * k}; } }; typedef Point Vector; struct Segment { Point p, v; }; typedef Segment Line; void pr(Point p) { printf("%.5f %.5f %.5f\n", p.x, p.y, p.z); } double abs(Point p) { return sqrt(p.x * p.x + p.y * p.y + p.z * p.z); } double norm(Point p) { return p.x * p.x + p.y * p.y + p.z * p.z; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y + a.z * b.z; } Point cross(Point a, Point b) { double nx = a.y * b.z - b.y * a.z; double ny = a.z * b.x - b.z * a.x; double nz = a.x * b.y - b.x * a.y; return (Point){nx, ny, nz}; } bool eq(Point a, Point b) { return (eq(a.x, b.x) && eq(a.y, b.y) && eq(a.z, b.z)); } double getDistanceLP(Line a, Point p) { Point k = p - a.p; double c = abs(cross(a.v, k)); return c / abs(a.v); } double getDistanceSP(Segment a, Point p) { Point k = p - a.p; if (dot(a.v, k) < 0) return abs(a.v - p); k = p - (a.p + a.v); if (dot(a.v * -1.0, k) < 0) return abs(a.p + a.v - p); return getDistanceLP(a, p); } bool getCrossCS(Point c, double r, Segment s) { double A = getDistanceSP(s, c); double B = abs(c - s.p); double C = abs(c - (s.p + s.v)); if (r + eps < A) return false; if (B + eps < r && C + eps < r) return false; return true; } int N, M, R; Point S[2005]; double Sr[2005]; Point E; Point T[20]; double Tb[20]; vector<int> G[20]; void init() { for (int i = 0; i < M; i++) G[i].clear(); } int main() { while (1) { cin >> N >> M >> R; if (N == 0 && M == 0 && R == 0) break; init(); for (int i = 0; i < N; i++) scanf("%lf %lf %lf %lf", &S[i].x, &S[i].y, &S[i].z, &Sr[i]); for (int i = 0; i < M; i++) scanf("%lf %lf %lf %lf", &T[i].x, &T[i].y, &T[i].z, &Tb[i]); scanf("%lf %lf %lf", &E.x, &E.y, &E.z); for (int i = 0; i < M; i++) { Tb[i] /= norm(T[i] - E); for (int j = 0; j < N; j++) { if (getCrossCS(S[j], Sr[j], (Segment){T[i], E - T[i]})) G[i].push_back(j); } } double ans = 0; for (int i = 0; i < (1 << M); i++) { bool flg[2005] = {}; int cnt = 0; double sum = 0; for (int j = 0; j < M; j++) { if (i >> j & 1) continue; sum += Tb[j]; for (int k = 0; k < (int)G[j].size(); k++) { if (flg[G[j][k]]) continue; cnt++; flg[G[j][k]] = true; } } if (cnt > R) continue; ans = max(ans, sum); } printf("%.8f\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 0.0000000001; bool eq(double a, double b) { return (fabs(a - b) < eps); } struct Point { double x, y, z; Point operator+(Point p) { return (Point){x + p.x, y + p.y, z + p.z}; } Point operator-(Point p) { return (Point){x - p.x, y - p.y, z - p.z}; } Point operator*(double k) { return (Point){x * k, y * k, z * k}; } }; typedef Point Vector; struct Segment { Point p, v; }; typedef Segment Line; void pr(Point p) { printf("%.5f %.5f %.5f\n", p.x, p.y, p.z); } double abs(Point p) { return sqrt(p.x * p.x + p.y * p.y + p.z * p.z); } double norm(Point p) { return p.x * p.x + p.y * p.y + p.z * p.z; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y + a.z * b.z; } Point cross(Point a, Point b) { double nx = a.y * b.z - b.y * a.z; double ny = a.z * b.x - b.z * a.x; double nz = a.x * b.y - b.x * a.y; return (Point){nx, ny, nz}; } bool eq(Point a, Point b) { return (eq(a.x, b.x) && eq(a.y, b.y) && eq(a.z, b.z)); } double getDistanceLP(Line a, Point p) { Point k = p - a.p; double c = abs(cross(a.v, k)); return c / abs(a.v); } double getDistanceSP(Segment a, Point p) { Point k = p - a.p; if (dot(a.v, k) < 0) return abs(a.p - p); k = p - (a.p + a.v); if (dot(a.v * -1.0, k) < 0) return abs(a.p + a.v - p); return getDistanceLP(a, p); } bool getCrossCS(Point c, double r, Segment s) { double A = getDistanceSP(s, c); double B = abs(c - s.p); double C = abs(c - (s.p + s.v)); if (r + eps < A) return false; if (B + eps < r && C + eps < r) return false; return true; } int N, M, R; Point S[2005]; double Sr[2005]; Point E; Point T[20]; double Tb[20]; vector<int> G[20]; void init() { for (int i = 0; i < M; i++) G[i].clear(); } int main() { while (1) { cin >> N >> M >> R; if (N == 0 && M == 0 && R == 0) break; init(); for (int i = 0; i < N; i++) scanf("%lf %lf %lf %lf", &S[i].x, &S[i].y, &S[i].z, &Sr[i]); for (int i = 0; i < M; i++) scanf("%lf %lf %lf %lf", &T[i].x, &T[i].y, &T[i].z, &Tb[i]); scanf("%lf %lf %lf", &E.x, &E.y, &E.z); for (int i = 0; i < M; i++) { Tb[i] /= norm(T[i] - E); for (int j = 0; j < N; j++) { if (getCrossCS(S[j], Sr[j], (Segment){T[i], E - T[i]})) G[i].push_back(j); } } double ans = 0; for (int i = 0; i < (1 << M); i++) { bool flg[2005] = {}; int cnt = 0; double sum = 0; for (int j = 0; j < M; j++) { if (i >> j & 1) continue; sum += Tb[j]; for (int k = 0; k < (int)G[j].size(); k++) { if (flg[G[j][k]]) continue; cnt++; flg[G[j][k]] = true; } } if (cnt > R) continue; ans = max(ans, sum); } printf("%.8f\n", ans); } return 0; }
[["-", 0, 2, 3, 4, 0, 16, 31, 118, 119, 120], ["+", 0, 2, 3, 4, 0, 16, 31, 118, 119, 120]]
1
1,131
require 'set' require 'matrix' def d2(x1, y1, z1, x2, y2, z2) (x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2 end loop do n, m, removable = gets.split.map(&:to_i) break if n == 0 baloons = (1..n).map {gets.split.map(&:to_i)} lights = (1..m).map {gets.split.map(&:to_i)} x0, y0, z0 = gets.split.map(&:to_i) intensity = lights.map do |x, y, z, b| b.to_f / d2(x, y, z, x0, y0, z0) end cost = lights.map do |xl, yl, zl, _| u = Vector[xl - x0, yl - y0, zl - z0] set = 0 (0..n-1).each do |i| xb, yb, zb, r = baloons[i] v = Vector[xb - x0, yb - y0, zb - z0] w = v - u if (v.r < r) ^ (w.r < r) set |= (1 << i) else a = v.inner_product(u) / u.r if 0 < a && a < u.r && (v.r >= r) && (w.r >= r) && v.r ** 2 - a ** 2 < r * 2 set |= (1 << i) end end end set end max = 0 total_cost = [0] total_value = [0] (1..2**m-1).each do |x| y = (x & -x) i = y.to_s(2).size-1 total_cost[x] = total_cost[x - y] | cost[i] total_value[x] = total_value[x - y] + intensity[i] next if total_cost[x].to_s(2).count(?1) > removable max = total_value[x] if total_value[x] > max end p max end
require 'set' require 'matrix' def d2(x1, y1, z1, x2, y2, z2) (x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2 end loop do n, m, removable = gets.split.map(&:to_i) break if n == 0 baloons = (1..n).map {gets.split.map(&:to_i)} lights = (1..m).map {gets.split.map(&:to_i)} x0, y0, z0 = gets.split.map(&:to_i) intensity = lights.map do |x, y, z, b| b.to_f / d2(x, y, z, x0, y0, z0) end cost = lights.map do |xl, yl, zl, _| u = Vector[xl - x0, yl - y0, zl - z0] set = 0 (0..n-1).each do |i| xb, yb, zb, r = baloons[i] v = Vector[xb - x0, yb - y0, zb - z0] w = v - u if (v.r < r) ^ (w.r < r) set |= (1 << i) else a = v.inner_product(u) / u.r if 0 < a && a < u.r && (v.r >= r) && (w.r >= r) && v.r ** 2 - a ** 2 < r ** 2 set |= (1 << i) end end end set end max = 0 total_cost = [0] total_value = [0] (1..2**m-1).each do |x| y = (x & -x) i = y.to_s(2).size-1 total_cost[x] = total_cost[x - y] | cost[i] total_value[x] = total_value[x - y] + intensity[i] next if total_cost[x].to_s(2).count(?1) > removable max = total_value[x] if total_value[x] > max end p max end
[["-", 0, 121, 15, 738, 12, 738, 12, 738, 17, 48], ["+", 0, 121, 15, 738, 12, 738, 12, 738, 17, 578]]
4
441
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; inline int f(int n, int i, int L, const vector<int> &sum_rev) { return (sum_rev[i - n] - sum_rev[i]) + L * (n - 1); } int dp[50010]; int sum_dp[50010]; int main() { int W, N; while (cin >> W >> N && N) { vector<int> x(N); REP(i, N) cin >> x[i]; vector<int> sum_rev(N + 1); sum_rev[N] = 0; for (int i = N - 1; i >= 0; i--) { sum_rev[i] = sum_rev[i + 1] + x[i]; } int lb_L = 0, ub_L = W; // ub while (ub_L - lb_L > 1) { int L = (lb_L + ub_L) / 2; dp[0] = 1; dp[1] = 0; sum_dp[1] = 1; sum_dp[2] = 1; for (int i = 2; i < N; i++) { dp[i] = 0; sum_dp[i] = sum_dp[i - 1] + dp[i - 1]; int lb = 1, ub = min(i, (W + L) / (L + 1)); // ub true ?? while (ub - lb > 1) { int n = (ub + lb) / 2; if (f(n, i, L, sum_rev) >= W) { ub = n; } else { lb = n; } } int begin_n = ub; // printf("dp[%d] : begin %d \n", i, begin_n); if (f(begin_n, i, L, sum_rev) < W) { continue; } lb = begin_n, ub = i + 1; // lb while (ub - lb > 1) { int n = (ub + lb) / 2; if (f(n, i, 1, sum_rev) <= W) { lb = n; } else { ub = n; } } int end_n = lb; // printf("dp[%d] : end %d \n", i, end_n); if (f(end_n, i, 1, sum_rev) > W) { continue; } int begin = i - end_n; int end = i - begin_n; // [begin, end] if (sum_dp[end + 1] - sum_dp[begin] > 0) { dp[i] = 1; } // printf("dp[%d] : [dp[%d], dp[%d]] (%d) -> %d\n", i, begin, end, // sum_dp[end + 1] - sum_dp[begin], dp[i]); } bool ok = false; for (int n = 1; n <= N && (n - 1) + sum_rev[N - n] <= W; n++) { // printf("n - 1 + sum_rev[%d] = %d\n", i, n - 1 + sum_rev[i]); if (dp[N - n]) { ok = true; break; } } // printf("L %d is %d\n", L, ok); if (ok) { ub_L = L; } else { lb_L = L; } } cout << ub_L << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; inline int f(int n, int i, int L, const vector<int> &sum_rev) { return (sum_rev[i - n] - sum_rev[i]) + L * (n - 1); } int dp[50010]; int sum_dp[50010]; int main() { int W, N; while (cin >> W >> N && N) { vector<int> x(N); REP(i, N) cin >> x[i]; vector<int> sum_rev(N + 1); sum_rev[N] = 0; for (int i = N - 1; i >= 0; i--) { sum_rev[i] = sum_rev[i + 1] + x[i]; } int lb_L = 0, ub_L = W; // ub while (ub_L - lb_L > 1) { int L = (lb_L + ub_L) / 2; dp[0] = 1; dp[1] = 0; sum_dp[1] = 1; sum_dp[2] = 1; for (int i = 2; i < N; i++) { dp[i] = 0; sum_dp[i] = sum_dp[i - 1] + dp[i - 1]; int lb = 1, ub = min(i, (W + L) / (L + 1) + 2); // ub true ?? while (ub - lb > 1) { int n = (ub + lb) / 2; if (f(n, i, L, sum_rev) >= W) { ub = n; } else { lb = n; } } int begin_n = ub; // printf("dp[%d] : begin %d \n", i, begin_n); if (f(begin_n, i, L, sum_rev) < W) { continue; } lb = begin_n, ub = i + 1; // lb while (ub - lb > 1) { int n = (ub + lb) / 2; if (f(n, i, 1, sum_rev) <= W) { lb = n; } else { ub = n; } } int end_n = lb; // printf("dp[%d] : end %d \n", i, end_n); if (f(end_n, i, 1, sum_rev) > W) { continue; } int begin = i - end_n; int end = i - begin_n; // [begin, end] if (sum_dp[end + 1] - sum_dp[begin] > 0) { dp[i] = 1; } // printf("dp[%d] : [dp[%d], dp[%d]] (%d) -> %d\n", i, begin, end, // sum_dp[end + 1] - sum_dp[begin], dp[i]); } bool ok = false; for (int n = 1; n <= N && (n - 1) + sum_rev[N - n] <= W; n++) { // printf("n - 1 + sum_rev[%d] = %d\n", i, n - 1 + sum_rev[i]); if (dp[N - n]) { ok = true; break; } } // printf("L %d is %d\n", L, ok); if (ok) { ub_L = L; } else { lb_L = L; } } cout << ub_L << endl; } return 0; }
[["+", 49, 50, 51, 2, 3, 4, 0, 16, 17, 72], ["+", 49, 50, 51, 2, 3, 4, 0, 16, 12, 13]]
1
579
import java.util.*; public class Main { public void solve() { Scanner sc = new Scanner(System.in); while (true) { int N = sc.nextInt(); int K = sc.nextInt(); int S = sc.nextInt(); while (N + K + S == 0) break; int ans = 0; for (int i = 0; i < (1 << N); i++) { if (Integer.bitCount(i) == K) { int sum = 0; for (int j = 0; j < N; j++) { if ((i & (1 << j)) != 0) { sum += (j + 1); } } if (sum == S) ans++; } } System.out.println(ans); } } public static void main(String[] args) { new Main().solve(); } }
import java.util.*; public class Main { public void solve() { Scanner sc = new Scanner(System.in); while (true) { int N = sc.nextInt(); int K = sc.nextInt(); int S = sc.nextInt(); if (N + K + S == 0) break; int ans = 0; for (int i = 0; i < (1 << N); i++) { if (Integer.bitCount(i) == K) { int sum = 0; for (int j = 0; j < N; j++) { if ((i & (1 << j)) != 0) { sum += (j + 1); } } if (sum == S) ans++; } } System.out.println(ans); } } public static void main(String[] args) { new Main().solve(); } }
[["-", 8, 196, 0, 52, 8, 196, 0, 52, 0, 89], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 121]]
3
197
#include <bits/stdc++.h> using namespace std; int main() { int n, k, s, f = 0; while (scanf("%d%d%d", &n, &k, &s) && n != 0 && k != 0 && s != 0) { int ans = 0; for (int i = 1; i < 1 << n; i++) { int sum = 0, cnt = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum += (j + 1); cnt++; } } if (cnt == k && sum == s) ans++; } printf("%d", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k, s, f = 0; while (scanf("%d%d%d", &n, &k, &s) && n != 0 && k != 0 && s != 0) { int ans = 0; for (int i = 1; i < 1 << n; i++) { int sum = 0, cnt = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum += (j + 1); cnt++; } } if (cnt == k && sum == s) ans++; } printf("%d\n", ans); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
151
#include <iostream> using namespace std; int main() { int n, k, s, ans, t, u; while (cin >> n >> k >> s, n) { int A[k]; if (k == 1) { if (n >= s) cout << 1 << endl; else cout << 0 << endl; } for (int i = 0; i < k - 1; ++i) A[i] = i + 1; A[k - 1] = s - k * (k - 1) / 2; ans = 0; t = k - 2; while (A[k - 1] > A[k - 2]) { if (A[k - 1] <= n) ++ans; ++A[t]; --A[t + 1]; if (A[t] >= A[t + 1]) { while (t > 0) { --t; ++A[t]; u = 0; for (int i = 0; i <= t; ++i) u += A[i]; for (int i = t + 1; i < k - 1; ++i) { A[i] = A[i - 1] + 1; u += A[i]; } A[k - 1] = s - u; if (A[k - 1] > A[k - 2]) { t = k - 2; break; } } if (!t) break; } } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n, k, s, ans, t, u; while (cin >> n >> k >> s, n) { int A[k]; if (k == 1) { if (n >= s) cout << 1 << endl; else cout << 0 << endl; continue; } for (int i = 0; i < k - 1; ++i) A[i] = i + 1; A[k - 1] = s - k * (k - 1) / 2; ans = 0; t = k - 2; while (A[k - 1] > A[k - 2]) { if (A[k - 1] <= n) ++ans; ++A[t]; --A[t + 1]; if (A[t] >= A[t + 1]) { while (t > 0) { --t; ++A[t]; u = 0; for (int i = 0; i <= t; ++i) u += A[i]; for (int i = t + 1; i < k - 1; ++i) { A[i] = A[i - 1] + 1; u += A[i]; } A[k - 1] = s - u; if (A[k - 1] > A[k - 2]) { t = k - 2; break; } } if (!t) break; } } cout << ans << endl; } return 0; }
[["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 35]]
1
324
while (h, w = gets.split.map(&:to_i)).inject(:+) != 0 do donut = Array.new count = Hash.new(0) h.times do donut.push(gets.chomp.chars) end h.times do |y| w.times do |x| dy = [0, 1, 1, 1, 0, -1, -1, -1] dx = [1, 1, 0, -1, -1, -1, 0, 1] 8.times do |i| used = Array.new(h){Array.new(w,false)} string = donut[y][x] used[y][x] = true ny = (y + dy[i]) % h nx = (x + dx[i]) % w while !used[ny][nx] do string += donut[ny][nx] count[string] += 1 used[ny][nx] = true ny = (ny + dy[i]) % h nx = (nx + dx[i]) % w end end end end spell = count.select{|key, value| value >= 2}.keys.sort{|v1, v2| v1.size <=> v2.size} spell.select!{|v| v.size == spell.last.size} puts spell.size >= 1 ? spell.sort.last : 0 end
while (h, w = gets.split.map(&:to_i)).inject(:+) != 0 do donut = Array.new count = Hash.new(0) h.times do donut.push(gets.chomp.chars) end h.times do |y| w.times do |x| dy = [0, 1, 1, 1, 0, -1, -1, -1] dx = [1, 1, 0, -1, -1, -1, 0, 1] 8.times do |i| used = Array.new(h){Array.new(w,false)} string = donut[y][x] used[y][x] = true ny = (y + dy[i]) % h nx = (x + dx[i]) % w while !used[ny][nx] do string += donut[ny][nx] count[string] += 1 used[ny][nx] = true ny = (ny + dy[i]) % h nx = (nx + dx[i]) % w end end end end spell = count.select{|key, value| value >= 2}.keys.sort{|v1, v2| v1.size <=> v2.size} spell.select!{|v| v.size == spell.last.size} puts spell.size >= 1 ? spell.sort.first : 0 end
[["-", 0, 652, 3, 4, 0, 754, 64, 652, 735, 22], ["+", 0, 652, 3, 4, 0, 754, 64, 652, 735, 22]]
4
304
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> v[6005], v2[6005]; map<string, int> mp; int mpn, n, m, k; bool c[6005]; unsigned dis[6005]; char a[20]; int b[305]; queue<int> q; inline int s2i(string x) { if (!mp.count(x)) return mp[x] = mpn++; return mp[x]; } void spfa(int st) { int i, j, x, y; memset(dis, -1, sizeof(dis)); dis[st] = 0; q.push(st); while (!q.empty()) { x = q.front(); q.pop(); for (i = 0, j = v[x].size(); i < j; i++) { y = v[x][i].first; if (dis[x] + v[x][i].second > (unsigned)k || dis[x] + v[x][i].second > dis[y]) continue; dis[y] = dis[x] + v[x][i].second; q.push(y); } } for (i = 0; i < n; i++) if (c[i] && i != st && dis[i] != -1) v2[st].push_back(make_pair(i, dis[i])); } int main() { int st, ed, i, j, x, y, w; while (~scanf("%d%d%d", &n, &m, &k) && (n + m + k)) { memset(c, 0, sizeof(c)); mpn = 0; k *= 10; scanf("%s", a); st = s2i(a); scanf("%s", a); ed = s2i(a); for (i = 0; i < n; i++) { scanf("%s", a); x = s2i(a); scanf("%s", a); y = s2i(a); scanf("%d", &w); v[x].push_back(make_pair(y, w)); v[y].push_back(make_pair(x, w)); } for (i = 0; i < m; i++) { scanf("%s", a); c[b[i] = s2i(a)] = 1; } c[st] = c[ed] = 1; spfa(st); for (i = 0; i < m; i++) spfa(b[i]); memset(dis, -1, sizeof(dis)); dis[st] = 0; q.push(st); while (!q.empty()) { x = q.front(); q.pop(); for (i = 0, j = v2[x].size(); i < j; i++) { y = v2[x][i].first; if (dis[x] + v2[x][i].second < dis[y]) { dis[y] = dis[x] + v2[x][i].second; q.push(y); } } } printf("%d\n", dis[ed]); for (i = 0; i < mpn; i++) v[i].clear(), v2[i].clear(); mp.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> v[6005], v2[6005]; map<string, int> mp; int mpn, n, m, k; bool c[6005]; unsigned dis[6005]; char a[20]; int b[305]; queue<int> q; inline int s2i(string x) { if (!mp.count(x)) return mp[x] = mpn++; return mp[x]; } void spfa(int st) { int i, j, x, y; memset(dis, -1, sizeof(dis)); dis[st] = 0; q.push(st); while (!q.empty()) { x = q.front(); q.pop(); for (i = 0, j = v[x].size(); i < j; i++) { y = v[x][i].first; if (dis[x] + v[x][i].second > (unsigned)k || dis[x] + v[x][i].second > dis[y]) continue; dis[y] = dis[x] + v[x][i].second; q.push(y); } } for (i = 0; i < mpn; i++) if (c[i] && i != st && dis[i] != -1) v2[st].push_back(make_pair(i, dis[i])); } int main() { int st, ed, i, j, x, y, w; while (~scanf("%d%d%d", &n, &m, &k) && (n + m + k)) { memset(c, 0, sizeof(c)); mpn = 0; k *= 10; scanf("%s", a); st = s2i(a); scanf("%s", a); ed = s2i(a); for (i = 0; i < n; i++) { scanf("%s", a); x = s2i(a); scanf("%s", a); y = s2i(a); scanf("%d", &w); v[x].push_back(make_pair(y, w)); v[y].push_back(make_pair(x, w)); } for (i = 0; i < m; i++) { scanf("%s", a); c[b[i] = s2i(a)] = 1; } c[st] = c[ed] = 1; spfa(st); for (i = 0; i < m; i++) spfa(b[i]); memset(dis, -1, sizeof(dis)); dis[st] = 0; q.push(st); while (!q.empty()) { x = q.front(); q.pop(); for (i = 0, j = v2[x].size(); i < j; i++) { y = v2[x][i].first; if (dis[x] + v2[x][i].second < dis[y]) { dis[y] = dis[x] + v2[x][i].second; q.push(y); } } } printf("%d\n", dis[ed]); for (i = 0; i < mpn; i++) v[i].clear(), v2[i].clear(); mp.clear(); } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
1
791
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define REP(i, n) for (int i = n - 1; i >= (0); i--) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define all(v) (v).begin(), (v).end() #define eall(v) unique(all(v), v.end()) #define pb push_back #define mp make_pair #define fi first #define se second #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) const int MOD = 1e9 + 7; const int INF = 1e9; const ll INFF = 1e18; map<string, int> city2num; set<int> LPG; vector<pair<int, int>> G[6010]; int dist[6010][2010]; // dist[i][j] := // ??????i?????§??????LPG???j??§??\????????????????????¢ using tup = tuple<int, int, int>; void dijkstra(int start, int cap) { rep(i, 6010) rep(j, 2010) dist[i][j] = INF; cap *= 10; dist[start][cap] = 0; priority_queue<tup, vector<tup>, greater<tup>> que; // ?????¢ ?????? ?????????LPG que.push(make_tuple(0, start, cap)); while (!que.empty()) { int cost, u, lpg; tie(cost, u, lpg) = que.top(); que.pop(); if (dist[u][lpg] < cost) continue; for (auto &tmp : G[u]) { int v = tmp.first, di = tmp.second; int nlpg = lpg - di; int ncost = cost + di; if (nlpg < 0) continue; if (LPG.count(v)) { /* for (int i = nlpg; i <= cap; ++i){ if(dist[v][i] > ncost) { dist[v][i] = ncost; que.push(make_tuple(ncost, v, i)); } } */ if (dist[v][cap] > ncost) { dist[v][cap] = ncost; que.push(make_tuple(ncost, v, cap)); } } else { if (dist[v][nlpg] > ncost) { dist[v][nlpg] = ncost; que.push(make_tuple(ncost, v, nlpg)); } } } } } string c1[6010], c2[6010]; int d[6010]; string s[6010]; int main(void) { while (1) { city2num.clear(); LPG.clear(); rep(i, 6010) G[i].clear(); int N, M, cap; scanf("%d %d %d", &N, &M, &cap); if (N == 0 && M == 0 && cap == 0) break; string src, dest; cin >> src >> dest; rep(i, N) cin >> c1[i] >> c2[i] >> d[i]; rep(i, M) cin >> s[i]; set<string> city; rep(i, N) city.insert(c1[i]), city.insert(c2[i]); int cnt = 0; for (auto &u : city) { city2num[u] = cnt++; } rep(i, M) LPG.insert(city2num[s[i]]); rep(i, N) G[city2num[c1[i]]].pb(mp(city2num[c2[i]], d[i])), G[city2num[c2[i]]].pb(mp(city2num[c1[i]], d[i])); int ns = city2num[src], nd = city2num[dest]; dijkstra(ns, cap); ll ans = INF; rep(i, cap + 1) chmin(ans, dist[nd][i]); if (ans != INF) printf("%lld\n", ans); else printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define REP(i, n) for (int i = n - 1; i >= (0); i--) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define all(v) (v).begin(), (v).end() #define eall(v) unique(all(v), v.end()) #define pb push_back #define mp make_pair #define fi first #define se second #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) const int MOD = 1e9 + 7; const int INF = 1e9; const ll INFF = 1e18; map<string, int> city2num; set<int> LPG; vector<pair<int, int>> G[6010]; int dist[6010][2010]; // dist[i][j] := // ??????i?????§??????LPG???j??§??\????????????????????¢ using tup = tuple<int, int, int>; void dijkstra(int start, int cap) { rep(i, 6010) rep(j, 2010) dist[i][j] = INF; cap *= 10; dist[start][cap] = 0; priority_queue<tup, vector<tup>, greater<tup>> que; // ?????¢ ?????? ?????????LPG que.push(make_tuple(0, start, cap)); while (!que.empty()) { int cost, u, lpg; tie(cost, u, lpg) = que.top(); que.pop(); // if(u == goal) dist[u][lpg]; // printf("cost %d u %d lpg %d\n", cost, u, lpg); if (dist[u][lpg] < cost) continue; for (auto &tmp : G[u]) { int v = tmp.first, di = tmp.second; int nlpg = lpg - di; int ncost = cost + di; // printf("v %d di %d nlpg %d ncost %d\n", v, di, nlpg, ncost); if (nlpg < 0) continue; if (LPG.count(v)) { /* for (int i = nlpg; i <= cap; ++i){ if(dist[v][i] > ncost) { dist[v][i] = ncost; que.push(make_tuple(ncost, v, i)); } } */ if (dist[v][cap] > ncost) { dist[v][cap] = ncost; // printf("dist[%d][%d] = %d\n", v, cap, ncost); que.push(make_tuple(ncost, v, cap)); } } else { if (dist[v][nlpg] > ncost) { dist[v][nlpg] = ncost; // printf("dist[%d][%d] = %d\n", v, nlpg, ncost); que.push(make_tuple(ncost, v, nlpg)); } } } } } string c1[6010], c2[6010]; int d[6010]; string s[6010]; int main(void) { while (1) { city2num.clear(); LPG.clear(); rep(i, 6010) G[i].clear(); int N, M, cap; scanf("%d %d %d", &N, &M, &cap); if (N == 0 && M == 0 && cap == 0) break; string src, dest; cin >> src >> dest; rep(i, N) cin >> c1[i] >> c2[i] >> d[i]; rep(i, M) cin >> s[i]; set<string> city; rep(i, N) city.insert(c1[i]), city.insert(c2[i]); int cnt = 0; for (auto &u : city) { city2num[u] = cnt++; } rep(i, M) LPG.insert(city2num[s[i]]); rep(i, N) G[city2num[c1[i]]].pb(mp(city2num[c2[i]], d[i])), G[city2num[c2[i]]].pb(mp(city2num[c1[i]], d[i])); int ns = city2num[src], nd = city2num[dest]; dijkstra(ns, cap); ll ans = INF; rep(i, cap * 10 + 1) chmin(ans, dist[nd][i]); if (ans != INF) printf("%lld\n", ans); else printf("-1\n"); } return 0; }
[["+", 0, 2, 3, 4, 0, 16, 31, 16, 17, 48], ["+", 0, 2, 3, 4, 0, 16, 31, 16, 12, 13]]
1
822
#include <algorithm> #include <deque> #include <iostream> #include <map> #include <queue> #include <string> #include <utility> using namespace std; #define INF 1000000000 int N, M, cap; string cityfrom, cityto; string cityname[3000][2]; string citystat[300]; int d[3000]; typedef pair<int, int> pii; typedef priority_queue<pii, deque<pii>, greater<pii>> pq_t; int solve() { map<string, int> m_city; for (int i = 0; i < N; ++i) { m_city.insert(make_pair(cityname[i][0], m_city.size())); m_city.insert(make_pair(cityname[i][1], m_city.size())); } int n = m_city.size(); vector<vector<pii>> road(n); for (int i = 0; i < N; ++i) { int a = m_city[cityname[i][0]]; int b = m_city[cityname[i][1]]; road[a].push_back(pii(b, d[i])); road[b].push_back(pii(a, d[i])); } vector<int> nstat(M + 2); //スタンドの番号。 //ただし最初2つは出発地と目的地の番号としておく。 nstat[0] = m_city[cityfrom]; nstat[1] = m_city[cityto]; for (int i = 0; i < M; ++i) { nstat[i + 2] = m_city[citystat[i]]; } cap *= 10; vector<vector<int>> dist(nstat.size(), vector<int>(n, INF)); for (int from = 0; from < nstat.size(); ++from) { int n_from = nstat[from]; dist[from][n_from] = 0; pq_t pq; pq.push(pii(0, nstat[from])); while (!pq.empty()) { pii p = pq.top(); pq.pop(); int n_f = p.second; if (dist[from][n_f] != p.first) { continue; } for (int i = 0; i < road[n_from].size(); ++i) { int n_to = road[n_from][i].first; int ds = p.first + road[n_from][i].second; if (ds < cap && dist[from][n_to] > ds) { dist[from][n_to] = ds; pq.push(pii(ds, n_to)); } } } } vector<int> dp(nstat.size(), INF); dp[0] = 0; pq_t pq; pq.push(make_pair(0, 0)); while (!pq.empty()) { pii p = pq.top(); pq.pop(); int from = p.second; if (dp[from] != p.first) { continue; } int n_from = nstat[from]; for (int to = 0; to < nstat.size(); ++to) { int n_to = nstat[to]; if (dist[from][n_to] <= cap && dp[to] > dp[from] + dist[from][n_to]) { dp[to] = dp[from] + dist[from][n_to]; pq.push(make_pair(dp[to], to)); } } } if (dp[1] == INF) { return -1; } return dp[1]; } int main() { for (; cin >> N >> M >> cap >> cityfrom >> cityto;) { for (int i = 0; i < N; ++i) { cin >> cityname[i][0] >> cityname[i][1] >> d[i]; } for (int i = 0; i < M; ++i) { cin >> citystat[i]; } cout << solve() << endl; } }
#include <algorithm> #include <deque> #include <iostream> #include <map> #include <queue> #include <string> #include <utility> using namespace std; #define INF 1000000000 int N, M, cap; string cityfrom, cityto; string cityname[3000][2]; string citystat[300]; int d[3000]; typedef pair<int, int> pii; typedef priority_queue<pii, deque<pii>, greater<pii>> pq_t; int solve() { map<string, int> m_city; for (int i = 0; i < N; ++i) { m_city.insert(make_pair(cityname[i][0], m_city.size())); m_city.insert(make_pair(cityname[i][1], m_city.size())); } int n = m_city.size(); vector<vector<pii>> road(n); for (int i = 0; i < N; ++i) { int a = m_city[cityname[i][0]]; int b = m_city[cityname[i][1]]; road[a].push_back(pii(b, d[i])); road[b].push_back(pii(a, d[i])); } vector<int> nstat(M + 2); //スタンドの番号。 //ただし最初2つは出発地と目的地の番号としておく。 nstat[0] = m_city[cityfrom]; nstat[1] = m_city[cityto]; for (int i = 0; i < M; ++i) { nstat[i + 2] = m_city[citystat[i]]; } cap *= 10; vector<vector<int>> dist(nstat.size(), vector<int>(n, INF)); for (int from = 0; from < nstat.size(); ++from) { int n_from = nstat[from]; dist[from][n_from] = 0; pq_t pq; pq.push(pii(0, nstat[from])); while (!pq.empty()) { pii p = pq.top(); pq.pop(); int n_f = p.second; if (dist[from][n_f] != p.first) { continue; } for (int i = 0; i < road[n_f].size(); ++i) { int n_to = road[n_f][i].first; int ds = p.first + road[n_f][i].second; if (ds <= cap && dist[from][n_to] > ds) { dist[from][n_to] = ds; pq.push(pii(ds, n_to)); } } } } vector<int> dp(nstat.size(), INF); dp[0] = 0; pq_t pq; pq.push(make_pair(0, 0)); while (!pq.empty()) { pii p = pq.top(); pq.pop(); int from = p.second; if (dp[from] != p.first) { continue; } int n_from = nstat[from]; for (int to = 0; to < nstat.size(); ++to) { int n_to = nstat[to]; if (dist[from][n_to] <= cap && dp[to] > dp[from] + dist[from][n_to]) { dp[to] = dp[from] + dist[from][n_to]; pq.push(make_pair(dp[to], to)); } } } if (dp[1] == INF) { return -1; } return dp[1]; } int main() { for (; cin >> N >> M >> cap >> cityfrom >> cityto;) { for (int i = 0; i < N; ++i) { cin >> cityname[i][0] >> cityname[i][1] >> d[i]; } for (int i = 0; i < M; ++i) { cin >> citystat[i]; } cout << solve() << endl; } }
[["-", 12, 2, 63, 118, 28, 69, 341, 342, 0, 22], ["+", 12, 2, 63, 118, 28, 69, 341, 342, 0, 22], ["-", 51, 118, 28, 69, 28, 69, 341, 342, 0, 22], ["+", 51, 118, 28, 69, 28, 69, 341, 342, 0, 22], ["-", 12, 118, 28, 69, 28, 69, 341, 342, 0, 22], ["+", 12, 118, 28, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 19]]
1
849
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #if __GNUC__ #include <tr1/unordered_map> #include <tr1/unordered_set> using namespace tr1; #else #include <unordered_map> #include <unordered_set> #endif #ifdef __GNUC__ template <class T> int popcount(T n); template <> int popcount(unsigned int n) { return __builtin_popcount(n); } template <> int popcount(int n) { return __builtin_popcount(n); } template <> int popcount(unsigned long long n) { return __builtin_popcountll(n); } template <> int popcount(long long n) { return __builtin_popcountll(n); } #else #define __typeof__ decltype template <class T> int popcount(T n) { return n ? 1 + popcount(n & (n - 1)) : 0; } #endif #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define rforeach(it, c) \ for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CL(arr, val) memset(arr, val, sizeof(arr)) #define COPY(dest, src) memcpy(dest, src, sizeof(dest)) template <class T> void max_swap(T &a, const T &b) { a = max(a, b); } template <class T> void min_swap(T &a, const T &b) { a = min(a, b); } typedef long long ll; typedef pair<int, int> pint; template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) { os << "( " << p.first << ", " << p.second << " )"; return os; } template <class T> string to_s(const T &a) { ostringstream os; os << a; return os.str(); } bool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } template <class T> void print(T a, int n, int br = 1, const string &deli = ", ") { cout << "{ "; for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } cout << " }"; while (br--) cout << endl; } template <class T> void print(const vector<T> &v, int br = 1, const string &deli = ", ") { print(v, v.size(), br, deli); } template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; } template <class T> void input(T &a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <class T, class U> void input(T &a, U &b, int n) { for (int i = 0; i < n; ++i) cin >> a[i] >> b[i]; } const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int mod = 1000000007; const double PI = acos(-1.0); struct P { int p, c, f; P(int p, int c, int f) : p(p), c(c), f(f) {} bool operator<(const P &b) const { return c < b.c; } }; int nono(map<string, int> &no, const string &s) { if (!no.count(s)) { int t = no.size(); no[s] = t; } return no[s]; } int main() { ios::sync_with_stdio(false); int n, m, cap; while (cin >> n >> m >> cap, n) { cap *= 10; vector<vector<pint>> e(2 * n); // pint(cost, to) bool gas[6010]; CL(gas, 0); map<string, int> no; const int src = 0, dest = 1; { string a, b; cin >> a >> b; no[a] = src, no[b] = dest; for (int i = 0; i < n; ++i) { int d; cin >> a >> b >> d; int x = nono(no, a), y = nono(no, b); e[x].push_back(pint(d, y)); e[y].push_back(pint(d, x)); } for (int i = 0; i < m; ++i) cin >> a, gas[nono(no, a)] = true; } const int INF = 1 << 30; static int dis[6010][2010]; for (int i = 0; i < no.size(); ++i) for (int j = 0; j <= cap; ++j) dis[i][j] = INF; priority_queue<P> q; dis[src][cap] = 0; q.push(P(src, 0, cap)); int res = -1; while (!q.empty()) { P cur = q.top(); q.pop(); int p = cur.p, c = cur.c, f = cur.f; if (c > dis[p][f]) continue; else if (p == dest) { res = c; break; } for (int i = 0; i < e[p].size(); ++i) { int to = e[p][i].second; int d = e[p][i].first; int nc = c + d; int nf = f - d; if (nf >= 0) { if (gas[to]) nf = cap; if (nc < dis[to][nf]) { dis[to][nf] = nc; q.push(P(to, nc, nf)); } } } } cout << res << endl; } }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #if __GNUC__ #include <tr1/unordered_map> #include <tr1/unordered_set> using namespace tr1; #else #include <unordered_map> #include <unordered_set> #endif #ifdef __GNUC__ template <class T> int popcount(T n); template <> int popcount(unsigned int n) { return __builtin_popcount(n); } template <> int popcount(int n) { return __builtin_popcount(n); } template <> int popcount(unsigned long long n) { return __builtin_popcountll(n); } template <> int popcount(long long n) { return __builtin_popcountll(n); } #else #define __typeof__ decltype template <class T> int popcount(T n) { return n ? 1 + popcount(n & (n - 1)) : 0; } #endif #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define rforeach(it, c) \ for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CL(arr, val) memset(arr, val, sizeof(arr)) #define COPY(dest, src) memcpy(dest, src, sizeof(dest)) template <class T> void max_swap(T &a, const T &b) { a = max(a, b); } template <class T> void min_swap(T &a, const T &b) { a = min(a, b); } typedef long long ll; typedef pair<int, int> pint; template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) { os << "( " << p.first << ", " << p.second << " )"; return os; } template <class T> string to_s(const T &a) { ostringstream os; os << a; return os.str(); } bool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } template <class T> void print(T a, int n, int br = 1, const string &deli = ", ") { cout << "{ "; for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } cout << " }"; while (br--) cout << endl; } template <class T> void print(const vector<T> &v, int br = 1, const string &deli = ", ") { print(v, v.size(), br, deli); } template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; } template <class T> void input(T &a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <class T, class U> void input(T &a, U &b, int n) { for (int i = 0; i < n; ++i) cin >> a[i] >> b[i]; } const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int mod = 1000000007; const double PI = acos(-1.0); struct P { int p, c, f; P(int p, int c, int f) : p(p), c(c), f(f) {} bool operator<(const P &b) const { return c > b.c; } }; int nono(map<string, int> &no, const string &s) { if (!no.count(s)) { int t = no.size(); no[s] = t; } return no[s]; } int main() { ios::sync_with_stdio(false); int n, m, cap; while (cin >> n >> m >> cap, n) { cap *= 10; vector<vector<pint>> e(2 * n); // pint(cost, to) bool gas[6010]; CL(gas, 0); map<string, int> no; const int src = 0, dest = 1; { string a, b; cin >> a >> b; no[a] = src, no[b] = dest; for (int i = 0; i < n; ++i) { int d; cin >> a >> b >> d; int x = nono(no, a), y = nono(no, b); e[x].push_back(pint(d, y)); e[y].push_back(pint(d, x)); } for (int i = 0; i < m; ++i) cin >> a, gas[nono(no, a)] = true; } const int INF = 1 << 30; static int dis[6010][2010]; for (int i = 0; i < no.size(); ++i) for (int j = 0; j <= cap; ++j) dis[i][j] = INF; priority_queue<P> q; dis[src][cap] = 0; q.push(P(src, 0, cap)); int res = -1; while (!q.empty()) { P cur = q.top(); q.pop(); int p = cur.p, c = cur.c, f = cur.f; if (c > dis[p][f]) continue; else if (p == dest) { res = c; break; } for (int i = 0; i < e[p].size(); ++i) { int to = e[p][i].second; int d = e[p][i].first; int nc = c + d; int nf = f - d; if (nf >= 0) { if (gas[to]) nf = cap; if (nc < dis[to][nf]) { dis[to][nf] = nc; q.push(P(to, nc, nf)); } } } } cout << res << endl; } }
[["-", 0, 14, 8, 9, 0, 37, 0, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 17, 47]]
1
1,424
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; struct edge { int to; int cost; }; int d[2001][6011]; vector<edge> G[6011]; bool isGas[10001]; map<string, int> m; int idx; int st, gl; int N, M, cap; const int INF = 1 << 30; typedef pair<int, int> pii; typedef pair<int, pii> Sit; int dijkstra() { for (int i = 0; i < 2001; i++) for (int j = 0; j < 6011; j++) d[i][j] = INF; priority_queue<Sit, vector<Sit>, greater<Sit>> pq; pq.push(make_pair(0, pii(cap * 10, st))); d[cap * 10][st] = 0; while (pq.size()) { Sit p = pq.top(); pq.pop(); int ccost = p.first; int ccap = p.second.first; int cnode = p.second.second; if (d[ccap][cnode] < ccost) continue; for (int i = 0; i < (int)G[cnode].size(); i++) { edge &e = G[cnode][i]; int ncap = ccap - e.cost; if (ncap < 0) continue; int nnode = e.to; int ncost = ccost + e.cost; if (isGas[nnode]) ncap = cap * 10; if (d[ncap][nnode] > ncost) { d[ncap][nnode] = ncost; pq.push(make_pair(ncost, pii(ncap, nnode))); } } } int res = INF; for (int i = 0; i < 2001; i++) res = min(res, d[i][gl]); return res; } int main() { while (cin >> N >> M >> cap && (N | M | cap)) { memset(isGas, 0, sizeof(isGas)); for (int i = 0; i < 6011; i++) G[i].clear(); idx = 0; string s, t; cin >> s >> t; if (m.count(s) == 0) m[s] = idx++; if (m.count(t) == 0) m[t] = idx++; st = m[s]; gl = m[t]; for (int i = 0; i < N; i++) { string a, b; int c; cin >> a >> b >> c; if (m.count(a) == 0) m[a] = idx++; if (m.count(b) == 0) m[b] = idx++; int x, y; x = m[a]; y = m[b]; edge e; e.to = y; e.cost = c; G[x].push_back(e); e.to = x; G[y].push_back(e); } for (int i = 0; i < M; i++) { string s; cin >> s; if (m.count(s) == 0) m[s] = idx++; isGas[m[s]] = true; } int res = dijkstra(); if (res == INF) cout << -1 << endl; else cout << res << endl; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; struct edge { int to; int cost; }; int d[2001][6011]; vector<edge> G[6011]; bool isGas[10001]; map<string, int> m; int idx; int st, gl; int N, M, cap; const int INF = 1 << 30; typedef pair<int, int> pii; typedef pair<int, pii> Sit; int dijkstra() { for (int i = 0; i < 2001; i++) for (int j = 0; j < 6011; j++) d[i][j] = INF; priority_queue<Sit, vector<Sit>, greater<Sit>> pq; pq.push(make_pair(0, pii(cap * 10, st))); d[cap * 10][st] = 0; while (pq.size()) { Sit p = pq.top(); pq.pop(); int ccost = p.first; int ccap = p.second.first; int cnode = p.second.second; if (d[ccap][cnode] < ccost) continue; for (int i = 0; i < (int)G[cnode].size(); i++) { edge &e = G[cnode][i]; int ncap = ccap - e.cost; if (ncap < 0) continue; int nnode = e.to; int ncost = ccost + e.cost; if (isGas[nnode]) ncap = cap * 10; if (d[ncap][nnode] > ncost) { d[ncap][nnode] = ncost; pq.push(make_pair(ncost, pii(ncap, nnode))); } } } int res = INF; for (int i = 0; i < 2001; i++) res = min(res, d[i][gl]); return res; } int main() { while (cin >> N >> M >> cap && (N | M | cap)) { memset(isGas, 0, sizeof(isGas)); for (int i = 0; i < 6011; i++) G[i].clear(); m.clear(); idx = 0; string s, t; cin >> s >> t; if (m.count(s) == 0) m[s] = idx++; if (m.count(t) == 0) m[t] = idx++; st = m[s]; gl = m[t]; for (int i = 0; i < N; i++) { string a, b; int c; cin >> a >> b >> c; if (m.count(a) == 0) m[a] = idx++; if (m.count(b) == 0) m[b] = idx++; int x, y; x = m[a]; y = m[b]; edge e; e.to = y; e.cost = c; G[x].push_back(e); e.to = x; G[y].push_back(e); } for (int i = 0; i < M; i++) { string s; cin >> s; if (m.count(s) == 0) m[s] = idx++; isGas[m[s]] = true; } int res = dijkstra(); if (res == INF) cout << -1 << endl; else cout << res << endl; } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
756
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; class Edge { public: int from; int to; int dist; Edge() {} Edge(int _f, int _t, int _d) : from(_f), to(_t), dist(_d) {} }; bool is_LPG[3005]; bool dp[3005][2005]; // dp[city_id][remaining_LPG] = dist; static int gCity_idx = 0; int get_city_id(map<string, int> &cities, const string &city) { if (cities.find(city) == cities.end()) { cities[city] = gCity_idx++; } return cities[city]; } class State { public: int city_id; int dist; int remaining_LPG; State(int _city_id, int _dist, int _con) { city_id = _city_id; dist = _dist; remaining_LPG = _con; } bool operator<(const State &s) const { return (dist < s.dist); } bool operator>(const State &s) const { return (dist < s.dist); } }; int main() { int total_roads; int total_LPG_stations; int capacity; while (cin >> total_roads >> total_LPG_stations >> capacity) { if (total_roads == 0 && total_LPG_stations == 0 && capacity == 0) break; gCity_idx = 0; map<string, int> cities; vector<Edge> edges(total_roads); memset(is_LPG, false, sizeof(is_LPG)); memset(dp, false, sizeof(dp)); string src, dst; cin >> src >> dst; get_city_id(cities, src); get_city_id(cities, dst); for (int road_idx = 0; road_idx < total_roads; road_idx++) { string from_str, to_str; int dist; cin >> from_str >> to_str >> dist; int from_num = get_city_id(cities, from_str); int to_num = get_city_id(cities, to_str); edges[road_idx] = Edge(from_num, to_num, dist); } for (int LPG_idx = 0; LPG_idx < total_LPG_stations; LPG_idx++) { string city; cin >> city; is_LPG[get_city_id(cities, city)] = true; } priority_queue<State, vector<State>, greater<State>> que; que.push(State(get_city_id(cities, src), 0, 10 * capacity)); int dst_id = get_city_id(cities, dst); vector<vector<Edge>> nodes(cities.size()); for (int i = 0; i < edges.size(); i++) { Edge e = edges[i]; nodes[e.from].push_back(Edge(e.from, e.to, e.dist)); nodes[e.to].push_back(Edge(e.to, e.from, e.dist)); } int res = INF; while (!que.empty()) { State s = que.top(); que.pop(); if (s.city_id == dst_id) { res = s.dist; goto found; } if (dp[s.city_id][s.remaining_LPG]) continue; dp[s.city_id][s.remaining_LPG] = true; for (int to_idx = 0; to_idx < nodes[s.city_id].size(); to_idx++) { int to = nodes[s.city_id][to_idx].to; int dist = nodes[s.city_id][to_idx].dist; if (s.remaining_LPG < dist) continue; int next_remaining_LPG = (is_LPG[to] ? 10 * capacity : s.remaining_LPG - dist); que.push(State(to, s.dist + dist, next_remaining_LPG)); } } found:; printf("%d\n", res >= INF ? -1 : res); } return 0; }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; class Edge { public: int from; int to; int dist; Edge() {} Edge(int _f, int _t, int _d) : from(_f), to(_t), dist(_d) {} }; bool is_LPG[3005]; bool dp[3005][2005]; // dp[city_id][remaining_LPG] = dist; static int gCity_idx = 0; int get_city_id(map<string, int> &cities, const string &city) { if (cities.find(city) == cities.end()) { cities[city] = gCity_idx++; } return cities[city]; } class State { public: int city_id; int dist; int remaining_LPG; State(int _city_id, int _dist, int _con) { city_id = _city_id; dist = _dist; remaining_LPG = _con; } bool operator<(const State &s) const { return (dist < s.dist); } bool operator>(const State &s) const { return (dist > s.dist); } }; int main() { int total_roads; int total_LPG_stations; int capacity; while (cin >> total_roads >> total_LPG_stations >> capacity) { if (total_roads == 0 && total_LPG_stations == 0 && capacity == 0) break; gCity_idx = 0; map<string, int> cities; vector<Edge> edges(total_roads); memset(is_LPG, false, sizeof(is_LPG)); memset(dp, false, sizeof(dp)); string src, dst; cin >> src >> dst; get_city_id(cities, src); get_city_id(cities, dst); for (int road_idx = 0; road_idx < total_roads; road_idx++) { string from_str, to_str; int dist; cin >> from_str >> to_str >> dist; int from_num = get_city_id(cities, from_str); int to_num = get_city_id(cities, to_str); edges[road_idx] = Edge(from_num, to_num, dist); } for (int LPG_idx = 0; LPG_idx < total_LPG_stations; LPG_idx++) { string city; cin >> city; is_LPG[get_city_id(cities, city)] = true; } priority_queue<State, vector<State>, greater<State>> que; que.push(State(get_city_id(cities, src), 0, 10 * capacity)); int dst_id = get_city_id(cities, dst); vector<vector<Edge>> nodes(cities.size()); for (int i = 0; i < edges.size(); i++) { Edge e = edges[i]; nodes[e.from].push_back(Edge(e.from, e.to, e.dist)); nodes[e.to].push_back(Edge(e.to, e.from, e.dist)); } int res = INF; while (!que.empty()) { State s = que.top(); que.pop(); if (s.city_id == dst_id) { res = s.dist; goto found; } if (dp[s.city_id][s.remaining_LPG]) continue; dp[s.city_id][s.remaining_LPG] = true; for (int to_idx = 0; to_idx < nodes[s.city_id].size(); to_idx++) { int to = nodes[s.city_id][to_idx].to; int dist = nodes[s.city_id][to_idx].dist; if (s.remaining_LPG < dist) continue; int next_remaining_LPG = (is_LPG[to] ? 10 * capacity : s.remaining_LPG - dist); que.push(State(to, s.dist + dist, next_remaining_LPG)); } } found:; printf("%d\n", res >= INF ? -1 : res); } return 0; }
[["-", 8, 9, 0, 37, 0, 23, 0, 16, 17, 18], ["+", 8, 9, 0, 37, 0, 23, 0, 16, 17, 47]]
1
832
#include <iostream> #include <map> #include <queue> #include <set> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) bool used[6000][2001]; vector<pair<short, short>> edge[6000]; int main() { while (true) { rep(i, 6000) edge[i].clear(); rep(i, 6000) rep(j, 2001) used[i][j] = false; int n, m, cap; string src, dest; cin >> n >> m >> cap; cap *= 10; if (n == 0) break; cin >> src >> dest; map<string, int> mp; rep(i, n) { string c1, c2; int d; cin >> c1 >> c2 >> d; short nc1, nc2; if (mp.count(c1)) { nc1 = mp[c1]; } else { nc1 = mp.size(); mp[c1] = nc1; } if (mp.count(c2)) { nc2 = mp[c2]; } else { nc2 = mp.size(); mp[c2] = nc2; } edge[nc1].push_back(make_pair(nc2, d)); edge[nc2].push_back(make_pair(nc1, d)); } set<int> st; rep(i, m) { string s; cin >> s; st.insert(mp[s]); } priority_queue<pair<int, pair<short, short>>, vector<pair<int, pair<short, short>>>, greater<pair<int, pair<short, short>>>> que; que.push(make_pair(0, make_pair(mp[src], cap))); int res = 1e9; while (!que.empty()) { pair<int, pair<short, short>> now = que.top(); que.pop(); int cost = now.first; short pos = now.second.first; short f = now.second.second; if (used[pos][f]) continue; used[pos][f] = true; if (mp[dest] == pos) { res = cost; } rep(i, edge[pos].size()) { int nc = cost + edge[pos][i].second; short np = edge[pos][i].first; short nf = f - edge[pos][i].second; if (nf < 0) continue; if (st.count(np)) nf = cap; if (used[np][nf]) continue; que.push(make_pair(nc, make_pair(np, nf))); } } if (res == 1e9) cout << -1 << endl; else cout << res << endl; } }
#include <iostream> #include <map> #include <queue> #include <set> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) bool used[6000][2001]; vector<pair<short, short>> edge[6000]; int main() { while (true) { rep(i, 6000) edge[i].clear(); rep(i, 6000) rep(j, 2001) used[i][j] = false; int n, m, cap; string src, dest; cin >> n >> m >> cap; cap *= 10; if (n == 0) break; cin >> src >> dest; map<string, int> mp; rep(i, n) { string c1, c2; int d; cin >> c1 >> c2 >> d; short nc1, nc2; if (mp.count(c1)) { nc1 = mp[c1]; } else { nc1 = mp.size(); mp[c1] = nc1; } if (mp.count(c2)) { nc2 = mp[c2]; } else { nc2 = mp.size(); mp[c2] = nc2; } edge[nc1].push_back(make_pair(nc2, d)); edge[nc2].push_back(make_pair(nc1, d)); } set<int> st; rep(i, m) { string s; cin >> s; st.insert(mp[s]); } priority_queue<pair<int, pair<short, short>>, vector<pair<int, pair<short, short>>>, greater<pair<int, pair<short, short>>>> que; que.push(make_pair(0, make_pair(mp[src], cap))); int res = 1e9; while (!que.empty()) { pair<int, pair<short, short>> now = que.top(); que.pop(); int cost = now.first; short pos = now.second.first; short f = now.second.second; if (used[pos][f]) continue; used[pos][f] = true; if (mp[dest] == pos) { res = cost; break; } rep(i, edge[pos].size()) { int nc = cost + edge[pos][i].second; short np = edge[pos][i].first; short nf = f - edge[pos][i].second; if (nf < 0) continue; if (st.count(np)) nf = cap; if (used[np][nf]) continue; que.push(make_pair(nc, make_pair(np, nf))); } } if (res == 1e9) cout << -1 << endl; else cout << res << endl; } }
[["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 35]]
1
599
import java.util.Arrays; import java.util.Scanner; // City Merger public class Main { int n; String[] s; int[][] cut; int[][] dp; int get(int S, int last) { if (dp[S][last] != -1) return dp[S][last]; int res = 1 << 29; int ns = S - (1 << last); for (int j = 0; j < n; j++) { if (((ns >> j) & 1) == 0) continue; res = Math.min(res, get(ns, j) + s[last].length() - cut[j][last]); } return dp[S][last] = res; } void run() { Scanner sc = new Scanner(System.in); for (;;) { n = sc.nextInt(); if (n == 0) break; String[] t = new String[n]; for (int i = 0; i < n; i++) t[i] = sc.next(); boolean[] del = new boolean[n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j && t[i].contains(t[j])) del[j] = true; int N = 0; for (boolean f : del) if (!f) N++; s = new String[N]; int id = 0; for (int i = 0; i < n; i++) if (!del[i]) s[id++] = t[i]; n = N; cut = new int[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { for (int k = 0; k < t[i].length(); k++) { if (t[j].startsWith(t[i].substring(k))) { cut[i][j] = t[i].length() - k; break; } } } dp = new int[1 << n][n]; for (int[] a : dp) Arrays.fill(a, -1); for (int i = 0; i < n; i++) dp[1 << i][i] = s[i].length(); int res = 1 << 29; for (int i = 0; i < n; i++) res = Math.min(res, get((1 << n) - 1, i)); System.out.println(res); } } public static void main(String[] args) { new Main().run(); } }
import java.util.Arrays; import java.util.Scanner; // City Merger public class Main { int n; String[] s; int[][] cut; int[][] dp; int get(int S, int last) { if (dp[S][last] != -1) return dp[S][last]; int res = 1 << 29; int ns = S - (1 << last); for (int j = 0; j < n; j++) { if (((ns >> j) & 1) == 0) continue; res = Math.min(res, get(ns, j) + s[last].length() - cut[j][last]); } return dp[S][last] = res; } void run() { Scanner sc = new Scanner(System.in); for (;;) { n = sc.nextInt(); if (n == 0) break; String[] t = new String[n]; for (int i = 0; i < n; i++) t[i] = sc.next(); boolean[] del = new boolean[n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j && t[i].contains(t[j])) del[j] = true; int N = 0; for (boolean f : del) if (!f) N++; s = new String[N]; int id = 0; for (int i = 0; i < n; i++) if (!del[i]) s[id++] = t[i]; n = N; cut = new int[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { for (int k = 0; k < s[i].length(); k++) { if (s[j].startsWith(s[i].substring(k))) { cut[i][j] = s[i].length() - k; break; } } } dp = new int[1 << n][n]; for (int[] a : dp) Arrays.fill(a, -1); for (int i = 0; i < n; i++) dp[1 << i][i] = s[i].length(); int res = 1 << 29; for (int i = 0; i < n; i++) res = Math.min(res, get((1 << n) - 1, i)); System.out.println(res); } } public static void main(String[] args) { new Main().run(); } }
[["-", 0, 7, 15, 16, 12, 492, 500, 504, 516, 22], ["+", 0, 7, 15, 16, 12, 492, 500, 504, 516, 22], ["-", 0, 57, 15, 15, 0, 492, 500, 504, 516, 22], ["+", 0, 57, 15, 15, 0, 492, 500, 504, 516, 22], ["-", 0, 492, 3, 4, 0, 492, 500, 504, 516, 22], ["+", 0, 492, 3, 4, 0, 492, 500, 504, 516, 22], ["-", 0, 11, 12, 16, 31, 492, 500, 504, 516, 22], ["+", 0, 11, 12, 16, 31, 492, 500, 504, 516, 22]]
3
625
#include <bits/stdc++.h> using namespace std; int n; vector<string> str; string mem[15][15]; string con(string a, string b) { int K = 0; for (int i = 0; i < a.size(); i++) { int j = i, k = 0; while (a[j] && b[k] && a[j] == b[k]) j++, k++; if (!a[j] || !b[k]) K = max(K, k); } return b.substr(K, b.size()); } string solve() { string dp[1 << 14][14]; for (int i = 0; i < n; i++) dp[1 << i][i] = str[i]; for (int i = 0; i < (1 << n); i++) for (int k = 0; k < n; k++) { if (dp[i][k] == "") continue; for (int j = 0; j < n; j++) { if ((i & (1 << j))) continue; string nx = dp[i][k] + mem[k][j], &ndp = dp[i | 1 << j][j]; if (!ndp.size() || nx.size() < ndp.size() || (nx.size() == ndp.size() && ndp > nx)) ndp = nx; } } string res = dp[(1 << n) - 1][0]; for (int i = 1; i < n; i++) { string a = dp[(1 << n) - 1][i]; if (res.size() > a.size() || (a.size() == res.size() && res > a)) res = a; } return res; } int main() { while (cin >> n, n) { str.resize(n); for (int i = 0; i < n; i++) cin >> str[i]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j && con(str[i], str[j]) == "") str.erase(str.begin() + j), j = -1, i = 0, n--; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mem[i][j] = con(str[i], str[j]); cout << solve() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<string> str; string mem[15][15]; string con(string a, string b) { int K = 0; for (int i = 0; i < a.size(); i++) { int j = i, k = 0; while (a[j] && b[k] && a[j] == b[k]) j++, k++; if (!a[j] || !b[k]) K = max(K, k); } return b.substr(K, b.size()); } string solve() { string dp[1 << 14][14]; for (int i = 0; i < n; i++) dp[1 << i][i] = str[i]; for (int i = 0; i < (1 << n); i++) for (int k = 0; k < n; k++) { if (dp[i][k] == "") continue; for (int j = 0; j < n; j++) { if ((i & (1 << j))) continue; string nx = dp[i][k] + mem[k][j], &ndp = dp[i | 1 << j][j]; if (!ndp.size() || nx.size() < ndp.size() || (nx.size() == ndp.size() && ndp > nx)) ndp = nx; } } string res = dp[(1 << n) - 1][0]; for (int i = 1; i < n; i++) { string a = dp[(1 << n) - 1][i]; if (res.size() > a.size() || (a.size() == res.size() && res > a)) res = a; } return res; } int main() { while (cin >> n, n) { str.resize(n); for (int i = 0; i < n; i++) cin >> str[i]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j && con(str[i], str[j]) == "") str.erase(str.begin() + j), j = -1, i = 0, n--; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mem[i][j] = con(str[i], str[j]); cout << solve().size() << endl; } return 0; }
[["+", 0, 16, 31, 16, 12, 2, 63, 118, 17, 131], ["+", 0, 16, 31, 16, 12, 2, 63, 118, 119, 120], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 24], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 25]]
1
603
#ifndef VS #include <algorithm> #include <assert.h> #include <ctype.h> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> #endif using namespace std; typedef long long LL; #ifdef BTK #define DEBUG if (1) #else #define DEBUG if (0) #endif #define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() #define REC(ret, ...) std::function<ret(__VA_ARGS__)> template <typename T> inline bool chmin(T &l, T r) { bool a = l > r; if (a) l = r; return a; } template <typename T> inline bool chmax(T &l, T r) { bool a = l < r; if (a) l = r; return a; } struct input { bool success; input() { success = true; } #ifdef BTK template <typename T> inline input &operator>>(T &x) { success = !!(cin >> x); return *this; } #else inline input &operator>>(LL &x) { success = (scanf("%lld", &x) != EOF); return *this; } inline input &operator>>(int &x) { success = (scanf("%d", &x) != EOF); return *this; } inline input &operator>>(char *x) { success = (scanf("%s", x) != EOF); return *this; } inline input &operator>>(double &x) { success = (scanf("%llf", &x) != EOF); return *this; } #endif } in; const char space = ' '; const char ln = '\n'; struct output { #ifdef BTK template <typename T> inline output &operator<<(T x) { cout << x; return (*this); } inline void fp(const double x, const char *s) { char buf[20]; sprintf(buf, s, x); cout << buf; } #else inline output &operator<<(const int x) { printf("%d", x); return *this; } inline output &operator<<(const string x) { printf("%s", x.c_str()); return *this; } inline output &operator<<(const char x) { putchar(x); return *this; } inline output &operator<<(const LL x) { printf("%lld", x); return *this; } inline output &operator<<(const double x) { printf("%lf", x); return *this; } inline void fp(const double x, const char *s) { printf(s, x); } #endif } out; typedef vector<int> V; typedef vector<V> VV; // KMP int kmp_search(string &S, string &word, V &table) { int n = S.size(); int m = word.size(); int i = 0, j = 0; for (; j < n && i < m; i++, j++) while (i >= 0 && word[i] != S[j]) i = table[i]; if (i == m) return j - m; else return -1; } V kmp_table_build(string word) { int n = word.size() + 1; V table(n, 0); table[0] = -1; for (int i = 2, j = 0; i < n;) { if (word[i - 1] == word[j]) table[i++] = ++j; else if (j > 0) j = table[j]; else table[i++] = 0; } return table; } int cww(string &S, string &word, V &table) { int n = S.size(); int m = word.size(); int i = 0, j = 0; for (; j < n && i < m; i++, j++) { while (i >= 0 && word[i] != S[j]) i = table[i]; } if (i == m) return 0; else return m - i; } char buf[128]; string s[16]; int len[16][16]; int bit[16][16]; V tables[16]; int dpl[1 << 15][15]; int ts[1 << 15][15]; int stamp[1600]; string ret[1600]; int main() { // int T=1;in >> T;REP(_,T){int n;in >> n; int n; while (cin >> n, n) { int cnt = 0; REP(i, n) { in >> buf; s[i] = buf; tables[i] = kmp_table_build(s[i]); } REP(i, n) REP(j, n) { int l = cww(s[i], s[j], tables[j]); string st = s[i] + s[j].substr(s[j].size() - l, l); len[i][j] = l; if (i == j) len[i][j] = s[i].size(); bit[i][j] = 0; REP(k, n) if (kmp_search(st, s[k], tables[k]) >= 0) bit[i][j] |= (1 << k); } REP(i, 1 << n) REP(j, n) { dpl[i][j] = 2000; ts[i][j] = 0; } int ret_len = 2000; REP(i, n) { int b = bit[i][i]; dpl[b][i] = len[i][i]; } FOR(b, 1, (1 << n)) { REP(j, n) { if (dpl[b][j] == 2000) continue; REP(k, n) if (j != k) { int nb = b | bit[j][k]; if (nb == b) continue; int nl = dpl[b][j] + len[j][k]; chmin(dpl[nb][k], nl); } } } REP(i, n) chmin(ret_len, dpl[(1 << n) - 1][i]); REP(i, ret_len + 1) { ret[i] = "["; stamp[i] = 0; } REP(i, n) if (dpl[(1 << n) - 1][i] != ret_len) dpl[1 << n][i] = 2000; for (int b = (1 << n) - 2; b > 0; b--) { REP(j, n) { if (dpl[b][j] == 2000) continue; bool ok = false; REP(k, n) if (j != k) { int nb = b | bit[j][k]; if (nb == b) continue; int nl = dpl[b][j] + len[j][k]; ok |= (nl == dpl[nb][k]); } if (ok == false) dpl[b][j] = 2000; } } REP(i, n) { int b = bit[i][i]; int l = len[i][i]; if (l == dpl[b][i]) { if (ret[l] == s[i]) { ts[b][i] = stamp[l]; } if (ret[l] > s[i]) { stamp[l] = ++cnt; ts[b][i] = stamp[l]; ret[l] = s[i]; } } } FOR(b, 1, (1 << n)) { REP(j, n) { int l = dpl[b][j]; if (l == 2000) continue; if (ts[b][j] != stamp[l]) continue; REP(k, n) if (j != k) { int nb = b | bit[j][k]; if (nb == b) continue; int nl = dpl[b][j] + len[j][k]; if (dpl[nb][k] == nl) { string nxt = ret[l] + s[k].substr(s[k].size() - len[j][k], len[j][k]); if (ret[nl] == nxt) { ts[nb][k] = stamp[nl]; } else if (ret[nl] > nxt) { ++cnt; ts[nb][k] = cnt; stamp[nl] = cnt; ret[nl] = nxt; } } } } } // out << "Scenario #" << _ + 1 << ":" << ln; // out << ret[ret_len] << ln; out << ln; out << (int)ret[ret_len].size() << ln; } return 0; }
#ifndef VS #include <algorithm> #include <assert.h> #include <ctype.h> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> #endif using namespace std; typedef long long LL; #ifdef BTK #define DEBUG if (1) #else #define DEBUG if (0) #endif #define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() #define REC(ret, ...) std::function<ret(__VA_ARGS__)> template <typename T> inline bool chmin(T &l, T r) { bool a = l > r; if (a) l = r; return a; } template <typename T> inline bool chmax(T &l, T r) { bool a = l < r; if (a) l = r; return a; } struct input { bool success; input() { success = true; } #ifdef BTK template <typename T> inline input &operator>>(T &x) { success = !!(cin >> x); return *this; } #else inline input &operator>>(LL &x) { success = (scanf("%lld", &x) != EOF); return *this; } inline input &operator>>(int &x) { success = (scanf("%d", &x) != EOF); return *this; } inline input &operator>>(char *x) { success = (scanf("%s", x) != EOF); return *this; } inline input &operator>>(double &x) { success = (scanf("%llf", &x) != EOF); return *this; } #endif } in; const char space = ' '; const char ln = '\n'; struct output { #ifdef BTK template <typename T> inline output &operator<<(T x) { cout << x; return (*this); } inline void fp(const double x, const char *s) { char buf[20]; sprintf(buf, s, x); cout << buf; } #else inline output &operator<<(const int x) { printf("%d", x); return *this; } inline output &operator<<(const string x) { printf("%s", x.c_str()); return *this; } inline output &operator<<(const char x) { putchar(x); return *this; } inline output &operator<<(const LL x) { printf("%lld", x); return *this; } inline output &operator<<(const double x) { printf("%lf", x); return *this; } inline void fp(const double x, const char *s) { printf(s, x); } #endif } out; typedef vector<int> V; typedef vector<V> VV; // KMP int kmp_search(string &S, string &word, V &table) { int n = S.size(); int m = word.size(); int i = 0, j = 0; for (; j < n && i < m; i++, j++) while (i >= 0 && word[i] != S[j]) i = table[i]; if (i == m) return j - m; else return -1; } V kmp_table_build(string word) { int n = word.size() + 1; V table(n, 0); table[0] = -1; for (int i = 2, j = 0; i < n;) { if (word[i - 1] == word[j]) table[i++] = ++j; else if (j > 0) j = table[j]; else table[i++] = 0; } return table; } int cww(string &S, string &word, V &table) { int n = S.size(); int m = word.size(); int i = 0, j = 0; for (; j < n && i < m; i++, j++) { while (i >= 0 && word[i] != S[j]) i = table[i]; } if (i == m) return 0; else return m - i; } char buf[128]; string s[16]; int len[16][16]; int bit[16][16]; V tables[16]; int dpl[1 << 15][15]; int ts[1 << 15][15]; int stamp[1600]; string ret[1600]; int main() { // int T=1;in >> T;REP(_,T){int n;in >> n; int n; while (cin >> n, n) { int cnt = 0; REP(i, n) { in >> buf; s[i] = buf; tables[i] = kmp_table_build(s[i]); } REP(i, n) REP(j, n) { int l = cww(s[i], s[j], tables[j]); string st = s[i] + s[j].substr(s[j].size() - l, l); len[i][j] = l; if (i == j) len[i][j] = s[i].size(); bit[i][j] = 0; REP(k, n) if (kmp_search(st, s[k], tables[k]) >= 0) bit[i][j] |= (1 << k); } REP(i, 1 << n) REP(j, n) { dpl[i][j] = 2000; ts[i][j] = 0; } int ret_len = 2000; REP(i, n) { int b = bit[i][i]; dpl[b][i] = len[i][i]; } FOR(b, 1, (1 << n)) { REP(j, n) { if (dpl[b][j] == 2000) continue; REP(k, n) if (j != k) { int nb = b | bit[j][k]; if (nb == b) continue; int nl = dpl[b][j] + len[j][k]; chmin(dpl[nb][k], nl); } } } REP(i, n) chmin(ret_len, dpl[(1 << n) - 1][i]); REP(i, ret_len + 1) { ret[i] = "["; stamp[i] = 0; } REP(i, n) if (dpl[(1 << n) - 1][i] != ret_len) dpl[(1 << n) - 1][i] = 2000; for (int b = (1 << n) - 2; b > 0; b--) { REP(j, n) { if (dpl[b][j] == 2000) continue; bool ok = false; REP(k, n) if (j != k) { int nb = b | bit[j][k]; if (nb == b) continue; int nl = dpl[b][j] + len[j][k]; ok |= (nl == dpl[nb][k]); } if (ok == false) dpl[b][j] = 2000; } } REP(i, n) { int b = bit[i][i]; int l = len[i][i]; if (l == dpl[b][i]) { if (ret[l] == s[i]) { ts[b][i] = stamp[l]; } if (ret[l] > s[i]) { stamp[l] = ++cnt; ts[b][i] = stamp[l]; ret[l] = s[i]; } } } FOR(b, 1, (1 << n)) { REP(j, n) { int l = dpl[b][j]; if (l == 2000) continue; if (ts[b][j] != stamp[l]) continue; REP(k, n) if (j != k) { int nb = b | bit[j][k]; if (nb == b) continue; int nl = dpl[b][j] + len[j][k]; if (dpl[nb][k] == nl) { string nxt = ret[l] + s[k].substr(s[k].size() - len[j][k], len[j][k]); if (ret[nl] == nxt) { ts[nb][k] = stamp[nl]; } else if (ret[nl] > nxt) { ++cnt; ts[nb][k] = cnt; stamp[nl] = cnt; ret[nl] = nxt; } } } } } // out << "Scenario #" << _ + 1 << ":" << ln; // out << ret[ret_len] << ln; out << ln; out << (int)ret[ret_len].size() << ln; } return 0; }
[["+", 28, 69, 341, 342, 0, 16, 31, 23, 0, 24], ["+", 28, 69, 341, 342, 0, 16, 31, 23, 0, 25], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
1,931
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)n; ++i) using namespace std; typedef pair<int, int> P; const int MAX_N = 100005; int main() { while (1) { int n; scanf("%d", &n); if (n == 0) { break; } string s[14]; int d[14][14]; rep(i, n) { cin >> s[i]; } set<int> inc; rep(i, n) { for (int j = i + 1; j < n; j++) { string s1, s2; int flag; if (s[i].length() > s[j].length()) { s1 = s[i]; s2 = s[j]; flag = 0; } else { s1 = s[j]; s2 = s[i]; flag = 1; } rep(k, s1.length() - s2.length() + 1) { bool ff = false; rep(l, s2.length()) { if (s1[k + l] != s2[l]) { ff = true; break; } } if (!ff) { flag += 2; break; } } if (flag >= 2) { if (flag == 2) { inc.insert(j); } else { inc.insert(i); } } } } vector<int> id; rep(i, n) { if (inc.find(i) == inc.end()) { id.push_back(i); } } int nn = id.size(); rep(i, nn) { rep(j, nn) { if (i != j) { int cnt = 0; for (int k = 1; k <= min(s[id[i]].length(), s[id[j]].length()); k++) { bool flag = false; rep(l, k) { if (s[id[i]][s[id[i]].length() - k + l] != s[id[j]][l]) { flag = true; break; } } if (!flag) { cnt = k; } } d[i][j] = cnt; } else { d[i][j] = 0; } } } int dp[(1 << 14)][14]; rep(i, 1 << nn) { rep(j, nn) { dp[i][j] = -INF; } } rep(i, nn) { dp[(1 << nn) - 1][i] = 0; } for (int i = (1 << nn) - 2; i >= 0; i--) { rep(j, nn) { rep(k, nn) { if (!(i & (1 << k))) { dp[i][j] = max(dp[i][j], dp[i | (1 << k)][k] + d[j][k]); } } } } int mx = 0; rep(i, nn) { mx = max(mx, dp[(1 << i)][i]); } int sm = 0; rep(i, nn) { sm += s[i].length(); } printf("%d\n", sm - mx); } return 0; }
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)n; ++i) using namespace std; typedef pair<int, int> P; const int MAX_N = 100005; int main() { while (1) { int n; scanf("%d", &n); if (n == 0) { break; } string s[14]; int d[14][14]; rep(i, n) { cin >> s[i]; } set<int> inc; rep(i, n) { for (int j = i + 1; j < n; j++) { string s1, s2; int flag; if (s[i].length() > s[j].length()) { s1 = s[i]; s2 = s[j]; flag = 0; } else { s1 = s[j]; s2 = s[i]; flag = 1; } rep(k, s1.length() - s2.length() + 1) { bool ff = false; rep(l, s2.length()) { if (s1[k + l] != s2[l]) { ff = true; break; } } if (!ff) { flag += 2; break; } } if (flag >= 2) { if (flag == 2) { inc.insert(j); } else { inc.insert(i); } } } } vector<int> id; rep(i, n) { if (inc.find(i) == inc.end()) { id.push_back(i); } } int nn = id.size(); rep(i, nn) { rep(j, nn) { if (i != j) { int cnt = 0; for (int k = 1; k <= min(s[id[i]].length(), s[id[j]].length()); k++) { bool flag = false; rep(l, k) { if (s[id[i]][s[id[i]].length() - k + l] != s[id[j]][l]) { flag = true; break; } } if (!flag) { cnt = k; } } d[i][j] = cnt; } else { d[i][j] = 0; } } } int dp[(1 << 14)][14]; rep(i, 1 << nn) { rep(j, nn) { dp[i][j] = -INF; } } rep(i, nn) { dp[(1 << nn) - 1][i] = 0; } for (int i = (1 << nn) - 2; i >= 0; i--) { rep(j, nn) { rep(k, nn) { if (!(i & (1 << k))) { dp[i][j] = max(dp[i][j], dp[i | (1 << k)][k] + d[j][k]); } } } } int mx = 0; rep(i, nn) { mx = max(mx, dp[(1 << i)][i]); } int sm = 0; rep(i, nn) { sm += s[id[i]].length(); } printf("%d\n", sm - mx); } return 0; }
[["+", 63, 118, 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]]
1
756
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #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 toStr(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 FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-12; const double PI = acos(-1.0); const int INF = INT_MAX / 10; string merge(string a, string b) { int al = a.length(), bl = b.length(); string res = a + b; FOR(i, 1, min(al, bl)) { string as = a.substr(al - i); string bs = b.substr(0, i); if (as == bs) { res = a + b.substr(i); break; } } return res; } int diffstr(string a, string b) { return merge(a, b).length() - a.length(); } int main() { int N; while (cin >> N, N) { vs data(N); REP(i, N) { cin >> data[i]; } vs city; REP(i, N) { bool ok = true; REP(j, N) { if (i != j && data[j].find(data[i]) != string::npos) { ok = false; break; } } if (ok) { city.push_back(data[i]); } } N = city.size(); vvi diff(N, vi(N)); REP(i, N) { REP(j, N) { diff[i][j] = diffstr(city[i], city[j]); } } vvi dp(1 << N, vi(N, INF)); REP(i, 1 << N) { if (i == 0) { REP(j, N) { dp[1 << j][j] = city[j].length(); } continue; } REP(j, N) { if (i & (1 << j)) { REP(k, N) { if (j != k && !(i & (1 << k))) { dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + diff[j][k]); } } } } } int ans = INF; REP(i, N) { ans = min(ans, dp[(1 << N) - 1][i]); } cout << ans << endl; } }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #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 toStr(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 FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-12; const double PI = acos(-1.0); const int INF = INT_MAX / 10; string merge(string a, string b) { int al = a.length(), bl = b.length(); string res = a + b; FOR(i, 1, min(al, bl)) { string as = a.substr(al - i); string bs = b.substr(0, i); if (as == bs) { res = a + b.substr(i); } } return res; } int diffstr(string a, string b) { return merge(a, b).length() - a.length(); } int main() { int N; while (cin >> N, N) { vs data(N); REP(i, N) { cin >> data[i]; } vs city; REP(i, N) { bool ok = true; REP(j, N) { if (i != j && data[j].find(data[i]) != string::npos) { ok = false; break; } } if (ok) { city.push_back(data[i]); } } N = city.size(); vvi diff(N, vi(N)); REP(i, N) { REP(j, N) { diff[i][j] = diffstr(city[i], city[j]); } } vvi dp(1 << N, vi(N, INF)); REP(i, 1 << N) { if (i == 0) { REP(j, N) { dp[1 << j][j] = city[j].length(); } continue; } REP(j, N) { if (i & (1 << j)) { REP(k, N) { if (j != k && !(i & (1 << k))) { dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + diff[j][k]); } } } } } int ans = INF; REP(i, N) { ans = min(ans, dp[(1 << N) - 1][i]); } cout << ans << endl; } }
[["-", 0, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["-", 0, 9, 0, 57, 64, 9, 0, 93, 0, 35]]
1
675
/* * GCA : "Computer is artificial subject absolutely,Math is God" */ #include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> using namespace std; #ifdef DEBUG #define VAR(a, b) __typeof(b) a = (b) #define debug(...) printf("DEBUG: "), printf(__VA_ARGS__) #else #define VAR(a, b) __typeof(b) a = (b) #define debug(...) #endif typedef unsigned int uint; typedef long long int Int; typedef unsigned long long int UInt; #define Set(a, s) memset(a, s, sizeof(a)) #define Pln() printf("\n") #define For(i, x) for (int i = 0; i < x; i++) #define CON(x, y) x##y #define M 25 #define PB push_back #define oo INT_MAX #define FOR(a, b) for (VAR(a, (b).begin()); a != (b).end(); ++a) #define eps 1e-9 #define X first #define Y second inline bool xdy(double x, double y) { return x > y + eps; } inline bool xddy(double x, double y) { return x > y - eps; } inline bool xcy(double x, double y) { return x < y - eps; } inline bool xcdy(double x, double y) { return x < y + eps; } const Int mod = 1000000007; int n; char str[M][M]; int mz[M][M]; int pnt[M]; bool nonused[M]; bool substr(int x, int y) { int xlen = strlen(str[x]); int ylen = strlen(str[y]); string sx(str[x]), sy(str[y]); if (ylen > xlen) return false; for (int i = 0; i <= xlen - ylen; i++) { if (sx.substr(i, ylen) == sy) return true; } return false; } int count_w(int x, int y) { int len = strlen(str[x]); int len2 = strlen(str[y]); for (int i = max(0, len - len2); str[x][i]; i++) { bool suc = true; int j, k; for (j = i, k = 0; str[x][j] && str[y][k]; j++, k++) { if (str[x][j] != str[y][k]) { suc = false; break; } } if (suc) return k; } return 0; } int dp[1 << 15][15]; int dfs(int s, int now, int dep) { // debug("%d %d\n",now,dep); if (s == (1 << n) - 1) { return 0; } if (dp[s][now] != -1) return dp[s][now]; int minans = -1, t; for (int i = 0; i < n; i++) { if (i != now && !((s >> i) & 1)) { int h; if ((h = dfs(s | (1 << i), i, dep + 1)) != -1) { t = h + strlen(str[i]) - mz[now][i]; } if (minans == -1 || t < minans) { minans = t; } } } return dp[s][now] = minans; } void solve() { Set(nonused, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (substr(i, j)) nonused[j] = 1; } } int cnt = 0; for (int i = 0; i < n; i++) { if (!nonused[i]) pnt[cnt++] = i; } n = cnt; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; mz[i][j] = count_w(pnt[i], pnt[j]); // debug("%d %d %d\n",i,j,mz[i][j]); } } Set(dp, -1); int ans = oo; // ans=dfs(1,0,0); for (int i = 0; i < n; i++) { int t = strlen(str[pnt[i]]) + dfs(1 << i, i, 0); ans = min(ans, t); } printf("%d\n", ans); } int main() { ios_base::sync_with_stdio(0); while (~scanf("%d", &n) && n) { for (int i = 0; i < n; i++) scanf("%s", str[i]); Set(mz, 0); solve(); } }
/* * GCA : "Computer is artificial subject absolutely,Math is God" */ #include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> using namespace std; #ifdef DEBUG #define VAR(a, b) __typeof(b) a = (b) #define debug(...) printf("DEBUG: "), printf(__VA_ARGS__) #else #define VAR(a, b) __typeof(b) a = (b) #define debug(...) #endif typedef unsigned int uint; typedef long long int Int; typedef unsigned long long int UInt; #define Set(a, s) memset(a, s, sizeof(a)) #define Pln() printf("\n") #define For(i, x) for (int i = 0; i < x; i++) #define CON(x, y) x##y #define M 25 #define PB push_back #define oo INT_MAX #define FOR(a, b) for (VAR(a, (b).begin()); a != (b).end(); ++a) #define eps 1e-9 #define X first #define Y second inline bool xdy(double x, double y) { return x > y + eps; } inline bool xddy(double x, double y) { return x > y - eps; } inline bool xcy(double x, double y) { return x < y - eps; } inline bool xcdy(double x, double y) { return x < y + eps; } const Int mod = 1000000007; int n; char str[M][M]; int mz[M][M]; int pnt[M]; bool nonused[M]; bool substr(int x, int y) { int xlen = strlen(str[x]); int ylen = strlen(str[y]); string sx(str[x]), sy(str[y]); if (ylen > xlen) return false; for (int i = 0; i <= xlen - ylen; i++) { if (sx.substr(i, ylen) == sy) return true; } return false; } int count_w(int x, int y) { int len = strlen(str[x]); int len2 = strlen(str[y]); for (int i = max(0, len - len2); str[x][i]; i++) { bool suc = true; int j, k; for (j = i, k = 0; str[x][j] && str[y][k]; j++, k++) { if (str[x][j] != str[y][k]) { suc = false; break; } } if (suc) return k; } return 0; } int dp[1 << 15][15]; int dfs(int s, int now, int dep) { // debug("%d %d\n",now,dep); if (s == (1 << n) - 1) { return 0; } if (dp[s][now] != -1) return dp[s][now]; int minans = -1, t; for (int i = 0; i < n; i++) { if (i != now && !((s >> i) & 1)) { int h; if ((h = dfs(s | (1 << i), i, dep + 1)) != -1) { t = h + strlen(str[pnt[i]]) - mz[now][i]; } if (minans == -1 || t < minans) { minans = t; } } } return dp[s][now] = minans; } void solve() { Set(nonused, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (substr(i, j)) nonused[j] = 1; } } int cnt = 0; for (int i = 0; i < n; i++) { if (!nonused[i]) pnt[cnt++] = i; } n = cnt; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; mz[i][j] = count_w(pnt[i], pnt[j]); // debug("%d %d %d\n",i,j,mz[i][j]); } } Set(dp, -1); int ans = oo; // ans=dfs(1,0,0); for (int i = 0; i < n; i++) { int t = strlen(str[pnt[i]]) + dfs(1 << i, i, 0); ans = min(ans, t); } printf("%d\n", ans); } int main() { ios_base::sync_with_stdio(0); while (~scanf("%d", &n) && n) { for (int i = 0; i < n; i++) scanf("%s", str[i]); Set(mz, 0); solve(); } }
[["+", 3, 4, 0, 69, 341, 342, 0, 69, 28, 22], ["+", 0, 69, 341, 342, 0, 69, 341, 342, 0, 70], ["+", 0, 69, 341, 342, 0, 69, 341, 342, 0, 73]]
1
981
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) typedef complex<double> P; typedef pair<P, P> Edge; typedef long long ll; const double PI = 4 * atan(1.0); void fast_stream() { std::ios_base::sync_with_stdio(0); } #define RED_FILE #ifdef RED_FILE ifstream ifs("input.txt"); ofstream ofs("output.txt"); #define cin ifs #define cout ofs #endif int n, m; P bp[1000]; P wp[1000]; // “àÏ double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); } // ŠOÏ double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); } // “_a,b‚ð’ʂ钼ü‚Æ“_c‚ÌŠÔ‚Ì‹——£ double distance_l_p(P a, P b, P c) { return abs(cross(b - a, c - a)) / abs(b - a); } // ‰E‘¤‚Ì—v‘f‚ª‚ ‚éF‚Å‚ ‚é‚©‚Ç‚¤‚©ƒ`ƒFƒbƒN bool checkRIghtincludeLine(P a, P b, bool isW) { if (isW) { for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (wp[i].real() < a.real() && !EQ(wp[i].real(), a.real())) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * wp[i].real() + e > wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if (!(bp[i].real() < a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * bp[i].real() + e > bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } return true; } else { for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if ((bp[i].real() < a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * bp[i].real() + e > bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (!(wp[i].real() < a.real() && !EQ(wp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * wp[i].real() + e > wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } return true; } } // ¶‘¤‚Ì—v‘f‚ª‚ ‚éF‚Å‚ ‚é‚©‚Ç‚¤‚©ƒ`ƒFƒbƒN bool checkLeftincludeLine(P a, P b, bool isW) { if (isW) { for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (wp[i].real() > a.real() && !EQ(wp[i].real(), a.real())) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * wp[i].real() + e < wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if (!(bp[i].real() > a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * bp[i].real() + e < bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } return true; } else { for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if ((bp[i].real() > a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * bp[i].real() + e < bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (!(wp[i].real() > a.real() && !EQ(wp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * wp[i].real() + e < wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } return true; } } int main() { while (cin >> n >> m && !(n == 0 && m == 0)) { for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; bp[i] = P(x, y); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; wp[i] = P(x, y); } if (n == 1 && m == 1) { cout << "YES" << endl; continue; } if (m != 1) { // 2“_‚ð‘I‚ñ‚Å’¼ü‚ðì¬‚µAŠe“_‚ª‚»‚Ì’¼ü‚ÅŠ®‘S‚É•ªŠ„‚Å‚«‚é‚©’²‚ׂé bool ok = false; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { bool isLeft = false; bool no = false; // x‚ªˆê’v if (EQ(wp[i].real(), wp[j].real())) { // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < n; k++) { if (EQ(bp[k].real(), wp[i].real())) { no = true; break; } else { if (bp[k].real() < wp[i].real()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } else { // y=d*x+e double d = (wp[i].imag() - wp[j].imag()) / (wp[i].real() - wp[j].real()); double e = wp[j].imag() - wp[j].real() * d; // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < n; k++) { if (EQ(d * bp[k].real() + e, bp[k].imag())) { no = true; break; } else { if (d * bp[k].real() + e < bp[k].imag()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } // ¶‚ɍ•‚ªŠ‘® if (isLeft) { if (checkRIghtincludeLine(wp[i], wp[j], true)) { // ok ok = true; break; } } // ‰E else { if (checkLeftincludeLine(wp[i], wp[j], true)) { // ok ok = true; break; } } } if (ok) break; } if (ok) { cout << "YES" << endl; continue; } } if (n != 1) { // 2“_‚ð‘I‚ñ‚Å’¼ü‚ðì¬‚µAŠe“_‚ª‚»‚Ì’¼ü‚ÅŠ®‘S‚É•ªŠ„‚Å‚«‚é‚©’²‚ׂé bool ok = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bool isLeft = false; bool no = false; // x‚ªˆê’v if (EQ(bp[i].real(), bp[j].real())) { // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < m; k++) { if (EQ(wp[k].real(), bp[i].real())) { no = true; break; } else { if (wp[k].real() < bp[i].real()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } else { // y=d*x+e double d = (bp[i].imag() - bp[j].imag()) / (bp[i].real() - bp[j].real()); double e = bp[j].imag() - bp[j].real() * d; // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < m; k++) { if (EQ(d * wp[k].real() + e, wp[k].imag())) { no = true; break; } else { if (d * wp[k].real() + e < wp[k].imag()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } // ¶‚ɍ•‚ªŠ‘® if (isLeft) { if (checkRIghtincludeLine(bp[i], bp[j], false)) { // ok ok = true; break; } } // ‰E else { if (checkLeftincludeLine(bp[i], bp[j], false)) { // ok ok = true; break; } } } if (ok) break; } if (ok) { cout << "YES" << endl; continue; } } cout << "NO" << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) typedef complex<double> P; typedef pair<P, P> Edge; typedef long long ll; const double PI = 4 * atan(1.0); void fast_stream() { std::ios_base::sync_with_stdio(0); } //#define RED_FILE #ifdef RED_FILE ifstream ifs("input.txt"); ofstream ofs("output.txt"); #define cin ifs #define cout ofs #endif int n, m; P bp[1000]; P wp[1000]; // “àÏ double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); } // ŠOÏ double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); } // “_a,b‚ð’ʂ钼ü‚Æ“_c‚ÌŠÔ‚Ì‹——£ double distance_l_p(P a, P b, P c) { return abs(cross(b - a, c - a)) / abs(b - a); } // ‰E‘¤‚Ì—v‘f‚ª‚ ‚éF‚Å‚ ‚é‚©‚Ç‚¤‚©ƒ`ƒFƒbƒN bool checkRIghtincludeLine(P a, P b, bool isW) { if (isW) { for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (wp[i].real() < a.real() && !EQ(wp[i].real(), a.real())) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * wp[i].real() + e > wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if (!(bp[i].real() < a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * bp[i].real() + e > bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } return true; } else { for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if ((bp[i].real() < a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * bp[i].real() + e > bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (!(wp[i].real() < a.real() && !EQ(wp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * wp[i].real() + e > wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } return true; } } // ¶‘¤‚Ì—v‘f‚ª‚ ‚éF‚Å‚ ‚é‚©‚Ç‚¤‚©ƒ`ƒFƒbƒN bool checkLeftincludeLine(P a, P b, bool isW) { if (isW) { for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (wp[i].real() > a.real() && !EQ(wp[i].real(), a.real())) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * wp[i].real() + e < wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if (!(bp[i].real() > a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * bp[i].real() + e < bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } return true; } else { for (int i = 0; i < n; i++) { if (EQ(a.real(), b.real())) { if ((bp[i].real() > a.real() && !EQ(bp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if (!(d * bp[i].real() + e < bp[i].imag() || EQ(d * bp[i].real() + e, bp[i].imag()))) return false; } } for (int i = 0; i < m; i++) { if (EQ(a.real(), b.real())) { if (!(wp[i].real() > a.real() && !EQ(wp[i].real(), a.real()))) return false; } else { // y=d*x+e double d = (a.imag() - b.imag()) / (a.real() - b.real()); double e = b.imag() - b.real() * d; if ((d * wp[i].real() + e < wp[i].imag() || EQ(d * wp[i].real() + e, wp[i].imag()))) return false; } } return true; } } int main() { while (cin >> n >> m && !(n == 0 && m == 0)) { for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; bp[i] = P(x, y); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; wp[i] = P(x, y); } if (n == 1 && m == 1) { cout << "YES" << endl; continue; } if (m != 1) { // 2“_‚ð‘I‚ñ‚Å’¼ü‚ðì¬‚µAŠe“_‚ª‚»‚Ì’¼ü‚ÅŠ®‘S‚É•ªŠ„‚Å‚«‚é‚©’²‚ׂé bool ok = false; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { bool isLeft = false; bool no = false; // x‚ªˆê’v if (EQ(wp[i].real(), wp[j].real())) { // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < n; k++) { if (EQ(bp[k].real(), wp[i].real())) { no = true; break; } else { if (bp[k].real() < wp[i].real()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } else { // y=d*x+e double d = (wp[i].imag() - wp[j].imag()) / (wp[i].real() - wp[j].real()); double e = wp[j].imag() - wp[j].real() * d; // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < n; k++) { if (EQ(d * bp[k].real() + e, bp[k].imag())) { no = true; break; } else { if (d * bp[k].real() + e < bp[k].imag()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } // ¶‚ɍ•‚ªŠ‘® if (isLeft) { if (checkRIghtincludeLine(wp[i], wp[j], true)) { // ok ok = true; break; } } // ‰E else { if (checkLeftincludeLine(wp[i], wp[j], true)) { // ok ok = true; break; } } } if (ok) break; } if (ok) { cout << "YES" << endl; continue; } } if (n != 1) { // 2“_‚ð‘I‚ñ‚Å’¼ü‚ðì¬‚µAŠe“_‚ª‚»‚Ì’¼ü‚ÅŠ®‘S‚É•ªŠ„‚Å‚«‚é‚©’²‚ׂé bool ok = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bool isLeft = false; bool no = false; // x‚ªˆê’v if (EQ(bp[i].real(), bp[j].real())) { // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < m; k++) { if (EQ(wp[k].real(), bp[i].real())) { no = true; break; } else { if (wp[k].real() < bp[i].real()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } else { // y=d*x+e double d = (bp[i].imag() - bp[j].imag()) / (bp[i].real() - bp[j].real()); double e = bp[j].imag() - bp[j].real() * d; // ‚Ç‚¿‚ç‚Ì—v‘f‚ª‚Ç‚¿‚ç‚É‘®‚·‚é‚© for (int k = 0; k < m; k++) { if (EQ(d * wp[k].real() + e, wp[k].imag())) { no = true; break; } else { if (d * wp[k].real() + e < wp[k].imag()) { isLeft = true; break; } else { isLeft = false; break; } } } if (no) continue; } // ¶‚ɍ•‚ªŠ‘® if (isLeft) { if (checkRIghtincludeLine(bp[i], bp[j], false)) { // ok ok = true; break; } } // ‰E else { if (checkLeftincludeLine(bp[i], bp[j], false)) { // ok ok = true; break; } } } if (ok) break; } if (ok) { cout << "YES" << endl; continue; } } cout << "NO" << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 0, 148], ["-", 36, 36, 36, 36, 0, 30, 0, 58, 141, 22]]
1
2,785
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; using ti3 = tuple<int, int, int>; using vti3 = vector<ti3>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = (a)-1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) #define repi(i, a, b) \ for (int i = a; i < b; i++) \ ; const ll mod = 1000000007; template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int popcnt(unsigned long long a) { a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); } class unionfind { vector<int> par, rank, size_; //????????§??????????????¢???????????????????????????rank???????????????size????????? public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; typedef complex<ld> P; typedef vector<P> VP; const ld eps = 1e-11, pi = acos(-1.0); ld dot(P a, P b) { return real(conj(a) * b); } ld cross(P a, P b) { return imag(conj(a) * b); } namespace std { bool operator<(const P &a, const P &b) { return abs(a.real() - b.real()) < eps ? a.imag() < b.imag() : a.real() < b.real(); } } // namespace std struct L { P a, b; }; // line->l,segment->s struct C { P p; ld r; }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > eps) return 1; // counter clockwise if (cross(b, c) < -eps) return -1; // clockwise if (dot(b, c) < 0) return 2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; // a--c--b on line } bool isis_ll(L l, L m) { // is intersect return abs(cross(l.b - l.a, m.b - m.a)) > eps; } bool isis_ls(L l, L s) { ld a = cross(l.b - l.a, s.a - l.a); ld b = cross(l.b - l.a, s.b - l.a); return (a * b < eps); } bool isis_lp(L l, P p) { return abs(cross(l.b - p, l.a - p)) < eps; } bool isis_ss(L s, L t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } P is_ll(L s, L t) { // intersect P sv = s.b - s.a, tv = t.b - t.a; return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } bool isis_sp(L s, P p) { return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps; } P proj(L l, P p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } ld dist_lp(L l, P p) { return abs(p - proj(l, p)); } ld dist_ll(L l, L m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls(L l, L s) { if (isis_ls(l, s)) return 0; return min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp(L s, P p) { P r = proj(s, p); if (isis_sp(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } ld dist_ss(L s, L t) { if (isis_ss(s, t)) return 0; ld a = min(dist_sp(s, t.a), dist_sp(t, s.a)); ld b = min(dist_sp(s, t.b), dist_sp(t, s.b)); return min(a, b); } VP is_cc(C c1, C c2) { VP res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); P diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * P(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * P(rc, -rs)); return res; } bool isis_vc(vector<C> vc) { VP crs; int n = vc.size(); rep(i, n) rep(j, i) for (P p : is_cc(vc[i], vc[j])) crs.push_back(p); rep(i, n) crs.push_back(vc[i].p); for (P p : crs) { bool valid = true; rep(i, n) if (abs(p - vc[i].p) > vc[i].r + eps) valid = false; if (valid) return true; } return false; } VP is_lc(C c, L l) { VP res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; P nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } VP is_sc(C c, L l) { VP v = is_lc(c, l), res; for (P p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<L> tangent_cp(C c, P p) { //????????\???? vector<L> ret; P v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (std::isnan(l)) { return ret; } P v1 = v * P(l / d, c.r / d); P v2 = v * P(l / d, -c.r / d); ret.push_back(L{p, p + v1}); if (l < eps) return ret; ret.push_back(L{p, p + v2}); return ret; } vector<L> tangent_cc(C c1, C c2) { vector<L> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<L> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { P v = c2.p - c1.p; v /= abs(v); P q1 = c1.p + v * P(0, 1) * c1.r; P q2 = c1.p + v * P(0, -1) * c1.r; ret.push_back(L{q1, q1 + v}); ret.push_back(L{q2, q2 + v}); } return ret; } ld area(const VP &p) { //??¢????? ld res = 0; int n = p.size(); rep(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } bool is_polygon(L l, VP &g) { int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i]; P b = g[(i + 1) % n]; if (isis_ss(l, L{a, b})) return true; } return false; } int is_in_Polygon(const VP &g, P p) { bool in = false; int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i] - p, b = g[(i + 1) % n] - p; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (abs(cross(a, b)) < eps && dot(a, b) < eps) return 0; // on } if (in) return 1; // in return -1; // out } VP ConvexHull(VP ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); VP ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } VP ConvexCut(const VP &ps, L l) { VP Q; for (int i = 0; i < (int)ps.size(); i++) { P A = ps[i], B = ps[(i + 1) % ps.size()]; if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0) Q.push_back(is_ll(L{A, B}, l)); } return Q; } // end of lib // template<class S=void,int ptr_num, class T = char>class trie { // umap<T, trie<S, ptr_num, T> next; // public: // S key; // trie<S, ptr_num, T>* ptr[ptr_num] = {}; // trie(S &&data) :key(data) {} // trie(const S &data) :key(data) {} // void add(T x,S data) { // if (!next.find(x))next.insert(x, data); // } // trie& operator[](T x) { // return next[x]; // } // bool find(T x) { // retun next.find(x); // } //}; // template<class T=char>class AhoCorasick { // trie<pair<bool,int>, 2, T> tree; // AhoCorasick(vector<string> p) { // int num = 0; // vector<decltype(&tree)> que(p.size(),&tree); // for (int i = 0;; i++) { // bool end = 1; // int i = 0; // for (auto a : p) { // if (i >= a.size())break; // end = ;0 // que[i] = (*que[i])[a[i]]; // i++; // } // if (end)break; // } // } //}; #include <fstream> #include <sstream> class svg { ofstream f; stringstream s; double x1 = 1e9, y1 = 1e9, x2 = -1e9, y2 = -1e9; public: svg(string path) : f(path, ios::trunc) {} ~svg() { f << "<?xml version=\"1.0\" standalone=\"no\"?><svg viewBox='" << x1 * 1.1 - x2 * 0.1 << " " << y1 * 1.1 - y2 * 0.1 << " " << (x2 - x1) * 1.2 << " " << (y2 - y1) * 1.2 << "' xmlns = \"http://www.w3.org/2000/svg\" xmlns:xlink = " "\"http://www.w3.org/1999/xlink\" >\n"; f << s.str(); f << "</svg>"; } void circle(C c) { s << fixed << "<circle cx = \"" << c.p.real() << "\" cy = \"" << c.p.imag() << "\" r = \"" << c.r << "\" stroke=\"black\" stroke-width=\"0.01\" fill = \"none\"/>\n"; cmin(x1, c.p.real() - c.r); cmin(y1, c.p.imag() - c.r); cmax(x2, c.p.real() + c.r); cmax(y2, c.p.imag() + c.r); } void line(L l) { s << fixed << "<line x1=\"" << l.a.real() << "\" y1=\"" << l.a.imag() << "\" x2=\"" << l.b.real() << "\" y2=\"" << l.b.imag() << "\" stroke=\"black\" stroke-width=\"0.01\"/>\n"; cmin(x1, l.a.real()); cmin(y1, l.a.imag()); cmax(x2, l.a.real()); cmax(y2, l.a.imag()); cmin(x1, l.b.real()); cmin(y1, l.b.imag()); cmax(x2, l.b.real()); cmax(y2, l.b.imag()); } }; int main() { VP p(3); for (;;) { rep(i, 3) { int x, y; cin >> x >> y; p[i] = {(double)x, (double)y}; } if (p[0] == p[1]) break; P up1 = (p[1] * abs(p[2] - p[0]) + p[2] * abs(p[1] - p[0])) / (abs(p[2] - p[0]) + abs(p[1] - p[0])); P dn1 = p[0]; P up2, up3; double r1, r2, r3; rep(i, 30) { P m1 = (up1 + dn1) * 0.5; r1 = dist_lp({p[0], p[1]}, m1); up2 = (p[0] * abs(p[2] - p[1]) + p[2] * abs(p[0] - p[1])) / (abs(p[2] - p[1]) + abs(p[0] - p[1])); up3 = (p[0] * abs(p[2] - p[1]) + p[1] * abs(p[0] - p[2])) / (abs(p[2] - p[1]) + abs(p[0] - p[2])); P dn2 = p[1], dn3 = p[2]; rep(j, 30) { P m2 = (up2 + dn2) * 0.5, m3 = (up3 + dn3) * 0.5; r2 = dist_lp({p[0], p[1]}, m2); r3 = dist_lp({p[0], p[2]}, m3); if (!isis_ls({p[0], up1}, {p[1], m2}) && dist_lp({p[0], p[2]}, m2) > r2 && abs(m1 - m2) > r1 + r2) dn2 = m2; else up2 = m2; if (!isis_ls({p[0], up1}, {p[2], m3}) && dist_lp({p[0], p[1]}, m3) > r3 && abs(m3 - m1) > r1 + r3) dn3 = m3; else up3 = m3; } if (dist_lp({p[1], p[2]}, m1) > r1 && abs(up2 - up3) < r2 + r3) dn1 = m1; else up1 = m1; } cout << fixed << setprecision(10) << r1 << "," << r2 << "," << r3 << endl; svg s("out.svg"); rep(i, 3) s.line({p[i], p[(i + 1) % 3]}); s.circle({up1, r1}); s.circle({up2, r2}); s.circle({up3, r3}); } }
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; using ti3 = tuple<int, int, int>; using vti3 = vector<ti3>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = (a)-1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) #define repi(i, a, b) \ for (int i = a; i < b; i++) \ ; const ll mod = 1000000007; template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int popcnt(unsigned long long a) { a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); } class unionfind { vector<int> par, rank, size_; //????????§??????????????¢???????????????????????????rank???????????????size????????? public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; typedef complex<ld> P; typedef vector<P> VP; const ld eps = 1e-11, pi = acos(-1.0); ld dot(P a, P b) { return real(conj(a) * b); } ld cross(P a, P b) { return imag(conj(a) * b); } namespace std { bool operator<(const P &a, const P &b) { return abs(a.real() - b.real()) < eps ? a.imag() < b.imag() : a.real() < b.real(); } } // namespace std struct L { P a, b; }; // line->l,segment->s struct C { P p; ld r; }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > eps) return 1; // counter clockwise if (cross(b, c) < -eps) return -1; // clockwise if (dot(b, c) < 0) return 2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; // a--c--b on line } bool isis_ll(L l, L m) { // is intersect return abs(cross(l.b - l.a, m.b - m.a)) > eps; } bool isis_ls(L l, L s) { ld a = cross(l.b - l.a, s.a - l.a); ld b = cross(l.b - l.a, s.b - l.a); return (a * b < eps); } bool isis_lp(L l, P p) { return abs(cross(l.b - p, l.a - p)) < eps; } bool isis_ss(L s, L t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } P is_ll(L s, L t) { // intersect P sv = s.b - s.a, tv = t.b - t.a; return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } bool isis_sp(L s, P p) { return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps; } P proj(L l, P p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } ld dist_lp(L l, P p) { return abs(p - proj(l, p)); } ld dist_ll(L l, L m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls(L l, L s) { if (isis_ls(l, s)) return 0; return min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp(L s, P p) { P r = proj(s, p); if (isis_sp(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } ld dist_ss(L s, L t) { if (isis_ss(s, t)) return 0; ld a = min(dist_sp(s, t.a), dist_sp(t, s.a)); ld b = min(dist_sp(s, t.b), dist_sp(t, s.b)); return min(a, b); } VP is_cc(C c1, C c2) { VP res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); P diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * P(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * P(rc, -rs)); return res; } bool isis_vc(vector<C> vc) { VP crs; int n = vc.size(); rep(i, n) rep(j, i) for (P p : is_cc(vc[i], vc[j])) crs.push_back(p); rep(i, n) crs.push_back(vc[i].p); for (P p : crs) { bool valid = true; rep(i, n) if (abs(p - vc[i].p) > vc[i].r + eps) valid = false; if (valid) return true; } return false; } VP is_lc(C c, L l) { VP res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; P nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } VP is_sc(C c, L l) { VP v = is_lc(c, l), res; for (P p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<L> tangent_cp(C c, P p) { //????????\???? vector<L> ret; P v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (std::isnan(l)) { return ret; } P v1 = v * P(l / d, c.r / d); P v2 = v * P(l / d, -c.r / d); ret.push_back(L{p, p + v1}); if (l < eps) return ret; ret.push_back(L{p, p + v2}); return ret; } vector<L> tangent_cc(C c1, C c2) { vector<L> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<L> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { P v = c2.p - c1.p; v /= abs(v); P q1 = c1.p + v * P(0, 1) * c1.r; P q2 = c1.p + v * P(0, -1) * c1.r; ret.push_back(L{q1, q1 + v}); ret.push_back(L{q2, q2 + v}); } return ret; } ld area(const VP &p) { //??¢????? ld res = 0; int n = p.size(); rep(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } bool is_polygon(L l, VP &g) { int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i]; P b = g[(i + 1) % n]; if (isis_ss(l, L{a, b})) return true; } return false; } int is_in_Polygon(const VP &g, P p) { bool in = false; int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i] - p, b = g[(i + 1) % n] - p; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (abs(cross(a, b)) < eps && dot(a, b) < eps) return 0; // on } if (in) return 1; // in return -1; // out } VP ConvexHull(VP ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); VP ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } VP ConvexCut(const VP &ps, L l) { VP Q; for (int i = 0; i < (int)ps.size(); i++) { P A = ps[i], B = ps[(i + 1) % ps.size()]; if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0) Q.push_back(is_ll(L{A, B}, l)); } return Q; } // end of lib // template<class S=void,int ptr_num, class T = char>class trie { // umap<T, trie<S, ptr_num, T> next; // public: // S key; // trie<S, ptr_num, T>* ptr[ptr_num] = {}; // trie(S &&data) :key(data) {} // trie(const S &data) :key(data) {} // void add(T x,S data) { // if (!next.find(x))next.insert(x, data); // } // trie& operator[](T x) { // return next[x]; // } // bool find(T x) { // retun next.find(x); // } //}; // template<class T=char>class AhoCorasick { // trie<pair<bool,int>, 2, T> tree; // AhoCorasick(vector<string> p) { // int num = 0; // vector<decltype(&tree)> que(p.size(),&tree); // for (int i = 0;; i++) { // bool end = 1; // int i = 0; // for (auto a : p) { // if (i >= a.size())break; // end = ;0 // que[i] = (*que[i])[a[i]]; // i++; // } // if (end)break; // } // } //}; #include <fstream> #include <sstream> class svg { ofstream f; stringstream s; double x1 = 1e9, y1 = 1e9, x2 = -1e9, y2 = -1e9; public: svg(string path) : f(path, ios::trunc) {} ~svg() { f << "<?xml version=\"1.0\" standalone=\"no\"?><svg viewBox='" << x1 * 1.1 - x2 * 0.1 << " " << y1 * 1.1 - y2 * 0.1 << " " << (x2 - x1) * 1.2 << " " << (y2 - y1) * 1.2 << "' xmlns = \"http://www.w3.org/2000/svg\" xmlns:xlink = " "\"http://www.w3.org/1999/xlink\" >\n"; f << s.str(); f << "</svg>"; } void circle(C c) { s << fixed << "<circle cx = \"" << c.p.real() << "\" cy = \"" << c.p.imag() << "\" r = \"" << c.r << "\" stroke=\"black\" stroke-width=\"0.01\" fill = \"none\"/>\n"; cmin(x1, c.p.real() - c.r); cmin(y1, c.p.imag() - c.r); cmax(x2, c.p.real() + c.r); cmax(y2, c.p.imag() + c.r); } void line(L l) { s << fixed << "<line x1=\"" << l.a.real() << "\" y1=\"" << l.a.imag() << "\" x2=\"" << l.b.real() << "\" y2=\"" << l.b.imag() << "\" stroke=\"black\" stroke-width=\"0.01\"/>\n"; cmin(x1, l.a.real()); cmin(y1, l.a.imag()); cmax(x2, l.a.real()); cmax(y2, l.a.imag()); cmin(x1, l.b.real()); cmin(y1, l.b.imag()); cmax(x2, l.b.real()); cmax(y2, l.b.imag()); } }; int main() { VP p(3); for (;;) { rep(i, 3) { int x, y; cin >> x >> y; p[i] = {(double)x, (double)y}; } if (p[0] == p[1]) break; P up1 = (p[1] * abs(p[2] - p[0]) + p[2] * abs(p[1] - p[0])) / (abs(p[2] - p[0]) + abs(p[1] - p[0])); P dn1 = p[0]; P up2, up3; double r1, r2, r3; rep(i, 50) { P m1 = (up1 + dn1) * 0.5; r1 = dist_lp({p[0], p[1]}, m1); up2 = (p[0] * abs(p[2] - p[1]) + p[2] * abs(p[0] - p[1])) / (abs(p[2] - p[1]) + abs(p[0] - p[1])); up3 = (p[0] * abs(p[2] - p[1]) + p[1] * abs(p[0] - p[2])) / (abs(p[2] - p[1]) + abs(p[0] - p[2])); P dn2 = p[1], dn3 = p[2]; rep(j, 50) { P m2 = (up2 + dn2) * 0.5, m3 = (up3 + dn3) * 0.5; r2 = dist_lp({p[0], p[1]}, m2); r3 = dist_lp({p[0], p[2]}, m3); if (!isis_ls({p[0], up1}, {p[1], m2}) && dist_lp({p[0], p[2]}, m2) > r2 && abs(m1 - m2) > r1 + r2) dn2 = m2; else up2 = m2; if (!isis_ls({p[0], up1}, {p[2], m3}) && dist_lp({p[0], p[1]}, m3) > r3 && abs(m3 - m1) > r1 + r3) dn3 = m3; else up3 = m3; } if (dist_lp({p[1], p[2]}, m1) > r1 && abs(up2 - up3) < r2 + r3) dn1 = m1; else up1 = m1; } cout << fixed << setprecision(10) << r1 << " " << r2 << " " << r3 << endl; svg s("out.svg"); rep(i, 3) s.line({p[i], p[(i + 1) % 3]}); s.circle({up1, r1}); s.circle({up2, r2}); s.circle({up3, r3}); } }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 16, 31, 16, 31, 16, 12, 5, 0, 6]]
1
4,654
#include <bits/stdc++.h> using namespace std; int m, n; int ans[15]; int dp[2050][2050]; // S?????????????????£?????? using Set = bitset<12>; int rec(int asked, int answer) { // ??????????????????aked???answer??¨?????????????????? int &res = dp[asked][answer]; if (res != -1) return res; res = m; // vector<int> a; { int cnt = 0; for (int i = 0; i < n; ++i) { if ((ans[i] & asked) == answer) { ++cnt; } } if (cnt <= 1) return res = 0; } // cout << "A " << Set(asked) << ' ' << Set(answer) << endl; // cout << "B "; // for(int &x : a) cout << Set(ans[x]) << ' '; // cout << endl; for (int i = 0; i < m; ++i) { if (asked >> i & 1) continue; int cnt[2] = {}; for (int j = 0; j < n; ++j) { if ((ans[j] & asked) == answer) { ++cnt[ans[i] >> j & 1]; } } // if(cnt[0] == 0 || cnt[1] == 0) continue; int tmp = 0; for (int j = 0; j < 2; ++j) { tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i))); } res = min(res, tmp); } // assert(res != 1e9); return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); while (cin >> m >> n && m) { for (int i = 0; i < n; ++i) { char buf[100]; cin >> buf; ans[i] = 0; for (int j = 0; j < m; ++j) { if (buf[j] - '0') ans[i] |= 1 << j; } } memset(dp, -1, sizeof(dp)); cout << rec(0, 0) << endl; // cout << "=================" << endl; } }
#include <bits/stdc++.h> using namespace std; int m, n; int ans[150]; int dp[2050][2050]; // S?????????????????£?????? using Set = bitset<12>; int rec(int asked, int answer) { // ??????????????????aked???answer??¨?????????????????? int &res = dp[asked][answer]; if (res != -1) return res; res = m; // vector<int> a; { int cnt = 0; for (int i = 0; i < n; ++i) { if ((ans[i] & asked) == answer) { ++cnt; } } if (cnt <= 1) return res = 0; } // cout << "A " << Set(asked) << ' ' << Set(answer) << endl; // cout << "B "; // for(int &x : a) cout << Set(ans[x]) << ' '; // cout << endl; for (int i = 0; i < m; ++i) { if (asked >> i & 1) continue; int cnt[2] = {}; for (int j = 0; j < n; ++j) { if ((ans[j] & asked) == answer) { ++cnt[ans[i] >> j & 1]; } } // if(cnt[0] == 0 || cnt[1] == 0) continue; int tmp = 0; for (int j = 0; j < 2; ++j) { tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i))); } res = min(res, tmp); } // assert(res != 1e9); return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); while (cin >> m >> n && m) { for (int i = 0; i < n; ++i) { char buf[100]; cin >> buf; ans[i] = 0; for (int j = 0; j < m; ++j) { if (buf[j] - '0') ans[i] |= 1 << j; } } memset(dp, -1, sizeof(dp)); cout << rec(0, 0) << endl; // cout << "=================" << endl; } }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13]]
1
394
#include <algorithm> #include <climits> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <vector> using namespace std; typedef unsigned long long ll; const int mod = 1000000007; int m, n; string g[129]; map<vector<string>, int> ma; int dfs(vector<string> q, int de) { if (q.size() < 2) return 0; if (ma[q]) return ma[q]; int ret = INT_MAX; for (int i = 0; i < m; i++) { vector<string> zero, one; for (int j = 0; j < q.size(); j++) { if (q[j][i] == '0') { zero.push_back(q[j]); } else { one.push_back(q[j]); } } if (zero.size() == q.size() || one.size() == q.size()) { continue; } int o = dfs(one, de + 1); int z = dfs(zero, de + 1); if (max(o, z) + 1 < ret) { ret = max(o, z) + 1; } // ret=min(ret,min(dfs(one),dfs(zero))+1); } return ma[q] = ret; } int main() { while (cin >> m >> n) { vector<string> q; for (int i = 0; i < n; i++) { cin >> g[i]; q.push_back(g[i]); } cout << dfs(q, 0) << endl; } }
#include <algorithm> #include <climits> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <vector> using namespace std; typedef unsigned long long ll; const int mod = 1000000007; int m, n; string g[129]; map<vector<string>, int> ma; int dfs(vector<string> q, int de) { if (q.size() < 2) return 0; if (ma[q]) return ma[q]; int ret = INT_MAX; for (int i = 0; i < m; i++) { vector<string> zero, one; for (int j = 0; j < q.size(); j++) { if (q[j][i] == '0') { zero.push_back(q[j]); } else { one.push_back(q[j]); } } if (zero.size() == q.size() || one.size() == q.size()) { continue; } int o = dfs(one, de + 1); int z = dfs(zero, de + 1); if (max(o, z) + 1 < ret) { ret = max(o, z) + 1; } // ret=min(ret,min(dfs(one),dfs(zero))+1); } return ma[q] = ret; } int main() { while (cin >> m >> n, m) { vector<string> q; for (int i = 0; i < n; i++) { cin >> g[i]; q.push_back(g[i]); } cout << dfs(q, 0) << endl; } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
341
import java.util.HashMap; import java.util.Scanner; import java.util.TreeSet; public class Main { static HashMap<String, String> groupdef; static String firstgroup; static TreeSet<String> members; static TreeSet<String> checked; static void get(String g) { String con = groupdef.get(g); int start = 0; int mem = 0; for (int i = 0; i < con.length(); i++) { if (con.charAt(i) == ',' || con.charAt(i) == '.') { String word = con.substring(start, i); start = i + 1; if (groupdef.containsKey(word) && !checked.contains(word)) { get(word); } else { members.add(word); } } } checked.add(g); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; groupdef = new HashMap<String, String>(); members = new TreeSet<String>(); checked = new TreeSet<String>(); for (int i = 0; i < n; i++) { String s = sc.next(); int lim = s.indexOf(':'); String groupname = s.substring(0, lim); String groupcon = s.substring(lim + 1); if (i == 0) firstgroup = groupname; groupdef.put(groupname, groupcon); } get(firstgroup); System.out.println(members.size()); } } }
import java.util.HashMap; import java.util.Scanner; import java.util.TreeSet; public class Main { static HashMap<String, String> groupdef; static String firstgroup; static TreeSet<String> members; static TreeSet<String> checked; static void get(String g) { String con = groupdef.get(g); int start = 0; int mem = 0; for (int i = 0; i < con.length(); i++) { if (con.charAt(i) == ',' || con.charAt(i) == '.') { String word = con.substring(start, i); start = i + 1; if (groupdef.containsKey(word)) { if (!checked.contains(word)) get(word); } else { members.add(word); } } } checked.add(g); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; groupdef = new HashMap<String, String>(); members = new TreeSet<String>(); checked = new TreeSet<String>(); for (int i = 0; i < n; i++) { String s = sc.next(); int lim = s.indexOf(':'); String groupname = s.substring(0, lim); String groupcon = s.substring(lim + 1); if (i == 0) firstgroup = groupname; groupdef.put(groupname, groupcon); } get(firstgroup); System.out.println(members.size()); } } }
[["-", 64, 196, 0, 57, 15, 15, 0, 16, 17, 98], ["+", 0, 57, 64, 196, 0, 57, 15, 15, 0, 25], ["+", 0, 57, 64, 196, 0, 57, 64, 196, 0, 45], ["+", 64, 196, 0, 57, 64, 196, 0, 57, 0, 121], ["+", 0, 57, 64, 196, 0, 57, 15, 15, 0, 24], ["-", 0, 57, 64, 196, 0, 57, 64, 196, 0, 45]]
3
357
#include <bits/stdc++.h> using namespace std; typedef vector<int> row; typedef vector<row> matrix; matrix multi(const matrix &a, const matrix &b, const int mod) { matrix c(a.size(), row(b[0].size(), 0)); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { for (int k = 0; k < b[0].size(); k++) { c[i][k] = (c[i][k] + a[i][j] * b[j][k]) % mod; } } } return c; } matrix pow(matrix a, int n, const int mod) { matrix b(a.size(), row(a.size(), 0)); for (int i = 0; i < a.size(); i++) b[i][i] = 1; while (n > 0) { if (n & 1) b = multi(b, a, mod); a = multi(a, a, mod); n >>= 1; } return b; } int main() { int N, M, A, B, C, T; while (cin >> N >> M >> A >> B >> C >> T, N) { matrix a(N, row(N, 0)), b(N, row(1, 0)); for (int i = 0; i < N; i++) { if (i > 0) a[i][i - 1] = C; a[i][i] = B; if (i + 1 < N) a[i][i + 1] = A; } for (int i = 0; i < N; i++) cin >> b[i][0]; a = multi(pow(a, T, M), b, M); for (int i = 0; i < N; i++) cout << a[i][0] << (i == N - 1 ? '\n' : ' '); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> row; typedef vector<row> matrix; matrix multi(const matrix &a, const matrix &b, const int mod) { matrix c(a.size(), row(b[0].size(), 0)); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { for (int k = 0; k < b[0].size(); k++) { c[i][k] = (c[i][k] + a[i][j] * b[j][k]) % mod; } } } return c; } matrix pow(matrix a, int n, const int mod) { matrix b(a.size(), row(a.size(), 0)); for (int i = 0; i < a.size(); i++) b[i][i] = 1; while (n > 0) { if (n & 1) b = multi(b, a, mod); a = multi(a, a, mod); n >>= 1; } return b; } int main() { int N, M, A, B, C, T; while (cin >> N >> M >> A >> B >> C >> T, N) { matrix a(N, row(N, 0)), b(N, row(1, 0)); for (int i = 0; i < N; i++) { if (i > 0) a[i][i - 1] = A; a[i][i] = B; if (i + 1 < N) a[i][i + 1] = C; } for (int i = 0; i < N; i++) cin >> b[i][0]; a = multi(pow(a, T, M), b, M); for (int i = 0; i < N; i++) cout << a[i][0] << (i == N - 1 ? '\n' : ' '); } return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 12, 22], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 22]]
1
485
#include <iostream> #include <stdio.h> #include <valarray> using namespace std; int N, M, T; struct Matrix { valarray<int> a; int N; Matrix(int N0) : a(N0 * N0), N(N0) { a = 0; } int &access(int i, int j) { return a[i * N + j]; } const int &access(int i, int j) const { return this->a[i * N + j]; } Matrix(const Matrix &other) : a(other.N * other.N), N(other.N) { a = other.a; } Matrix operator*(const Matrix &B) const { Matrix C(N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) C.a[i * N + j] = (a[slice(i * N, N, 1)] * B.a[slice(j, N, N)]).sum() % M; return C; } Matrix expt(int n) const { if (n == 0) { Matrix I(N); for (int i = 0; i < N; ++i) I.a[i * N + i] = 1; return I; } if (n == 1) return *this; if (n % 2) return expt(n - 1) * (*this); Matrix B = expt(n / 2); return B * B; } void show(void) const { puts("Matrix:("); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%d ", this->access(i, j)); } puts(""); } puts(")"); } valarray<int> apply(const valarray<int> &vec) const { valarray<int> result(N); for (int i = 0; i < N; i++) { // result[i]=(vec*valarray<int>(a[slice(i*N,N,1)])).sum()%M; result[i] = (vec * a[slice(i * N, N, 1)]).sum() % M; // valarray[slice]はslice_array型,const valarray[slice]はvalarray型 } return result; } }; Matrix getTrans(int n, int a, int b, int c) { Matrix t(n); for (int i = 0; i < n; i++) { if (i >= 1) { t.access(i, i - 1) = a; } t.access(i, i) = b; if (i <= n - 2) { t.access(i, i + 1) = c; } } return t; } int main(void) { int A, B, C; while (scanf("%d%d%d%d%d%d", &N, &M, &A, &B, &C, &T) == 6 && (N || M || A || B || C || T)) { Matrix trans = getTrans(N, A, B, C); valarray<int> init(N); for (int i = 0; i < N; i++) { int s; scanf("%d", &s); init[i] = s; } // trans.show(); Matrix powerT = trans.expt(T); valarray<int> result = powerT.apply(init); for (int i = 0; i < N; i++) { printf("%d", result[i]); if (i <= N - 2) { printf(" "); } } } }
#include <iostream> #include <stdio.h> #include <valarray> using namespace std; int N, M, T; struct Matrix { valarray<int> a; int N; Matrix(int N0) : a(N0 * N0), N(N0) { a = 0; } int &access(int i, int j) { return a[i * N + j]; } const int &access(int i, int j) const { return this->a[i * N + j]; } Matrix(const Matrix &other) : a(other.N * other.N), N(other.N) { a = other.a; } Matrix operator*(const Matrix &B) const { Matrix C(N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) C.a[i * N + j] = (a[slice(i * N, N, 1)] * B.a[slice(j, N, N)]).sum() % M; return C; } Matrix expt(int n) const { if (n == 0) { Matrix I(N); for (int i = 0; i < N; ++i) I.a[i * N + i] = 1; return I; } if (n == 1) return *this; if (n % 2) return expt(n - 1) * (*this); Matrix B = expt(n / 2); return B * B; } void show(void) const { puts("Matrix:("); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%d ", this->access(i, j)); } puts(""); } puts(")"); } valarray<int> apply(const valarray<int> &vec) const { valarray<int> result(N); for (int i = 0; i < N; i++) { // result[i]=(vec*valarray<int>(a[slice(i*N,N,1)])).sum()%M; result[i] = (vec * a[slice(i * N, N, 1)]).sum() % M; // valarray[slice]はslice_array型,const valarray[slice]はvalarray型 } return result; } }; Matrix getTrans(int n, int a, int b, int c) { Matrix t(n); for (int i = 0; i < n; i++) { if (i >= 1) { t.access(i, i - 1) = a; } t.access(i, i) = b; if (i <= n - 2) { t.access(i, i + 1) = c; } } return t; } int main(void) { int A, B, C; while (scanf("%d%d%d%d%d%d", &N, &M, &A, &B, &C, &T) == 6 && (N || M || A || B || C || T)) { Matrix trans = getTrans(N, A, B, C); valarray<int> init(N); for (int i = 0; i < N; i++) { int s; scanf("%d", &s); init[i] = s; } // trans.show(); Matrix powerT = trans.expt(T); valarray<int> result = powerT.apply(init); for (int i = 0; i < N; i++) { printf("%d", result[i]); if (i <= N - 2) { printf(" "); } else puts(""); } } }
[["+", 0, 7, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 0, 57, 75, 76, 0, 1, 0, 2, 63, 22], ["+", 75, 76, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["+", 75, 76, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 57, 75, 76, 0, 1, 0, 35]]
1
764
#include <cstdio> #include <valarray> using namespace std; typedef valarray<int> V; V e, s; int N, M; V mul(const V &a, const V &b) { V c(N * N); int i = 0, j; for (; i < N; i++) for (j = 0; j < N; j++) c[i * N + j] = (a[slice(i * N, N, 1)] * b[slice(j, N, N)]).sum() % M; return c; } V exp(const V &a, const int n) { // if(!n)return e; if (n == 1) return a; if (n % 2 == 1) return mul(exp(a, n - 1), a); V b = exp(a, n / 2); return mul(b, b); } main() { int i, a, b, c, t; for (; scanf("%d%d%d%d%d%d", &N, &M, &a, &b, &c, &t), N;) { s.resize(N); e.resize(1); e.resize(N * N); V x(N * N); for (i = 0; i < N; i++) { scanf("%d", &s[i]); e[i * N + i] = 1; if (i > 0) x[i * N + i - 1] = a; x[i * N + i] = b; if (i < N - 1) x[i * N + i + 1] = c; } const V r = exp(x, t); for (i = 0; i < N; i++) { printf(i < N - 1 ? "%d " : "%d\n", (r[slice(i * N, N, 1)] * s).sum() % M); } } }
#include <cstdio> #include <valarray> using namespace std; typedef valarray<int> V; V e, s; int N, M; V mul(const V &a, const V &b) { V c(N * N); int i = 0, j; for (; i < N; i++) for (j = 0; j < N; j++) c[i * N + j] = (a[slice(i * N, N, 1)] * b[slice(j, N, N)]).sum() % M; return c; } V exp(const V &a, const int n) { // if(!n)return e; if (n == 1) return a; if (n % 2 == 1) return mul(exp(a, n - 1), a); V b = exp(a, n / 2); return mul(b, b); } main() { int i, a, b, c, t; for (; scanf("%d%d%d%d%d%d", &N, &M, &a, &b, &c, &t), N;) { s.resize(N); e.resize(1); e.resize(N * N); V x(N * N); for (i = 0; i < N; i++) { scanf("%d", &s[i]); e[i * N + i] = 1; if (i > 0) x[i * N + i - 1] = a; x[i * N + i] = b; if (i < N - 1) x[i * N + i + 1] = c; } const V r = t ? exp(x, t) : e; for (i = 0; i < N; i++) { printf(i < N - 1 ? "%d " : "%d\n", (r[slice(i * N, N, 1)] * s).sum() % M); } } }
[["+", 8, 9, 0, 43, 49, 50, 51, 41, 15, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 41, 0, 101], ["+", 8, 9, 0, 43, 49, 50, 51, 41, 0, 102], ["+", 8, 9, 0, 43, 49, 50, 51, 41, 75, 22]]
1
429
#include <cstdio> #include <valarray> using namespace std; typedef valarray<int> V; int n, m; V z; V &Me(const V &_x, const V &_y) { int i = 0, j; for (; i < n; i++) for (j = 0; j < n; j++) z[i * n + j] = (_x[slice(i * n, n, 1)] * _y[slice(j, n, n)]).sum() % m; return z; } V &Mx(const V &_x) { int i = 0, j; for (; i < n; i++) for (j = 0; j < n; j++) z[i * n + j] = (_x[slice(i * n, n, 1)] * _x[slice(j, n, n)]).sum() % m; return z; } main() { int i, a, b, c, t; for (; scanf("%d%d%d%d%d%d", &n, &m, &a, &b, &c, &t), n;) { V x(n * n); V e(n * n); z.resize(n * n); x[0] = b; x[1] = c; e[0] = 1; for (i = 1; i < n - 1; i++) { x[i * n + i - 1] = a; x[i * n + i] = b; x[i * n + i + 1] = c; e[i * n + i] = 1; } x[i * n + i - 1] = a; x[i * n + i] = b; e[i * n + i] = 1; for (; t; t >>= 1) { if (t & 1) e = Me(e, x); x = Mx(x); } V s(n); for (i = 0; i < n; i++) scanf("%d", &s[i]); const V &r = e; for (; i < n - 1; i++) printf("%d ", (r[slice(i * n, n, 1)] * s).sum() % m); printf("%d\n", (r[slice(i * n, n, 1)] * s).sum() % m); } }
#include <cstdio> #include <valarray> using namespace std; typedef valarray<int> V; int n, m; V z; V &Me(const V &_x, const V &_y) { int i = 0, j; for (; i < n; i++) for (j = 0; j < n; j++) z[i * n + j] = (_x[slice(i * n, n, 1)] * _y[slice(j, n, n)]).sum() % m; return z; } V &Mx(const V &_x) { int i = 0, j; for (; i < n; i++) for (j = 0; j < n; j++) z[i * n + j] = (_x[slice(i * n, n, 1)] * _x[slice(j, n, n)]).sum() % m; return z; } main() { int i, a, b, c, t; for (; scanf("%d%d%d%d%d%d", &n, &m, &a, &b, &c, &t), n;) { V x(n * n); V e(n * n); z.resize(n * n); x[0] = b; x[1] = c; e[0] = 1; for (i = 1; i < n - 1; i++) { x[i * n + i - 1] = a; x[i * n + i] = b; x[i * n + i + 1] = c; e[i * n + i] = 1; } x[i * n + i - 1] = a; x[i * n + i] = b; e[i * n + i] = 1; for (; t; t >>= 1) { if (t & 1) e = Me(e, x); x = Mx(x); } V s(n); for (i = 0; i < n; i++) scanf("%d", &s[i]); const V &r = e; for (i = 0; i < n - 1; i++) printf("%d ", (r[slice(i * n, n, 1)] * s).sum() % m); printf("%d\n", (r[slice(i * n, n, 1)] * s).sum() % m); } }
[["+", 0, 7, 8, 9, 0, 7, 10, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 7, 10, 11, 17, 32], ["+", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13]]
1
544
#include <algorithm> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef vector<int> vec; typedef vector<vec> mat; int N, M, A, B, C, T; mat mat_mul(mat &A, mat &B) { mat C(A.size(), vec(B[0].size())); for (int i = 0; i < A.size(); i++) for (int k = 0; k < B.size(); k++) for (int j = 0; j < B[0].size(); j++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M; return C; } mat mod_pow(mat A, int N) { mat B(A.size(), vec(A.size())); for (int i = 0; i < A.size(); i++) B[i][i] = 1; while (N > 0) { if (N & 1) B = mat_mul(B, A); A = mat_mul(A, A); N >>= 1; } return B; } int main() { for (; scanf("%d%d%d%d%d%d", &N, &M, &A, &B, &C, &T), N;) { mat X(N, vec(N)); mat Y(N, vec(N)); for (int i = 0; i < N; i++) scanf("%d", &X[i][0]); for (int i = 0; i < N; i++) { if (i - 1 >= 0) Y[i][i - 1] = A; Y[i][i] = B; if (i + 1 < N) Y[i][i + 1] = C; } mat Z = mod_pow(Y, T); mat R = mat_mul(Z, X); for (int i = 0; i < N; i++) printf("%d%s", T == 0 ? X[0][i] : R[i][0], i == N - 1 ? "\n" : " "); } return 0; }
#include <algorithm> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef vector<int> vec; typedef vector<vec> mat; int N, M, A, B, C, T; mat mat_mul(mat &A, mat &B) { mat C(A.size(), vec(B[0].size())); for (int i = 0; i < A.size(); i++) for (int k = 0; k < B.size(); k++) for (int j = 0; j < B[0].size(); j++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M; return C; } mat mod_pow(mat A, int N) { mat B(A.size(), vec(A.size())); for (int i = 0; i < A.size(); i++) B[i][i] = 1; while (N > 0) { if (N & 1) B = mat_mul(B, A); A = mat_mul(A, A); N >>= 1; } return B; } int main() { for (; scanf("%d%d%d%d%d%d", &N, &M, &A, &B, &C, &T), N;) { mat X(N, vec(N)); mat Y(N, vec(N)); for (int i = 0; i < N; i++) scanf("%d", &X[i][0]); for (int i = 0; i < N; i++) { if (i - 1 >= 0) Y[i][i - 1] = A; Y[i][i] = B; if (i + 1 < N) Y[i][i + 1] = C; } mat Z = mod_pow(Y, T); mat R = mat_mul(Z, X); for (int i = 0; i < N; i++) printf("%d%s", T == 0 ? X[i][0] : R[i][0], i == N - 1 ? "\n" : " "); } return 0; }
[["-", 0, 41, 64, 69, 28, 69, 341, 342, 0, 13], ["+", 0, 41, 64, 69, 28, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 41, 64, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 41, 64, 69, 341, 342, 0, 13]]
1
502
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: n,M,A,B,C,T = LI() if n == 0: break a = tuple(LI()) if T == 0: rr.append(' '.join(map(str, a))) continue m = [[0]*n for _ in range(n)] for i in range(n): m[i][i] = B if i > 0: m[i][i-1] = A if i < n-1: m[i][i+1] = C mod = M class Matrix(): def __init__(self, A): self.A = A self.row = len(A) self.col = len(A[0]) def __iter__(self): return self.A.__iter__() def __getitem__(self, i): return self.A.__getitem__(i) def __add__(self, B): aa = self.A bb = B.A return Matrix([[(aa[i][j] + bb[i][j]) for j in range(self.col)] for i in range(self.row)]) def __sub__(self, B): aa = self.A bb = B.A return Matrix([[aa[i][j] - bb[i][j] for j in range(self.col)] for i in range(self.row)]) def __mul__(self, B): bb = [[B.A[j][i] for j in range(B.row)] for i in range(B.col)] return Matrix([[sum([ak * bk for ak,bk in zip(ai,bj)]) % mod for bj in bb] for ai in self.A]) def __truediv__(self, x): pass def pow(self, n): A = self r = Matrix([[0 if j != i else 1 for j in range(self.row)] for i in range(self.row)]) while n > 0: if n % 2 == 1: r *= A A *= A n //= 2 return r def __str__(self): return self.A.__str__() m = Matrix(m) mp = m.pow(T) r = mp * Matrix([[a[i]] for i in range(n)]) print(' '.join(map(lambda x: str(x[0]), r))) return '\n'.join(map(str,rr)) print(main())
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: n,M,A,B,C,T = LI() if n == 0: break a = tuple(LI()) if T == 0: rr.append(' '.join(map(str, a))) continue m = [[0]*n for _ in range(n)] for i in range(n): m[i][i] = B if i > 0: m[i][i-1] = A if i < n-1: m[i][i+1] = C mod = M class Matrix(): def __init__(self, A): self.A = A self.row = len(A) self.col = len(A[0]) def __iter__(self): return self.A.__iter__() def __getitem__(self, i): return self.A.__getitem__(i) def __add__(self, B): aa = self.A bb = B.A return Matrix([[(aa[i][j] + bb[i][j]) for j in range(self.col)] for i in range(self.row)]) def __sub__(self, B): aa = self.A bb = B.A return Matrix([[aa[i][j] - bb[i][j] for j in range(self.col)] for i in range(self.row)]) def __mul__(self, B): bb = [[B.A[j][i] for j in range(B.row)] for i in range(B.col)] return Matrix([[sum([ak * bk for ak,bk in zip(ai,bj)]) % mod for bj in bb] for ai in self.A]) def __truediv__(self, x): pass def pow(self, n): A = self r = Matrix([[0 if j != i else 1 for j in range(self.row)] for i in range(self.row)]) while n > 0: if n % 2 == 1: r *= A A *= A n //= 2 return r def __str__(self): return self.A.__str__() m = Matrix(m) mp = m.pow(T) r = mp * Matrix([[a[i]] for i in range(n)]) rr.append(' '.join(map(lambda x: str(x[0]), r))) return '\n'.join(map(str,rr)) print(main())
[["-", 0, 52, 8, 196, 0, 1, 0, 652, 63, 22], ["+", 8, 196, 0, 1, 0, 652, 63, 319, 500, 22], ["+", 8, 196, 0, 1, 0, 652, 63, 319, 0, 131], ["+", 8, 196, 0, 1, 0, 652, 63, 319, 319, 22]]
5
869
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define repit(it, u) \ for (__typeof((u).begin()) it = (u).begin(); it != (u).end(); ++it) #define all(u) (u).begin(), (u).end() #define rall(u) (u).rbegin(), (u).rend() #define uniq(u) (u).erase(unique(all(u)), (u).end()) #define ll long #define long int64_t #define mp make_pair #define pb push_back typedef double number; typedef vector<vector<number>> matrix; inline number my_hypot(number x, number y) { return sqrt(x * x + y * y); } inline void givens_rotate(number &x, number &y, number c, number s) { number u = c * x + s * y, v = -s * x + c * y; x = u, y = v; } vector<number> givens(matrix A, vector<number> b) { const int n = b.size(); rep(i, n) repi(j, i + 1, n) { const number r = my_hypot(A[i][i], A[j][i]); const number c = A[i][i] / r, s = A[j][i] / r; givens_rotate(b[i], b[j], c, s); repi(k, i + 1, n) givens_rotate(A[i][k], A[j][k], c, s); } for (int i = n - 1; i >= 0; --i) { repi(j, i + 1, n) b[i] -= A[i][j] * b[j]; b[i] /= A[i][i]; } return b; } const double eps = 1e-4; int d; vector<number> v; vector<number> solve_eq(int m, int n) { matrix A(d + 1, vector<number>(d + 1)); vector<number> b(d + 1); int cnt = 0; rep(i, d + 3) if (i != m && i != n) { b[cnt] = v[i]; rep(j, d + 1) A[cnt][j] = pow(1.0 * i, 1.0 * j); cnt++; } return givens(A, b); } int main() { while (cin >> d, d) { v.resize(d + 3); int ans = -1; rep(i, d + 3) cin >> v[i]; rep(i, d + 3) { repi(j, i + 1, d + 3) { vector<number> b = solve_eq(i, j); number e = 0, f = 0; rep(k, d + 1) e += pow(1.0 * i, 1.0 * k) * b[k]; rep(k, d + 1) f += pow(1.0 * j, 1.0 * k) * b[k]; if (abs(e - v[i]) > eps && abs(f - v[j]) < eps) { ans = i; break; } if (abs(e - v[i]) < eps && abs(f - v[j]) > eps) { ans = j; break; } } if (ans >= 0) break; } cout << ans << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define repit(it, u) \ for (__typeof((u).begin()) it = (u).begin(); it != (u).end(); ++it) #define all(u) (u).begin(), (u).end() #define rall(u) (u).rbegin(), (u).rend() #define uniq(u) (u).erase(unique(all(u)), (u).end()) #define ll long #define long int64_t #define mp make_pair #define pb push_back typedef double number; typedef vector<vector<number>> matrix; inline number my_hypot(number x, number y) { return sqrt(x * x + y * y); } inline void givens_rotate(number &x, number &y, number c, number s) { number u = c * x + s * y, v = -s * x + c * y; x = u, y = v; } vector<number> givens(matrix A, vector<number> b) { const int n = b.size(); rep(i, n) repi(j, i + 1, n) { const number r = my_hypot(A[i][i], A[j][i]); const number c = A[i][i] / r, s = A[j][i] / r; givens_rotate(b[i], b[j], c, s); repi(k, i, n) givens_rotate(A[i][k], A[j][k], c, s); } for (int i = n - 1; i >= 0; --i) { repi(j, i + 1, n) b[i] -= A[i][j] * b[j]; b[i] /= A[i][i]; } return b; } const double eps = 0.01; int d; vector<number> v; vector<number> solve_eq(int m, int n) { matrix A(d + 1, vector<number>(d + 1)); vector<number> b(d + 1); int cnt = 0; rep(i, d + 3) if (i != m && i != n) { b[cnt] = v[i]; rep(j, d + 1) A[cnt][j] = pow(1.0 * i, 1.0 * j); cnt++; } return givens(A, b); } int main() { while (cin >> d, d) { v.resize(d + 3); int ans = -1; rep(i, d + 3) cin >> v[i]; rep(i, d + 3) { repi(j, i + 1, d + 3) { vector<number> b = solve_eq(i, j); number e = 0, f = 0; rep(k, d + 1) e += pow(1.0 * i, 1.0 * k) * b[k]; rep(k, d + 1) f += pow(1.0 * j, 1.0 * k) * b[k]; if (abs(e - v[i]) > eps && abs(f - v[j]) < eps) { ans = i; break; } if (abs(e - v[i]) < eps && abs(f - v[j]) > eps) { ans = j; break; } } if (ans >= 0) break; } cout << ans << endl; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13]]
1
757
#include <bits/stdc++.h> using namespace std; double calc(vector<double> x, vector<double> y, double X) { int n = x.size(); double answer = 0; for (int i = 0; i < n; i++) { double sub = y[i]; for (int j = 0; j < n; j++) { if (i != j) { sub *= (X - x[j]); sub /= (x[i] - x[j]); } } answer += sub; } return answer; } int main() { int d; while (cin >> d && d) { vector<double> v(d + 3); for (int i = 0; i < v.size(); i++) cin >> v[i]; vector<int> vote(v.size(), 0); for (int i = 0; i < v.size(); i++) { vector<double> x, y; for (int j = 0; j < v.size(); j++) { if (i != j) { x.push_back(j); y.push_back(v[j]); } } x.resize(d + 1); y.resize(d + 1); int ok = 1; for (int j = 0; j < v.size(); j++) { if (i != j && abs(calc(x, y, j) - v[j]) > 1) ok = 0; } if (ok) { cout << i << endl; break; } } } }
#include <bits/stdc++.h> using namespace std; double calc(vector<double> x, vector<double> y, double X) { int n = x.size(); double answer = 0; for (int i = 0; i < n; i++) { double sub = y[i]; for (int j = 0; j < n; j++) { if (i != j) { sub *= (X - x[j]); sub /= (x[i] - x[j]); } } answer += sub; } return answer; } int main() { int d; while (cin >> d && d) { vector<double> v(d + 3); for (int i = 0; i < v.size(); i++) cin >> v[i]; vector<int> vote(v.size(), 0); for (int i = 0; i < v.size(); i++) { vector<double> x, y; for (int j = 0; j < v.size(); j++) { if (i != j) { x.push_back(j); y.push_back(v[j]); } } x.resize(d + 1); y.resize(d + 1); int ok = 1; for (int j = 0; j < v.size(); j++) { if (i != j && abs(calc(x, y, j) - v[j]) > 0.5) ok = 0; } if (ok) { cout << i << endl; break; } } } }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
1
349
#include <cmath> #include <iostream> using namespace std; int d; double pt[10]; bool lagrange(int x, int o) { int i, j; double p; double sum = 0; for (i = 0; i < d + 3; i++) { if (i != x && i != o) { p = pt[i]; for (j = 0; j < d + 3; j++) { if ((j != i && j != x) && j != o) { p *= (x - j) / double(i - j); } } sum += p; } } if (abs(sum - pt[x]) < 0.1) return true; else return false; } bool outlier(int i) { for (int j = 0; j < d + 3; j++) { if (i == j) continue; if (!lagrange(j, i)) { return false; } } return true; } int main() { ios_base::sync_with_stdio(0); int i; while (true) { cin >> d; if (d == 0) break; for (i = 0; i < d + 3; i++) { cin >> pt[i]; } for (i = 0; i < d + 3; i++) { if (outlier(i)) { cout << i; break; } } } return 0; }
#include <cmath> #include <iostream> using namespace std; int d; double pt[10]; bool lagrange(int x, int o) { int i, j; double p; double sum = 0; for (i = 0; i < d + 3; i++) { if (i != x && i != o) { p = pt[i]; for (j = 0; j < d + 3; j++) { if ((j != i && j != x) && j != o) { p *= (x - j) / double(i - j); } } sum += p; } } if (abs(sum - pt[x]) < 0.1) return true; else return false; } bool outlier(int i) { for (int j = 0; j < d + 3; j++) { if (i == j) continue; if (!lagrange(j, i)) { return false; } } return true; } int main() { ios_base::sync_with_stdio(0); int i; while (true) { cin >> d; if (d == 0) break; for (i = 0; i < d + 3; i++) { cin >> pt[i]; } for (i = 0; i < d + 3; i++) { if (outlier(i)) { cout << i << endl; break; } } } return 0; }
[["+", 0, 57, 64, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 57, 64, 9, 0, 1, 0, 16, 12, 22]]
1
297
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define fo(i, n) for (int i = 0; i < (int)n; i++) #define rep(it, s) \ for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) #define mp(a, b) make_pair(a, b) #define pb(x) push_back(x) #define pii pair<int, int> typedef vector<double> vec; typedef vector<vec> mat; const double eps = 1.0 - 8; int d; double x[8]; int m; vec gauss_jordan(const mat &A, const vec &v) { int n = A.size(); mat B(n, vec(n + 1)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) B[i][j] = A[i][j]; for (int i = 0; i < n; i++) B[i][n] = v[i]; for (int i = 0; i < n; i++) { int piv = -1; double tmp = -1; for (int j = i; j < n; j++) { if (tmp < abs(B[j][i])) { piv = j; tmp = abs(B[j][i]); } } if (piv == -1 || tmp < eps) return vec(); swap(B[piv], B[i]); double divNum = B[i][i]; for (int j = i; j <= n; j++) B[i][j] /= divNum; for (int j = 0; j < n; j++) { if (i == j) continue; double mulNum = B[j][i]; for (int k = i; k <= n; k++) B[j][k] -= mulNum * B[i][k]; } } vec res(n); for (int i = 0; i < n; i++) { res[i] = B[i][n]; } return res; } int solve() { for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { mat A(d + 1, vec(d + 1, 0.0)); vector<int> v; for (int k = 0; k < m; k++) { if (k == i || k == j) continue; v.push_back(k); } for (int k = 0; k < v.size(); k++) { int cur = 1; for (int r = d; r >= 0; r--) { A[k][r] = cur; cur *= v[k]; } } vec c(d + 1, 0); for (int k = 0; k < d + 1; k++) { c[k] = x[v[k]]; } vec ans = gauss_jordan(A, c); if (ans.size() > 0) { double xx = 1; double y = 0.0; bool found = 1; for (int l = d; l >= 0; l--) { y += ans[l] * xx; xx *= i; } if (abs(y - x[i]) < eps) { found = 1; } xx = 1; y = 0.0; for (int l = d; l >= 0; l--) { y += ans[l] * xx; xx *= j; } if (abs(y - x[j]) < eps) { if (found) continue; if (!found) return i; } else { if (found) return j; } } } } return -1; } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); while (cin >> d && d) { m = d + 3; for (int i = 0; i < m; i++) cin >> x[i]; cout << solve() << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define fo(i, n) for (int i = 0; i < (int)n; i++) #define rep(it, s) \ for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) #define mp(a, b) make_pair(a, b) #define pb(x) push_back(x) #define pii pair<int, int> typedef vector<double> vec; typedef vector<vec> mat; const double eps = 1.0e-5; int d; double x[8]; int m; vec gauss_jordan(const mat &A, const vec &v) { int n = A.size(); mat B(n, vec(n + 1)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) B[i][j] = A[i][j]; for (int i = 0; i < n; i++) B[i][n] = v[i]; for (int i = 0; i < n; i++) { int piv = -1; double tmp = -1; for (int j = i; j < n; j++) { if (tmp < abs(B[j][i])) { piv = j; tmp = abs(B[j][i]); } } if (piv == -1 || tmp < eps) return vec(); swap(B[piv], B[i]); double divNum = B[i][i]; for (int j = i; j <= n; j++) B[i][j] /= divNum; for (int j = 0; j < n; j++) { if (i == j) continue; double mulNum = B[j][i]; for (int k = i; k <= n; k++) B[j][k] -= mulNum * B[i][k]; } } vec res(n); for (int i = 0; i < n; i++) { res[i] = B[i][n]; } return res; } int solve() { for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { mat A(d + 1, vec(d + 1, 0.0)); vector<int> v; for (int k = 0; k < m; k++) { if (k == i || k == j) continue; v.push_back(k); } for (int k = 0; k < v.size(); k++) { double cur = 1; for (int r = d; r >= 0; r--) { A[k][r] = cur; cur *= v[k]; } } vec c(d + 1, 0); for (int k = 0; k < d + 1; k++) { c[k] = x[v[k]]; } vec ans = gauss_jordan(A, c); if (ans.size() > 0) { double xx = 1; double y = 0.0; bool found = 0; for (int l = d; l >= 0; l--) { y += ans[l] * xx; xx *= i; } if (abs(y - x[i]) < eps) { found = 1; } xx = 1; y = 0.0; for (int l = d; l >= 0; l--) { y += ans[l] * xx; xx *= j; } if (abs(y - x[j]) < eps) { if (found) continue; if (!found) return i; } else { if (found) return j; } } } } return -1; } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); while (cin >> d && d) { m = d + 3; for (int i = 0; i < m; i++) cin >> x[i]; cout << solve() << endl; } return 0; }
[["-", 0, 30, 0, 43, 49, 50, 51, 16, 31, 13], ["-", 0, 30, 0, 43, 49, 50, 51, 16, 17, 33], ["-", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 7, 8, 9, 0, 43, 39, 40], ["+", 8, 9, 0, 7, 8, 9, 0, 43, 39, 40], ["-", 0, 57, 64, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 57, 64, 9, 0, 43, 49, 50, 51, 13]]
1
873
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; int dy[] = {-1, 0, 1, 0}; int dx[] = {0, 1, 0, -1}; int ey[] = {-1, -1, 0, 1, 2, 2, 1, 0}; int ex[] = {0, 1, 2, 2, 1, 0, -1, -1}; int H, W; char t[50][50]; int sy, sx; int py0, px0, py1, px1; int d[50][50]; int calc(int gy, int gx, int sy, int sx, int ty, int tx, bool flg = false) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) d[i][j] = 1e8; d[sy][sx] = 0; queue<int> qy, qx; qy.push(sy); qx.push(sx); while (!qy.empty()) { int y = qy.front(); qy.pop(); int x = qx.front(); qx.pop(); if (y == ty && x == tx) break; for (int i = 0; i < 4; i++) { int ny = y + dy[i]; int nx = x + dx[i]; if (ny < 0 || nx < 0) continue; if (ny >= H || nx >= W) continue; if (t[ny][nx] == '*') continue; if (gy <= ny && ny < gy + 2 && gx <= nx && nx < gx + 2) continue; if (d[ny][nx] > d[y][x] + 1) { d[ny][nx] = d[y][x] + 1; qy.push(ny); qx.push(nx); } } } return d[ty][tx]; } int dd[50][50][4]; int solve(int sy, int sx, int sd) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) for (int k = 0; k < 4; k++) dd[i][j][k] = 1e8; dd[sy][sx][sd] = 0; priority_queue<PP, vector<PP>, greater<PP>> Q; Q.push(PP(0, P(sy * 50 + sx, sd))); while (!Q.empty()) { PP pp = Q.top(); Q.pop(); int y = pp.second.first / 50; int x = pp.second.first % 50; int dir = pp.second.second; if (y == 0 && x == 0) return dd[y][x][dir]; if (pp.first > dd[y][x][dir]) continue; for (int i = 0; i < 4; i++) { int ncost = dd[y][x][dir]; int ay = y + ey[i * 2], ax = x + ex[i * 2]; int by = y + ey[i * 2 + 1], bx = x + ex[i * 2 + 1]; int flg = (dir + 2) % 4; int Ay = y + ey[flg * 2], Ax = x + ex[flg * 2]; int By = y + ey[flg * 2 + 1], Bx = x + ex[flg * 2 + 1]; int ny = y + dy[i], nx = x + dx[i]; if (ay < 0 || ax < 0 || ay >= H || ax >= W) continue; if (by < 0 || bx < 0 || by >= H || bx >= W) continue; if (t[ay][ax] == '*' || t[by][bx] == '*') continue; ncost += min(calc(y, x, ay, ax, Ay, Ax) + calc(y, x, by, bx, By, Bx), calc(y, x, ay, ax, By, Bx) + calc(y, x, by, bx, Ay, Ax)); ncost++; if (dd[ny][nx][i] > ncost) { dd[ny][nx][i] = ncost; Q.push(PP(ncost, P(ny * 50 + nx, i))); } } } return 1e8; } int main() { while (1) { cin >> H >> W; if (H == 0 && W == 0) break; sy = sx = -1; py0 = px0 = -1; py1 = px1 = -1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> t[i][j]; if (t[i][j] == 'X' && sy == -1 && sx == -1) { sy = i; sx = j; } if (t[i][j] == '.') { py0 = py1; px0 = px1; py1 = i; px1 = j; } } } int ans = 1e9; if (sy == 0 && sx == 0) ans = 0; for (int i = 0; i < 4; i++) { int ay = sy + ey[i * 2], ax = sx + ex[i * 2]; int by = sy + ey[i * 2 + 1], bx = sx + ex[i * 2 + 1]; if (ay < 0 || ax < 0 || by < 0 || bx < 0) continue; if (ay >= H || ax >= W || by >= H || bx >= W) continue; if (t[ay][ax] == '*' || t[by][bx] == '*') continue; int cost = min(calc(sy, sx, py0, px0, ay, ax) + calc(sy, sx, py1, px1, by, bx), calc(sy, sx, py0, px0, by, bx) + calc(sy, sx, py1, px1, ay, ax)); ans = min(ans, cost + solve(sy, sx, i)); } if (ans >= 1e8) ans = -1; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; int dy[] = {-1, 0, 1, 0}; int dx[] = {0, 1, 0, -1}; int ey[] = {-1, -1, 0, 1, 2, 2, 1, 0}; int ex[] = {0, 1, 2, 2, 1, 0, -1, -1}; int H, W; char t[50][50]; int sy, sx; int py0, px0, py1, px1; int d[50][50]; int calc(int gy, int gx, int sy, int sx, int ty, int tx, bool flg = false) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) d[i][j] = 1e8; d[sy][sx] = 0; queue<int> qy, qx; qy.push(sy); qx.push(sx); while (!qy.empty()) { int y = qy.front(); qy.pop(); int x = qx.front(); qx.pop(); if (y == ty && x == tx) break; for (int i = 0; i < 4; i++) { int ny = y + dy[i]; int nx = x + dx[i]; if (ny < 0 || nx < 0) continue; if (ny >= H || nx >= W) continue; if (t[ny][nx] == '*') continue; if (gy <= ny && ny < gy + 2 && gx <= nx && nx < gx + 2) continue; if (d[ny][nx] > d[y][x] + 1) { d[ny][nx] = d[y][x] + 1; qy.push(ny); qx.push(nx); } } } return d[ty][tx]; } int dd[50][50][4]; int solve(int sy, int sx, int sd) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) for (int k = 0; k < 4; k++) dd[i][j][k] = 1e8; dd[sy][sx][sd] = 0; priority_queue<PP, vector<PP>, greater<PP>> Q; Q.push(PP(0, P(sy * 50 + sx, sd))); while (!Q.empty()) { PP pp = Q.top(); Q.pop(); int y = pp.second.first / 50; int x = pp.second.first % 50; int dir = pp.second.second; if (y == 0 && x == 0) return dd[y][x][dir]; if (pp.first > dd[y][x][dir]) continue; for (int i = 0; i < 4; i++) { int ncost = dd[y][x][dir]; int ay = y + ey[i * 2], ax = x + ex[i * 2]; int by = y + ey[i * 2 + 1], bx = x + ex[i * 2 + 1]; int flg = (dir + 2) % 4; int Ay = y + ey[flg * 2], Ax = x + ex[flg * 2]; int By = y + ey[flg * 2 + 1], Bx = x + ex[flg * 2 + 1]; int ny = y + dy[i], nx = x + dx[i]; if (ay < 0 || ax < 0 || ay >= H || ax >= W) continue; if (by < 0 || bx < 0 || by >= H || bx >= W) continue; if (t[ay][ax] == '*' || t[by][bx] == '*') continue; ncost += min(calc(y, x, ay, ax, Ay, Ax) + calc(y, x, by, bx, By, Bx), calc(y, x, ay, ax, By, Bx) + calc(y, x, by, bx, Ay, Ax)); ncost++; if (dd[ny][nx][i] > ncost) { dd[ny][nx][i] = ncost; Q.push(PP(ncost, P(ny * 50 + nx, i))); } } } return 1e8; } int main() { while (1) { cin >> H >> W; if (H == 0 && W == 0) break; sy = sx = -1; py0 = px0 = -1; py1 = px1 = -1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> t[i][j]; if (t[i][j] == 'X' && sy == -1 && sx == -1) { sy = i; sx = j; } if (t[i][j] == '.') { py0 = py1; px0 = px1; py1 = i; px1 = j; } } } int ans = 1e9; if (sy == 0 && sx == 0) ans = 0; for (int i = 0; i < 4; i++) { int ay = sy + ey[i * 2], ax = sx + ex[i * 2]; int by = sy + ey[i * 2 + 1], bx = sx + ex[i * 2 + 1]; if (ay < 0 || ax < 0 || by < 0 || bx < 0) continue; if (ay >= H || ax >= W || by >= H || bx >= W) continue; if (t[ay][ax] == '*' || t[by][bx] == '*') continue; int cost = min(calc(sy, sx, py0, px0, ay, ax) + calc(sy, sx, py1, px1, by, bx), calc(sy, sx, py0, px0, by, bx) + calc(sy, sx, py1, px1, ay, ax)); ans = min(ans, cost + solve(sy, sx, (i + 2) % 4)); } if (ans >= 1e8) ans = -1; cout << ans << endl; } return 0; }
[["+", 12, 2, 3, 4, 0, 16, 31, 23, 0, 24], ["+", 3, 4, 0, 16, 31, 23, 0, 16, 17, 72], ["+", 3, 4, 0, 16, 31, 23, 0, 16, 12, 13], ["+", 12, 2, 3, 4, 0, 16, 31, 23, 0, 25], ["+", 0, 16, 12, 2, 3, 4, 0, 16, 17, 109], ["+", 0, 16, 12, 2, 3, 4, 0, 16, 12, 13]]
1
1,440
#include <bits/stdc++.h> using namespace std; using Int = long long; // BEGIN CUT HERE template <typename T> struct WeightedUnionFind { int n; T d; vector<int> r, p; vector<T> ws; WeightedUnionFind() {} WeightedUnionFind(int sz, T d_) : n(sz), d(d_), r(n, 1), p(n), ws(n, d) { iota(p.begin(), p.end(), 0); } int find(int x) { if (x == p[x]) { return x; } else { int t = find(p[x]); ws[x] += ws[p[x]]; return p[x] = t; } } T weight(int x) { find(x); return ws[x]; } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y, T w) { w += weight(x); w -= weight(y); x = find(x); y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y), w = -w; r[x] += r[y]; p[y] = x; ws[y] = w; } T diff(int x, int y) { return weight(x) - weight(y); } }; // END CUT HERE signed main() { int n, m; while (cin >> n >> m, n || m) { WeightedUnionFind<Int> u(n, 0); for (int i = 0; i < m; i++) { int a, b; char c; cin >> c >> a >> b; a--; b--; if (c == '!') { Int w; cin >> w; u.unite(a, b, w); } else { if (!u.same(a, b)) cout << "UNKNOWN" << endl; else cout << u.diff(a, b) << endl; } } } return 0; } /* verified on 2017/10/29 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1330 */
#include <bits/stdc++.h> using namespace std; using Int = long long; // BEGIN CUT HERE template <typename T> struct WeightedUnionFind { int n; T d; vector<int> r, p; vector<T> ws; WeightedUnionFind() {} WeightedUnionFind(int sz, T d_) : n(sz), d(d_), r(n, 1), p(n), ws(n, d) { iota(p.begin(), p.end(), 0); } int find(int x) { if (x == p[x]) { return x; } else { int t = find(p[x]); ws[x] += ws[p[x]]; return p[x] = t; } } T weight(int x) { find(x); return ws[x]; } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y, T w) { w += weight(x); w -= weight(y); x = find(x); y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y), w = -w; r[x] += r[y]; p[y] = x; ws[y] = w; } T diff(int x, int y) { return weight(y) - weight(x); } }; // END CUT HERE signed main() { int n, m; while (cin >> n >> m, n || m) { WeightedUnionFind<Int> u(n, 0); for (int i = 0; i < m; i++) { int a, b; char c; cin >> c >> a >> b; a--; b--; if (c == '!') { Int w; cin >> w; u.unite(a, b, w); } else { if (!u.same(a, b)) cout << "UNKNOWN" << endl; else cout << u.diff(a, b) << endl; } } } return 0; } /* verified on 2017/10/29 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1330 */
[["-", 0, 37, 0, 16, 31, 2, 3, 4, 0, 22], ["+", 0, 37, 0, 16, 31, 2, 3, 4, 0, 22], ["-", 0, 37, 0, 16, 12, 2, 3, 4, 0, 22], ["+", 0, 37, 0, 16, 12, 2, 3, 4, 0, 22]]
1
466
#include <iostream> #include <map> #include <string> #include <vector> using namespace std; string s; int p; int T; int number() { int res = s[p] - '0'; p++; return res; } int formula() { int res = number(); for (;;) { if (s[p] == '*') { p++; res *= number(); } else { break; } } return res; } int expr() { int res = number(); for (;;) { if (s[p] == '+') { p++; res += formula(); } else { break; } } return res; } int expr2() { int res = number(); for (;;) { if (s[p] == '+') { p++; res += number(); } else if (s[p] == '*') { p++; res *= number(); } else { break; } } return res; } int main() { cin >> s >> T; bool l1, l2; p = 0; l1 = expr() == T; p = 0; l2 = expr2() == T; if (l1 && l2) cout << "U" << endl; if (l1 && !l2) cout << "M" << endl; if (!l1 && l2) cout << "L" << endl; if (!l1 && !l2) cout << "I" << endl; }
#include <iostream> #include <map> #include <string> #include <vector> using namespace std; string s; int p; int T; int number() { int res = s[p] - '0'; p++; return res; } int formula() { int res = number(); for (;;) { if (s[p] == '*') { p++; res *= number(); } else { break; } } return res; } int expr() { int res = formula(); for (;;) { if (s[p] == '+') { p++; res += formula(); } else { break; } } return res; } int expr2() { int res = number(); for (;;) { if (s[p] == '+') { p++; res += number(); } else if (s[p] == '*') { p++; res *= number(); } else { break; } } return res; } int main() { cin >> s >> T; bool l1, l2; p = 0; l1 = expr() == T; p = 0; l2 = expr2() == T; if (l1 && l2) cout << "U" << endl; if (l1 && !l2) cout << "M" << endl; if (!l1 && l2) cout << "L" << endl; if (!l1 && !l2) cout << "I" << endl; }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
319
#include <bits/stdc++.h> using namespace std; const int INF = 999; //世界一競技プログラミングができない人のソース2 #define int long long int get_number(string str, int &idx) { int ret = 0; while (isdigit(str[idx])) { ret = ret * 10 + str[idx] - '0'; ++idx; } return ret; } int Expression(string str, int &idx) { int ret = get_number(str, idx); while (true) { switch (str[idx]) { case '*': ++idx; ret *= get_number(str, idx); break; default: return ret; } } } int nomal(string str, int idx = 0) { int res = Expression(str, idx); while (true) { switch (str[idx]) { case '+': ++idx; res += Expression(str, idx); break; default: return res; } } } int special(string str, int idx = 0) { int ret = get_number(str, idx); while (idx < str.size() - 1) { if (str[idx] == '+') ret += get_number(str, ++idx); if (str[idx] == '*') ret *= get_number(str, ++idx); else break; } return ret; } signed main() { string str; cin >> str; str += "~"; int digit; cin >> digit; bool judge_a = nomal(str) == digit; bool judge_b = special(str) == digit; if (judge_a & judge_b) { cout << "U" << endl; } else if (judge_a) { cout << "M" << endl; } else if (judge_b) { cout << "L" << endl; } else { cout << "I" << endl; } }
#include <bits/stdc++.h> using namespace std; const int INF = 999; //世界一競技プログラミングができない人のソース2 #define int long long int get_number(string str, int &idx) { int ret = 0; while (isdigit(str[idx])) { ret = ret * 10 + str[idx] - '0'; ++idx; } return ret; } int Expression(string str, int &idx) { int ret = get_number(str, idx); while (true) { switch (str[idx]) { case '*': ++idx; ret *= get_number(str, idx); break; default: return ret; } } } int nomal(string str, int idx = 0) { int res = Expression(str, idx); while (true) { switch (str[idx]) { case '+': ++idx; res += Expression(str, idx); break; default: return res; } } } int special(string str, int idx = 0) { int ret = get_number(str, idx); while (idx < str.size() - 1) { if (str[idx] == '+') ret += get_number(str, ++idx); else if (str[idx] == '*') ret *= get_number(str, ++idx); else break; } return ret; } signed main() { string str; cin >> str; str += "~"; int digit; cin >> digit; bool judge_a = nomal(str) == digit; bool judge_b = special(str) == digit; if (judge_a & judge_b) { cout << "U" << endl; } else if (judge_a) { cout << "M" << endl; } else if (judge_b) { cout << "L" << endl; } else { cout << "I" << endl; } }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
377
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <sstream> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; typedef vector<ll> vl; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<string> vs; int main() { //入力 string s; cin >> s; ll ans; cin >> ans; //まずはかけ算 ll ans1(0); int tmp(1); if (s.length() == 1) { ans1 = s[0] - '0'; } else { for (int i = 0; i < (s.length()) / 2; i++) { if (i == 0) { if (s[i * 2 + 1] == '+') { ans1 += s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { ans1 += s[i * 2 + 2] - '0'; } } else { tmp *= s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { tmp *= s[i * 2 + 2] - '0'; ans1 += tmp; } } } else { if (s[i * 2 + 1] == '+') { if (s[i * 2 - 1] == '*') { tmp *= s[i * 2] - '0'; ans1 += tmp; tmp = 1; if (i == (-3 + s.length()) / 2) { ans1 += s[i * 2 + 2] - '0'; } } else { ans1 += s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { ans1 += s[i * 2 + 2] - '0'; } } } else { tmp *= s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { tmp *= s[i * 2 + 2] - '0'; ans1 += tmp; } } } } } //足し算 ll ans2(0); if (s.length() == 1) { ans2 += s[0] - '0'; } else { for (int i = 0; i < (s.length()) / 2; i++) { if (i == 0) { if (s[i * 2 + 1] == '+') { ans2 += s[i * 2] - '0' + s[i * 2 + 2] - '0'; } else { ans2 += s[i * 2] - '0' + s[i * 2 + 2] - '0'; } } else { if (s[i * 2 + 1] == '+') { ans2 += s[i * 2 + 2] - '0'; } else { ans2 *= s[i * 2 + 2] - '0'; } } } } bool m = (ans == ans1); bool l = (ans == ans2); if (m && !l) { cout << 'M' << endl; } if (!m && l) { cout << 'L' << endl; } if (m && l) { cout << 'U' << endl; } if (!m && !l) { cout << 'I' << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <sstream> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; typedef vector<ll> vl; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<string> vs; int main() { //入力 string s; cin >> s; ll ans; cin >> ans; //まずはかけ算 ll ans1(0); int tmp(1); if (s.length() == 1) { ans1 = s[0] - '0'; } else { for (int i = 0; i < (s.length()) / 2; i++) { if (i == 0) { if (s[i * 2 + 1] == '+') { ans1 += s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { ans1 += s[i * 2 + 2] - '0'; } } else { tmp *= s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { tmp *= s[i * 2 + 2] - '0'; ans1 += tmp; } } } else { if (s[i * 2 + 1] == '+') { if (s[i * 2 - 1] == '*') { tmp *= s[i * 2] - '0'; ans1 += tmp; tmp = 1; if (i == (-3 + s.length()) / 2) { ans1 += s[i * 2 + 2] - '0'; } } else { ans1 += s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { ans1 += s[i * 2 + 2] - '0'; } } } else { tmp *= s[i * 2] - '0'; if (i == (-3 + s.length()) / 2) { tmp *= s[i * 2 + 2] - '0'; ans1 += tmp; } } } } } //足し算 ll ans2(0); if (s.length() == 1) { ans2 += s[0] - '0'; } else { for (int i = 0; i < (s.length()) / 2; i++) { if (i == 0) { if (s[i * 2 + 1] == '+') { ans2 += s[i * 2] - '0' + s[i * 2 + 2] - '0'; // cout<<"-1-"<<" "<<ans2<<endl; } else { ans2 += (s[i * 2] - '0') * (s[i * 2 + 2] - '0'); // cout<<"-2-"<<" "<<ans2<<endl; } } else { if (s[i * 2 + 1] == '+') { ans2 += s[i * 2 + 2] - '0'; // cout<<"-3-"<<" "<<ans2<<endl; } else { ans2 *= s[i * 2 + 2] - '0'; // cout<<"-4-"<<" "<<ans2<<endl; } } } } bool m = (ans == ans1); bool l = (ans == ans2); if (m && !l) { cout << 'M' << endl; } if (!m && l) { cout << 'L' << endl; } if (m && l) { cout << 'U' << endl; } if (!m && !l) { cout << 'I' << endl; } return 0; }
[["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 24], ["-", 0, 1, 0, 11, 12, 16, 31, 16, 17, 72], ["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 25], ["+", 0, 9, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 25]]
1
745
#include <bits/stdc++.h> using namespace std; #define int long long template <class X> class Solve { public: string s; int p; Solve(string s_) { s = s_; s += "~"; p = 0; } X one() { X r = num(); while (s[p] == '+' || s[p] == '*') { char c = s[p]; p++; X l = num(); if (c == '*') { r *= l; } else { r += l; } } return r; } X num() { X ans = 0; while (s[p] >= '0' && s[p] <= '9') { ans = ans * 10 + s[p] - '0'; p++; } return ans; } X two() { X r = num(); while (s[p] == '+') { char c = s[p]; p++; X l = three(); r += l; } return r; } X three() { X r = num(); while (s[p] == '*') { char c = s[p]; p++; X l = num(); r *= l; } return r; } }; signed main() { string s; int n; cin >> s >> n; int b = 0; if (fabs(Solve<double>(s).one() - n) < 1e5) { if (Solve<int>(s).one() - n == 0) { b += 1; } } if (fabs(Solve<double>(s).two() - n) < 1e5) { if (Solve<int>(s).two() - n == 0) { b += 2; } } string table = "ILMU"; cout << table[b] << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long template <class X> class Solve { public: string s; int p; Solve(string s_) { s = s_; s += "~"; p = 0; } X one() { X r = num(); while (s[p] == '+' || s[p] == '*') { char c = s[p]; p++; X l = num(); if (c == '*') { r *= l; } else { r += l; } } return r; } X num() { X ans = 0; while (s[p] >= '0' && s[p] <= '9') { ans = ans * 10 + s[p] - '0'; p++; } return ans; } X two() { X r = three(); while (s[p] == '+') { char c = s[p]; p++; X l = three(); r += l; } return r; } X three() { X r = num(); while (s[p] == '*') { char c = s[p]; p++; X l = num(); r *= l; } return r; } }; signed main() { string s; int n; cin >> s >> n; int b = 0; if (fabs(Solve<double>(s).one() - n) < 1e5) { if (Solve<int>(s).one() - n == 0) { b += 1; } } if (fabs(Solve<double>(s).two() - n) < 1e5) { if (Solve<int>(s).two() - n == 0) { b += 2; } } string table = "ILMU"; cout << table[b] << endl; }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
414
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #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 toStr(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 FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-10; const double PI = acos(-1.0); const int INF = INT_MAX / 10; string s; int p; int term(); int num(); int expression() { int ret = num(); while (s[p] == '+') { p++; ret += term(); } return ret; } int term() { int ret = num(); while (s[p] == '*') { p++; ret *= num(); } return ret; } int num() { int ret = 0; while (isdigit(s[p])) { ret *= 10; ret += (s[p] - '0'); p++; } return ret; } int expression2() { int ret = num(); while (true) { if (s[p] == '+') { p++; ret += num(); } else if (s[p] == '*') { p++; ret *= num(); } else { break; } } return ret; } int main() { int n; cin >> s >> n; p = 0; int ans1 = expression(); p = 0; int ans2 = expression2(); if (n == ans1) { if (n == ans2) { cout << 'U' << endl; } else { cout << 'M' << endl; } } else if (n == ans2) { cout << 'L' << endl; } else { cout << 'I' << endl; } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #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 toStr(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 FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-10; const double PI = acos(-1.0); const int INF = INT_MAX / 10; string s; int p; int term(); int num(); int expression() { int ret = term(); while (s[p] == '+') { p++; ret += term(); } return ret; } int term() { int ret = num(); while (s[p] == '*') { p++; ret *= num(); } return ret; } int num() { int ret = 0; while (isdigit(s[p])) { ret *= 10; ret += (s[p] - '0'); p++; } return ret; } int expression2() { int ret = num(); while (true) { if (s[p] == '+') { p++; ret += num(); } else if (s[p] == '*') { p++; ret *= num(); } else { break; } } return ret; } int main() { int n; cin >> s >> n; p = 0; int ans1 = expression(); p = 0; int ans2 = expression2(); if (n == ans1) { if (n == ans2) { cout << 'U' << endl; } else { cout << 'M' << endl; } } else if (n == ans2) { cout << 'L' << endl; } else { cout << 'I' << endl; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
496
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> s >> n; int a = s[0] - '0', c = s[0] - '0', b = 0; for (int i = 1; i < s.size(); i += 2) { int p = s[i + 1] - '0'; if (s[i] == '+') a += p, b += c, c = p; else a *= p, c *= p; } b += c; if (b == n && a != n) cout << 'M'; else if (a == n && b != n) cout << 'L'; else if (a == b) cout << 'U'; else cout << 'I'; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> s >> n; int a = s[0] - '0', c = s[0] - '0', b = 0; for (int i = 1; i < s.size(); i += 2) { int p = s[i + 1] - '0'; if (s[i] == '+') a += p, b += c, c = p; else a *= p, c *= p; } b += c; if (b == n && a != n) cout << 'M'; else if (a == n && b != n) cout << 'L'; else if (a == b && a == n) cout << 'U'; else cout << 'I'; cout << endl; return 0; }
[["+", 75, 76, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 60], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 22]]
1
182
#include <iostream> #include <string> using namespace std; int ctoi(char c) { int i; char ch = '1'; for (i = 1; i < 10; i++) { if (c == ch) return i; else ch++; } return -1; } int main() { string s; cin >> s; int ans1 = 0, ans2, anss = 0; for (int i = 0; i < s.size(); i += 2) { if (i == 0) { anss = ctoi(s[i]); ans2 = ctoi(s[i]); } else if (i % 2 == 0) { if (s[i - 1] == '+') { ans1 += anss; anss = ctoi(s[i]); ans2 += ctoi(s[i]); } else { anss *= ctoi(s[i]); ans2 *= ctoi(s[i]); } } } ans1 += anss; int v; cin >> v; if (ans1 == v) { if (ans2 == v) cout << "U\n"; else cout << "M\n"; } else { if (ans2 == v) cout << "L\n"; else cout << "I\n"; } }
#include <iostream> #include <string> using namespace std; int ctoi(char c) { int i; char ch = '0'; for (i = 0; i < 10; i++) { if (c == ch) return i; else ch++; } return -1; } int main() { string s; cin >> s; int ans1 = 0, ans2, anss = 0; for (int i = 0; i < s.size(); i += 2) { if (i == 0) { anss = ctoi(s[i]); ans2 = ctoi(s[i]); } else if (i % 2 == 0) { if (s[i - 1] == '+') { ans1 += anss; anss = ctoi(s[i]); ans2 += ctoi(s[i]); } else { anss *= ctoi(s[i]); ans2 *= ctoi(s[i]); } } } ans1 += anss; int v; cin >> v; if (ans1 == v) { if (ans2 == v) cout << "U\n"; else cout << "M\n"; } else { if (ans2 == v) cout << "L\n"; else cout << "I\n"; } }
[["-", 8, 9, 0, 43, 49, 50, 51, 103, 0, 125], ["+", 8, 9, 0, 43, 49, 50, 51, 103, 0, 125], ["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13]]
1
267
#include "bits/stdc++.h" using namespace std; int main() { string s; cin >> s; int n; cin >> n; int pu = 0; int ka = 0; vector<int> x(17, -1); vector<int> f(17, -1); int b = 0; int c = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '+') { pu++; f[c] = 0; b++; c++; } else if (s[i] == '*') { ka++; f[c] = 1; b++; c++; } else { if (x[b] == -1) x[b] = 0; x[b] *= 10; x[b] += s[i] - '0'; } } int lans = x[0]; for (int i = 1; x[i] != -1; i++) { if (f[i - 1] == 0) lans += x[i]; if (f[i - 1] == 1) lans *= x[i]; } // cout<<lans<<endl; b = 0; c = 0; int tmp = 1; vector<int> tas; // tmp=x[0]; for (int i = 0; x[i] != -1; i++) { if (f[c] == 0) { tmp *= x[i]; tas.push_back(tmp); // cout<<tmp<<"aaa"<<endl; tmp = 1; } else { tmp *= x[i]; } b++; c++; } tas.push_back(tmp); int mans = 0; for (int i = 0; i < tas.size(); i++) { mans += tas[i]; // cout<<tas[i]<<endl; } // cout<<lans<<" "<<mans<<endl; if (n != lans && n != mans) { cout << "I" << endl; return 0; } if (pu < 2) { cout << "U" << endl; return 0; } if (n == mans) { cout << "M" << endl; return 0; } if (n == lans) { cout << "L" << endl; return 0; } return 0; }
#include "bits/stdc++.h" using namespace std; int main() { string s; cin >> s; int n; cin >> n; int pu = 0; int ka = 0; vector<int> x(17, -1); vector<int> f(17, -1); int b = 0; int c = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '+') { pu++; f[c] = 0; b++; c++; } else if (s[i] == '*') { ka++; f[c] = 1; b++; c++; } else { if (x[b] == -1) x[b] = 0; x[b] *= 10; x[b] += s[i] - '0'; } } int lans = x[0]; for (int i = 1; x[i] != -1; i++) { if (f[i - 1] == 0) lans += x[i]; if (f[i - 1] == 1) lans *= x[i]; } // cout<<lans<<endl; b = 0; c = 0; int tmp = 1; vector<int> tas; // tmp=x[0]; for (int i = 0; x[i] != -1; i++) { if (f[c] == 0) { tmp *= x[i]; tas.push_back(tmp); // cout<<tmp<<"aaa"<<endl; tmp = 1; } else { tmp *= x[i]; } b++; c++; } tas.push_back(tmp); int mans = 0; for (int i = 0; i < tas.size(); i++) { mans += tas[i]; // cout<<tas[i]<<endl; } // cout<<lans<<" "<<mans<<endl; if (n != lans && n != mans) { cout << "I" << endl; return 0; } if (lans == mans) { cout << "U" << endl; return 0; } if (n == mans) { cout << "M" << endl; return 0; } if (n == lans) { cout << "L" << endl; return 0; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22]]
1
462
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; #define all(a) a.begin(), a.end() int main() { int l = 0, n = 0, p; vector<int> a(1); string s; cin >> s >> p; l = a[0] = s[0] - '0'; for (int i = 2; i < s.size(); i += 2) { int t = s[i] - '0'; if (s[i - 1] == '*') { l *= t; a.back() *= t; } else { l += t; a.push_back(t); } } n = accumulate(all(a), 0); cout << "ILMU"[l == p ? 1 : 0 + n == p ? 2 : 0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; #define all(a) a.begin(), a.end() int main() { int l = 0, n = 0, p; vector<int> a(1); string s; cin >> s >> p; l = a[0] = s[0] - '0'; for (int i = 2; i < s.size(); i += 2) { int t = s[i] - '0'; if (s[i - 1] == '*') { l *= t; a.back() *= t; } else { l += t; a.push_back(t); } } n = accumulate(all(a), 0); cout << "ILMU"[(l == p ? 1 : 0) + (n == p ? 2 : 0)] << endl; return 0; }
[["+", 12, 69, 341, 342, 0, 16, 31, 23, 0, 24], ["+", 12, 69, 341, 342, 0, 16, 31, 23, 0, 25], ["+", 12, 69, 341, 342, 0, 16, 12, 23, 0, 24], ["+", 12, 69, 341, 342, 0, 16, 12, 23, 0, 25]]
1
185
e = gets.chomp a = gets.to_i x = eval e class Fixnum alias / - end y = eval(e.tr "+", "/") puts x == a && y == a ? ?U : x == a ? ?M : y == a ? ?L : ?I
e = gets.chomp a = gets.to_i x = eval e class Fixnum alias / + end y = eval(e.tr "+", "/") puts x == a && y == a ? ?U : x == a ? ?M : y == a ? ?L : ?I
[["-", 0, 269, 8, 736, 0, 705, 705, 17, 0, 33], ["+", 0, 269, 8, 736, 0, 705, 705, 17, 0, 72]]
4
59
s = input() a = int(input()) print("IMLU"[int(eval(s) == a)+int(eval("("*s.count("*")+s.replace("*",")*"))== a)<<1])
s = input() a = int(input()) print("IMLU"[int(eval(s) == a)+int(eval("("*s.count("*")+s.replace("*",")*")) == a)*2])
[["-", 0, 652, 3, 4, 0, 206, 206, 657, 17, 151], ["-", 0, 652, 3, 4, 0, 206, 206, 657, 12, 612], ["+", 3, 4, 0, 206, 206, 657, 12, 657, 17, 48], ["+", 3, 4, 0, 206, 206, 657, 12, 657, 12, 612]]
5
66
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; int main() { int N, m; cin >> N >> m; vector<int> backs(N, false); for (int i = 0; i < m; ++i) { int t, f; cin >> t >> f; t--; f--; for (int j = t; j < f; ++j) { backs[j] = true; } } int ans = 1 + N + accumulate(backs.begin(), backs.end(), 0); cout << ans << endl; return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; int main() { int N, m; cin >> N >> m; vector<int> backs(N, false); for (int i = 0; i < m; ++i) { int t, f; cin >> t >> f; t--; f--; for (int j = t; j < f; ++j) { backs[j] = true; } } int ans = 1 + N + 2 * accumulate(backs.begin(), backs.end(), 0); cout << ans << endl; return 0; }
[["+", 0, 43, 49, 50, 51, 16, 12, 16, 31, 13], ["+", 0, 43, 49, 50, 51, 16, 12, 16, 17, 48]]
1
153
#include <iostream> using namespace std; int n, q, a, b; int x[100000], sum; int main() { cin >> n >> q; for (int i = 0; i < q; i++) { cin >> a >> b; for (int j = a; j < b; j++) { x[j] = 1; } } for (int i = 0; i < n; i++) { sum += 1 + x[i] * 2; } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int n, q, a, b; int x[100000], sum; int main() { cin >> n >> q; for (int i = 0; i < q; i++) { cin >> a >> b; for (int j = a; j < b; j++) { x[j] = 1; } } for (int i = 0; i < n; i++) { sum += 1 + x[i] * 2; } cout << sum + 1 << endl; return 0; }
[["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
116
#include <bits/stdc++.h> using namespace std; typedef pair<int, string> State; const int v1[] = {1, 2, 3, 4, 5, 6, 7, 8, 0}; const int v2[] = {8, 0, 1, 2, 3, 4, 5, 6, 7}; const int v3[] = {6, 7, 8, 0, 1, 2, 3, 4, 5}; const int v4[] = {3, 4, 5, 6, 7, 8, 0, 1, 2}; int main() { int Horizon, Vertical; while (cin >> Horizon >> Vertical, Horizon) { string D = "", E = ""; for (int i = 0; i < 9; i++) { string K; cin >> K; } for (int i = 0; i < 9; i++) { string K; cin >> K; E += K; } priority_queue<State, vector<State>, greater<State>> que; que.push(State(0, D)); unordered_map<string, int> used; used[D] = 0; while (!que.empty()) { string q = que.top().second; int cost = que.top().first; que.pop(); if (q == E) { cout << cost << endl; break; } if (used.find(q) != used.end() && used[q] < cost) { continue; } int pos = q.find('0'), nextCost; nextCost = cost + Horizon; swap(q[pos], q[v1[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v1[pos]]); swap(q[pos], q[v2[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v2[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } nextCost = cost + Vertical; swap(q[pos], q[v3[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v3[pos]]); swap(q[pos], q[v4[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v4[pos]]); } } }
#include <bits/stdc++.h> using namespace std; typedef pair<int, string> State; const int v1[] = {1, 2, 3, 4, 5, 6, 7, 8, 0}; const int v2[] = {8, 0, 1, 2, 3, 4, 5, 6, 7}; const int v3[] = {6, 7, 8, 0, 1, 2, 3, 4, 5}; const int v4[] = {3, 4, 5, 6, 7, 8, 0, 1, 2}; int main() { int Horizon, Vertical; while (cin >> Horizon >> Vertical, Horizon) { string D = "", E = ""; for (int i = 0; i < 9; i++) { string K; cin >> K; D += K; } for (int i = 0; i < 9; i++) { string K; cin >> K; E += K; } priority_queue<State, vector<State>, greater<State>> que; que.push(State(0, D)); unordered_map<string, int> used; used[D] = 0; while (!que.empty()) { string q = que.top().second; int cost = que.top().first; que.pop(); if (q == E) { cout << cost << endl; break; } if (used.find(q) != used.end() && used[q] < cost) { continue; } int pos = q.find('0'), nextCost; nextCost = cost + Horizon; swap(q[pos], q[v1[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v1[pos]]); swap(q[pos], q[v2[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v2[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } nextCost = cost + Vertical; swap(q[pos], q[v3[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v3[pos]]); swap(q[pos], q[v4[pos]]); if (used.find(q) == used.end() || used[q] > nextCost) { used[q] = nextCost; que.push(State(nextCost, q)); } swap(q[pos], q[v4[pos]]); } } }
[["+", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 107], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 22], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
1
696
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int a, b, c = ~(1 << 31), M = (1 << 16) - 1; int r() { a = 36969 * (a & M) + (a >> 16); b = 18000 * (b & M) + (a >> 16); return (c & ((a << 16) + b)) % 1000000; } set<pair<int, int>> st[303030]; int main() { int m, n; while (cin >> m >> n >> a >> b, m + n != 0) { int l = m + n; int x[l], y[l], z[l]; rep(i, m) cin >> x[i] >> y[i] >> z[i]; rep(i, n) { x[i + m] = r(); y[i + m] = r(); z[i + m] = r(); } vector<int> ord(l); rep(i, l) ord[i] = i; sort(ord.begin(), ord.end(), [&](int s, int t) { return x[s] == x[t] ? y[s] > y[t] : x[s] < x[t]; }); rep(i, 303030) { if (st[i].size() == 2) continue; st[i].clear(); st[i].insert({inf, -inf}); st[i].insert({-inf, inf}); } int ans = 0; for (auto i : ord) { int left = 0, right = ans + 1; while (right - left > 1) { int mid = (left + right) / 2; auto itr = --st[mid].lower_bound({y[i], -1}); if (itr->second < z[i]) left = mid; else right = mid; } int ret = left + 1; ans = max(ret, ans); auto itr = --st[ret].lower_bound({y[i], inf}); if (z[i] >= itr->second) continue; if (itr->first < y[i]) itr++; while (itr->second >= z[i]) itr = st[ret].erase(itr); st[ret].insert({y[i], z[i]}); } cout << ans << endl; } return 0; }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int a, b, c = ~(1 << 31), M = (1 << 16) - 1; int r() { a = 36969 * (a & M) + (a >> 16); b = 18000 * (b & M) + (b >> 16); return (c & ((a << 16) + b)) % 1000000; } set<pair<int, int>> st[303030]; int main() { int m, n; while (cin >> m >> n >> a >> b, m + n != 0) { int l = m + n; int x[l], y[l], z[l]; rep(i, m) cin >> x[i] >> y[i] >> z[i]; rep(i, n) { x[i + m] = r(); y[i + m] = r(); z[i + m] = r(); } vector<int> ord(l); rep(i, l) ord[i] = i; sort(ord.begin(), ord.end(), [&](int s, int t) { return x[s] == x[t] ? y[s] > y[t] : x[s] < x[t]; }); rep(i, 303030) { if (st[i].size() == 2) continue; st[i].clear(); st[i].insert({inf, -inf}); st[i].insert({-inf, inf}); } int ans = 0; for (auto i : ord) { int left = 0, right = ans + 1; while (right - left > 1) { int mid = (left + right) / 2; auto itr = --st[mid].lower_bound({y[i], -1}); if (itr->second < z[i]) left = mid; else right = mid; } int ret = left + 1; ans = max(ret, ans); auto itr = --st[ret].lower_bound({y[i], inf}); if (z[i] >= itr->second) continue; if (itr->first < y[i]) itr++; while (itr->second >= z[i]) itr = st[ret].erase(itr); st[ret].insert({y[i], z[i]}); } cout << ans << endl; } return 0; }
[["-", 0, 11, 12, 16, 12, 23, 0, 16, 31, 22], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 31, 22]]
1
656
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> PII; const int MAXN = 300000 + 10, inf = 1e9; struct Node { PII e; Node *ls, *rs; int dim, cur, sub; } pool[MAXN], *cnt; struct Point { int x, y, z; Point() {} Point(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {} bool operator<(const Point &rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && z < rhs.z); } } P[MAXN]; map<int, int> best[MAXN]; int dp[MAXN], ret; int r(int &a, int &b) { static const int C = ~(1 << 31), M = (1 << 16) - 1; a = 36969 * (a & M) + (a >> 16); b = 18000 * (b & M) + (b >> 16); return (C & ((a << 16) + b)) % 1000000; } inline bool check(const map<int, int> &mp, int x, int y) { auto it = mp.lower_bound(x); return it != mp.begin() && (--it)->second < y; } int query(int x, int y) { int left = 0, right = ret; while (left < right) { int mid = (left + right + 1) >> 1; if (check(best[mid], x, y)) left = mid; else right = mid - 1; } return left; } void update(map<int, int> &mp, int x, int y) { auto it = mp.upper_bound(x); if (it != mp.begin() && (--it)->second <= y) return; it = mp.lower_bound(x); while (it != mp.end() && it->second >= y) mp.erase(it++); mp[x] = y; } int main() { int N, M, A, B; while (scanf("%d%d%d%d", &M, &N, &A, &B) == 4 && (N + M)) { for (int i = 0; i < M; ++i) { scanf("%d%d%d", &P[i].x, &P[i].y, &P[i].z); } N += M; for (int i = M; i < N; ++i) { P[i].x = r(A, B); P[i].y = r(A, B); P[i].z = r(A, B); } for (int i = 0; i <= N; ++i) best[i].clear(); sort(P, P + N); ret = 0; for (int i = 0, j; i < N;) { for (j = i; j < N && P[i].x == P[j].x; ++j) dp[j] = query(P[i].y, P[i].z) + 1; for (; i < j; ++i) { update(best[dp[i]], P[i].y, P[i].z); ret = max(ret, dp[i]); } } printf("%d\n", ret); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> PII; const int MAXN = 300000 + 10, inf = 1e9; struct Node { PII e; Node *ls, *rs; int dim, cur, sub; } pool[MAXN], *cnt; struct Point { int x, y, z; Point() {} Point(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {} bool operator<(const Point &rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && z < rhs.z); } } P[MAXN]; map<int, int> best[MAXN]; int dp[MAXN], ret; int r(int &a, int &b) { static const int C = ~(1u << 31), M = (1 << 16) - 1; a = 36969 * (a & M) + (a >> 16); b = 18000 * (b & M) + (b >> 16); return (C & ((a << 16) + b)) % 1000000; } inline bool check(const map<int, int> &mp, int x, int y) { auto it = mp.lower_bound(x); return it != mp.begin() && (--it)->second < y; } int query(int x, int y) { int left = 0, right = ret; while (left < right) { int mid = (left + right + 1) >> 1; if (check(best[mid], x, y)) left = mid; else right = mid - 1; } return left; } void update(map<int, int> &mp, int x, int y) { auto it = mp.upper_bound(x); if (it != mp.begin() && (--it)->second <= y) return; it = mp.lower_bound(x); while (it != mp.end() && it->second >= y) mp.erase(it++); mp[x] = y; } int main() { int N, M, A, B; while (scanf("%d%d%d%d", &M, &N, &A, &B) == 4 && (N + M)) { for (int i = 0; i < M; ++i) { scanf("%d%d%d", &P[i].x, &P[i].y, &P[i].z); } N += M; for (int i = M; i < N; ++i) { P[i].x = r(A, B); P[i].y = r(A, B); P[i].z = r(A, B); } for (int i = 0; i <= N; ++i) best[i].clear(); sort(P, P + N); ret = 0; for (int i = 0, j; i < N;) { for (j = i; j < N && P[i].x == P[j].x; ++j) dp[j] = query(P[j].y, P[j].z) + 1; for (; i < j; ++i) { update(best[dp[i]], P[i].y, P[i].z); ret = max(ret, dp[i]); } } printf("%d\n", ret); } return 0; }
[["-", 49, 50, 51, 91, 28, 23, 0, 16, 31, 13], ["+", 49, 50, 51, 91, 28, 23, 0, 16, 31, 13], ["-", 3, 4, 0, 118, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 118, 28, 69, 341, 342, 0, 22]]
1
791
#include <algorithm> #include <cstdlib> #include <iostream> #include <map> #include <tuple> #include <vector> using namespace std; typedef tuple<int, int, int> point; #define x(p) get<0>(p) #define y(p) get<1>(p) #define z(p) get<2>(p) int r(int &a, int &b) { static constexpr int C = ~(1 << 31); static constexpr int M = (1 << 16) - 1; a = 36969 * (a & M) + (a >> 16); b = 18000 * (b & M) + (b >> 16); return (C & ((a << 16) + b)) % 1000000; } bool check(int y, int z, const map<int, int> &contour) { const auto it = contour.upper_bound(y); return it != contour.end() && it->second < z; } int solve(vector<point> &points) { sort(points.begin(), points.end()); int max_count = 0; int prev_x = x(points.front()); vector<tuple<int, int, int>> buf; vector<map<int, int>> maps{{{1, -1}}}; buf.reserve(points.size()); maps.reserve(points.size()); for (const auto &p : points) { const int x = x(p); const int y = -y(p); const int z = z(p); if (prev_x != x) { maps.resize(max_count + 1); prev_x = x; for (const auto &e : buf) { int k, ty, tz; tie(k, ty, tz) = e; auto &target = maps[k]; auto it = target.find(ty); if (it == target.end() || it->second > tz) { target[ty] = tz; it = target.find(ty); while (it != target.begin()) { --it; if (tz > it->second) break; it = target.erase(it); } } } buf.clear(); } int low = 0; int high = maps.size(); while (low + 1 < high) { const int mid = (low + high) / 2; if (check(y, z, maps[mid])) { low = mid; } else { high = mid; } } if (low == max_count) ++max_count; buf.emplace_back(high, y, z); } return max_count; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); for (int m, n, a, b; cin >> m >> n >> a >> b && (m | n);) { vector<point> points; points.reserve(n + m); for (int i = 0; i < m; ++i) { int x, y, z; cin >> x >> y >> z; points.emplace_back(x, y, z); } for (int i = 0; i < n; ++i) { const int x = r(a, b); const int y = r(a, b); const int z = r(a, b); points.emplace_back(x, y, z); } cout << solve(points) << endl; } return EXIT_SUCCESS; }
#include <algorithm> #include <cstdlib> #include <iostream> #include <map> #include <tuple> #include <vector> using namespace std; typedef tuple<int, int, int> point; #define x(p) get<0>(p) #define y(p) get<1>(p) #define z(p) get<2>(p) int r(int &a, int &b) { static constexpr int C = ~(1 << 31); static constexpr int M = (1 << 16) - 1; a = 36969 * (a & M) + (a >> 16); b = 18000 * (b & M) + (b >> 16); return (C & ((a << 16) + b)) % 1000000; } bool check(int y, int z, const map<int, int> &contour) { const auto it = contour.upper_bound(y); return it != contour.end() && it->second < z; } int solve(vector<point> &points) { sort(points.begin(), points.end()); int max_count = 0; int prev_x = x(points.front()); vector<tuple<int, int, int>> buf; vector<map<int, int>> maps{{{1, -1}}}; buf.reserve(points.size()); maps.reserve(points.size()); for (const auto &p : points) { const int x = x(p); const int y = -y(p); const int z = z(p); if (prev_x != x) { maps.resize(max_count + 1); prev_x = x; for (const auto &e : buf) { int k, ty, tz; tie(k, ty, tz) = e; auto &target = maps[k]; auto it = target.lower_bound(ty); if (it == target.end() || it->second > tz) { target[ty] = tz; it = target.find(ty); while (it != target.begin()) { --it; if (tz > it->second) break; it = target.erase(it); } } } buf.clear(); } int low = 0; int high = maps.size(); while (low + 1 < high) { const int mid = (low + high) / 2; if (check(y, z, maps[mid])) { low = mid; } else { high = mid; } } if (low == max_count) ++max_count; buf.emplace_back(high, y, z); } return max_count; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); for (int m, n, a, b; cin >> m >> n >> a >> b && (m | n);) { vector<point> points; points.reserve(n + m); for (int i = 0; i < m; ++i) { int x, y, z; cin >> x >> y >> z; points.emplace_back(x, y, z); } for (int i = 0; i < n; ++i) { const int x = r(a, b); const int y = r(a, b); const int z = r(a, b); points.emplace_back(x, y, z); } cout << solve(points) << endl; } return EXIT_SUCCESS; }
[["-", 0, 43, 49, 50, 51, 2, 63, 118, 119, 120], ["+", 0, 43, 49, 50, 51, 2, 63, 118, 119, 120]]
1
725
#include <bits/stdc++.h> #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) #define PI 3.141592653589793238 #define int long long using namespace std; // COUNTER CLOCKWISE static const int CCW_COUNTER_CLOCKWISE = 1; static const int CCW_CLOCKWISE = -1; static const int CCW_ONLINE_BACK = 2; static const int CCW_ONLINE_FRONT = -2; static const int CCW_ON_SEGMENT = 0; // Intercsect Circle & Circle static const int ICC_SEPERATE = 4; static const int ICC_CIRCUMSCRIBE = 3; static const int ICC_INTERSECT = 2; static const int ICC_INSCRIBE = 1; static const int ICC_CONTAIN = 0; struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double k) { return Point(x * k, y * k); } Point operator/(double k) { return Point(x / k, y / k); } double norm() { return x * x + y * y; } double abs() { return sqrt(norm()); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; istream &operator>>(istream &is, Point &p) { is >> p.x >> p.y; return is; } ostream &operator<<(ostream &os, Point p) { os << fixed << setprecision(12) << p.x << " " << p.y; return os; } bool sort_x(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; } bool sort_y(Point a, Point b) { return a.y != b.y ? a.y < b.y : a.x < b.x; } typedef Point Vector; typedef vector<Point> Polygon; struct Segment { Point p1, p2; Segment() {} Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; typedef Segment Line; istream &operator>>(istream &is, Segment &s) { is >> s.p1 >> s.p2; return is; } struct Circle { Point c; double r; Circle() {} Circle(Point c, double r) : c(c), r(r) {} }; istream &operator>>(istream &is, Circle &c) { is >> c.c >> c.r; return is; } double norm(Vector a) { return a.x * a.x + a.y * a.y; } double abs(Vector a) { return sqrt(norm(a)); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool isOrthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) { return isOrthogonal(a1 - a2, b1 - b2); } bool isOrthogonal(Segment s1, Segment s2) { return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool isParallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool isParallel(Point a1, Point a2, Point b1, Point b2) { return isParallel(a1 - a2, b1 - b2); } bool isParallel(Segment s1, Segment s2) { return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } double arg(Vector p) { return atan2(p.y, p.x); } Vector polar(double a, double r) { return Point(cos(r) * a, sin(r) * a); } int ccw(Point p0, Point p1, Point p2); bool intersectSS(Point p1, Point p2, Point p3, Point p4); bool intersectSS(Segment s1, Segment s2); int intersectCC(Circle c1, Circle c2); bool intersectSC(Segment s, Circle c); double getDistanceLP(Line l, Point p); double getDistanceSP(Segment s, Point p); double getDistanceSS(Segment s1, Segment s2); Point getCrossPointSS(Segment s1, Segment s2); Point getCrossPointLL(Line l1, Line l2); pair<Point, Point> getCrossPointCL(Circle c, Line l); pair<Point, Point> getCrossPointCC(Circle c1, Circle c2); int contains(Polygon g, Point p); Polygon andrewScan(Polygon s); Polygon convex_hull(Polygon ps); double diameter(Polygon s); bool isConvex(Polygon p); double area(Polygon s); Polygon convexCut(Polygon p, Line l); Line bisector(Point p1, Point p2); Vector translate(Vector v, double theta); vector<Line> corner(Line l1, Line l2); int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return CCW_COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CCW_CLOCKWISE; if (dot(a, b) < -EPS) return CCW_ONLINE_BACK; if (a.norm() < b.norm()) return CCW_ONLINE_FRONT; return CCW_ON_SEGMENT; } bool intersectSS(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersectSS(Segment s1, Segment s2) { return intersectSS(s1.p1, s1.p2, s2.p1, s2.p2); } int intersectCC(Circle c1, Circle c2) { if (c1.r < c2.r) swap(c1, c2); double d = abs(c1.c - c2.c); double r = c1.r + c2.r; if (equals(d, r)) return ICC_CIRCUMSCRIBE; if (d > r) return ICC_SEPERATE; if (equals(d + c2.r, c1.r)) return ICC_INSCRIBE; if (d + c2.r < c1.r) return ICC_CONTAIN; return ICC_INTERSECT; } bool intersectSC(Segment s, Circle c) { double d = getDistanceSP(s, c.c); return d <= c.r; } double getDistanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } double getDistanceSS(Segment s1, Segment s2) { if (intersectSS(s1, s2)) return 0.0; return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)), min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2))); } Point getCrossPointSS(Segment s1, Segment s2) { Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base, s1.p1 - s2.p1)); double d2 = abs(cross(base, s1.p2 - s2.p1)); double t = d1 / (d1 + d2); return s1.p1 + (s1.p2 - s1.p1) * t; } Point getCrossPointLL(Line l1, Line l2) { double a = cross(l1.p2 - l1.p1, l2.p2 - l2.p1); double b = cross(l1.p2 - l1.p1, l1.p2 - l2.p1); if (abs(a) < EPS && abs(b) < EPS) return l2.p1; return l2.p1 + (l2.p2 - l2.p1) * (b / a); } pair<Point, Point> getCrossPointCL(Circle c, Line l) { Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(c.r * c.r - norm(pr - c.c)); return make_pair(pr + e * base, pr - e * base); } pair<Point, Point> getCrossPointCC(Circle c1, Circle c2) { double d = abs(c1.c - c2.c); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a)); } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (fabs(cross(a, b)) < EPS && dot(a, b) < EPS) return 1; if (a.y > b.y) swap(a, b); if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x; } return (x ? 2 : 0); } Polygon andrewScan(Polygon s) { Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); for (int i = 2; i < (int)s.size(); i++) { for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CCW_CLOCKWISE; n--) { u.pop_back(); } u.push_back(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CCW_CLOCKWISE; n--) { l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) l.push_back(u[i]); return l; } Polygon convex_hull(Polygon ps) { int n = ps.size(); sort(ps.begin(), ps.end(), sort_y); int k = 0; Polygon qs(n * 2); for (int i = 0; i < n; i++) { while (k > 1 && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < 0) k--; qs[k++] = ps[i]; } for (int i = n - 2, t = k; i >= 0; i--) { while (k > t && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < 0) k--; qs[k++] = ps[i]; } qs.resize(k - 1); return qs; } double diameter(Polygon s) { Polygon p = s; int n = p.size(); if (n == 2) return abs(p[0] - p[1]); int i = 0, j = 0; for (int k = 0; k < n; k++) { if (p[i] < p[k]) i = k; if (!(p[j] < p[k])) j = k; } double res = 0; int si = i, sj = j; while (i != sj || j != si) { res = max(res, abs(p[i] - p[j])); if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) < 0.0) { i = (i + 1) % n; } else { j = (j + 1) % n; } } return res; } bool isConvex(Polygon p) { bool f = 1; int n = p.size(); for (int i = 0; i < n; i++) { int t = ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]); f &= t != CCW_CLOCKWISE; } return f; } double area(Polygon s) { double res = 0; for (int i = 0; i < (int)s.size(); i++) { res += cross(s[i], s[(i + 1) % s.size()]) / 2.0; } return abs(res); } Polygon convexCut(Polygon p, Line l) { Polygon q; for (int i = 0; i < (int)p.size(); i++) { Point a = p[i], b = p[(i + 1) % p.size()]; if (ccw(l.p1, l.p2, a) != -1) q.push_back(a); if (ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) < 0) q.push_back(getCrossPointLL(Line(a, b), l)); } return q; } Line bisector(Point p1, Point p2) { Circle c1 = Circle(p1, abs(p1 - p2)), c2 = Circle(p2, abs(p1 - p2)); pair<Point, Point> p = getCrossPointCC(c1, c2); if (cross(p2 - p1, p.first - p1) > 0) swap(p.first, p.second); return Line(p.first, p.second); } Vector translate(Vector v, double theta) { Vector res; res.x = cos(theta) * v.x - sin(theta) * v.y; res.y = sin(theta) * v.x + cos(theta) * v.y; return res; } vector<Line> corner(Line l1, Line l2) { vector<Line> res; if (isParallel(l1, l2)) { double d = getDistanceLP(l1, l2.p1) / 2.0; Vector v1 = l1.p2 - l1.p1; v1 = v1 / v1.abs() * d; Point p = l2.p1 + translate(v1, 90.0 * (PI / 180.0)); double d1 = getDistanceLP(l1, p); double d2 = getDistanceLP(l2, p); if (abs(d1 - d2) > d) { p = l2.p1 + translate(v1, -90.0 * (PI / 180.0)); } res.push_back(Line(p, p + v1)); } else { Point p = getCrossPointLL(l1, l2); Vector v1 = l1.p2 - l1.p1, v2 = l2.p2 - l2.p1; v1 = v1 / v1.abs(); v2 = v2 / v2.abs(); res.push_back(Line(p, p + (v1 + v2))); res.push_back(Line(p, p + translate(v1 + v2, 90.0 * (PI / 180.0)))); } return res; } inline bool calc(Point q, Circle *c, int n) { bool tmp = 0; for (int a = 0; a < n; a++) tmp |= abs(q - c[a].c) < c[a].r; return tmp; } signed main() { int n; double w; while (cin >> n >> w, n) { Polygon p(n); double h[n]; for (int i = 0; i < n; i++) cin >> p[i] >> h[i]; double L = 0, R = 1e3; for (int k = 0; k < 30; k++) { double M = (L + R) / 2; // cout<<L<<" "<<M<<" "<<R<<endl; double x1, x2; if (M <= w) { x1 = M; x2 = 100 - M; } else { double tmp = sqrt(2 * M * w - w * w); x1 = tmp; x2 = 100 - tmp; } if (x2 < x1) R = M; else L = M; } { Circle c[n]; for (int i = 0; i < n; i++) { c[i].c = p[i]; if (L <= h[i]) c[i].r = L; else c[i].r = sqrt(2 * L * h[i] - h[i] * h[i]); } Point q(50, 50); if (!calc(q, c, n)) goto SKIP; } L = 0; for (int k = 0; k < 30; k++) { double M = (L + R) / 2; // cout<<L<<" "<<M<<" "<<R<<endl; double x1, x2; if (M <= w) { x1 = M; x2 = 100 - M; } else { double tmp = sqrt(2 * M * w - w * w); x1 = tmp; x2 = 100 - tmp; } // cout<<x1<<" "<<x1<<endl;cout<<x1<<" "<<x2<<endl; // cout<<x2<<" "<<x2<<endl;cout<<x2<<" "<<x1<<endl; bool flg = 0; Circle c[n]; for (int i = 0; i < n; i++) { c[i].c = p[i]; if (M <= h[i]) c[i].r = M; else c[i].r = sqrt(2 * M * h[i] - h[i] * h[i]); // cout<<i<<" "<<c[i].r<<endl; } Polygon qs(4); qs[0] = Point(x1, x1); qs[1] = Point(x1, x2); qs[2] = Point(x2, x2); qs[3] = Point(x2, x1); { Point q(50, 50); if (!calc(q, c, n)) { flg = 1; goto END; } for (int i = 0; i < 4; i++) { if (!calc(qs[i], c, n)) { flg = 1; goto END; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (abs(c[i].c - c[j].c) > c[i].r + c[j].r) continue; auto q = getCrossPointCC(c[i], c[j]); if (x1 <= q.first.x && q.first.x <= x2 && x1 <= q.first.y && q.first.y <= x2 && !calc(q.first, c, n)) { flg = 1; goto END; } if (x1 <= q.second.x && q.second.x <= x2 && x1 <= q.second.y && q.second.y <= x2 && !calc(q.second, c, n)) { flg = 1; goto END; } } for (int j = 0; j < 4; j++) { Line l = Line(qs[j], qs[(j + 1) % 4]); if (getDistanceLP(l, c[i].c) > c[i].r) continue; auto q = getCrossPointCL(c[i], l); if (x1 <= q.first.x && q.first.x <= x2 && x1 <= q.first.y && q.first.y <= x2 && !calc(q.first, c, n)) { flg = 1; goto END; } if (x1 <= q.second.x && q.second.x <= x2 && x1 <= q.second.y && q.second.y <= x2 && !calc(q.second, c, n)) { flg = 1; goto END; } } } END: if (flg) L = M; else R = M; } SKIP: printf("%.5f\n", L); } return 0; }
#include <bits/stdc++.h> #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) #define PI 3.141592653589793238 #define int long long using namespace std; // COUNTER CLOCKWISE static const int CCW_COUNTER_CLOCKWISE = 1; static const int CCW_CLOCKWISE = -1; static const int CCW_ONLINE_BACK = 2; static const int CCW_ONLINE_FRONT = -2; static const int CCW_ON_SEGMENT = 0; // Intercsect Circle & Circle static const int ICC_SEPERATE = 4; static const int ICC_CIRCUMSCRIBE = 3; static const int ICC_INTERSECT = 2; static const int ICC_INSCRIBE = 1; static const int ICC_CONTAIN = 0; struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double k) { return Point(x * k, y * k); } Point operator/(double k) { return Point(x / k, y / k); } double norm() { return x * x + y * y; } double abs() { return sqrt(norm()); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; istream &operator>>(istream &is, Point &p) { is >> p.x >> p.y; return is; } ostream &operator<<(ostream &os, Point p) { os << fixed << setprecision(12) << p.x << " " << p.y; return os; } bool sort_x(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; } bool sort_y(Point a, Point b) { return a.y != b.y ? a.y < b.y : a.x < b.x; } typedef Point Vector; typedef vector<Point> Polygon; struct Segment { Point p1, p2; Segment() {} Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; typedef Segment Line; istream &operator>>(istream &is, Segment &s) { is >> s.p1 >> s.p2; return is; } struct Circle { Point c; double r; Circle() {} Circle(Point c, double r) : c(c), r(r) {} }; istream &operator>>(istream &is, Circle &c) { is >> c.c >> c.r; return is; } double norm(Vector a) { return a.x * a.x + a.y * a.y; } double abs(Vector a) { return sqrt(norm(a)); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool isOrthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) { return isOrthogonal(a1 - a2, b1 - b2); } bool isOrthogonal(Segment s1, Segment s2) { return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool isParallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool isParallel(Point a1, Point a2, Point b1, Point b2) { return isParallel(a1 - a2, b1 - b2); } bool isParallel(Segment s1, Segment s2) { return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } double arg(Vector p) { return atan2(p.y, p.x); } Vector polar(double a, double r) { return Point(cos(r) * a, sin(r) * a); } int ccw(Point p0, Point p1, Point p2); bool intersectSS(Point p1, Point p2, Point p3, Point p4); bool intersectSS(Segment s1, Segment s2); int intersectCC(Circle c1, Circle c2); bool intersectSC(Segment s, Circle c); double getDistanceLP(Line l, Point p); double getDistanceSP(Segment s, Point p); double getDistanceSS(Segment s1, Segment s2); Point getCrossPointSS(Segment s1, Segment s2); Point getCrossPointLL(Line l1, Line l2); pair<Point, Point> getCrossPointCL(Circle c, Line l); pair<Point, Point> getCrossPointCC(Circle c1, Circle c2); int contains(Polygon g, Point p); Polygon andrewScan(Polygon s); Polygon convex_hull(Polygon ps); double diameter(Polygon s); bool isConvex(Polygon p); double area(Polygon s); Polygon convexCut(Polygon p, Line l); Line bisector(Point p1, Point p2); Vector translate(Vector v, double theta); vector<Line> corner(Line l1, Line l2); int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return CCW_COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CCW_CLOCKWISE; if (dot(a, b) < -EPS) return CCW_ONLINE_BACK; if (a.norm() < b.norm()) return CCW_ONLINE_FRONT; return CCW_ON_SEGMENT; } bool intersectSS(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersectSS(Segment s1, Segment s2) { return intersectSS(s1.p1, s1.p2, s2.p1, s2.p2); } int intersectCC(Circle c1, Circle c2) { if (c1.r < c2.r) swap(c1, c2); double d = abs(c1.c - c2.c); double r = c1.r + c2.r; if (equals(d, r)) return ICC_CIRCUMSCRIBE; if (d > r) return ICC_SEPERATE; if (equals(d + c2.r, c1.r)) return ICC_INSCRIBE; if (d + c2.r < c1.r) return ICC_CONTAIN; return ICC_INTERSECT; } bool intersectSC(Segment s, Circle c) { double d = getDistanceSP(s, c.c); return d <= c.r; } double getDistanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } double getDistanceSS(Segment s1, Segment s2) { if (intersectSS(s1, s2)) return 0.0; return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)), min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2))); } Point getCrossPointSS(Segment s1, Segment s2) { Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base, s1.p1 - s2.p1)); double d2 = abs(cross(base, s1.p2 - s2.p1)); double t = d1 / (d1 + d2); return s1.p1 + (s1.p2 - s1.p1) * t; } Point getCrossPointLL(Line l1, Line l2) { double a = cross(l1.p2 - l1.p1, l2.p2 - l2.p1); double b = cross(l1.p2 - l1.p1, l1.p2 - l2.p1); if (abs(a) < EPS && abs(b) < EPS) return l2.p1; return l2.p1 + (l2.p2 - l2.p1) * (b / a); } pair<Point, Point> getCrossPointCL(Circle c, Line l) { Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(c.r * c.r - norm(pr - c.c)); return make_pair(pr + e * base, pr - e * base); } pair<Point, Point> getCrossPointCC(Circle c1, Circle c2) { double d = abs(c1.c - c2.c); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a)); } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (fabs(cross(a, b)) < EPS && dot(a, b) < EPS) return 1; if (a.y > b.y) swap(a, b); if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x; } return (x ? 2 : 0); } Polygon andrewScan(Polygon s) { Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); for (int i = 2; i < (int)s.size(); i++) { for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CCW_CLOCKWISE; n--) { u.pop_back(); } u.push_back(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CCW_CLOCKWISE; n--) { l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) l.push_back(u[i]); return l; } Polygon convex_hull(Polygon ps) { int n = ps.size(); sort(ps.begin(), ps.end(), sort_y); int k = 0; Polygon qs(n * 2); for (int i = 0; i < n; i++) { while (k > 1 && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < 0) k--; qs[k++] = ps[i]; } for (int i = n - 2, t = k; i >= 0; i--) { while (k > t && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < 0) k--; qs[k++] = ps[i]; } qs.resize(k - 1); return qs; } double diameter(Polygon s) { Polygon p = s; int n = p.size(); if (n == 2) return abs(p[0] - p[1]); int i = 0, j = 0; for (int k = 0; k < n; k++) { if (p[i] < p[k]) i = k; if (!(p[j] < p[k])) j = k; } double res = 0; int si = i, sj = j; while (i != sj || j != si) { res = max(res, abs(p[i] - p[j])); if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) < 0.0) { i = (i + 1) % n; } else { j = (j + 1) % n; } } return res; } bool isConvex(Polygon p) { bool f = 1; int n = p.size(); for (int i = 0; i < n; i++) { int t = ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]); f &= t != CCW_CLOCKWISE; } return f; } double area(Polygon s) { double res = 0; for (int i = 0; i < (int)s.size(); i++) { res += cross(s[i], s[(i + 1) % s.size()]) / 2.0; } return abs(res); } Polygon convexCut(Polygon p, Line l) { Polygon q; for (int i = 0; i < (int)p.size(); i++) { Point a = p[i], b = p[(i + 1) % p.size()]; if (ccw(l.p1, l.p2, a) != -1) q.push_back(a); if (ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) < 0) q.push_back(getCrossPointLL(Line(a, b), l)); } return q; } Line bisector(Point p1, Point p2) { Circle c1 = Circle(p1, abs(p1 - p2)), c2 = Circle(p2, abs(p1 - p2)); pair<Point, Point> p = getCrossPointCC(c1, c2); if (cross(p2 - p1, p.first - p1) > 0) swap(p.first, p.second); return Line(p.first, p.second); } Vector translate(Vector v, double theta) { Vector res; res.x = cos(theta) * v.x - sin(theta) * v.y; res.y = sin(theta) * v.x + cos(theta) * v.y; return res; } vector<Line> corner(Line l1, Line l2) { vector<Line> res; if (isParallel(l1, l2)) { double d = getDistanceLP(l1, l2.p1) / 2.0; Vector v1 = l1.p2 - l1.p1; v1 = v1 / v1.abs() * d; Point p = l2.p1 + translate(v1, 90.0 * (PI / 180.0)); double d1 = getDistanceLP(l1, p); double d2 = getDistanceLP(l2, p); if (abs(d1 - d2) > d) { p = l2.p1 + translate(v1, -90.0 * (PI / 180.0)); } res.push_back(Line(p, p + v1)); } else { Point p = getCrossPointLL(l1, l2); Vector v1 = l1.p2 - l1.p1, v2 = l2.p2 - l2.p1; v1 = v1 / v1.abs(); v2 = v2 / v2.abs(); res.push_back(Line(p, p + (v1 + v2))); res.push_back(Line(p, p + translate(v1 + v2, 90.0 * (PI / 180.0)))); } return res; } inline bool calc(Point q, Circle *c, int n) { bool tmp = 0; for (int a = 0; a < n; a++) tmp |= abs(q - c[a].c) + EPS < c[a].r; return tmp; } signed main() { int n; double w; while (cin >> n >> w, n) { Polygon p(n); double h[n]; for (int i = 0; i < n; i++) cin >> p[i] >> h[i]; double L = 0, R = 1e3; for (int k = 0; k < 30; k++) { double M = (L + R) / 2; // cout<<L<<" "<<M<<" "<<R<<endl; double x1, x2; if (M <= w) { x1 = M; x2 = 100 - M; } else { double tmp = sqrt(2 * M * w - w * w); x1 = tmp; x2 = 100 - tmp; } if (x2 < x1) R = M; else L = M; } { Circle c[n]; for (int i = 0; i < n; i++) { c[i].c = p[i]; if (L <= h[i]) c[i].r = L; else c[i].r = sqrt(2 * L * h[i] - h[i] * h[i]); } Point q(50, 50); if (!calc(q, c, n)) goto SKIP; } L = 0; for (int k = 0; k < 30; k++) { double M = (L + R) / 2; double x1, x2; if (M <= w) { x1 = M; x2 = 100 - M; } else { double tmp = sqrt(2 * M * w - w * w); x1 = tmp; x2 = 100 - tmp; } bool flg = 0; Circle c[n]; for (int i = 0; i < n; i++) { c[i].c = p[i]; if (M <= h[i]) c[i].r = M; else c[i].r = sqrt(2 * M * h[i] - h[i] * h[i]); } Polygon qs(4); qs[0] = Point(x1, x1); qs[1] = Point(x1, x2); qs[2] = Point(x2, x2); qs[3] = Point(x2, x1); { Point q(50, 50); if (!calc(q, c, n)) { flg = 1; goto END; } for (int i = 0; i < 4; i++) { if (!calc(qs[i], c, n)) { flg = 1; goto END; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (abs(c[i].c - c[j].c) > c[i].r + c[j].r) continue; auto q = getCrossPointCC(c[i], c[j]); if (x1 <= q.first.x && q.first.x <= x2 && x1 <= q.first.y && q.first.y <= x2 && !calc(q.first, c, n)) { flg = 1; goto END; } if (x1 <= q.second.x && q.second.x <= x2 && x1 <= q.second.y && q.second.y <= x2 && !calc(q.second, c, n)) { flg = 1; goto END; } } for (int j = 0; j < 4; j++) { Line l = Line(qs[j], qs[(j + 1) % 4]); if (getDistanceLP(l, c[i].c) > c[i].r) continue; auto q = getCrossPointCL(c[i], l); if (x1 <= q.first.x && q.first.x <= x2 && x1 <= q.first.y && q.first.y <= x2 && !calc(q.first, c, n)) { flg = 1; goto END; } if (x1 <= q.second.x && q.second.x <= x2 && x1 <= q.second.y && q.second.y <= x2 && !calc(q.second, c, n)) { flg = 1; goto END; } } } END: if (flg) L = M; else R = M; } SKIP: printf("%.5f\n", L); } return 0; }
[["+", 8, 1, 0, 11, 12, 16, 31, 16, 17, 72], ["+", 8, 1, 0, 11, 12, 16, 31, 16, 12, 22]]
1
4,737
#include <cstdio> #include <map> #include <queue> using namespace std; int main() { int n, m; scanf("%d%d", &n, &m); vector<int> start(n), end1(n), end2(n); for (int i = 0; i < n; i++) scanf("%d", &start[i]); { vector<int> p(m); for (int i = 0; i < m; i++) scanf("%d", &p[i]); for (int i = 0, c = 0, x = 0; i < m; i++, x ^= 1) for (int j = 0; j < p[i]; j++, c++) end1[c] = x; for (int i = 0, c = 0, x = 1; i < m; i++, x ^= 1) for (int j = 0; j < p[i]; j++, c++) end2[c] = x; } map<vector<int>, int> depth; queue<vector<int>> q; depth[start] = 1, depth[end1] = depth[end2] = -1; q.push(start), q.push(end1), q.push(end2); for (; !q.empty();) { vector<int> cur = q.front(); q.pop(); int d = depth[cur]; for (int i = 0; i < n - 1; i++) if (cur[i] != cur[i + 1]) { swap(cur[i], cur[i + 1]); map<vector<int>, int>::iterator it = depth.find(cur); if (it == depth.end()) { depth[cur] = d + (d > 0 ? 1 : -1); q.push(cur); } else if (it->second * d < 0) { d -= it->second; if (d < 0) d = -d; printf("%d\n", d - 1); return 0; } swap(cur[i], cur[i + 1]); } } }
#include <cstdio> #include <map> #include <queue> using namespace std; int main() { int n, m; scanf("%d%d", &n, &m); vector<int> start(n), end1(n), end2(n); for (int i = 0; i < n; i++) scanf("%d", &start[i]); { vector<int> p(m); for (int i = 0; i < m; i++) scanf("%d", &p[i]); for (int i = 0, c = 0, x = 0; i < m; i++, x ^= 1) for (int j = 0; j < p[i]; j++, c++) end1[c] = x; for (int i = 0, c = 0, x = 1; i < m; i++, x ^= 1) for (int j = 0; j < p[i]; j++, c++) end2[c] = x; } map<vector<int>, int> depth; queue<vector<int>> q; depth[start] = 1, depth[end1] = depth[end2] = -1; q.push(start), q.push(end1), q.push(end2); for (; !q.empty();) { vector<int> cur = q.front(); q.pop(); int d = depth[cur]; for (int i = 0; i < n - 1; i++) if (cur[i] != cur[i + 1]) { swap(cur[i], cur[i + 1]); map<vector<int>, int>::iterator it = depth.find(cur); if (it == depth.end()) { depth[cur] = d + (d > 0 ? 1 : -1); q.push(cur); } else if (it->second * d < 0) { d -= it->second; if (d < 0) d = -d; printf("%d\n", d - 1); return 0; } swap(cur[i], cur[i + 1]); } } puts("0"); }
[["+", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
486
#include <bits/stdc++.h> using namespace std; int calc(int x, vector<int> b, const vector<int> &p) { const int n = b.size(); int res = 0; int index = 0; for (const auto &e : p) { for (int i = 0; i < e; ++i) { int next_index = index; while (next_index < n && b[next_index] != x) ++next_index; if (next_index == n) return INT_MAX; res += next_index - index; swap(b[index], b[next_index]); ++index; } x ^= 0x1; } return res; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> b(n), p(m); for (auto &e : b) cin >> e; for (auto &e : p) cin >> e; cout << min(calc(1, b, p), calc(0, move(b), p)) << endl; return EXIT_SUCCESS; }
#include <bits/stdc++.h> using namespace std; int calc(int x, vector<int> b, const vector<int> &p) { const int n = b.size(); int res = 0; int index = 0; for (const auto &e : p) { for (int i = 0; i < e; ++i) { int next_index = index; while (next_index < n && b[next_index] != x) ++next_index; if (next_index == n) return INT_MAX; res += next_index - index; swap(b[index], b[next_index]); ++index; } x ^= 0x1; } return res; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> b(n), p(m); for (auto &e : b) cin >> e; for (auto &e : p) cin >> e; cout << min(calc(1, b, p), calc(0, b, p)) << endl; return EXIT_SUCCESS; }
[["-", 3, 4, 0, 2, 3, 4, 0, 2, 63, 22], ["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 24], ["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
1
234
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) int f(const vector<int> &b, const vector<int> &a) { int n = b.size(); { int s1 = 0, s2 = 0; rep(i, n) s1 += b[i], s2 += a[i]; if (s1 != s2) return 10000000; } vector<int> c = b; int s = 0; rep(i, n) if (c[i] != a[i]) { int j = i; while (c[j] != a[i]) j++; s += (j - i); for (int k = j; k > i; k--) swap(c[k - 1], c[k]); } return s; } int main() { int n, m; cin >> n >> m; vector<int> b(n); rep(i, n) cin >> b[i]; vector<int> p(m); rep(i, m) cin >> p[i]; vector<int> a0, a1; rep(i, m) { rep(_, p[i]) { a0.push_back(i % 2); a1.push_back(i & 1); } } cout << min(f(b, a0), f(b, a1)) << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) int f(const vector<int> &b, const vector<int> &a) { int n = b.size(); { int s1 = 0, s2 = 0; rep(i, n) s1 += b[i], s2 += a[i]; if (s1 != s2) return 10000000; } vector<int> c = b; int s = 0; rep(i, n) if (c[i] != a[i]) { int j = i; while (c[j] != a[i]) j++; s += (j - i); for (int k = j; k > i; k--) swap(c[k - 1], c[k]); } return s; } int main() { int n, m; cin >> n >> m; vector<int> b(n); rep(i, n) cin >> b[i]; vector<int> p(m); rep(i, m) cin >> p[i]; vector<int> a0, a1; rep(i, m) { rep(_, p[i]) { a0.push_back(i % 2); a1.push_back((i % 2) ^ 1); } } cout << min(f(b, a0), f(b, a1)) << endl; return 0; }
[["+", 0, 2, 3, 4, 0, 16, 31, 23, 0, 24], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 67], ["+", 3, 4, 0, 16, 31, 23, 0, 16, 17, 109], ["+", 3, 4, 0, 16, 31, 23, 0, 16, 12, 13], ["+", 0, 2, 3, 4, 0, 16, 31, 23, 0, 25], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 140]]
1
314
#include <bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define all(v) (v).begin(), (v).end() #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define pb push_back #define fi first #define se second template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } int T[10][10]; bool check(vint v) { int r = 0; for (int i = 0; i < 5; i++) r = T[r][v[i]]; return r == 0; } signed main() { rep(i, 10) rep(j, 10) cin >> T[i][j]; int cnt = 0; rep(a, 10) rep(b, 10) rep(c, 10) rep(d, 10) { int e = T[T[T[T[0][a]][b]][c]][d]; vint v = {a, b, c, d, e}; bool ok = true; rep(i, 4) { reps(j, 1, 10) { int tmp = v[i]; v[i] = (v[i] + j) % 10; if (check(v)) ok = false; v[i] = tmp; } } rep(i, 4) { swap(v[i], v[i + 1]); if (check(v) && v[i] != v[i + 1]) ok = false; swap(v[i], v[i + 1]); } if (!ok) cnt++; } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define all(v) (v).begin(), (v).end() #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define pb push_back #define fi first #define se second template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } int T[10][10]; bool check(vint v) { int r = 0; for (int i = 0; i < 5; i++) r = T[r][v[i]]; return r == 0; } signed main() { rep(i, 10) rep(j, 10) cin >> T[i][j]; int cnt = 0; rep(a, 10) rep(b, 10) rep(c, 10) rep(d, 10) { int e = T[T[T[T[0][a]][b]][c]][d]; vint v = {a, b, c, d, e}; bool ok = true; rep(i, 5) { reps(j, 1, 10) { int tmp = v[i]; v[i] = (v[i] + j) % 10; if (check(v)) ok = false; v[i] = tmp; } } rep(i, 4) { swap(v[i], v[i + 1]); if (check(v) && v[i] != v[i + 1]) ok = false; swap(v[i], v[i + 1]); } if (!ok) cnt++; } cout << cnt << endl; }
[["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
436
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> using namespace std; int number[10]; int table[15][15]; bool check(int x) { for (int i = 4; i >= 1; --i) number[i] = x % 10, x /= 10; number[5] = 0; // check number for (int i = 1; i <= 4; ++i) number[5] = table[number[5]][number[i]]; /* cout << "number" << endl; for (int i = 1; i <= 5; ++i) cout << number[i] << " "; cout << endl; */ // error1 for (int i = 1; i <= 4; ++i) for (int j = 0; j < 10; ++j) if (j != number[i]) { int tmp = 0; for (int k = 1; k <= 5; ++k) { if (k == i) tmp = table[tmp][j]; else tmp = table[tmp][number[k]]; } if (!tmp) return true; } // error2 for (int i = 1, j = 2; i <= 4; ++i, ++j) if (number[i] != number[j]) { int tmp = 0; for (int k = 1; k <= 5; ++k) { if (k == i) tmp = table[tmp][number[j]]; else if (k == j) tmp = table[tmp][number[i]]; else tmp = table[tmp][number[k]]; } if (!tmp) return true; } return false; } signed main(void) { for (int i = 0; i < 10; ++i) for (int j = 0; j < 10; ++j) cin >> table[i][j]; int answer = 0; for (int i = 0; i < 10000; ++i) if (check(i)) ++answer; cout << answer << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> using namespace std; int number[10]; int table[15][15]; bool check(int x) { for (int i = 4; i >= 1; --i) number[i] = x % 10, x /= 10; number[5] = 0; // check number for (int i = 1; i <= 4; ++i) number[5] = table[number[5]][number[i]]; /* cout << "number" << endl; for (int i = 1; i <= 5; ++i) cout << number[i] << " "; cout << endl; */ // error1 for (int i = 1; i <= 5; ++i) for (int j = 0; j < 10; ++j) if (j != number[i]) { int tmp = 0; for (int k = 1; k <= 5; ++k) { if (k == i) tmp = table[tmp][j]; else tmp = table[tmp][number[k]]; } if (!tmp) return true; } // error2 for (int i = 1, j = 2; i <= 4; ++i, ++j) if (number[i] != number[j]) { int tmp = 0; for (int k = 1; k <= 5; ++k) { if (k == i) tmp = table[tmp][number[j]]; else if (k == j) tmp = table[tmp][number[i]]; else tmp = table[tmp][number[k]]; } if (!tmp) return true; } return false; } signed main(void) { for (int i = 0; i < 10; ++i) for (int j = 0; j < 10; ++j) cin >> table[i][j]; int answer = 0; for (int i = 0; i < 10000; ++i) if (check(i)) ++answer; cout << answer << endl; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
1
410
#include "bits/stdc++.h" using namespace std; //#define int long long #define DBG 1 #define dump(o) \ if (DBG) { \ cerr << #o << " " << (o) << " "; \ } #define dumpl(o) \ if (DBG) { \ cerr << #o << " " << (o) << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // 0?????? void fill(string &s, int length, char c = '0') { string prefix = ""; for (int i = s.size(); i < length; i++) prefix += c; s = prefix + s; } int table[10][10]; char e(string &s) { int ret = 0; rep(i, 0, 4) { ret = table[ret][s[i] - '0']; } return (char)(ret + '0'); } int check(string &s) { string abcd = s.substr(0, 4); return table[e(abcd) - '0'][s[4] - '0']; } signed main() { rep(i, 0, 10) rep(j, 0, 10) cin >> table[i][j]; int ans = 0; rep(x, 0, 10000) { string abcd = to_string(x); fill(abcd, 4); string abcde = abcd + e(abcd); bool flag = false; rep(i, 0, 5) { rep(c, 0, 10) { if (c == abcde[i] - '0') continue; string tmp = abcde; tmp[i] = c + '0'; if (check(tmp) == 0) { bool flag = true; i = INF; break; } } } if (flag) { ans++; continue; } rep(i, 1, 5) { string tmp = abcde; swap(tmp[i], tmp[i - 1]); if (tmp == abcde) continue; if (check(tmp) == 0) { flag = true; ans++; break; } } } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; //#define int long long #define DBG 1 #define dump(o) \ if (DBG) { \ cerr << #o << " " << (o) << " "; \ } #define dumpl(o) \ if (DBG) { \ cerr << #o << " " << (o) << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // 0?????? void fill(string &s, int length, char c = '0') { string prefix = ""; for (int i = s.size(); i < length; i++) prefix += c; s = prefix + s; } int table[10][10]; char e(string &s) { int ret = 0; rep(i, 0, 4) { ret = table[ret][s[i] - '0']; } return (char)(ret + '0'); } int check(string &s) { string abcd = s.substr(0, 4); return table[e(abcd) - '0'][s[4] - '0']; } signed main() { rep(i, 0, 10) rep(j, 0, 10) cin >> table[i][j]; int ans = 0; rep(x, 0, 10000) { string abcd = to_string(x); fill(abcd, 4); string abcde = abcd + e(abcd); bool flag = false; rep(i, 0, 5) { rep(c, 0, 10) { if (c == abcde[i] - '0') continue; string tmp = abcde; tmp[i] = c + '0'; if (check(tmp) == 0) { flag = true; i = INF; break; } } } if (flag) { ans++; continue; } rep(i, 1, 5) { string tmp = abcde; swap(tmp[i], tmp[i - 1]); if (tmp == abcde) continue; if (check(tmp) == 0) { flag = true; ans++; break; } } } cout << ans << endl; return 0; }
[["-", 0, 9, 0, 57, 64, 9, 0, 43, 39, 40]]
1
560
#include <bits/stdc++.h> using namespace std; int x[10][10]; string tos(int num) { string res; while (num) { res += num % 10 + '0'; num /= 10; } reverse(res.begin(), res.end()); while (res.size() != 4) res = '0' + res; return res; } bool check2(string s) { int n = 0; for (int i = 0; i < s.size(); i++) n = x[n][s[i] - '0']; return n == 0; } bool check(int num) { string s = tos(num); int e = 0; for (int i = 0; i < s.size(); i++) e = x[e][s[i] - '0']; s += e + '0'; string temp = s; bool res = false; for (int i = 0; i < s.size(); i++) { char t = s[i]; for (int j = 0; j < 9; j++) { s[i] = j + '0'; if (temp == s) continue; if (check2(s)) res = true; } s[i] = t; } for (int i = 0; i < s.size() - 1; i++) { swap(s[i], s[i + 1]); if (temp == s) { swap(s[i], s[i + 1]); continue; } if (check2(s)) res = true; swap(s[i], s[i + 1]); } return res; } int main() { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) cin >> x[i][j]; int ans = 0; for (int i = 0; i < 10000; i++) if (check(i)) ans++; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int x[10][10]; string tos(int num) { string res; while (num) { res += num % 10 + '0'; num /= 10; } reverse(res.begin(), res.end()); while (res.size() != 4) res = '0' + res; return res; } bool check2(string s) { int n = 0; for (int i = 0; i < s.size(); i++) n = x[n][s[i] - '0']; return n == 0; } bool check(int num) { string s = tos(num); int e = 0; for (int i = 0; i < s.size(); i++) e = x[e][s[i] - '0']; s += e + '0'; string temp = s; bool res = false; for (int i = 0; i < s.size(); i++) { char t = s[i]; for (int j = 0; j < 10; j++) { s[i] = j + '0'; if (temp == s) continue; if (check2(s)) res = true; } s[i] = t; } for (int i = 0; i < s.size() - 1; i++) { swap(s[i], s[i + 1]); if (temp == s) { swap(s[i], s[i + 1]); continue; } if (check2(s)) res = true; swap(s[i], s[i + 1]); } return res; } int main() { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) cin >> x[i][j]; int ans = 0; for (int i = 0; i < 10000; i++) if (check(i)) ans++; cout << ans << endl; return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
1
463
#include <algorithm> #include <cmath> #include <ctime> #include <fstream> #include <iostream> #include <locale> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; long long t[10][10]; long long f(long long a[4]) { long long x = 0; for (long long i = 0; i < 4; i++) x = t[x][a[i]]; return x; } int main() { { for (long long i = 0; i < 10; i++) for (long long j = 0; j < 10; j++) scanf("%ld", &t[i][j]); long long s = 0; for (long long i = 0; i < 10000; i++) { long long a[4], b[4], k = 0; a[3] = i % 10; a[2] = i / 10 % 10; a[1] = i / 100 % 10; a[0] = i / 1000 % 10; long long x0 = f(a); for (long long j = 0; j < 4 && k == 0; j++) { for (long long i = 0; i < 4; i++) b[i] = a[i]; for (long long x = 0; x < 10 && k == 0; x++) { if (a[j] == x) continue; b[j] = x; if (t[f(b)][x0] == 0) { s++; k++; } } } for (long j = 0; j < 10 && k == 0; j++) { if (j == x0) continue; if (t[j][x0] == 0) { s++; k++; } } for (long long j = 1; j < 4 && k == 0; j++) { for (long long x = 0; x < 4; x++) b[x] = a[x]; if (a[j] == a[j - 1]) continue; { long long x = b[j]; b[j] = b[j - 1]; b[j - 1] = x; } if (t[f(b)][x0] == 0) { s++; k++; } } if (x0 != a[3] && k == 0) { for (long long x = 0; x < 4; x++) b[x] = a[x]; b[3] = x0; if (t[f(b)][a[3]] == 0) { s++; k++; } } // cout<<i<<' '<<s<<endl; } cout << s << endl; } return 0; }
#include <algorithm> #include <cmath> #include <ctime> #include <fstream> #include <iostream> #include <locale> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; long long t[10][10]; long long f(long long a[4]) { long long x = 0; for (long long i = 0; i < 4; i++) x = t[x][a[i]]; return x; } int main() { { for (long long i = 0; i < 10; i++) for (long long j = 0; j < 10; j++) scanf("%ld", &t[i][j]); long long s = 0; for (long long i = 0; i < 10000; i++) { long long a[4], b[4], k = 0; a[3] = i % 10; a[2] = i / 10 % 10; a[1] = i / 100 % 10; a[0] = i / 1000 % 10; long long x0 = f(a); for (long long j = 0; j < 4 && k == 0; j++) { for (long long i = 0; i < 4; i++) b[i] = a[i]; for (long long x = 0; x < 10 && k == 0; x++) { if (a[j] == x) continue; b[j] = x; if (t[f(b)][x0] == 0) { s++; k++; } } } for (long j = 0; j < 10 && k == 0; j++) { if (j == x0) continue; if (t[x0][j] == 0) { s++; k++; } } for (long long j = 1; j < 4 && k == 0; j++) { for (long long x = 0; x < 4; x++) b[x] = a[x]; if (a[j] == a[j - 1]) continue; { long long x = b[j]; b[j] = b[j - 1]; b[j - 1] = x; } if (t[f(b)][x0] == 0) { s++; k++; } } if (x0 != a[3] && k == 0) { for (long long x = 0; x < 4; x++) b[x] = a[x]; b[3] = x0; if (t[f(b)][a[3]] == 0) { s++; k++; } } // cout<<i<<' '<<s<<endl; } cout << s << endl; } return 0; }
[["-", 51, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 51, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22]]
1
590
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int>::iterator I; int main() { int n, m; cin >> n >> m; vector<int> v[n]; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[y].push_back(-x); } for (int i = 0; i < n; i++) sort(v[i].begin(), v[i].end()); map<I, int> m1, m2; vector<I> vi; for (int i = 0; i < n; i++) { int t = i, b = i + 1, p = -100000; vi.clear(); while (1) { if (t == 0) break; I it = lower_bound(v[t].begin(), v[t].end(), p); if (it == v[t].end()) break; if (m1.find(it) != m1.end()) { t = m1[it]; break; } p = *it; t--; vi.push_back(it); } for (int j = 0; j < (int)vi.size(); j++) m1[vi[i]] = t; vi.clear(); p = -100000; while (1) { if (b == n) break; I it = lower_bound(v[b].begin(), v[b].end(), p); if (it == v[b].end()) break; if (m2.find(it) != m2.end()) { b = m2[it]; break; } p = *it; b++; vi.push_back(it); } for (int j = 0; j < (int)vi.size(); j++) m2[vi[i]] = b; cout << b - t << " \n"[i == n - 1]; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int>::iterator I; int main() { int n, m; cin >> n >> m; vector<int> v[n]; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[y].push_back(-x); } for (int i = 0; i < n; i++) sort(v[i].begin(), v[i].end()); map<I, int> m1, m2; vector<I> vi; for (int i = 0; i < n; i++) { int t = i, b = i + 1, p = -100000; vi.clear(); while (1) { if (t == 0) break; I it = lower_bound(v[t].begin(), v[t].end(), p); if (it == v[t].end()) break; if (m1.find(it) != m1.end()) { t = m1[it]; break; } p = *it; t--; vi.push_back(it); } for (int j = 0; j < (int)vi.size(); j++) m1[vi[j]] = t; vi.clear(); p = -100000; while (1) { if (b == n) break; I it = lower_bound(v[b].begin(), v[b].end(), p); if (it == v[b].end()) break; if (m2.find(it) != m2.end()) { b = m2[it]; break; } p = *it; b++; vi.push_back(it); } for (int j = 0; j < (int)vi.size(); j++) m2[vi[j]] = b; cout << b - t << " \n"[i == n - 1]; } return 0; }
[["-", 31, 69, 341, 342, 0, 69, 341, 342, 0, 22], ["+", 31, 69, 341, 342, 0, 69, 341, 342, 0, 22]]
1
457
#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 = (a)-1; i >= b; i--) #define chmin(a, b) (a) = min((a), (b)); #define chmax(a, b) (a) = max((a), (b)); #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ cout << (#v) << ":"; \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; #define printVS(vs) \ cout << (#vs) << ":" << endl; \ for (auto(s) : (vs)) { \ cout << (s) << endl; \ } #define printVV(vv) \ cout << (#vv) << ":" << endl; \ for (auto(v) : (vv)) { \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; \ } #define printP(p) cout << (#p) << (p).first << " " << (p).second << endl; #define printVP(vp) \ cout << (#vp) << ":" << endl; \ for (auto(p) : (vp)) { \ cout << (p).first << " " << (p).second << endl; \ } inline void output() { cerr << endl; } template <typename First, typename... Rest> inline void output(const First &first, const Rest &...rest) { cerr << first << " "; output(rest...); } using ll = long long; using Pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; const int inf = 2e9; const int mod = 1e9 + 7; int n, m; const int MAX = 100000; int y[MAX]; int l[2 * MAX]; int r[2 * MAX]; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, m; cin >> n >> m; rep(i, m) { int x, _y; cin >> x >> _y; y[x] = _y; } rep(i, n) { l[i] = r[i] = i; } rep(i, MAX) { if (!y[i]) continue; l[y[i] - 1] = l[y[i]] = min(l[y[i] - 1], l[y[i]]); r[y[i] - 1] = r[y[i]] = min(r[y[i] - 1], r[y[i]]); } rep(i, n) { cout << (i != 0 ? " " : "") << r[i] - l[i] + 1; } cout << 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 = (a)-1; i >= b; i--) #define chmin(a, b) (a) = min((a), (b)); #define chmax(a, b) (a) = max((a), (b)); #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ cout << (#v) << ":"; \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; #define printVS(vs) \ cout << (#vs) << ":" << endl; \ for (auto(s) : (vs)) { \ cout << (s) << endl; \ } #define printVV(vv) \ cout << (#vv) << ":" << endl; \ for (auto(v) : (vv)) { \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; \ } #define printP(p) cout << (#p) << (p).first << " " << (p).second << endl; #define printVP(vp) \ cout << (#vp) << ":" << endl; \ for (auto(p) : (vp)) { \ cout << (p).first << " " << (p).second << endl; \ } inline void output() { cerr << endl; } template <typename First, typename... Rest> inline void output(const First &first, const Rest &...rest) { cerr << first << " "; output(rest...); } using ll = long long; using Pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; const int inf = 2e9; const int mod = 1e9 + 7; int n, m; const int MAX = 100000; int y[MAX]; int l[2 * MAX]; int r[2 * MAX]; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, m; cin >> n >> m; rep(i, m) { int x, _y; cin >> x >> _y; y[x] = _y; } rep(i, n) { l[i] = r[i] = i; } rep(i, MAX) { if (!y[i]) continue; l[y[i] - 1] = l[y[i]] = min(l[y[i] - 1], l[y[i]]); r[y[i] - 1] = r[y[i]] = max(r[y[i] - 1], r[y[i]]); } rep(i, n) { cout << (i != 0 ? " " : "") << r[i] - l[i] + 1; } cout << endl; }
[["-", 0, 1, 0, 11, 12, 11, 12, 2, 63, 22], ["+", 0, 1, 0, 11, 12, 11, 12, 2, 63, 22]]
1
461
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define each(i, a) for (auto &&i : a) #define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++) #define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME #define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__) #define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__) #define pb push_back #define all(a) (a).begin(), (a).end() #define chmin(x, v) x = min(x, v) #define chmax(x, v) x = max(x, v) const ll linf = 1e18; const int inf = 1e9; const double eps = 1e-12; const double pi = acos(-1); template <typename T> istream &operator>>(istream &is, vector<T> &vec) { each(x, vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { rep(i, vec.size()) { if (i) os << " "; os << vec[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &vec) { rep(i, vec.size()) { if (i) os << endl; os << vec[i]; } return os; } class SegmentManager { ll len; set<P> s; ll length(const P &p) { return p.second - p.first; } set<P>::iterator erase(const set<P>::iterator it) { len -= length(*it); return s.erase(it); } void insert(const P &p) { len += length(p); s.insert(p); } set<P>::iterator lb(ll l) { return s.lower_bound({l, l}); } set<P>::iterator ub(ll l) { return lb(l + 1); } public: SegmentManager() : len(0) {} set<P> getset() { return s; } void add(ll l, ll r) { if (r <= l) return; erase(l, r); // merge right { auto it = lb(r); if (it != s.end() && it->first == r) { r = it->second; erase(it); } } // merge left { auto it = lb(l); if (it != s.begin()) { --it; if (it->second == l) { l = it->first; erase(it); } } } // add insert({l, r}); } void erase(ll l, ll r) { if (r <= l) return; // cut left { auto it = lb(l); if (it != s.begin()) { --it; if (it->second > l) { insert({it->first, l}); if (it->second > r) { insert({r, it->second}); erase(it); return; } erase(it); } } } auto it = lb(l); auto itr = lb(r); while (it != itr) { if (it->second > r) { insert({r, it->second}); it = erase(it); break; } it = erase(it); } } bool is_in(ll l, ll r) { assert(r >= l); auto it = ub(l); if (it == s.begin()) return false; --it; return it->first <= l && r <= it->second; } bool is_in(ll pos) { return is_in(pos, pos + 1); } ll length() { return len; } ll count() { return s.size(); } void out() { each(p, s) { cout << "[" << p.first << ", " << p.second << ")" << endl; } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, m; cin >> n >> m; vector<P> crane; rep(i, n) { ll x, y; cin >> x >> y; crane.pb({x, y - 1}); } sort(all(crane)); vector<SegmentManager> sm(n); rep(i, n) sm[i].add(i, i + 1); each(p, crane) { ll pos = p.second; each(pp, sm[pos + 1].getset()) { sm[pos].add(pp.first, pp.second); } sm[pos + 1] = sm[pos]; } vector<ll> ans; rep(i, n) ans.pb(sm[i].length()); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define each(i, a) for (auto &&i : a) #define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++) #define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME #define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__) #define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__) #define pb push_back #define all(a) (a).begin(), (a).end() #define chmin(x, v) x = min(x, v) #define chmax(x, v) x = max(x, v) const ll linf = 1e18; const int inf = 1e9; const double eps = 1e-12; const double pi = acos(-1); template <typename T> istream &operator>>(istream &is, vector<T> &vec) { each(x, vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { rep(i, vec.size()) { if (i) os << " "; os << vec[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &vec) { rep(i, vec.size()) { if (i) os << endl; os << vec[i]; } return os; } class SegmentManager { ll len; set<P> s; ll length(const P &p) { return p.second - p.first; } set<P>::iterator erase(const set<P>::iterator it) { len -= length(*it); return s.erase(it); } void insert(const P &p) { len += length(p); s.insert(p); } set<P>::iterator lb(ll l) { return s.lower_bound({l, l}); } set<P>::iterator ub(ll l) { return lb(l + 1); } public: SegmentManager() : len(0) {} set<P> getset() { return s; } void add(ll l, ll r) { if (r <= l) return; erase(l, r); // merge right { auto it = lb(r); if (it != s.end() && it->first == r) { r = it->second; erase(it); } } // merge left { auto it = lb(l); if (it != s.begin()) { --it; if (it->second == l) { l = it->first; erase(it); } } } // add insert({l, r}); } void erase(ll l, ll r) { if (r <= l) return; // cut left { auto it = lb(l); if (it != s.begin()) { --it; if (it->second > l) { insert({it->first, l}); if (it->second > r) { insert({r, it->second}); erase(it); return; } erase(it); } } } auto it = lb(l); auto itr = lb(r); while (it != itr) { if (it->second > r) { insert({r, it->second}); it = erase(it); break; } it = erase(it); } } bool is_in(ll l, ll r) { assert(r >= l); auto it = ub(l); if (it == s.begin()) return false; --it; return it->first <= l && r <= it->second; } bool is_in(ll pos) { return is_in(pos, pos + 1); } ll length() { return len; } ll count() { return s.size(); } void out() { each(p, s) { cout << "[" << p.first << ", " << p.second << ")" << endl; } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, m; cin >> n >> m; vector<P> crane; rep(i, m) { ll x, y; cin >> x >> y; crane.pb({x, y - 1}); } sort(all(crane)); // vector<ll> l(n), r(n); // rep(i, n) l[i] = i, r[i] = i+1; // each(p, crane) { // ll i = p.second; // l[i] = l[i+1] = min(l[i], l[i+1]); // r[i] = r[i+1] = max(r[i], r[i+1]); // } vector<SegmentManager> sm(n); rep(i, n) sm[i].add(i, i + 1); each(p, crane) { ll pos = p.second; each(pp, sm[pos + 1].getset()) { sm[pos].add(pp.first, pp.second); } sm[pos + 1] = sm[pos]; } vector<ll> ans; // rep(i, n) ans.pb(r[i]-l[i]); rep(i, n) ans.pb(sm[i].length()); cout << ans << endl; }
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78]]
1
1,073
#include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " = " << x << endl; #define ii pair<int, int> #define ll long long const int p1 = 1000000007; const int p2 = 1000000009; const int b = 300; const int ALPHA = 26; int pot1[ALPHA], pot2[ALPHA]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); pot1[0] = 1; pot2[0] = 1; for (int i = 1; i < ALPHA; i++) { pot1[i] = (1ll * pot1[i - 1] * b) % p1; pot2[i] = (1ll * pot2[i - 1] * b) % p2; } string sa, sb; cin >> sa >> sb; int na = (int)sa.size(), nb = (int)sb.size(); vector<ll> hashes; for (int i = 1; i <= na; i++) // tamanho da substring { int hash1 = 0, hash2 = 0; for (int j = 0; j < i; j++) { hash1 = (hash1 + pot1[sa[j] - 'a']) % p1; hash2 = (hash2 + pot2[sa[j] - 'a']) % p2; } hashes.push_back(1ll * p1 * hash1 + hash2); for (int j = i; j < na; j++) { hash1 = (hash1 + pot1[sa[j] - 'a']) % p1; hash1 = (hash1 - pot1[sa[j - i] - 'a'] + p1) % p1; hash2 = (hash2 + pot2[sa[j] - 'a']) % p2; hash2 = (hash2 - pot2[sa[j - i] - 'a'] + p2) % p2; hashes.push_back(1ll * p1 * hash1 + hash2); } } sort(hashes.begin(), hashes.end()); for (int i = nb; i >= 1; i--) { int hash1 = 0, hash2 = 0; for (int j = 0; j < i; j++) { hash1 = (hash1 + pot1[sb[j] - 'a']) % p1; hash2 = (hash2 + pot2[sb[j] - 'a']) % p2; } auto it = lower_bound(hashes.begin(), hashes.end(), 1ll * p1 * hash1 + hash2); if (*it == 1ll * p1 * hash1 + hash2) { cout << i << endl; return 0; } for (int j = i; j < na; j++) { hash1 = (hash1 + pot1[sb[j] - 'a']) % p1; hash1 = (hash1 - pot1[sb[j - i] - 'a'] + p1) % p1; hash2 = (hash2 + pot2[sb[j] - 'a']) % p2; hash2 = (hash2 - pot2[sb[j - i] - 'a'] + p2) % p2; auto it = lower_bound(hashes.begin(), hashes.end(), 1ll * p1 * hash1 + hash2); if (*it == 1ll * p1 * hash1 + hash2) { cout << i << endl; return 0; } } } cout << "0" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " = " << x << endl; #define ii pair<int, int> #define ll long long const int p1 = 1000000007; const int p2 = 1000000009; const int b = 300; const int ALPHA = 26; int pot1[ALPHA], pot2[ALPHA]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); pot1[0] = 1; pot2[0] = 1; for (int i = 1; i < ALPHA; i++) { pot1[i] = (1ll * pot1[i - 1] * b) % p1; pot2[i] = (1ll * pot2[i - 1] * b) % p2; } string sa, sb; cin >> sa >> sb; int na = (int)sa.size(), nb = (int)sb.size(); vector<ll> hashes; for (int i = 1; i <= na; i++) // tamanho da substring { int hash1 = 0, hash2 = 0; for (int j = 0; j < i; j++) { hash1 = (hash1 + pot1[sa[j] - 'a']) % p1; hash2 = (hash2 + pot2[sa[j] - 'a']) % p2; } hashes.push_back(1ll * p1 * hash1 + hash2); for (int j = i; j < na; j++) { hash1 = (hash1 + pot1[sa[j] - 'a']) % p1; hash1 = (hash1 - pot1[sa[j - i] - 'a'] + p1) % p1; hash2 = (hash2 + pot2[sa[j] - 'a']) % p2; hash2 = (hash2 - pot2[sa[j - i] - 'a'] + p2) % p2; hashes.push_back(1ll * p1 * hash1 + hash2); } } sort(hashes.begin(), hashes.end()); for (int i = nb; i >= 1; i--) { int hash1 = 0, hash2 = 0; for (int j = 0; j < i; j++) { hash1 = (hash1 + pot1[sb[j] - 'a']) % p1; hash2 = (hash2 + pot2[sb[j] - 'a']) % p2; } auto it = lower_bound(hashes.begin(), hashes.end(), 1ll * p1 * hash1 + hash2); if (*it == 1ll * p1 * hash1 + hash2) { cout << i << endl; return 0; } for (int j = i; j < nb; j++) { hash1 = (hash1 + pot1[sb[j] - 'a']) % p1; hash1 = (hash1 - pot1[sb[j - i] - 'a'] + p1) % p1; hash2 = (hash2 + pot2[sb[j] - 'a']) % p2; hash2 = (hash2 - pot2[sb[j - i] - 'a'] + p2) % p2; auto it = lower_bound(hashes.begin(), hashes.end(), 1ll * p1 * hash1 + hash2); if (*it == 1ll * p1 * hash1 + hash2) { cout << i << endl; return 0; } } } cout << "0" << endl; return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
713
#include <bits/stdc++.h> using namespace std; typedef int _loop_int; #define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i) #define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i) #define FORR(i, a, b) \ for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i) #define DEBUG(x) cout << #x << " : " << x << endl #define ALL(v) (v).begin(), (v).end() typedef vector<short> vi; int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); int ans = 0; FOR(l, 1, min(n, m) + 1) { set<vi> S; vi op(26, 0); REP(i, l - 1) op[s[i] - 'a']++; FOR(i, l - 1, n) { op[s[i] - 'a']++; S.insert(op); op[s[i + 1 - l] - 'a']--; } op = vi(26, 0); REP(i, l - 1) op[t[i] - 'a']++; FOR(i, l - 1, n) { op[t[i] - 'a']++; if (S.count(op)) { ans = l; break; } op[t[i + 1 - l] - 'a']--; } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef int _loop_int; #define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i) #define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i) #define FORR(i, a, b) \ for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i) #define DEBUG(x) cout << #x << " : " << x << endl #define ALL(v) (v).begin(), (v).end() typedef vector<short> vi; int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); int ans = 0; FOR(l, 1, min(n, m) + 1) { set<vi> S; vi op(26, 0); REP(i, l - 1) op[s[i] - 'a']++; FOR(i, l - 1, n) { op[s[i] - 'a']++; S.insert(op); op[s[i + 1 - l] - 'a']--; } op = vi(26, 0); REP(i, l - 1) op[t[i] - 'a']++; FOR(i, l - 1, m) { op[t[i] - 'a']++; if (S.count(op)) { ans = l; break; } op[t[i + 1 - l] - 'a']--; } } printf("%d\n", ans); return 0; }
[["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
303
#include <bits/stdc++.h> using namespace std; using ll = __int128; const ll b = 1713302033171ll; ll Pow(ll x, ll n) { ll res = 1; while (n) { if (n & 1) { res = (res * x) % b; } x = (x * x) % b; n >>= 1; } return res; } int main() { vector<ll> prime; for (ll i = 100000; prime.size() < 26; i++) { bool flag = true; for (ll j = 2; j * j <= i; j++) { if (i % j == 0) { flag = false; break; } } if (flag) { prime.push_back(i); } } string s1, s2; cin >> s1 >> s2; int n1 = s1.size(), n2 = s2.size(); vector<ll> rh1(n1 + 1), rrh1(n1 + 1), rh2(n2 + 1), rrh2(n2 + 1); rh1[0] = rrh1[0] = rh2[0] = rrh2[0] = 1; for (int i = 1; i <= n1; i++) { rh1[i] = (rh1[i - 1] * prime[s1[i - 1] - 'a']) % b; rrh1[i] = Pow(rh1[i], b - 1); } for (int i = 1; i <= n2; i++) { rh2[i] = (rh2[i - 1] * prime[s2[i - 1] - 'a']) % b; rrh2[i] = Pow(rh2[i], b - 1); } bool fini = true; for (int k = min(n1, n2); k > 0; k--) { bool can = false; vector<ll> hs(n1 - k + 1); for (int i = 0; i + k <= n1; i++) { hs[i] = (rrh1[i] * rh1[i + k]) % b; } sort(hs.begin(), hs.end()); for (int j = 0; j + k <= n2; j++) { if (binary_search(hs.begin(), hs.end(), (rrh2[j] * rh2[j + k]) % b)) { can = true; break; } } if (can) { fini = false; cout << k << endl; break; } } if (fini) { cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = __int128; const ll b = 1713302033171ll; ll Pow(ll x, ll n) { ll res = 1; while (n) { if (n & 1) { res = (res * x) % b; } x = (x * x) % b; n >>= 1; } return res; } int main() { vector<ll> prime; for (ll i = 100000; prime.size() < 26; i++) { bool flag = true; for (ll j = 2; j * j <= i; j++) { if (i % j == 0) { flag = false; break; } } if (flag) { prime.push_back(i); } } string s1, s2; cin >> s1 >> s2; int n1 = s1.size(), n2 = s2.size(); vector<ll> rh1(n1 + 1), rrh1(n1 + 1), rh2(n2 + 1), rrh2(n2 + 1); rh1[0] = rrh1[0] = rh2[0] = rrh2[0] = 1; for (int i = 1; i <= n1; i++) { rh1[i] = (rh1[i - 1] * prime[s1[i - 1] - 'a']) % b; rrh1[i] = Pow(rh1[i], b - 2); } for (int i = 1; i <= n2; i++) { rh2[i] = (rh2[i - 1] * prime[s2[i - 1] - 'a']) % b; rrh2[i] = Pow(rh2[i], b - 2); } bool fini = true; for (int k = min(n1, n2); k > 0; k--) { bool can = false; vector<ll> hs(n1 - k + 1); for (int i = 0; i + k <= n1; i++) { hs[i] = (rrh1[i] * rh1[i + k]) % b; } sort(hs.begin(), hs.end()); for (int j = 0; j + k <= n2; j++) { if (binary_search(hs.begin(), hs.end(), (rrh2[j] * rh2[j + k]) % b)) { can = true; break; } } if (can) { fini = false; cout << k << endl; break; } } if (fini) { cout << 0 << endl; } return 0; }
[["-", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
1
556
// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ? #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++) #define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--) #define int long long int template <typename T> void chmax(T &a, T b) { a = max(a, b); } template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chadd(T &a, T b) { a = a + b; } typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; int hash_num = 0; map<vector<int>, int> hash_s; signed main() { string s, t; cin >> s >> t; int len = min(s.length(), t.length()); int ans = 0; repq(i, 1, len) { hash_s.clear(); hash_num = 0; int cnt = 0; vector<int> app_s(26), app_t(26); rep(k, 0, s.length()) { int val = s[k] - 'a'; app_s[val]++; cnt++; if (cnt == i) { if (!hash_s.count(app_s)) { hash_s[app_s] = hash_num++; } int prev = k - i + 1; int prev_val = s[prev] - 'a'; app_s[prev_val]--; cnt--; } } cnt = 0; rep(k, 0, s.length()) { int val = t[k] - 'a'; app_t[val]++; cnt++; if (cnt == i) { if (hash_s.count(app_t)) chmax(ans, i); int prev = k - i + 1; int prev_val = t[prev] - 'a'; app_t[prev_val]--; cnt--; } } } cout << ans << endl; return 0; }
// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ? #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++) #define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--) #define int long long int template <typename T> void chmax(T &a, T b) { a = max(a, b); } template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chadd(T &a, T b) { a = a + b; } typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; int hash_num = 0; map<vector<int>, int> hash_s; signed main() { string s, t; cin >> s >> t; int len = min(s.length(), t.length()); int ans = 0; repq(i, 1, len) { hash_s.clear(); hash_num = 0; int cnt = 0; vector<int> app_s(26), app_t(26); rep(k, 0, s.length()) { int val = s[k] - 'a'; app_s[val]++; cnt++; if (cnt == i) { if (!hash_s.count(app_s)) { hash_s[app_s] = hash_num++; } int prev = k - i + 1; int prev_val = s[prev] - 'a'; app_s[prev_val]--; cnt--; } } cnt = 0; rep(k, 0, t.length()) { int val = t[k] - 'a'; app_t[val]++; cnt++; if (cnt == i) { if (hash_s.count(app_t)) chmax(ans, i); int prev = k - i + 1; int prev_val = t[prev] - 'a'; app_t[prev_val]--; cnt--; } } } cout << ans << endl; return 0; }
[["-", 0, 2, 3, 4, 0, 2, 63, 118, 28, 22], ["+", 0, 2, 3, 4, 0, 2, 63, 118, 28, 22]]
1
510
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; vector<vector<short>> vec(t.size() + 1, vector<short>(26)); for (int i = 1; i <= t.size(); i++) { vec[i][t[i - 1] - 'a']++; for (int j = 0; j < 26; j++) { vec[i][j] += vec[i - 1][j]; } } vector<vector<short>> v(s.size() + 1, vector<short>(26)); for (int i = 1; i <= s.size(); i++) { v[i][s[i - 1] - 'a']++; for (int j = 0; j < 26; j++) { v[i][j] += v[i - 1][j]; } } for (int i = s.size(); i > 1; i--) { set<vector<short>> sv; for (int j = 0; j + i < vec.size(); j++) { vector<short> sub(26); for (int k = 0; k < 26; k++) { sub[k] += vec[i + j][k] - vec[j][k]; } sv.insert(sub); } for (int j = 0; j + i < v.size(); j++) { vector<short> sub(26); for (int k = 0; k < 26; k++) { sub[k] = v[i + j][k] - v[j][k]; } if (sv.find(sub) != sv.end()) { cout << i << endl; return 0; } } } cout << 0 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; vector<vector<short>> vec(t.size() + 1, vector<short>(26)); for (int i = 1; i <= t.size(); i++) { vec[i][t[i - 1] - 'a']++; for (int j = 0; j < 26; j++) { vec[i][j] += vec[i - 1][j]; } } vector<vector<short>> v(s.size() + 1, vector<short>(26)); for (int i = 1; i <= s.size(); i++) { v[i][s[i - 1] - 'a']++; for (int j = 0; j < 26; j++) { v[i][j] += v[i - 1][j]; } } for (int i = s.size(); i > 0; i--) { set<vector<short>> sv; for (int j = 0; j + i < vec.size(); j++) { vector<short> sub(26); for (int k = 0; k < 26; k++) { sub[k] += vec[i + j][k] - vec[j][k]; } sv.insert(sub); } for (int j = 0; j + i < v.size(); j++) { vector<short> sub(26); for (int k = 0; k < 26; k++) { sub[k] = v[i + j][k] - v[j][k]; } if (sv.find(sub) != sv.end()) { cout << i << endl; return 0; } } } cout << 0 << endl; return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
1
430
#include <algorithm> #include <assert.h> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef int Def; typedef pair<Def, Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def, pii> pip; typedef vector<pip> vip; //#define mt make_tuple // typedef tuple<double,int,double> tp; // typedef vector<tp> vt; template <typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } template <typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; } const double PI = acos(-1); const double EPS = 1e-7; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #define MAX 27 int ash[2][MAX] = {0}; int MOD[2] = {1000000009, 1000000007}; int x[2] = {1000000007, 1000000003}; int power[2][MAX]; int main() { string s, t; cin >> s >> t; if (s.size() > t.size()) swap(s, t); rep(i, 2) { power[i][0] = 1; rep(j, MAX) power[i][j + 1] = (ll)power[i][j] * x[i] % MOD[i]; } for (int n = s.size(); n > 0; n--) { map<pii, int> ma; vi co(26); rep(i, n) co[s[i] - 'a']++; rep(p, s.size() - n + 1) { pii has; rep(i, 2) { int ha = 0; rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i]; if (i) has.second = ha; else has.first = ha; } ma[has] = true; if (p == s.size() - n) break; co[s[n + p] - 'a']++; co[s[p] - 'a']--; } co = vi(26); rep(i, n) co[t[i] - 'a']++; bool h = false; rep(p, t.size() - n + 1) { pii has; rep(i, 1) { int ha = 0; rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i]; if (i) has.second = ha; else has.first = ha; } if (ma.count(has)) { h = true; break; } if (p == t.size() - n) break; co[t[n + p] - 'a']++; co[t[p] - 'a']--; } if (h) { cout << n << endl; return 0; } } cout << 0 << endl; }
#include <algorithm> #include <assert.h> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef int Def; typedef pair<Def, Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def, pii> pip; typedef vector<pip> vip; //#define mt make_tuple // typedef tuple<double,int,double> tp; // typedef vector<tp> vt; template <typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } template <typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; } const double PI = acos(-1); const double EPS = 1e-7; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #define MAX 27 int ash[2][MAX] = {0}; int MOD[2] = {1145141919, 1145148109}; int x[2] = {381038107, 524524524}; int power[2][MAX]; int main() { string s, t; cin >> s >> t; if (s.size() > t.size()) swap(s, t); rep(i, 2) { power[i][0] = 1; rep(j, MAX) power[i][j + 1] = (ll)power[i][j] * x[i] % MOD[i]; } for (int n = s.size(); n > 0; n--) { map<pii, int> ma; vi co(26); rep(i, n) co[s[i] - 'a']++; rep(p, s.size() - n + 1) { pii has; rep(i, 2) { int ha = 0; rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i]; if (i) has.second = ha; else has.first = ha; } ma[has] = true; if (p == s.size() - n) break; co[s[n + p] - 'a']++; co[s[p] - 'a']--; } co = vi(26); rep(i, n) co[t[i] - 'a']++; bool h = false; rep(p, t.size() - n + 1) { pii has; rep(i, 2) { int ha = 0; rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i]; if (i) has.second = ha; else has.first = ha; } // cout<<has.first<<" "<<has.second<<endl; if (ma.count(has)) { h = true; break; } if (p == t.size() - n) break; co[t[n + p] - 'a']++; co[t[p] - 'a']--; } if (h) { cout << n << endl; return 0; } } cout << 0 << endl; }
[["-", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
821
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); const double INF = 1e9; int main() { cin.tie(0); ios_base::sync_with_stdio(false); double D; int N, B; cin >> D >> N >> B; vector<double> P(N), H(N); REP(i, N) cin >> P[i] >> H[i]; double ans = INF; for (int bi = 0; bi <= B; ++bi) { double span = D / (bi + 1); double vy = 0.; for (int i = 0; i < N; ++i) { double pos = P[i]; while (pos > span) pos -= span; if (pos > span / 2) pos = span - pos; double t = pos / span; vy = max(vy, sqrt(H[i] / (2 * t * (1. - t)))); } double vx = span / (2 * vy); if (vx >= vy) ans = sqrt(D); ans = min(ans, sqrt(vx * vx + vy * vy)); } cout << fixed << setprecision(9) << ans << endl; return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); const double INF = 1e9; int main() { cin.tie(0); ios_base::sync_with_stdio(false); double D; int N, B; cin >> D >> N >> B; vector<double> P(N), H(N); REP(i, N) cin >> P[i] >> H[i]; double ans = INF; for (int bi = 0; bi <= B; ++bi) { double span = D / (bi + 1); double vy = 0.; for (int i = 0; i < N; ++i) { double pos = P[i]; while (pos > span) pos -= span; if (pos > span / 2) pos = span - pos; double t = pos / span; vy = max(vy, sqrt(H[i] / (2 * t * (1. - t)))); } double vx = span / (2 * vy); if (vx >= vy) ans = sqrt(span); ans = min(ans, sqrt(vx * vx + vy * vy)); } cout << fixed << setprecision(9) << ans << endl; return 0; }
[["-", 64, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 64, 1, 0, 11, 12, 2, 3, 4, 0, 22]]
1
427
#include <cmath> #include <iostream> #include <stdio.h> #define eps 1.0e-10 using namespace std; double d; int n, b; int p[15], h[15]; double coe[3]; void Lagrange(double x1, double y1, double x2, double y2, double x3, double y3) { double x[] = {x1, x2, x3}, y[] = {y1, y2, y3}; double l[3][3]; for (int i = 0; i < 3; i++) { l[i][0] = 1; l[i][1] = 0; for (int j = 0; j < 3; j++) if (i != j) l[i][1] -= x[j]; l[i][2] = 1; for (int j = 0; j < 3; j++) if (i != j) l[i][2] *= x[j]; double bunbo = 1; for (int j = 0; j < 3; j++) if (i != j) bunbo *= (x[i] - x[j]); for (int j = 0; j < 3; j++) l[i][j] /= bunbo; } for (int i = 0; i < 3; i++) { coe[i] = 0; for (int j = 0; j < 3; j++) { coe[i] += y[j] * l[j][i]; } } } int main(void) { cin >> d >> n >> b; for (int i = 0; i < n; i++) cin >> p[i] >> h[i]; double ans = 1.0e10; double l, r; for (int i = 0; i < n; i++) { for (int j = 0; j <= b; j++) { for (int k = 0; k < j; k++) { l = d / (j + 1) * k; r = d / (j + 1) * (k + 1); if (p[i] <= l || p[i] >= r) continue; Lagrange(0, 0, r - l, 0, p[i] - l, h[i]); bool flag = true; for (int z = 0; z < n; z++) { int no = p[z] / (r - l); double x = p[z] - no * (r - l); double y = coe[0] * x * x + coe[1] * x + coe[2]; if (y < h[z] - eps) { flag = false; break; } } if (flag) { double th = atan(coe[1]); double v = sqrt(-0.5 / (coe[0] * cos(th) * cos(th))); ans = min(ans, v); } } } } for (int j = 0; j <= b; j++) { l = 0.0; r = d / (j + 1); coe[0] = -1.0 / (r - l); coe[1] = 1.0; coe[2] = 0.0; bool flag = true; for (int z = 0; z < n; z++) { int no = p[z] / (r - l); double x = p[z] - no * (r - l); double y = coe[0] * x * x + coe[1] * x + coe[2]; if (y < h[z] - eps) { flag = false; break; } } if (flag) { double th = atan(coe[1]); double v = sqrt(-0.5 / (coe[0] * cos(th) * cos(th))); ans = min(ans, v); } } printf("%.10f\n", ans); return 0; }
#include <cmath> #include <iostream> #include <stdio.h> #define eps 1.0e-8 using namespace std; double d; int n, b; int p[15], h[15]; double coe[3]; void Lagrange(double x1, double y1, double x2, double y2, double x3, double y3) { double x[] = {x1, x2, x3}, y[] = {y1, y2, y3}; double l[3][3]; for (int i = 0; i < 3; i++) { l[i][0] = 1; l[i][1] = 0; for (int j = 0; j < 3; j++) if (i != j) l[i][1] -= x[j]; l[i][2] = 1; for (int j = 0; j < 3; j++) if (i != j) l[i][2] *= x[j]; double bunbo = 1; for (int j = 0; j < 3; j++) if (i != j) bunbo *= (x[i] - x[j]); for (int j = 0; j < 3; j++) l[i][j] /= bunbo; } for (int i = 0; i < 3; i++) { coe[i] = 0; for (int j = 0; j < 3; j++) { coe[i] += y[j] * l[j][i]; } } } int main(void) { cin >> d >> n >> b; for (int i = 0; i < n; i++) cin >> p[i] >> h[i]; double ans = 1.0e10; double l, r; for (int i = 0; i < n; i++) { for (int j = 0; j <= b; j++) { for (int k = 0; k <= j; k++) { l = d / (j + 1) * k; r = d / (j + 1) * (k + 1); if (p[i] <= l || p[i] >= r) continue; Lagrange(0, 0, r - l, 0, p[i] - l, h[i]); bool flag = true; for (int z = 0; z < n; z++) { int no = p[z] / (r - l); double x = p[z] - no * (r - l); double y = coe[0] * x * x + coe[1] * x + coe[2]; if (y < h[z] - eps) { flag = false; break; } } if (flag) { double th = atan(coe[1]); double v = sqrt(-0.5 / (coe[0] * cos(th) * cos(th))); ans = min(ans, v); } } } } for (int j = 0; j <= b; j++) { l = 0.0; r = d / (j + 1); coe[0] = -1.0 / (r - l); coe[1] = 1.0; coe[2] = 0.0; bool flag = true; for (int z = 0; z < n; z++) { int no = p[z] / (r - l); double x = p[z] - no * (r - l); double y = coe[0] * x * x + coe[1] * x + coe[2]; if (y < h[z] - eps) { flag = false; break; } } if (flag) { double th = atan(coe[1]); double v = sqrt(-0.5 / (coe[0] * cos(th) * cos(th))); ans = min(ans, v); } } printf("%.8f\n", ans); return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
857
#include <bits/stdc++.h> using namespace std; #define r(i, n) for (int i = 0; i < n; i++) typedef pair<int, int> P; #define F first #define S second int n, m, a1, a2, a3; int par[10001]; int rak[10001]; void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; rak[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unit(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rak[x] < rak[y]) { par[x] = y; } else { par[y] = x; if (rak[x] == rak[y]) rak[x]++; } } bool same(int x, int y) { return find(x) == find(y); } struct edge { int u, v, cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } edge es[100001], e; int ans; vector<edge> v; int V, E; int kruskal() { sort(es, es + E, comp); init(V); int res = 0; for (int i = 0; i < E; i++) { edge e = es[i]; if (!same(e.u, e.v)) { unit(e.u, e.v); v.push_back(e); res += e.cost; } } ans = res; return res; } int kruskal2(edge now) { sort(es, es + E, comp); init(V); int res = 0, res1 = 0; for (int i = 0; i < E; i++) { edge e = es[i]; if (e.v == now.v && e.u == now.u && e.cost == now.cost) continue; if (!same(e.u, e.v)) { unit(e.u, e.v); res++; res1 += e.cost; } } return res == n - 1 && ans == res1; } int main() { int ans1 = 0, ans2 = 0; cin >> n >> m; r(i, m) { cin >> a1 >> a2 >> a3; e.u = a1, e.v = a2, e.cost = a3; es[i] = e; } E = m; V = n; kruskal(); r(i, v.size()) { if (kruskal2(v[i])) { // cout<<v[i].u<<endl; ans1++; ans2 += v[i].cost; } } cout << ans1 << ' ' << ans2 << endl; }
#include <bits/stdc++.h> using namespace std; #define r(i, n) for (int i = 0; i < n; i++) typedef pair<int, int> P; #define F first #define S second int n, m, a1, a2, a3; int par[10001]; int rak[10001]; void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; rak[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unit(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rak[x] < rak[y]) { par[x] = y; } else { par[y] = x; if (rak[x] == rak[y]) rak[x]++; } } bool same(int x, int y) { return find(x) == find(y); } struct edge { int u, v, cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } edge es[100001], e; int ans; vector<edge> v; int V, E; int kruskal() { sort(es, es + E, comp); init(V); int res = 0; for (int i = 0; i < E; i++) { edge e = es[i]; if (!same(e.u, e.v)) { unit(e.u, e.v); v.push_back(e); res += e.cost; } } ans = res; return res; } int kruskal2(edge now) { sort(es, es + E, comp); init(V); int res = 0, res1 = 0; for (int i = 0; i < E; i++) { edge e = es[i]; if (e.v == now.v && e.u == now.u && e.cost == now.cost) continue; if (!same(e.u, e.v)) { unit(e.u, e.v); res++; res1 += e.cost; } } return res != n - 1 || ans != res1; } int main() { int ans1 = 0, ans2 = 0; cin >> n >> m; r(i, m) { cin >> a1 >> a2 >> a3; e.u = a1, e.v = a2, e.cost = a3; es[i] = e; } E = m; V = n + 1; kruskal(); r(i, v.size()) { if (kruskal2(v[i])) { // cout<<v[i].u<<endl; ans1++; ans2 += v[i].cost; } } cout << ans1 << ' ' << ans2 << endl; }
[["-", 8, 9, 0, 37, 0, 16, 31, 16, 17, 60], ["+", 8, 9, 0, 37, 0, 16, 31, 16, 17, 79], ["-", 0, 14, 8, 9, 0, 37, 0, 16, 17, 98], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 17, 106], ["-", 8, 9, 0, 37, 0, 16, 12, 16, 17, 60], ["+", 8, 9, 0, 37, 0, 16, 12, 16, 17, 79], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
645
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, pa> #define ppap pair<pa, int> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; using namespace std; class pa3 { public: int x, y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: double x; int y, z, w; pa4(double x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; #define ppa pair<int, pas> class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; #define pl pair<int, pas> struct Segment { Point p1, p2; }; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool parareru(Point a, Point b, Point c, Point d) { // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a - b, d - c)) < EPS; } double distance_ls_p(Point a, Point b, Point c) { if (dot(b - a, c - a) < EPS) return (c - a).absv(); if (dot(a - b, c - b) < EPS) return (c - b).absv(); return abs(cross(b - a, c - a)) / (b - a).absv(); } bool is_intersected_ls(Segment a, Segment b) { if (a.p1 == b.p1 || a.p2 == b.p1 || a.p1 == b.p2 || a.p2 == b.p2) return false; if (parareru((a.p2), (a.p1), (a.p1), (b.p2)) && parareru((a.p2), (a.p1), (a.p1), (b.p1))) { // cout<<"sss"<<endl; if (dot(a.p1 - b.p1, a.p1 - b.p2) < EPS) return true; if (dot(a.p2 - b.p1, a.p2 - b.p2) < EPS) return true; if (dot(a.p1 - b.p1, a.p2 - b.p1) < EPS) return true; if (dot(a.p1 - b.p2, a.p2 - b.p2) < EPS) return true; return false; } else return (cross(a.p2 - a.p1, b.p1 - a.p1) * cross(a.p2 - a.p1, b.p2 - a.p1) < EPS) && (cross(b.p2 - b.p1, a.p1 - b.p1) * cross(b.p2 - b.p1, a.p2 - b.p1) < EPS); } double segment_dis(Segment a, Segment b) { if (is_intersected_ls(a, b)) return 0; double r = distance_ls_p(a.p1, a.p2, b.p1); r = min(r, distance_ls_p(a.p1, a.p2, b.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2 - b.p1; double d1 = abs(cross(ba, a.p1 - b.p1)); double d2 = abs(cross(ba, a.p2 - b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2 - a.p1) * t; } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } // int pr[2000010]; // int inv[2000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1ll; if (rr == 1) return wa % warukazu; if (rr % 2 == 1) return (beki(wa, rr - 1, warukazu) * wa) % warukazu; int zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } /* int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=inf; r*=inv[nn-rr]; r%=inf; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<ert;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<ert;i++) inv[i]=beki(pr[i],inf-2,inf); } */ // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ // vector<double> ans(100000000),ans2(100000000); int par[200100], ranks[200100], kosuu[200100]; void shoki(int n) { for (int i = 0; i < n; i++) { par[i] = i; ranks[i] = 0; kosuu[i] = 1; } } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); int xx = kosuu[x], yy = kosuu[y]; if (x == y) return; if (ranks[x] < ranks[y]) { par[x] = y; kosuu[y] = yy + xx; } else { par[y] = x; if (ranks[x] == ranks[y]) ranks[x] = ranks[x] + 1; kosuu[x] = yy + xx; } return; } vector<int> ans; vector<pa3> ve; signed main() { int n, m; cin >> n >> m; shoki(n + 10000); for (int i = 0; i < m; i++) { int y, yy, c; cin >> y >> yy >> c; ve.pb({c, y, yy}); } sort(ve.begin(), ve.end()); int zenbu = 0; for (int i = 0; i < m; i++) { if (!same(ve[i].y, ve[i].z)) { unite(ve[i].y, ve[i].z); ans.pb(i); zenbu += ve[i].x; } } int a = 0, b = 0; for (int k = 0; k < ans.size(); k++) { shoki(n + 100); int zenbu1 = 0; int kz = 0; for (int i = 0; i < m; i++) if (i != ans[k]) { if (!same(ve[i].y, ve[i].z)) { unite(ve[i].y, ve[i].z); kz++; zenbu1 += ve[i].x; } } if (kz == n - 1 && zenbu1 == zenbu) { } else { a++; b += ve[k].x; } } cout << a << " " << b << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, pa> #define ppap pair<pa, int> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; using namespace std; class pa3 { public: int x, y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: double x; int y, z, w; pa4(double x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; #define ppa pair<int, pas> class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; #define pl pair<int, pas> struct Segment { Point p1, p2; }; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool parareru(Point a, Point b, Point c, Point d) { // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a - b, d - c)) < EPS; } double distance_ls_p(Point a, Point b, Point c) { if (dot(b - a, c - a) < EPS) return (c - a).absv(); if (dot(a - b, c - b) < EPS) return (c - b).absv(); return abs(cross(b - a, c - a)) / (b - a).absv(); } bool is_intersected_ls(Segment a, Segment b) { if (a.p1 == b.p1 || a.p2 == b.p1 || a.p1 == b.p2 || a.p2 == b.p2) return false; if (parareru((a.p2), (a.p1), (a.p1), (b.p2)) && parareru((a.p2), (a.p1), (a.p1), (b.p1))) { // cout<<"sss"<<endl; if (dot(a.p1 - b.p1, a.p1 - b.p2) < EPS) return true; if (dot(a.p2 - b.p1, a.p2 - b.p2) < EPS) return true; if (dot(a.p1 - b.p1, a.p2 - b.p1) < EPS) return true; if (dot(a.p1 - b.p2, a.p2 - b.p2) < EPS) return true; return false; } else return (cross(a.p2 - a.p1, b.p1 - a.p1) * cross(a.p2 - a.p1, b.p2 - a.p1) < EPS) && (cross(b.p2 - b.p1, a.p1 - b.p1) * cross(b.p2 - b.p1, a.p2 - b.p1) < EPS); } double segment_dis(Segment a, Segment b) { if (is_intersected_ls(a, b)) return 0; double r = distance_ls_p(a.p1, a.p2, b.p1); r = min(r, distance_ls_p(a.p1, a.p2, b.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2 - b.p1; double d1 = abs(cross(ba, a.p1 - b.p1)); double d2 = abs(cross(ba, a.p2 - b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2 - a.p1) * t; } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } // int pr[2000010]; // int inv[2000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1ll; if (rr == 1) return wa % warukazu; if (rr % 2 == 1) return (beki(wa, rr - 1, warukazu) * wa) % warukazu; int zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } /* int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=inf; r*=inv[nn-rr]; r%=inf; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<ert;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<ert;i++) inv[i]=beki(pr[i],inf-2,inf); } */ // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ // vector<double> ans(100000000),ans2(100000000); int par[200100], ranks[200100], kosuu[200100]; void shoki(int n) { for (int i = 0; i < n; i++) { par[i] = i; ranks[i] = 0; kosuu[i] = 1; } } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); int xx = kosuu[x], yy = kosuu[y]; if (x == y) return; if (ranks[x] < ranks[y]) { par[x] = y; kosuu[y] = yy + xx; } else { par[y] = x; if (ranks[x] == ranks[y]) ranks[x] = ranks[x] + 1; kosuu[x] = yy + xx; } return; } vector<int> ans; vector<pa3> ve; signed main() { int n, m; cin >> n >> m; shoki(n + 10000); for (int i = 0; i < m; i++) { int y, yy, c; cin >> y >> yy >> c; ve.pb({c, y, yy}); } sort(ve.begin(), ve.end()); int zenbu = 0; for (int i = 0; i < m; i++) { if (!same(ve[i].y, ve[i].z)) { unite(ve[i].y, ve[i].z); ans.pb(i); zenbu += ve[i].x; // cout<<" "<<ve[i].x<<endl; } } // cout<<zenbu<<endl; int a = 0, b = 0; for (int k = 0; k < ans.size(); k++) { shoki(n + 100); int zenbu1 = 0; int kz = 0; for (int i = 0; i < m; i++) if (i != ans[k]) { if (!same(ve[i].y, ve[i].z)) { unite(ve[i].y, ve[i].z); kz++; zenbu1 += ve[i].x; } } // cout<<kz<<" "<<zenbu1<<endl; if (kz == n - 1 && zenbu1 == zenbu) { } else { a++; // cout<<k<<endl; b += ve[ans[k]].x; } } cout << a << " " << b << endl; return 0; }
[["+", 12, 118, 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]]
1
2,442
#include <bits/stdc++.h> using namespace std; struct T; #define null ((T *)NULL) typedef int ll; struct T { ll v; ll m; int c; T *l, *r; bool lz; ll lazy; T() {} T(ll v) : v(v), m(v), c(1), l(null), r(null), lz(false), lazy(0) {} }; int hogeS = 0; #define SSS 2222222 T hoge[SSS]; inline T *New(int v) { if (hogeS >= SSS) exit(-1); hoge[hogeS] = T(v); return &hoge[hogeS++]; } inline int count(T *t) { return t ? t->c : 0; } inline void push(T *t) { if (!t) return; if (t->lz) { t->v += t->lazy; t->m += t->lazy; t->lz = false; if (t->r) { t->r->lz |= true; t->r->lazy += t->lazy; } if (t->l) { t->l->lz |= true; t->l->lazy += t->lazy; } t->lazy = 0; } } inline ll mini(T *t) { push(t); return t ? t->m : LLONG_MAX; } inline T *update(T *t) { if (!t) return t; push(t); t->c = count(t->l) + count(t->r) + 1; t->m = min(t->v, min(mini(t->l), mini(t->r))); return t; } T *merge(T *a, T *b) { push(a); push(b); if (!a || !b) return a ? a : b; if (rand() % (count(a) + count(b)) < count(a)) { a->r = merge(a->r, b); return update(a); } else { b->l = merge(a, b->l); return update(b); } } pair<T *, T *> split(T *a, int k) { push(a); if (!a) return make_pair(null, null); if (k <= count(a->l)) { pair<T *, T *> s = split(a->l, k); a->l = s.second; return make_pair(s.first, update(a)); } else { pair<T *, T *> s = split(a->r, k - count(a->l) - 1); a->r = s.first; return make_pair(update(a), s.second); } } inline T *insert(T *t, int k, int v) { push(t); pair<T *, T *> a = split(t, k); return merge(merge(a.first, New(v)), a.second); } inline T *add(T *t, int l, int r, int k) { push(t); pair<T *, T *> u = split(t, r); pair<T *, T *> v = split(u.first, l); T *w = v.second; w->lz = true; w->lazy += k; return merge(merge(v.first, update(w)), u.second); } int n, m; char s[333333]; void print(T *t) { if (t == null) return; push(t); print(t->l); printf("%2lld,%2lld|", t->v, t->m); print(t->r); } int main(void) { T *root = null; int n, q; set<int> toji; scanf("%d%d", &n, &q); scanf("%s", s); for (int i = 0; i < n; i++) { root = insert(root, i, 0); } for (int i = 0; i < n; i++) { root = add(root, i, n, s[i] == '(' ? 1 : -1); if (s[i] == ')') toji.insert(i); } int k; for (int ii = 0; ii < q; ii++) { scanf("%d", &k); if (s[k - 1] == '(') { s[k - 1] = ')'; toji.insert(k - 1); root = add(root, k - 1, n, -2); int me = *(toji.begin()); printf("%d\n", me + 1); s[me] = '('; toji.erase(me); root = add(root, me, n, 2); } else { toji.erase(k - 1); s[k - 1] = '('; root = add(root, k - 1, n, 2); int l = 0, r = n; while (l < r) { int mb = (l + r) / 2; auto t = split(root, mb); if (mini(t.second) >= 2) { r = mb; } else { l = mb + 1; } root = merge(t.first, t.second); } printf("%d\n", l + 1); s[l] = ')'; toji.insert(l); root = add(root, l, n, -2); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct T; #define null ((T *)NULL) typedef int ll; struct T { ll v; ll m; int c; T *l, *r; bool lz; ll lazy; T() {} T(ll v) : v(v), m(v), c(1), l(null), r(null), lz(false), lazy(0) {} }; int hogeS = 0; #define SSS 2222222 T hoge[SSS]; inline T *New(int v) { if (hogeS >= SSS) exit(-1); hoge[hogeS] = T(v); return &hoge[hogeS++]; } inline int count(T *t) { return t ? t->c : 0; } inline void push(T *t) { if (!t) return; if (t->lz) { t->v += t->lazy; t->m += t->lazy; t->lz = false; if (t->r) { t->r->lz |= true; t->r->lazy += t->lazy; } if (t->l) { t->l->lz |= true; t->l->lazy += t->lazy; } t->lazy = 0; } } inline ll mini(T *t) { push(t); return t ? t->m : INT_MAX; } inline T *update(T *t) { if (!t) return t; push(t); t->c = count(t->l) + count(t->r) + 1; t->m = min(t->v, min(mini(t->l), mini(t->r))); return t; } T *merge(T *a, T *b) { push(a); push(b); if (!a || !b) return a ? a : b; if (rand() % (count(a) + count(b)) < count(a)) { a->r = merge(a->r, b); return update(a); } else { b->l = merge(a, b->l); return update(b); } } pair<T *, T *> split(T *a, int k) { push(a); if (!a) return make_pair(null, null); if (k <= count(a->l)) { pair<T *, T *> s = split(a->l, k); a->l = s.second; return make_pair(s.first, update(a)); } else { pair<T *, T *> s = split(a->r, k - count(a->l) - 1); a->r = s.first; return make_pair(update(a), s.second); } } inline T *insert(T *t, int k, int v) { push(t); pair<T *, T *> a = split(t, k); return merge(merge(a.first, New(v)), a.second); } inline T *add(T *t, int l, int r, int k) { push(t); pair<T *, T *> u = split(t, r); pair<T *, T *> v = split(u.first, l); T *w = v.second; w->lz = true; w->lazy += k; return merge(merge(v.first, update(w)), u.second); } int n, m; char s[333333]; void print(T *t) { if (t == null) return; push(t); print(t->l); printf("%2lld,%2lld|", t->v, t->m); print(t->r); } int main(void) { T *root = null; int n, q; set<int> toji; scanf("%d%d", &n, &q); scanf("%s", s); for (int i = 0; i < n; i++) { root = insert(root, i, 0); } for (int i = 0; i < n; i++) { root = add(root, i, n, s[i] == '(' ? 1 : -1); if (s[i] == ')') toji.insert(i); } int k; for (int ii = 0; ii < q; ii++) { scanf("%d", &k); if (s[k - 1] == '(') { s[k - 1] = ')'; toji.insert(k - 1); root = add(root, k - 1, n, -2); int me = *(toji.begin()); printf("%d\n", me + 1); s[me] = '('; toji.erase(me); root = add(root, me, n, 2); } else { toji.erase(k - 1); s[k - 1] = '('; root = add(root, k - 1, n, 2); int l = 0, r = n; while (l < r) { int mb = (l + r) / 2; auto t = split(root, mb); if (mini(t.second) >= 2) { r = mb; } else { l = mb + 1; } root = merge(t.first, t.second); } printf("%d\n", l + 1); s[l] = ')'; toji.insert(l); root = add(root, l, n, -2); } } return 0; }
[["-", 0, 14, 8, 9, 0, 37, 0, 41, 75, 22], ["+", 0, 14, 8, 9, 0, 37, 0, 41, 75, 22]]
1
1,257
#include <stdio.h> int main(void) { int n, a, i, ii, y, s[2048]; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a); s[i] = a; } for (i = 0; i <= 9; i++) { y = 0; for (ii = 1; ii <= n; ii++) { if (s[n] == i) { y = 1; break; } } if (y == 0) { printf("%d\n", i); return 0; } } for (i = 10; i <= 99; i++) { y = 0; for (ii = 1; ii <= n - 1; ii++) { if (s[n] == i / 10 && s[n + 1] == i % 10) { y = 1; break; } } if (y == 0) { printf("%d\n", i); return 0; } } for (i = 100; i <= 999; i++) { y = 0; for (ii = 1; ii <= n - 2; ii++) { if (s[n] == i / 100 && s[n + 1] == (i / 10) % 10 && s[n + 2] == i % 10) { y = 1; break; } } if (y == 0) { printf("%d\n", i); return 0; } } return 0; }
#include <stdio.h> int main(void) { int n, a, i, ii, y, s[2048]; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a); s[i] = a; } for (i = 0; i <= 9; i++) { y = 0; for (ii = 1; ii <= n; ii++) { if (s[ii] == i) { y = 1; break; } } if (y == 0) { printf("%d\n", i); return 0; } } for (i = 10; i <= 99; i++) { y = 0; for (ii = 1; ii <= n - 1; ii++) { if (s[ii] == i / 10 && s[ii + 1] == i % 10) { y = 1; break; } } if (y == 0) { printf("%d\n", i); return 0; } } for (i = 100; i <= 999; i++) { y = 0; for (ii = 1; ii <= n - 2; ii++) { if (s[ii] == i / 100 && s[ii + 1] == (i / 10) % 10 && s[ii + 2] == i % 10) { y = 1; break; } } if (y == 0) { printf("%d\n", i); return 0; } } return 0; }
[["-", 0, 57, 15, 23, 0, 16, 31, 69, 71, 22], ["+", 0, 57, 15, 23, 0, 16, 31, 69, 71, 22], ["-", 15, 23, 0, 16, 31, 16, 31, 69, 71, 22], ["+", 15, 23, 0, 16, 31, 16, 31, 69, 71, 22], ["-", 0, 16, 12, 16, 31, 69, 71, 16, 31, 22], ["+", 0, 16, 12, 16, 31, 69, 71, 16, 31, 22], ["-", 0, 16, 31, 16, 31, 16, 31, 69, 71, 22], ["+", 0, 16, 31, 16, 31, 16, 31, 69, 71, 22], ["-", 31, 16, 12, 16, 31, 69, 71, 16, 31, 22], ["+", 31, 16, 12, 16, 31, 69, 71, 16, 31, 22]]
0
331
#include <bits/stdc++.h> #define REP(x, y, z) for (int x = y; x <= z; x++) #define FORD(x, y, z) for (int x = y; x >= z; x--) #define MSET(x, y) memset(x, y, sizeof(x)) #define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++) #define F first #define S second #define MP make_pair #define PB push_back #define SZ size() #define M 1005 void RI() {} template <typename... T> void RI(int &head, T &...tail) { scanf("%d", &head); RI(tail...); } using namespace std; typedef long long LL; int n; string in; int work() { char buf[30]; string tmp; n = in.length(); REP(i, 1, n * n) { sprintf(buf, "%d", i); tmp = buf; auto pos = in.find(tmp); if (pos == string::npos) return i; } return -1; } int main() { int x; while (~scanf("%d", &n)) { in = ""; REP(i, 1, n) { RI(x); in += (char)(x + 48); } printf("%d\n", work()); } return 0; }
#include <bits/stdc++.h> #define REP(x, y, z) for (int x = y; x <= z; x++) #define FORD(x, y, z) for (int x = y; x >= z; x--) #define MSET(x, y) memset(x, y, sizeof(x)) #define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++) #define F first #define S second #define MP make_pair #define PB push_back #define SZ size() #define M 1005 void RI() {} template <typename... T> void RI(int &head, T &...tail) { scanf("%d", &head); RI(tail...); } using namespace std; typedef long long LL; int n; string in; int work() { char buf[30]; string tmp; n = in.length(); REP(i, 0, n * n) { sprintf(buf, "%d", i); tmp = buf; auto pos = in.find(tmp); if (pos == string::npos) return i; } return -1; } int main() { int x; while (~scanf("%d", &n)) { in = ""; REP(i, 1, n) { RI(x); in += (char)(x + 48); } printf("%d\n", work()); } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
259
#include <iostream> using namespace std; int main() { int n; string s; cin >> n; for (int i = 0; i < n; i++) { int d; cin >> d; s += to_string(d); } for (int i = 1; 1; i++) { if (s.find(to_string(i)) != string::npos) continue; cout << i << endl; break; } return 0; }
#include <iostream> using namespace std; int main() { int n; string s; cin >> n; for (int i = 0; i < n; i++) { int d; cin >> d; s += to_string(d); } for (int i = 0; 1; i++) { if (s.find(to_string(i)) != string::npos) continue; cout << i << endl; break; } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
95
#include <iostream> #include <vector> using namespace std; int n, m, x[3], a, b, dist[55]; vector<int> G[55]; bool dp[55][55][109]; int main() { cin >> n >> m; for (int i = 0; i < 3; i++) cin >> x[i]; for (int i = 0; i < m; i++) { cin >> a >> b; a--, b--; G[a].push_back(b); } for (int i = 0; i < n; i++) { dp[i][i][0] = true; for (int j = 0; j < 100; j++) { for (int k = 0; k < n; k++) { if (dp[i][k][j]) { for (int l : G[k]) dp[i][l][j + 1] = true; } } } } fill(dist, dist + n - 1, 999999999); for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { if (dist[j] != 999999999) continue; int cnt = 0; for (int k = 0; k < 3; k++) { for (int l = 0; l < n; l++) { if (dp[j][l][x[k]] && dist[l] <= i) { cnt++; break; } } } if (cnt == 3) dist[j] = i + 1; } } if (dist[0] == 999999999) cout << "Impossible" << endl; else cout << dist[0] << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int n, m, x[3], a, b, dist[55]; vector<int> G[55]; bool dp[55][55][109]; int main() { cin >> n >> m; for (int i = 0; i < 3; i++) cin >> x[i]; for (int i = 0; i < m; i++) { cin >> a >> b; a--, b--; G[a].push_back(b); } for (int i = 0; i < n; i++) { dp[i][i][0] = true; for (int j = 0; j < 100; j++) { for (int k = 0; k < n; k++) { if (dp[i][k][j]) { for (int l : G[k]) dp[i][l][j + 1] = true; } } } } fill(dist, dist + n - 1, 999999999); for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { if (dist[j] != 999999999) continue; int cnt = 0; for (int k = 0; k < 3; k++) { for (int l = 0; l < n; l++) { if (dp[j][l][x[k]] && dist[l] <= i) { cnt++; break; } } } if (cnt == 3) dist[j] = i + 1; } } if (dist[0] == 999999999) cout << "IMPOSSIBLE" << endl; else cout << dist[0] << endl; return 0; }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
388
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define rep(i, n) for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() #define INF 1e9 using vi = vector<int>; using vvi = vector<vi>; vvi matmul(vvi a, int p) { int n = a.size(); auto r = a; rep(_, p - 1) { auto d = vvi(n, vi(n, 0)); rep(i, n) rep(j, n) rep(k, n) { d[i][j] = r[i][k] * a[k][j]; } r = d; } return r; } int main() { int n, m, a[3]; while (cin >> n >> m >> a[0] >> a[1] >> a[2]) { vvi z(n, vi(n, 0)); auto e = z; rep(_, m) { int p, q; cin >> p >> q; e[p - 1][q - 1] = 1; } vector<vvi> d(3); auto f = e; rep(p, 3) { d[p] = matmul(e, a[p]); } vi g(n, 0); g[n - 1] = 1; rep(t, 1000) { bool next = 0; vi ng(n, 0); rep(i, n) if (!g[i]) { bool ok = 1, r; rep(p, 3)[&] { r = 0; rep(j, n) r |= (g[j] and d[p][i][j]); ok &= r; } (); if (ok) ng[i] = next = 1; } if (!next) { cout << "IMPOSSIBLE" << endl; break; } else if (ng[0]) { cout << t + 1 << endl; break; } rep(i, n) g[i] |= ng[i]; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define rep(i, n) for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() #define INF 1e9 using vi = vector<int>; using vvi = vector<vi>; vvi matmul(vvi a, int p) { int n = a.size(); auto r = a; rep(_, p - 1) { auto d = vvi(n, vi(n, 0)); rep(i, n) rep(j, n) rep(k, n) { d[i][j] |= r[i][k] * a[k][j]; } r = d; } return r; } int main() { int n, m, a[3]; while (cin >> n >> m >> a[0] >> a[1] >> a[2]) { vvi z(n, vi(n, 0)); auto e = z; rep(_, m) { int p, q; cin >> p >> q; e[p - 1][q - 1] = 1; } vector<vvi> d(3); auto f = e; rep(p, 3) { d[p] = matmul(e, a[p]); } vi g(n, 0); g[n - 1] = 1; rep(t, 1000) { bool next = 0; vi ng(n, 0); rep(i, n) if (!g[i]) { bool ok = 1, r; rep(p, 3)[&] { r = 0; rep(j, n) r |= (g[j] and d[p][i][j]); ok &= r; } (); if (ok) ng[i] = next = 1; } if (!next) { cout << "IMPOSSIBLE" << endl; break; } else if (ng[0]) { cout << t + 1 << endl; break; } rep(i, n) g[i] |= ng[i]; } } return 0; }
[["-", 0, 14, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 17, 160]]
1
470
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; #define rep(i, n) for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() vvi matmul(vvi a, vvi b) { int n = a.size(); vvi d(n, vi(n, 0)); rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j]; return d; } static map<int, vvi> memo; vvi matpow(vvi a, int p) { return memo[p] = memo.find(p) != memo.end() ? p % 2 ? matmul(matpow(a, p - 1), a) : matmul(matpow(a, p / 2), matpow(a, p / 2)) : memo[p]; } int main() { int n, m, a[3]; while (cin >> n >> m >> a[0] >> a[1] >> a[2]) { vvi z(n, vi(n, 0)), e = z; rep(_, m) { int p, q; cin >> p >> q; e[p - 1][q - 1] = 1; } memo.clear(); memo[1] = e; vector<vvi> d(3); rep(p, 3) d[p] = matpow(e, a[p]); vi g(n, 0), ng(n, 0); g[n - 1] = 1; rep(t, 1000) { bool next = 0; fill(all(ng), 0); rep(i, n) if (!g[i]) { bool ok = 1, r; rep(p, 3)[&] { r = 0; rep(j, n) r |= (g[j] and d[p][i][j]); ok &= r; } (); if (ok) ng[i] = next = 1; } if (!next) { cout << "IMPOSSIBLE" << endl; break; } else if (ng[0]) { cout << t + 1 << endl; break; } rep(i, n) g[i] |= ng[i]; } } return 0; }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; #define rep(i, n) for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() vvi matmul(vvi a, vvi b) { int n = a.size(); vvi d(n, vi(n, 0)); rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j]; return d; } unordered_map<int, vvi> memo; inline vvi matpow(vvi a, int p) { return memo[p] = memo.find(p) == memo.end() ? p % 2 ? matmul(matpow(a, p - 1), a) : matmul(matpow(a, p / 2), matpow(a, p / 2)) : memo[p]; } int main() { int n, m, a[3]; while (cin >> n >> m >> a[0] >> a[1] >> a[2]) { vvi z(n, vi(n, 0)), e = z; rep(_, m) { int p, q; cin >> p >> q; e[p - 1][q - 1] = 1; } memo.clear(); memo[1] = e; vector<vvi> d(3); rep(p, 3) d[p] = matpow(e, a[p]); vi g(n, 0), ng(n, 0); g[n - 1] = 1; rep(t, 1000) { bool next = 0; fill(all(ng), 0); rep(i, n) if (!g[i]) { bool ok = 1, r; rep(p, 3)[&] { r = 0; rep(j, n) r |= (g[j] and d[p][i][j]); ok &= r; } (); if (ok) ng[i] = next = 1; } if (!next) { cout << "IMPOSSIBLE" << endl; break; } else if (ng[0]) { cout << t + 1 << endl; break; } rep(i, n) g[i] |= ng[i]; } } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 0, 114, 0, 115], ["-", 36, 36, 0, 30, 0, 43, 39, 344, 141, 78], ["+", 36, 36, 0, 30, 0, 43, 39, 344, 141, 78], ["+", 36, 36, 0, 30, 0, 14, 0, 114, 0, 174], ["-", 0, 37, 0, 11, 12, 41, 15, 16, 17, 79], ["+", 0, 37, 0, 11, 12, 41, 15, 16, 17, 60]]
1
523
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) x.begin(), x.end() #define pb push_back #define eb emplace_back #define X first #define Y second using namespace std; typedef long long int ll; typedef pair<int, int> pii; const int INF = 1e9; int main() { int ans = INF; int n, w, d; vector<pair<int, pii>> vr; cin >> n >> w >> d; int N = 2 * (w + d); rep(i, n) { int x, y; char f; cin >> x >> y >> f; int l, r; if (f == 'W') { l = 2 * w + d - x + y; r = N + x - d + y; } if (f == 'E') { l = x + d - y; r = 2 * w + d - x - y; } if (f == 'N') { l = 2 * w + d + y + x; r = N + d - y + x; } if (f == 'S') { l = 2 * w + d - y - x; r = 2 * w + d + y - x; } for (; r < 5 * N; r += N, l += N) { vr.eb(r, pii(i, l)); } } sort(all(vr)); int start = N; rep(i, n) { int cl = 0; auto cur = vr.begin(); while (cur->X < start) cur++; start = max(start + 1, cur->X); int cnt = 0, sum = 0; bool f[1005] = {}; for (; cnt < n; cur++) { int r = cur->X; int id = cur->Y.X; int l = cur->Y.Y; if (f[id]) continue; if (cl < l) { cl = r; sum++; } f[id] = true; cnt++; } ans = min(ans, sum); } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) x.begin(), x.end() #define pb push_back #define eb emplace_back #define X first #define Y second using namespace std; typedef long long int ll; typedef pair<int, int> pii; const int INF = 1e9; int main() { int ans = INF; int n, w, d; vector<pair<int, pii>> vr; cin >> n >> w >> d; int N = 2 * (w + d); rep(i, n) { int x, y; char f; cin >> x >> y >> f; int l, r; if (f == 'W') { l = 2 * w + d - x + y; r = N + x - d + y; } if (f == 'E') { l = x + d - y; r = 2 * w + d - x - y; } if (f == 'N') { l = 2 * w + d + y + x; r = N + d - y + x; } if (f == 'S') { l = 2 * w + d - y - x; r = 2 * w + d + y - x; } // cout << l << " " << r << endl; for (; r < 5 * N; r += N, l += N) { vr.eb(r, pii(i, l)); } } sort(all(vr)); int start = N - 1; rep(i, n + 1) { int cl = 0; auto cur = vr.begin(); while (cur->X < start) cur++; start = max(start + 1, cur->X); int cnt = 0, sum = 0; bool f[1005] = {}; for (; cnt < n; cur++) { int r = cur->X; int id = cur->Y.X; int l = cur->Y.Y; if (f[id]) continue; if (cl < l) { cl = r; sum++; } f[id] = true; cnt++; } ans = min(ans, sum); } cout << ans << endl; }
[["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 33], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
454
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int N, W, H; vector<P> t; bool check(P p, int a) { if (p.first < p.second) return (p.first <= a && a <= p.second); return (p.first <= a || a <= p.second); } int calcArg(int x, int y) { if (x == 0) return y; if (y == H) return H + x; if (x == W) return H + W + H - y; return H + W + H + W - x; } int calcLU(int x, int y) { int d = min(x, H - y); return calcArg(x - d, y + d); } int calcLD(int x, int y) { int d = min(x, y); return calcArg(x - d, y - d); } int calcRU(int x, int y) { int d = min(W - x, H - y); return calcArg(x + d, y + d); } int calcRD(int x, int y) { int d = min(W - x, y); return calcArg(x + d, y - d); } int solve(int si) { int res = 1, last = t[si].second; for (int i = si; i < si + N; i++) { if (check(t[i], last)) continue; res++; last = t[i].second; } return res; } int main() { cin >> N >> W >> H; for (int i = 0; i < N; i++) { int x, y; char ch; P p; cin >> x >> y >> ch; if (ch == 'N') { p.first = calcLU(x, y); p.second = calcRU(x, y); } if (ch == 'E') { p.first = calcRU(x, y); p.second = calcRD(x, y); } if (ch == 'S') { p.first = calcRU(x, y); p.second = calcLU(x, y); } if (ch == 'W') { p.first = calcLD(x, y); p.second = calcLU(x, y); } t.push_back(p); } for (int i = 0; i < N; i++) swap(t[i].first, t[i].second); sort(t.begin(), t.end()); for (int i = 0; i < N; i++) t.push_back(t[i]); for (int i = 0; i < N + N; i++) swap(t[i].first, t[i].second); int ans = N; for (int i = 0; i < N; i++) ans = min(ans, solve(i)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int N, W, H; vector<P> t; bool check(P p, int a) { if (p.first < p.second) return (p.first <= a && a <= p.second); return (p.first <= a || a <= p.second); } int calcArg(int x, int y) { if (x == 0) return y; if (y == H) return H + x; if (x == W) return H + W + H - y; return H + W + H + W - x; } int calcLU(int x, int y) { int d = min(x, H - y); return calcArg(x - d, y + d); } int calcLD(int x, int y) { int d = min(x, y); return calcArg(x - d, y - d); } int calcRU(int x, int y) { int d = min(W - x, H - y); return calcArg(x + d, y + d); } int calcRD(int x, int y) { int d = min(W - x, y); return calcArg(x + d, y - d); } int solve(int si) { int res = 1, last = t[si].second; for (int i = si; i < si + N; i++) { if (check(t[i], last)) continue; res++; last = t[i].second; } return res; } int main() { cin >> N >> W >> H; for (int i = 0; i < N; i++) { int x, y; char ch; P p; cin >> x >> y >> ch; if (ch == 'N') { p.first = calcLU(x, y); p.second = calcRU(x, y); } if (ch == 'E') { p.first = calcRU(x, y); p.second = calcRD(x, y); } if (ch == 'S') { p.first = calcRD(x, y); p.second = calcLD(x, y); } if (ch == 'W') { p.first = calcLD(x, y); p.second = calcLU(x, y); } t.push_back(p); } for (int i = 0; i < N; i++) swap(t[i].first, t[i].second); sort(t.begin(), t.end()); for (int i = 0; i < N; i++) t.push_back(t[i]); for (int i = 0; i < N + N; i++) swap(t[i].first, t[i].second); int ans = N; for (int i = 0; i < N; i++) ans = min(ans, solve(i)); cout << ans << endl; return 0; }
[["-", 64, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 64, 9, 0, 1, 0, 11, 12, 2, 63, 22]]
1
672
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int w, h, n; cin >> n >> w >> h; int x, y, B = 2 * (w + h), ans = n; char c; vector<P> m; for (int i = 0; i < n; i++) { cin >> x >> y >> c; if (c == 'S') m.push_back(P(x + y, (x - y + B) % B)); if (c == 'E') m.push_back(P(w + y + (w - x), w + y - (w - x))); if (c == 'N') m.push_back(P(w + h + (w - x) + (h - y), w + h + (w - x) - (h - y))); if (c == 'W') m.push_back(P((w * 2 + h + (h - y) + x) % B, w * 2 + h + (h - y) - x)); } sort(m.begin(), m.end()); for (int i = 0; i < n; i++) { int tp = 0, p = m[i].first; for (int j = 0; j < n; j++) { int s = m[(i + j) % n].second, g = m[(i + j) % n].first; if (s > g && !(g < p && p < s)) continue; if (s < g && s <= p && p <= g) continue; tp++, p = g; } ans = min(tp, ans); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int w, h, n; cin >> n >> w >> h; int x, y, B = 2 * (w + h), ans = n; char c; vector<P> m; for (int i = 0; i < n; i++) { cin >> x >> y >> c; if (c == 'S') m.push_back(P(x + y, (x - y + B) % B)); if (c == 'E') m.push_back(P(w + y + (w - x), w + y - (w - x))); if (c == 'N') m.push_back(P(w + h + (w - x) + (h - y), w + h + (w - x) - (h - y))); if (c == 'W') m.push_back(P((w * 2 + h + (h - y) + x) % B, w * 2 + h + (h - y) - x)); } sort(m.begin(), m.end()); for (int i = 0; i < n; i++) { int tp = 1, p = m[i].first; for (int j = 0; j < n; j++) { int s = m[(i + j) % n].second, g = m[(i + j) % n].first; if (s > g && !(g < p && p < s)) continue; if (s < g && s <= p && p <= g) continue; tp++, p = g; } ans = min(tp, ans); } cout << ans << endl; return 0; }
[["-", 0, 7, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 7, 8, 9, 0, 43, 49, 50, 51, 13]]
1
402
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; int n, w, d; P v[1002]; int main() { cin >> n >> w >> d; int l = 2 * d + 2 * w; for (int i = 0; i < n; ++i) { int x, y; char f; cin >> x >> y >> f; int c, b; switch (f) { case 'N': c = x; b = d - y; break; case 'E': c = d - y + w; b = w - x; break; case 'S': c = w - x + w + d; b = y; break; case 'W': c = y + 2 * w + d; b = x; break; } // v[i] = (left end of view, right end of view) v[i] = P((c - b + l) % l, (c + b + l) % l); } // sort by the left end of view sort(v, v + n); int minc = 10000; for (int i = 0; i < n; ++i) { // Put a clock at the left end of i-th member's view int c = 1; int tmp = v[i].first; for (int j = 0; j < n - 1; ++j) { int k = (i - j + n) % n; int left = v[k].first; int right = v[k].second; if (left > right) right += l; if (left <= tmp && tmp <= right) continue; if (left - l <= tmp && tmp <= right - l) continue; c++; tmp = left; } if (c < minc) minc = c; } cout << minc << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; int n, w, d; P v[1002]; int main() { cin >> n >> w >> d; int l = 2 * d + 2 * w; for (int i = 0; i < n; ++i) { int x, y; char f; cin >> x >> y >> f; int c, b; switch (f) { case 'N': c = x; b = d - y; break; case 'E': c = d - y + w; b = w - x; break; case 'S': c = w - x + w + d; b = y; break; case 'W': c = y + 2 * w + d; b = x; break; } // v[i] = (left end of view, right end of view) v[i] = P((c - b + l) % l, (c + b + l) % l); } // sort by the left end of view sort(v, v + n); int minc = 10000; for (int i = 0; i < n; ++i) { // Put a clock at the left end of i-th member's view int c = 1; int tmp = v[i].first; for (int j = 1; j < n; ++j) { int k = (i - j + n) % n; int left = v[k].first; int right = v[k].second; if (left > right) right += l; if (left <= tmp && tmp <= right) continue; if (left - l <= tmp && tmp <= right - l) continue; c++; tmp = left; } if (c < minc) minc = c; } cout << minc << endl; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
375
#include <iostream> #include <string> #include <vector> using namespace std; string s; int n, sums; long long prods = 1, fact[19]; long long anagram(string t) { int c[10] = {0}; for (int i = 0; i < t.size(); i++) c[t[i] - 48]++; long long ret = fact[t.size()]; for (int i = 0; i < 10; i++) ret /= fact[c[i]]; return ret; } long long anagram2(int ptr, string t) { if (ptr == n) return 0; long long ret = 0; bool vis[26] = {false}; for (int i = 0; i < t.size(); i++) { if (vis[t[i] - 48]) continue; if (t[i] < s[ptr]) { string u = t; u.erase(u.begin() + i); ret += anagram(u); } if (t[i] == s[ptr]) { string u = t; u.erase(u.begin() + i); ret += anagram2(ptr + 1, u); } vis[t[i] - 48] = true; } return ret; } int rec(int ptr, int digit, int sum, int prod, string x) { if (ptr == 10) { if (digit != n) return 0; if (sum != sums) return (sum < sums ? anagram(x) : 0); if (prod != prods) return (prod < prods ? anagram(x) : 0); return anagram2(0, x); } long long ret = 0; for (int i = 0; digit + i <= n; i++) { ret += rec(ptr + 1, digit + i, sum, prod, x); sum += ptr; prod *= (ptr + 1); x += ptr + 48; } return ret; } int main() { cin >> s; n = s.size(); fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; for (int i = 0; i < n; i++) { sums += s[i] - 48; prods *= s[i] - 47; } long long ret = rec(0, 0, 0, 1, ""); cout << ret << endl; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; string s; int n, sums; long long prods = 1, fact[19]; long long anagram(string t) { int c[10] = {0}; for (int i = 0; i < t.size(); i++) c[t[i] - 48]++; long long ret = fact[t.size()]; for (int i = 0; i < 10; i++) ret /= fact[c[i]]; return ret; } long long anagram2(int ptr, string t) { if (ptr == n) return 0; long long ret = 0; bool vis[26] = {false}; for (int i = 0; i < t.size(); i++) { if (vis[t[i] - 48]) continue; if (t[i] < s[ptr]) { string u = t; u.erase(u.begin() + i); ret += anagram(u); } if (t[i] == s[ptr]) { string u = t; u.erase(u.begin() + i); ret += anagram2(ptr + 1, u); } vis[t[i] - 48] = true; } return ret; } long long rec(int ptr, int digit, int sum, long long prod, string x) { if (ptr == 10) { if (digit != n) return 0; if (sum != sums) return (sum < sums ? anagram(x) : 0); if (prod != prods) return (prod < prods ? anagram(x) : 0); return anagram2(0, x); } long long ret = 0; for (int i = 0; digit + i <= n; i++) { ret += rec(ptr + 1, digit + i, sum, prod, x); sum += ptr; prod *= (ptr + 1); x += ptr + 48; } return ret; } int main() { cin >> s; n = s.size(); fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; for (int i = 0; i < n; i++) { sums += s[i] - 48; prods *= s[i] - 47; } long long ret = rec(0, 0, 0, 1, ""); cout << ret << endl; return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["+", 36, 36, 0, 30, 0, 14, 39, 86, 0, 96], ["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 40], ["+", 49, 53, 54, 55, 0, 56, 39, 86, 0, 96]]
1
548
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int t, head[50], w[50]; struct edges { int to, next; } e[(50 * 50) << 1]; void add(int x, int y) { e[++t] = (edges){y, head[x]}; head[x] = t; } int n, m, ans = 1000000, now; int vis[50], step; void dfs(int x) { step++; if (x == n) { for (int p = head[x]; p; p = e[p].next) if (!vis[e[p].to]) now += w[e[p].to]; ans = min(ans, now); for (int p = head[x]; p; p = e[p].next) if (!vis[e[p].to]) now -= w[e[p].to]; return; } for (int p = head[x]; p; p = e[p].next) if (!vis[e[p].to]) { vis[e[p].to] = step; now += w[e[p].to]; } for (int p = head[x]; p; p = e[p].next) if (vis[e[p].to] == step) { dfs(e[p].to); } for (int p = head[x]; p; p = e[p].next) if (vis[e[p].to] == step) { vis[e[p].to] = 0; now -= w[e[p].to]; } step--; } int main() { int x, y; cin >> n >> m; for (int i = 1; i <= n; ++i) scanf("%d", &w[i]); for (int i = 1; i <= m; ++i) { scanf("%d%d", &x, &y); add(x, y); add(y, x); } now = w[1]; vis[1] = 1; dfs(1); cout << ans << endl; return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int t, head[50], w[50]; struct edges { int to, next; } e[(50 * 50) << 1]; void add(int x, int y) { e[++t] = (edges){y, head[x]}; head[x] = t; } int n, m, ans = 1000000, now; int vis[50], step; void dfs(int x) { step++; // cout<<x<<' '<<step<<endl; if (x == n) { for (int p = head[x]; p; p = e[p].next) if (!vis[e[p].to]) now += w[e[p].to]; ans = min(ans, now); for (int p = head[x]; p; p = e[p].next) if (!vis[e[p].to]) now -= w[e[p].to]; step--; // printf("vis[2]=%d\n",vis[2]); return; } for (int p = head[x]; p; p = e[p].next) if (!vis[e[p].to]) { vis[e[p].to] = step; now += w[e[p].to]; } for (int p = head[x]; p; p = e[p].next) if (vis[e[p].to] == step) { dfs(e[p].to); // printf("**step=%d next(%d)=%d // vis[]=%d\n",step,x,e[e[p].next].to,vis[e[e[p].next].to]); } for (int p = head[x]; p; p = e[p].next) if (vis[e[p].to] == step) { vis[e[p].to] = 0; now -= w[e[p].to]; } step--; // cout<<step<<endl; } int main() { int x, y; cin >> n >> m; for (int i = 1; i <= n; ++i) scanf("%d", &w[i]); for (int i = 1; i <= m; ++i) { scanf("%d%d", &x, &y); add(x, y); add(y, x); } now = w[1]; vis[1] = 1; dfs(1); cout << ans << endl; return 0; }
[["+", 0, 57, 64, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 27, 17, 68], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
508
import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * Rearranging a Sequence */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; line = br.readLine(); int n, m; n = parseInt(line.substring(0, line.indexOf(' '))); m = parseInt(line.substring(line.indexOf(' ') + 1)); List<Integer> list = new ArrayList<>(n + m); boolean[] dup = new boolean[n + 1]; for (int i = n; i >= 1; i--) list.add(i); for (int i = 0; i < m; i++) list.add(parseInt(br.readLine())); StringBuilder sb = new StringBuilder(); for (int i = list.size() - 1; i >= 1; i--) { int e = list.get(i); if (!dup[e]) { sb.append(e).append('\n'); dup[e] = true; } } System.out.print(sb.toString()); } }
import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * Rearranging a Sequence */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; line = br.readLine(); int n, m; n = parseInt(line.substring(0, line.indexOf(' '))); m = parseInt(line.substring(line.indexOf(' ') + 1)); List<Integer> list = new ArrayList<>(n + m); boolean[] dup = new boolean[n + 1]; for (int i = n; i >= 1; i--) list.add(i); for (int i = 0; i < m; i++) list.add(parseInt(br.readLine())); StringBuilder sb = new StringBuilder(); for (int i = list.size() - 1; i >= 0; i--) { int e = list.get(i); if (!dup[e]) { sb.append(e).append('\n'); dup[e] = true; } } System.out.print(sb.toString()); } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 499], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 499]]
3
299
#include <stdio.h> #include <stdlib.h> int main() { int n, m; scanf("%d%d", &n, &m); int input[m], flag[n + 1]; int i; for (i = 0; i < m; i++) { scanf("%d", &input[i]); } for (i = 0; i < n; i++) { flag[i] = 0; } for (i = m - 1; i >= 0; i--) { if (flag[input[i]] == 0) { printf("%d\n", input[i]); flag[input[i]] = m - i; } } for (i = 1; i <= n; i++) { if (flag[i] == 0) { printf("%d\n", i); } } return 0; }
#include <stdio.h> #include <stdlib.h> int main() { int n, m; scanf("%d%d", &n, &m); int input[m], flag[n + 1]; int i; for (i = 0; i < m; i++) { scanf("%d", &input[i]); } for (i = 0; i < n + 1; i++) { flag[i] = 0; } for (i = m - 1; i >= 0; i--) { if (flag[input[i]] == 0) { printf("%d\n", input[i]); flag[input[i]] = m - i; } } for (i = 1; i <= n; i++) { if (flag[i] == 0) { printf("%d\n", i); } } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
0
189
#include <algorithm> #include <iostream> using namespace std; int p[300000], n, m; pair<int, int> x[300000]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { p[i] = i; } for (int i = 0; i < m; i++) { int r; cin >> r; p[r] = -i; } for (int i = 1; i <= n; i++) { x[i] = make_pair(p[i], i); } sort(x + 1, x + n + 1); for (int i = 1; i <= n; i++) { cout << x[i].first << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int p[300000], n, m; pair<int, int> x[300000]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { p[i] = i; } for (int i = 0; i < m; i++) { int r; cin >> r; p[r] = -i; } for (int i = 1; i <= n; i++) { x[i] = make_pair(p[i], i); } sort(x + 1, x + n + 1); for (int i = 1; i <= n; i++) { cout << x[i].second << endl; } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 118, 119, 120], ["+", 0, 1, 0, 16, 31, 16, 12, 118, 119, 120]]
1
169
#include <algorithm> #include <map> #include <stdio.h> #include <stdlib.h> #include <utility> #include <vector> using namespace std; bool mycomp1(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } bool mycomp2(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } map<int, int> update; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int r; scanf("%d", &r); update[r] = i; } vector<pair<int, int>> requests; for (map<int, int>::iterator it = update.begin(); it != update.end(); it++) { requests.push_back(pair<int, int>(it->first, it->second)); } std::sort(requests.begin(), requests.end(), mycomp1); for (int i = 0; i < requests.size(); i++) { printf("%d\n", requests[i].first); } std::sort(requests.begin(), requests.end(), mycomp2); int now = 0; for (int i = 0; i < requests.size(); i++) { int exclude = requests[i].first; for (int j = now; j < exclude; j++) { printf("%d\n", j); } now = exclude + 1; } for (int j = now; j < n; j++) { printf("%d\n", j); } }
#include <algorithm> #include <map> #include <stdio.h> #include <stdlib.h> #include <utility> #include <vector> using namespace std; bool mycomp1(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } bool mycomp2(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } map<int, int> update; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int r; scanf("%d", &r); update[r] = i; } vector<pair<int, int>> requests; for (map<int, int>::iterator it = update.begin(); it != update.end(); it++) { requests.push_back(pair<int, int>(it->first, it->second)); } std::sort(requests.begin(), requests.end(), mycomp1); for (int i = 0; i < requests.size(); i++) { printf("%d\n", requests[i].first); } std::sort(requests.begin(), requests.end(), mycomp2); int now = 1; for (int i = 0; i < requests.size(); i++) { int exclude = requests[i].first; for (int j = now; j < exclude; j++) { printf("%d\n", j); } now = exclude + 1; } for (int j = now; j <= n; j++) { printf("%d\n", j); } }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19]]
1
373
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) #define REP2(i, x, n) for (int i = x; i < (n); i++) #define ALL(x) begin(x), end(x) using namespace std; struct cww { cww() { ios::sync_with_stdio(false); cin.tie(0); } } star; int main() { int N, M; cin >> N >> M; vector<int> e(M); for (auto &x : e) { cin >> x; } reverse(ALL(e)); vector<int> vc, flag(N + 1, 0); REP(i, M) // O(n) { if (flag[e[i]] == 0) // O(1) { vc.emplace_back(e[i]); // O(1) flag[e[i]] = 1; } } // move for (const auto &x : vc) { cout << x << endl; } sort(ALL(vc)); // not move REP2(i, 1, N) // O(n) { if (flag[i] != 0) // O(1) { continue; } cout << i << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) #define REP2(i, x, n) for (int i = x; i < (n); i++) #define ALL(x) begin(x), end(x) using namespace std; struct cww { cww() { ios::sync_with_stdio(false); cin.tie(0); } } star; int main() { int N, M; cin >> N >> M; vector<int> e(M); for (auto &x : e) { cin >> x; } reverse(ALL(e)); vector<int> vc, flag(N + 1, 0); REP(i, M) // O(n) { if (flag[e[i]] == 0) // O(1) { vc.emplace_back(e[i]); // O(1) flag[e[i]] = 1; } } // move for (const auto &x : vc) { cout << x << endl; } sort(ALL(vc)); // not move REP2(i, 1, N + 1) // O(n) { if (flag[i] != 0) // O(1) { continue; } cout << i << endl; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
225
#include <utility> // pair make_pair using std::make_pair; using std::pair; #include <tuple> // tuple(c++11) make_tuple(c++11) using std::make_tuple; using std::tuple; // get<address>(variable) #include <string> // string stoi(c++11) stoll(c++11) to_string(c++11) using std::stoi; using std::stoll; using std::string; using std::to_string; #include <queue> // queue priority_queue using std::priority_queue; using std::queue; #include <stack> // stack using std::stack; #include <vector> // vector using std::vector; #include <map> // map using std::map; #include <set> // set using std::set; // insert erase find count #include <algorithm> // min max using std::lower_bound; // iterator, >= using std::max; using std::max_element; // iterator using std::min; using std::min_element; // iterator using std::sort; using std::upper_bound; // iterator, > #include <complex> // complex using std::complex; // real imag #include <iostream> // cin cout using std::cin; using std::cout; using std::endl; #include <iomanip> // setprecision fixed(manipulator) using std::fixed; using std::setprecision; #include <cmath> #include <cstdio> #define lli long long int #define INF (int)1e09 #define FOR(i, m, n) for (int i = m; i < n; i++) #define EFOR(i, m, n) for (int i = m; i <= n; i++) #define RFOR(i, m, n) for (int i = m; i > n; i--) #define REFOR(i, m, n) for (int i = m; i >= n; i--) #define VSORT(v) sort((v).begin(), (v).end()) // sort vector typedef pair<int, int> PI; typedef tuple<int, int, int> TI; template <typename T> using Vec = vector<T>; // c++11 int main(void) { int n; int m; cin >> n >> m; Vec<int> e(m); FOR(i, 0, m) { cin >> e[i]; } Vec<bool> check(n, true); REFOR(i, m - 1, 0) { if (check[e[i] - 1] == true) { cout << e[i] << endl; check[e[i] - 1] = false; } } FOR(i, 0, m) { if (check[i] == true) { cout << i + 1 << endl; } } return 0; }
#include <utility> // pair make_pair using std::make_pair; using std::pair; #include <tuple> // tuple(c++11) make_tuple(c++11) using std::make_tuple; using std::tuple; // get<address>(variable) #include <string> // string stoi(c++11) stoll(c++11) to_string(c++11) using std::stoi; using std::stoll; using std::string; using std::to_string; #include <queue> // queue priority_queue using std::priority_queue; using std::queue; #include <stack> // stack using std::stack; #include <vector> // vector using std::vector; #include <map> // map using std::map; #include <set> // set using std::set; // insert erase find count #include <algorithm> // min max using std::lower_bound; // iterator, >= using std::max; using std::max_element; // iterator using std::min; using std::min_element; // iterator using std::sort; using std::upper_bound; // iterator, > #include <complex> // complex using std::complex; // real imag #include <iostream> // cin cout using std::cin; using std::cout; using std::endl; #include <iomanip> // setprecision fixed(manipulator) using std::fixed; using std::setprecision; #include <cmath> #include <cstdio> #define lli long long int #define INF (int)1e09 #define FOR(i, m, n) for (int i = m; i < n; i++) #define EFOR(i, m, n) for (int i = m; i <= n; i++) #define RFOR(i, m, n) for (int i = m; i > n; i--) #define REFOR(i, m, n) for (int i = m; i >= n; i--) #define VSORT(v) sort((v).begin(), (v).end()) // sort vector typedef pair<int, int> PI; typedef tuple<int, int, int> TI; template <typename T> using Vec = vector<T>; // c++11 int main(void) { int n; int m; cin >> n >> m; Vec<int> e(m); FOR(i, 0, m) { cin >> e[i]; } Vec<bool> check(n, true); REFOR(i, m - 1, 0) { if (check[e[i] - 1] == true) { cout << e[i] << endl; check[e[i] - 1] = false; } } FOR(i, 0, n) { if (check[i] == true) { cout << i + 1 << endl; } } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
408
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<bool> used(n + 1, false); vector<int> e(m); for (int i = 0; i < m; i++) { cin >> e[i]; } reverse(e.begin(), e.end()); for (int i = 0; i < m; i++) { if (used[e[i]] == false) { cout << e[i]; used[e[i]] = true; } } for (int i = 1; i <= n; i++) { if (used[i] == false) cout << i << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<bool> used(n + 1, false); vector<int> e(m); for (int i = 0; i < m; i++) { cin >> e[i]; } reverse(e.begin(), e.end()); for (int i = 0; i < m; i++) { if (used[e[i]] == false) { cout << e[i] << endl; used[e[i]] = true; } } for (int i = 1; i <= n; i++) { if (used[i] == false) cout << i << endl; } return 0; }
[["+", 0, 57, 64, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 57, 64, 9, 0, 1, 0, 16, 12, 22]]
1
164
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<char, LL> P; const LL mod = 998244353; const LL LINF = 1LL << 62; const int INF = 1 << 30; vector<vector<int>> nextIndex(const string &s) { int n = s.length(); vector<vector<int>> ret(n + 1, vector<int>(2, n)); for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 2; j++) ret[i][j] = ret[i + 1][j]; ret[i][s[i] - '0'] = i; } return ret; } int main() { string s, t; cin >> s >> t; auto p = nextIndex(s); auto q = nextIndex(t); int sn = s.length(), tn = t.length(); vector<vector<int>> dp(sn + 2, vector<int>(tn + 2, INF)); vector<vector<pair<char, pair<int, int>>>> recon( sn + 2, vector<pair<char, pair<int, int>>>(tn + 2, mp(',', mp(sn, tn)))); dp[sn + 1][tn + 1] = 0; dp[sn][tn] = 1; for (int i = sn + 1; i >= 0; i--) { for (int j = tn + 1; j >= 0; j--) { if (i == sn + 1 && j == tn + 1) continue; for (int k = 0; k < 2; k++) { int ni, nj; if (i >= sn) ni = sn; else ni = p[i][k]; if (j >= tn) nj = tn; else nj = q[j][k]; if (dp[i][j] > dp[ni + 1][nj + 1] + 1) { dp[i][j] = dp[ni + 1][nj + 1] + 1; recon[i][j] = mp('0' + k, mp(ni + 1, nj + 1)); } } } } int i = 0, j = 0; string ans = ""; for (int k = 0; k < dp[0][0]; k++) { auto st = recon[i][j]; ans += st.fs; i = st.sc.fs; j = st.sc.sc; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<char, LL> P; const LL mod = 998244353; const LL LINF = 1LL << 62; const int INF = 1 << 30; vector<vector<int>> nextIndex(const string &s) { int n = s.length(); vector<vector<int>> ret(n + 1, vector<int>(2, n)); for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 2; j++) ret[i][j] = ret[i + 1][j]; ret[i][s[i] - '0'] = i; } return ret; } int main() { string s, t; cin >> s >> t; auto p = nextIndex(s); auto q = nextIndex(t); int sn = s.length(), tn = t.length(); vector<vector<int>> dp(sn + 2, vector<int>(tn + 2, INF)); vector<vector<pair<char, pair<int, int>>>> recon( sn + 2, vector<pair<char, pair<int, int>>>(tn + 2, mp('0', mp(sn, tn)))); dp[sn + 1][tn + 1] = 0; dp[sn][tn] = 1; for (int i = sn + 1; i >= 0; i--) { for (int j = tn + 1; j >= 0; j--) { if (i == sn + 1 && j == tn + 1) continue; for (int k = 0; k < 2; k++) { int ni, nj; if (i >= sn) ni = sn; else ni = p[i][k]; if (j >= tn) nj = tn; else nj = q[j][k]; if (dp[i][j] > dp[ni + 1][nj + 1] + 1) { dp[i][j] = dp[ni + 1][nj + 1] + 1; recon[i][j] = mp('0' + k, mp(ni + 1, nj + 1)); } } } } int i = 0, j = 0; string ans = ""; for (int k = 0; k < dp[0][0]; k++) { auto st = recon[i][j]; ans += st.fs; i = st.sc.fs; j = st.sc.sc; } cout << ans << endl; return 0; }
[["-", 3, 4, 0, 2, 3, 4, 0, 103, 0, 125], ["+", 3, 4, 0, 2, 3, 4, 0, 103, 0, 125]]
1
616