text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1 << 10;
int pointer = SIZE;
char buffer[SIZE];
char Advance() {
if (pointer == SIZE) {
fread(buffer, 1, SIZE, stdin);
pointer = 0;
}
return buffer[pointer++];
}
int Read() {
int answer = 0;
char ch = Advance();
while (!isdigit(ch)) ch = Advance();
while (isdigit(ch)) {
answer = answer * 10 + ch - '0';
ch = Advance();
}
return answer;
}
const int MAXN = 100000;
const int SIGMA = 6;
char s[1 + MAXN];
int have[SIGMA], sum[1 << SIGMA], limit[1 << SIGMA];
int mask[1 + MAXN];
char answer[1 + MAXN];
void Modify(int mask, int add) {
for (int i = 0; i < (1 << SIGMA); i++) limit[i] += add;
mask = (1 << SIGMA) - 1 - mask;
for (int subMask = mask; subMask; subMask = (mask & (subMask - 1)))
limit[subMask] -= add;
}
bool Check() {
for (int i = 0; i < SIGMA; i++) sum[1 << i] = have[i];
for (int mask = 1; mask < (1 << SIGMA); mask++) {
int bit = (mask & -mask);
if (bit != mask) sum[mask] = sum[bit] + sum[mask - bit];
if (sum[mask] > limit[mask]) return false;
}
return true;
}
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
for (int i = 1; i <= n; i++) {
have[s[i] - 'a']++;
mask[i] = (1 << SIGMA) - 1;
}
int m;
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
int x;
scanf("%d %s", &x, s + 1);
int l = strlen(s + 1);
mask[x] = 0;
for (int j = 1; j <= l; j++) mask[x] |= (1 << (s[j] - 'a'));
}
for (int i = 1; i <= n; i++) Modify(mask[i], 1);
for (int i = 1; i <= n; i++) {
Modify(mask[i], -1);
for (int j = 0; j < SIGMA; j++)
if ((mask[i] & (1 << j)) && have[j]) {
have[j]--;
if (Check()) {
answer[i] = j + 'a';
break;
} else
have[j]++;
}
if (!answer[i]) {
printf("Impossible\n");
return 0;
}
}
printf("%s\n", answer + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);
template <class T, class S>
inline void add(T& a, S b) {
a += b;
if (a >= mod) a -= mod;
}
template <class T, class S>
inline void sub(T& a, S b) {
a -= b;
if (a < 0) a += mod;
}
template <class T, class S>
inline bool chkmax(T& a, S b) {
return a < b ? a = b, true : false;
}
template <class T, class S>
inline bool chkmin(T& a, S b) {
return a > b ? a = b, true : false;
}
int n, m, mask[N], c[N], cnt[N];
char s[N], t[N], ans[N];
bool check() {
int sum = 0, ret = inf;
for (int i = 0; i < 6; i++) sum += c[i];
for (int s1 = 0; s1 < 64; s1++) {
int tmp = 0;
for (int i = 0; i < 6; i++)
if (s1 >> i & 1) tmp += c[i];
for (int s2 = 0; s2 < 64; s2++)
if ((s2 | s1) != s1) tmp += cnt[s2];
chkmin(ret, tmp);
}
return sum == ret;
}
inline int getId(char c) { return c - 'a'; }
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; i++) c[getId(s[i])]++;
scanf("%d", &m);
while (m--) {
int p;
scanf("%d%s", &p, t + 1);
for (int i = 1; t[i]; i++) mask[p] |= 1 << getId(t[i]);
}
for (int i = 1; i <= n; i++) {
if (!mask[i]) mask[i] = 63;
cnt[mask[i]]++;
}
for (int i = 1; i <= n; i++) {
bool flag = false;
for (int j = 0; j < 6; j++) {
if (c[j] && mask[i] >> j & 1) {
c[j]--;
cnt[mask[i]]--;
flag = check();
if (flag) {
ans[i] = 'a' + j;
break;
}
c[j]++;
cnt[mask[i]]++;
}
}
if (!flag) return puts("Impossible"), 0;
}
ans[n + 1] = '\0';
puts(ans + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6;
const int OO = 0x3f3f3f3f;
const double EPS = 1e-9;
const int S = 70;
const int T = 71;
struct edge {
int v, c, f;
edge(int _v, int _c, int _f) : v(_v), c(_c), f(_f) {}
};
vector<edge> edges;
vector<int> G[100];
string str, aux;
int n, idx, cor[MAX], tempo = 1;
int mask[MAX], cntLetter[10], cntMask[70], edge_num[100][100];
void addEdge(int u, int v, int c, int rc) {
edge_num[u][v] = edges.size();
G[u].push_back(edges.size());
edges.push_back(edge(v, c, 0));
G[v].push_back(edges.size());
edges.push_back(edge(u, rc, 0));
}
int dfs(int s, int t, int f) {
if (s == t) return f;
cor[s] = tempo;
for (int &e : G[s])
if (cor[edges[e].v] < tempo and edges[e].c - edges[e].f > 0)
if (int a = dfs(edges[e].v, t, min(f, edges[e].c - edges[e].f))) {
edges[e].f += a;
edges[e ^ 1].f -= a;
return a;
}
return 0;
}
int maxFlow(int s, int t) {
int mf = 0;
while (int a = dfs(s, t, OO)) mf += a, tempo++;
return mf;
}
bool check(int j, int mask) {
if ((mask & (1 << j)) == 0) return false;
int e1 = edge_num[S][j];
int e2 = edge_num[mask + 6][T];
if (edges[e1].f == 0 or edges[e2].f == 0) return false;
edges[e1].f--;
edges[e1 ^ 1].f++;
vector<int> alterado;
alterado.push_back(e1);
for (int &e : G[edges[e1].v]) {
if (!(e & 1) and edges[e].f > 0) {
alterado.push_back(e);
edges[e].f--;
edges[e ^ 1].f++;
for (int &ee : G[edges[e].v])
if (!(ee & 1)) {
alterado.push_back(ee);
edges[ee].f--;
edges[ee ^ 1].f++;
break;
}
break;
}
}
if (edges[e2].f < edges[e2].c) {
edges[e1].c--;
edges[e2].c--;
return true;
}
edges[e2].f--;
edges[e2 ^ 1].f++;
alterado.push_back(e2);
for (int &e : G[mask + 6]) {
if ((e & 1) and edges[e].f < 0) {
alterado.push_back(e ^ 1);
edges[e].f++;
edges[e ^ 1].f--;
for (int &ee : G[edges[e].v])
if (ee & 1) {
alterado.push_back(ee ^ 1);
edges[ee].f++;
edges[ee ^ 1].f--;
break;
}
break;
}
}
tempo++;
edges[e1].c--;
edges[e2].c--;
if (dfs(S, T, OO)) return true;
edges[e1].c++;
edges[e2].c++;
for (int &e : alterado) {
edges[e].f++;
edges[e ^ 1].f--;
}
return false;
}
int main() {
cin >> str >> n;
for (int i = 0; i < (int)str.size(); i++) mask[i] = 63;
for (int i = 0; i < (int)n; i++) {
cin >> idx >> aux;
idx--;
mask[idx] = 0;
for (char &c : aux) mask[idx] |= (1 << (c - 'a'));
}
for (int i = 0; i < (int)str.size(); i++) {
cntLetter[str[i] - 'a']++;
cntMask[mask[i]]++;
}
for (int i = 0; i < (int)6; i++) addEdge(S, i, cntLetter[i], 0);
for (int i = 0; i < (int)(1 << 6); i++) addEdge(i + 6, T, cntMask[i], 0);
for (int i = 0; i < (int)6; i++)
for (int j = 0; j < (int)(1 << 6); j++)
if (j & (1 << i)) addEdge(i, j + 6, OO, 0);
int mf = maxFlow(S, T);
if (mf != (int)str.size())
puts("Impossible");
else {
for (int i = 0; i < (int)str.size(); i++)
for (int j = 0; j < (int)6; j++)
if (check(j, mask[i])) {
str[i] = char(j + 'a');
break;
}
cout << str << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
const int C = 6;
const int INF = 1 << 30;
struct maxflow {
struct edge {
int j, cap, flow;
edge *pair;
edge(int j, int cap) : j(j), cap(cap), flow(0), pair(NULL) {}
void _add_flow(int add) {
flow += add;
assert(flow <= cap);
}
void add_flow(int add) {
_add_flow(add);
pair->_add_flow(-add);
}
bool in_res() { return cap != flow; }
int res() { return cap - flow; }
};
int n;
vector<edge *> adj[N];
edge *parent[N];
int flow = 0;
maxflow(int n) : n(n) {
for (int i = 0; i < n; i++) adj[i].clear();
}
map<pair<int, int>, edge *> es;
int get_cap(int i, int j) { return es[{i, j}]->cap; }
void add_edge(int i, int j, int cap) {
assert(0 <= i && i < n);
assert(0 <= j && j < n);
assert(i != j);
edge *ij = new edge(j, cap);
edge *ji = new edge(i, 0);
ij->pair = ji;
ji->pair = ij;
adj[i].push_back(ij);
adj[j].push_back(ji);
es[{i, j}] = ij;
es[{j, i}] = ji;
}
bool aug(int s, int t) {
for (int i = 0; i < n; i++) parent[i] = NULL;
parent[s] = new edge(s, 0);
vector<int> queue;
queue.push_back(s);
for (int f = 0; f < queue.size(); f++) {
int i = queue[f];
for (edge *e : adj[i]) {
if (e->in_res()) {
int j = e->j;
if (parent[j] == NULL) {
parent[j] = e;
if (j == t) return true;
queue.push_back(j);
}
}
}
}
return false;
}
bool caug(int s, int t, int d) {
for (int i = 0; i < n; i++) parent[i] = NULL;
parent[s] = new edge(s, 0);
vector<int> queue;
queue.push_back(s);
for (int f = 0; f < queue.size(); f++) {
int i = queue[f];
for (edge *e : adj[i]) {
if (e->flow >= d) {
int j = e->j;
if (parent[j] == NULL) {
parent[j] = e;
if (j == t) return true;
queue.push_back(j);
}
}
}
}
return false;
}
void cancel_flow(int s, int t, int d) {
if (s == t) return;
bool res = caug(s, t, d);
assert(res);
for (int i = t; i != s; i = parent[i]->pair->j) {
parent[i]->add_flow(-d);
}
}
void modify_edge(int s, int t, int i, int j, int d) {
edge *e = es[{i, j}];
e->cap += d;
if (e->flow > e->cap) {
flow -= e->flow - e->cap;
cancel_flow(s, i, e->flow - e->cap);
cancel_flow(j, t, e->flow - e->cap);
e->flow = e->cap;
}
}
int max_flow(int s, int t) {
assert(s != t);
while (aug(s, t)) {
int add = INF;
for (int i = t; i != s; i = parent[i]->pair->j) {
add = min(add, parent[i]->res());
}
flow += add;
for (int i = t; i != s; i = parent[i]->pair->j) {
parent[i]->add_flow(add);
}
}
return flow;
}
};
int ctl[C], ctr[1 << C], m[N];
char s[N], t[N];
int main() {
scanf("%s", s);
int n = strlen(s);
for (int i = 0; i < n; i++) {
ctl[s[i] - 'a']++;
m[i] = (1 << C) - 1;
}
int q;
scanf("%d", &q);
while (q--) {
int i;
scanf("%d%s", &i, t);
m[--i] = 0;
for (char *c = t; *c; c++) {
m[i] |= 1 << *c - 'a';
}
}
for (int i = 0; i < n; i++) {
ctr[m[i]]++;
}
maxflow mf(C + (1 << C) + 2);
int st = C + (1 << C), en = st + 1;
for (int i = 0; i < C; i++) {
mf.add_edge(st, i, ctl[i]);
}
for (int j = 0; j < 1 << C; j++) {
mf.add_edge(C + j, en, ctr[j]);
}
for (int i = 0; i < C; i++) {
for (int j = 0; j < 1 << C; j++) {
if (j & (1 << i)) {
mf.add_edge(i, C + j, n);
}
}
}
if (mf.max_flow(st, en) != n) {
puts("Impossible");
} else {
for (int p = 0; p < n; p++) {
t[p] = 0;
for (int i = 0; i < C; i++) {
if (m[p] & (1 << i)) {
if (mf.get_cap(st, i) > 0 && mf.get_cap(i, C + m[p]) > 0 &&
mf.get_cap(C + m[p], en) > 0) {
mf.modify_edge(st, en, st, i, -1);
mf.modify_edge(st, en, i, C + m[p], -1);
mf.modify_edge(st, en, C + m[p], en, -1);
if (mf.max_flow(st, en) == n - p - 1) {
t[p] = 'a' + i;
break;
}
mf.modify_edge(st, en, st, i, +1);
mf.modify_edge(st, en, i, C + m[p], +1);
mf.modify_edge(st, en, C + m[p], en, +1);
mf.max_flow(st, en);
}
}
}
assert(t[p]);
}
t[n] = 0;
printf("%s\n", t);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
char A[N], B[10];
int cnt[6], lim[N], G[N][64];
bool check(int j) {
for (int i = 0; i < 64; i++) {
int sum = 0;
for (int k = 0; k < 6; k++)
if (i & (1 << k)) sum += cnt[k];
if (sum < G[j][i]) return false;
}
return true;
}
int main() {
int m;
scanf("%s%d", A, &m);
int la = strlen(A);
for (int i = 0; i < la; i++) cnt[A[i] - 'a']++;
for (int i = 0; i < m; i++) {
int x;
scanf("%d%s", &x, B);
int res = 0, lb = strlen(B);
for (int j = 0; j < lb; j++) res |= 1 << (B[j] - 'a');
lim[--x] = res;
}
for (int i = 0; i < la; i++)
if (!lim[i]) lim[i] = 63;
for (int i = la - 1; i > -1; i--) {
int u = lim[i];
for (int j = 0; j < 64; j++) {
G[i][j] = G[i + 1][j];
if ((u | j) == j) G[i][j]++;
}
}
for (int i = 0; i < la; i++) {
bool pass = true;
for (int j = 0; j < 6; j++) {
if (cnt[j] <= 0 || !(lim[i] & (1 << j))) continue;
cnt[j]--;
if (check(i + 1)) {
A[i] = (char)(j + 'a');
pass = false;
break;
}
cnt[j]++;
}
if (pass) return cout << "Impossible\n", 0;
}
cout << A << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
class Edge {
public:
Edge(int i, int u, int v, int f, int c, bool b = false)
: id(i), from(u), to(v), flow(f), capacity(c), rid(-1), bReverse(b) {}
int id;
int rid;
int from;
int to;
int flow;
int capacity;
bool bReverse;
int remain() { return capacity - flow; }
void update(int x) { flow += x; }
void shrink(int x = 1) { capacity -= x; }
void pair(int r) { rid = r; }
int reverseEdge() { return rid; }
bool isReverse() { return bReverse; }
};
class Solution {
public:
Solution(int A = 6) : an(A), N(1 << A), size(0), s(-1), t(-1), clock(0) {}
string rename(string& s, vector<pair<int, string>>& rs) {
build(s, rs);
if (maximumFlow() != s.length()) {
return "";
}
string res;
for (int i = 0; i < s.length(); ++i) {
for (int j = 0; j < an; ++j) {
if (check(j, allowed[i])) {
res += 'a' + j;
break;
}
}
}
return res;
}
private:
int an;
int N;
int size;
int s;
int t;
int clock;
vector<vector<int>> graph;
vector<Edge> edges;
vector<int> allowed;
vector<int> charVertex;
vector<int> maskVertex;
unordered_map<unsigned int, int> edgeIds;
vector<int> visited;
unsigned int hash(int u, int v) {
unsigned int res = u;
res = res << 16 | v;
return res;
}
void build(string& str, vector<pair<int, string>>& rs) {
int n = str.length();
size = n;
graph.clear();
graph.resize(N * 2);
s = graph.size() - 2;
t = graph.size() - 1;
int idx = 0;
charVertex.resize(an);
for (int i = 0; i < an; ++i) {
charVertex[i] = idx++;
}
maskVertex.resize(N);
for (int i = 0; i < N; ++i) {
maskVertex[i] = idx++;
}
allowed.resize(n);
fill(allowed.begin(), allowed.end(), N - 1);
for (auto& p : rs) {
int j = p.first;
auto& t = p.second;
allowed[j] = 0;
for (auto c : t) {
allowed[j] |= 1 << (c - 'a');
}
}
edges.clear();
vector<int> cnts(an, 0);
for (auto c : str) {
++cnts[c - 'a'];
}
for (int i = 0; i < an; ++i) {
addEdge(s, charVertex[i], cnts[i]);
}
for (int i = 0; i < an; ++i) {
for (int j = 0; j < N; ++j) {
if (j & (1 << i)) {
addEdge(charVertex[i], maskVertex[j], 100000);
}
}
}
vector<int> mcnts(N, 0);
for (auto m : allowed) {
++mcnts[m];
}
for (int i = 0; i < N; ++i) {
addEdge(maskVertex[i], t, mcnts[i]);
}
visited.resize(graph.size());
fill(visited.begin(), visited.end(), 0);
}
int maximumFlow() {
int flow = 0;
while (true) {
++clock;
int inc = advance(s, 100000);
if (inc == 0) {
break;
}
flow += inc;
}
return flow;
}
int advance(int u, int mx) {
if (u == t) {
return mx;
}
if (visited[u] == clock) {
return 0;
}
visited[u] = clock;
for (auto e : graph[u]) {
int x = edges[e].remain();
if (x > 0) {
int inc = advance(edges[e].to, min(mx, x));
if (inc > 0) {
update(e, inc);
return inc;
}
}
}
return 0;
}
bool check(int c, int mask) {
if (!((1 << c) & mask)) {
return false;
}
int u = charVertex[c];
int v = maskVertex[mask];
int e1 = edgeIds[hash(s, u)];
int e2 = edgeIds[hash(v, t)];
if (edges[e1].flow == 0 || edges[e2].flow == 0) {
return false;
}
update(e1, -1);
vector<int> changed;
changed.push_back(e1);
for (auto e : graph[u]) {
if (edges[e].isReverse()) {
continue;
}
if (edges[e].flow > 0) {
int x = edges[e].to;
update(e, -1);
changed.push_back(e);
for (auto ex : graph[x]) {
if (edges[ex].isReverse()) {
continue;
}
update(ex, -1);
changed.push_back(ex);
break;
}
break;
}
}
if (edges[e2].flow < edges[e2].capacity) {
edges[e2].shrink();
edges[e1].shrink();
return true;
}
update(e2, -1);
changed.push_back(e2);
for (auto e : graph[v]) {
if (!edges[e].isReverse()) {
continue;
}
if (edges[e].flow < 0) {
int re = edges[e].reverseEdge();
update(re, -1);
changed.push_back(re);
int x = edges[e].to;
for (auto ex : graph[x]) {
if (!edges[ex].isReverse()) {
continue;
}
int rex = edges[ex].reverseEdge();
update(rex, -1);
changed.push_back(rex);
break;
}
break;
}
}
edges[e1].shrink();
edges[e2].shrink();
++clock;
if (advance(s, 1)) {
return true;
} else {
edges[e1].shrink(-1);
edges[e2].shrink(-1);
for (auto e : changed) {
update(e, 1);
}
return false;
}
}
void addEdge(int u, int v, int c) {
int idx = edges.size();
edgeIds[hash(u, v)] = idx;
graph[u].push_back(idx);
edges.emplace_back(idx, u, v, 0, c);
edgeIds[hash(v, u)] = ++idx;
graph[v].push_back(idx);
edges.emplace_back(idx, v, u, 0, 0, true);
pair(idx - 1, idx);
}
void pair(int x, int y) {
edges[x].pair(y);
edges[y].pair(x);
}
void update(int x, int v) {
edges[x].update(v);
edges[edges[x].reverseEdge()].update(-v);
}
void shrink(int x) { edges[x].shrink(); }
void print() {
for (int i = 0; i < an; ++i) {
int u = charVertex[i];
int e = edgeIds[hash(s, u)];
cout << "s -> " << i << " : " << edges[e].flow << endl;
}
for (int i = 0; i < N; ++i) {
int v = maskVertex[i];
int e = edgeIds[hash(v, t)];
if (edges[e].flow > 0) {
cout << i << " -> t : " << edges[e].flow << endl;
}
}
}
};
int main(int argc, char** argv) {
char buf[100100] = {0};
scanf("%s", buf);
string s(buf);
int m;
scanf("%d", &m);
vector<pair<int, string>> rs;
rs.reserve(m);
for (int i = 0; i < m; ++i) {
int j = 0;
scanf("%d", &j);
char buf2[10] = {0};
scanf("%s", buf2);
string ss(buf2);
rs.emplace_back(j - 1, ss);
}
Solution sol;
auto res = sol.rename(s, rs);
if (res.empty()) {
printf("Impossible\n");
} else {
printf("%s\n", res.c_str());
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int num[64], cnt[6];
char c[100004], buf[7];
int pos[100004], got[64];
char ans[100004];
bool check() {
for (int i = 0; i < 64; i++)
if (got[i] > num[i]) return 0;
return 1;
}
int main() {
scanf("%s", c);
int n = strlen(c);
for (int i = 0; i < n; i++) cnt[c[i] - 'a']++, pos[i] = 63;
int m;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int p;
scanf("%d%s", &p, buf);
int nn = strlen(buf);
pos[p - 1] = 0;
for (int j = 0; j < nn; j++) pos[p - 1] |= (1 << (buf[j] - 'a'));
}
for (int i = 0; i < 64; i++)
for (int j = 0; j < 6; j++)
if ((1 << j) & i) got[i] += cnt[j];
for (int i = 0; i < n; i++)
for (int j = 0; j < 64; j++)
if (j & pos[i]) num[j]++;
if (!check()) {
puts("Impossible");
return 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 64; j++)
if (j & pos[i]) num[j]--;
for (int j = 0; j < 6; j++)
if (cnt[j] > 0 && ((1 << j) & pos[i])) {
cnt[j]--;
for (int k = 0; k < 64; k++)
if ((1ll << j) & k) got[k]--;
if (check()) {
ans[i] = j;
break;
}
cnt[j]++;
for (int k = 0; k < 64; k++)
if ((1ll << j) & k) got[k]++;
}
}
for (int i = 0; i < n; i++) putchar(ans[i] + 'a');
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
using v2d = vector<vector<T> >;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 10;
int n, m, d[maxN], fr[1 << 6], cnt[6];
char s[maxN];
string ans;
namespace flow {
const int maxN = 1e2;
const int max_cap = 1e9;
int n, s, t, p[maxN], cap[maxN][maxN], flow[maxN][maxN], d[maxN];
vector<int> adj[maxN];
void init(int nn, int source, int sink) {
n = nn;
s = source;
t = sink;
for (int i = 0; i < (int)(n); ++i) {
adj[i].clear();
}
}
void addEdge(int u, int v, int w) {
adj[u].emplace_back(v);
adj[v].emplace_back(u);
cap[u][v] = w;
}
void incCap(int u, int v) { cap[u][v]++; }
void decCap(int u, int v) { cap[u][v]--; }
bool dfs(int u) {
if (u == t) {
return 1;
}
for (auto &v : adj[u]) {
if (p[v] == -1 && flow[u][v] < cap[u][v]) {
p[v] = u;
if (dfs(v)) {
return 1;
}
}
}
return 0;
}
bool incFlow() {
fill(p, p + n, -1);
p[s] = -2;
return dfs(s);
}
bool positiveFlow(int u, int v) { return flow[u][v] != 0; }
void decFlow(int u, int v) {
flow[u][v]--;
flow[v][u]++;
for (int cur = u; cur != s;) {
for (auto nxt : adj[cur]) {
if (d[nxt] < d[cur] && flow[nxt][cur] > 0) {
flow[nxt][cur]--;
flow[cur][nxt]++;
cur = nxt;
break;
}
}
}
for (int cur = v; cur != t;) {
for (auto &nxt : adj[cur]) {
if (d[nxt] > d[cur] && flow[cur][nxt] > 0) {
flow[cur][nxt]--;
flow[nxt][cur]++;
cur = nxt;
break;
}
}
}
}
void relax() {
for (int u = t; u != s; u = p[u]) {
flow[p[u]][u]++;
flow[u][p[u]]--;
}
}
int computeFlow() {
fill(d, d + n, -1);
d[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto &v : adj[u]) {
if (d[v] == -1) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
int maxflow = 0;
while (incFlow()) {
relax();
maxflow++;
}
return maxflow;
}
} // namespace flow
void solve() {
cin >> (s + 1);
n = strlen(s + 1);
fill(d + 1, d + n + 1, (1 << 6) - 1);
cin >> m;
while (m--) {
int pos, mask = 0;
string t;
cin >> pos >> t;
for (auto &c : t) {
mask |= 1 << (c - 'a');
}
d[pos] &= mask;
}
for (int i = 1; i <= (int)(n); ++i) {
fr[d[i]]++;
}
for (int i = 1; i <= (int)(n); ++i) {
cnt[s[i] - 'a']++;
}
int source = (1 << 6) + 6, sink = (1 << 6) + 6 + 1;
flow::init((1 << 6) + 6 + 2, source, sink);
for (int mask = 0; mask < (int)(1 << 6); ++mask) {
flow::addEdge(source, mask, fr[mask]);
for (int i = 0; i < (int)(6); ++i) {
if (mask >> i & 1) {
flow::addEdge(mask, (1 << 6) + i, 1e9);
}
}
}
for (int i = 0; i < (int)(6); ++i) {
flow::addEdge((1 << 6) + i, sink, cnt[i]);
}
if (flow::computeFlow() < n) {
cout << "Impossible";
return;
}
for (int i = 1; i <= (int)(n); ++i) {
for (int j = 0; j < (int)(6); ++j) {
if (d[i] >> j & 1) {
if (flow::positiveFlow(d[i], (1 << 6) + j)) {
flow::decFlow(d[i], (1 << 6) + j);
flow::decCap(source, d[i]);
flow::decCap((1 << 6) + j, sink);
ans += (char)(j + 'a');
break;
}
if (!flow::positiveFlow(source, d[i]) ||
!flow::positiveFlow((1 << 6) + j, sink)) {
continue;
}
flow::decFlow(source, d[i]);
flow::decCap(source, d[i]);
flow::decFlow((1 << 6) + j, sink);
flow::decCap((1 << 6) + j, sink);
if (flow::incFlow()) {
flow::relax();
ans += (char)(j + 'a');
break;
}
flow::incCap(source, d[i]);
flow::incCap((1 << 6) + j, sink);
while (flow::incFlow()) {
flow::relax();
}
}
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, msk[100020], p, fn[80] = {0}, gn[80] = {0}, ccnt[10] = {0};
char s[100020], cm[10], ans[100020];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i < 100010; i++) msk[i] = 63;
cin >> s;
n = (int)strlen(s);
cin >> m;
for (int i = 0; i < m; i++) {
cin >> p >> cm;
p -= 1;
msk[p] = 0;
for (int i = 0; i < strlen(cm); i++)
if (cm[i] >= 'a' && cm[i] <= 'f') msk[p] += 1 << (cm[i] - 'a');
}
for (int i = 0; i < n; i++) {
fn[msk[i]]++;
ccnt[s[i] - 'a'] += 1;
}
for (int i = 0; i < 64; i++) {
for (int j = 0; j < 64; j++)
if ((i & j) == j) gn[i] += fn[j];
}
for (int po = 0; po < n; po++) {
ans[po] = 'u';
for (int i = 0; i < 64; i++)
if ((i & msk[po]) == msk[po]) gn[i] -= 1;
for (int c = 0; c < 6; c++) {
if ((msk[po] & (1 << c)) == 0) continue;
ccnt[c] -= 1;
int co = 1, ccn = 0;
for (int i = 0; i < 64; i++) {
ccn = 0;
for (int j = 0; j < 6; j++)
if (i & (1 << j)) ccn += ccnt[j];
if (ccn < gn[i]) {
co = 0;
break;
}
}
if (co) {
ans[po] = 'a' + c;
break;
} else {
ccnt[c] += 1;
}
}
if (ans[po] == 'u') {
cout << "Impossible" << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 72;
const int S = N - 1, T = N - 2;
int msk[100003];
int n;
bool pos = true;
char ans[100003];
struct edge {
int from, to, flow, cap;
edge* rev;
};
vector<vector<edge*>> V(N);
vector<edge*> all;
void addEdge(int from, int to, int cap) {
edge* dir = new edge;
edge* rev = new edge;
dir->from = from;
dir->to = to;
dir->flow = 0;
dir->cap = cap;
dir->rev = rev;
rev->from = to;
rev->to = from;
rev->flow = cap;
rev->cap = cap;
rev->rev = dir;
V[from].push_back(dir);
V[to].push_back(rev);
all.push_back(dir);
}
void printEdge(edge* e) {
int x = e->from;
string fr;
if (x < 64) {
fr = to_string(x);
}
if (x > 63) {
fr = "";
fr += (char)('a' + x - 64);
}
if (x == S) fr = "S";
if (x == T) fr = "T";
x = e->to;
string tt;
if (x < 64) {
tt = to_string(x);
}
if (x > 63) {
tt = "";
tt += (char)('a' + x - 64);
}
if (x == S) tt = "S";
if (x == T) tt = "T";
cout << "e = (" << fr << ", " << tt << "), cap = " << e->cap
<< ", flow = " << e->flow << (char)10;
}
vector<vector<edge*>> lg(N);
queue<int> Q;
int dist[N];
int dfs(int v, int ile = 10000000) {
if (v == T) {
return ile;
}
int ans = 0;
for (edge* e : lg[v]) {
if (e->flow < e->cap) {
int wlew = dfs(e->to, min(ile - ans, e->cap - e->flow));
e->flow += wlew;
e->rev->flow -= wlew;
ans += wlew;
}
if (ans == ile) {
return ile;
}
}
return ans;
}
int max_flow() {
for (int i = 0; i < N; ++i) dist[i] = -1;
dist[S] = 0;
Q.push(S);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
lg[x].clear();
for (edge* e : V[x]) {
if (dist[e->to] == -1 && e->flow < e->cap) {
Q.push(e->to);
dist[e->to] = dist[x] + 1;
}
if (dist[e->to] == dist[x] + 1) {
lg[x].push_back(e);
}
}
}
if (dist[T] == -1) return 0;
return dfs(S) + max_flow();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
n = s.size();
int cs[6];
for (int i = 0; i < 6; ++i) cs[i] = 0;
int msku[] = {1, 2, 4, 8, 16, 32};
int cnt[64];
for (int i = 0; i < 64; ++i) cnt[i] = 0;
for (int i = 0; i < n; ++i) {
cs[s[i] - 'a']++;
msk[i] = 63;
}
int m;
cin >> m;
cnt[63] = (n - m);
for (int i = 0; i < m; ++i) {
int p;
string t;
cin >> p >> t;
int tmsk = 0;
for (int j = 0; j < t.size(); ++j) {
tmsk |= msku[t[j] - 'a'];
}
msk[p - 1] = tmsk;
cnt[tmsk]++;
}
for (int i = 0; i < 64; ++i) {
for (int j = 0; j < 6; ++j) {
if (i & msku[j]) {
addEdge(j + 64, i, 1000000);
}
}
addEdge(i, T, cnt[i]);
}
for (int i = 0; i < 6; ++i) addEdge(S, i + 64, cs[i]);
for (int i = 0; i < n; ++i) {
ans[i] = '0';
for (int j = 0; j < 6; ++j) {
if (msk[i] & msku[j]) {
for (edge* e : all) {
e->flow = 0;
e->rev->flow = e->cap;
}
if (V[T][msk[i]]->rev->cap == 0 || V[S][j]->cap == 0) continue;
V[T][msk[i]]->rev->cap--;
V[S][j]->cap--;
int zzz = max_flow();
if (zzz == n - i - 1) {
ans[i] = 'a' + j;
break;
}
V[T][msk[i]]->rev->cap++;
V[S][j]->cap++;
}
}
if (ans[i] == '0') {
cout << "Impossible" << (char)10;
return 0;
}
}
for (int i = 0; i < n; ++i) cout << ans[i];
cout << (char)10;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (T e : v) {
os << e << ' ';
}
return os;
}
static constexpr ll mod = 998244353LL;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int n = s.size();
int counts[6] = {};
int m;
cin >> m;
string res(n, '0');
unordered_map<int, int> q;
int mask_counts[64] = {};
mask_counts[63] = n - m;
for (int i = 0; i < m; ++i) {
int idx;
cin >> idx;
idx--;
string tmp;
cin >> tmp;
int mask = 0;
for (char c : tmp) {
mask |= 1 << (c - 'a');
}
q[idx] = mask;
mask_counts[mask]++;
}
for (int i = 0; i < n; ++i) {
counts[s[i] - 'a']++;
}
function<bool()> gut = [&] {
for (int i = 1; i < 64; ++i) {
int cx = 0, mask = i;
for (int j = 0; mask != 0; ++j) {
if (mask & (1 << j)) {
cx += counts[j];
mask ^= 1 << j;
}
}
for (int j = 1; j < 64; ++j) {
if (i & j) {
cx -= mask_counts[j];
}
}
if (cx > 0) {
return false;
}
}
return true;
};
for (int i = 0; i < n; ++i) {
int mask = 63;
if (q.count(i)) {
mask = q[i];
}
mask_counts[mask]--;
for (int j = 0; mask != 0; ++j) {
if (mask & (1 << j)) {
if (counts[j]) {
counts[j]--;
if (gut()) {
res[i] = 'a' + j;
break;
}
counts[j]++;
}
mask ^= 1 << j;
}
}
if (res[i] == '0') {
cout << "Impossible\n";
return 0;
}
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using uint = unsigned int;
constexpr uint MaxL(100000u);
constexpr unsigned short SIGMA(6u);
constexpr unsigned short FULL_STATE((1u << 6u) - 1u);
namespace IOManager {
constexpr uint FILESZ(1500010u);
unsigned char buf[FILESZ];
const unsigned char *ibuf = buf;
struct IOManager {
IOManager() { buf[fread(buf, 1, FILESZ, stdin)] = ' '; }
~IOManager() {}
template <class _Tp>
inline operator _Tp() {
_Tp s = 0u;
for (; !isdigit(*ibuf); ++ibuf)
;
for (; isdigit(*ibuf); ++ibuf) s = (_Tp)(s * 10u + *ibuf - 48u);
return s;
}
template <class _Tp>
inline _Tp operator()(_Tp &rhs) {
_Tp s = 0u;
for (; !isdigit(*ibuf); ++ibuf)
;
for (; isdigit(*ibuf); ++ibuf) s = (_Tp)(s * 10u + *ibuf - 48u);
return rhs = s;
}
inline uint operator()(unsigned char *const &str) {
unsigned char *s = str;
for (; isspace(*ibuf); ++ibuf)
;
for (; !isspace(*ibuf);) *s++ = *ibuf++;
return *s = 0u, s - str;
}
inline uint operator()(char *str) { return (*this)((unsigned char *)str); }
inline uint operator()(const char *str) {
return (*this)((unsigned char *)str);
}
inline uint operator()(const unsigned char *str) {
return (*this)((unsigned char *)str);
}
};
} // namespace IOManager
IOManager::IOManager io;
using IOManager::ibuf;
unsigned char s[MaxL + 1u];
unsigned short vaild[MaxL + 1u];
unsigned short lg2[64u];
uint cnts[6u];
uint cntv[64u];
uint cntsum[64u];
inline bool chk() {
for (uint i = 1; i <= 63u; ++i)
if ((cntsum[i] = cntsum[i ^ (i & -i)] + cnts[lg2[i & -i]]) + cntv[i ^ 63u] >
cntv[63u])
return false;
return true;
}
int main() {
lg2[1u] = 0u;
lg2[2u] = 1u;
lg2[4u] = 2u;
lg2[8u] = 3u;
lg2[16u] = 4u;
lg2[32u] = 5u;
uint n = io(s + 1u), m = io;
for (uint i = 1u; i <= n; ++i) vaild[i] = 63u;
for (uint pos; m; --m) {
for (vaild[pos = io] = 0u; isspace(*ibuf); ++ibuf)
;
for (; !isspace(*ibuf);) vaild[pos] |= (1u << ((*ibuf++) - 'a'));
}
for (uint i = 1u; i <= n; ++i) ++cnts[s[i] - 'a'], ++cntv[vaild[i]];
for (uint i = 0u; i < 6u; ++i)
for (uint j = 0u; j <= 63u; ++j)
if ((j >> i) & 1u) cntv[j] += cntv[j ^ (1u << i)];
if (!chk()) return puts("Impossible"), 0;
unsigned char *Ans = s;
for (uint i = 1u; i <= n; ++i) {
for (uint j = vaild[i]; j != 63u; j = (j + 1u) | vaild[i]) --cntv[j];
--cntv[63u];
for (unsigned short c = 0u; c < 6u; ++c)
if ((vaild[i] >> c) & 1u) {
--cnts[c];
if (chk()) {
*++Ans = c + 'a';
break;
}
++cnts[c];
}
}
fwrite(s + 1, 1, n, stdout);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << ' ' << H;
debug_out(T...);
}
const int INF = 1e9;
const long long MOD = 924844033;
const int MAXN = 1e5 + 5, MAXSGM = 6;
int G[MAXN], C[MAXSGM], M[MAXN];
bool Hall() {
for (int mask = 0; mask < (1 << MAXSGM); mask++) {
int s = 0;
for (int i = 0; i < MAXSGM; i++)
if ((mask >> i) & 1) s += C[i];
int ns = M[mask];
if (s > ns) return 0;
}
return 1;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
string s;
cin >> s;
int n = (int)(s.size());
for (char c : s) C[c - 'a']++;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int p;
cin >> p;
p--;
string t;
cin >> t;
for (char c : t) G[p] |= (1 << (c - 'a'));
}
for (int i = 0; i < n; i++)
if (G[i] == 0) G[i] = (1 << MAXSGM) - 1;
for (int i = 0; i < n; i++)
for (int mask = 0; mask < (1 << MAXSGM); mask++)
if (G[i] & mask) M[mask]++;
if (!Hall()) return cout << "Impossible" << '\n', 0;
;
for (int i = 0; i < n; i++) {
for (int mask = 0; mask < (1 << MAXSGM); mask++)
if (G[i] & mask) M[mask]--;
for (int j = 0; j < MAXSGM; j++)
if ((G[i] >> j) & 1 && C[j]) {
C[j]--;
if (!Hall()) {
C[j]++;
continue;
}
cout << (char)('a' + j);
break;
}
}
cout << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <typename T>
T gcd(T m, T n) {
while (n) {
T t = m % n;
m = n;
n = t;
};
return m;
}
template <typename T>
T exgcd(T a, T b, T& sa, T& ta) {
T q, r, sb = 0, tb = 1, sc, tc;
sa = 1, ta = 0;
if (b) do
q = a / b, r = a - q * b, a = b, b = r, sc = sa - q * sb, sa = sb,
sb = sc, tc = ta - q * tb, ta = tb, tb = tc;
while (b);
return a;
}
template <typename T>
T mul_inv(T a, T b) {
T t1 = a, t2 = b, t3;
T v1 = 1, v2 = 0, v3;
T x;
while (t2 != 1)
x = t1 / t2, t3 = t1 - x * t2, v3 = v1 - x * v2, t1 = t2, t2 = t3, v1 = v2,
v2 = v3;
return (v2 + b) % b;
}
template <typename T>
T powmod(T a, T b, T MOD) {
if (b < 0) return 0;
T rv = 1;
while (b) (b % 2) && (rv = (rv * a) % MOD), a = a * a % MOD, b /= 2;
return rv;
}
template <long long n>
struct BitSize {
enum { Size = BitSize<n / 2>::Size + 1 };
};
template <>
struct BitSize<0> {
enum { Size = 1 };
};
template <>
struct BitSize<1> {
enum { Size = 1 };
};
template <typename TH>
void _dbg(const char* sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << "=" << h << ",";
_dbg(sdbg + 1, t...);
}
const int maxn = 1e5 + 7;
const int maxb = 6;
const int maxbs = 1 << maxb;
int n;
char s[maxn];
int msk[maxn];
int cnt[maxb];
int bcnt[maxbs];
int bcnt2[maxbs];
bool is_valid() {
memcpy(bcnt2, bcnt, sizeof(bcnt));
for (int j = 0; j < maxb; j++)
for (int i = 0; i < maxbs; i++)
if (i & (1 << j)) bcnt2[i] += bcnt2[i ^ (1 << j)];
for (int i = 0; i < maxbs; i++) {
int x = 0;
for (int j = 0; j < maxb; j++)
if (i & (1 << j)) x += cnt[j];
if (x < bcnt2[i]) {
return false;
}
}
return true;
}
int main(int argc, char* argv[]) {
std::cin.sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> s;
n = strlen(s);
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; i++) {
msk[i] = maxbs - 1;
cnt[s[i] - 'a']++;
}
int m;
cin >> m;
while (m--) {
int i;
cin >> i >> s;
i--;
msk[i] = 0;
for (int j = 0; s[j]; j++) msk[i] |= 1 << (s[j] - 'a');
}
memset(bcnt, 0, sizeof(bcnt));
for (int i = 0; i < n; i++) {
bcnt[msk[i]]++;
}
for (int i = 0; i < n; i++) {
bcnt[msk[i]]--;
bool ok = false;
for (int j = 0; j < maxb; j++) {
if (cnt[j] && (msk[i] & (1 << j))) {
cnt[j]--;
if (is_valid()) {
s[i] = 'a' + j;
ok = true;
break;
}
cnt[j]++;
}
}
if (!ok) {
cout << "Impossible" << endl;
return 0;
}
}
s[n] = 0;
cout << s << endl;
return 0;
}
|
#include <bits/stdc++.h>
const int maxn = 1E+5 + 5;
int n, m, lim[maxn];
char s[maxn];
int cnt[6], tim[64];
inline bool check() {
for (int i = 1; i < 64; ++i) {
int tot = 0;
for (int j = 0; j < 6; ++j)
if (i >> j & 1) tot += cnt[j];
if (tot > tim[i]) return false;
}
return true;
}
inline void GT(int S, int v) {
for (int T = 1; T < 64; ++T)
if (S & T) tim[T] += v;
}
int main() {
scanf("%s%d", s + 1, &m), n = strlen(s + 1);
std::fill(lim + 1, lim + n + 1, 63);
for (int i = 1; i <= m; ++i) {
int p;
char c[10];
scanf("%d%s", &p, c);
int r = strlen(c);
lim[p] = 0;
for (int j = 0; j < r; ++j) lim[p] |= 1 << c[j] - 'a';
}
for (int i = 1; i <= n; ++i) ++cnt[s[i] - 'a'], GT(lim[i], 1);
if (!check()) return puts("Impossible"), 0;
for (int i = 1; i <= n; ++i) {
GT(lim[i], -1);
for (int j = 0; j < 6; ++j)
if (lim[i] >> j & 1) {
--cnt[j];
if (check()) {
putchar('a' + j);
break;
}
++cnt[j];
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char s[N];
int cnt[1 << 7];
int match[N];
int need[1 << 7];
int n, m;
bool judge() {
for (int i = 0; i < 64; i++) {
if (need[i] < cnt[i]) return 0;
}
return 1;
}
void remove(int pos, int ch) {
for (int mask = 0; mask < 64; mask++) {
if (mask & (1 << ch)) {
cnt[mask]--;
}
if (mask & match[pos]) {
need[mask]--;
}
}
}
void add(int pos, int ch) {
for (int mask = 0; mask < 64; mask++) {
if (mask & (1 << ch)) {
cnt[mask]++;
}
if (mask & match[pos]) {
need[mask]++;
}
}
}
int ans[N];
int main() {
cin >> s;
n = strlen(s);
for (int i = 0; i < n; i++) {
cnt[1 << (s[i] - 'a')]++;
}
for (int mask = 1; mask < 64; mask++) {
for (int sub = 1; sub < mask; sub++) {
if ((sub & mask) == sub) {
cnt[mask] = cnt[sub] + cnt[mask ^ sub];
}
}
}
for (int i = 1; i <= n; i++) {
match[i] = 63;
}
cin >> m;
int p;
char str[10];
for (int i = 1; i <= m; i++) {
scanf("%d%s", &p, str);
int l = strlen(str);
int mask = 0;
for (int j = 0; j < l; j++) {
mask |= 1 << (str[j] - 'a');
}
match[p] &= mask;
}
for (int mask = 0; mask < 64; mask++) {
for (int i = 1; i <= n; i++) {
if (mask & match[i]) {
need[mask]++;
}
}
}
if (!judge()) {
cout << "Impossible" << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 6; j++) {
if (match[i] & (1 << j)) {
remove(i, j);
if (judge()) {
ans[i] = j;
break;
} else {
add(i, j);
}
}
}
}
for (int i = 1; i <= n; i++) {
cout << (char)(ans[i] + 'a');
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << " = " << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << " = " << h << ", ";
_dbg(sdbg + 1, a...);
}
const int maxn = (1e6) + 7;
const int maxk = 20;
const int inf = (1e9) + 7;
const long long LLinf = (1e18) + 7;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
int ile[maxn];
int stan[maxn];
int suma[maxn];
int xd[maxn];
int pos[maxn];
bool czy() {
for (int i = 0; i < 6; i++)
if (ile[i] < 0) return 0;
for (int i = 0; i < (1 << 6); i++)
if (suma[i] < stan[i]) return 0;
for (int i = 0; i < (1 << 6); i++) {
int cnt = 0;
for (int j = 0; j < 6; j++)
if ((1 << j) & i) cnt += ile[j];
if (cnt < stan[i]) return 0;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
for (auto v : s) ile[v - 'a']++;
int q;
cin >> q;
for (int i = 0; i < ((int)(s).size()); i++) xd[i] = ((1 << 6) - 1);
for (int i = 0; i < q; i++) {
cin >> pos[i];
string sl;
cin >> sl;
int mask = 0;
for (auto v : sl) mask |= (1 << (v - 'a'));
for (int i = 0; i < (1 << 6); i++)
if ((i & mask) == mask) stan[i]++;
xd[pos[i] - 1] = mask;
}
for (int i = 0; i < (1 << 6); i++)
for (int j = 0; j < 6; j++)
if ((1 << j) & i) suma[i] += ile[j];
if (czy() == 0) {
cout << "Impossible\n";
return 0;
}
string wyn;
for (int i = 0; i < ((int)(s).size()); i++) {
for (int j = 0; j < (1 << 6); j++)
if ((j & xd[i]) == xd[i]) stan[j]--;
for (int j = 0; j < 6; j++)
if (((1 << j) & xd[i]) && ile[j] > 0) {
ile[j]--;
for (int i = 0; i < (1 << 6); i++) suma[i] = 0;
for (int i = 0; i < (1 << 6); i++)
for (int j = 0; j < 6; j++)
if ((1 << j) & i) suma[i] += ile[j];
if (czy()) {
wyn.push_back(j + 'a');
break;
}
ile[j]++;
for (int i = 0; i < (1 << 6); i++) suma[i] = 0;
for (int i = 0; i < (1 << 6); i++)
for (int j = 0; j < 6; j++)
if ((1 << j) & i) suma[i] += ile[j];
}
}
cout << wyn;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
char str[MAX];
int sets[MAX];
int a[10];
char op[10];
int num[(1 << 6)];
int have[(1 << 6)];
int tot[(1 << 6)];
int getnum(int x) {
int ans = 0;
while (x) {
ans += (x & 1);
x >>= 1;
}
return ans;
}
void del(int pos, int v) {
for (int i = 0; i < (1 << 6); i++) {
if (sets[pos] & i) {
tot[i]--;
}
if (i & (1 << v)) {
have[i]--;
}
}
}
void add(int pos, int v) {
for (int i = 0; i < (1 << 6); i++) {
if (sets[pos] & i) {
tot[i]++;
}
if (i & (1 << v)) {
have[i]++;
}
}
}
bool check() {
for (int i = 0; i < (1 << 6); i++) {
if (have[i] > tot[i]) {
return 0;
}
}
return 1;
}
int main() {
scanf("%s", str);
for (int i = 1; i < (1 << 6); i++) {
num[i] = getnum(i);
}
int len = strlen(str);
for (int i = 0; i < len; i++) {
sets[i] = (1 << 6) - 1;
}
int n;
int pos;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &pos);
scanf("%s", op);
int lens = strlen(op);
int s = 0;
for (int j = 0; j < lens; j++) {
s |= (1 << (op[j] - 'a'));
}
sets[pos - 1] = s;
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < (1 << 6); j++) {
if (j & (1 << (str[i] - 'a'))) {
have[j]++;
}
if (sets[i] & j) {
tot[j]++;
}
}
}
string ans;
if (!check()) {
cout << "Impossible\n";
} else {
for (int i = 0; i < len; i++) {
for (int j = 0; j < 6; j++) {
if (sets[i] & (1 << j)) {
del(i, j);
if (check()) {
ans += (char)(j + 'a');
break;
}
add(i, j);
}
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
std::string s;
int n, m;
std::vector<int> cnt, tot, f;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> s >> m;
n = s.length();
tot.resize(64);
for (char c : s)
for (int i = 0; i < 64; ++i)
if (i >> (c - 'a') & 1) ++tot[i];
f.assign(n, 63);
for (int i = 0; i < m; ++i) {
int pos;
std::cin >> pos;
--pos;
std::string t;
std::cin >> t;
int x = 0;
for (char c : t) x |= 1 << (c - 'a');
f[pos] = x;
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < 64; ++j)
if ((j & f[i]) == f[i]) --tot[j];
if (*min_element(tot.begin(), tot.end()) < 0) {
std::cout << "Impossible\n";
return 0;
}
s.clear();
for (int i = 0; i < n; ++i) {
for (int c = 0;; ++c) {
if ((f[i] >> c & 1) && [&]() {
for (int j = 0; j < 64; ++j)
if (tot[j] + ((j & f[i]) == f[i]) - (j >> c & 1) < 0)
return false;
return true;
}()) {
for (int j = 0; j < 64; ++j)
tot[j] += ((j & f[i]) == f[i]) - (j >> c & 1);
s += c + 'a';
break;
}
}
}
std::cout << s << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T>
inline void chkmax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void chkmin(T &x, T y) {
if (x > y) x = y;
}
inline int read() {
int x = 0;
char c = getchar();
while (c < 48) c = getchar();
while (c > 47) x = x * 10 + (c ^ 48), c = getchar();
return x;
}
const int maxn = 1e5 + 10;
char str[maxn], ans[maxn];
int n, m, cnt[6], state[maxn];
int data[1 << 6];
const int inf = 1e9;
int S, T, ecnt, h[100], cur[100], dis[100];
struct edges {
int nxt, to, w;
edges(int x = 0, int y = 0, int z = 0) : nxt(x), to(y), w(z) {}
} e[2000];
void addline(int u, int v, int w) {
e[++ecnt] = edges(h[u], v, w), h[u] = ecnt;
e[++ecnt] = edges(h[v], u, 0), h[v] = ecnt;
}
bool bfs() {
static int Q[100];
memcpy(cur, h, sizeof h);
memset(dis, 0, sizeof dis);
int l = 1, r = 1;
Q[1] = S, dis[S] = 1;
while (l <= r) {
int u = Q[l++];
for (int i = h[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (e[i].w && !dis[v]) {
Q[++r] = v, dis[v] = dis[u] + 1;
if (v == T) return 1;
}
}
}
return 0;
}
int dfs(int u, int f) {
if (u == T || !f) {
return f;
}
int res = 0, tmp;
for (int &i = cur[u]; i && f; i = e[i].nxt) {
int v = e[i].to;
if (e[i].w && dis[v] == dis[u] + 1) {
if (!(tmp = dfs(v, min(f, e[i].w)))) {
dis[v] = 0;
continue;
}
res += tmp, f -= tmp, e[i].w -= tmp, e[i ^ 1].w += tmp;
}
}
return res;
}
int dinic() {
int res = 0;
while (bfs()) {
res += dfs(S, inf);
}
return res;
}
bool check() {
ecnt = 1;
memset(h, 0, sizeof h);
S = 90, T = 91;
int sum = 0;
for (int i = (0), iend = (5); i <= iend; ++i)
if (cnt[i]) addline(S, 70 + i, cnt[i]);
for (int i = (1), iend = ((1 << 6) - 2); i <= iend; ++i)
if (data[i]) addline(i, T, data[i]), sum += data[i];
for (int i = (0), iend = (5); i <= iend; ++i)
if (cnt[i]) {
for (int j = (1), jend = ((1 << 6) - 2); j <= jend; ++j)
if (data[j] && (j >> i & 1)) {
addline(70 + i, j, inf);
}
}
return dinic() == sum;
}
void solve() {
scanf("%s %d", str + 1, &m);
n = strlen(str + 1);
for (int i = (1), iend = (m); i <= iend; ++i) {
int pos = read();
static char tmp[10];
scanf("%s", tmp + 1);
for (int j = (1), jend = (strlen(tmp + 1)); j <= jend; ++j)
state[pos] |= 1 << (tmp[j] - 'a');
}
for (int i = (1), iend = (n); i <= iend; ++i) {
cnt[str[i] - 'a']++;
if (!state[i]) state[i] = (1 << 6) - 1;
data[state[i]]++;
}
if (!check()) return puts("Impossible"), void();
for (int i = (1), iend = (n); i <= iend; ++i) {
data[state[i]]--;
for (int j = (0), jend = (5); j <= jend; ++j)
if ((state[i] >> j & 1) && cnt[j]) {
cnt[j]--;
if (check()) {
ans[i] = 'a' + j;
break;
}
cnt[j]++;
}
}
for (int i = (1), iend = (n); i <= iend; ++i) putchar(ans[i]);
}
signed main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
int cnt[6];
int bms[100005];
int reqs[1 << 6];
bool chk() {
for (int i = 0; i < (signed)(1 << 6); i++) {
int amt = 0;
for (int j = 0; j < (signed)(6); j++)
if (i & (1 << j)) amt += cnt[j];
if (amt < reqs[i]) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string second;
cin >> second;
n = second.length();
for (int i = 0; i < (signed)(n); i++) cnt[second[i] - 'a']++;
int m;
cin >> m;
for (int i = 0; i < (signed)(m); i++) {
int x;
string t;
cin >> x >> t;
int bm = 0;
for (int j = 0; j < (signed)(t.length()); j++) {
bm += 1 << (t[j] - 'a');
}
bms[x] = bm;
}
for (int i = (1); i <= (signed)(n); i++) {
if (bms[i] == 0) bms[i] = 63;
for (int j = 0; j < (signed)(1 << 6); j++) {
if ((bms[i] & j) == bms[i]) {
++reqs[j];
}
}
}
if (!chk()) {
cout << "Impossible\n";
return 0;
}
string ans = "";
for (int i = (1); i <= (signed)(n); i++) {
int cbm = bms[i];
for (int j = 0; j < (signed)(1 << 6); j++)
if ((cbm & j) == cbm) --reqs[j];
for (int j = 0; j < (signed)(6); j++)
if ((cbm & (1 << j)) && cnt[j] != 0) {
--cnt[j];
if (chk()) {
ans += (char)('a' + j);
break;
}
++cnt[j];
}
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int y;
int c;
int f;
edge(){};
edge(int y, int c, int f) : y(y), c(c), f(f){};
};
const int N = 100;
vector<edge> e;
vector<int> g[N];
int edge_num[N][N];
int char_vertex[6];
int mask_vertex[N];
int used[N];
int cc = 0;
int s, t;
void add_edge(int x, int y, int c) {
edge_num[x][y] = ((int)(e).size());
g[x].push_back(((int)(e).size()));
e.push_back(edge(y, c, 0));
edge_num[y][x] = ((int)(e).size());
g[y].push_back(((int)(e).size()));
e.push_back(edge(x, 0, 0));
}
int rem(int num) { return e[num].c - e[num].f; }
int dfs(int x, int mx) {
if (x == t) return mx;
if (used[x] == cc) return 0;
used[x] = cc;
for (auto num : g[x]) {
if (rem(num)) {
int pushed = dfs(e[num].y, min(mx, rem(num)));
if (pushed) {
e[num].f += pushed;
e[num ^ 1].f -= pushed;
return pushed;
}
}
}
return 0;
}
bool check(int ch, int mask) {
if ((mask & (1 << ch)) == 0) return false;
int cv = char_vertex[ch];
int mv = mask_vertex[mask];
int e1 = edge_num[s][cv];
int e2 = edge_num[mv][t];
if (e[e1].f == 0) return false;
e[e1].f--;
e[e1 ^ 1].f++;
vector<int> affected_edges;
affected_edges.push_back(e1);
for (auto x : g[cv]) {
if ((x & 1) == 0 && e[x].f > 0) {
affected_edges.push_back(x);
e[x].f--;
e[x ^ 1].f++;
int y = e[x].y;
for (auto x2 : g[y]) {
if ((x2 & 1) == 0) {
affected_edges.push_back(x2);
e[x2].f--;
e[x2 ^ 1].f++;
break;
}
}
break;
}
}
if (e[e2].f < e[e2].c) {
e[e1].c--;
e[e2].c--;
return true;
}
affected_edges.push_back(e2);
e[e2].f--;
e[e2 ^ 1].f++;
for (auto x : g[mv]) {
if ((x & 1) == 1 && e[x].f < 0) {
affected_edges.push_back(x ^ 1);
e[x].f++;
e[x ^ 1].f--;
int y = e[x].y;
for (auto x2 : g[y]) {
if ((x2 & 1) == 1) {
affected_edges.push_back(x2 ^ 1);
e[x2].f++;
e[x2 ^ 1].f--;
break;
}
}
break;
}
}
cc++;
e[e1].c--;
e[e2].c--;
if (dfs(s, 1))
return true;
else {
e[e1].c++;
e[e2].c++;
for (auto x : affected_edges) {
e[x].f++;
e[x ^ 1].f--;
}
return false;
}
}
char buf[100043];
string allowed[100043];
int allowed_mask[100043];
int main() {
s = 70;
t = 71;
scanf("%s", buf);
string z = buf;
int n = ((int)(z).size());
int m;
scanf("%d", &m);
for (int i = 0; i < n; i++) {
allowed[i] = "abcdef";
allowed_mask[i] = 63;
}
for (int i = 0; i < m; i++) {
int idx;
scanf("%d", &idx);
--idx;
scanf("%s", buf);
allowed[idx] = buf;
allowed_mask[idx] = 0;
for (auto x : allowed[idx]) {
allowed_mask[idx] |= (1 << (x - 'a'));
}
}
for (int i = 0; i < 6; i++) char_vertex[i] = i;
for (int i = 0; i < (1 << 6); i++) mask_vertex[i] = i + 6;
for (int i = 0; i < (1 << 6); i++)
for (int j = 0; j < 6; j++)
if (i & (1 << j)) add_edge(char_vertex[j], mask_vertex[i], 100000);
for (int i = 0; i < 6; i++) {
int cnt = 0;
for (int j = 0; j < n; j++)
if (z[j] == 'a' + i) cnt++;
add_edge(s, char_vertex[i], cnt);
}
for (int i = 0; i < (1 << 6); i++) {
int cnt = 0;
for (int j = 0; j < n; j++)
if (allowed_mask[j] == i) cnt++;
add_edge(mask_vertex[i], t, cnt);
}
int flow = 0;
while (true) {
cc++;
int p = dfs(s, 100000);
if (p)
flow += p;
else
break;
}
if (flow != n) {
puts("Impossible");
return 0;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 6; j++) {
if (check(j, allowed_mask[i])) {
printf("%c", j + 'a');
break;
}
}
puts("");
}
|
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e18;
const int N = 100005;
const int E = 2000006;
vector<int> g[N];
int ne;
struct Edge {
int from, to;
long long flow, cap;
} edge[E];
int lvl[N], vis[N], pass, start = N - 2, target = N - 1;
int qu[N], qt, px[N];
long long run(int s, int sink, long long minE) {
if (s == sink) return minE;
long long ans = 0;
for (; px[s] < (int)g[s].size(); px[s]++) {
int e = g[s][px[s]];
auto &v = edge[e], &rev = edge[e ^ 1];
if (lvl[v.to] != lvl[s] + 1 || v.flow >= v.cap) continue;
long long tmp = run(v.to, sink, min(minE, v.cap - v.flow));
v.flow += tmp, rev.flow -= tmp;
ans += tmp, minE -= tmp;
if (minE == 0) break;
}
return ans;
}
bool bfs(int source, int sink) {
qt = 0;
qu[qt++] = source;
lvl[source] = 1;
vis[source] = ++pass;
for (int i = 0; i < qt; i++) {
int u = qu[i];
px[u] = 0;
if (u == sink) return true;
for (int e : g[u]) {
auto v = edge[e];
if (v.flow >= v.cap || vis[v.to] == pass) continue;
vis[v.to] = pass;
lvl[v.to] = lvl[u] + 1;
qu[qt++] = v.to;
}
}
return false;
}
long long flow(int source = start, int sink = target) {
long long ans = 0;
while (bfs(source, sink)) ans += run(source, sink, oo);
return ans;
}
void addEdge(int u, int v, long long c = 1, long long rc = 0) {
edge[ne] = {u, v, 0, c};
g[u].push_back(ne++);
edge[ne] = {v, u, 0, rc};
g[v].push_back(ne++);
}
void reset_flow() {
for (int i = 0; i < ne; i++) edge[i].flow = 0;
}
char s[N];
int m, cnt_letter[300], cnt_bit[1 << 7];
int get_bit() {
int ans = 0;
for (int i = 0; s[i]; i++) ans |= 1 << int(s[i] - 'a');
return ans;
}
int bit[N];
int main() {
int n;
scanf("%s", s);
n = (int)strlen(s);
for (int i = 0; s[i]; i++) cnt_letter[int(s[i] - 'a')]++;
scanf("%d", &m);
for (int i = 0; i < n; i++) bit[i] = (1 << 6) - 1;
for (int i = 0; i < m; i++) {
int pos;
scanf("%d %s", &pos, s);
pos--;
bit[pos] = get_bit();
}
for (int i = 0; i < n; i++) cnt_bit[bit[i]]++;
for (int i = 0; i < 6; i++) {
addEdge(start, i, cnt_letter[i]);
}
for (int i = 0; i < 64; i++) {
addEdge(10 + i, target, cnt_bit[i]);
}
for (int i = 0; i < 6; i++)
if (cnt_letter[i])
for (int j = 0; j < 64; j++)
if (cnt_bit[j])
if ((1 << i) & j) addEdge(i, 10 + j, oo);
if (flow() < n) return printf("Impossible\n"), 0;
for (int i = 0; i < n; i++) {
edge[2 * (6 + bit[i])].cap--;
for (int j = 0; j < 6; j++)
if (bit[i] & (1 << j)) {
if (!edge[2 * j].cap) continue;
edge[2 * j].cap--;
reset_flow();
if (flow() == n - i - 1) {
s[i] = char(j + 'a');
break;
}
edge[2 * j].cap++;
}
}
s[n] = 0;
printf("%s\n", s);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, num[70], cnt[100005][70], sta[100005];
char S[100005];
string out;
int main() {
scanf("%s", S + 1);
n = strlen(S + 1);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (1 << 6); j++)
if (j & (1 << (S[i] - 'a'))) num[j]++;
}
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
int id;
char s[10];
scanf("%d", &id);
scanf("%s", s + 1);
int len = strlen(s + 1);
for (int j = 1; j <= len; j++) sta[id] |= (1 << (s[j] - 'a'));
}
for (int i = 1; i <= n; i++)
if (!sta[i]) sta[i] = 63;
for (int i = n; i >= 1; i--) {
for (int j = 0; j < 64; j++) {
cnt[i][j] = cnt[i + 1][j];
if ((j & sta[i]) == sta[i]) cnt[i][j]++;
}
}
for (int i = 1; i <= n; i++) {
bool can = 0;
for (int t = 0; t < 6; t++) {
if (num[1 << t] > 0 && (sta[i] & (1 << t)) > 0) {
bool can1 = 1;
for (int s = 0; s < 64; s++)
if (cnt[i + 1][s] > num[s] - (((s & (1 << t)) > 0))) {
can1 = 0;
break;
}
can = can1;
if (can) {
out += t + 'a';
for (int s = 0; s < 64; s++)
if ((s & (1 << t)) > 0) num[s]--;
break;
}
}
}
if (!can) {
printf("Impossible\n");
return 0;
}
}
cout << out << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-13;
const long double PI = acos(-1);
const int INF = (int)1e9;
const long long INFF = (long long)1e18;
const int mod = (int)1e9 + 7;
const int MXN = (int)2e5 + 7;
char s[MXN];
int cnt[MXN];
int num[MXN], num2[MXN], a[MXN];
bool check() {
for (int i = 0; i < 1 << 6; i++) num2[i] = num[i];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 1 << 6; j++) {
if (j & (1 << i)) num2[j] += num2[j ^ (1 << i)];
}
}
for (int i = 0; i < 1 << 6; i++) {
int sum = 0;
for (int j = 0; j < 6; j++)
if (i & (1 << j)) sum += cnt[j];
if (sum < num2[i]) return false;
}
return true;
}
char ans[MXN];
int main() {
scanf("%s", s + 1);
int l = strlen(s + 1);
for (int i = 1; s[i]; i++) {
cnt[s[i] - 'a']++;
a[i] = (1 << 6) - 1;
}
int m;
scanf("%d", &m);
while (m--) {
int p;
scanf("%d %s", &p, s + 1);
a[p] = 0;
for (int i = 1; s[i]; i++) a[p] ^= (1 << (s[i] - 'a'));
}
for (int i = 1; i <= l; i++) {
num[a[i]]++;
}
for (int i = 1; i <= l; i++) {
num[a[i]]--;
for (int j = 0; j < 6; j++) {
if (!cnt[j] || !(a[i] & (1 << j))) continue;
cnt[j]--;
if (check()) {
ans[i] = j + 'a';
break;
} else {
cnt[j]++;
}
}
if (!ans[i]) {
puts("Impossible");
return 0;
}
}
printf("%s\n", ans + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
inline int read() {
char ch;
bool sign = 0;
int res = 0;
while (!isdigit(ch = getchar()))
if (ch == '-') sign = 1;
while (isdigit(ch)) res = res * 10 + (ch ^ 48), ch = getchar();
if (sign)
return -res;
else
return res;
}
const int maxn = 100000 + 10;
const long long inf = 68721573904;
const int MAXM = 3;
const int MOD[MAXM] = {469762049, 998244353, 1004535809};
const int mod = 998244353;
const int maxm = 6;
char s[maxn];
int suf[maxn][1 << maxm];
int dp[maxn][1 << maxm];
int can[maxn];
int n, m;
int has[maxm];
inline int check(int p) {
for (auto i = (0); i < (1 << maxm); ++i) {
int now = 0;
for (auto j = (0); j < (maxm); ++j) now += (((1 << j) & i) >> j) * has[j];
if (now < dp[p][i]) return 0;
}
return 1;
}
inline int solve() {
string t;
for (auto i = (1); i <= (n); ++i) has[s[i] - 'a']++;
for (auto i = (1); i <= (n); ++i) {
for (auto j = (0); j < (maxm); ++j) {
if (!((1 << j) & can[i])) continue;
has[j]--;
if (!check(i + 1))
has[j]++;
else {
t.push_back(j + 'a');
break;
}
}
if (t.length() != i) return 0;
}
printf("%s", t.c_str());
return 1;
}
int main(void) {
scanf("%s", s + 1);
n = strlen(s + 1);
scanf("%d", &(m));
for (auto i = (1); i <= (n); ++i)
suf[i][(1 << 6) - 1]++, can[i] = (1 << 6) - 1;
for (auto i = (1); i <= (m); ++i) {
int p;
char t[10];
scanf("%d %s", &p, t);
int now = 0;
for (int j = 0; t[j]; ++j) now |= (1 << (t[j] - 'a'));
suf[p][now]++;
suf[p][(1 << 6) - 1]--;
can[p] = now;
}
for (auto i = (n); i >= (1); --i) {
for (auto j = (0); j < (1 << 6); ++j) {
suf[i][j] += suf[i + 1][j];
for (int k = j; k > 0; k = (k - 1) & j) {
dp[i][j] += suf[i][k];
}
}
}
solve() ? puts("") : puts("Impossible");
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
using namespace std;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN];
int U[6], D[1 << 6], dp[1 << 6];
string s;
bool Match() {
int S = 0;
memset(dp, 0, sizeof(dp));
for (int i = 0; i < 64; i++) dp[63 - i] = D[i], S += D[i];
for (int j = 0; j < 6; j++)
for (int i = 0; i < 64; i++)
if (i & (1 << j)) dp[i ^ (1 << j)] += dp[i];
for (int i = 0; i < 64; i++) {
int sum = 0;
for (int j = 0; j < 6; j++)
if (i & (1 << j)) sum += U[j];
if (sum > S - dp[i]) return 0;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s >> m;
n = s.size();
for (char ch : s) U[ch - 'a']++;
for (int i = 1; i <= n; i++) A[i] = 63;
while (m--) {
cin >> x >> s;
int mask = 0;
for (char ch : s) mask |= (1 << (ch - 'a'));
A[x] = mask;
}
for (int i = 1; i <= n; i++) D[A[i]]++;
if (!Match()) return cout << "Impossible" << '\n', 0;
for (int i = 1; i <= n; i++) {
D[A[i]]--;
for (int j = 0; j < 6; j++)
if (A[i] & (1 << j)) {
U[j]--;
if (Match()) {
cout << (char)('a' + j);
break;
}
U[j]++;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 5;
const int N = 105;
int dp[100005], dp1[65], a[6] = {0};
vector<int> g[65];
int ask[65] = {0}, sum[65] = {0};
int check() {
int flag = 1;
for (int i = 1; i < 64; i++) {
if (sum[i] - ask[i] > 0) flag = 0;
if (!flag) break;
}
return flag;
}
int main() {
ios::sync_with_stdio(false);
string x;
cin >> x;
int len = (int)x.size();
for (auto &t : x) a[t - 'a']++;
int m;
cin >> m;
for (int i = 0; i < len; i++) dp[i] = 63;
for (int i = 1; i <= m; i++) {
int id;
string y;
cin >> id >> y;
int tmp = 0;
for (auto &t : y) tmp |= (1 << ((int)(t - 'a')));
dp[id - 1] = tmp;
}
for (int i = 0; i < len; i++) dp1[dp[i]]++;
long long vis[65] = {0};
map<int, int> ss[64];
for (int i = 1; i < 64; i++) {
for (int j = 0; j < 6; j++) {
if (i & (1 << j)) {
sum[i] += a[j];
for (int r = 1; r < 64; r++)
if (r & (1 << j)) vis[i] |= ((long long)1 << r);
}
}
for (int j = 0; j < 64; j++) {
if (vis[i] & ((long long)1 << j)) {
ss[i][j] = 1;
ask[i] += dp1[j];
}
}
}
string ans = "";
int flag = 1;
for (int i = 0; i < len; i++) {
int tmp = dp[i];
dp1[dp[i]]--;
for (int j = 0; j < 64; j++) {
if (ss[j][dp[i]]) ask[j]--;
}
flag = 1;
for (int j = 0; j < 6; j++) {
if (tmp & (1 << j) && a[j]) {
a[j]--;
for (int r = 0; r < 64; r++) {
if (r & (1 << j)) {
sum[r]--;
}
}
if (check()) {
ans += 'a' + j;
flag = 0;
break;
}
a[j]++;
for (int r = 0; r < 64; r++) {
if (r & (1 << j)) {
sum[r]++;
}
}
}
}
if (flag) break;
}
if (flag)
printf("Impossible\n");
else
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
char A[N], L[N][10];
bool vis[N];
vector<int> Q[6][6];
int stk[10], cnt = 0;
bool M[6];
int tar[100], par[N];
void ff(int x) {
int l = strlen(L[x]);
for (int j = 0; j < l; j++) Q[L[x][j] - 'a'][A[x] - 'a'].push_back(x);
}
void Swap(int idx, int ins) {
int tn = 0, op = idx;
tar[tn++] = idx;
while (par[op] != -1) op = tar[tn++] = par[op];
int tt = tn;
while (tt > 0) {
int p = tar[--tt], ins1 = A[p] - 'a';
A[p] = (char)(ins + 'a');
ff(p);
ins = ins1;
}
}
bool f(int idx, int G) {
memset(M, 0, sizeof(M));
cnt = 0;
stk[cnt++] = idx;
if (G == (A[stk[0]] - 'a')) return true;
M[(A[stk[0]] - 'a')] = true;
par[idx] = -1;
while (cnt > 0) {
int p = stk[--cnt];
int u = (A[p] - 'a');
for (int j = 0; j < 6; j++) {
vector<int>& q = Q[u][j];
while (!q.empty()) {
int g = q.back();
if (vis[g] || (A[g] - 'a') != j)
q.pop_back();
else {
if (!M[j]) {
M[j] = true, stk[cnt++] = g, par[g] = p;
if (G == j) {
Swap(g, j);
return true;
}
}
break;
}
}
}
}
return false;
}
int W[N], wn = 0;
int main() {
int m;
scanf("%s %d", A, &m);
int la = strlen(A);
for (int i = 0; i < m; i++) {
int x;
scanf("%d", &x);
x--;
scanf("%s", L[x]);
ff(x);
W[wn++] = x;
}
for (int i = 0; i < la; i++) {
if (strlen(L[i]) == 0) {
strcpy(L[i], "abcdef");
ff(i);
}
}
for (int k = 0; k < wn; k++) {
int i = W[k];
bool pass = false;
for (int j = 0; j < strlen(L[i]); j++)
if (f(i, L[i][j] - 'a')) {
pass = true;
break;
}
if (!pass) return cout << "Impossible\n", 0;
}
for (int i = 0; i < la; i++) {
bool pass = false;
vector<int> tmp;
tmp.clear();
for (int j = 0; j < strlen(L[i]); j++) tmp.push_back(L[i][j] - 'a');
sort(tmp.begin(), tmp.end());
for (int j = 0; j < strlen(L[i]); j++)
if (f(i, tmp[j])) {
pass = true;
vis[i] = true;
break;
}
if (!pass) return cout << "Impossible\n", 0;
}
cout << A << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100, Z = 1 << 6, D = 6;
char s[N];
bool v[N][D], nemp[N];
int un[Z][N], ct[Z], sz[Z];
bool check() {
for (int i = 1; i < Z; i++) {
if (ct[i] > sz[i]) {
return false;
}
}
return true;
}
void add(int i, int d) {
int g = Z - 1 - (1 << i);
for (int j = g;; j = (j - 1) & g) {
ct[j | (1 << i)] += d;
if (j == 0) break;
}
}
int main() {
int n, m;
scanf("%s%d", s, &m);
n = strlen(s);
for (int i = 0; i < m; i++) {
int p;
char t[7];
scanf("%d %s", &p, t);
p--;
for (int j = strlen(t) - 1; j >= 0; j--) {
v[p][t[j] - 'a'] = 1;
}
nemp[p] = 1;
}
for (int i = 0; i < n; i++) {
if (!nemp[i]) {
for (int j = 0; j < D; j++) v[i][j] = 1;
}
for (int j = 0; j < D; j++) {
if (v[i][j]) {
un[1 << j][i]++;
sz[1 << j]++;
}
}
ct[1 << (s[i] - 'a')]++;
}
for (int i = 0; i < Z; i++) {
if ((i & -i) != i) {
ct[i] = ct[i & -i] + ct[i ^ (i & -i)];
for (int j = 0; j < n; j++) {
for (int k = 0; k < D; k++) {
if ((i >> k) & 1) un[i][j] += un[1 << k][j];
}
}
for (int j = 0; j < n; j++) {
sz[i] += un[i][j] > 0;
}
}
}
string r;
for (int i = 0; i < n; i++) {
for (int j = 0; j < Z; j++) {
for (int k = 0; k < D; k++) {
if (((j >> k) & 1) && v[i][k]) {
un[j][i]--;
if (un[j][i] == 0) sz[j]--;
}
}
}
for (int j = 0; j < D; j++) {
if (v[i][j] && ct[1 << j]) {
add(j, -1);
if (check()) {
r.push_back(j + 'a');
break;
}
add(j, 1);
}
}
}
if (r.empty())
printf("Impossible");
else
printf("%s", r.c_str());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char s[maxn], t[maxn], res[maxn];
int num[6], sum[64], mask[maxn], cnt[64];
bool check() {
int all = 0;
for (int i = 0; i < 64; i++) all += (sum[i] = cnt[i]);
for (int i = 0; i < 6; i++)
for (int j = 0; j < 64; j++)
if (~j >> i & 1) sum[j | (1 << i)] += sum[j];
for (int i = 0; i < 64; i++) {
int now = 0;
for (int j = 0; j < 6; j++)
if (i >> j & 1) now += num[j];
if (now > all - sum[63 - i]) return false;
}
return true;
}
int main() {
int n, m;
scanf("%s", s + 1);
n = strlen(s + 1);
scanf("%d", &m);
for (int i = 1; i <= n; i++) mask[i] = 63;
while (m--) {
int p;
scanf("%d", &p);
mask[p] = 0;
scanf("%s", t);
for (int i = 0; t[i]; i++) mask[p] |= (1 << (t[i] - 'a'));
}
for (int i = 1; i <= n; i++) {
num[s[i] - 'a']++;
cnt[mask[i]]++;
}
if (!check()) return 0 * puts("Impossible\n");
for (int i = 1; i <= n; i++) {
bool ok = false;
for (int j = 0; j < 6; j++) {
if (num[j] && ((mask[i] >> j) & 1)) {
num[j]--;
cnt[mask[i]]--;
if (check()) {
res[i] = j + 'a';
ok = true;
break;
}
num[j]++;
cnt[mask[i]]++;
}
}
if (!ok) return 0 * puts("Impossible\n");
}
printf("%s\n", res + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int mask[100100];
int maskc[1 << 6];
int chc[10];
const int ss = 75, tt = 76;
const int N = 100;
struct Edge {
int v, r;
long long f, c, p;
};
vector<Edge> adj[N];
int sz[N];
long long mc;
vector<Edge> mcf_edges;
void init(int n = N) {
mc = 0;
fill(sz, sz + n, 0);
fill(adj, adj + n, vector<Edge>());
}
void add_edge(int a, int b, long long c = 1, long long p = 0) {
mc = max(mc, c);
adj[a].push_back({b, sz[b]++, 0, c, p});
adj[b].push_back({a, sz[a]++, 0, 0, -p});
}
int get_flow(int u, int v) {
int z = -1;
for (int i = 0; i < adj[u].size(); ++i) {
if (adj[u][i].v == v) {
z = i;
break;
}
}
assert(z != -1);
return adj[u][z].f;
}
void add_flow_cap(int u, int v, int flow_amt, int cap_amt) {
int z = -1;
for (int i = 0; i < adj[u].size(); ++i) {
if (adj[u][i].v == v) {
z = i;
break;
}
}
assert(z != -1);
adj[u][z].f += flow_amt;
adj[u][z].c += cap_amt;
int oidx = adj[u][z].r;
adj[v][oidx].f -= flow_amt;
}
int pa[N];
int ei[N];
int ca[N];
int augment(int ss, int tt) {
memset(ca, 0, sizeof ca);
ca[ss] = 1e9;
queue<int> q;
q.push(ss);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < adj[u].size(); ++i) {
auto& e = adj[u][i];
if (e.f == e.c) {
continue;
}
if (!ca[e.v]) {
ca[e.v] = min(e.c - e.f, (long long)ca[u]);
pa[e.v] = u;
ei[e.v] = i;
q.push(e.v);
}
}
}
if (!ca[tt]) {
return 0;
}
int x = tt;
int flw = ca[tt];
while (x != ss) {
int p = pa[x];
adj[p][ei[x]].f += flw;
int oidx = adj[p][ei[x]].r;
adj[x][oidx].f -= flw;
x = p;
}
return flw;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
string s;
cin >> s;
int n = s.size();
for (int i = 1; i <= n; ++i) {
mask[i] = 63;
chc[s[i - 1] - 'a']++;
}
int m;
cin >> m;
for (int i = 1; i <= m; ++i) {
int idx;
cin >> idx;
mask[idx] = 0;
string t;
cin >> t;
for (auto& c : t) {
mask[idx] |= (1 << (c - 'a'));
}
}
for (int i = 1; i <= n; ++i) {
maskc[mask[i]]++;
}
for (int i = 0; i < 6; ++i) {
add_edge(ss, 64 + i, chc[i]);
}
for (int i = 0; i < (1 << 6); ++i) {
add_edge(i, tt, maskc[i]);
for (int r = 0; r < 6; ++r) {
if (i & (1 << r)) {
add_edge(r + 64, i, 1e5);
}
}
}
int extra = 0;
int flw = 0;
while (extra = augment(ss, tt)) {
flw += extra;
}
if (flw < n) {
cout << "Impossible\n";
return 0;
}
string ans;
for (int i = 1; i <= n; ++i) {
int q = mask[i];
for (int r = 0; r < 6; ++r) {
if (!(q & (1 << r))) {
continue;
}
int cur_flow = get_flow(r + 64, q);
if (cur_flow) {
add_flow_cap(ss, r + 64, -1, -1);
add_flow_cap(r + 64, q, -1, 0);
add_flow_cap(q, tt, -1, -1);
ans.push_back(r + 'a');
break;
}
int y = -1;
for (int i = 0; i < adj[r + 64].size(); ++i) {
if (adj[r + 64][i].f > 0) {
y = i;
break;
}
}
if (y == -1) {
continue;
}
int z = -1;
for (int i = 0; i < adj[q].size(); ++i) {
if (adj[q][i].f < 0) {
z = i;
break;
}
}
if (z == -1) {
continue;
}
add_flow_cap(r + 64, adj[r + 64][y].v, -1, 0);
add_flow_cap(adj[q][z].v, q, -1, 0);
add_flow_cap(ss, r + 64, -1, -1);
add_flow_cap(q, tt, -1, -1);
add_flow_cap(ss, adj[q][z].v, -1, 0);
add_flow_cap(adj[r + 64][y].v, tt, -1, 0);
if (augment(ss, tt)) {
ans.push_back(r + 'a');
break;
}
add_flow_cap(r + 64, adj[r + 64][y].v, 1, 0);
add_flow_cap(adj[q][z].v, q, 1, 0);
add_flow_cap(ss, r + 64, 1, 1);
add_flow_cap(q, tt, 1, 1);
add_flow_cap(ss, adj[q][z].v, 1, 0);
add_flow_cap(adj[r + 64][y].v, tt, 1, 0);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
namespace Dinic {
const int N = 100005;
const int E = 2 * N;
const int oo = 1e8;
vector<int> g[N];
int ne;
struct Edge {
int from, to;
int flow, cap;
} edge[E];
int lvl[N], vis[N], pass, source = N - 2, target = N - 1, px[N];
int q[N], qt;
int run(int s, int minE) {
if (s == target) return minE;
int ans = 0;
for (; px[s] < (int)g[s].size(); px[s]++) {
int e = g[s][px[s]];
auto &v = edge[e], &rev = edge[e ^ 1];
if (lvl[v.to] != lvl[s] + 1 || v.flow >= v.cap) continue;
int tmp = run(v.to, min(minE, v.cap - v.flow));
v.flow += tmp, rev.flow -= tmp;
ans += tmp, minE -= tmp;
if (minE == 0) break;
}
return ans;
}
int bfs() {
qt = 0;
q[qt++] = source;
lvl[source] = 1;
vis[source] = ++pass;
for (int i = 0; i < qt; i++) {
int u = q[i];
px[u] = 0;
for (int e : g[u]) {
auto v = edge[e];
if (v.flow >= v.cap || vis[v.to] == pass) continue;
vis[v.to] = pass;
lvl[v.to] = lvl[u] + 1;
q[qt++] = v.to;
}
}
return vis[target] == pass;
}
int flow() {
int ans = 0, tmp;
while (bfs()) {
while ((tmp = run(source, oo)) > 0) ans += tmp;
}
return ans;
}
void addEdge(int u, int v, int c) {
edge[ne] = {u, v, 0, c};
g[u].push_back(ne++);
edge[ne] = {v, u, 0, 0};
g[v].push_back(ne++);
}
void reset_flow() {
for (int i = 0; i < ne; i++) edge[i].flow = 0;
}
} // namespace Dinic
const int N = 100005;
char s[N];
int m, cnt_letter[300], cnt_bit[1 << 7];
int get_bit() {
int ans = 0;
for (int i = 0; s[i]; i++) ans |= 1 << int(s[i] - 'a');
return ans;
}
int bit[N];
int main() {
int n;
scanf("%s", s);
n = (int)strlen(s);
for (int i = 0; s[i]; i++) cnt_letter[int(s[i] - 'a')]++;
scanf("%d", &m);
for (int i = 0; i < n; i++) bit[i] = (1 << 6) - 1;
for (int i = 0; i < m; i++) {
int pos;
scanf("%d %s", &pos, s);
pos--;
bit[pos] = get_bit();
}
for (int i = 0; i < n; i++) cnt_bit[bit[i]]++;
for (int i = 0; i < 6; i++) {
Dinic::addEdge(Dinic::source, i, cnt_letter[i]);
}
for (int i = 0; i < 64; i++) {
Dinic::addEdge(10 + i, Dinic::target, cnt_bit[i]);
}
for (int i = 0; i < 6; i++)
if (cnt_letter[i])
for (int j = 0; j < 64; j++)
if (cnt_bit[j])
if ((1 << i) & j) Dinic::addEdge(i, 10 + j, Dinic::oo);
if (Dinic::flow() < n) return printf("Impossible\n"), 0;
for (int i = 0; i < n; i++) {
Dinic::edge[2 * (6 + bit[i])].cap--;
for (int j = 0; j < 6; j++)
if (bit[i] & (1 << j)) {
if (!Dinic::edge[2 * j].cap) continue;
Dinic::edge[2 * j].cap--;
Dinic::reset_flow();
if (Dinic::flow() == n - i - 1) {
s[i] = char(j + 'a');
break;
}
Dinic::edge[2 * j].cap++;
}
}
s[n] = 0;
printf("%s\n", s);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MX = 1 << 6;
const int ITS_SO_FUCKING_BIG = 1e9;
int cnt[MX], maskat[MAX], f[6], n, req;
char s[MAX];
template <int SZ>
struct Dinic {
int N = 0, s, t;
struct Edge {
int to, rev;
int flow, cap;
int id;
};
vector<Edge> adj[SZ];
void addEdge(int u, int v, int cap, int id = 0) {
N = max(N, max(u, v) + 1);
Edge a = {v, (int)adj[v].size(), 0, cap, id},
b = {u, (int)adj[u].size(), 0, 0, 0};
adj[u].push_back(a), adj[v].push_back(b);
}
int lvl[SZ];
typename vector<Edge>::iterator cur[SZ];
bool bfs() {
for (int i = 0; i < SZ; i++) lvl[i] = -1, cur[i] = adj[i].begin();
queue<int> q({s});
lvl[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto &e : adj[u])
if (lvl[e.to] == -1 && e.flow < e.cap) {
q.push(e.to);
lvl[e.to] = lvl[u] + 1;
}
}
return lvl[t] >= 0;
}
int dfs(int u, int flow) {
if (u == t || !flow) return flow;
for (; cur[u] != adj[u].end(); cur[u]++) {
Edge &e = *cur[u];
if (lvl[e.to] != lvl[u] + 1 || e.flow == e.cap) continue;
int df = dfs(e.to, min(flow, e.cap - e.flow));
if (df) {
e.flow += df;
adj[e.to][e.rev].flow -= df;
return df;
}
}
return 0;
}
int maxFlow(int _s, int _t) {
s = _s, t = _t;
int totFlow = 0;
while (bfs()) {
int df = dfs(s, numeric_limits<int>::max());
while (df) {
totFlow += df;
df = dfs(s, numeric_limits<int>::max());
}
}
return totFlow;
}
};
Dinic<71> Network;
bool do_the_good_stuff() {
for (int i = 0; i < 71; i++) Network.adj[i].clear();
int s = 0, t = 70;
for (int i = 1; i <= 6; i++) {
if (!f[i - 1]) continue;
Network.addEdge(s, i, f[i - 1]);
for (int mask = 1; mask < MX; mask++) {
if (!cnt[mask]) continue;
if ((mask >> (i - 1)) & 1)
Network.addEdge(i, mask + 6, ITS_SO_FUCKING_BIG);
}
}
for (int i = 7; i < t; i++)
if (cnt[i - 6]) Network.addEdge(i, t, cnt[i - 6]);
int flow = Network.maxFlow(s, t);
return flow == req;
}
int main() {
scanf("%s", s);
n = strlen(s);
req = n;
int m;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int pos;
char cur[6];
scanf("%d", &pos);
scanf("%s", cur);
int len = strlen(cur);
int mask = 0;
for (int j = 0; j < len; j++) mask |= 1 << (cur[j] - 'a');
maskat[pos] = mask;
cnt[mask]++;
}
if (m == 0) {
sort(s, s + n);
return printf("%s", s), 0;
}
for (int i = 1; i <= n; i++) {
if (!maskat[i]) {
maskat[i] = MX - 1;
cnt[maskat[i]]++;
}
f[s[i - 1] - 'a']++;
}
if (!do_the_good_stuff()) return cout << "Impossible", 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 6; j++) {
if (((maskat[i] >> j) & 1) && f[j]) {
if (!(maskat[i] >> (j + 1))) {
f[j]--;
cnt[maskat[i]]--;
req--;
printf("%c", char('a' + j));
break;
}
f[j]--;
cnt[maskat[i]]--;
req--;
if (do_the_good_stuff()) {
printf("%c", char('a' + j));
break;
}
f[j]++;
cnt[maskat[i]]++;
req++;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = (int)1e9 + 7;
const int N = (int)1e5 + 7;
char s[N], t[N][30], ans[N];
int can[N], need[N], msk[N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
scanf("%s", s);
int len = strlen(s);
for (int i = (0); i < (len); ++i) {
int c = s[i] - 'a';
for (int j = (1 << c); j < (1 << 6); j = (j + 1) | (1 << c)) {
can[j]++;
}
}
int m;
scanf("%d", &m);
for (int i = (0); i < (len); ++i) {
msk[i] = (1 << 6) - 1;
for (int j = (0); j < (6); ++j) t[i][j] = 'a' + j;
t[i][6] = 0;
}
for (int i = (0); i < (m); ++i) {
int k;
scanf("%d", &k);
k--;
scanf("%s", t[k]);
msk[k] = 0;
sort(t[k], t[k] + strlen(t[k]));
for (int j = 0; t[k][j]; j++) {
msk[k] |= 1 << (t[k][j] - 'a');
}
}
for (int i = (0); i < (len); ++i) {
for (int j = msk[i]; j < (1 << 6); j = (j + 1) | msk[i]) {
need[j]++;
}
}
for (int i = (0); i < (1 << 6); ++i) {
if (need[i] > can[i]) return 0 * printf("Impossible\n");
}
for (int i = (0); i < (len); ++i) {
for (int j = msk[i]; j < (1 << 6); j = (j + 1) | msk[i]) need[j]--;
for (int k = 0; t[i][k]; k++) {
int c = t[i][k] - 'a';
for (int j = (1 << c); j < (1 << 6); j = (j + 1) | (1 << c)) can[j]--;
bool f = 1;
for (int j = (1 << c); j < (1 << 6); j = (j + 1) | (1 << c))
f &= can[j] >= need[j];
if (!f)
for (int j = (1 << c); j < (1 << 6); j = (j + 1) | (1 << c)) can[j]++;
else {
ans[i] = c + 'a';
break;
}
}
}
printf("%s\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int maxm = 6;
int n, m;
char s[maxn], ans[maxn];
int cnt[maxm], val[1 << maxm], msk[maxn];
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; i++) cnt[s[i] - 'a']++;
for (int mask = 0; mask < (1 << maxm); mask++)
for (int i = 0; i < maxm; i++)
if ((mask >> i) & 1) val[mask] += cnt[i];
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int pos;
scanf("%d%s", &pos, s);
pos--;
int k = strlen(s);
for (int j = 0; j < k; j++) msk[pos] |= (1 << (s[j] - 'a'));
}
for (int i = 0; i < n; i++) {
if (!msk[i]) msk[i] = (1 << maxm) - 1;
for (int mask = 0; mask < (1 << maxm); mask++)
if ((mask & msk[i]) > 0) val[mask]--;
}
for (int i = 0; i < n; i++) {
bool f = false;
for (int j = 0; j < maxm; j++)
if (cnt[j] && ((msk[i] >> j) & 1)) {
bool ok = true;
for (int mask = 0; mask < (1 << maxm); mask++) {
if ((mask >> j) & 1) val[mask]--;
if ((mask & msk[i]) > 0) val[mask]++;
if (val[mask] > 0) ok = false;
}
if (ok) {
f = true;
cnt[j]--;
ans[i] = char(j + 'a');
break;
}
for (int mask = 0; mask < (1 << maxm); mask++) {
if ((mask >> j) & 1) val[mask]++;
if ((mask & msk[i]) > 0) val[mask]--;
}
}
if (!f) {
puts("Impossible");
return 0;
}
}
printf("%s\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const int MAXM = 10005;
const int MAXN = 100005;
const int MAXS = (1 << 6) - 1;
struct Edge {
int to, next, c;
} e[MAXM];
int head[200], cur[200], edge_num[200][200], te = 1;
inline void addE(int u, int v, int c) {
e[++te] = (Edge){v, head[u], c}, head[u] = te, edge_num[u][v] = te;
e[++te] = (Edge){u, head[v], 0}, head[v] = te, edge_num[v][u] = te;
}
char s[MAXN], str[10];
int sta[MAXN], sum[200];
int n, N, p, S, T;
queue<int> q;
int dis[MAXN];
bool bfs() {
memset(dis, -1, sizeof(dis));
q.push(S);
dis[S] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int j = head[u]; j != 0; j = e[j].next) {
int v = e[j].to;
if (dis[v] == -1 && e[j].c > 0) {
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
return dis[T] != -1;
}
int dfs(int u, int F) {
if (u == T) return F;
int ret = 0;
for (int &j = cur[u]; j != 0; j = e[j].next) {
int v = e[j].to;
if (dis[v] == dis[u] + 1) {
int flow = dfs(v, min(F, e[j].c));
e[j].c -= flow;
e[j ^ 1].c += flow;
ret += flow;
F -= flow;
if (F == 0) return ret;
}
}
return ret;
}
int Dinic() {
int ret = 0;
while (bfs()) {
for (int i = S; i <= T; i++) cur[i] = head[i];
ret += dfs(S, INF);
}
return ret;
}
int ti = 0;
int used[200];
int dfs2(int u, int F) {
if (u == T) return F;
if (used[u] == ti) return 0;
used[u] = ti;
for (int j = head[u]; j != 0; j = e[j].next) {
int v = e[j].to;
if (e[j].c > 0) {
int flow = dfs2(v, min(F, e[j].c));
if (flow) {
e[j].c -= flow;
e[j ^ 1].c += flow;
return flow;
}
}
}
return 0;
}
bool check(int c, int state) {
if ((state & (1 << (c - 1))) == 0) return 0;
int cv = c, sv = state + 6;
int e1 = edge_num[S][cv];
int e2 = edge_num[sv][T];
if (e[e1 ^ 1].c == 0 || e[e2 ^ 1].c == 0) return 0;
vector<int> aff;
aff.push_back(e1);
e[e1].c++, e[e1 ^ 1].c--;
for (int j = head[cv]; j != 0; j = e[j].next) {
if ((j & 1) == 0 && e[j ^ 1].c > 0) {
aff.push_back(j);
e[j].c++;
e[j ^ 1].c--;
int v = e[j].to;
for (int k = head[v]; k != 0; k = e[k].next) {
if ((k & 1) == 0) {
aff.push_back(k);
e[k].c++;
e[k ^ 1].c--;
break;
}
}
break;
}
}
if (e[e2].c > 0) {
e[e1].c--;
e[e2].c--;
return 1;
}
aff.push_back(e2);
e[e2].c++;
e[e2 ^ 1].c--;
for (int j = head[sv]; j != 0; j = e[j].next) {
if ((j & 1) == 1 && e[j].c > 0) {
aff.push_back(j ^ 1);
e[j ^ 1].c++;
e[j].c--;
int v = e[j].to;
for (int k = head[v]; k != 0; k = e[k].next) {
if ((k & 1) == 1) {
aff.push_back(k ^ 1);
e[k ^ 1].c++;
e[k].c--;
break;
}
}
break;
}
}
e[e1].c--;
e[e2].c--;
ti++;
int ret = dfs2(S, 1);
if (ret == 1)
return 1;
else {
e[e1].c++;
e[e2].c++;
for (vector<int>::iterator it = aff.begin(); it != aff.end(); it++) {
int j = *it;
e[j].c--;
e[j ^ 1].c++;
}
return 0;
}
}
int main() {
ios::sync_with_stdio(false);
scanf("%s", s);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &p);
scanf("%s", str);
int now = 0, sz = strlen(str);
for (int i = 0; i < sz; i++) now |= (1 << (str[i] - 'a'));
sta[p] = now;
}
N = strlen(s);
for (int i = 1; i <= N; i++)
if (sta[i] == 0) sta[i] = MAXS;
for (int i = 0; i < N; i++) sum[s[i] - 'a' + 1]++;
for (int i = 1; i <= N; i++) sum[6 + sta[i]]++;
S = 0, T = 6 + (1 << 6);
for (int i = 1; i <= 6; ++i) addE(S, i, sum[i]);
for (int i = 1; i <= 6; i++)
for (int j = 1; j <= MAXS; j++)
if (j & (1 << (i - 1))) addE(i, 6 + j, INF);
for (int j = 1; j <= MAXS; j++) addE(j + 6, T, sum[j + 6]);
int w = Dinic();
if (w != N)
cout << "Impossible" << endl;
else {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= 6; j++) {
if (check(j, sta[i])) {
putchar((char)('a' + j - 1));
break;
}
}
}
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);
template <class T, class S>
inline void add(T& a, S b) {
a += b;
if (a >= mod) a -= mod;
}
template <class T, class S>
inline void sub(T& a, S b) {
a -= b;
if (a < 0) a += mod;
}
template <class T, class S>
inline bool chkmax(T& a, S b) {
return a < b ? a = b, true : false;
}
template <class T, class S>
inline bool chkmin(T& a, S b) {
return a > b ? a = b, true : false;
}
int n, m, mask[N], c[N], cnt[N], sos[N];
char s[N], t[N], ans[N];
bool check() {
int sum = 0, ret = inf;
for (int i = 0; i < 6; i++) sum += c[i];
for (int i = 0; i < 64; i++) sos[i] = cnt[i];
for (int i = 0; i < 6; i++)
for (int j = 0; j < 64; j++)
if (j >> i & 1) sos[j] += sos[j ^ (1 << i)];
for (int s1 = 0; s1 < 64; s1++) {
int tmp = 0;
for (int i = 0; i < 6; i++)
if (s1 >> i & 1) tmp += c[i];
tmp += sum - sos[s1];
chkmin(ret, tmp);
}
return sum == ret;
}
inline int getId(char c) { return c - 'a'; }
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; i++) c[getId(s[i])]++;
scanf("%d", &m);
while (m--) {
int p;
scanf("%d%s", &p, t + 1);
for (int i = 1; t[i]; i++) mask[p] |= 1 << getId(t[i]);
}
for (int i = 1; i <= n; i++) {
if (!mask[i]) mask[i] = 63;
cnt[mask[i]]++;
}
for (int i = 1; i <= n; i++) {
bool flag = false;
for (int j = 0; j < 6; j++) {
if (c[j] && mask[i] >> j & 1) {
c[j]--;
cnt[mask[i]]--;
flag = check();
if (flag) {
ans[i] = 'a' + j;
break;
}
c[j]++;
cnt[mask[i]]++;
}
}
if (!flag) return puts("Impossible"), 0;
}
ans[n + 1] = '\0';
puts(ans + 1);
return 0;
}
|
#include <bits/stdc++.h>
const int N = 100005, ST = 63;
int n, m, bit[N], ans[N], cnt[7], num[ST + 2][N];
char s[N], ssr[9];
inline int Check(int x) {
for (int st = 0; st <= ST; ++st) {
int cnum = 0;
for (int i = 0; i < 6; ++i) {
if ((st >> i) & 1) cnum += cnt[i];
}
if (num[st][n] - num[st][x] < cnum) return 0;
}
return 1;
}
int main() {
scanf("%s%d", s + 1, &m);
n = strlen(s + 1);
for (int i = 1; i <= n; ++i) {
bit[i] = ST;
ans[i] = -1;
++cnt[s[i] - 'a'];
}
for (int i = 1, x; i <= m; ++i) {
scanf("%d%s", &x, ssr);
int le = strlen(ssr), st = 0;
for (int j = 0; j < le; ++j) {
st |= 1 << (ssr[j] - 'a');
}
bit[x] &= st;
}
for (int st = 0; st <= ST; ++st) {
for (int i = 1; i <= n; ++i) {
num[st][i] = num[st][i - 1] + (bool)(bit[i] & st);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 6; ++j) {
--cnt[j];
if (((bit[i] >> j) & 1) && Check(i)) {
ans[i] = j;
break;
}
++cnt[j];
}
if (ans[i] == -1) {
puts("Impossible");
return 0;
}
}
for (int i = 1; i <= n; ++i) {
putchar(ans[i] + 'a');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char s[110000], ans[110000];
int rs[70], G[110000][64], E[110000];
inline int read() {
int n = 0;
char a;
bool z = false;
while (a = getchar()) {
if (a > '9' || a < '0')
if (z)
break;
else
continue;
if (!z) z = true;
n = (n << 1) + (n << 3) + (a ^ 48);
}
return n;
}
int main() {
scanf("%s", s + 1);
int m = strlen(s + 1), n = read(), len, x;
for (int i = 1; i <= m; ++i) ++rs[1 << s[i] - 'a'];
for (int S = 0; S < 64; ++S)
if (S & S - 1) rs[S] = rs[S & S - 1] + rs[S & -S];
for (int i = 1; i <= n; ++i) {
x = read();
scanf("%s", s + 1);
len = strlen(s + 1);
for (int j = 1; j <= len; ++j) E[x] |= 1 << s[j] - 'a';
}
for (int i = 1; i <= m; ++i)
if (!E[i]) E[i] = 63;
for (int i = m; i; --i)
for (int S = 0; S < 64; ++S) G[i][S] = G[i + 1][S] + (S & E[i] ? 1 : 0);
for (int i = 1; i <= m; ++i) {
bool zz = false;
for (int j = 1; j <= 6; ++j) {
if (!rs[1 << j - 1] || !(E[i] & 1 << j - 1)) continue;
bool Z = true;
for (int S = 0; S < 64; ++S)
if (G[i + 1][S] < rs[S] - (S & 1 << j - 1 ? 1 : 0)) {
Z = false;
break;
}
if (Z) {
ans[i] = j + 'a' - 1;
zz = true;
for (int S = 0; S < 64; ++S) rs[S] -= S & 1 << j - 1 ? 1 : 0;
break;
}
}
if (!zz) return !printf("Impossible");
}
printf("%s", ans + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, sm[N], f[N][70], ok[N];
char s[N], c[10], flg, ans[N];
int main() {
scanf("%s%d", s + 1, &m);
n = strlen(s + 1);
for (int i = 1; i <= n; i++)
for (int j = 0; j < (1 << 6); j++)
if (j & (1 << (s[i] - 'a'))) sm[j]++;
for (int i = 1; i <= n; i++) ok[i] = (1 << 6) - 1;
for (int i = 1, x; i <= m; i++) {
scanf("%d%s", &x, c + 1);
ok[x] = 0;
for (int j = 1; j <= strlen(c + 1); j++) ok[x] += (1 << (c[j] - 'a'));
}
for (int i = n; i >= 1; i--)
for (int j = 0; j < (1 << 6); j++)
f[i][j] = f[i + 1][j] + (((j & ok[i]) == ok[i]) ? 1 : 0);
for (int i = 1; i <= n; i++) {
bool fl = 0;
for (int j = 0; j < 6 && !fl; j++)
if (sm[1 << j] && (ok[i] & (1 << j))) {
flg = 1;
for (int k = 0; k < (1 << 6) && flg; k++)
if (f[i + 1][k] > sm[k] - ((k >> j) & 1)) flg = 0;
if (flg) {
fl = 1;
ans[i] = 'a' + j;
for (int k = 0; k < (1 << 6); k++)
if (k & (1 << j)) sm[k]--;
}
}
if (!flg) {
puts("Impossible");
return 0;
}
}
for (int i = 1; i <= n; i++) printf("%c", ans[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 999999999999999999;
const double PI = acos(-1.0);
void stop() { exit(0); }
int main() {
double a, b, c, x1, y1, x2, y2;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
double var1 = abs(x1 - x2) + abs(y1 - y2);
double var2 = 0, var3 = 0, var4 = 0, var5 = 0;
double y11;
if (-x1 * a - c == 0 && b == 0) {
y11 = 0;
} else if (b == 0) {
y11 = INF;
} else {
y11 = (-x1 * a - c) / b;
}
double x11;
if (-y1 * b - c == 0 && a == 0) {
x11 = 0;
} else if (a == 0) {
x11 = INF;
} else {
x11 = (-y1 * b - c) / a;
}
double y22;
if (-x2 * a - c == 0 && b == 0) {
y22 = 0;
} else if (b == 0) {
y22 = INF;
} else {
y22 = (-x2 * a - c) / b;
}
double x22;
if (-y2 * b - c == 0 && a == 0) {
x22 = 0;
} else if (a == 0) {
x22 = INF;
} else {
x22 = (-y2 * b - c) / a;
}
var2 += abs(x1 - x11);
var2 += abs(x2 - x22);
var2 += sqrt((y1 - y2) * (y1 - y2) + (x11 - x22) * (x11 - x22));
var3 += abs(x1 - x11);
var3 += abs(y2 - y22);
var3 += sqrt((y1 - y22) * (y1 - y22) + (x11 - x2) * (x11 - x2));
var4 += abs(y1 - y11);
var4 += abs(x2 - x22);
var4 += sqrt((y11 - y2) * (y11 - y2) + (x1 - x22) * (x1 - x22));
var5 += abs(y1 - y11);
var5 += abs(y2 - y22);
var5 += sqrt((y11 - y22) * (y11 - y22) + (x1 - x2) * (x1 - x2));
double res = var1;
res = min(res, var2);
res = min(res, var3);
res = min(res, var4);
res = min(res, var5);
printf("%.8lf", res);
stop();
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
double a, b, c, ans;
double x[N], y[N];
double GetDis(int i, int j) { return fabs(x[i] - x[j]) + fabs(y[i] - y[j]); }
double GG(int i, int j) {
return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
}
int main() {
scanf("%lf%lf%lf", &a, &b, &c);
scanf("%lf%lf%lf%lf", &x[1], &y[1], &x[2], &y[2]);
ans = GetDis(1, 2);
x[3] = x[1];
y[3] = -(a * x[1] + c) / b;
x[4] = -(b * y[1] + c) / a;
y[4] = y[1];
x[5] = x[2];
y[5] = -(a * x[2] + c) / b;
x[6] = -(b * y[2] + c) / a;
y[6] = y[2];
ans = min(ans, GetDis(1, 3) + GG(3, 5) + GetDis(2, 5));
ans = min(ans, GetDis(1, 3) + GG(3, 6) + GetDis(2, 6));
ans = min(ans, GetDis(1, 4) + GG(4, 5) + GetDis(2, 5));
ans = min(ans, GetDis(1, 4) + GG(4, 6) + GetDis(2, 6));
printf("%.12lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct {
double x, y;
} Point;
long long a, b, c;
double distance(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
Point A, B, Apx, Apy, Bpx, Bpy;
int main() {
cin >> a >> b >> c;
cin >> A.x >> A.y >> B.x >> B.y;
double manhattan_distance = (double)(abs(A.x - B.x) + abs(A.y - B.y));
Apx.x = A.x;
Apx.y = -(a * A.x + c) / b;
Apy.y = A.y;
Apy.x = -(b * A.y + c) / a;
Bpx.x = B.x;
Bpx.y = -(a * B.x + c) / b;
Bpy.y = B.y;
Bpy.x = -(b * B.y + c) / a;
double avedistance_1 =
distance(A, Apx) + distance(Apx, Bpx) + distance(Bpx, B);
double avedistance_2 =
distance(A, Apx) + distance(Apx, Bpy) + distance(Bpy, B);
double avedistance_3 =
distance(A, Apy) + distance(Apy, Bpx) + distance(Bpx, B);
double avedistance_4 =
distance(A, Apy) + distance(Apy, Bpy) + distance(Bpy, B);
double result = min(manhattan_distance, avedistance_1);
result = min(result, avedistance_2);
result = min(result, avedistance_3);
result = min(result, avedistance_4);
cout.precision(10);
cout << result;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline double D(const double &x, const double &y) {
return sqrt(x * x + y * y);
}
int main() {
double _x1, _y1, _x2, _y2;
long long A, B, C, x1, y1, x2, y2;
int i, n;
bool flag;
scanf("%lld%lld%lld", &A, &B, &C);
if (A < 0) {
A = -A;
B = -B;
C = -C;
}
if (B < 0) {
B = -B;
C = -C;
flag = true;
} else
flag = false;
scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
if (flag) x1 = -x1, x2 = -x2;
if (x1 > x2) swap(x1, x2), swap(y1, y2);
if ((x1 == x2) || (y1 <= y2))
printf("%lld.000\n", x2 - x1 + ((long long)abs(y2 - y1)));
else {
_x1 = -((double)C + B * y1) / A;
_x2 = -((double)C + B * y2) / A;
_y1 = -((double)C + A * x1) / B;
_y2 = -((double)C + A * x2) / B;
if ((_x1 >= x1) && (_x1 <= x2))
if ((_x2 >= x1) && (_x2 <= x2))
printf("%.10lf\n", _x1 - x1 + x2 - _x2 + D(_x2 - _x1, y1 - y2));
else
printf("%.10lf\n", _x1 - x1 + _y2 - y2 + D(x2 - _x1, y1 - _y2));
else if ((_y1 >= y2) && (_y1 <= y1))
if ((_y2 >= y2) && (_y2 <= y1))
printf("%.10lf\n", y1 - _y1 + _y2 - y2 + D(x2 - x1, _y1 - _y2));
else
printf("%.10lf\n", y1 - _y1 + x2 - _x2 + D(_x2 - x1, _y1 - y2));
else
printf("%lld.000000\n", x2 - x1 + y1 - y2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long a, b, c;
double z, p[2][2], prj[2][2][2];
double dist(double x1, double y1, double x2, double y2) {
double dx = x1 - x2, dy = y1 - y2;
return sqrt(dx * dx + dy * dy);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(15);
cin >> a >> b >> c;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) cin >> p[i][j];
z = abs(p[0][0] - p[1][0]) + abs(p[0][1] - p[1][1]);
if (a && b) {
double x1, y1;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) {
if (j) {
y1 = p[i][j];
x1 = -(c + b * y1) / (1.0 * a);
} else {
x1 = p[i][j];
y1 = -(c + a * x1) / (1.0 * b);
}
prj[i][j][0] = x1;
prj[i][j][1] = y1;
}
double d, md1, md2;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) {
md1 = abs(p[0][0] - prj[0][i][0]) + abs(p[0][1] - prj[0][i][1]);
md2 = abs(p[1][0] - prj[1][j][0]) + abs(p[1][1] - prj[1][j][1]);
d = dist(prj[0][i][0], prj[0][i][1], prj[1][j][0], prj[1][j][1]);
z = min(z, md1 + d + md2);
}
}
cout << z << "\n";
return 0;
}
|
#include <bits/stdc++.h>
struct Point {
double x, y;
};
double distance(Point, Point);
int main() {
double a, b, c;
double min_dist;
scanf("%lf %lf %lf", &a, &b, &c);
Point p1, p2, temp1, temp2;
scanf("%lf %lf %lf %lf", &p1.x, &p1.y, &p2.x, &p2.y);
min_dist = fabs(p2.x - p1.x) + fabs(p2.y - p1.y);
if (p1.x == p2.x || p1.y == p2.y)
min_dist = distance(p1, p2);
else {
if (a != 0.0f && b != 0.0f) {
if ((-b * p1.y - c) / a >= ((p1.x < p2.x) ? p1.x : p2.x) &&
(-b * p1.y - c) / a <= ((p1.x > p2.x) ? p1.x : p2.x)) {
temp1 = {(-b * p1.y - c) / a, p1.y};
if ((-a * p2.x - c) / b >= ((p1.y < p2.y) ? p1.y : p2.y) &&
(-a * p2.x - c) / b <= ((p1.y > p2.y) ? p1.y : p2.y)) {
temp2 = {p2.x, (-a * p2.x - c) / b};
min_dist = distance(p1, temp1) + distance(temp1, temp2) +
distance(temp2, p2);
} else if ((-b * p2.y - c) / a >= ((p1.x < p2.x) ? p1.x : p2.x) &&
(-b * p2.y - c) / a <= ((p1.x > p2.x) ? p1.x : p2.x) &&
abs(p2.x - (-b * p2.y - c) / a) <
abs(p2.x - (-b * p1.y - c) / a)) {
temp2 = {(-b * p2.y - c) / a, p2.y};
min_dist = distance(p1, temp1) + distance(temp1, temp2) +
distance(temp2, p2);
}
} else if ((-a * p1.x - c) / b >= ((p1.y < p2.y) ? p1.y : p2.y) &&
(-a * p1.x - c) / b <= ((p1.y > p2.y) ? p1.y : p2.y)) {
temp1 = {p1.x, (-a * p1.x - c) / b};
if ((-b * p2.y - c) / a >= ((p1.x < p2.x) ? p1.x : p2.x) &&
(-b * p2.y - c) / a <= ((p1.x > p2.x) ? p1.x : p2.x)) {
temp2 = {(-b * p2.y - c) / a, p2.y};
min_dist = distance(p1, temp1) + distance(temp1, temp2) +
distance(temp2, p2);
} else if ((-a * p2.x - c) / b >= ((p1.y < p2.y) ? p1.y : p2.y) &&
(-a * p2.x - c) / b <= ((p1.y > p2.y) ? p1.y : p2.y) &&
abs(p2.y - (-a * p2.x - c) / b) <
abs(p2.y - (-a * p1.x - c) / b)) {
temp2 = {p2.x, (-a * p2.x - c) / b};
min_dist = distance(p1, temp1) + distance(temp1, temp2) +
distance(temp2, p2);
}
}
}
}
printf("%f\n", min_dist);
return 0;
}
double distance(Point a, Point b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
const long double eps = 0.0000000001;
const long long INF = 0x3fffffffffffffff;
long double a, b, c, sx, sy, ex, ey;
long double ans;
struct point {
long double x, y;
} s, e;
long double sqr(long double x) { return x * x; }
long double dist(point A, point B) {
return sqrt(sqr(A.x - B.x) + sqr(A.y - B.y));
}
point Y(long double X) { return {X, -(c + a * X) / b}; }
point X(long double Y) { return {-(c + b * Y) / a, Y}; }
signed main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(100);
cin >> a >> b >> c;
cin >> sx >> sy >> ex >> ey;
if (a == 0 || b == 0) {
cout << llabs(sx - ex) + llabs(sy - ey) << endl;
return 0;
}
ans = llabs(sx - ex) + llabs(sy - ey);
s = {sx, sy};
e = {ex, ey};
ans = min(ans, dist(s, Y(sx)) + dist(Y(sx), Y(ex)) + dist(Y(ex), e));
ans = min(ans, dist(s, Y(sx)) + dist(Y(sx), X(ey)) + dist(X(ey), e));
ans = min(ans, dist(s, X(sy)) + dist(X(sy), Y(ex)) + dist(Y(ex), e));
ans = min(ans, dist(s, X(sy)) + dist(X(sy), X(ey)) + dist(X(ey), e));
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
long double x1, y1, x2, y2, s, x3, y3, x4, y4, x5, y5, x6, y6;
cin >> a >> b >> c;
cin >> x1 >> y1 >> x2 >> y2;
x3 = x1;
y3 = (-a * x3 - c) / b;
y4 = y1;
x4 = (-b * y4 - c) / a;
x5 = x2;
y5 = (-a * x5 - c) / b;
y6 = y2;
x6 = (-b * y6 - c) / a;
s = min(min(min((abs(x1 - x2) + abs(y1 - y2)),
(abs(y1 - y3) + abs(y2 - y5) +
sqrt((y3 - y5) * (y3 - y5) + (x3 - x5) * (x3 - x5)))),
min((abs(y1 - y3) + abs(x2 - x6) +
sqrt((y3 - y6) * (y3 - y6) + (x3 - x6) * (x3 - x6))),
(abs(x1 - x4) + abs(y2 - y5) +
sqrt((y4 - y5) * (y4 - y5) + (x4 - x5) * (x4 - x5))))),
(abs(x1 - x4) + abs(x2 - x6) +
sqrt((y4 - y6) * (y4 - y6) + (x4 - x6) * (x4 - x6))));
cout << setprecision(19) << s;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main() {
ios_base::sync_with_stdio(0);
cout.precision(20);
cout << fixed;
cin.tie(0);
cout.tie(0);
ld a, b, c;
cin >> a >> b >> c;
ld x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ld l1 = abs(x2 - x1) + abs(y2 - y1);
ld y_forx1 = (-a * x1 - c) / b;
ld y_forx2 = (-a * x2 - c) / b;
ld x_fory1 = (-b * y1 - c) / a;
ld x_fory2 = (-b * y2 - c) / a;
ld l2 =
abs(y1 - y_forx1) + hypot(x1 - x2, y_forx1 - y_forx2) + abs(y2 - y_forx2);
ld l3 =
abs(y1 - y_forx1) + hypot(x1 - x_fory2, y_forx1 - y2) + abs(x2 - x_fory2);
ld l4 =
abs(x1 - x_fory1) + hypot(x_fory1 - x_fory2, y1 - y2) + abs(x2 - x_fory2);
ld l5 =
abs(x1 - x_fory1) + hypot(x_fory1 - x2, y1 - y_forx2) + abs(y2 - y_forx2);
cout << min(l1, min(l2, min(l3, min(l4, l5))));
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
double eps = 0.0000001;
struct point {
double x, y;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long a, b, c;
cin >> a >> b >> c;
cout << fixed << setprecision(8);
point q, p;
cin >> q.x >> q.y >> p.x >> p.y;
double ans = fabs(q.x - p.x) + fabs(q.y - p.y);
double res;
if (a == 0 || b == 0) {
cout << ans;
return 0;
}
vector<point> w(4);
w[0].x = q.x;
w[0].y = -1. * (a * q.x + c) / b;
w[1].y = q.y;
w[1].x = -1. * (b * q.y + c) / a;
w[2].x = p.x;
w[2].y = -1. * (a * p.x + c) / b;
w[3].y = p.y;
w[3].x = -1. * (b * p.y + c) / a;
for (int i = 0; i < 2; i++) {
for (int j = 2; j < 4; j++) {
res = fabs(q.x - w[i].x) + fabs(q.y - w[i].y);
res += sqrt((w[i].x - w[j].x) * (w[i].x - w[j].x) +
(w[i].y - w[j].y) * (w[i].y - w[j].y));
res += fabs(p.x - w[j].x) + fabs(p.y - w[j].y);
ans = min(ans, res);
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ii = pair<int, int>;
using lu = unsigned long long;
using l = long long;
using vs = vector<string>;
using vii = vector<ii>;
using vl = vector<l>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using ll = pair<l, l>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using mll = unordered_map<l, l>;
using sl = unordered_set<l>;
const l INF = numeric_limits<l>::max();
const double EPS = 1e-10;
const double PI = 3.14159265358979323846;
const l e0 = 1, e3 = 1000, e5 = 100000, e6 = 10 * e5, e7 = 10 * e6,
e8 = 10 * e7, e9 = 10 * e8;
const char lf = '\n';
void solve(istream& in, ostream& out);
int main(int argc, char** argv) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
solve(cin, cout);
}
const l MOD = e9 + 7;
struct point {
double x, y;
point() {}
point(double _x, double _y) : x(_x), y(_y) {}
double operator*(const point& o) { return x * o.x + y * o.y; }
point operator*(const double m) { return point(x * m, y * m); }
double operator^(const point& o) { return x * o.y - y * o.x; }
point operator-(const point& o) { return point(x - o.x, y - o.y); }
point operator+(const point& o) { return point(x + o.x, y + o.y); }
double operator>>(const point& o) {
point d = (*this - o);
return d * d;
}
double distance(const point& o) { return sqrt(*this >> o); }
bool equal(double a, double b) const {
if (abs(a - b) < EPS) return true;
if (abs(a) < EPS || abs(b) < EPS) return false;
return (abs((a - b) / a) < EPS) && (abs((a - b) / b) < EPS);
}
bool operator<(const point& o) const {
if (o.x != x) return x < o.x;
if (not equal(o.x, x)) return x < o.x;
return y < o.y;
}
};
l segment_intersection(point a, point b, point c, point d, point& o1,
point& o2) {
point x = c - a, y = d - a, z = b - a;
double t = (x ^ z) + (z ^ y);
if (abs(t) < EPS) {
if (abs(x ^ y) < EPS) {
vector<point> v = {a, b, c, d};
sort(begin(v), end(v));
o1 = v[1];
o2 = v[2];
return 2;
} else {
return 0;
}
}
t = (x ^ y) / t;
if (t < 0 or t > 1) return 0;
o1 = o2 = a + z * t;
return 1;
}
ostream& operator<<(ostream& s, const point& p) {
s << "(" << p.x << ", " << p.y << ")";
return s;
}
using vp = vector<point>;
vp intersect(double a, double b, double c, point p) {
vp z;
if (b != 0) {
z.emplace_back(p.x, (-c - a * p.x) / b);
}
if (a != 0) {
z.emplace_back((-c - b * p.y) / a, p.y);
}
return z;
}
void solve(istream& in, ostream& out) {
double a, b, c;
in >> a >> b >> c;
point q, w;
in >> q.x >> q.y >> w.x >> w.y;
double z = abs(q.x - w.x) + abs(q.y - w.y);
auto iq = intersect(a, b, c, q), iw = intersect(a, b, c, w);
for (auto x : iq)
for (auto y : iw) {
z = min(z, q.distance(x) + x.distance(y) + y.distance(w));
}
out << setprecision(12) << z << lf;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Point {
double x = 0, y = 0;
Point(double a, double b) {
x = a;
y = b;
}
Point() {}
};
struct Line {
double a, b, c;
Line(double aa, double bb, double cc) {
a = aa;
b = bb;
c = cc;
}
Line() {}
double per(double x) { return (-c - a * x) / b; }
};
struct Vector {
double x = 0, y = 0;
Vector(Point p, Point q) {
x = q.x - p.x;
y = q.y - p.y;
}
double dist() { return sqrt(x * x + y * y); }
};
Vector operator+(Vector a, Vector b) {
return Vector(Point(0, 0), Point(a.x + b.x, a.y + b.y));
}
istream &operator>>(istream &in, Point &p) {
in >> p.x >> p.y;
return in;
}
istream &operator>>(istream &in, Vector &v) {
in >> v.x >> v.y;
return in;
}
istream &operator>>(istream &in, Line &l) {
in >> l.a >> l.b >> l.c;
return in;
}
ostream &operator<<(ostream &out, Point p) {
out << p.x << " " << p.y;
return out;
}
ostream &operator<<(ostream &out, Vector v) {
out << v.x << " " << v.y;
return out;
}
double dot_product(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross_product(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
Vector normalize(Vector v) {
double d = v.dist();
if (d == 0 || d == 1) {
return Vector(Point(0, 0), Point(v.x, v.y));
}
return Vector(Point(0, 0), Point(v.x / d, v.y / d));
}
Vector change_distance(Vector v, double new_d) {
double d = v.dist();
if (new_d == 0) {
return Vector(Point(0, 0), Point(0, 0));
}
return Vector(Point(0, 0), Point(v.x * new_d / d, v.y * new_d / d));
}
Vector turn_90(Vector v, double side) {
return Vector(Point(0, 0), Point(v.y * side, -v.x * side));
}
Point peresek(Line a, Line b) {
double x = (-a.c * b.b + b.c * a.b) / (a.a * b.b - a.b * b.a);
return Point(x, a.per(x));
}
double dist(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool paral(Line a, Line b) { return a.a * b.b == a.b * b.a; }
bool sliv(Line a, Line b) {
return a.a * b.c == a.c * b.a && a.b * b.c == a.c * b.b;
}
int main() {
Line kek;
cin >> kek;
Point a, b;
cin >> a >> b;
Line a1(0, 1, -a.y);
Line a2(-1, 0, a.x);
Line b1(0, 1, -b.y);
Line b2(-1, 0, b.x);
Point k1 = peresek(a1, b2);
Point ak1, ak2, bk1, bk2;
double ans = dist(a, k1) + dist(k1, b);
if (!paral(kek, a1)) {
Point f = peresek(kek, a1);
ak1.x = f.x;
ak1.y = f.y;
} else if (sliv(kek, a1)) {
ak1.x = a.x;
ak1.y = a.y;
} else {
ak1.x = 4 * (1e9);
ak1.y = 4 * (1e9);
}
if (!paral(kek, a2)) {
Point f = peresek(kek, a2);
ak2.x = f.x;
ak2.y = f.y;
} else if (sliv(kek, a2)) {
ak2.x = a.x;
ak2.y = a.y;
} else {
ak2.x = 4 * (1e9);
ak2.y = 4 * (1e9);
}
if (!paral(kek, b1)) {
Point f = peresek(kek, b1);
bk1.x = f.x;
bk1.y = f.y;
} else if (sliv(kek, b1)) {
bk1.x = b.x;
bk1.y = b.y;
} else {
bk1.x = 4 * (1e9);
bk1.y = 4 * (1e9);
}
if (!paral(kek, b2)) {
Point f = peresek(kek, b2);
bk2.x = f.x;
bk2.y = f.y;
} else if (sliv(kek, b2)) {
bk2.x = b.x;
bk2.y = b.y;
} else {
bk2.x = 4 * (1e9);
bk2.y = 4 * (1e9);
}
ans = min(ans, dist(a, ak1) + dist(ak1, bk1) + dist(bk1, b));
ans = min(ans, dist(a, ak1) + dist(ak1, bk2) + dist(bk2, b));
ans = min(ans, dist(a, ak2) + dist(ak2, bk1) + dist(bk1, b));
ans = min(ans, dist(a, ak2) + dist(ak2, bk2) + dist(bk2, b));
cout << fixed;
cout.precision(10);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double get_x(double yy, double a, double b, double c) {
return -(b * yy + c) / a;
}
double get_y(double xx, double a, double b, double c) {
return -(a * xx + c) / b;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
double a, b, c;
cin >> a >> b >> c;
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
complex<double> A = {x1, y1};
complex<double> B = {x2, y2};
double ans = abs(A.real() - B.real()) + abs(A.imag() - B.imag());
x1 = get_x(A.imag(), a, b, c);
x2 = get_x(B.imag(), a, b, c);
y1 = get_y(A.real(), a, b, c);
y2 = get_y(B.real(), a, b, c);
double curr =
abs(x1 - A.real()) +
abs((complex<double>){x1, A.imag()} - (complex<double>){x2, B.imag()}) +
abs(x2 - B.real());
ans = min(ans, curr);
curr =
abs(x1 - A.real()) +
abs((complex<double>){x1, A.imag()} - (complex<double>){B.real(), y2}) +
abs(y2 - B.imag());
ans = min(ans, curr);
curr =
abs(y1 - A.imag()) +
abs((complex<double>){A.real(), y1} - (complex<double>){x2, B.imag()}) +
abs(x2 - B.real());
ans = min(ans, curr);
curr =
abs(y1 - A.imag()) +
abs((complex<double>){A.real(), y1} - (complex<double>){B.real(), y2}) +
abs(y2 - B.imag());
ans = min(ans, curr);
cout.precision(10);
cout << fixed;
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void USACO(string s) {
ios_base::sync_with_stdio(0);
cin.tie(0);
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
double a, b, c, x[10], y[10], ans;
const long long MAXN = 100010;
void solve() {
cin >> a >> b >> c >> x[1] >> y[1] >> x[2] >> y[2];
ans = fabs(x[1] - x[2]) + fabs(y[1] - y[2]);
x[3] = x[1];
y[3] = (-a * x[1] - c) / b;
x[4] = (-b * y[1] - c) / a;
y[4] = y[1];
x[5] = x[2];
y[5] = (-a * x[2] - c) / b;
x[6] = (-b * y[2] - c) / a;
y[6] = y[2];
for (int i = 3; i <= 4; i++)
for (int j = 5; j <= 6; j++) {
ans = min(ans, fabs(x[1] - x[i]) + fabs(y[1] - y[i]) +
sqrt((x[i] - x[j]) * (x[i] - x[j]) +
(y[i] - y[j]) * (y[i] - y[j])) +
fabs(x[2] - x[j]) + fabs(y[2] - y[j]));
}
cout << setprecision(9) << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t N = 1e6 + 5;
long double dist(long double x1, long double y1, long double x2,
long double y2) {
long double dx = abs(x1 - x2);
long double dy = abs(y1 - y2);
return sqrtl(dx * dx + dy * dy);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64_t a, b, c;
cin >> a >> b >> c;
int64_t x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
pair<long double, long double> ap1;
pair<long double, long double> ap2;
pair<long double, long double> bp1;
pair<long double, long double> bp2;
if (a == 0 || b == 0) {
long double ans = (long double)(abs(x1 - x2) + abs(y1 - y2));
cout << setprecision(15) << ans << "\n";
return 0;
}
ap1 = {1.0 * (-c - b * y1) / a, y1};
ap2 = {x1, 1.0 * (-c - a * x1) / b};
bp1 = {1.0 * (-c - b * y2) / a, y2};
bp2 = {x2, 1.0 * (-c - a * x2) / b};
long double ans = dist(x1, y1, ap1.first, ap1.second) +
dist(ap1.first, ap1.second, bp1.first, bp1.second) +
dist(bp1.first, bp1.second, x2, y2);
ans = min(ans, dist(x1, y1, ap2.first, ap2.second) +
dist(ap2.first, ap2.second, bp1.first, bp1.second) +
dist(bp1.first, bp1.second, x2, y2));
ans = min(ans, dist(x1, y1, ap1.first, ap1.second) +
dist(ap1.first, ap1.second, bp2.first, bp2.second) +
dist(bp2.first, bp2.second, x2, y2));
ans = min(ans, dist(x1, y1, ap2.first, ap2.second) +
dist(ap2.first, ap2.second, bp2.first, bp2.second) +
dist(bp2.first, bp2.second, x2, y2));
ans = min(ans, (long double)(abs(x1 - x2) + abs(y1 - y2)));
cout << setprecision(15) << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double dist(double x1, double y1, double x2, double y2) {
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
double Mod(double a) { return a > 0 ? a : -a; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
double a, b, c, x1, y1, x2, y2, min_dist;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
min_dist = Mod(y2 - y1) + Mod(x2 - x1);
if ((x1 == x2) || (y1 == y2) || b == 0 || a == 0) {
printf("%.8f", min_dist);
} else {
double x1_int = (c + b * y1) / (-a);
double y1_int = (c + a * x1) / (-b);
double x2_int = (c + b * y2) / (-a);
double y2_int = (c + a * x2) / (-b);
min_dist = min(min_dist, Mod(x1_int - x1) + Mod(x2_int - x2) +
dist(x1_int, y1, x2_int, y2));
min_dist = min(min_dist, Mod(x1_int - x1) + Mod(y2_int - y2) +
dist(x1_int, y1, x2, y2_int));
min_dist = min(min_dist, Mod(y1_int - y1) + Mod(x2_int - x2) +
dist(x1, y1_int, x2_int, y2));
min_dist = min(min_dist, Mod(y1_int - y1) + Mod(y2_int - y2) +
dist(x1, y1_int, x2, y2_int));
printf("%.8f", min_dist);
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("fast-math")
using namespace std;
const long long MAXN = 1e5 + 7, MAX_LOG = log2(MAXN) + 1, mod = 1e9 + 1123,
inf = 1e18 + 7;
const int MEM_INF = (1 << 7) - 1;
const long double eps = 1e-9;
long double a, b, c, x1, y1228, x2, y2;
long double dist_first;
long double intersect_x(long double x, long double y1228, long double y2) {
if (!b) return -inf;
long double cur_y = (a * x + c) / (-b);
if (cur_y < y1228 - eps || cur_y - eps > y2) return -inf;
return cur_y;
}
long double intersect_y(long double x1, long double x2, long double y) {
if (!a) return -inf;
long double cur_x = (b * y + c) / (-a);
if (cur_x < x1 + eps || cur_x + eps > x2) return -inf;
return cur_x;
}
long double dist(pair<long double, long double> p1,
pair<long double, long double> p2) {
return fabs(p1.first - p2.first) + fabs(p1.second - p2.second);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> a >> b >> c >> x1 >> y1228 >> x2 >> y2;
dist_first = fabs(x1 - x2) + fabs(y1228 - y2);
int min_x = min(x1, x2), max_x = max(x1, x2);
int min_y = min(y1228, y2), max_y = max(y1228, y2);
vector<pair<long double, long double>> p;
if (intersect_x(x1, min_y, max_y) != -inf) {
p.push_back({x1, intersect_x(x1, min_y, max_y)});
}
if (intersect_x(x2, min_y, max_y) != -inf) {
p.push_back({x2, intersect_x(x2, min_y, max_y)});
}
if (intersect_y(min_x, max_x, y1228) != -inf) {
p.push_back({intersect_y(min_x, max_x, y1228), y1228});
}
if (intersect_y(min_x, max_x, y2) != -inf) {
p.push_back({intersect_y(min_x, max_x, y2), y2});
}
vector<pair<long double, long double>> ans;
sort(p.begin(), p.end());
for (int i = 0; i < p.size(); i++) {
ans.push_back(p[i]);
int j = i;
while (j < p.size() && dist(p[i], p[j]) < eps) j++;
if (i != j) i = j - 1;
}
if (ans.size() < 2) {
cout << fixed << setprecision(10) << dist_first << "\n";
return 0;
}
long double d =
sqrt((ans[0].first - ans[1].first) * (ans[0].first - ans[1].first) +
(ans[0].second - ans[1].second) * (ans[0].second - ans[1].second));
long double d1 = inf, d2 = inf;
d1 = min(dist({x1, y1228}, ans[0]), dist({x1, y1228}, ans[1]));
d2 = min(dist({x2, y2}, ans[0]), dist({x2, y2}, ans[1]));
cout << fixed << setprecision(10) << min(dist_first, d + d1 + d2);
}
|
#include <bits/stdc++.h>
using namespace std;
struct pt {
long double x, y;
};
pt a, b, au, ar, bu, br;
long double n, m, c, res = 1e18;
long double manhet(pt u, pt v) { return fabs(u.x - v.x) + fabs(u.y - v.y); }
long double euclid(pt u, pt v) {
return sqrt((u.x - v.x) * (u.x - v.x) + (u.y - v.y) * (u.y - v.y));
}
void solve() {
cin >> n >> m >> c;
cin >> a.x >> a.y >> b.x >> b.y;
res = fabs(a.x - b.x) + fabs(a.y - b.y);
if (fabs(m) > 0.4444 && fabs(n) > 0.4444) {
au = (pt){a.x, -(n * a.x + c) / m};
ar = (pt){-(m * a.y + c) / n, a.y};
bu = (pt){b.x, -(n * b.x + c) / m};
br = (pt){-(m * b.y + c) / n, b.y};
res = min(res, manhet(a, au) + euclid(au, bu) + manhet(bu, b));
res = min(res, manhet(a, au) + euclid(au, br) + manhet(br, b));
res = min(res, manhet(a, ar) + euclid(ar, bu) + manhet(bu, b));
res = min(res, manhet(a, ar) + euclid(ar, br) + manhet(br, b));
}
cout << setprecision(8) << fixed << res;
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
double Fx(double y) { return (-c - b * y) / a; }
double Fy(double x) { return (-c - a * x) / b; }
double Distance(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
cin >> a >> b >> c;
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
double ans = fabs(x1 - x2) + fabs(y1 - y2);
if (!a || !b) {
printf("%.8f", ans);
return 0;
}
double X1, Y1, X2, Y2;
X1 = Fx(y1);
X2 = Fx(y2);
ans = min(ans, fabs(x1 - X1) + Distance(X1, y1, X2, y2) + fabs(X2 - x2));
Y2 = Fy(x2);
ans = min(ans, fabs(x1 - X1) + Distance(X1, y1, x2, Y2) + fabs(Y2 - y2));
Y1 = Fy(x1);
X2 = Fx(y2);
ans = min(ans, fabs(y1 - Y1) + Distance(x1, Y1, X2, y2) + fabs(X2 - x2));
Y2 = Fy(x2);
ans = min(ans, fabs(y1 - Y1) + Distance(x1, Y1, x2, Y2) + fabs(y2 - Y2));
printf("%.8f", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
double a, b, c;
cin >> a >> b >> c;
pair<double, double> A, B;
cin >> A.first >> A.second >> B.first >> B.second;
double ans = abs(A.first - B.first) + abs(A.second - B.second);
vector<pair<double, double> > xx(2), yy(2);
xx[0].first = A.first;
xx[0].second = (-a * A.first - c) / b;
xx[1].first = (-b * A.second - c) / a;
xx[1].second = A.second;
yy[0].first = B.first;
yy[0].second = (-a * B.first - c) / b;
yy[1].first = (-b * B.second - c) / a;
yy[1].second = B.second;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
double nr = 0;
if (i == 0)
nr += abs(A.second - xx[i].second);
else
nr += abs(A.first - xx[i].first);
if (j == 0)
nr += abs(B.second - yy[j].second);
else
nr += abs(B.first - yy[j].first);
nr += sqrt(pow(xx[i].first - yy[j].first, 2) +
pow(xx[i].second - yy[j].second, 2));
ans = min(ans, nr);
}
}
cout << fixed;
cout.precision(10);
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
const long long INF = 1e+17;
using namespace std;
struct pt {
long double x, y;
};
long double dist(pt a, pt b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long double a, b, c, x1, x2, y1, y2;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
long double ans = abs(x2 - x1) + abs(y2 - y1);
pair<pt, pt> lr;
lr.first = {(-c - b * y1) / a, y1};
lr.second = {x1, (-c - a * x1) / b};
pt f = {(-c - b * y2) / a, y2};
ans = min(ans, dist(f, lr.first) + abs(x1 - lr.first.x) + abs(x2 - f.x));
ans = min(ans, dist(f, lr.second) + abs(y1 - lr.second.y) + abs(x2 - f.x));
f = {x2, (-c - a * x2) / b};
ans = min(ans, dist(f, lr.first) + abs(x1 - lr.first.x) + abs(y2 - f.y));
ans = min(ans, dist(f, lr.second) + abs(y1 - lr.second.y) + abs(y2 - f.y));
cout << fixed << setprecision(10) << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
double dist(double X1, double X2, double Y1, double Y2) {
return sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2));
}
int main() {
double a, b, c, w, x, y, z, d, x1, x2, y1, y2, x_1, x_2, y_1, y_2;
cin >> a >> b >> c;
cin >> w >> x >> y >> z;
d = abs(w - y) + abs(x - z);
if (a == 0 || b == 0) {
} else {
if (w < y)
x1 = w, y1 = x, x2 = y, y2 = z;
else
x2 = w, y2 = x, x1 = y, y1 = z;
x_1 = (-c - b * y1) / a;
x_2 = (-c - b * y2) / a;
y_1 = (-c - a * x1) / b;
y_2 = (-c - a * x2) / b;
d = min(d,
min(min(abs(x1 - x_1) + dist(x_1, x2, y1, y_2) + abs(y2 - y_2),
abs(y_1 - y1) + dist(x1, x2, y_1, y_2) + abs(y_2 - y2)),
min(abs(x1 - x_1) + dist(x2, x_1, y1, y2) + abs(x2 - x_2),
abs(y_1 - y1) + dist(x1, x_2, y_1, y2) + abs(x2 - x_2))));
d = min(d, abs(x1 - x_1) + dist(x_1, x_2, y1, y2) + abs(x2 - x_2));
}
printf("%0.10lf", d);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
pair<double, double> A, B;
vector<pair<double, double> > ta, tb;
double get_1(pair<double, double> u, pair<double, double> v) {
return abs(u.first - v.first) + abs(u.second - v.second);
}
double get_2(pair<double, double> u, pair<double, double> v) {
return sqrt((u.first - v.first) * (u.first - v.first) +
(u.second - v.second) * (u.second - v.second));
}
pair<double, double> get_dt(long long x) {
return pair<double, double>(x, -(c + a * x) / b);
}
void check(pair<double, double> u, vector<pair<double, double> > &tc) {
double X, Y;
X = u.first;
Y = -(c + a * X) / b;
tc.push_back(pair<double, double>(X, Y));
Y = u.second;
X = -(c + b * Y) / a;
tc.push_back(pair<double, double>(X, Y));
}
int main() {
cin >> a >> b >> c;
cin >> A.first >> A.second >> B.first >> B.second;
double res = get_1(A, B);
if (a == 0 || b == 0) {
cout << fixed << setprecision(10) << res;
return 0;
}
check(A, ta);
check(B, tb);
for (auto u : ta)
for (auto v : tb) {
res = min(res, get_2(A, u) + get_2(B, v) + get_2(u, v));
}
cout << fixed << setprecision(10) << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll inf = 1000000001, INF = (ll)1e18 + 1;
ld sqrd(ld x) { return x * x; }
ld dist(pair<ld, ld> a, pair<ld, ld> b) {
if (a.first == b.first) return abs(a.second - b.second);
if (a.second == b.second) return abs(a.first - b.first);
return sqrt(sqrd(a.first - b.first) + sqrd(a.second - b.second));
}
void solve() {
cout << setprecision(6) << fixed;
ld a, b, c, x1, y1, x2, y2;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
vector<pair<ld, ld>> pa(2), pb(2);
pa[0] = {x1, (ld)((ld)-x1 * a - c) / (ld)b};
pa[1] = {(ld)((ld)-y1 * b - c) / (ld)a, y1};
pb[0] = {x2, (ld)((ld)-x2 * a - c) / (ld)b};
pb[1] = {(ld)((ld)-y2 * b - c) / (ld)a, y2};
ld ans = abs(x1 - x2) + abs(y1 - y2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
ans = min(ans, dist({x1, y1}, pa[i]) + dist(pa[i], pb[j]) +
dist(pb[j], {x2, y2}));
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename TF>
void debug(ostream &out, TF const &f) {
out << f << std::endl;
}
template <typename TF, typename... TR>
void debug(ostream &out, TF const &f, TR const &...rest) {
out << f << " ";
debug(out, rest...);
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vii = vector<pii>;
using vll = vector<ll>;
using vs = vector<string>;
const int DR[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int DC[] = {0, -1, 0, 1, -1, -1, 1, 1};
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const ll INFLL = 4e18;
const int MAX = 1e5;
const ld INF = 1e100;
const ld EPS = 1e-12;
struct PT {
ld x, y;
PT() {}
PT(ld x, ld y) : x(x), y(y) {}
PT(const PT &p) : x(p.x), y(p.y) {}
PT(const PT &a, const PT &b) : x(b.x - a.x), y(b.y - a.y) {}
PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
PT operator*(ld c) const { return PT(x * c, y * c); }
PT operator/(ld c) const { return PT(x / c, y / c); }
};
ostream &operator<<(ostream &os, const PT &p) {
os << "(" << p.x << "," << p.y << ")";
}
ld dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
ld dist2(PT p, PT q) { return dot(p - q, p - q); }
ld dist(PT p, PT q) { return sqrt(dist2(p, q)); }
ld cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }
bool LinesParallel(PT a, PT b, PT c, PT d) {
return fabs(cross(b - a, c - d)) < EPS;
}
bool LinesCollinear(PT a, PT b, PT c, PT d) {
return LinesParallel(a, b, c, d) && fabs(cross(a - b, a - c)) < EPS &&
fabs(cross(c - d, c - a)) < EPS;
}
bool SegmentsIntersect(PT a, PT b, PT c, PT d) {
if (LinesCollinear(a, b, c, d)) {
if (dist2(a, c) < EPS || dist2(a, d) < EPS || dist2(b, c) < EPS ||
dist2(b, d) < EPS)
return true;
if (dot(c - a, c - b) > 0 && dot(d - a, d - b) > 0 && dot(c - b, d - b) > 0)
return false;
return true;
}
if (cross(d - a, b - a) * cross(c - a, b - a) > 0) return false;
if (cross(a - c, d - c) * cross(b - c, d - c) > 0) return false;
return true;
}
PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
b = b - a;
d = c - d;
c = c - a;
assert(dot(b, b) > EPS && dot(d, d) > EPS);
return a + b * cross(c, d) / cross(b, d);
}
ld manhattan(PT p, PT q) { return abs(p.x - q.x) + abs(p.y - q.y); }
ld getY(ld a, ld b, ld c, ld x) { return -(a * x + c) / b; }
ld a, b, c;
PT p0, p1;
void read() {
cin >> a >> b >> c;
cin >> p0.x >> p0.y >> p1.x >> p1.y;
if (p1.x < p0.x) swap(p0, p1);
}
inline ld coba(const PT &cutA, const PT &cutB) {
ld ret = manhattan(p0, cutA);
ret += dist(cutA, cutB);
ret += manhattan(cutB, p1);
return ret;
}
void solve() {
ld ans = manhattan(p0, p1);
if (p0.x == p1.x || p0.y == p1.y) {
cout << ans << "\n";
return;
}
PT seg1a(p0.x, p0.y), seg1b(p1.x, p0.y);
PT seg2a(p0.x, p0.y), seg2b(p0.x, p1.y);
PT seg3a(p0.x, p1.y), seg3b(p1.x, p1.y);
PT seg4a(p1.x, p0.y), seg4b(p1.x, p1.y);
PT L1a(-1e10, getY(a, b, c, -1e10)), L1b(1e10, getY(a, b, c, 1e10));
bool crs1 = SegmentsIntersect(L1a, L1b, seg1a, seg1b);
bool crs2 = SegmentsIntersect(L1a, L1b, seg2a, seg2b);
bool crs3 = SegmentsIntersect(L1a, L1b, seg3a, seg3b);
bool crs4 = SegmentsIntersect(L1a, L1b, seg4a, seg4b);
PT cut1 =
crs1 ? ComputeLineIntersection(L1a, L1b, seg1a, seg1b) : PT(1e10, 1e10);
PT cut2 =
crs2 ? ComputeLineIntersection(L1a, L1b, seg2a, seg2b) : PT(1e10, 1e10);
PT cut3 =
crs3 ? ComputeLineIntersection(L1a, L1b, seg3a, seg3b) : PT(1e10, 1e10);
PT cut4 =
crs4 ? ComputeLineIntersection(L1a, L1b, seg4a, seg4b) : PT(1e10, 1e10);
if (crs1) {
if (crs3) ans = min(ans, coba(cut1, cut3));
if (crs4) ans = min(ans, coba(cut1, cut4));
}
if (crs2) {
if (crs3) ans = min(ans, coba(cut2, cut3));
if (crs4) ans = min(ans, coba(cut2, cut4));
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
int TC = 1;
for (int tc = (int)1; tc <= (int)TC; tc++) {
read();
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
long long x0, y0, x, y;
cin >> a >> b >> c >> x0 >> y0 >> x >> y;
c += a * x0;
c += b * y0;
c = -c;
x -= x0;
y -= y0;
if (x < 0) a = -a, x = -x;
if (y < 0) b = -b, y = -y;
if (c < 0) a = -a, b = -b, c = -c;
if (!x || !y || a * b >= 0)
printf("%.12f\n", 1.0 * (x + y));
else {
double u = min(1.0 * x, max(0., 1.0 * c / a)),
v = max(0., min(1.0 * x, 1.0 * (c - b * y) / a)),
uu = 1.0 * (c - a * u) / b, vv = 1.0 * (c - a * v) / b;
printf("%.12f\n", u + uu + sqrt((v - u) * (v - u) + (vv - uu) * (vv - uu)) +
x + y - v - vv);
}
}
|
#include <bits/stdc++.h>
using namespace std;
long double sq(long double a) { return a * a; }
long double dist(pair<long double, long double> a,
pair<long double, long double> b) {
return sqrt((long double)(sq(a.second - b.second) + sq(a.first - b.first)));
}
long double mandist(pair<long long, long long> a,
pair<long long, long long> b) {
return abs(a.second - b.second) + abs(a.first - b.first);
}
long long a, b, c;
pair<long long, long long> p1, p2;
int main() {
scanf(" %lld%lld%lld", &a, &b, &c);
scanf(" %lld%lld%lld%lld", &p1.first, &p1.second, &p2.first, &p2.second);
if (a == 0 || b == 0)
printf("%.20Lf\n", mandist(p1, p2));
else {
long double res = mandist(p1, p2);
long double interhoriz1 =
((long double)(-c - a * p1.first)) / ((long double)b);
long double interhoriz2 =
((long double)(-c - a * p2.first)) / ((long double)b);
long double intervert1 =
((long double)(-c - b * p1.second)) / ((long double)a);
long double intervert2 =
((long double)(-c - b * p2.second)) / ((long double)a);
res = min(res, abs((long double)p1.second - interhoriz1) +
abs((long double)p2.second - interhoriz2) +
dist(make_pair((long double)p1.first, interhoriz1),
make_pair((long double)p2.first, interhoriz2)));
res = min(res, abs((long double)p1.second - interhoriz1) +
abs((long double)p2.first - intervert2) +
dist(make_pair((long double)p1.first, interhoriz1),
make_pair(intervert2, (long double)p2.second)));
res = min(res, abs((long double)p1.first - intervert1) +
abs((long double)p2.first - intervert2) +
dist(make_pair(intervert1, (long double)p1.second),
make_pair(intervert2, (long double)p2.second)));
res = min(res, abs((long double)p1.first - intervert1) +
abs((long double)p2.second - interhoriz2) +
dist(make_pair(intervert1, (long double)p1.second),
make_pair((long double)p2.first, interhoriz2)));
printf("%.20Lf\n", res);
}
}
|
#include <bits/stdc++.h>
using namespace std;
double get_dis(pair<double, double> a, pair<double, double> b) {
return sqrt((a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second));
}
int main() {
double a, b, c;
cin >> a >> b >> c;
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
double ans_1 = x1, ans_2 = -(a * x1 + c) / (double)b, dist1 = abs(ans_2 - y1);
double ans_3 = -(b * y1 + c) / (double)a, ans_4 = y1, dist2 = abs(ans_3 - x1);
double ans_5 = x2, ans_6 = -(a * x2 + c) / (double)b, dist3 = abs(ans_6 - y2);
double ans_7 = -(b * y2 + c) / (double)a, ans_8 = y2, dist4 = abs(ans_7 - x2);
double res = 1e20;
res = min(res, get_dis({ans_1, ans_2}, {ans_5, ans_6}) + dist1 + dist3);
res = min(res, get_dis({ans_1, ans_2}, {ans_7, ans_8}) + dist1 + dist4);
res = min(res, get_dis({ans_3, ans_4}, {ans_5, ans_6}) + dist2 + dist3);
res = min(res, get_dis({ans_3, ans_4}, {ans_7, ans_8}) + dist2 + dist4);
res = min(res, (double)abs(x1 - x2) + abs(y1 - y2));
printf("%.8lf", res);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct point {
double x, y;
};
double dist(point a, point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main() {
cout.setf(ios::fixed);
cout.precision(8);
double a, b, c;
cin >> a >> b >> c;
point s, e;
cin >> s.x >> s.y >> e.x >> e.y;
point cn1 = {-1e15, -1e15}, cn2 = {-1e15, -1e15}, rc1 = {-1e15, -1e15},
rc2 = {-1e15, -1e15};
if (a != 0) {
cn2 = {-(b * s.y + c) / a, s.y};
rc1 = {-(b * e.y + c) / a, e.y};
}
if (b != 0) {
cn1 = {s.x, -(a * s.x + c) / b};
rc2 = {e.x, -(a * e.x + c) / b};
}
auto len1 = dist(s, cn1) + dist(cn1, rc1) + dist(rc1, e);
auto len2 = dist(s, cn1) + dist(cn1, rc2) + dist(rc2, e);
auto len3 = dist(s, cn2) + dist(cn2, rc1) + dist(rc1, e);
auto len4 = dist(s, cn2) + dist(cn2, rc2) + dist(rc2, e);
auto len = min(min(len3, len4), min(len1, len2));
cout << min(len, abs(s.x - e.x) + abs(s.y - e.y));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double l2(long double x, long double y) { return sqrt(x * x + y * y); }
long double l1(long double x, long double y) { return abs(x) + abs(y); }
int main() {
long long a, b, c, x, y, xx, yy;
cout.precision(17);
cin >> a >> b >> c >> x >> y >> xx >> yy;
if (a == 0 || b == 0 || x == xx || y == yy) {
cout << l1(x - xx, y - yy);
return 0;
}
if (x > xx) {
swap(x, xx);
swap(y, yy);
}
if (y > yy) {
y = -y;
yy = -yy;
b = -b;
}
if (-a / (1.0 * b) < 0) {
cout << l1(x - xx, y - yy);
return 0;
}
xx -= x;
yy -= y;
c += a * x + b * y;
x = 0;
y = 0;
if (-c / (1.0 * b) < 0) {
swap(xx, yy);
swap(a, b);
}
if (-c / (1.0 * b) > yy) {
cout << l1(x - xx, y - yy);
return 0;
}
long double y_cross = -c / (1.0 * b);
long double x_cross = 0;
long double gor_x = -c / (1.0 * a) - b * yy / (1.0 * a);
long double vert_y = -c / (1.0 * b) - a * xx / (1.0 * b);
if (gor_x < xx) {
long double x_cross2 = gor_x;
long double y_cross2 = yy;
cout << y_cross + l2(x_cross2 - x_cross, y_cross2 - y_cross) + xx -
x_cross2;
} else {
long double x_cross2 = xx;
long double y_cross2 = vert_y;
cout << y_cross + l2(x_cross2 - x_cross, y_cross2 - y_cross) + yy -
y_cross2;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int p = 997;
const int base = 1e9 + 7;
const int MAXN = 1000;
const int INF = 1e9;
const int MOD = 1e9;
const double PII = 3.14159265358979323846264338327950288419716939937510;
const double Epsilon = 1.19209e-01;
struct Point {
long double x, y;
Point() : x(0), y(0) {}
Point(long double x0, long double y0) : x(x0), y(y0) {}
long double dist(const Point& other) {
return hypot(other.x - x, other.y - y);
}
bool operator==(const Point& other) {
return (*this).x == other.x && (*this).y == other.y;
}
};
struct Vector {
long double x, y;
Vector() : x(0), y(0) {}
Vector(long double x0, long double y0) : x(x0), y(y0) {}
Vector(const Point& A, const Point& B) {
x = B.x - A.x;
y = B.y - A.y;
}
long double dist() { return hypot(x, y); }
};
struct Line {
long double a, b, c;
Line() : a(0), b(0), c(0) {}
Line(long double f, long double g, long double h) : a(f), b(g), c(h) {}
Line(const Vector& f, const Point& m) {
a = f.y;
b = -f.x;
c = -f.y * m.x + f.x * m.y;
}
};
long double dot_product(const Vector& a, const Vector& b) {
return a.x * b.x + a.y * b.y;
}
long double cross_product(const Vector& a, const Vector& b) {
return a.x * b.y - a.y * b.x;
}
Vector operator+(const Vector& a, const Vector& b) {
return Vector(a.x + b.x, a.y + b.y);
}
long double operator*(const Vector& a, const Vector& b) {
return a.x * b.x + a.y * b.y;
}
ostream& operator<<(ostream& out, const Vector& a) {
out << a.x << " " << a.y;
return out;
}
istream& operator>>(istream& in, Vector& a) {
in >> a.x >> a.y;
return in;
}
istream& operator>>(istream& in, Point& a) {
in >> a.x >> a.y;
return in;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long double x, y, a, b, c, pyt;
cin >> a >> b >> c;
Line prosp(a, b, c);
cin >> x >> y;
Point A(x, y);
cin >> x >> y;
Point B(x, y);
pyt = abs(A.x - B.x) + abs(A.y - B.y);
Line f((long double)1, (long double)0, -A.x);
if (!(prosp.a == f.a && prosp.b == f.b)) {
long double r, k;
r = (f.c * prosp.b - prosp.c * f.b) / (prosp.a * f.b - f.a * prosp.b);
k = (f.c * prosp.a - prosp.c * f.a) / (prosp.b * f.a - f.b * prosp.a);
Point j(r, k);
Line d(1, 0, -B.x);
if (!(prosp.a == d.a && prosp.b == d.b)) {
r = (d.c * prosp.b - prosp.c * d.b) / (prosp.a * d.b - d.a * prosp.b);
k = (d.c * prosp.a - prosp.c * d.a) / (prosp.b * d.a - d.b * prosp.a);
Point jj(r, k);
pyt = min(pyt, A.dist(j) + j.dist(jj) + jj.dist(B));
}
d = Line(0, 1, -B.y);
if (!(prosp.a == d.a && prosp.b == d.b)) {
r = (d.c * prosp.b - prosp.c * d.b) / (prosp.a * d.b - d.a * prosp.b);
k = (d.c * prosp.a - prosp.c * d.a) / (prosp.b * d.a - d.b * prosp.a);
Point jj(r, k);
pyt = min(pyt, A.dist(j) + j.dist(jj) + jj.dist(B));
}
}
f = Line((long double)0, (long double)1, -A.y);
if (!(prosp.a == f.a && prosp.b == f.b)) {
long double r, k;
r = (f.c * prosp.b - prosp.c * f.b) / (prosp.a * f.b - f.a * prosp.b);
k = (f.c * prosp.a - prosp.c * f.a) / (prosp.b * f.a - f.b * prosp.a);
Point j(r, k);
Line d(1, 0, -B.x);
if (!(prosp.a == d.a && prosp.b == d.b)) {
r = (d.c * prosp.b - prosp.c * d.b) / (prosp.a * d.b - d.a * prosp.b);
k = (d.c * prosp.a - prosp.c * d.a) / (prosp.b * d.a - d.b * prosp.a);
Point jj(r, k);
pyt = min(pyt, A.dist(j) + j.dist(jj) + jj.dist(B));
}
d = Line(0, 1, -B.y);
if (!(prosp.a == d.a && prosp.b == d.b)) {
r = (d.c * prosp.b - prosp.c * d.b) / (prosp.a * d.b - d.a * prosp.b);
k = (d.c * prosp.a - prosp.c * d.a) / (prosp.b * d.a - d.b * prosp.a);
Point jj(r, k);
pyt = min(pyt, A.dist(j) + j.dist(jj) + jj.dist(B));
}
}
cout << fixed << setprecision(12) << pyt;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int sum, suf, pre, max;
} Node;
int toint(const string &s) {
stringstream ss;
ss << s;
int x;
ss >> x;
return x;
}
const int MAXN = 2e5 + 100;
const int UP = 31;
const long long int highest = 1e18;
const double pi = acos(-1);
const double Phi = 1.618033988749894;
const int logn = 20;
const double root5 = 2.236067977;
const int INF = 1000000000;
const int ini = -1e9;
const int N = 1e5 + 10;
double inf = 1e9;
double a, b, c, x1, y11, x2, y2;
double sqr(double x) { return x * x; }
double dist(double x1, double y11, double x2, double y2) {
return (sqrt(sqr(x1 - x2) + sqr(y11 - y2)));
}
pair<double, double> getx(double x) { return make_pair(x, (-a * x - c) / b); }
pair<double, double> gety(double y) { return make_pair((-b * y - c) / a, y); }
int main() {
cin >> a >> b >> c;
cin >> x1 >> y11;
cin >> x2 >> y2;
double ans = (abs(x1 - x2) + abs(y11 - y2));
std::vector<pair<double, double> > v1;
std::vector<pair<double, double> > v2;
v1.push_back(getx(x1));
v1.push_back(gety(y11));
v2.push_back(getx(x2));
v2.push_back(gety(y2));
for (auto i : v1) {
for (auto j : v2) {
double dd = dist(x1, y11, i.first, i.second) +
dist(i.first, i.second, j.first, j.second) +
dist(j.first, j.second, x2, y2);
ans = min(ans, dd);
}
}
printf("%.10lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct P {
double x, y;
double operator+(const P& a) const { return abs(a.x - x) + abs(a.y - y); }
double operator*(const P& a) const {
return sqrt((a.x - x) * (a.x - x) + (a.y - y) * (a.y - y));
}
};
double a, b, c, res;
P A, B;
P Ax, Ay, Bx, By;
int main() {
cout.precision(30);
cin >> a >> b >> c;
cin >> A.x >> A.y >> B.x >> B.y;
res = A + B;
if (!a || !b) {
cout << res;
return 0;
}
Ax = (P){A.x, (-c - a * A.x) / b};
Ay = (P){(-c - b * A.y) / a, A.y};
Bx = (P){B.x, (-c - a * B.x) / b};
By = (P){(-c - b * B.y) / a, B.y};
res = min(res, (A + Ax) + (Ax * Bx) + (B + Bx));
res = min(res, (A + Ax) + (Ax * By) + (B + By));
res = min(res, (A + Ay) + (Ay * Bx) + (B + Bx));
res = min(res, (A + Ay) + (Ay * By) + (B + By));
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
cin >> a >> b >> c;
long long x[2], y[2];
for (int i = 0; i < 2; i++) {
cin >> x[i] >> y[i];
}
double ans = 1e10;
ans = abs(x[0] - x[1]) + abs(y[0] - y[1]);
double nx[2], ny[2];
for (int i = 0; i < 2; i++) {
double tmp = 0;
if (a == 0 || b == 0) break;
if (i) {
nx[0] = -1.0 * (b * y[0] + c) / a;
ny[0] = y[0];
} else {
nx[0] = x[0];
ny[0] = -1.0 * (a * x[0] + c) / b;
}
for (int j = 0; j < 2; j++) {
tmp = 0;
if (j) {
nx[1] = x[1];
ny[1] = -1.0 * (a * x[1] + c) / b;
} else {
nx[1] = -1.0 * (b * y[1] + c) / a;
ny[1] = y[1];
}
for (int k = 0; k < 2; k++) {
tmp += abs(ny[k] - y[k]) + abs(nx[k] - x[k]);
}
tmp += sqrt((ny[0] - ny[1]) * (ny[0] - ny[1]) +
(nx[0] - nx[1]) * (nx[0] - nx[1]));
ans = min(ans, tmp);
}
}
printf("%.9f", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int a, b, c;
double x[3], y[3];
set<pair<double, double> > st;
double ans;
double dist(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
cin >> a >> b >> c;
cin >> x[1] >> y[1];
cin >> x[2] >> y[2];
ans = abs(x[1] - x[2]) + abs(y[2] - y[1]);
double X, Y;
if (a != 0) {
X = (-b * y[1] - c + 0.0) / a;
if (min(x[1], x[2]) <= X && X <= max(x[1], x[2]))
st.insert(make_pair(X, y[1]));
X = (-b * y[2] - c + 0.0) / a;
if (min(x[1], x[2]) <= X && X <= max(x[1], x[2]))
st.insert(make_pair(X, y[2]));
}
if (b != 0) {
Y = (-a * x[1] - c + 0.0) / b;
if (min(y[1], y[2]) <= Y && Y <= max(y[1], y[2]))
st.insert(make_pair(x[1], Y));
Y = (-a * x[2] - c + 0.0) / b;
if (min(y[1], y[2]) <= Y && Y <= max(y[1], y[2]))
st.insert(make_pair(x[2], Y));
}
if (st.size() == 2) {
double x1 = st.begin()->first, y1 = st.begin()->second,
x2 = (++st.begin())->first, y2 = (++st.begin())->second;
ans = min(ans, abs(x1 - x[1]) + abs(y1 - y[1]) + dist(x1, y1, x2, y2) +
abs(x2 - x[2]) + abs(y2 - y[2]));
ans = min(ans, abs(x1 - x[2]) + abs(y1 - y[2]) + dist(x1, y1, x2, y2) +
abs(x2 - x[1]) + abs(y2 - y[1]));
}
cout << fixed << setprecision(10) << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long double a, b, c;
long double dis1(long long int x1, long long int y1, long long int x2,
long long int y2) {
return abs(x1 - x2) + abs(y1 - y2);
}
long double dis2(long long int x1, long long int y1, long long int x2,
long long int y2) {
long double x01 = x1;
long double y01 = -(c + a * (long double)x1) / b;
long double sx1 = fabs(y1 - y01);
long double x02 = x2;
long double y02 = -(c + a * (long double)x2) / b;
long double sx2 = fabs(y2 - y02);
long double s = sqrt((x01 - x02) * (x01 - x02) + (y01 - y02) * (y01 - y02));
return sx1 + s + sx2;
}
long double dis3(long long int x1, long long int y1, long long int x2,
long long int y2) {
long double x01 = x1;
long double y01 = -(c + a * (long double)x1) / b;
long double sx1 = fabs(y1 - y01);
long double x02 = -(c + b * (long double)y2) / a;
long double y02 = y2;
long double sy2 = fabs(x2 - x02);
long double s = sqrt((x01 - x02) * (x01 - x02) + (y01 - y02) * (y01 - y02));
return sx1 + s + sy2;
}
long double dis4(long long int x1, long long int y1, long long int x2,
long long int y2) {
long double x01 = -(c + b * (long double)y1) / a;
long double y01 = y1;
long double sy1 = fabs(x1 - x01);
long double x02 = x2;
long double y02 = -(c + a * (long double)x2) / b;
long double sx2 = fabs(y2 - y02);
long double s = sqrt((x01 - x02) * (x01 - x02) + (y01 - y02) * (y01 - y02));
return sy1 + s + sx2;
}
long double dis5(long long int x1, long long int y1, long long int x2,
long long int y2) {
long double x01 = -(c + b * (long double)y1) / a;
long double y01 = y1;
long double sy1 = fabs(x1 - x01);
long double x02 = -(c + b * (long double)y2) / a;
long double y02 = y2;
long double sy2 = fabs(x2 - x02);
long double s = sqrt((x01 - x02) * (x01 - x02) + (y01 - y02) * (y01 - y02));
return sy1 + s + sy2;
}
int main() {
cin >> a >> b >> c;
long long int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
long double ans =
min(dis1(x1, y1, x2, y2),
min(dis2(x1, y1, x2, y2),
min(dis3(x1, y1, x2, y2),
min(dis4(x1, y1, x2, y2), dis5(x1, y1, x2, y2)))));
printf("%.10lf", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long double a, b, c, x1, y1, x2, y2;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
complex<double> A(x1, y1), B(x2, y2), u1, u2, d1, d2;
long double ans = abs(x1 - x2) + abs(y1 - y2);
if (a) {
u1 = complex<double>(((-c - (b * y1)) / a), y1);
u2 = complex<double>(((-c - (b * y2)) / a), y2);
}
if (b) {
d1 = complex<double>(x1, (-c - (a * x1)) / b);
d2 = complex<double>(x2, (-c - (a * x2)) / b);
}
if (!a || !b) {
cout << fixed << setprecision(10) << (long long)ans << '\n';
return 0;
}
ans = min(ans, (long double)abs(A - u1) + abs(u1 - u2) + abs(u2 - B));
ans = min(ans, (long double)abs(A - u1) + abs(u1 - d2) + abs(d2 - B));
ans = min(ans, (long double)abs(A - d1) + abs(d1 - u2) + abs(u2 - B));
ans = min(ans, (long double)abs(A - d1) + abs(d1 - d2) + abs(d2 - B));
cout << fixed << setprecision(10) << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double dist(long double x, long double y, long double a, long double b) {
return sqrt((a - x) * (a - x) + (y - b) * (y - b));
}
long double distMan(long double x, long double y, long double a,
long double b) {
return abs(x - a) + abs(y - b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tt = 1;
while (tt--) {
long double a, b, c;
cin >> a >> b >> c;
long double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (a == 0 || b == 0) {
cout << ' ' << fixed << setprecision(20) << ' '
<< abs(x1 - x2) + abs(y1 - y2) << '\n';
} else {
long double ans = abs(x2 - x1) + abs(y2 - y1);
long double x3, y3, x4, y4;
x3 = x1, x4 = x2;
y3 = (-a * x3 - c) / b, y4 = (-a * x4 - c) / b;
ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +
dist(x4, y4, x3, y3));
x3 = x1, y4 = y2;
y3 = (-a * x3 - c) / b, x4 = (-b * y4 - c) / a;
ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +
dist(x4, y4, x3, y3));
y3 = y1, x4 = x2;
x3 = (-b * y3 - c) / a, y4 = (-a * x4 - c) / b;
ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +
dist(x4, y4, x3, y3));
y3 = y1, y4 = y2;
x3 = (-b * y3 - c) / a, x4 = (-b * y4 - c) / a;
ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +
dist(x4, y4, x3, y3));
cout << ' ' << fixed << setprecision(20) << ' ' << ans << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000000;
int inf = 1000000000;
const int maxn = 100100;
long long a, b, c;
long double getX(long long y) { return -((long double)c + b * y) / a; }
long double getY(long long x) { return -((long double)c + a * x) / b; }
long double len(long double x, long double y) { return sqrt(x * x + y * y); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b >> c;
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (a == 0 || b == 0) {
cout << abs(x1 - x2) + abs(y1 - y2);
return 0;
}
long double p1x = getX(y1);
long double p1y = getY(x1);
long double p2x = getX(y2);
long double p2y = getY(x2);
long double len1 = abs(x1 - x2) + abs(y1 - y2);
long double len2 = len(p1x - p2x, y1 - y2) + abs(x1 - p1x) + abs(x2 - p2x);
long double len3 = len(p1y - p2y, x1 - x2) + abs(y1 - p1y) + abs(y2 - p2y);
long double len4 = len(p1x - x2, y1 - p2y) + abs(x1 - p1x) + abs(y2 - p2y);
long double len5 = len(p1y - y2, x1 - p2x) + abs(y1 - p1y) + abs(x2 - p2x);
long double ans = min(len1, min(len2, min(len3, min(len4, len5))));
cout.precision(15);
cout << fixed << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
long double a, b, c;
pair<long double, long double> one;
pair<long double, long double> two;
long double dis(pair<long double, long double> a,
pair<long double, long double> b) {
return sqrt(pow((a.first - b.first), 2) + pow(a.second - b.second, 2));
}
long double mat(pair<long double, long double> a,
pair<long double, long double> b) {
return abs(a.first - b.first) + abs(a.second - b.second);
}
vector<pair<long double, long double>> inter() {
vector<pair<long double, long double>> v;
set<pair<long double, long double>> st;
long double i1, i2, i3, i4;
i1 = (-b * one.second - c) / a;
i2 = (-a * two.first - c) / b;
i3 = (-b * two.second - c) / a;
i4 = (-a * one.first - c) / b;
if ((i1 <= one.first && i1 >= two.first) ||
(i1 >= one.first && i1 <= two.first)) {
if (!st.count({i1, one.second})) {
v.push_back({i1, one.second});
st.insert({i1, one.second});
}
}
if ((i3 <= one.first && i3 >= two.first) ||
(i3 >= one.first && i3 <= two.first)) {
if (!st.count({i3, two.second})) {
v.push_back({i3, two.second});
st.insert({i3, two.second});
}
}
if ((i2 <= one.second && i2 >= two.second) ||
(i2 >= one.second && i2 <= two.second)) {
if (!st.count({two.first, i2})) {
v.push_back({two.first, i2});
st.insert({two.first, i2});
}
}
if ((i4 <= one.second && i4 >= two.second) ||
(i4 >= one.second && i4 <= two.second)) {
if (!st.count({one.first, i4})) {
v.push_back({one.first, i4});
st.insert({one.first, i4});
}
}
return v;
}
int main() {
cout << setprecision(8);
cin >> a >> b >> c;
cin >> one.first >> one.second >> two.first >> two.second;
long double normaldis =
abs(one.first - two.first) + abs(one.second - two.second);
if (a == 0 || b == 0) {
cout << normaldis << endl;
return 0;
}
vector<pair<long double, long double>> vv = inter();
long double ans;
if (vv.size() != 2)
ans = normaldis;
else
ans = min(mat(one, vv[0]) + mat(two, vv[1]),
mat(one, vv[1]) + mat(two, vv[0])) +
dis(vv[0], vv[1]);
cout << min(ans, normaldis) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long double dist(long double x1, long double y1, long double x2,
long double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
long double a, b, c;
long double x1, y1, x2, y2;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
if (a == 0) {
long double A_inter_y = (-c - a * x1) / b;
long double B_inter_y = (-c - a * x2) / b;
long double d1 = dist(x1, A_inter_y, x2, B_inter_y);
cout << fixed << setprecision(12)
<< min(d1 + abs(y1 - A_inter_y) + abs(y2 - B_inter_y),
abs(x1 - x2) + abs(y1 - y2));
return 0;
} else if (b == 0) {
long double A_inter_x = (-c - b * y1) / a, B_inter_x = (-c - b * y2) / a;
long double d4 = dist(A_inter_x, y1, B_inter_x, y2);
cout << fixed << setprecision(12)
<< min(d4 + abs(x1 - A_inter_x) + abs(x2 - B_inter_x),
abs(x1 - x2) + abs(y1 - y2));
return 0;
}
long double A_inter_y = (-c - a * x1) / b, A_inter_x = (-c - b * y1) / a;
long double B_inter_y = (-c - a * x2) / b, B_inter_x = (-c - b * y2) / a;
long double d1 = dist(x1, A_inter_y, x2, B_inter_y),
d2 = dist(x1, A_inter_y, B_inter_x, y2),
d3 = dist(A_inter_x, y1, x2, B_inter_y),
d4 = dist(A_inter_x, y1, B_inter_x, y2);
vector<pair<long double, string> > d = {
{d1, "1"}, {d2, "2"}, {d3, "3"}, {d4, "4"}};
sort(d.begin(), d.end());
if (d[0].second == "1") {
cout << fixed << setprecision(12)
<< min(d[0].first + abs(y1 - A_inter_y) + abs(y2 - B_inter_y),
abs(x1 - x2) + abs(y1 - y2));
} else if (d[0].second == "2") {
cout << fixed << setprecision(12)
<< min(d[0].first + abs(y1 - A_inter_y) + abs(x2 - B_inter_x),
abs(x1 - x2) + abs(y1 - y2));
} else if (d[0].second == "3") {
cout << fixed << setprecision(12)
<< min(d[0].first + abs(x1 - A_inter_x) + abs(y2 - B_inter_y),
abs(x1 - x2) + abs(y1 - y2));
} else {
cout << fixed << setprecision(12)
<< min(d[0].first + abs(x1 - A_inter_x) + abs(x2 - B_inter_x),
abs(x1 - x2) + abs(y1 - y2));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double d[6][6];
vector<pair<long double, long double>> v;
int main() {
cout.precision(20);
long long i, j, k, a, b, c;
long double x[2], y[2];
cin >> a >> b >> c >> x[0] >> y[0] >> x[1] >> y[1];
for (i = 0; i < 2; i++) v.push_back({x[i], y[i]});
for (i = 0; i < 2; i++) {
if (b) v.push_back({x[i], -((a * x[i] + c) / b)});
if (a) v.push_back({-((b * y[i] + c) / a), y[i]});
}
int n = v.size();
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i == j) {
d[i][j] = 0;
} else if (i >= 2 && j >= 2) {
d[i][j] = sqrt(pow((v[i].first - v[j].first), 2) +
pow((v[i].second - v[j].second), 2));
} else {
d[i][j] = abs(v[i].first - v[j].first) + abs(v[i].second - v[j].second);
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
if (d[j][i] + d[i][k] < d[j][k]) d[j][k] = d[j][i] + d[i][k];
}
}
}
cout << d[0][1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct point {
double x, y;
point() {}
point(double _x, double _y) {
x = _x;
y = _y;
}
double len() { return sqrt(x * x + y * y); }
friend point operator-(const point& a, const point& b) {
return point(a.x - b.x, a.y - b.y);
}
};
struct line {
double a, b, c;
double x(double y) { return ((-c - b * y) / a); }
double y(double x) { return ((-c - a * x) / b); }
};
int main() {
point f, s;
line l;
cin >> l.a >> l.b >> l.c;
cin >> f.x >> f.y >> s.x >> s.y;
double s1 = abs(f.x - s.x) + abs(f.y - s.y);
cout << fixed;
if (l.a == 0 || l.b == 0) {
cout << s1;
return 0;
}
point t1(f.x, l.y(f.x)), t2(l.x(f.y), f.y);
point a = (s - t1).len() - (s - t2).len() > 1e-6 ? t2 : t1;
point t11(s.x, l.y(s.x)), t21(l.x(s.y), s.y);
point b = (f - t11).len() - (f - t21).len() > 1e-6 ? t21 : t11;
double s2 = (abs(f.x - a.x) + abs(f.y - a.y) + (b - a).len() +
abs(s.x - b.x) + abs(s.y - b.y));
cout << min(s1, s2);
}
|
#include <bits/stdc++.h>
using namespace std;
namespace Geom2d {
using ll = long double;
const double pi = acos(-1);
struct point {
ll x, y;
point(ll _x = 0, ll _y = 0) { x = _x, y = _y; }
};
const point ERRPoint = point(-1e12, -1e12);
bool operator==(point a, point b) { return a.x == b.x && a.y == b.y; }
bool operator!=(point a, point b) { return !(a == b); }
bool operator<(point a, point b) {
return make_pair(a.x, a.y) < make_pair(b.x, b.y);
}
bool operator>(point a, point b) { return b < a; }
bool operator<=(point a, point b) { return !(b < a); }
bool operator>=(point a, point b) { return !(a < b); }
ll dist(point a, point b) {
return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y);
}
double sqdist(point a, point b) { return sqrt((double)dist(a, b)); }
struct vect {
ll x, y;
vect(ll _x = 0, ll _y = 0) { x = _x, y = _y; }
vect(point a) { x = a.x, y = a.y; }
};
const vect ERRVect = vect(-1e12, -1e12);
vect operator+(vect a, vect b) { return vect(a.x + b.x, a.y + b.y); }
vect operator-(vect a, vect b) { return vect(a.x - b.x, a.y - b.y); }
vect operator*(vect a, ll k) { return vect(a.x * k, a.y * k); }
vect operator/(vect a, ll k) { return vect(a.x / k, a.y / k); }
ll operator*(vect a, vect b) { return a.x * b.x + a.y * b.y; }
ll operator^(vect a, vect b) { return a.x * b.y - b.x * a.y; }
ll dist(vect a) { return a.x * a.x + a.y * a.y; }
double sqdist(vect a) { return sqrt((double)dist(a)); }
bool operator==(vect a, vect b) { return a.x == b.x && a.y == b.y; }
bool operator!=(vect a, vect b) { return !(a == b); }
bool operator<(vect a, vect b) {
ll v = a ^ b;
if (v == 0)
return dist(a) < dist(b);
else
return v > 0;
}
bool operator>(vect a, vect b) { return b < a; }
bool operator<=(vect a, vect b) { return !(b < a); }
bool operator>=(vect a, vect b) { return !(a < b); }
point operator+(point a, vect b) { return point(a.x + b.x, a.y + b.y); }
vect operator-(point a, point b) { return vect(a.x - b.x, a.y - b.y); }
vect norm(vect a) { return a / sqdist(a); }
double angle(vect a, vect b) { return atan2(a ^ b, a * b); }
struct line {
ll a, b, c;
line(ll _a = 0, ll _b = 0, ll _c = 0) { a = _a, b = _b, c = _c; }
line(point p1, point p2) {
a = p1.y - p2.y, b = p2.x - p1.x, c = p1.x * p2.y - p2.x * p1.y;
}
};
const line ERRLine = line(0, 0, 0);
ll dist(point a, line l) {
ll val = l.a * a.x + l.b * a.y + l.c;
return (val * val) / (l.a * l.a + l.b * l.b);
}
double sqdist(point a, line l) { return sqrt((double)dist(a, l)); }
point intersect(line l1, line l2) {
ll det = l1.a * l2.b - l2.a * l1.b;
if (det == 0) return ERRPoint;
return point((l2.c * l1.b - l1.c * l2.b) / det,
-(l2.c * l1.a - l1.c * l2.a) / det);
}
line move(line l, vect a) {
return line(l.a, l.b, l.c - l.a * a.x - l.b * a.y);
}
point get_point(line l, ll c) {
if (l.a != 0) {
return point((-l.c - l.b * c) / l.a, c);
}
if (l.b != 0) {
return point(c, (-l.c - l.a * c) / l.b);
}
}
} // namespace Geom2d
using namespace Geom2d;
int main() {
cout.precision(30);
line l;
point p1, p2;
cin >> l.a >> l.b >> l.c;
cin >> p1.x >> p1.y >> p2.x >> p2.y;
ll var1 = abs(p1.x - p2.x) + abs(p1.y - p2.y);
line l1 = line(p1, p1 + vect(1, 0));
line l2 = line(p1, p1 + vect(0, 1));
line l3 = line(p2, p2 + vect(1, 0));
line l4 = line(p2, p2 + vect(0, 1));
point p11 = intersect(l, l1);
point p12 = intersect(l, l2);
point p13 = intersect(l, l3);
point p14 = intersect(l, l4);
vector<point> ps1 = {p11, p12}, ps2 = {p13, p14};
vector<ll> dist1(2), dist2(2);
for (int i = 0; i < 2; i++) {
if (ps1[i] != ERRPoint) dist1[i] = sqdist(ps1[i], p1);
}
for (int i = 0; i < 2; i++) {
if (ps2[i] != ERRPoint) dist2[i] = sqdist(ps2[i], p2);
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (ps1[i] == ERRPoint || ps2[j] == ERRPoint) continue;
var1 = min(var1, dist1[i] + dist2[j] + sqdist(ps1[i], ps2[j]));
}
}
cout << var1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
struct point {
double x, y;
};
void build(vector<point> &P, point p) {
if (b) P.push_back({p.x, 1.0 * (-c - a * p.x) / b});
if (a) P.push_back({1.0 * (-c - b * p.y) / a, p.y});
}
double sqr(double x) { return x * x; }
double dist(point p1, point p2) {
return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y));
}
int main() {
cin >> a >> b >> c;
point p1, p2;
cin >> p1.x >> p1.y >> p2.x >> p2.y;
vector<point> P1, P2;
build(P1, p1);
build(P2, p2);
double ans = abs(p1.x - p2.x) + abs(p2.y - p1.y);
for (auto x : P1)
for (auto y : P2) ans = min(ans, dist(p1, x) + dist(x, y) + dist(y, p2));
cout << fixed << setprecision(10) << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
int nint() {
int x;
scanf("%d", &x);
return x;
}
const long long mod = 1000000000 + 7;
int m;
int n;
long long ans;
long long a, b, c;
double x1, y95874, x2, y2;
double x[50], y[50];
int t[50];
double getSimple(double x1, double y95874, double x2, double y2) {
return abs(x1 - x2) + abs(y95874 - y2);
}
double get(int p1, int p2) {
double ans = getSimple(x[p1], y[p1], x[p2], y[p2]);
if (t[p1] && t[p2])
ans = min(ans, sqrt(abs(((x[p1] - x[p2]) * (x[p1] - x[p2])) +
((y[p1] - y[p2]) * (y[p1] - y[p2])))));
return ans;
}
void add(double cx, double cy, int ct) {
x[m] = cx;
y[m] = cy;
t[m] = ct;
m++;
}
double d[50];
int was[50];
double dij(int x, int y) {
for (int i = 0; i < (n); i++) d[i] = 100 * mod;
d[x] = 0;
for (int o = 0; o < (n); o++) {
int id = -1;
for (int i = 0; i < (n); i++) {
if (was[i]) continue;
if (id == -1 || d[id] > d[i]) id = i;
}
was[id] = 1;
for (int i = 0; i < (n); i++) d[i] = min(d[i], d[id] + get(i, id));
}
return d[y];
}
int main() {
do {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
} while (0);
cin >> a >> b >> c;
cin >> x1 >> y95874 >> x2 >> y2;
if (a == 0 || b == 0) {
printf("%.6lf", getSimple(x1, y95874, x2, y2));
return 0;
}
add(x1, y95874, 0);
add(x2, y2, 0);
for (auto o : {make_pair(x1, y95874), make_pair(x2, y2)}) {
double cx = o.first;
double cy = -((double)a * cx + (double)c) / (double)b;
add(cx, cy, 1);
cy = o.second;
cx = -((double)b * cy + (double)c) / (double)a;
add(cx, cy, 1);
}
n = m;
printf("%.6lf", dij(0, 1));
}
|
#include <bits/stdc++.h>
using namespace std;
void solve();
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t = 1;
for (int i = 1; i <= t; ++i) solve();
cerr << "Time taken: " << ((clock() * 1000) / CLOCKS_PER_SEC) << "ms\n";
}
long double a, b, c;
pair<long double, long double> f(pair<long double, long double> p) {
return {(-c - b * p.second) / a, (-c - a * p.first) / b};
}
long double dis(pair<long double, long double> p,
pair<long double, long double> q) {
return sqrt((p.first - q.first) * (p.first - q.first) +
(p.second - q.second) * (p.second - q.second));
}
void solve() {
cin >> a >> b >> c;
pair<long double, long double> p, q;
cin >> p.first >> p.second >> q.first >> q.second;
if (a == 0 or b == 0) {
cout << setprecision(10)
<< abs(p.first - q.first) + abs(p.second - q.second) << '\n';
return;
}
pair<long double, long double> l = f(p);
pair<long double, long double> m = f(q);
pair<long double, pair<long double, long double> > P[4];
P[0] = {abs(p.second - l.second), {p.first, l.second}};
P[1] = {abs(p.first - l.first), {l.first, p.second}};
P[2] = {abs(q.second - m.second), {q.first, m.second}};
P[3] = {abs(q.first - m.first), {m.first, q.second}};
long double ans = abs(p.first - q.first) + abs(p.second - q.second);
for (int i = 0; i < 2; ++i) {
for (int j = 2; j < 4; ++j) {
ans = min(ans, P[i].first + P[j].first + dis(P[i].second, P[j].second));
}
}
cout << setprecision(10) << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct {
double x, y;
} point;
double dis(point a, point b) {
return (sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
}
int main() {
double a, b, c;
cin >> a >> b >> c;
point A, B;
cin >> A.x >> A.y >> B.x >> B.y;
if (b == 0) {
point A1, B1;
A1.x = -(double)c / a;
A1.y = A.y;
B1.x = -(double)c / a;
B1.y = B.y;
double mandis = abs(A.x - B.x) + abs(A.y - B.y);
double AA1, BB1, A1B1;
AA1 = dis(A, A1);
BB1 = dis(B, B1);
A1B1 = dis(A1, B1);
double ans = min(mandis, (AA1 + BB1 + A1B1));
printf("%.10lf", ans);
} else if (a == 0) {
point A1, B1;
A1.x = A.x;
A1.y = -(double)c / b;
B1.y = -(double)c / b;
B1.x = B.x;
double mandis = abs(A.x - B.x) + abs(A.y - B.y);
double AA1, BB1, A1B1;
AA1 = dis(A, A1);
BB1 = dis(B, B1);
A1B1 = dis(A1, B1);
double ans = min(mandis, (AA1 + BB1 + A1B1));
printf("%.10lf", ans);
} else {
point A1, A2, B1, B2;
A1.x = A.x;
A1.y = -((double)(a * A.x + c) / b);
A2.x = -((double)(b * A.y + c) / a);
A2.y = A.y;
B1.x = B.x;
B1.y = -((double)(a * B.x + c) / b);
B2.x = -((double)(b * B.y + c) / a);
B2.y = B.y;
double mandis = abs(A.x - B.x) + abs(A.y - B.y);
double AA1, AA2, BB1, BB2, A1B1, A1B2, A2B1, A2B2;
AA1 = dis(A, A1);
AA2 = dis(A, A2);
BB1 = dis(B, B1);
BB2 = dis(B, B2);
A1B1 = dis(A1, B1);
A1B2 = dis(A1, B2);
A2B1 = dis(A2, B1);
A2B2 = dis(A2, B2);
double path1, path2, path3, path4;
path1 = AA1 + A1B1 + BB1;
path2 = AA1 + A1B2 + BB2;
path3 = AA2 + A2B1 + BB1;
path4 = AA2 + A2B2 + BB2;
vector<double> paths;
paths.push_back(path1);
paths.push_back(path2);
paths.push_back(path3);
paths.push_back(path4);
sort(paths.begin(), paths.end());
double ans = min(paths[0], mandis);
printf("%.10lf", ans);
}
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
inline long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
inline long long power(long long a, long long n, long long m) {
if (n == 0) return 1;
long long p = power(a, n / 2, m);
p = (p * p) % m;
if (n % 2)
return (p * a) % m;
else
return p;
}
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 1000000007;
int main() {
long double a, b, c;
long double inf = -10000000000000000.0;
cin >> a >> b >> c;
long double x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
pair<long double, long double> p1[3];
pair<long double, long double> p2[3];
long double cost[3];
long double cost2[3];
cost[0] = 0;
cost2[0] = 0;
long double mn = 100000000000000.0;
p1[0].first = x1;
p1[0].second = y1;
p2[0].first = x2;
p2[0].second = y2;
long double vl;
if (a != 0) {
vl = -c;
vl = vl - b * y1;
vl = (long double)vl / a;
p1[1].first = vl;
p1[1].second = y1;
cost[1] = abs(vl - x1);
} else {
p1[1].first = -10000000000000000.0;
p2[1].second = -10000000000000000.0;
}
if (b != 0) {
vl = -c;
vl = vl - a * x1;
vl = (long double)vl / b;
p1[2].first = x1;
p1[2].second = vl;
cost[2] = abs(vl - y1);
} else {
p1[2].first = -10000000000000000.0;
p1[2].second = -10000000000000000.0;
}
if (a != 0) {
vl = -c;
vl = vl - b * y2;
vl = (long double)vl / a;
p2[1].first = vl;
p2[1].second = y2;
cost2[1] = abs(vl - x2);
} else {
p2[1].first = -10000000000000000.0;
p2[1].second = -10000000000000000.0;
}
if (b != 0) {
vl = -c;
vl = vl - a * x2;
vl = (long double)vl / b;
p2[2].first = x2;
p2[2].second = vl;
cost2[2] = abs(vl - y2);
} else {
p2[2].first = -10000000000000000.0;
p2[2].second = -10000000000000000.0;
}
mn = min(mn,
abs(p1[0].first - p2[0].first) + abs(p1[0].second - p2[0].second));
long double ans;
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
if (p1[i].first != inf && p1[j].first != inf) {
ans = cost[i] + cost2[j];
x1 = abs(p1[i].first - p2[j].first);
y1 = abs(p1[i].second - p2[j].second);
x1 = x1 * x1;
y1 = y1 * y1;
x1 = sqrt(x1 + y1);
mn = min(mn, ans + x1);
}
}
}
cout << setprecision(20);
cout << mn << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
using namespace std;
long double fx(long double a, long double b, long double y, long double c) {
return (-c - y * b) / a;
}
long double fy(long double a, long double x, long double b, long double c) {
return (-c - a * x) / b;
}
long double second(long double x1, long double y1, long double x2,
long double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
signed main() {
long double a, b, c;
cin >> a >> b >> c;
long double x, y, x1, y1;
cin >> x >> y >> x1 >> y1;
long double S1 = abs(x - x1) + abs(y - y1);
long double xx1 = fx(a, b, y, c);
long double yy1 = fy(a, x, b, c);
long double xx2 = fx(a, b, y1, c);
long double yy2 = fy(a, x1, b, c);
S1 = min(abs(xx1 - x) + abs(xx2 - x1) + second(xx1, y, xx2, y1), S1);
S1 = min(S1, abs(xx1 - x) + abs(yy2 - y1) + second(xx1, y, x1, yy2));
S1 = min(S1, abs(yy1 - y) + abs(xx2 - x1) + second(x, yy1, xx2, y1));
S1 = min(abs(yy1 - y) + abs(yy2 - y1) + second(x, yy1, x1, yy2), S1);
cout << fixed << setprecision(15) << S1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long md = 998244353;
const long double eps = 1e-6;
const int maxn = 1e6;
mt19937 rnd(100500);
int main() {
ios_base::sync_with_stdio(false);
;
cin.tie(NULL);
;
long double a, b, c, x1, y1, x2, y2, ans = 1e18;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
cout << fixed << setprecision(10);
if (x1 == x2 || y1 == y2) {
cout << fabs(x1 - x2) + fabs(y1 - y2);
return 0;
}
if (a == 0 || b == 0)
ans = fabs(x1 - x2) + fabs(y1 - y2);
else {
ans = fabs(x1 - x2) + fabs(y1 - y2);
long double x3 = x1;
long double y3 = (-c - a * x1) / b;
long double x4 = x2;
long double y4 = (-c - a * x2) / b;
long double d =
fabs(y3 - y1) + fabs(y4 - y2) +
sqrt(fabs(x3 - x4) * fabs(x3 - x4) + fabs(y3 - y4) * fabs(y3 - y4));
if (ans - d > eps) ans = d;
y4 = y2;
x4 = (-c - b * y2) / a;
d = fabs(y3 - y1) + fabs(x4 - x2) +
sqrt(fabs(x3 - x4) * fabs(x3 - x4) + fabs(y3 - y4) * fabs(y3 - y4));
if (ans - d > eps) ans = d;
y3 = y1;
x3 = (-c - b * y1) / a;
x4 = x2;
y4 = (-c - a * x2) / b;
d = fabs(x3 - x1) + fabs(y4 - y2) +
sqrt(fabs(x3 - x4) * fabs(x3 - x4) + fabs(y3 - y4) * fabs(y3 - y4));
if (ans - d > eps) ans = d;
y3 = y1;
x3 = (-c - b * y1) / a;
y4 = y2;
x4 = (-c - b * y2) / a;
d = fabs(x3 - x1) + fabs(x4 - x2) +
sqrt(fabs(x3 - x4) * fabs(x3 - x4) + fabs(y3 - y4) * fabs(y3 - y4));
if (ans - d > eps) ans = d;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline double d(double dx, double dy) { return sqrt(dx * dx + dy * dy); }
int main() {
int a, b, c, x0, y0, x1, y1;
scanf("%d%d%d%d%d%d%d", &a, &b, &c, &x0, &y0, &x1, &y1);
double mind = abs<double>(x1 - x0) + abs<double>(y1 - y0);
if (a != 0 && b != 0) {
double ax0 = -(double)((long long)b * y0 + c) / a;
double ay0 = -(double)((long long)a * x0 + c) / b;
double ax1 = -(double)((long long)b * y1 + c) / a;
double ay1 = -(double)((long long)a * x1 + c) / b;
double dxx = abs(ax0 - x0) + d(ax1 - ax0, y1 - y0) + abs(x1 - ax1);
double dxy = abs(ax0 - x0) + d(x1 - ax0, ay1 - y0) + abs(y1 - ay1);
double dyx = abs(ay0 - y0) + d(ax1 - x0, y1 - ay0) + abs(x1 - ax1);
double dyy = abs(ay0 - y0) + d(x1 - x0, ay1 - ay0) + abs(y1 - ay1);
mind = min(mind, min(min(dxx, dxy), min(dyx, dyy)));
}
printf("%.10lf\n", mind);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a, b, c, xx1, xx2, yy1, yy2, kx1, kx2, ky1, ky2, r1, r2, r3, r4, r5;
double rast(double x, double y, double x1, double y1) {
return (sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)));
}
int main() {
cin >> a >> b >> c;
cin >> xx1 >> yy1;
cin >> xx2 >> yy2;
r1 = (abs(xx1 - xx2) + abs(yy1 - yy2));
kx1 = ((-1) * (c + b * yy1)) / a;
kx2 = ((-1) * (c + b * yy2)) / a;
ky1 = ((-1) * (c + a * xx1)) / b;
ky2 = ((-1) * (c + a * xx2)) / b;
r2 = abs(xx1 - kx1) + rast(kx1, yy1, kx2, yy2) + abs(kx2 - xx2);
r3 = abs(xx1 - kx1) + rast(kx1, yy1, xx2, ky2) + abs(ky2 - yy2);
r4 = abs(yy1 - ky1) + rast(xx1, ky1, kx2, yy2) + abs(kx2 - xx2);
r5 = abs(yy1 - ky1) + rast(xx1, ky1, xx2, ky2) + abs(ky2 - yy2);
cout.precision(11);
cout << min(r1, min(r2, min(r3, min(r4, r5))));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void acceleration() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
acceleration();
double ax, ay, bx, by, a, b, c;
cin >> a >> b >> c >> ax >> ay >> bx >> by;
double ans = abs(ax - bx) + abs(ay - by);
double y1 = -(a * ax + c) / b, x1 = -(b * ay + c) / a;
double r1 = abs(ax - x1), r2 = abs(ay - y1);
double y2 = -(a * bx + c) / b, x2 = -(b * by + c) / a;
double r3 = abs(bx - x2), r4 = abs(by - y2);
double s1 = sqrtl((ax - bx) * (ax - bx) + (y1 - y2) * (y1 - y2));
double s2 = sqrtl((ax - x2) * (ax - x2) + (y1 - by) * (y1 - by));
double s3 = sqrtl((x1 - bx) * (x1 - bx) + (ay - y2) * (ay - y2));
double s4 = sqrtl((x1 - x2) * (x1 - x2) + (ay - by) * (ay - by));
double p1 = r2 + s1 + r4;
double p2 = r2 + s2 + r3;
double p3 = r1 + s3 + r4;
double p4 = r1 + s4 + r3;
ans = min(ans, min(p1, min(p2, min(p3, p4))));
cout << fixed << setprecision(15) << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
float phytag(pair<float, float> a, pair<float, float> b) {
return sqrt(pow(a.first - b.first, 2) + pow(a.second - b.second, 2));
}
int main() {
float a, b, c;
pair<float, float> p[2];
pair<float, float> px[2], py[2];
cin >> a >> b >> c;
cin >> p[0].first >> p[0].second >> p[1].first >> p[1].second;
for (int i = 0; i < 2; i++) {
px[i].first = p[i].first;
px[i].second = (a * p[i].first + c) / (float)-b;
py[i].second = p[i].second;
py[i].first = (b * p[i].second + c) / (float)-a;
}
float minDist =
(float)(abs(p[0].first - p[1].first) + abs(p[0].second - p[1].second));
printf(
"%.10f\n",
fmin(
minDist,
fmin(
fmin(fabs(p[0].second - px[0].second) +
fabs(p[1].second - px[1].second) + phytag(px[0], px[1]),
fabs(p[0].first - py[0].first) +
fabs(p[1].second - px[1].second) + phytag(py[0], px[1])),
fmin(fabs(p[0].second - px[0].second) +
fabs(p[1].first - py[1].first) + phytag(px[0], py[1]),
fabs(p[0].first - py[0].first) +
fabs(p[1].first - py[1].first) +
phytag(py[0], py[1])))));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > point(4);
int sign(long long int x) {
if (x > 0) return 1;
if (x < 0) return -1;
if (x == 0) return 0;
}
int check(long long int a, long long int b, long long int c) {
int i;
long long int tp;
vector<int> side(4);
for (i = 0; i < 4; i++)
tp = (a * point[i].first + b * point[i].second + c), side[i] = sign(tp);
if (side[0] > 0 & side[1] > 0 & side[2] > 0 & side[3] > 0) return 0;
if (side[0] < 0 & side[1] < 0 & side[2] < 0 & side[3] < 0) return 0;
return 1;
}
double dis(double w, double x, double y, double z) {
double tp;
double d;
tp = ((w - y) * (w - y) + (x - z) * (x - z));
d = sqrt(tp);
return d;
}
int main() {
long long int a, b, c, x1, x2, y1, y2;
int f, k;
cin >> a >> b >> c;
double ans = (1e10);
double p, q, temp, r;
cout << setprecision(12) << fixed;
f = 0;
cin >> x1 >> y1 >> x2 >> y2;
point[0] = {x1, y1};
point[1] = {x2, y2};
point[2] = {x1, y2};
point[3] = {x2, y1};
if (a == 0 || b == 0)
f = 1;
else {
if (!check(a, b, c))
f = 1;
else {
k = -1, k *= sign(a * b), k *= sign((y2 - y1) * (x2 - x1));
if (k <= 0)
f = 1;
else {
if (sign(a * x1 + b * y1 + c) * sign(a * x2 + b * y2 + c) >= 0) {
p = -c - a * x1, p /= b, q = -c - b * y2, q /= a;
temp = abs(y1 - p) + abs(x2 - q) + dis(x1, p, q, y2);
ans = min(ans, temp);
p = -c - a * x2, p /= b, q = -c - b * y1, q /= a;
temp = abs(y2 - p) + abs(x1 - q) + dis(x2, p, q, y1);
ans = min(ans, temp);
} else {
p = -c - b * y1, p /= a, q = -c - b * y2, q /= a;
temp = abs(x1 - p) + abs(x2 - q) + dis(p, y1, q, y2);
ans = min(ans, temp);
p = -c - a * x1, p /= b, q = -c - a * x2, q /= b;
temp = abs(y1 - p) + abs(y2 - q) + dis(x1, p, x2, q);
ans = min(ans, temp);
}
}
}
}
temp = 0;
if (f) ans = abs(x1 - x2) + abs(y1 - y2);
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long double dist(long double x1, long double y1, long double x2,
long double y2) {
return fabs(x1 - x2) + fabs(y1 - y2);
}
long double dist2(long double x1, long double y1, long double x2,
long double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
long double a, b, c, x1, y1, x2, y2;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
long double ans = dist(x1, y1, x2, y2);
vector<pair<long double, pair<long double, long double> > > v1, v2;
if (b != 0) {
long double x0 = x1, y0 = (-c - x1 * a) / b;
v1.push_back({dist(x1, y1, x0, y0), {x0, y0}});
}
if (a != 0) {
long double x0 = (-c - y1 * b) / a, y0 = y1;
v1.push_back({dist(x0, y0, x1, y1), {x0, y0}});
}
if (b != 0) {
long double x0 = x2, y0 = (-c - x2 * a) / b;
v2.push_back({dist(x2, y2, x0, y0), {x0, y0}});
}
if (a != 0) {
long double x0 = (-c - y2 * b) / a, y0 = y2;
v2.push_back({dist(x0, y0, x2, y2), {x0, y0}});
}
for (auto p1 : v1) {
for (auto p2 : v2) {
ans = min(ans, p1.first + p2.first +
dist2(p1.second.first, p1.second.second,
p2.second.first, p2.second.second));
}
}
cout << fixed << setprecision(8);
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c;
long long int x1, x2, y1, y2;
double ans = 1000.0 * 1000 * 1000 * 1000;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
if (a == 0 || b == 0) {
ans = abs(y1 - y2) + abs(x1 - x2);
} else {
double ydwx1 = ((-a + 0.0) / (b + 0.0)) * ((x1 + 0.0)) - ((c + 0.0) / b);
double ydwx2 = ((-a + 0.0) / (b + 0.0)) * ((x2 + 0.0)) - ((c + 0.0) / b);
double xdwy1 = ((-b + 0.0) / (a + 0.0)) * ((y1 + 0.0)) - ((c + 0.0) / a);
double xdwy2 = ((-b + 0.0) / (a + 0.0)) * ((y2 + 0.0)) - ((c + 0.0) / a);
double posans1 =
abs(y1 - ydwx1) +
sqrt((y2 - ydwx1) * (y2 - ydwx1) + (xdwy2 - x1) * (xdwy2 - x1)) +
abs(x2 - xdwy2);
double posans2 =
abs(y1 - ydwx1) +
sqrt((ydwx2 - ydwx1) * (ydwx2 - ydwx1) + (x2 - x1) * (x2 - x1)) +
abs(y2 - ydwx2);
double posans3 =
abs(x1 - xdwy1) +
sqrt((y2 - y1) * (y2 - y1) + (xdwy2 - xdwy1) * (xdwy2 - xdwy1)) +
abs(x2 - xdwy2);
double posans4 =
abs(x1 - xdwy1) +
sqrt((ydwx2 - y1) * (ydwx2 - y1) + (x2 - xdwy1) * (x2 - xdwy1)) +
abs(y2 - ydwx2);
ans = min(abs(y2 + 0.0 - y1 + 0.0) + abs(x2 + 0.0 - x1 + 0.0),
min(ans, min(posans1, (min(posans2, min(posans3, posans4))))));
}
cout << fixed << setprecision(15) << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long double a, b, c, x1, y1, x2, y2;
cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;
complex<long double> A(x1, y1), B(x2, y2), p1, p2, p3, p4;
long double ans = abs(x1 - x2) + abs(y2 - y1);
if (a) {
p1 = complex<long double>((-c - (y1 * b)) / a, y1);
p3 = complex<long double>((-c - (y2 * b)) / a, y2);
}
if (b) {
p2 = complex<long double>(x1, (-c - (x1 * a)) / b);
p4 = complex<long double>(x2, (-c - (x2 * a)) / b);
}
if (!a || !b) return cout << fixed << setprecision(12) << ans, 0;
long double option1, option2, option3, option4;
option1 = abs(A - p1) + abs(p1 - p3) + abs(p3 - B);
option2 = abs(A - p2) + abs(p2 - p4) + abs(p4 - B);
option3 = abs(A - p1) + abs(p1 - p4) + abs(p4 - B);
option4 = abs(A - p2) + abs(p2 - p3) + abs(p3 - B);
cout << fixed << setprecision(12)
<< min(ans, min(option1, min(option2, min(option3, option4)))) << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.