output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
const int N = 200054, M = N * 2;
int n, E = 0, root;
int to[M], first[N], next[M];
int a[N], b[N], Lu, Lv, Lt;
int p[N], dep[N];
int diff = 0, buc[N];
int len = 0, cyc[N], in_cyc[N];
int A[N], B[N];
inline long long DD(long long &x, const long long y) {
return x > y ? x = y : 0;
}
inline int dmax(const int x, const int y) { return dep[x] < dep[y] ? y : x; }
inline void addedge(int u, int v) {
to[++E] = v, next[E] = first[u], first[u] = E;
to[++E] = u, next[E] = first[v], first[v] = E;
}
void dfs(int x) {
int i, y;
for (i = first[x]; i; i = next[i])
if ((y = to[i]) != p[x]) p[y] = x, dep[y] = dep[x] + 1, dfs(y);
}
int LCA(int x, int y) {
for (; x != y; dep[x] < dep[y] ? y = p[y] : (x = p[x]))
;
return x;
}
inline bool find_endpoint() {
int i;
for (i = 1; i < diff && p[buc[i]] == buc[i - 1]; ++i)
;
if (i == diff) return Lt = Lu = p[*buc], Lv = buc[diff - 1], true;
Lu = Lv = Lt = p[*buc];
for (i = 0; i < diff; ++i) {
if (p[buc[i]] == Lu)
Lu = buc[i];
else if (p[buc[i]] == Lv)
Lv = buc[i];
else
return false;
}
return true;
}
int main() {
int i, j, u, v, w, z, offset;
long long D;
scanf("%d", &n);
for (i = 1; i <= n; ++i) scanf("%d", a + i);
for (i = 1; i <= n; ++i) scanf("%d", b + i);
for (i = 1; i < n; ++i) scanf("%d%d", &u, &v), addedge(u, v);
dfs(root = std::find(b + 1, b + (n + 1), 0) - b);
u = v = std::find(a + 1, a + (n + 1), 0) - a;
for (i = 0; u != root; ++i) std::swap(a[u], a[p[u]]), u = p[u];
if (!memcmp(a + 1, b + 1, n << 2)) return printf("0 %d\n", i), 0;
for (i = 1; i <= n; ++i)
if (a[i] != b[i]) buc[diff++] = i;
std::sort(buc, buc + diff,
[](const int x, const int y) { return dep[x] < dep[y]; });
if (!find_endpoint()) return puts("-1"), 0;
if (Lu > Lv) std::swap(Lu, Lv);
cyc[len++] = Lt;
for (i = Lu; i != Lt; i = p[i]) cyc[len++] = i;
std::reverse(cyc + 1, cyc + len);
for (i = Lv; i != Lt; i = p[i]) cyc[len++] = i;
for (i = 0; i < len; ++i) in_cyc[cyc[i]] = i;
assert(len > 2);
w = LCA(v, Lt);
if (w == Lt) z = dmax(LCA(v, Lu), LCA(v, Lv));
if (w == Lt && w != z) {
if ((in_cyc[z] + 1) % len != in_cyc[p[z]])
for (std::reverse(cyc + 1, cyc + len), i = 0; i < len; ++i)
in_cyc[cyc[i]] = i;
assert((in_cyc[z] + 1) % len == in_cyc[p[z]]);
for (j = 0, i = Lt; i != root; i = p[i]) buc[j++] = i;
for (; j--;
std::swap(a[buc[j]], a[p[buc[j]]]), std::swap(b[buc[j]], b[p[buc[j]]]))
;
for (i = 0; i < len; ++i) A[i] = a[cyc[i]], B[i] = b[cyc[i]];
assert(!(*A || *B));
offset = std::find(A + 1, A + len, B[1]) - A;
if (offset >= len ||
memcmp(A + 1, B + (len - offset + 1), (offset - 1) << 2) ||
memcmp(A + offset, B + 1, (len - offset) << 2))
return puts("-1"), 0;
;
D = dep[v] +
std::min(len * (offset - 1ll),
(long long)len * (len - offset) - 2 * (dep[z] - dep[w]));
} else {
for (j = 0, i = Lt; i != root; i = p[i]) buc[j++] = i;
for (; j--;
std::swap(a[buc[j]], a[p[buc[j]]]), std::swap(b[buc[j]], b[p[buc[j]]]))
;
for (i = 0; i < len; ++i) A[i] = a[cyc[i]], B[i] = b[cyc[i]];
assert(!(*A || *B));
offset = std::find(A + 1, A + len, B[1]) - A;
if (offset >= len ||
memcmp(A + 1, B + (len - offset + 1), (offset - 1) << 2) ||
memcmp(A + offset, B + 1, (len - offset) << 2))
return puts("-1"), 0;
;
D = dep[v] + 2 * (dep[Lt] - dep[w]) +
(long long)len * std::min(offset - 1, len - offset);
}
printf("%d %d %lld\n", Lu, Lv, D);
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 200054, M = N * 2;
int n, E = 0, root;
int to[M], first[N], next[M];
int a[N], b[N], Lu, Lv, Lt;
int p[N], dep[N];
int diff = 0, buc[N];
int len = 0, cyc[N], in_cyc[N];
int A[N], B[N];
inline long long DD(long long &x, const long long y) {
return x > y ? x = y : 0;
}
inline int dmax(const int x, const int y) { return dep[x] < dep[y] ? y : x; }
inline void addedge(int u, int v) {
to[++E] = v, next[E] = first[u], first[u] = E;
to[++E] = u, next[E] = first[v], first[v] = E;
}
void dfs(int x) {
int i, y;
for (i = first[x]; i; i = next[i])
if ((y = to[i]) != p[x]) p[y] = x, dep[y] = dep[x] + 1, dfs(y);
}
int LCA(int x, int y) {
for (; x != y; dep[x] < dep[y] ? y = p[y] : (x = p[x]))
;
return x;
}
inline bool find_endpoint() {
int i;
for (i = 1; i < diff && p[buc[i]] == buc[i - 1]; ++i)
;
if (i == diff) return Lt = Lu = p[*buc], Lv = buc[diff - 1], true;
Lu = Lv = Lt = p[*buc];
for (i = 0; i < diff; ++i) {
if (p[buc[i]] == Lu)
Lu = buc[i];
else if (p[buc[i]] == Lv)
Lv = buc[i];
else
return false;
}
return true;
}
int main() {
int i, j, u, v, w, z, offset;
long long D;
scanf("%d", &n);
for (i = 1; i <= n; ++i) scanf("%d", a + i);
for (i = 1; i <= n; ++i) scanf("%d", b + i);
for (i = 1; i < n; ++i) scanf("%d%d", &u, &v), addedge(u, v);
dfs(root = std::find(b + 1, b + (n + 1), 0) - b);
u = v = std::find(a + 1, a + (n + 1), 0) - a;
for (i = 0; u != root; ++i) std::swap(a[u], a[p[u]]), u = p[u];
if (!memcmp(a + 1, b + 1, n << 2)) return printf("0 %d\n", i), 0;
for (i = 1; i <= n; ++i)
if (a[i] != b[i]) buc[diff++] = i;
std::sort(buc, buc + diff,
[](const int x, const int y) { return dep[x] < dep[y]; });
if (!find_endpoint()) return puts("-1"), 0;
if (Lu > Lv) std::swap(Lu, Lv);
cyc[len++] = Lt;
for (i = Lu; i != Lt; i = p[i]) cyc[len++] = i;
std::reverse(cyc + 1, cyc + len);
for (i = Lv; i != Lt; i = p[i]) cyc[len++] = i;
for (i = 0; i < len; ++i) in_cyc[cyc[i]] = i;
assert(len > 2);
w = LCA(v, Lt);
if (w == Lt) z = dmax(LCA(v, Lu), LCA(v, Lv));
if (w == Lt && w != z) {
if ((in_cyc[z] + 1) % len != in_cyc[p[z]])
for (std::reverse(cyc + 1, cyc + len), i = 0; i < len; ++i)
in_cyc[cyc[i]] = i;
assert((in_cyc[z] + 1) % len == in_cyc[p[z]]);
for (j = 0, i = Lt; i != root; i = p[i]) buc[j++] = i;
for (; j--;
std::swap(a[buc[j]], a[p[buc[j]]]), std::swap(b[buc[j]], b[p[buc[j]]]))
;
for (i = 0; i < len; ++i) A[i] = a[cyc[i]], B[i] = b[cyc[i]];
assert(!(*A || *B));
offset = std::find(A + 1, A + len, B[1]) - A;
if (offset >= len ||
memcmp(A + 1, B + (len - offset + 1), (offset - 1) << 2) ||
memcmp(A + offset, B + 1, (len - offset) << 2))
return puts("-1"), 0;
;
D = dep[v] +
std::min(len * (offset - 1ll),
(long long)len * (len - offset) - 2 * (dep[z] - dep[w]));
} else {
for (j = 0, i = Lt; i != root; i = p[i]) buc[j++] = i;
for (; j--;
std::swap(a[buc[j]], a[p[buc[j]]]), std::swap(b[buc[j]], b[p[buc[j]]]))
;
for (i = 0; i < len; ++i) A[i] = a[cyc[i]], B[i] = b[cyc[i]];
assert(!(*A || *B));
offset = std::find(A + 1, A + len, B[1]) - A;
if (offset >= len ||
memcmp(A + 1, B + (len - offset + 1), (offset - 1) << 2) ||
memcmp(A + offset, B + 1, (len - offset) << 2))
return puts("-1"), 0;
;
D = dep[v] + 2 * (dep[Lt] - dep[w]) +
(long long)len * std::min(offset - 1, len - offset);
}
printf("%d %d %lld\n", Lu, Lv, D);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
x *= f;
}
void FAIL() {
printf("-1");
exit(0);
}
const int N = 2e5 + 10;
int head[N], ecnt, n;
struct ed {
int to, next;
} e[N << 1];
void ad(int x, int y) {
e[++ecnt] = (ed){y, head[x]};
head[x] = ecnt;
e[++ecnt] = (ed){x, head[y]};
head[y] = ecnt;
}
int vis[N], dep[N], fa[N];
int ca[N], cb[N];
int S, T;
void dfs(int u, int last) {
dep[u] = dep[last] + 1, fa[u] = last;
for (int k = head[u]; k; k = e[k].next) {
int v = e[k].to;
if (v == last) continue;
dfs(v, u);
}
}
bool jud(int u) { return ca[u] == cb[u]; }
vector<int> cir;
int inc[N], id1;
int main() {
rd(n);
for (int i = 1; i <= n; ++i) {
rd(ca[i]);
if (!ca[i]) S = i;
}
for (int i = 1; i <= n; ++i) {
rd(cb[i]);
if (!cb[i]) T = i;
}
for (int i = 1, x, y; i < n; ++i) rd(x), rd(y), ad(x, y);
dfs(T, 0);
{
int u = S;
while (fa[u]) swap(ca[u], ca[fa[u]]), vis[u] = 1, u = fa[u];
vis[T] = 1;
}
int s1 = 0, s2 = 0, d, m, u, v;
{
for (int i = 1; i <= n; ++i)
if (!jud(i) && dep[i] > dep[s1]) s1 = i;
if (!s1) {
printf("0 %d\n", dep[S] - 1);
return 0;
}
for (u = s1; !jud(u); u = fa[u]) cir.push_back(u), inc[u] = 1;
for (int i = 1; i <= n; ++i)
if (!jud(i) && dep[i] > dep[s2] && !inc[i]) s2 = i;
id1 = cir.size();
if (s2) {
for (v = s2; !jud(v); v = fa[v]) {
if (inc[v]) FAIL();
cir.push_back(v), inc[v] = 1;
}
if (v != u) FAIL();
reverse(cir.begin() + id1, cir.end());
} else
v = s2 = u;
for (int i = 1; i <= n; ++i)
if (!jud(i) && !inc[i]) FAIL();
m = cir.size();
for (d = 1; d < m; ++d)
if (ca[cir[d]] == cb[cir[0]]) break;
for (int i = 0; i < m; ++i)
if (ca[cir[(i + d) % m]] != cb[cir[i]]) FAIL();
printf("%d %d ", min(s1, s2), max(s1, s2));
}
{
int tot = 0, flg_ons1 = 0;
for (int i = S; i; i = fa[i]) vis[i] = 1;
for (int i = 0; i < cir.size(); ++i) tot += vis[cir[i]];
for (int i = 0; i < id1 && !flg_ons1; ++i)
if (vis[cir[i]]) flg_ons1 = 1;
long long ans = dep[S] - 1;
if (tot) {
if (!flg_ons1) d = m - d;
ans +=
min(d * (long long)(m + 1), (m - d) * (long long)(m + 1) - 2 * tot);
} else {
ans += min(d, m - d) * (long long)(m + 1);
while (!vis[u]) ans += 2, u = fa[u];
}
printf("%lld", ans);
}
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
x *= f;
}
void FAIL() {
printf("-1");
exit(0);
}
const int N = 2e5 + 10;
int head[N], ecnt, n;
struct ed {
int to, next;
} e[N << 1];
void ad(int x, int y) {
e[++ecnt] = (ed){y, head[x]};
head[x] = ecnt;
e[++ecnt] = (ed){x, head[y]};
head[y] = ecnt;
}
int vis[N], dep[N], fa[N];
int ca[N], cb[N];
int S, T;
void dfs(int u, int last) {
dep[u] = dep[last] + 1, fa[u] = last;
for (int k = head[u]; k; k = e[k].next) {
int v = e[k].to;
if (v == last) continue;
dfs(v, u);
}
}
bool jud(int u) { return ca[u] == cb[u]; }
vector<int> cir;
int inc[N], id1;
int main() {
rd(n);
for (int i = 1; i <= n; ++i) {
rd(ca[i]);
if (!ca[i]) S = i;
}
for (int i = 1; i <= n; ++i) {
rd(cb[i]);
if (!cb[i]) T = i;
}
for (int i = 1, x, y; i < n; ++i) rd(x), rd(y), ad(x, y);
dfs(T, 0);
{
int u = S;
while (fa[u]) swap(ca[u], ca[fa[u]]), vis[u] = 1, u = fa[u];
vis[T] = 1;
}
int s1 = 0, s2 = 0, d, m, u, v;
{
for (int i = 1; i <= n; ++i)
if (!jud(i) && dep[i] > dep[s1]) s1 = i;
if (!s1) {
printf("0 %d\n", dep[S] - 1);
return 0;
}
for (u = s1; !jud(u); u = fa[u]) cir.push_back(u), inc[u] = 1;
for (int i = 1; i <= n; ++i)
if (!jud(i) && dep[i] > dep[s2] && !inc[i]) s2 = i;
id1 = cir.size();
if (s2) {
for (v = s2; !jud(v); v = fa[v]) {
if (inc[v]) FAIL();
cir.push_back(v), inc[v] = 1;
}
if (v != u) FAIL();
reverse(cir.begin() + id1, cir.end());
} else
v = s2 = u;
for (int i = 1; i <= n; ++i)
if (!jud(i) && !inc[i]) FAIL();
m = cir.size();
for (d = 1; d < m; ++d)
if (ca[cir[d]] == cb[cir[0]]) break;
for (int i = 0; i < m; ++i)
if (ca[cir[(i + d) % m]] != cb[cir[i]]) FAIL();
printf("%d %d ", min(s1, s2), max(s1, s2));
}
{
int tot = 0, flg_ons1 = 0;
for (int i = S; i; i = fa[i]) vis[i] = 1;
for (int i = 0; i < cir.size(); ++i) tot += vis[cir[i]];
for (int i = 0; i < id1 && !flg_ons1; ++i)
if (vis[cir[i]]) flg_ons1 = 1;
long long ans = dep[S] - 1;
if (tot) {
if (!flg_ons1) d = m - d;
ans +=
min(d * (long long)(m + 1), (m - d) * (long long)(m + 1) - 2 * tot);
} else {
ans += min(d, m - d) * (long long)(m + 1);
while (!vis[u]) ans += 2, u = fa[u];
}
printf("%lld", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline int Add(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline void Sub(int &x, int y) { x = Sub(x - y); }
inline int Mul(int x, int y) { return (int)((unsigned long long)x * y % mod); }
inline int Mul(int x, int y, int z) {
return (int)((unsigned long long)x * y % mod * z % mod);
}
int Pow(int x, int y) {
int z = 1;
for (; y; y >>= 1) {
if (y & 1) z = Mul(z, x);
x = Mul(x, x);
}
return z;
}
int Inv(int x) { return Pow(x, mod - 2); }
template <class T>
inline void Max(T &x, T y) {
if (y > x) x = y;
}
template <class T>
inline void Min(T &x, T y) {
if (y < x) x = y;
}
void Reverse(vector<int> &v) { reverse(v.begin(), v.end()); }
void Concat(vector<int> &a, vector<int> &b) {
copy(b.begin(), b.end(), inserter(a, a.end()));
}
const int maxn = 1e6;
int n, S, T, A, B;
vector<int> g[maxn];
int label[maxn], origin[maxn], target[maxn];
int par[maxn], dep[maxn];
int id[maxn], ord[maxn];
int spos[maxn], tpos[maxn];
void Die(void) {
puts("-1");
exit(0);
}
void DfsI(int u) {
for (int v : g[u]) {
if (v == par[u]) continue;
dep[v] = dep[u] + 1;
par[v] = u;
DfsI(v);
}
}
void InitTree(int r) {
par[r] = -1;
dep[r] = 0;
DfsI(r);
}
vector<int> GetPath(int s, int t) {
vector<int> L1, L2;
while (dep[s] > dep[t]) {
L1.push_back(s);
s = par[s];
}
while (dep[t] > dep[s]) {
L2.push_back(t);
t = par[t];
}
while (s != t) {
L1.push_back(s);
s = par[s];
L2.push_back(t);
t = par[t];
}
L1.push_back(s);
Reverse(L2);
Concat(L1, L2);
return L1;
}
int Dist(int s, int t) { return (int)GetPath(s, t).size() - 1; }
void DfsF(int u, vector<int> &L) {
L.push_back(u);
int cnt = 0;
for (int v : g[u]) {
if (v == par[u] || label[v] == target[v]) continue;
if (++cnt == 1) {
DfsF(v, L);
} else {
Die();
}
}
}
void FindOrDie(int u) {
vector<int> line1, line2;
int cnt = 0;
for (int v : g[u]) {
if (v == par[u] || label[v] == target[v]) continue;
switch (++cnt) {
case 1:
DfsF(v, line1);
break;
case 2:
DfsF(v, line2);
break;
default:
Die();
}
}
A = line1.back();
B = cnt == 2 ? line2.back() : u;
int tot1 = (int)(line1.size() + line2.size());
int tot2 = 0;
for (int i = 0; i < n; ++i) {
if (label[i] != target[i]) ++tot2;
}
if (tot1 != tot2) Die();
}
long long Solve(int low) {
vector<int> path = GetPath(A, B);
int clk = 0;
for (int x : path) {
if (x != low) {
id[x] = clk;
ord[clk++] = x;
}
}
for (int i = 0; i < n; ++i) {
if (label[i] != -1) {
spos[label[i]] = i;
}
if (target[i] != -1) {
tpos[target[i]] = i;
}
}
int c = id[tpos[label[ord[0]]]];
for (int i = 0; i < path.size(); ++i) {
int u = path[i];
if (u == low) continue;
int p = label[u];
if ((id[tpos[p]] - id[spos[p]] + clk) % clk != c) {
Die();
}
}
return Dist(S, A) + (long long)(c - 1) * (clk + 1) + 1 + Dist(B, T);
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", label + i);
if (--label[i] == -1) {
S = i;
}
}
for (int i = 0; i < n; ++i) {
scanf("%d", target + i);
if (--target[i] == -1) {
T = i;
}
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d%d", &u, &v);
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
InitTree(T);
memcpy((origin), (label), sizeof(label));
vector<int> path = GetPath(S, T);
for (int i = 0; i < (int)path.size() - 1; ++i) {
swap(label[path[i]], label[path[i + 1]]);
}
int low = -1;
for (int i = 0; i < n; ++i) {
if (label[i] != target[i] && (low == -1 || dep[i] < dep[low])) {
low = i;
}
}
if (low == -1) {
int dist = (int)path.size() - 1;
printf("0 %d\n", dist);
return 0;
}
low = par[low];
FindOrDie(low);
long long ans = Solve(low);
swap(A, B);
Min(ans, Solve(low));
if (A > B) swap(A, B);
printf("%d %d %lld\n", A + 1, B + 1, ans);
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline int Add(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline void Sub(int &x, int y) { x = Sub(x - y); }
inline int Mul(int x, int y) { return (int)((unsigned long long)x * y % mod); }
inline int Mul(int x, int y, int z) {
return (int)((unsigned long long)x * y % mod * z % mod);
}
int Pow(int x, int y) {
int z = 1;
for (; y; y >>= 1) {
if (y & 1) z = Mul(z, x);
x = Mul(x, x);
}
return z;
}
int Inv(int x) { return Pow(x, mod - 2); }
template <class T>
inline void Max(T &x, T y) {
if (y > x) x = y;
}
template <class T>
inline void Min(T &x, T y) {
if (y < x) x = y;
}
void Reverse(vector<int> &v) { reverse(v.begin(), v.end()); }
void Concat(vector<int> &a, vector<int> &b) {
copy(b.begin(), b.end(), inserter(a, a.end()));
}
const int maxn = 1e6;
int n, S, T, A, B;
vector<int> g[maxn];
int label[maxn], origin[maxn], target[maxn];
int par[maxn], dep[maxn];
int id[maxn], ord[maxn];
int spos[maxn], tpos[maxn];
void Die(void) {
puts("-1");
exit(0);
}
void DfsI(int u) {
for (int v : g[u]) {
if (v == par[u]) continue;
dep[v] = dep[u] + 1;
par[v] = u;
DfsI(v);
}
}
void InitTree(int r) {
par[r] = -1;
dep[r] = 0;
DfsI(r);
}
vector<int> GetPath(int s, int t) {
vector<int> L1, L2;
while (dep[s] > dep[t]) {
L1.push_back(s);
s = par[s];
}
while (dep[t] > dep[s]) {
L2.push_back(t);
t = par[t];
}
while (s != t) {
L1.push_back(s);
s = par[s];
L2.push_back(t);
t = par[t];
}
L1.push_back(s);
Reverse(L2);
Concat(L1, L2);
return L1;
}
int Dist(int s, int t) { return (int)GetPath(s, t).size() - 1; }
void DfsF(int u, vector<int> &L) {
L.push_back(u);
int cnt = 0;
for (int v : g[u]) {
if (v == par[u] || label[v] == target[v]) continue;
if (++cnt == 1) {
DfsF(v, L);
} else {
Die();
}
}
}
void FindOrDie(int u) {
vector<int> line1, line2;
int cnt = 0;
for (int v : g[u]) {
if (v == par[u] || label[v] == target[v]) continue;
switch (++cnt) {
case 1:
DfsF(v, line1);
break;
case 2:
DfsF(v, line2);
break;
default:
Die();
}
}
A = line1.back();
B = cnt == 2 ? line2.back() : u;
int tot1 = (int)(line1.size() + line2.size());
int tot2 = 0;
for (int i = 0; i < n; ++i) {
if (label[i] != target[i]) ++tot2;
}
if (tot1 != tot2) Die();
}
long long Solve(int low) {
vector<int> path = GetPath(A, B);
int clk = 0;
for (int x : path) {
if (x != low) {
id[x] = clk;
ord[clk++] = x;
}
}
for (int i = 0; i < n; ++i) {
if (label[i] != -1) {
spos[label[i]] = i;
}
if (target[i] != -1) {
tpos[target[i]] = i;
}
}
int c = id[tpos[label[ord[0]]]];
for (int i = 0; i < path.size(); ++i) {
int u = path[i];
if (u == low) continue;
int p = label[u];
if ((id[tpos[p]] - id[spos[p]] + clk) % clk != c) {
Die();
}
}
return Dist(S, A) + (long long)(c - 1) * (clk + 1) + 1 + Dist(B, T);
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", label + i);
if (--label[i] == -1) {
S = i;
}
}
for (int i = 0; i < n; ++i) {
scanf("%d", target + i);
if (--target[i] == -1) {
T = i;
}
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d%d", &u, &v);
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
InitTree(T);
memcpy((origin), (label), sizeof(label));
vector<int> path = GetPath(S, T);
for (int i = 0; i < (int)path.size() - 1; ++i) {
swap(label[path[i]], label[path[i + 1]]);
}
int low = -1;
for (int i = 0; i < n; ++i) {
if (label[i] != target[i] && (low == -1 || dep[i] < dep[low])) {
low = i;
}
}
if (low == -1) {
int dist = (int)path.size() - 1;
printf("0 %d\n", dist);
return 0;
}
low = par[low];
FindOrDie(low);
long long ans = Solve(low);
swap(A, B);
Min(ans, Solve(low));
if (A > B) swap(A, B);
printf("%d %d %lld\n", A + 1, B + 1, ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
namespace Dango {
const int MAXN = 200005;
vector<int> s, t;
int n, c[MAXN], p[MAXN], pos, root;
int head[MAXN], to[MAXN << 1], nxt[MAXN << 1];
int fa[MAXN], d[MAXN], mn;
int anc, tag[MAXN];
int start[2];
int st[MAXN], top;
long long res, ans;
bool vis[MAXN];
int read() {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) {
f |= (ch == '-');
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return f ? -x : x;
}
void add(int u, int v) {
static int cnt;
cnt++;
to[cnt] = v;
nxt[cnt] = head[u];
head[u] = cnt;
}
void dfs(int u, int f) {
fa[u] = f;
d[u] = d[f] + 1;
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (v == f) continue;
dfs(v, u);
}
}
int judge() {
int now = 0;
for (; now < t.size(); now++)
if (t[now] == s[0]) break;
for (int i = 0, j = now; i < s.size(); i++, j++)
if (s[i] != t[j]) return -1;
return now;
}
int work() {
n = read();
for (int i = 1; i <= n; i++) {
c[i] = read();
if (!c[i]) pos = i;
}
for (int i = 1; i <= n; i++) {
p[i] = read();
if (!p[i]) root = i;
}
for (int i = 1; i < n; i++) {
int u = read(), v = read();
add(u, v);
add(v, u);
}
dfs(root, 0);
for (int i = pos; i != root; i = fa[i]) swap(c[i], c[fa[i]]);
int flag = 0;
mn = n;
for (int i = 1; i <= n; i++)
if (c[i] != p[i]) {
flag = 1;
vis[i] = true;
if (d[i] < mn) {
mn = d[i];
anc = fa[i];
}
}
if (!flag) {
printf("0 %d", d[pos] - 1);
return 0;
}
for (int i = 1; i <= n; i++) {
if (!vis[i] || d[i] > mn) continue;
if (fa[i] != anc) {
printf("-1");
return 0;
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) continue;
int j = i;
for (; j != anc && !tag[fa[j]]; j = fa[j]) tag[fa[j]]++;
}
if (tag[anc] > 2) {
printf("-1");
return 0;
}
for (int i = 1; i <= n; i++)
if (vis[i] && tag[i] > 1) {
printf("-1");
return 0;
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (vis[i] && !tag[i]) start[cnt++] = i;
for (int i = start[0]; i != anc; i = fa[i]) {
s.push_back(c[i]);
t.push_back(p[i]);
}
if (cnt > 1)
for (int i = start[1]; i != anc; i = fa[i]) st[++top] = i;
else
start[1] = anc;
for (int i = st[top]; top; i = st[--top]) {
s.push_back(c[i]);
t.push_back(p[i]);
}
int tmp = t.size();
for (int i = 0; i < tmp; i++) t.push_back(t[i]);
long long step = 0;
if ((step = judge()) == -1) {
printf("-1");
return 0;
}
res = step * (s.size() + 1);
res += (d[anc] - 1) * 2 + d[pos] - 1;
for (int i = anc; i != root; i = fa[i]) vis[i] = true;
for (int i = start[1]; i != anc; i = fa[i]) vis[i] = false;
for (int i = pos; i; i = fa[i])
if (vis[i]) res -= 2;
ans = res;
step = s.size() - step;
res = step * (s.size() + 1);
res += (d[anc] - 1) * 2 + d[pos] - 1;
for (int i = start[1]; i != anc; i = fa[i]) vis[i] = true;
for (int i = start[0]; i != anc; i = fa[i]) vis[i] = false;
for (int i = pos; i; i = fa[i])
if (vis[i]) res -= 2;
ans = min(res, ans);
if (start[0] > start[1]) swap(start[0], start[1]);
printf("%d %d %lld", start[0], start[1], ans);
return 0;
}
} // namespace Dango
int main() { return Dango::work(); }
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace Dango {
const int MAXN = 200005;
vector<int> s, t;
int n, c[MAXN], p[MAXN], pos, root;
int head[MAXN], to[MAXN << 1], nxt[MAXN << 1];
int fa[MAXN], d[MAXN], mn;
int anc, tag[MAXN];
int start[2];
int st[MAXN], top;
long long res, ans;
bool vis[MAXN];
int read() {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) {
f |= (ch == '-');
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return f ? -x : x;
}
void add(int u, int v) {
static int cnt;
cnt++;
to[cnt] = v;
nxt[cnt] = head[u];
head[u] = cnt;
}
void dfs(int u, int f) {
fa[u] = f;
d[u] = d[f] + 1;
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (v == f) continue;
dfs(v, u);
}
}
int judge() {
int now = 0;
for (; now < t.size(); now++)
if (t[now] == s[0]) break;
for (int i = 0, j = now; i < s.size(); i++, j++)
if (s[i] != t[j]) return -1;
return now;
}
int work() {
n = read();
for (int i = 1; i <= n; i++) {
c[i] = read();
if (!c[i]) pos = i;
}
for (int i = 1; i <= n; i++) {
p[i] = read();
if (!p[i]) root = i;
}
for (int i = 1; i < n; i++) {
int u = read(), v = read();
add(u, v);
add(v, u);
}
dfs(root, 0);
for (int i = pos; i != root; i = fa[i]) swap(c[i], c[fa[i]]);
int flag = 0;
mn = n;
for (int i = 1; i <= n; i++)
if (c[i] != p[i]) {
flag = 1;
vis[i] = true;
if (d[i] < mn) {
mn = d[i];
anc = fa[i];
}
}
if (!flag) {
printf("0 %d", d[pos] - 1);
return 0;
}
for (int i = 1; i <= n; i++) {
if (!vis[i] || d[i] > mn) continue;
if (fa[i] != anc) {
printf("-1");
return 0;
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) continue;
int j = i;
for (; j != anc && !tag[fa[j]]; j = fa[j]) tag[fa[j]]++;
}
if (tag[anc] > 2) {
printf("-1");
return 0;
}
for (int i = 1; i <= n; i++)
if (vis[i] && tag[i] > 1) {
printf("-1");
return 0;
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (vis[i] && !tag[i]) start[cnt++] = i;
for (int i = start[0]; i != anc; i = fa[i]) {
s.push_back(c[i]);
t.push_back(p[i]);
}
if (cnt > 1)
for (int i = start[1]; i != anc; i = fa[i]) st[++top] = i;
else
start[1] = anc;
for (int i = st[top]; top; i = st[--top]) {
s.push_back(c[i]);
t.push_back(p[i]);
}
int tmp = t.size();
for (int i = 0; i < tmp; i++) t.push_back(t[i]);
long long step = 0;
if ((step = judge()) == -1) {
printf("-1");
return 0;
}
res = step * (s.size() + 1);
res += (d[anc] - 1) * 2 + d[pos] - 1;
for (int i = anc; i != root; i = fa[i]) vis[i] = true;
for (int i = start[1]; i != anc; i = fa[i]) vis[i] = false;
for (int i = pos; i; i = fa[i])
if (vis[i]) res -= 2;
ans = res;
step = s.size() - step;
res = step * (s.size() + 1);
res += (d[anc] - 1) * 2 + d[pos] - 1;
for (int i = start[1]; i != anc; i = fa[i]) vis[i] = true;
for (int i = start[0]; i != anc; i = fa[i]) vis[i] = false;
for (int i = pos; i; i = fa[i])
if (vis[i]) res -= 2;
ans = min(res, ans);
if (start[0] > start[1]) swap(start[0], start[1]);
printf("%d %d %lld", start[0], start[1], ans);
return 0;
}
} // namespace Dango
int main() { return Dango::work(); }
```
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 4e5 + 10;
long long n, Begin[maxn], Next[maxn], To[maxn], e, ans, x1, x2;
long long a[maxn], b[maxn], x, y, dis[maxn], anc[maxn], t, len;
bool use[maxn];
vector<vector<long long> > f;
vector<long long> A, B;
inline void add(long long u, long long v) {
To[++e] = v;
Next[e] = Begin[u];
Begin[u] = e;
}
inline void DFS(long long u, long long fa) {
for (long long i = Begin[u]; i; i = Next[i]) {
long long v = To[i];
if (v == fa) continue;
dis[v] = dis[u] + 1;
anc[v] = u;
DFS(v, u);
}
}
inline vector<long long> Get(long long x) {
vector<long long> p;
while (!use[x] && a[x] != b[x]) {
p.push_back(x);
x = anc[x];
}
return p;
}
inline long long LCA(long long x, long long y) {
if (dis[x] < dis[y]) swap(x, y);
while (dis[x] > dis[y]) x = anc[x];
if (x == y) return x;
while (anc[x] != anc[y]) {
x = anc[x];
y = anc[y];
}
return anc[x];
}
inline long long Dis(long long x, long long y) {
return dis[x] + dis[y] - 2 * dis[LCA(x, y)];
}
inline void DFS2(long long u, long long fa) {
for (long long i = Begin[u]; i; i = Next[i]) {
long long v = To[i];
if (v == fa) continue;
DFS2(v, u);
}
vector<long long> p = Get(u);
if (p.size()) {
f.push_back(p);
for (auto it : p) use[it] = true;
if (f.size() > 2) {
puts("-1");
exit(0);
}
}
}
signed main() {
scanf("%lld", &n);
for (long long i = (1), _end_ = (n); i <= _end_; ++i) {
scanf("%lld", &a[i]);
if (!a[i]) x = i;
}
for (long long i = (1), _end_ = (n); i <= _end_; ++i) {
scanf("%lld", &b[i]);
if (!b[i]) y = i;
}
for (long long i = (1), _end_ = (n - 1); i <= _end_; ++i) {
long long u, v, w;
scanf("%lld%lld", &u, &v);
add(u, v);
add(v, u);
}
dis[y] = 1;
DFS(y, 0);
long long u = x;
while (u != y) {
a[u] = a[anc[u]];
u = anc[u];
}
a[y] = 0;
DFS2(y, 0);
if (f.size() == 0) {
printf("0 %lld\n", dis[x] - dis[y]);
return 0;
}
if (f.size() == 1) {
vector<long long> p = f[0];
t = anc[p.back()];
x1 = p.front();
x2 = t;
len = dis[x1] - dis[t] + 1;
for (auto it : p) {
A.push_back(a[it]);
B.push_back(b[it]);
}
}
if (f.size() == 2) {
vector<long long> p = f[0];
t = anc[p.back()];
len = dis[p.front()] - dis[t];
x1 = p.front();
for (auto it : p) {
A.push_back(a[it]);
B.push_back(b[it]);
}
p = f[1];
if (anc[p.back()] != t) {
puts("-1");
return 0;
}
len += dis[p.front()] - dis[t] + 1;
x2 = p.front();
for (long long i = p.size() - 1; i >= 0; --i) {
A.push_back(a[p[i]]);
B.push_back(b[p[i]]);
}
}
long long ans = Dis(x, t) + Dis(t, y);
long long pos = -1;
for (long long i = 0; i < B.size(); ++i)
if (B[i] == A.front()) pos = i;
if (pos == -1) {
puts("-1");
return 0;
}
for (long long i = 0; i < A.size(); ++i)
if (A[i] != B[(i + pos) % B.size()]) {
puts("-1");
return 0;
}
long long sum1 = pos * len, sum2 = (len - pos - 1) * len;
if (f.size() == 2) {
vector<long long> p, q = f[0];
while (x) {
p.push_back(x);
x = anc[x];
}
while (!p.empty() && p.back() != t) p.pop_back();
if (!p.empty() && p.back() == t) p.pop_back();
vector<long long> g = p;
if (p.empty()) goto Next;
while (!p.empty() && !q.empty() && p.back() == q.back()) {
sum1 -= 2;
p.pop_back();
q.pop_back();
}
p = g;
q = f[1];
while (!p.empty() && !q.empty() && p.back() == q.back()) {
sum2 -= 2;
p.pop_back();
q.pop_back();
}
}
Next:;
ans += min(sum1, sum2);
if (x1 > x2) swap(x1, x2);
printf("%lld %lld %lld\n", x1, x2, ans);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 4e5 + 10;
long long n, Begin[maxn], Next[maxn], To[maxn], e, ans, x1, x2;
long long a[maxn], b[maxn], x, y, dis[maxn], anc[maxn], t, len;
bool use[maxn];
vector<vector<long long> > f;
vector<long long> A, B;
inline void add(long long u, long long v) {
To[++e] = v;
Next[e] = Begin[u];
Begin[u] = e;
}
inline void DFS(long long u, long long fa) {
for (long long i = Begin[u]; i; i = Next[i]) {
long long v = To[i];
if (v == fa) continue;
dis[v] = dis[u] + 1;
anc[v] = u;
DFS(v, u);
}
}
inline vector<long long> Get(long long x) {
vector<long long> p;
while (!use[x] && a[x] != b[x]) {
p.push_back(x);
x = anc[x];
}
return p;
}
inline long long LCA(long long x, long long y) {
if (dis[x] < dis[y]) swap(x, y);
while (dis[x] > dis[y]) x = anc[x];
if (x == y) return x;
while (anc[x] != anc[y]) {
x = anc[x];
y = anc[y];
}
return anc[x];
}
inline long long Dis(long long x, long long y) {
return dis[x] + dis[y] - 2 * dis[LCA(x, y)];
}
inline void DFS2(long long u, long long fa) {
for (long long i = Begin[u]; i; i = Next[i]) {
long long v = To[i];
if (v == fa) continue;
DFS2(v, u);
}
vector<long long> p = Get(u);
if (p.size()) {
f.push_back(p);
for (auto it : p) use[it] = true;
if (f.size() > 2) {
puts("-1");
exit(0);
}
}
}
signed main() {
scanf("%lld", &n);
for (long long i = (1), _end_ = (n); i <= _end_; ++i) {
scanf("%lld", &a[i]);
if (!a[i]) x = i;
}
for (long long i = (1), _end_ = (n); i <= _end_; ++i) {
scanf("%lld", &b[i]);
if (!b[i]) y = i;
}
for (long long i = (1), _end_ = (n - 1); i <= _end_; ++i) {
long long u, v, w;
scanf("%lld%lld", &u, &v);
add(u, v);
add(v, u);
}
dis[y] = 1;
DFS(y, 0);
long long u = x;
while (u != y) {
a[u] = a[anc[u]];
u = anc[u];
}
a[y] = 0;
DFS2(y, 0);
if (f.size() == 0) {
printf("0 %lld\n", dis[x] - dis[y]);
return 0;
}
if (f.size() == 1) {
vector<long long> p = f[0];
t = anc[p.back()];
x1 = p.front();
x2 = t;
len = dis[x1] - dis[t] + 1;
for (auto it : p) {
A.push_back(a[it]);
B.push_back(b[it]);
}
}
if (f.size() == 2) {
vector<long long> p = f[0];
t = anc[p.back()];
len = dis[p.front()] - dis[t];
x1 = p.front();
for (auto it : p) {
A.push_back(a[it]);
B.push_back(b[it]);
}
p = f[1];
if (anc[p.back()] != t) {
puts("-1");
return 0;
}
len += dis[p.front()] - dis[t] + 1;
x2 = p.front();
for (long long i = p.size() - 1; i >= 0; --i) {
A.push_back(a[p[i]]);
B.push_back(b[p[i]]);
}
}
long long ans = Dis(x, t) + Dis(t, y);
long long pos = -1;
for (long long i = 0; i < B.size(); ++i)
if (B[i] == A.front()) pos = i;
if (pos == -1) {
puts("-1");
return 0;
}
for (long long i = 0; i < A.size(); ++i)
if (A[i] != B[(i + pos) % B.size()]) {
puts("-1");
return 0;
}
long long sum1 = pos * len, sum2 = (len - pos - 1) * len;
if (f.size() == 2) {
vector<long long> p, q = f[0];
while (x) {
p.push_back(x);
x = anc[x];
}
while (!p.empty() && p.back() != t) p.pop_back();
if (!p.empty() && p.back() == t) p.pop_back();
vector<long long> g = p;
if (p.empty()) goto Next;
while (!p.empty() && !q.empty() && p.back() == q.back()) {
sum1 -= 2;
p.pop_back();
q.pop_back();
}
p = g;
q = f[1];
while (!p.empty() && !q.empty() && p.back() == q.back()) {
sum2 -= 2;
p.pop_back();
q.pop_back();
}
}
Next:;
ans += min(sum1, sum2);
if (x1 > x2) swap(x1, x2);
printf("%lld %lld %lld\n", x1, x2, ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int vis[201000], dep[201000], n, a[201000], b[201000], fa[201000], rta, rtb, O;
int cp, cq;
vector<int> G[201000], cir;
void dfs(int u, int f) {
fa[u] = f, dep[u] = dep[f] + 1;
for (auto v : G[u])
if (v != f) dfs(v, u);
}
void no() {
puts("-1");
exit(0);
}
int lca(int u, int v) {
while (u != v) dep[u] > dep[v] ? u = fa[u] : v = fa[v];
return u;
}
void findcir() {
int p = 0, q = 0;
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && dep[i] > dep[p]) p = i;
if (!p) return;
for (int i = p; i && a[i] != b[i]; i = fa[i]) cir.push_back(i), vis[i] = 1;
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && !vis[i] && dep[i] > dep[q]) q = i;
if (q) {
reverse(cir.begin(), cir.end());
for (int i = q; i && a[i] != b[i]; i = fa[i]) cir.push_back(i), vis[i] = 1;
} else
q = fa[cir.back()];
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && !vis[i]) no();
int lc = lca(p, q);
if (a[lc] != b[lc]) no();
if (cir.size() != dep[p] + dep[q] - 2 * dep[lc]) no();
O = lc, cp = p, cq = q;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
for (int i = 2, u, v; i <= n; ++i) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; ++i)
if (!a[i]) rta = i;
for (int i = 1; i <= n; ++i)
if (!b[i]) rtb = i;
dfs(rtb, 0);
for (int i = rta; i != rtb; i = fa[i]) swap(a[i], a[fa[i]]);
findcir();
if (!cir.size()) {
printf("0 %d\n", dep[rta] - 1);
return 0;
}
int mov = 0;
for (int i = 0; i < cir.size(); ++i)
if (a[cir[i]] == b[cir[0]]) mov = i;
for (int i = 0; i < cir.size(); ++i)
if (a[cir[(i + mov) % cir.size()]] != b[cir[i]]) no();
long long ans = dep[rta] - 1;
int len = cir.size();
memset(vis, 0, sizeof(vis));
for (int i = rta; i; i = fa[i]) vis[i] = 1;
if (vis[cir[0]] || vis[cir.back()]) {
int O = cir.size();
for (int i = 0; i < cir.size(); ++i)
if (!vis[cir[i]]) {
O = i;
break;
}
ans = ans - O +
min((long long)mov * (len + 1) - O,
abs((long long)(len - mov) * (len + 1) + O));
} else {
ans += (long long)min(len - mov, mov) * (len + 1);
}
for (; !vis[O]; O = fa[O]) ans += 2;
if (cp > cq) swap(cp, cq);
printf("%d %d %lld\n", cp, cq, ans);
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int vis[201000], dep[201000], n, a[201000], b[201000], fa[201000], rta, rtb, O;
int cp, cq;
vector<int> G[201000], cir;
void dfs(int u, int f) {
fa[u] = f, dep[u] = dep[f] + 1;
for (auto v : G[u])
if (v != f) dfs(v, u);
}
void no() {
puts("-1");
exit(0);
}
int lca(int u, int v) {
while (u != v) dep[u] > dep[v] ? u = fa[u] : v = fa[v];
return u;
}
void findcir() {
int p = 0, q = 0;
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && dep[i] > dep[p]) p = i;
if (!p) return;
for (int i = p; i && a[i] != b[i]; i = fa[i]) cir.push_back(i), vis[i] = 1;
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && !vis[i] && dep[i] > dep[q]) q = i;
if (q) {
reverse(cir.begin(), cir.end());
for (int i = q; i && a[i] != b[i]; i = fa[i]) cir.push_back(i), vis[i] = 1;
} else
q = fa[cir.back()];
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && !vis[i]) no();
int lc = lca(p, q);
if (a[lc] != b[lc]) no();
if (cir.size() != dep[p] + dep[q] - 2 * dep[lc]) no();
O = lc, cp = p, cq = q;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
for (int i = 2, u, v; i <= n; ++i) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; ++i)
if (!a[i]) rta = i;
for (int i = 1; i <= n; ++i)
if (!b[i]) rtb = i;
dfs(rtb, 0);
for (int i = rta; i != rtb; i = fa[i]) swap(a[i], a[fa[i]]);
findcir();
if (!cir.size()) {
printf("0 %d\n", dep[rta] - 1);
return 0;
}
int mov = 0;
for (int i = 0; i < cir.size(); ++i)
if (a[cir[i]] == b[cir[0]]) mov = i;
for (int i = 0; i < cir.size(); ++i)
if (a[cir[(i + mov) % cir.size()]] != b[cir[i]]) no();
long long ans = dep[rta] - 1;
int len = cir.size();
memset(vis, 0, sizeof(vis));
for (int i = rta; i; i = fa[i]) vis[i] = 1;
if (vis[cir[0]] || vis[cir.back()]) {
int O = cir.size();
for (int i = 0; i < cir.size(); ++i)
if (!vis[cir[i]]) {
O = i;
break;
}
ans = ans - O +
min((long long)mov * (len + 1) - O,
abs((long long)(len - mov) * (len + 1) + O));
} else {
ans += (long long)min(len - mov, mov) * (len + 1);
}
for (; !vis[O]; O = fa[O]) ans += 2;
if (cp > cq) swap(cp, cq);
printf("%d %d %lld\n", cp, cq, ans);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> sosedi[maxn];
int parent[maxn], shagi[maxn], start[maxn], finish[maxn], n;
bool visit_vertex[maxn];
void dfs(int v, int predok) {
parent[v] = predok;
shagi[v] = shagi[predok] + 1;
for (auto p : sosedi[v])
if (p != predok) dfs(p, v);
}
vector<int> circles;
int rt, st1, end1;
bool find_circle() {
circles.clear();
int p = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && shagi[i] > shagi[p]) p = i;
if (!p) return 1;
for (int u = p; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
int q = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i] && shagi[i] > shagi[q]) q = i;
if (q) {
reverse(circles.begin(), circles.end());
for (int u = q; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
} else
q = parent[circles.back()];
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i]) return 0;
int u, v;
for (u = p, v = q; u != v; u = parent[u])
if (shagi[u] < shagi[v]) swap(u, v);
rt = u;
if (circles.size() != shagi[p] + shagi[q] - 2 * shagi[rt]) return 0;
st1 = p, end1 = q;
return 1;
}
int main() {
cin >> n;
int pa, pb;
for (int i = 1; i <= n; i++) {
cin >> start[i];
if (start[i] == 0) pa = i;
}
for (int i = 1; i <= n; i++) {
cin >> finish[i];
if (finish[i] == 0) pb = i;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
sosedi[u].push_back(v), sosedi[v].push_back(u);
}
shagi[0] = -1;
dfs(pb, 0);
for (int u = pa; u != pb; u = parent[u]) swap(start[u], start[parent[u]]);
long long answer = shagi[pa];
if (!find_circle()) {
cout << -1 << endl;
return 0;
}
if (!circles.size()) {
cout << 0 << " " << answer << " " << endl;
return 0;
}
int gap = 0, len = circles.size();
for (int i = 0; i < len; i++)
if (finish[circles[i]] == start[circles[0]]) gap = i;
for (int i = 1; i < len; i++)
if (finish[circles[(i + gap) % len]] != start[circles[i]]) {
printf("-1\n");
return 0;
}
memset(visit_vertex, false, maxn);
for (int i = pa; i; i = parent[i]) visit_vertex[i] = 1;
if (visit_vertex[circles[0]] || visit_vertex[circles.back()]) {
int po = circles.size();
for (int i = 0; i < circles.size(); i++)
if (!visit_vertex[circles[i]]) {
po = i;
break;
}
answer = answer - po +
min(po + gap * 1ll * (len + 1),
abs((len - gap) * 1ll * (len + 1) - po));
} else
answer = (answer + min(gap, len - gap) * 1ll * (len + 1));
for (; !visit_vertex[rt]; rt = parent[rt]) answer += 2;
if (st1 > end1) swap(end1, st1);
cout << st1 << " " << end1 << " " << answer << " " << endl;
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> sosedi[maxn];
int parent[maxn], shagi[maxn], start[maxn], finish[maxn], n;
bool visit_vertex[maxn];
void dfs(int v, int predok) {
parent[v] = predok;
shagi[v] = shagi[predok] + 1;
for (auto p : sosedi[v])
if (p != predok) dfs(p, v);
}
vector<int> circles;
int rt, st1, end1;
bool find_circle() {
circles.clear();
int p = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && shagi[i] > shagi[p]) p = i;
if (!p) return 1;
for (int u = p; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
int q = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i] && shagi[i] > shagi[q]) q = i;
if (q) {
reverse(circles.begin(), circles.end());
for (int u = q; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
} else
q = parent[circles.back()];
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i]) return 0;
int u, v;
for (u = p, v = q; u != v; u = parent[u])
if (shagi[u] < shagi[v]) swap(u, v);
rt = u;
if (circles.size() != shagi[p] + shagi[q] - 2 * shagi[rt]) return 0;
st1 = p, end1 = q;
return 1;
}
int main() {
cin >> n;
int pa, pb;
for (int i = 1; i <= n; i++) {
cin >> start[i];
if (start[i] == 0) pa = i;
}
for (int i = 1; i <= n; i++) {
cin >> finish[i];
if (finish[i] == 0) pb = i;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
sosedi[u].push_back(v), sosedi[v].push_back(u);
}
shagi[0] = -1;
dfs(pb, 0);
for (int u = pa; u != pb; u = parent[u]) swap(start[u], start[parent[u]]);
long long answer = shagi[pa];
if (!find_circle()) {
cout << -1 << endl;
return 0;
}
if (!circles.size()) {
cout << 0 << " " << answer << " " << endl;
return 0;
}
int gap = 0, len = circles.size();
for (int i = 0; i < len; i++)
if (finish[circles[i]] == start[circles[0]]) gap = i;
for (int i = 1; i < len; i++)
if (finish[circles[(i + gap) % len]] != start[circles[i]]) {
printf("-1\n");
return 0;
}
memset(visit_vertex, false, maxn);
for (int i = pa; i; i = parent[i]) visit_vertex[i] = 1;
if (visit_vertex[circles[0]] || visit_vertex[circles.back()]) {
int po = circles.size();
for (int i = 0; i < circles.size(); i++)
if (!visit_vertex[circles[i]]) {
po = i;
break;
}
answer = answer - po +
min(po + gap * 1ll * (len + 1),
abs((len - gap) * 1ll * (len + 1) - po));
} else
answer = (answer + min(gap, len - gap) * 1ll * (len + 1));
for (; !visit_vertex[rt]; rt = parent[rt]) answer += 2;
if (st1 > end1) swap(end1, st1);
cout << st1 << " " << end1 << " " << answer << " " << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline void read(int &x) {
char c = nc(), b = 1;
for (; !(c >= '0' && c <= '9'); c = nc())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
;
x *= b;
}
const int N = 200005;
struct edge {
int u, v, next;
} G[N << 1];
int head[N], inum;
inline void add(int u, int v, int p) {
G[p].u = u;
G[p].v = v;
G[p].next = head[u];
head[u] = p;
}
int n;
int s, t;
int S[N], T[N], bS[N], bT[N];
int c[N];
int fat[N], depth[N];
int cir[N];
inline void dfs(int u, int fa) {
depth[u] = depth[fa] + 1;
fat[u] = fa;
for (int p = head[u]; p; p = G[p].next)
if (G[p].v != fa && !cir[G[p].v]) dfs(G[p].v, u);
}
int vst[N], deg[N];
int tot;
int a[N], b[N];
int lst[N], pnt;
int Ansu, Ansv;
int main() {
int iu, iv;
read(n);
for (int i = 1; i <= n; i++) {
read(S[i]);
if (!S[i]) s = i;
}
for (int i = 1; i <= n; i++) {
read(T[i]);
if (!T[i]) t = i;
}
for (int i = 1; i < n; i++)
read(iu), read(iv), add(iu, iv, ++inum), add(iv, iu, ++inum);
dfs(t, 0);
int p = s;
for (int i = 1; i <= n; i++) c[i] = S[i];
while (p != t) swap(c[p], c[fat[p]]), p = fat[p];
int flag = 0;
for (int i = 1; i <= n; i++) flag |= c[i] != T[i];
if (!flag) return printf("0 %d\n", depth[s] - 1), 0;
for (int i = 1; i <= n; i++) vst[i] = c[i] != T[i];
while (1) {
int k = 0;
for (int i = 1; i <= n; i++)
if (vst[i]) {
k = i;
break;
}
if (!k || tot > 2) break;
int t = k;
while (1) {
vst[t] = 0;
int tmp = t;
for (int p = head[t]; p; p = G[p].next)
if (vst[G[p].v]) {
t = G[p].v;
break;
}
if (tmp == t) break;
}
a[++tot] = t;
t = k;
while (1) {
vst[t] = 0;
int tmp = t;
for (int p = head[t]; p; p = G[p].next)
if (vst[G[p].v]) {
t = G[p].v;
break;
}
if (tmp == t) break;
}
b[tot] = t;
}
if (tot > 2) return printf("-1\n"), 0;
if (tot == 1) {
int &a = ::a[1], &b = ::b[1];
if (depth[a] > depth[b]) swap(a, b);
int t = b;
while (t && t != a) t = fat[t];
if (!t) return printf("-1\n"), 0;
add(fat[a], b, ++inum);
add(b, fat[a], ++inum);
if (fat[a] < b)
Ansu = fat[a], Ansv = b;
else
Ansu = b, Ansv = fat[a];
t = b;
while (t != fat[a]) cir[t] = 1, t = fat[t];
cir[fat[a]] = 1;
} else {
for (int i = 1; i <= 2; i++) {
int &a = ::a[i], &b = ::b[i];
if (depth[a] > depth[b]) swap(a, b);
int t = b;
while (t && t != a) t = fat[t];
if (!t) return printf("-1\n"), 0;
}
if (fat[a[1]] != fat[a[2]]) return printf("-1\n"), 0;
add(b[1], b[2], ++inum);
add(b[2], b[1], ++inum);
if (b[1] < b[2])
Ansu = b[1], Ansv = b[2];
else
Ansu = b[2], Ansv = b[1];
for (int i = 1; i <= 2; i++) {
int t = b[i];
while (t != fat[a[i]]) cir[t] = 1, t = fat[t];
cir[fat[a[i]]] = 1;
}
}
for (int i = 1; i <= n; i++)
if (cir[i]) dfs(i, 0);
long long ans = 0;
while (!cir[s]) swap(S[s], S[fat[s]]), s = fat[s], ans++;
while (!cir[t]) swap(T[t], T[fat[t]]), t = fat[t], ans++;
int _t = s;
pnt = -1;
while (1) {
cir[_t] = 0;
lst[++pnt] = _t;
int tmp = _t;
for (int p = head[_t]; p; p = G[p].next)
if (cir[G[p].v]) {
_t = G[p].v;
break;
}
if (tmp == _t) break;
}
long long ret = 1LL << 60;
int step = 0;
int pt = 0;
pnt++;
for (int i = 0; i < pnt; i++)
if (lst[i] == t) pt = i;
memcpy(bS, S, sizeof(S));
memcpy(bT, T, sizeof(T));
for (int i = pt; i; i--) swap(T[lst[i]], T[lst[i - 1]]), step++;
int pp = 1, qq = 0, gap;
for (int i = 1; i < pnt; i++)
if (T[lst[i]] == S[lst[1]]) qq = i;
gap = qq;
for (int i = 1; i < pnt; i++) {
if (S[lst[pp]] != T[lst[qq]]) return printf("-1\n", 0), 0;
pp = pp == pnt - 1 ? 1 : pp + 1;
qq = qq == pnt - 1 ? 1 : qq + 1;
}
ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
memcpy(S, bS, sizeof(S));
memcpy(T, bT, sizeof(T));
step = 0;
if (pt)
for (int i = pt; i < pnt; i++)
swap(T[lst[i]], T[lst[(i + 1) % pnt]]), step++;
pp = 1, qq = 0, gap;
for (int i = 1; i < pnt; i++)
if (T[lst[i]] == S[lst[1]]) qq = i;
gap = qq;
ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
printf("%d %d %I64d\n", Ansu, Ansv, ans + ret);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline void read(int &x) {
char c = nc(), b = 1;
for (; !(c >= '0' && c <= '9'); c = nc())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
;
x *= b;
}
const int N = 200005;
struct edge {
int u, v, next;
} G[N << 1];
int head[N], inum;
inline void add(int u, int v, int p) {
G[p].u = u;
G[p].v = v;
G[p].next = head[u];
head[u] = p;
}
int n;
int s, t;
int S[N], T[N], bS[N], bT[N];
int c[N];
int fat[N], depth[N];
int cir[N];
inline void dfs(int u, int fa) {
depth[u] = depth[fa] + 1;
fat[u] = fa;
for (int p = head[u]; p; p = G[p].next)
if (G[p].v != fa && !cir[G[p].v]) dfs(G[p].v, u);
}
int vst[N], deg[N];
int tot;
int a[N], b[N];
int lst[N], pnt;
int Ansu, Ansv;
int main() {
int iu, iv;
read(n);
for (int i = 1; i <= n; i++) {
read(S[i]);
if (!S[i]) s = i;
}
for (int i = 1; i <= n; i++) {
read(T[i]);
if (!T[i]) t = i;
}
for (int i = 1; i < n; i++)
read(iu), read(iv), add(iu, iv, ++inum), add(iv, iu, ++inum);
dfs(t, 0);
int p = s;
for (int i = 1; i <= n; i++) c[i] = S[i];
while (p != t) swap(c[p], c[fat[p]]), p = fat[p];
int flag = 0;
for (int i = 1; i <= n; i++) flag |= c[i] != T[i];
if (!flag) return printf("0 %d\n", depth[s] - 1), 0;
for (int i = 1; i <= n; i++) vst[i] = c[i] != T[i];
while (1) {
int k = 0;
for (int i = 1; i <= n; i++)
if (vst[i]) {
k = i;
break;
}
if (!k || tot > 2) break;
int t = k;
while (1) {
vst[t] = 0;
int tmp = t;
for (int p = head[t]; p; p = G[p].next)
if (vst[G[p].v]) {
t = G[p].v;
break;
}
if (tmp == t) break;
}
a[++tot] = t;
t = k;
while (1) {
vst[t] = 0;
int tmp = t;
for (int p = head[t]; p; p = G[p].next)
if (vst[G[p].v]) {
t = G[p].v;
break;
}
if (tmp == t) break;
}
b[tot] = t;
}
if (tot > 2) return printf("-1\n"), 0;
if (tot == 1) {
int &a = ::a[1], &b = ::b[1];
if (depth[a] > depth[b]) swap(a, b);
int t = b;
while (t && t != a) t = fat[t];
if (!t) return printf("-1\n"), 0;
add(fat[a], b, ++inum);
add(b, fat[a], ++inum);
if (fat[a] < b)
Ansu = fat[a], Ansv = b;
else
Ansu = b, Ansv = fat[a];
t = b;
while (t != fat[a]) cir[t] = 1, t = fat[t];
cir[fat[a]] = 1;
} else {
for (int i = 1; i <= 2; i++) {
int &a = ::a[i], &b = ::b[i];
if (depth[a] > depth[b]) swap(a, b);
int t = b;
while (t && t != a) t = fat[t];
if (!t) return printf("-1\n"), 0;
}
if (fat[a[1]] != fat[a[2]]) return printf("-1\n"), 0;
add(b[1], b[2], ++inum);
add(b[2], b[1], ++inum);
if (b[1] < b[2])
Ansu = b[1], Ansv = b[2];
else
Ansu = b[2], Ansv = b[1];
for (int i = 1; i <= 2; i++) {
int t = b[i];
while (t != fat[a[i]]) cir[t] = 1, t = fat[t];
cir[fat[a[i]]] = 1;
}
}
for (int i = 1; i <= n; i++)
if (cir[i]) dfs(i, 0);
long long ans = 0;
while (!cir[s]) swap(S[s], S[fat[s]]), s = fat[s], ans++;
while (!cir[t]) swap(T[t], T[fat[t]]), t = fat[t], ans++;
int _t = s;
pnt = -1;
while (1) {
cir[_t] = 0;
lst[++pnt] = _t;
int tmp = _t;
for (int p = head[_t]; p; p = G[p].next)
if (cir[G[p].v]) {
_t = G[p].v;
break;
}
if (tmp == _t) break;
}
long long ret = 1LL << 60;
int step = 0;
int pt = 0;
pnt++;
for (int i = 0; i < pnt; i++)
if (lst[i] == t) pt = i;
memcpy(bS, S, sizeof(S));
memcpy(bT, T, sizeof(T));
for (int i = pt; i; i--) swap(T[lst[i]], T[lst[i - 1]]), step++;
int pp = 1, qq = 0, gap;
for (int i = 1; i < pnt; i++)
if (T[lst[i]] == S[lst[1]]) qq = i;
gap = qq;
for (int i = 1; i < pnt; i++) {
if (S[lst[pp]] != T[lst[qq]]) return printf("-1\n", 0), 0;
pp = pp == pnt - 1 ? 1 : pp + 1;
qq = qq == pnt - 1 ? 1 : qq + 1;
}
ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
memcpy(S, bS, sizeof(S));
memcpy(T, bT, sizeof(T));
step = 0;
if (pt)
for (int i = pt; i < pnt; i++)
swap(T[lst[i]], T[lst[(i + 1) % pnt]]), step++;
pp = 1, qq = 0, gap;
for (int i = 1; i < pnt; i++)
if (T[lst[i]] == S[lst[1]]) qq = i;
gap = qq;
ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
printf("%d %d %I64d\n", Ansu, Ansv, ans + ret);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int w[N], ne[N], la[N];
int a[N], b[N], r, n, t, sw[N], fa[N], q, s1[N], dep[N], nx[N], s2[N];
void link(int x, int y) {
w[++t] = y;
ne[t] = la[x];
la[x] = t;
}
void dfs(int x) {
for (int y = la[x]; y; y = ne[y]) {
int z = w[y];
if (z == fa[x]) continue;
fa[z] = x;
dep[z] = dep[x] + 1;
dfs(z);
}
}
void go(int x) {
if (x == r) return;
if (fa[x] != r) go(fa[x]);
swap(a[fa[x]], a[x]), sw[x] = 1;
nx[fa[x]] = x;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), r = a[i] ? r : i;
for (int i = 1; i <= n; i++) scanf("%d", &b[i]), q = b[i] ? q : i;
int tag1 = 0;
if (n == 200000 && a[1] == 13809) {
tag1 = 0;
}
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
link(x, y);
link(y, x);
}
dfs(r);
long long ans = dep[q];
go(q);
sw[r] = 1;
int case0 = 0;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i] && sw[i]) {
case0 = 1;
}
}
if (case0) {
int cnt1 = 0, cnt2 = 0;
for (int x = 1; x <= n; x++) {
int cnt = 0;
if (a[x] != b[x]) {
for (int y = la[x]; y; y = ne[y]) cnt += a[w[y]] != b[w[y]];
if (cnt > 2) {
cout << -1 << endl;
return 0;
}
if (cnt == 1) cnt1++;
s1[x] = cnt;
s2[x] = cnt - (a[fa[x]] != b[fa[x]]);
if (cnt == 0) cnt1 += 2;
} else {
if (sw[x] && b[x]) {
for (int y = la[x]; y; y = ne[y]) cnt += a[w[y]] != b[w[y]];
if (cnt == 2 && a[fa[x]] != b[fa[x]]) {
cnt2++;
s1[x] = 100;
for (int y = la[x]; y; y = ne[y]) {
int z = w[y];
if (z != fa[x] && a[z] != b[z] && sw[z]) {
cnt2 = 100;
}
}
}
}
}
}
if (cnt1 == 2 && cnt2 == 0) {
int x, y, z, X, Y, l = 0;
for (int i = 1; i <= n; i++) {
if (s1[i] == 1) y = x, x = i;
if (a[i] != b[i] && (l == 0 || dep[i] < dep[l])) l = i;
}
if (sw[x] && sw[y]) {
if (dep[x] > dep[y]) swap(x, y);
X = x, z = Y = nx[y];
vector<int> a0;
while (y != x) a0.push_back(y), y = fa[y];
a0.push_back(x);
reverse(a0.begin(), a0.end());
int sum = 0, sum2 = 0;
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k,
ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
if (tag1) {
cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
}
if (tag1) {
cout << 1 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
if (sw[x] + sw[y] == 1) {
if (sw[x]) swap(x, y);
X = x, Y = nx[y];
vector<int> a1, a2, a0;
for (; x != l; x = fa[x]) a1.push_back(x);
a1.push_back(l);
for (; y != l; y = fa[y]) a2.push_back(y);
a0 = a1;
while (a2.size()) a0.push_back(a2.back()), a2.pop_back();
int sum = 0, sum2 = 0;
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k,
ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
if (tag1) {
cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
}
if (tag1) {
cout << 2 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
if (cnt1 == 4 && cnt2 == 1) {
int x, y, u, X, Y, l = 0;
for (int i = 1; i <= n; i++)
if (s1[i] == 100) u = i;
for (int i = 1; i <= n; i++) {
if ((s1[i] == 1 && !sw[i] || s1[i] == 0) && s2[i] == 0 && a[i] != b[i])
y = x, x = i;
if (a[i] != b[i] && (l == 0 || dep[i] < dep[l])) l = i;
}
if (fa[l] == u) {
cout << -1 << endl;
return 0;
}
static int tag0[N];
X = x, Y = y;
assert(sw[l]);
assert(a[l] != b[l]);
int x0 = x, y0 = y;
while (x0 != l && x0 != u) {
assert(a[x0] != b[x0]);
assert(sw[x0] == 0);
tag0[x0] = 1;
x0 = fa[x0];
}
while (y0 != l && y0 != u) {
assert(a[y0] != b[y0]);
assert(sw[y0] == 0);
tag0[y0] = 1;
y0 = fa[y0];
}
assert(x0 != y0);
int u0 = fa[u];
assert(sw[u] && a[u] == b[u]);
while (u0 != l) {
assert(sw[u0]);
assert(a[u0] != b[u0]);
tag0[u0] = 1;
u0 = fa[u0];
}
tag0[l] = 1;
for (int i = 1; i <= n; i++) {
if (tag0[i] != (a[i] != b[i])) {
cout << -1 << endl;
return 0;
}
assert(tag0[i] == (a[i] != b[i]));
}
vector<int> a1, a2, a0;
int tag = 0;
for (; x != l; x = fa[x]) a1.push_back(x), tag |= 1 * (x == u);
for (; y != l; y = fa[y]) a2.push_back(y), tag |= 2 * (y == u);
if (tag == 1) swap(a1, a2);
a0 = a1;
a0.push_back(l);
while (a2.size()) {
if (a2.back() != u) a0.push_back(a2.back());
a2.pop_back();
}
int sum = 0, sum2 = 0;
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k,
ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
if (tag1) {
cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
}
if (tag1) {
cout << l << ' ' << u << ' ' << dep[l] << ' ' << dep[u] << ' ' << dep[X]
<< ' ' << dep[Y] << ' ' << sw[u] << ' ' << nx[u] << endl;
cout << 3 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0;
for (int x = 1; x <= n; x++) {
int cnt = 0;
if (a[x] == b[x]) {
for (int y = la[x]; y; y = ne[y])
if (fa[x] != w[y]) cnt += a[w[y]] != b[w[y]];
if (cnt == 2) cnt1++;
if (cnt == 1) cnt3++;
if (cnt > 2) cnt4++;
} else {
cnt5++;
for (int y = la[x]; y; y = ne[y])
if (fa[x] != w[y]) cnt += a[w[y]] != b[w[y]];
if (cnt == 0) cnt2++;
if (cnt > 1) cnt4++;
}
s1[x] = cnt;
}
if (!cnt5) {
cout << 0 << ' ' << ans << endl;
return 0;
}
if (cnt4) {
cout << -1 << endl;
return 0;
}
if (cnt1 == 1 && cnt2 == 2) {
int x, y, u, v, X, Y, z;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i] && a[fa[i]] == b[fa[i]]) v = u, u = i;
if (a[i] != b[i] && s1[i] == 0) y = x, x = i;
}
X = x, Y = y;
z = fa[u];
assert(z == fa[v]);
if (sw[v]) swap(u, v);
vector<int> a1, a2, a0;
for (; x != u && x != v; x = fa[x]) a1.push_back(x);
a1.push_back(x);
for (; y != u && y != v; y = fa[y]) a2.push_back(y);
a2.push_back(y);
if (a2.back() == u) swap(a1, a2);
a0 = a1;
while (a2.size()) a0.push_back(a2.back()), a2.pop_back();
int sum = 0, sum2 = 0;
while (!sw[z] && z != r) sum2 += 2, z = fa[z];
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k - 2 * sum,
ans2 = (a0.size() + 1) * (a0.size() - k);
if (tag1) {
cout << 4 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
if (cnt3 == 1 && cnt2 == 1) {
int x, y, z, X, Y;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i] && a[fa[i]] == b[fa[i]]) x = i;
if (a[i] != b[i] && s1[i] == 0) y = i;
}
X = fa[x];
Y = y;
z = fa[x];
vector<int> a0;
while (y != x) a0.push_back(y), y = fa[y];
a0.push_back(x);
reverse(a0.begin(), a0.end());
int sum = 0, sum2 = 0;
while (!sw[z] && z != r) sum2 += 2, z = fa[z];
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k - 2 * sum,
ans2 = (a0.size() + 1) * (a0.size() - k);
if (tag1) {
cout << 5 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
cout << -1 << endl;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int w[N], ne[N], la[N];
int a[N], b[N], r, n, t, sw[N], fa[N], q, s1[N], dep[N], nx[N], s2[N];
void link(int x, int y) {
w[++t] = y;
ne[t] = la[x];
la[x] = t;
}
void dfs(int x) {
for (int y = la[x]; y; y = ne[y]) {
int z = w[y];
if (z == fa[x]) continue;
fa[z] = x;
dep[z] = dep[x] + 1;
dfs(z);
}
}
void go(int x) {
if (x == r) return;
if (fa[x] != r) go(fa[x]);
swap(a[fa[x]], a[x]), sw[x] = 1;
nx[fa[x]] = x;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), r = a[i] ? r : i;
for (int i = 1; i <= n; i++) scanf("%d", &b[i]), q = b[i] ? q : i;
int tag1 = 0;
if (n == 200000 && a[1] == 13809) {
tag1 = 0;
}
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
link(x, y);
link(y, x);
}
dfs(r);
long long ans = dep[q];
go(q);
sw[r] = 1;
int case0 = 0;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i] && sw[i]) {
case0 = 1;
}
}
if (case0) {
int cnt1 = 0, cnt2 = 0;
for (int x = 1; x <= n; x++) {
int cnt = 0;
if (a[x] != b[x]) {
for (int y = la[x]; y; y = ne[y]) cnt += a[w[y]] != b[w[y]];
if (cnt > 2) {
cout << -1 << endl;
return 0;
}
if (cnt == 1) cnt1++;
s1[x] = cnt;
s2[x] = cnt - (a[fa[x]] != b[fa[x]]);
if (cnt == 0) cnt1 += 2;
} else {
if (sw[x] && b[x]) {
for (int y = la[x]; y; y = ne[y]) cnt += a[w[y]] != b[w[y]];
if (cnt == 2 && a[fa[x]] != b[fa[x]]) {
cnt2++;
s1[x] = 100;
for (int y = la[x]; y; y = ne[y]) {
int z = w[y];
if (z != fa[x] && a[z] != b[z] && sw[z]) {
cnt2 = 100;
}
}
}
}
}
}
if (cnt1 == 2 && cnt2 == 0) {
int x, y, z, X, Y, l = 0;
for (int i = 1; i <= n; i++) {
if (s1[i] == 1) y = x, x = i;
if (a[i] != b[i] && (l == 0 || dep[i] < dep[l])) l = i;
}
if (sw[x] && sw[y]) {
if (dep[x] > dep[y]) swap(x, y);
X = x, z = Y = nx[y];
vector<int> a0;
while (y != x) a0.push_back(y), y = fa[y];
a0.push_back(x);
reverse(a0.begin(), a0.end());
int sum = 0, sum2 = 0;
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k,
ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
if (tag1) {
cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
}
if (tag1) {
cout << 1 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
if (sw[x] + sw[y] == 1) {
if (sw[x]) swap(x, y);
X = x, Y = nx[y];
vector<int> a1, a2, a0;
for (; x != l; x = fa[x]) a1.push_back(x);
a1.push_back(l);
for (; y != l; y = fa[y]) a2.push_back(y);
a0 = a1;
while (a2.size()) a0.push_back(a2.back()), a2.pop_back();
int sum = 0, sum2 = 0;
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k,
ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
if (tag1) {
cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
}
if (tag1) {
cout << 2 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
if (cnt1 == 4 && cnt2 == 1) {
int x, y, u, X, Y, l = 0;
for (int i = 1; i <= n; i++)
if (s1[i] == 100) u = i;
for (int i = 1; i <= n; i++) {
if ((s1[i] == 1 && !sw[i] || s1[i] == 0) && s2[i] == 0 && a[i] != b[i])
y = x, x = i;
if (a[i] != b[i] && (l == 0 || dep[i] < dep[l])) l = i;
}
if (fa[l] == u) {
cout << -1 << endl;
return 0;
}
static int tag0[N];
X = x, Y = y;
assert(sw[l]);
assert(a[l] != b[l]);
int x0 = x, y0 = y;
while (x0 != l && x0 != u) {
assert(a[x0] != b[x0]);
assert(sw[x0] == 0);
tag0[x0] = 1;
x0 = fa[x0];
}
while (y0 != l && y0 != u) {
assert(a[y0] != b[y0]);
assert(sw[y0] == 0);
tag0[y0] = 1;
y0 = fa[y0];
}
assert(x0 != y0);
int u0 = fa[u];
assert(sw[u] && a[u] == b[u]);
while (u0 != l) {
assert(sw[u0]);
assert(a[u0] != b[u0]);
tag0[u0] = 1;
u0 = fa[u0];
}
tag0[l] = 1;
for (int i = 1; i <= n; i++) {
if (tag0[i] != (a[i] != b[i])) {
cout << -1 << endl;
return 0;
}
assert(tag0[i] == (a[i] != b[i]));
}
vector<int> a1, a2, a0;
int tag = 0;
for (; x != l; x = fa[x]) a1.push_back(x), tag |= 1 * (x == u);
for (; y != l; y = fa[y]) a2.push_back(y), tag |= 2 * (y == u);
if (tag == 1) swap(a1, a2);
a0 = a1;
a0.push_back(l);
while (a2.size()) {
if (a2.back() != u) a0.push_back(a2.back());
a2.pop_back();
}
int sum = 0, sum2 = 0;
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k,
ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
if (tag1) {
cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
}
if (tag1) {
cout << l << ' ' << u << ' ' << dep[l] << ' ' << dep[u] << ' ' << dep[X]
<< ' ' << dep[Y] << ' ' << sw[u] << ' ' << nx[u] << endl;
cout << 3 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0;
for (int x = 1; x <= n; x++) {
int cnt = 0;
if (a[x] == b[x]) {
for (int y = la[x]; y; y = ne[y])
if (fa[x] != w[y]) cnt += a[w[y]] != b[w[y]];
if (cnt == 2) cnt1++;
if (cnt == 1) cnt3++;
if (cnt > 2) cnt4++;
} else {
cnt5++;
for (int y = la[x]; y; y = ne[y])
if (fa[x] != w[y]) cnt += a[w[y]] != b[w[y]];
if (cnt == 0) cnt2++;
if (cnt > 1) cnt4++;
}
s1[x] = cnt;
}
if (!cnt5) {
cout << 0 << ' ' << ans << endl;
return 0;
}
if (cnt4) {
cout << -1 << endl;
return 0;
}
if (cnt1 == 1 && cnt2 == 2) {
int x, y, u, v, X, Y, z;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i] && a[fa[i]] == b[fa[i]]) v = u, u = i;
if (a[i] != b[i] && s1[i] == 0) y = x, x = i;
}
X = x, Y = y;
z = fa[u];
assert(z == fa[v]);
if (sw[v]) swap(u, v);
vector<int> a1, a2, a0;
for (; x != u && x != v; x = fa[x]) a1.push_back(x);
a1.push_back(x);
for (; y != u && y != v; y = fa[y]) a2.push_back(y);
a2.push_back(y);
if (a2.back() == u) swap(a1, a2);
a0 = a1;
while (a2.size()) a0.push_back(a2.back()), a2.pop_back();
int sum = 0, sum2 = 0;
while (!sw[z] && z != r) sum2 += 2, z = fa[z];
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k - 2 * sum,
ans2 = (a0.size() + 1) * (a0.size() - k);
if (tag1) {
cout << 4 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
if (cnt3 == 1 && cnt2 == 1) {
int x, y, z, X, Y;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i] && a[fa[i]] == b[fa[i]]) x = i;
if (a[i] != b[i] && s1[i] == 0) y = i;
}
X = fa[x];
Y = y;
z = fa[x];
vector<int> a0;
while (y != x) a0.push_back(y), y = fa[y];
a0.push_back(x);
reverse(a0.begin(), a0.end());
int sum = 0, sum2 = 0;
while (!sw[z] && z != r) sum2 += 2, z = fa[z];
for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
int k = 0;
for (int i = 0; i < a0.size(); i++)
if (a[a0[i]] == b[a0[0]]) {
k = i;
break;
}
for (int i = 0; i < a0.size(); i++)
if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
cout << -1 << endl;
return 0;
}
long long ans1 = (a0.size() + 1) * k - 2 * sum,
ans2 = (a0.size() + 1) * (a0.size() - k);
if (tag1) {
cout << 5 << endl;
}
cout << min(X, Y) << ' ' << max(X, Y) << ' ';
cout << ans + sum2 + min(ans1, ans2) << endl;
return 0;
}
cout << -1 << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, a[N], b[N], fa[N], dep[N], pa, push_back;
vector<int> E[N], c;
bool vis[N];
void dfs(int u, int ff) {
fa[u] = ff, dep[u] = dep[ff] + 1;
for (auto v : E[u])
if (v ^ ff) dfs(v, u);
}
void No() {
cout << -1;
exit(0);
}
int main() {
n = gi();
for (int i = 1; i <= n; i++)
if (!(a[i] = gi())) pa = i;
for (int i = 1; i <= n; i++)
if (!(b[i] = gi())) push_back = i;
for (int i = 1, u, v; i < n; i++)
u = gi(), v = gi(), E[u].push_back(v), E[v].push_back(u);
dfs(push_back, 0);
long long ans = dep[pa] - 1;
int p = 0, q = 0, s = 0, t = 0;
for (int i = pa; i != push_back; i = fa[i]) swap(a[i], a[fa[i]]);
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && (!p || dep[p] < dep[i])) p = i;
if (!p) return cout << 0 << ' ' << ans, 0;
for (int i = p; a[i] != b[i]; i = fa[i])
vis[i] = 1, s = fa[i], c.push_back(i);
reverse(c.begin(), c.end());
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && (!q || dep[q] < dep[i]) && !vis[i]) q = i;
if (!q)
q = t = s;
else {
for (int i = q; a[i] != b[i]; i = fa[i])
vis[i] = 1, t = fa[i], c.push_back(i);
}
if (s != t || dep[p] + dep[q] - 2 * dep[s] != int(c.size())) No();
for (int i = 1; i <= n; i++)
if (!vis[i] && a[i] != b[i]) No();
int diff = 0;
for (int i = 0; i < int(c.size()); i++)
if (a[c[0]] == b[c[i]]) {
diff = i;
break;
}
for (int i = 0; i < int(c.size()); i++)
if (a[c[i]] != b[c[(i + diff) % int(c.size())]]) No();
memset(vis, 0, sizeof(vis));
for (int i = pa; i; i = fa[i]) vis[i] = 1;
if (!vis[c[0]]) reverse(c.begin(), c.end()), diff = int(c.size()) - diff;
int pp = 0;
if (vis[c[0]]) {
pp = int(c.size());
for (int i = 1; i < int(c.size()); i++)
if (!vis[c[i]]) {
pp = i;
break;
}
}
ans += min(1ll * diff * (int(c.size()) + 1),
1ll * (int(c.size()) - diff) * (int(c.size()) + 1) - 2 * pp);
for (int i = s; !vis[i]; i = fa[i]) ans += 2;
if (p > q) swap(p, q);
cout << p << ' ' << q << ' ' << ans;
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, a[N], b[N], fa[N], dep[N], pa, push_back;
vector<int> E[N], c;
bool vis[N];
void dfs(int u, int ff) {
fa[u] = ff, dep[u] = dep[ff] + 1;
for (auto v : E[u])
if (v ^ ff) dfs(v, u);
}
void No() {
cout << -1;
exit(0);
}
int main() {
n = gi();
for (int i = 1; i <= n; i++)
if (!(a[i] = gi())) pa = i;
for (int i = 1; i <= n; i++)
if (!(b[i] = gi())) push_back = i;
for (int i = 1, u, v; i < n; i++)
u = gi(), v = gi(), E[u].push_back(v), E[v].push_back(u);
dfs(push_back, 0);
long long ans = dep[pa] - 1;
int p = 0, q = 0, s = 0, t = 0;
for (int i = pa; i != push_back; i = fa[i]) swap(a[i], a[fa[i]]);
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && (!p || dep[p] < dep[i])) p = i;
if (!p) return cout << 0 << ' ' << ans, 0;
for (int i = p; a[i] != b[i]; i = fa[i])
vis[i] = 1, s = fa[i], c.push_back(i);
reverse(c.begin(), c.end());
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && (!q || dep[q] < dep[i]) && !vis[i]) q = i;
if (!q)
q = t = s;
else {
for (int i = q; a[i] != b[i]; i = fa[i])
vis[i] = 1, t = fa[i], c.push_back(i);
}
if (s != t || dep[p] + dep[q] - 2 * dep[s] != int(c.size())) No();
for (int i = 1; i <= n; i++)
if (!vis[i] && a[i] != b[i]) No();
int diff = 0;
for (int i = 0; i < int(c.size()); i++)
if (a[c[0]] == b[c[i]]) {
diff = i;
break;
}
for (int i = 0; i < int(c.size()); i++)
if (a[c[i]] != b[c[(i + diff) % int(c.size())]]) No();
memset(vis, 0, sizeof(vis));
for (int i = pa; i; i = fa[i]) vis[i] = 1;
if (!vis[c[0]]) reverse(c.begin(), c.end()), diff = int(c.size()) - diff;
int pp = 0;
if (vis[c[0]]) {
pp = int(c.size());
for (int i = 1; i < int(c.size()); i++)
if (!vis[c[i]]) {
pp = i;
break;
}
}
ans += min(1ll * diff * (int(c.size()) + 1),
1ll * (int(c.size()) - diff) * (int(c.size()) + 1) - 2 * pp);
for (int i = s; !vis[i]; i = fa[i]) ans += 2;
if (p > q) swap(p, q);
cout << p << ' ' << q << ' ' << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> sosedi[maxn];
int parent[maxn], shagi[maxn], start[maxn], finish[maxn], n;
bool visit_vertex[maxn];
void dfs(int v, int predok) {
parent[v] = predok;
shagi[v] = shagi[predok] + 1;
for (auto p : sosedi[v])
if (p != predok) dfs(p, v);
}
vector<int> circles;
int rt, lp, lq;
bool find_circle() {
circles.clear();
int p = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && shagi[i] > shagi[p]) p = i;
if (!p) return 1;
for (int u = p; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
int q = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i] && shagi[i] > shagi[q]) q = i;
if (q) {
reverse(circles.begin(), circles.end());
for (int u = q; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
} else
q = parent[circles.back()];
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i]) return 0;
int u, v;
for (u = p, v = q; u != v; u = parent[u])
if (shagi[u] < shagi[v]) swap(u, v);
rt = u;
if (circles.size() != shagi[p] + shagi[q] - 2 * shagi[rt]) return 0;
lp = p, lq = q;
return 1;
}
int main() {
cin >> n;
int pa, pb;
for (int i = 1; i <= n; i++) {
cin >> start[i];
if (start[i] == 0) pa = i;
}
for (int i = 1; i <= n; i++) {
cin >> finish[i];
if (finish[i] == 0) pb = i;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
sosedi[u].push_back(v), sosedi[v].push_back(u);
}
shagi[0] = -1;
dfs(pb, 0);
for (int u = pa; u != pb; u = parent[u]) swap(start[u], start[parent[u]]);
long long ans = shagi[pa];
if (!find_circle()) {
printf("-1\n");
return 0;
}
if (!circles.size()) {
printf("0 %d\n", ans);
return 0;
}
int gap = 0, len = circles.size();
for (int i = 0; i < len; i++)
if (finish[circles[i]] == start[circles[0]]) gap = i;
for (int i = 1; i < len; i++)
if (finish[circles[(i + gap) % len]] != start[circles[i]]) {
printf("-1\n");
return 0;
}
memset(visit_vertex, false, maxn);
for (int i = pa; i; i = parent[i]) visit_vertex[i] = 1;
if (visit_vertex[circles[0]] || visit_vertex[circles.back()]) {
int po = circles.size();
for (int i = 0; i < circles.size(); i++)
if (!visit_vertex[circles[i]]) {
po = i;
break;
}
ans = ans - po +
min(po + gap * 1ll * (len + 1),
abs((len - gap) * 1ll * (len + 1) - po));
} else
ans = (ans + min(gap, len - gap) * 1ll * (len + 1));
for (; !visit_vertex[rt]; rt = parent[rt]) ans += 2;
if (lp > lq) swap(lq, lp);
printf("%d %d %lld\n", lp, lq, ans);
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> sosedi[maxn];
int parent[maxn], shagi[maxn], start[maxn], finish[maxn], n;
bool visit_vertex[maxn];
void dfs(int v, int predok) {
parent[v] = predok;
shagi[v] = shagi[predok] + 1;
for (auto p : sosedi[v])
if (p != predok) dfs(p, v);
}
vector<int> circles;
int rt, lp, lq;
bool find_circle() {
circles.clear();
int p = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && shagi[i] > shagi[p]) p = i;
if (!p) return 1;
for (int u = p; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
int q = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i] && shagi[i] > shagi[q]) q = i;
if (q) {
reverse(circles.begin(), circles.end());
for (int u = q; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
} else
q = parent[circles.back()];
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i]) return 0;
int u, v;
for (u = p, v = q; u != v; u = parent[u])
if (shagi[u] < shagi[v]) swap(u, v);
rt = u;
if (circles.size() != shagi[p] + shagi[q] - 2 * shagi[rt]) return 0;
lp = p, lq = q;
return 1;
}
int main() {
cin >> n;
int pa, pb;
for (int i = 1; i <= n; i++) {
cin >> start[i];
if (start[i] == 0) pa = i;
}
for (int i = 1; i <= n; i++) {
cin >> finish[i];
if (finish[i] == 0) pb = i;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
sosedi[u].push_back(v), sosedi[v].push_back(u);
}
shagi[0] = -1;
dfs(pb, 0);
for (int u = pa; u != pb; u = parent[u]) swap(start[u], start[parent[u]]);
long long ans = shagi[pa];
if (!find_circle()) {
printf("-1\n");
return 0;
}
if (!circles.size()) {
printf("0 %d\n", ans);
return 0;
}
int gap = 0, len = circles.size();
for (int i = 0; i < len; i++)
if (finish[circles[i]] == start[circles[0]]) gap = i;
for (int i = 1; i < len; i++)
if (finish[circles[(i + gap) % len]] != start[circles[i]]) {
printf("-1\n");
return 0;
}
memset(visit_vertex, false, maxn);
for (int i = pa; i; i = parent[i]) visit_vertex[i] = 1;
if (visit_vertex[circles[0]] || visit_vertex[circles.back()]) {
int po = circles.size();
for (int i = 0; i < circles.size(); i++)
if (!visit_vertex[circles[i]]) {
po = i;
break;
}
ans = ans - po +
min(po + gap * 1ll * (len + 1),
abs((len - gap) * 1ll * (len + 1) - po));
} else
ans = (ans + min(gap, len - gap) * 1ll * (len + 1));
for (; !visit_vertex[rt]; rt = parent[rt]) ans += 2;
if (lp > lq) swap(lq, lp);
printf("%d %d %lld\n", lp, lq, ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 400005;
template <typename T>
void cmin(T &x, const T &y) {
if (y < x) x = y;
}
template <typename T>
void cmax(T &x, const T &y) {
if (y > x) x = y;
}
template <typename T>
void read(T &x) {
x = 0;
char c = getchar();
bool f = 0;
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') f = 1, c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
int fst[MX], nxt[MX], v[MX], lnum;
int ida[MX], idb[MX], on_path[MX];
int n;
void addeg(int nu, int nv) {
nxt[++lnum] = fst[nu];
fst[nu] = lnum;
v[lnum] = nv;
}
void input() {
read(n);
for (int i = 1; i <= n; i++) read(ida[i]);
for (int i = 1; i <= n; i++) read(idb[i]);
for (int i = 1; i < n; i++) {
int a, b;
read(a), read(b);
addeg(a, b);
addeg(b, a);
}
}
int dep[MX], fa[MX], usd_mov;
void dfs1(int x, int f, int tar) {
dep[x] = dep[f] + 1, fa[x] = f;
if (x == tar) {
int tmp = x;
on_path[x] = 1;
while (fa[tmp])
swap(ida[tmp], ida[fa[tmp]]), tmp = fa[tmp], on_path[tmp] = 1, usd_mov++;
}
for (int i = fst[x]; i; i = nxt[i]) {
int y = v[i];
if (v[i] != f) dfs1(v[i], x, tar);
}
}
void fuck_out(char *s) {
cerr << s << endl;
puts("-1");
exit(0);
}
int posa[MX], posb[MX], on_cyc[MX];
void work() {
int frm = 0, tar = 0;
for (int i = 1; i <= n; i++)
if (!ida[i]) frm = i;
for (int i = 1; i <= n; i++)
if (!idb[i]) tar = i;
dfs1(tar, 0, frm);
for (int i = 1; i <= n; i++) posa[ida[i]] = i, posb[idb[i]] = i;
for (int i = 1; i <= n; i++)
if (ida[i] != idb[i]) on_cyc[i] = 1;
int bot1 = 0, bot2 = 0;
static int vis[MX];
vector<int> cyc;
for (int i = 1; i <= n; i++)
if (on_cyc[i] && dep[i] > dep[bot1]) bot1 = i;
for (int x = bot1; on_cyc[x]; x = fa[x]) cyc.push_back(x), vis[x] = 1;
reverse(cyc.begin(), cyc.end());
if (!bot1) {
printf("%d %d\n", 0, usd_mov);
return;
}
for (int i = 1; i <= n; i++)
if (on_cyc[i] && dep[i] > dep[bot2] && !vis[i]) bot2 = i;
for (int x = bot2; on_cyc[x]; x = fa[x]) cyc.push_back(x), vis[x] = -1;
int top1 = *cyc.begin(), top2 = *cyc.rbegin(), entry = fa[top1];
if (bot2 && fa[top1] != fa[top2]) fuck_out("unconnected loop!");
for (int i = 1; i <= n; i++)
if (on_cyc[i] && !vis[i]) fuck_out("not a single loop!");
int step = 0, revs = 0;
for (int i = 0; i < cyc.size(); i++)
if (idb[cyc[i]] == ida[cyc[0]]) step = i;
for (int i = 0; i < cyc.size(); i++)
if (idb[cyc[(i + step) % cyc.size()]] != ida[cyc[i]])
fuck_out("can't rearrange loop!");
for (int x = frm; x; x = fa[x]) revs += vis[x];
long long way1 = usd_mov + 1ll * step * (cyc.size() + 1);
long long way2 = usd_mov + 1ll * (cyc.size() - step) * (cyc.size() + 1);
if (revs > 0)
way2 -= revs * 2;
else
way1 += revs * 2;
long long ans = min(way1, way2);
int adda = 0, addb = 0;
if (bot1 && bot2)
adda = bot1, addb = bot2;
else
adda = bot1, addb = entry;
for (int x = entry; !on_path[x]; x = fa[x]) ans += 2;
if (adda > addb) swap(adda, addb);
printf("%d %d %lld\n", adda, addb, ans);
}
int main() {
input();
work();
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = 400005;
template <typename T>
void cmin(T &x, const T &y) {
if (y < x) x = y;
}
template <typename T>
void cmax(T &x, const T &y) {
if (y > x) x = y;
}
template <typename T>
void read(T &x) {
x = 0;
char c = getchar();
bool f = 0;
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') f = 1, c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
int fst[MX], nxt[MX], v[MX], lnum;
int ida[MX], idb[MX], on_path[MX];
int n;
void addeg(int nu, int nv) {
nxt[++lnum] = fst[nu];
fst[nu] = lnum;
v[lnum] = nv;
}
void input() {
read(n);
for (int i = 1; i <= n; i++) read(ida[i]);
for (int i = 1; i <= n; i++) read(idb[i]);
for (int i = 1; i < n; i++) {
int a, b;
read(a), read(b);
addeg(a, b);
addeg(b, a);
}
}
int dep[MX], fa[MX], usd_mov;
void dfs1(int x, int f, int tar) {
dep[x] = dep[f] + 1, fa[x] = f;
if (x == tar) {
int tmp = x;
on_path[x] = 1;
while (fa[tmp])
swap(ida[tmp], ida[fa[tmp]]), tmp = fa[tmp], on_path[tmp] = 1, usd_mov++;
}
for (int i = fst[x]; i; i = nxt[i]) {
int y = v[i];
if (v[i] != f) dfs1(v[i], x, tar);
}
}
void fuck_out(char *s) {
cerr << s << endl;
puts("-1");
exit(0);
}
int posa[MX], posb[MX], on_cyc[MX];
void work() {
int frm = 0, tar = 0;
for (int i = 1; i <= n; i++)
if (!ida[i]) frm = i;
for (int i = 1; i <= n; i++)
if (!idb[i]) tar = i;
dfs1(tar, 0, frm);
for (int i = 1; i <= n; i++) posa[ida[i]] = i, posb[idb[i]] = i;
for (int i = 1; i <= n; i++)
if (ida[i] != idb[i]) on_cyc[i] = 1;
int bot1 = 0, bot2 = 0;
static int vis[MX];
vector<int> cyc;
for (int i = 1; i <= n; i++)
if (on_cyc[i] && dep[i] > dep[bot1]) bot1 = i;
for (int x = bot1; on_cyc[x]; x = fa[x]) cyc.push_back(x), vis[x] = 1;
reverse(cyc.begin(), cyc.end());
if (!bot1) {
printf("%d %d\n", 0, usd_mov);
return;
}
for (int i = 1; i <= n; i++)
if (on_cyc[i] && dep[i] > dep[bot2] && !vis[i]) bot2 = i;
for (int x = bot2; on_cyc[x]; x = fa[x]) cyc.push_back(x), vis[x] = -1;
int top1 = *cyc.begin(), top2 = *cyc.rbegin(), entry = fa[top1];
if (bot2 && fa[top1] != fa[top2]) fuck_out("unconnected loop!");
for (int i = 1; i <= n; i++)
if (on_cyc[i] && !vis[i]) fuck_out("not a single loop!");
int step = 0, revs = 0;
for (int i = 0; i < cyc.size(); i++)
if (idb[cyc[i]] == ida[cyc[0]]) step = i;
for (int i = 0; i < cyc.size(); i++)
if (idb[cyc[(i + step) % cyc.size()]] != ida[cyc[i]])
fuck_out("can't rearrange loop!");
for (int x = frm; x; x = fa[x]) revs += vis[x];
long long way1 = usd_mov + 1ll * step * (cyc.size() + 1);
long long way2 = usd_mov + 1ll * (cyc.size() - step) * (cyc.size() + 1);
if (revs > 0)
way2 -= revs * 2;
else
way1 += revs * 2;
long long ans = min(way1, way2);
int adda = 0, addb = 0;
if (bot1 && bot2)
adda = bot1, addb = bot2;
else
adda = bot1, addb = entry;
for (int x = entry; !on_path[x]; x = fa[x]) ans += 2;
if (adda > addb) swap(adda, addb);
printf("%d %d %lld\n", adda, addb, ans);
}
int main() {
input();
work();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 1;
vector<int> adj[N];
int a[N], b[N];
int depth[N], par[N];
void dfs_init(int u) {
for (auto c : adj[u]) {
if (c != par[u]) {
depth[c] = depth[u] + 1;
par[c] = u;
dfs_init(c);
}
}
}
vector<int> path;
bool in_path[N];
int low, high;
void dfs_path(int u, int p = 0) {
for (auto c : adj[u]) {
if (c != p && (c == low || a[c] != b[c])) {
dfs_path(c, u);
break;
}
}
path.push_back(u);
in_path[u] = true;
}
int dist(int u, int v) {
int d = 0;
while (u != v) {
if (depth[u] > depth[v]) {
u = par[u];
} else {
v = par[v];
}
++d;
}
return d;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
cin >> b[i];
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int s = find(a + 1, a + n + 1, 0) - a;
int t = find(b + 1, b + n + 1, 0) - b;
dfs_init(t);
for (int u = s; u != t; u = par[u]) {
swap(a[par[u]], a[u]);
}
for (int i = 1; i <= n; ++i) {
if (a[i] != b[i]) {
if (low == 0 || depth[i] < depth[low]) {
low = i;
}
if (high == 0 || depth[i] > depth[high]) {
high = i;
}
}
}
if (low == 0) {
cout << 0 << " " << depth[s] << "\n";
exit(0);
}
low = par[low];
dfs_path(high);
for (int i = 1; i <= n; ++i) {
if (a[i] != b[i] && !in_path[i]) {
cout << -1 << "\n";
exit(0);
}
}
if (!in_path[low]) {
cout << -1 << "\n";
exit(0);
}
int u = path[0], v = path.back(), m = path.size() - 1;
path.erase(find(path.begin(), path.end(), low));
int r = 0;
for (; a[path[0]] != b[path[r]]; ++r)
;
for (int i = 0; i < m; ++i) {
if (a[path[i]] != b[path[(i + r) % m]]) {
cout << -1 << "\n";
exit(0);
}
}
long long ans =
1 + min(dist(s, u) + (long long)(r - 1) * (m + 1) + dist(v, t),
dist(s, v) + (long long)(m - r - 1) * (m + 1) + dist(u, t));
if (u > v) {
swap(u, v);
}
cout << u << " " << v << " " << ans << "\n";
}
|
### Prompt
Generate a Cpp solution to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 1;
vector<int> adj[N];
int a[N], b[N];
int depth[N], par[N];
void dfs_init(int u) {
for (auto c : adj[u]) {
if (c != par[u]) {
depth[c] = depth[u] + 1;
par[c] = u;
dfs_init(c);
}
}
}
vector<int> path;
bool in_path[N];
int low, high;
void dfs_path(int u, int p = 0) {
for (auto c : adj[u]) {
if (c != p && (c == low || a[c] != b[c])) {
dfs_path(c, u);
break;
}
}
path.push_back(u);
in_path[u] = true;
}
int dist(int u, int v) {
int d = 0;
while (u != v) {
if (depth[u] > depth[v]) {
u = par[u];
} else {
v = par[v];
}
++d;
}
return d;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
cin >> b[i];
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int s = find(a + 1, a + n + 1, 0) - a;
int t = find(b + 1, b + n + 1, 0) - b;
dfs_init(t);
for (int u = s; u != t; u = par[u]) {
swap(a[par[u]], a[u]);
}
for (int i = 1; i <= n; ++i) {
if (a[i] != b[i]) {
if (low == 0 || depth[i] < depth[low]) {
low = i;
}
if (high == 0 || depth[i] > depth[high]) {
high = i;
}
}
}
if (low == 0) {
cout << 0 << " " << depth[s] << "\n";
exit(0);
}
low = par[low];
dfs_path(high);
for (int i = 1; i <= n; ++i) {
if (a[i] != b[i] && !in_path[i]) {
cout << -1 << "\n";
exit(0);
}
}
if (!in_path[low]) {
cout << -1 << "\n";
exit(0);
}
int u = path[0], v = path.back(), m = path.size() - 1;
path.erase(find(path.begin(), path.end(), low));
int r = 0;
for (; a[path[0]] != b[path[r]]; ++r)
;
for (int i = 0; i < m; ++i) {
if (a[path[i]] != b[path[(i + r) % m]]) {
cout << -1 << "\n";
exit(0);
}
}
long long ans =
1 + min(dist(s, u) + (long long)(r - 1) * (m + 1) + dist(v, t),
dist(s, v) + (long long)(m - r - 1) * (m + 1) + dist(u, t));
if (u > v) {
swap(u, v);
}
cout << u << " " << v << " " << ans << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
namespace TYC {
const int N = 2e5 + 5;
int n, A[N], B[N], Dep[N], Fa[N], Vis[N], Id[N];
vector<int> E[N], X, Y;
inline int read() {
int x = 0, f = 0, ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f ? -x : x;
}
void dfs(const int u, const int f) {
for (int v : E[u])
if (v != f) Dep[v] = Dep[Fa[v] = u] + 1, dfs(v, u);
}
inline pair<int, int> get_chain() {
for (int k = 1; k <= n; k++) {
int i = Id[k];
if (!Vis[i] && A[i] != B[i]) {
int p;
for (p = i; A[p] != B[p]; p = Fa[p]) Vis[p] = 1;
return pair<int, int>(p, i);
}
}
return pair<int, int>(-1, -1);
}
inline int dis(int u, int v) {
int ans = 0;
while (u != v) {
if (Dep[u] < Dep[v]) swap(u, v);
ans++;
u = Fa[u];
}
return ans;
}
inline long long calc(const int S, const int T, const int u, const int v) {
int k = int(X.size());
for (int i = 0; i < k; i++)
if (Y[i] == X[0]) {
for (int x = 0, y = i; x < k; x++, y = (y + 1) % k)
if (X[x] != Y[y]) {
puts("-1");
exit(0);
}
long long ans = (long long)(i - 1) * (k + 1) + dis(S, v) + dis(T, u) + 1;
ans = min(ans,
(long long)(k - i - 1) * (k + 1) + dis(S, u) + dis(T, v) + 1);
return ans;
}
return 0;
}
void work() {
n = read();
int rot = 0, start = 0;
for (int i = 1; i <= n; i++)
if (!(A[i] = read())) start = i;
for (int i = 1; i <= n; i++)
if (!(B[i] = read())) rot = i;
for (int i = 1; i < n; i++) {
int u = read(), v = read();
E[u].push_back(v);
E[v].push_back(u);
}
dfs(rot, 0);
for (int p = start; p != rot; p = Fa[p]) swap(A[p], A[Fa[p]]);
bool flag = true;
for (int i = 1; i <= n; i++) flag &= A[i] == B[i];
if (flag) return void(printf("0 %d\n", dis(start, rot)));
for (int i = 1; i <= n; i++) Id[i] = i;
sort(Id + 1, Id + 1 + n,
[](const int a, const int b) { return Dep[a] > Dep[b]; });
pair<int, int> L1 = get_chain(), L2 = get_chain(), edge;
if (L2.first == -1) {
for (int i = L1.second; i != L1.first; i = Fa[i]) {
X.push_back(A[i]);
Y.push_back(B[i]);
}
edge = L1;
} else {
if (L1.first != L2.first || get_chain().first != -1)
return void(puts("-1"));
for (int i = L1.second; i != L1.first; i = Fa[i]) {
X.push_back(A[i]);
Y.push_back(B[i]);
}
reverse(X.begin(), X.end());
reverse(Y.begin(), Y.end());
for (int i = L2.second; i != L2.first; i = Fa[i]) {
X.push_back(A[i]);
Y.push_back(B[i]);
}
edge = pair<int, int>(L1.second, L2.second);
}
long long ans = calc(start, rot, edge.first, edge.second);
if (edge.first > edge.second) swap(edge.first, edge.second);
printf("%d %d %lld\n", edge.first, edge.second, ans);
}
} // namespace TYC
int main() {
TYC::work();
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace TYC {
const int N = 2e5 + 5;
int n, A[N], B[N], Dep[N], Fa[N], Vis[N], Id[N];
vector<int> E[N], X, Y;
inline int read() {
int x = 0, f = 0, ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f ? -x : x;
}
void dfs(const int u, const int f) {
for (int v : E[u])
if (v != f) Dep[v] = Dep[Fa[v] = u] + 1, dfs(v, u);
}
inline pair<int, int> get_chain() {
for (int k = 1; k <= n; k++) {
int i = Id[k];
if (!Vis[i] && A[i] != B[i]) {
int p;
for (p = i; A[p] != B[p]; p = Fa[p]) Vis[p] = 1;
return pair<int, int>(p, i);
}
}
return pair<int, int>(-1, -1);
}
inline int dis(int u, int v) {
int ans = 0;
while (u != v) {
if (Dep[u] < Dep[v]) swap(u, v);
ans++;
u = Fa[u];
}
return ans;
}
inline long long calc(const int S, const int T, const int u, const int v) {
int k = int(X.size());
for (int i = 0; i < k; i++)
if (Y[i] == X[0]) {
for (int x = 0, y = i; x < k; x++, y = (y + 1) % k)
if (X[x] != Y[y]) {
puts("-1");
exit(0);
}
long long ans = (long long)(i - 1) * (k + 1) + dis(S, v) + dis(T, u) + 1;
ans = min(ans,
(long long)(k - i - 1) * (k + 1) + dis(S, u) + dis(T, v) + 1);
return ans;
}
return 0;
}
void work() {
n = read();
int rot = 0, start = 0;
for (int i = 1; i <= n; i++)
if (!(A[i] = read())) start = i;
for (int i = 1; i <= n; i++)
if (!(B[i] = read())) rot = i;
for (int i = 1; i < n; i++) {
int u = read(), v = read();
E[u].push_back(v);
E[v].push_back(u);
}
dfs(rot, 0);
for (int p = start; p != rot; p = Fa[p]) swap(A[p], A[Fa[p]]);
bool flag = true;
for (int i = 1; i <= n; i++) flag &= A[i] == B[i];
if (flag) return void(printf("0 %d\n", dis(start, rot)));
for (int i = 1; i <= n; i++) Id[i] = i;
sort(Id + 1, Id + 1 + n,
[](const int a, const int b) { return Dep[a] > Dep[b]; });
pair<int, int> L1 = get_chain(), L2 = get_chain(), edge;
if (L2.first == -1) {
for (int i = L1.second; i != L1.first; i = Fa[i]) {
X.push_back(A[i]);
Y.push_back(B[i]);
}
edge = L1;
} else {
if (L1.first != L2.first || get_chain().first != -1)
return void(puts("-1"));
for (int i = L1.second; i != L1.first; i = Fa[i]) {
X.push_back(A[i]);
Y.push_back(B[i]);
}
reverse(X.begin(), X.end());
reverse(Y.begin(), Y.end());
for (int i = L2.second; i != L2.first; i = Fa[i]) {
X.push_back(A[i]);
Y.push_back(B[i]);
}
edge = pair<int, int>(L1.second, L2.second);
}
long long ans = calc(start, rot, edge.first, edge.second);
if (edge.first > edge.second) swap(edge.first, edge.second);
printf("%d %d %lld\n", edge.first, edge.second, ans);
}
} // namespace TYC
int main() {
TYC::work();
return 0;
}
```
|
#include <bits/stdc++.h>
const int MN = 200005;
int N, su, tu, A[MN], B[MN];
std::vector<int> G[MN];
int stk[MN], top;
int faz[MN], dep[MN], sv[MN];
void DFS0(int u, int fz) {
dep[u] = dep[faz[u] = fz] + 1;
for (auto v : G[u])
if (v != fz) DFS0(v, u);
}
int DFSD(int u, int fz, int d, int t, int x) {
if (u != x) stk[++top] = u;
if (u == t) return d;
for (auto v : G[u])
if (v != fz) {
int ret = DFSD(v, u, d + 1, t, x);
if (ret != -1) return ret;
}
if (u != x) --top;
return -1;
}
int Dist(int u, int v, int x = 0) { return top = 0, DFSD(u, 0, 0, v, x); }
int Calc(int u, int v) { return Dist(su, u) + Dist(v, tu) + 1; }
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
for (int i = 1; i <= N; ++i) scanf("%d", &B[i]);
for (int i = 1; i <= N; ++i) {
if (A[i] == 0) su = i;
if (B[i] == 0) tu = i;
}
for (int i = 1, x, y; i < N; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
DFS0(tu, 0);
for (int x = su; x != tu; x = faz[x]) A[x] = A[faz[x]];
A[tu] = 0;
int cnt = 0, tp = 0, p = 0;
static int seq[MN];
for (int i = 1; i <= N; ++i)
if (A[i] != B[i]) {
++cnt, sv[faz[i]] = 1;
if (!p || dep[i] <= dep[p]) p = faz[i];
}
if (!cnt) return printf("0 %d\n", dep[su] - 1), 0;
for (int i = 1; i <= N; ++i)
if (A[i] != B[i] && !sv[i]) seq[++tp] = i;
if (tp > 2) return puts("-1"), 0;
if (tp == 1) seq[2] = p;
if (Dist(seq[1], seq[2], p) != cnt) return puts("-1"), 0;
static int iB[MN], shift;
for (int i = 1; i <= top; ++i) iB[B[stk[i]]] = i;
if (!iB[A[stk[1]]])
return puts("-1"), 0;
else
shift = iB[A[stk[1]]] - 1;
for (int i = 2; i <= top; ++i)
if (!iB[A[stk[i]]] || (iB[A[stk[i]]] - i + cnt) % cnt != shift)
return puts("-1"), 0;
int len = cnt + 1;
long long cycuv = Calc(seq[1], seq[2]) + (long long)(shift - 1) * len;
long long cycvu = Calc(seq[2], seq[1]) + (long long)(cnt - shift - 1) * len;
if (seq[1] > seq[2]) std::swap(seq[1], seq[2]);
printf("%d %d %lld\n", seq[1], seq[2], std::min(cycuv, cycvu));
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
const int MN = 200005;
int N, su, tu, A[MN], B[MN];
std::vector<int> G[MN];
int stk[MN], top;
int faz[MN], dep[MN], sv[MN];
void DFS0(int u, int fz) {
dep[u] = dep[faz[u] = fz] + 1;
for (auto v : G[u])
if (v != fz) DFS0(v, u);
}
int DFSD(int u, int fz, int d, int t, int x) {
if (u != x) stk[++top] = u;
if (u == t) return d;
for (auto v : G[u])
if (v != fz) {
int ret = DFSD(v, u, d + 1, t, x);
if (ret != -1) return ret;
}
if (u != x) --top;
return -1;
}
int Dist(int u, int v, int x = 0) { return top = 0, DFSD(u, 0, 0, v, x); }
int Calc(int u, int v) { return Dist(su, u) + Dist(v, tu) + 1; }
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
for (int i = 1; i <= N; ++i) scanf("%d", &B[i]);
for (int i = 1; i <= N; ++i) {
if (A[i] == 0) su = i;
if (B[i] == 0) tu = i;
}
for (int i = 1, x, y; i < N; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
DFS0(tu, 0);
for (int x = su; x != tu; x = faz[x]) A[x] = A[faz[x]];
A[tu] = 0;
int cnt = 0, tp = 0, p = 0;
static int seq[MN];
for (int i = 1; i <= N; ++i)
if (A[i] != B[i]) {
++cnt, sv[faz[i]] = 1;
if (!p || dep[i] <= dep[p]) p = faz[i];
}
if (!cnt) return printf("0 %d\n", dep[su] - 1), 0;
for (int i = 1; i <= N; ++i)
if (A[i] != B[i] && !sv[i]) seq[++tp] = i;
if (tp > 2) return puts("-1"), 0;
if (tp == 1) seq[2] = p;
if (Dist(seq[1], seq[2], p) != cnt) return puts("-1"), 0;
static int iB[MN], shift;
for (int i = 1; i <= top; ++i) iB[B[stk[i]]] = i;
if (!iB[A[stk[1]]])
return puts("-1"), 0;
else
shift = iB[A[stk[1]]] - 1;
for (int i = 2; i <= top; ++i)
if (!iB[A[stk[i]]] || (iB[A[stk[i]]] - i + cnt) % cnt != shift)
return puts("-1"), 0;
int len = cnt + 1;
long long cycuv = Calc(seq[1], seq[2]) + (long long)(shift - 1) * len;
long long cycvu = Calc(seq[2], seq[1]) + (long long)(cnt - shift - 1) * len;
if (seq[1] > seq[2]) std::swap(seq[1], seq[2]);
printf("%d %d %lld\n", seq[1], seq[2], std::min(cycuv, cycvu));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int read();
int n;
int a[200005], b[200005], c[200005], bk[200005];
int hd[200005], nx[400005], to[400005], cnt;
void add(int f, int t) { nx[++cnt] = hd[f], hd[f] = cnt, to[cnt] = t; }
int fa[200005], dep[200005];
void dfs1(int u) {
for (int i = hd[u], v; i; i = nx[i])
if ((v = to[i]) != fa[u]) dep[v] = dep[fa[v] = u] + 1, dfs1(v);
}
bool check1() {
for (int i = 1; i <= n; ++i) c[i] = b[i];
int u = a[1];
while (fa[u]) swap(c[u], c[fa[u]]), u = fa[u];
for (int i = 1; i <= n; ++i)
if (c[i] != i) return 0;
return 1;
}
vector<int> st[2];
int idcnt;
void fail() { puts("-1"), exit(0); }
void get(int u, int id) {
if (id >= 2 || (id == 1 && fa[st[0][0]] != fa[u])) fail();
st[id].push_back(u);
while (1) {
int v = 0;
for (int i = hd[u]; i; i = nx[i])
if (to[i] != fa[u] && c[to[i]] != to[i]) v ? fail() : (v = to[i], void());
if (!v) break;
st[id].push_back(u = v);
}
}
void dfs2(int u) {
for (int i = hd[u], v; i; i = nx[i])
if ((v = to[i]) != fa[u]) (c[v] != v) ? get(v, idcnt++) : dfs2(v);
}
int p[200005], q[200005], len;
int M(int x) { return x >= len ? x - len : x; }
int getdis(int u, int v) {
int rt = 0;
while (u != v) (dep[u] < dep[v]) ? swap(u, v) : void(), ++rt, u = fa[u];
return rt;
}
void work() {
for (int i = 1; i <= n; ++i) q[i] = -1;
for (int i = 0; i < st[0].size(); ++i)
p[len] = c[st[0][i]], q[st[0][i]] = len, ++len;
int ru = st[0].back(), rv = fa[st[0].front()], rw = rv;
if (st[1].size()) {
rv = st[1].back();
for (int i = st[1].size() - 1; i >= 0; --i)
p[len] = c[st[1][i]], q[st[1][i]] = len, ++len;
}
if ((ru = bk[ru]) > (rv = bk[rv])) swap(ru, rv);
long long dis = q[p[0]];
for (int i = 1; i < len; ++i)
if (M(q[p[i]] - i + len) != dis) fail();
for (int i = 1; i <= n; ++i)
if (c[i] != i && q[i] == -1) fail();
int t = a[1];
while (q[t] == -1 && t != 1) t = fa[t];
if (t == 1) {
dis = min(dis, len - dis) * (len + 1);
printf("%d %d %lld\n", ru, rv, dis + getdis(a[1], rw) + dep[rw]);
return;
}
dis = q[t] >= st[0].size()
? min((dis - 1) * (len + 1) + (q[t] + 1),
1ll * M(len - dis) * (len + 1) + (len - q[t]))
: min(dis * (len + 1) + (q[t] + 1),
(len - dis - 1) * (len + 1) + (len - q[t]));
printf("%d %d %lld\n", ru, rv, dis + dep[rw] + getdis(a[1], t));
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) bk[a[i] = read() + 1] = i;
for (int i = 1; i <= n; ++i) b[a[i]] = read() + 1;
for (int i = 1, u, v; i < n; ++i)
u = a[read()], v = a[read()], add(u, v), add(v, u);
for (int i = 1; i <= n; ++i) a[b[i]] = i;
dfs1(1);
if (check1()) return printf("0 %d\n", dep[a[1]]), 0;
dfs2(1), work();
return 0;
}
const int _SIZE = 1 << 22;
char ibuf[_SIZE], *iS = ibuf, *iT = ibuf;
int read() {
int x = 0, f = 1;
char c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)),
(iS == iT ? EOF : *iS++) : *iS++);
while (!isdigit(c))
f = (c == '-') ? -1 : f,
c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)),
(iS == iT ? EOF : *iS++) : *iS++);
while (isdigit(c))
x = x * 10 + c - '0',
c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)),
(iS == iT ? EOF : *iS++) : *iS++);
return x * f;
}
|
### Prompt
Create a solution in CPP for the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int read();
int n;
int a[200005], b[200005], c[200005], bk[200005];
int hd[200005], nx[400005], to[400005], cnt;
void add(int f, int t) { nx[++cnt] = hd[f], hd[f] = cnt, to[cnt] = t; }
int fa[200005], dep[200005];
void dfs1(int u) {
for (int i = hd[u], v; i; i = nx[i])
if ((v = to[i]) != fa[u]) dep[v] = dep[fa[v] = u] + 1, dfs1(v);
}
bool check1() {
for (int i = 1; i <= n; ++i) c[i] = b[i];
int u = a[1];
while (fa[u]) swap(c[u], c[fa[u]]), u = fa[u];
for (int i = 1; i <= n; ++i)
if (c[i] != i) return 0;
return 1;
}
vector<int> st[2];
int idcnt;
void fail() { puts("-1"), exit(0); }
void get(int u, int id) {
if (id >= 2 || (id == 1 && fa[st[0][0]] != fa[u])) fail();
st[id].push_back(u);
while (1) {
int v = 0;
for (int i = hd[u]; i; i = nx[i])
if (to[i] != fa[u] && c[to[i]] != to[i]) v ? fail() : (v = to[i], void());
if (!v) break;
st[id].push_back(u = v);
}
}
void dfs2(int u) {
for (int i = hd[u], v; i; i = nx[i])
if ((v = to[i]) != fa[u]) (c[v] != v) ? get(v, idcnt++) : dfs2(v);
}
int p[200005], q[200005], len;
int M(int x) { return x >= len ? x - len : x; }
int getdis(int u, int v) {
int rt = 0;
while (u != v) (dep[u] < dep[v]) ? swap(u, v) : void(), ++rt, u = fa[u];
return rt;
}
void work() {
for (int i = 1; i <= n; ++i) q[i] = -1;
for (int i = 0; i < st[0].size(); ++i)
p[len] = c[st[0][i]], q[st[0][i]] = len, ++len;
int ru = st[0].back(), rv = fa[st[0].front()], rw = rv;
if (st[1].size()) {
rv = st[1].back();
for (int i = st[1].size() - 1; i >= 0; --i)
p[len] = c[st[1][i]], q[st[1][i]] = len, ++len;
}
if ((ru = bk[ru]) > (rv = bk[rv])) swap(ru, rv);
long long dis = q[p[0]];
for (int i = 1; i < len; ++i)
if (M(q[p[i]] - i + len) != dis) fail();
for (int i = 1; i <= n; ++i)
if (c[i] != i && q[i] == -1) fail();
int t = a[1];
while (q[t] == -1 && t != 1) t = fa[t];
if (t == 1) {
dis = min(dis, len - dis) * (len + 1);
printf("%d %d %lld\n", ru, rv, dis + getdis(a[1], rw) + dep[rw]);
return;
}
dis = q[t] >= st[0].size()
? min((dis - 1) * (len + 1) + (q[t] + 1),
1ll * M(len - dis) * (len + 1) + (len - q[t]))
: min(dis * (len + 1) + (q[t] + 1),
(len - dis - 1) * (len + 1) + (len - q[t]));
printf("%d %d %lld\n", ru, rv, dis + dep[rw] + getdis(a[1], t));
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) bk[a[i] = read() + 1] = i;
for (int i = 1; i <= n; ++i) b[a[i]] = read() + 1;
for (int i = 1, u, v; i < n; ++i)
u = a[read()], v = a[read()], add(u, v), add(v, u);
for (int i = 1; i <= n; ++i) a[b[i]] = i;
dfs1(1);
if (check1()) return printf("0 %d\n", dep[a[1]]), 0;
dfs2(1), work();
return 0;
}
const int _SIZE = 1 << 22;
char ibuf[_SIZE], *iS = ibuf, *iT = ibuf;
int read() {
int x = 0, f = 1;
char c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)),
(iS == iT ? EOF : *iS++) : *iS++);
while (!isdigit(c))
f = (c == '-') ? -1 : f,
c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)),
(iS == iT ? EOF : *iS++) : *iS++);
while (isdigit(c))
x = x * 10 + c - '0',
c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)),
(iS == iT ? EOF : *iS++) : *iS++);
return x * f;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> o;
int addnum, vel[400010], ne[400010], head[200010];
int a[200010], b[200010];
int n, rt, ed;
int tot, posx, posy, pos;
inline int read() {
int ans = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9')
ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
return ans;
}
void add(int u, int v) {
addnum++, vel[addnum] = v, ne[addnum] = head[u], head[u] = addnum;
}
bool dfs(int u, int fa, int T) {
o.push_back(u);
if (u == T) return 1;
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
if (dfs(v, u, T)) return 1;
}
o.pop_back();
return 0;
}
void dfst(int u, int fa) {
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
dfst(v, u);
}
if (a[u] != b[u]) {
int cnt = 0;
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
if (a[v] != b[v]) cnt++;
}
if (!cnt) {
tot++;
if (posx)
posy = u;
else {
posx = u;
o.clear();
dfs(u, -1, ed);
for (int i = 0; i < (int)o.size(); i++)
if (a[o[i]] == b[o[i]]) {
posy = pos = o[i];
break;
}
}
}
} else {
int cnt = 0;
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
if (a[v] != b[v]) cnt++;
}
if (cnt == 2) tot--, pos = u;
}
}
int calc(int x, int y) {
o.clear();
dfs(x, -1, y);
return o.size() - 1;
}
int main() {
n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
if (!a[i]) rt = i;
}
for (int i = 1; i <= n; i++) {
b[i] = read();
if (!b[i]) ed = i;
}
for (int i = 1; i < n; i++) {
int x = read(), y = read();
add(x, y), add(y, x);
}
o.clear();
dfs(rt, -1, ed);
for (int i = 1; i < (int)o.size(); i++) swap(a[o[i - 1]], a[o[i]]);
int flag = 1;
for (int i = 1; i <= n; i++) flag &= a[i] == b[i];
if (flag) {
printf("0 %d\n", calc(rt, ed));
return 0;
}
dfst(ed, -1);
if (tot != 1) {
printf("-1\n");
return 0;
}
o.clear();
dfs(posx, -1, posy);
for (int i = 0; i < (int)o.size(); i++)
if (o[i] == pos) o.erase(o.begin() + i);
int num = 0;
long long step = o.size(), sizd = step;
for (int i = 0; i < sizd; i++)
if (b[o[i]] == a[o[0]]) num = i;
for (int i = 0; i < sizd; i++)
if (a[o[i]] != b[o[(i + num) % sizd]]) {
printf("-1\n");
return 0;
}
printf(
"%d %d %lld\n", min(posx, posy), max(posx, posy),
min((step + 1) * (num - 1) + 1 + calc(rt, posx) + calc(posy, ed),
(step + 1) * (step - num - 1) + 1 + calc(rt, posy) + calc(posx, ed)));
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> o;
int addnum, vel[400010], ne[400010], head[200010];
int a[200010], b[200010];
int n, rt, ed;
int tot, posx, posy, pos;
inline int read() {
int ans = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9')
ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
return ans;
}
void add(int u, int v) {
addnum++, vel[addnum] = v, ne[addnum] = head[u], head[u] = addnum;
}
bool dfs(int u, int fa, int T) {
o.push_back(u);
if (u == T) return 1;
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
if (dfs(v, u, T)) return 1;
}
o.pop_back();
return 0;
}
void dfst(int u, int fa) {
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
dfst(v, u);
}
if (a[u] != b[u]) {
int cnt = 0;
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
if (a[v] != b[v]) cnt++;
}
if (!cnt) {
tot++;
if (posx)
posy = u;
else {
posx = u;
o.clear();
dfs(u, -1, ed);
for (int i = 0; i < (int)o.size(); i++)
if (a[o[i]] == b[o[i]]) {
posy = pos = o[i];
break;
}
}
}
} else {
int cnt = 0;
for (int e = head[u]; e; e = ne[e]) {
int v = vel[e];
if (v == fa) continue;
if (a[v] != b[v]) cnt++;
}
if (cnt == 2) tot--, pos = u;
}
}
int calc(int x, int y) {
o.clear();
dfs(x, -1, y);
return o.size() - 1;
}
int main() {
n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
if (!a[i]) rt = i;
}
for (int i = 1; i <= n; i++) {
b[i] = read();
if (!b[i]) ed = i;
}
for (int i = 1; i < n; i++) {
int x = read(), y = read();
add(x, y), add(y, x);
}
o.clear();
dfs(rt, -1, ed);
for (int i = 1; i < (int)o.size(); i++) swap(a[o[i - 1]], a[o[i]]);
int flag = 1;
for (int i = 1; i <= n; i++) flag &= a[i] == b[i];
if (flag) {
printf("0 %d\n", calc(rt, ed));
return 0;
}
dfst(ed, -1);
if (tot != 1) {
printf("-1\n");
return 0;
}
o.clear();
dfs(posx, -1, posy);
for (int i = 0; i < (int)o.size(); i++)
if (o[i] == pos) o.erase(o.begin() + i);
int num = 0;
long long step = o.size(), sizd = step;
for (int i = 0; i < sizd; i++)
if (b[o[i]] == a[o[0]]) num = i;
for (int i = 0; i < sizd; i++)
if (a[o[i]] != b[o[(i + num) % sizd]]) {
printf("-1\n");
return 0;
}
printf(
"%d %d %lld\n", min(posx, posy), max(posx, posy),
min((step + 1) * (num - 1) + 1 + calc(rt, posx) + calc(posy, ed),
(step + 1) * (step - num - 1) + 1 + calc(rt, posy) + calc(posx, ed)));
}
```
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200005;
int n;
int a[N], b[N], c[N];
vector<int> adj[N];
int st, ed;
int par[N], inCycle[N];
vector<int> leaf;
int inVer, outVer;
ll ans;
void dfs1(int u, int p) {
for (int v : adj[u]) {
if (v != p) {
par[v] = u;
dfs1(v, u);
}
}
}
void dfs2(int u, int p) {
int cnt = 0;
for (int v : adj[u]) {
if (v != p) {
par[v] = u;
if (a[v] != b[v]) cnt++;
dfs2(v, u);
}
}
if (a[u] != b[u] && cnt == 0) leaf.push_back(u);
if (a[u] != b[u] && cnt > 1) {
for (int i = 1; i <= 3; i++) leaf.push_back(-1);
}
}
void dfs3(int u, int p, int h) {
if (inCycle[u] == 1) {
inVer = u;
ans += h;
return;
}
for (int v : adj[u]) {
if (v != p) {
par[v] = u;
dfs3(v, u, h + 1);
}
}
}
void dfs4(int u, int p, int h) {
if (inCycle[u] == 1) {
outVer = u;
ans += h;
return;
}
for (int v : adj[u]) {
if (v != p) {
dfs4(v, u, h + 1);
}
}
}
ll solve(vector<int> ver) {
int sz = (int)ver.size();
int now, color;
for (int i = 0; i < sz; i++) {
int nex = (i == sz - 1 ? 0 : i + 1);
if (ver[i] == outVer) {
now = nex;
color = a[ver[nex]];
break;
}
}
int tmp = 0;
while (b[ver[now]] != color) {
now = (now == sz - 1 ? 0 : now + 1);
tmp++;
}
return 1LL * min(tmp, sz - 1 - tmp) * sz;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 0) st = i;
c[i] = a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
if (b[i] == 0) ed = i;
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs1(st, st);
vector<int> path;
int now = ed;
while (1) {
path.push_back(now);
if (now == st) break;
now = par[now];
}
for (int i = (int)path.size() - 1; i >= 1; i--) {
swap(a[path[i]], a[path[i - 1]]);
}
int ok = 1;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i]) {
ok = 0;
break;
}
}
if (ok == 1) {
cout << 0 << " " << (int)path.size() - 1;
return 0;
}
dfs2(ed, ed);
if ((int)leaf.size() > 2) {
cout << -1;
return 0;
}
vector<int> cycle;
pair<int, int> edge;
if ((int)leaf.size() == 1) {
int u = leaf[0];
while (1) {
cycle.push_back(u);
inCycle[u] = 1;
if (a[u] == b[u]) break;
u = par[u];
}
edge = {leaf[0], u};
} else {
int u = leaf[0];
while (1) {
cycle.push_back(u);
inCycle[u] = 1;
if (a[u] == b[u]) break;
u = par[u];
}
reverse(cycle.begin(), cycle.end());
u = leaf[1];
while (a[u] != b[u]) {
cycle.push_back(u);
inCycle[u] = 1;
u = par[u];
}
edge = {leaf[0], leaf[1]};
}
dfs4(ed, ed, 0);
dfs3(st, st, 0);
for (int i = 1; i <= n; i++) a[i] = c[i];
now = inVer;
path.clear();
while (1) {
path.push_back(now);
if (now == st) break;
now = par[now];
}
for (int i = (int)path.size() - 1; i >= 1; i--) {
swap(a[path[i]], a[path[i - 1]]);
}
ll res = 1e18, sz = (int)cycle.size();
vector<int> have, need;
int ptr = 0;
for (int i = 0; i < sz; i++) {
int u = cycle[i];
if (u != inVer) have.push_back(a[u]);
if (u != outVer) need.push_back(b[u]);
}
ok = 1;
for (int i = 0; i < sz - 1; i++)
if (need[i] == have[0]) now = i;
for (int i = 0; i < sz - 1; i++) {
if (have[i] != need[now]) ok = 0;
now = (now == sz - 2 ? 0 : now + 1);
}
if (ok == 0) {
cout << -1;
return 0;
}
if (inVer == outVer) {
res = min(res, solve(cycle));
} else {
int ptr;
for (int i = 0; i < sz; i++)
if (cycle[i] == inVer) ptr = i;
int tmp = 0;
while (cycle[ptr] != outVer) {
int nex = (ptr == sz - 1 ? 0 : ptr + 1);
swap(a[cycle[ptr]], a[cycle[nex]]);
ptr = nex;
tmp++;
}
res = min(res, tmp + solve(cycle));
while (cycle[ptr] != inVer) {
int nex = (ptr == 0 ? sz - 1 : ptr - 1);
swap(a[cycle[ptr]], a[cycle[nex]]);
ptr = nex;
}
tmp = 0;
while (cycle[ptr] != outVer) {
int nex = (ptr == 0 ? sz - 1 : ptr - 1);
swap(a[cycle[ptr]], a[cycle[nex]]);
ptr = nex;
tmp++;
}
res = min(res, tmp + solve(cycle));
}
ans += res;
if (edge.first > edge.second) swap(edge.first, edge.second);
cout << edge.first << " " << edge.second << " " << ans;
}
|
### Prompt
Create a solution in cpp for the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200005;
int n;
int a[N], b[N], c[N];
vector<int> adj[N];
int st, ed;
int par[N], inCycle[N];
vector<int> leaf;
int inVer, outVer;
ll ans;
void dfs1(int u, int p) {
for (int v : adj[u]) {
if (v != p) {
par[v] = u;
dfs1(v, u);
}
}
}
void dfs2(int u, int p) {
int cnt = 0;
for (int v : adj[u]) {
if (v != p) {
par[v] = u;
if (a[v] != b[v]) cnt++;
dfs2(v, u);
}
}
if (a[u] != b[u] && cnt == 0) leaf.push_back(u);
if (a[u] != b[u] && cnt > 1) {
for (int i = 1; i <= 3; i++) leaf.push_back(-1);
}
}
void dfs3(int u, int p, int h) {
if (inCycle[u] == 1) {
inVer = u;
ans += h;
return;
}
for (int v : adj[u]) {
if (v != p) {
par[v] = u;
dfs3(v, u, h + 1);
}
}
}
void dfs4(int u, int p, int h) {
if (inCycle[u] == 1) {
outVer = u;
ans += h;
return;
}
for (int v : adj[u]) {
if (v != p) {
dfs4(v, u, h + 1);
}
}
}
ll solve(vector<int> ver) {
int sz = (int)ver.size();
int now, color;
for (int i = 0; i < sz; i++) {
int nex = (i == sz - 1 ? 0 : i + 1);
if (ver[i] == outVer) {
now = nex;
color = a[ver[nex]];
break;
}
}
int tmp = 0;
while (b[ver[now]] != color) {
now = (now == sz - 1 ? 0 : now + 1);
tmp++;
}
return 1LL * min(tmp, sz - 1 - tmp) * sz;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 0) st = i;
c[i] = a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
if (b[i] == 0) ed = i;
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs1(st, st);
vector<int> path;
int now = ed;
while (1) {
path.push_back(now);
if (now == st) break;
now = par[now];
}
for (int i = (int)path.size() - 1; i >= 1; i--) {
swap(a[path[i]], a[path[i - 1]]);
}
int ok = 1;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i]) {
ok = 0;
break;
}
}
if (ok == 1) {
cout << 0 << " " << (int)path.size() - 1;
return 0;
}
dfs2(ed, ed);
if ((int)leaf.size() > 2) {
cout << -1;
return 0;
}
vector<int> cycle;
pair<int, int> edge;
if ((int)leaf.size() == 1) {
int u = leaf[0];
while (1) {
cycle.push_back(u);
inCycle[u] = 1;
if (a[u] == b[u]) break;
u = par[u];
}
edge = {leaf[0], u};
} else {
int u = leaf[0];
while (1) {
cycle.push_back(u);
inCycle[u] = 1;
if (a[u] == b[u]) break;
u = par[u];
}
reverse(cycle.begin(), cycle.end());
u = leaf[1];
while (a[u] != b[u]) {
cycle.push_back(u);
inCycle[u] = 1;
u = par[u];
}
edge = {leaf[0], leaf[1]};
}
dfs4(ed, ed, 0);
dfs3(st, st, 0);
for (int i = 1; i <= n; i++) a[i] = c[i];
now = inVer;
path.clear();
while (1) {
path.push_back(now);
if (now == st) break;
now = par[now];
}
for (int i = (int)path.size() - 1; i >= 1; i--) {
swap(a[path[i]], a[path[i - 1]]);
}
ll res = 1e18, sz = (int)cycle.size();
vector<int> have, need;
int ptr = 0;
for (int i = 0; i < sz; i++) {
int u = cycle[i];
if (u != inVer) have.push_back(a[u]);
if (u != outVer) need.push_back(b[u]);
}
ok = 1;
for (int i = 0; i < sz - 1; i++)
if (need[i] == have[0]) now = i;
for (int i = 0; i < sz - 1; i++) {
if (have[i] != need[now]) ok = 0;
now = (now == sz - 2 ? 0 : now + 1);
}
if (ok == 0) {
cout << -1;
return 0;
}
if (inVer == outVer) {
res = min(res, solve(cycle));
} else {
int ptr;
for (int i = 0; i < sz; i++)
if (cycle[i] == inVer) ptr = i;
int tmp = 0;
while (cycle[ptr] != outVer) {
int nex = (ptr == sz - 1 ? 0 : ptr + 1);
swap(a[cycle[ptr]], a[cycle[nex]]);
ptr = nex;
tmp++;
}
res = min(res, tmp + solve(cycle));
while (cycle[ptr] != inVer) {
int nex = (ptr == 0 ? sz - 1 : ptr - 1);
swap(a[cycle[ptr]], a[cycle[nex]]);
ptr = nex;
}
tmp = 0;
while (cycle[ptr] != outVer) {
int nex = (ptr == 0 ? sz - 1 : ptr - 1);
swap(a[cycle[ptr]], a[cycle[nex]]);
ptr = nex;
tmp++;
}
res = min(res, tmp + solve(cycle));
}
ans += res;
if (edge.first > edge.second) swap(edge.first, edge.second);
cout << edge.first << " " << edge.second << " " << ans;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200010], circle;
int fa[200010], dep[200010], a[200010], b[200010], n, rt, vs, vt, h1, h2;
bool vis[200010], bo[200010];
inline int rd() {
int x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x;
}
inline void dfs(int x) {
for (int i = 0, siz = v[x].size(); i < siz; i++) {
int t = v[x][i];
if (t == fa[x]) continue;
dep[t] = dep[x] + 1;
fa[t] = x;
dfs(t);
}
}
inline bool gao() {
int p = 0, q = 0;
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && dep[i] > dep[p]) p = i;
if (!p) return true;
for (int hh = p; a[hh] != b[hh]; hh = fa[hh])
bo[hh] = true, circle.push_back(hh);
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && !bo[i] && dep[i] > dep[q]) q = i;
if (q) {
reverse(circle.begin(), circle.end());
for (int hh = q; a[hh] != b[hh]; hh = fa[hh])
bo[hh] = true, circle.push_back(hh);
} else
q = fa[circle.back()];
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && !bo[i]) return false;
int h1 = p, h2 = q;
for (; h1 != h2; h1 = fa[h1])
if (dep[h1] < dep[h2]) swap(h1, h2);
rt = h1;
if (circle.size() != dep[p] + dep[q] - dep[rt] * 2) return false;
vs = p;
vt = q;
return true;
}
int main() {
n = rd();
for (int i = 1; i <= n; i++)
if (!(a[i] = rd())) h1 = i;
for (int i = 1; i <= n; i++)
if (!(b[i] = rd())) h2 = i;
for (int i = 1; i < n; i++) {
int x = rd(), y = rd();
v[x].push_back(y);
v[y].push_back(x);
}
dep[0] = -1;
dfs(h2);
for (int hh = h1; hh != h2; hh = fa[hh]) swap(a[hh], a[fa[hh]]);
long long ans = dep[h1];
if (!gao()) {
puts("-1");
return 0;
}
if (!circle.size()) {
printf("0 %d\n", ans);
return 0;
}
int gap = 0, siz = circle.size();
for (int i = 0; i < siz; i++)
if (b[circle[i]] == a[circle[0]]) gap = i;
for (int i = 0; i < siz; i++)
if (b[circle[(i + gap) % siz]] != a[circle[i]]) {
puts("-1");
return 0;
}
for (int i = h1; i; i = fa[i]) vis[i] = true;
if (vis[circle[0]] || vis[circle.back()]) {
int now = siz;
for (int i = 0; i < siz; i++)
if (!vis[circle[i]]) {
now = i;
break;
}
ans = ans - now +
min((long long)now + (long long)gap * (siz + 1),
abs((long long)(siz - gap) * (siz + 1) - now));
} else
ans = (ans + (long long)min(gap, siz - gap) * (siz + 1));
for (; !vis[rt]; rt = fa[rt]) ans += 2;
if (vs > vt) swap(vs, vt);
printf("%d %d %I64d\n", vs, vt, ans);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200010], circle;
int fa[200010], dep[200010], a[200010], b[200010], n, rt, vs, vt, h1, h2;
bool vis[200010], bo[200010];
inline int rd() {
int x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x;
}
inline void dfs(int x) {
for (int i = 0, siz = v[x].size(); i < siz; i++) {
int t = v[x][i];
if (t == fa[x]) continue;
dep[t] = dep[x] + 1;
fa[t] = x;
dfs(t);
}
}
inline bool gao() {
int p = 0, q = 0;
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && dep[i] > dep[p]) p = i;
if (!p) return true;
for (int hh = p; a[hh] != b[hh]; hh = fa[hh])
bo[hh] = true, circle.push_back(hh);
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && !bo[i] && dep[i] > dep[q]) q = i;
if (q) {
reverse(circle.begin(), circle.end());
for (int hh = q; a[hh] != b[hh]; hh = fa[hh])
bo[hh] = true, circle.push_back(hh);
} else
q = fa[circle.back()];
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && !bo[i]) return false;
int h1 = p, h2 = q;
for (; h1 != h2; h1 = fa[h1])
if (dep[h1] < dep[h2]) swap(h1, h2);
rt = h1;
if (circle.size() != dep[p] + dep[q] - dep[rt] * 2) return false;
vs = p;
vt = q;
return true;
}
int main() {
n = rd();
for (int i = 1; i <= n; i++)
if (!(a[i] = rd())) h1 = i;
for (int i = 1; i <= n; i++)
if (!(b[i] = rd())) h2 = i;
for (int i = 1; i < n; i++) {
int x = rd(), y = rd();
v[x].push_back(y);
v[y].push_back(x);
}
dep[0] = -1;
dfs(h2);
for (int hh = h1; hh != h2; hh = fa[hh]) swap(a[hh], a[fa[hh]]);
long long ans = dep[h1];
if (!gao()) {
puts("-1");
return 0;
}
if (!circle.size()) {
printf("0 %d\n", ans);
return 0;
}
int gap = 0, siz = circle.size();
for (int i = 0; i < siz; i++)
if (b[circle[i]] == a[circle[0]]) gap = i;
for (int i = 0; i < siz; i++)
if (b[circle[(i + gap) % siz]] != a[circle[i]]) {
puts("-1");
return 0;
}
for (int i = h1; i; i = fa[i]) vis[i] = true;
if (vis[circle[0]] || vis[circle.back()]) {
int now = siz;
for (int i = 0; i < siz; i++)
if (!vis[circle[i]]) {
now = i;
break;
}
ans = ans - now +
min((long long)now + (long long)gap * (siz + 1),
abs((long long)(siz - gap) * (siz + 1) - now));
} else
ans = (ans + (long long)min(gap, siz - gap) * (siz + 1));
for (; !vis[rt]; rt = fa[rt]) ans += 2;
if (vs > vt) swap(vs, vt);
printf("%d %d %I64d\n", vs, vt, ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> sosedi[maxn];
int parent[maxn], shagi[maxn], start[maxn], finish[maxn], n;
bool visit_vertex[maxn];
void dfs(int v, int predok) {
parent[v] = predok;
shagi[v] = shagi[predok] + 1;
for (auto daleko1 : sosedi[v])
if (daleko1 != predok) dfs(daleko1, v);
}
vector<int> circles;
int rt, st1, end1;
bool find_circle() {
int daleko1 = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && shagi[i] > shagi[daleko1]) daleko1 = i;
if (!daleko1) return 1;
int a = daleko1;
for (int u = daleko1; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
int daleko2 = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i] && shagi[i] > shagi[daleko2])
daleko2 = i;
if (daleko2) {
reverse(circles.begin(), circles.end());
for (int u = daleko2; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
} else
daleko2 = parent[circles.back()];
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i]) return 0;
int u, v;
for (u = daleko1, v = daleko2; u != v; u = parent[u])
if (shagi[u] < shagi[v]) swap(u, v);
rt = u;
if (circles.size() != shagi[daleko1] + shagi[daleko2] - 2 * shagi[rt])
return 0;
st1 = daleko1, end1 = daleko2;
return 1;
}
int main() {
cin >> n;
int st_empty_ped, end_empty_ped;
for (int i = 1; i <= n; i++) {
cin >> start[i];
if (start[i] == 0) st_empty_ped = i;
}
for (int i = 1; i <= n; i++) {
cin >> finish[i];
if (finish[i] == 0) end_empty_ped = i;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
sosedi[u].push_back(v), sosedi[v].push_back(u);
}
shagi[0] = -1;
dfs(end_empty_ped, 0);
for (int u = st_empty_ped; u != end_empty_ped; u = parent[u])
swap(start[u], start[parent[u]]);
long long answer = shagi[st_empty_ped];
if (!find_circle()) {
cout << -1 << endl;
return 0;
}
if (!circles.size()) {
cout << 0 << " " << answer << " " << endl;
return 0;
}
int no_vis_vertex = 0, lenght = circles.size();
for (int i = 0; i < lenght; i++)
if (finish[circles[i]] == start[circles[0]]) no_vis_vertex = i;
memset(visit_vertex, false, maxn);
for (int i = st_empty_ped; i; i = parent[i]) visit_vertex[i] = 1;
if (visit_vertex[circles[0]] || visit_vertex[circles.back()]) {
int po = circles.size();
for (int i = 0; i < circles.size(); i++)
if (!visit_vertex[circles[i]]) {
po = i;
break;
}
answer = answer - po +
min(po + no_vis_vertex * 1ll * (lenght + 1),
abs((lenght - no_vis_vertex) * 1ll * (lenght + 1) - po));
} else
answer = (answer +
min(no_vis_vertex, lenght - no_vis_vertex) * 1ll * (lenght + 1));
for (; !visit_vertex[rt]; rt = parent[rt]) answer += 2;
if (st1 > end1) swap(end1, st1);
cout << st1 << " " << end1 << " " << answer << " " << endl;
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.
Input
The first line contains a single integer n (2 β€ n β€ 200 000) β the total number of islands.
The second line contains n space-separated integers ai (0 β€ ai β€ n - 1) β the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 β€ bi β€ n - 1) β the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 β€ ui, vi β€ n) β the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.
Output
Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.
Otherwise, print u, v, and t (1 β€ u < v β€ n) β the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing - 1.
Examples
Input
3
1 0 2
2 0 1
1 2
2 3
Output
1 3 3
Input
2
1 0
0 1
1 2
Output
0 1
Input
4
0 1 2 3
0 2 3 1
1 2
1 3
1 4
Output
-1
Note
In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> sosedi[maxn];
int parent[maxn], shagi[maxn], start[maxn], finish[maxn], n;
bool visit_vertex[maxn];
void dfs(int v, int predok) {
parent[v] = predok;
shagi[v] = shagi[predok] + 1;
for (auto daleko1 : sosedi[v])
if (daleko1 != predok) dfs(daleko1, v);
}
vector<int> circles;
int rt, st1, end1;
bool find_circle() {
int daleko1 = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && shagi[i] > shagi[daleko1]) daleko1 = i;
if (!daleko1) return 1;
int a = daleko1;
for (int u = daleko1; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
int daleko2 = 0;
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i] && shagi[i] > shagi[daleko2])
daleko2 = i;
if (daleko2) {
reverse(circles.begin(), circles.end());
for (int u = daleko2; visit_vertex[u] = 1, circles.push_back(u),
start[parent[u]] != finish[parent[u]];
u = parent[u])
;
} else
daleko2 = parent[circles.back()];
for (int i = 1; i <= n; i++)
if (start[i] != finish[i] && !visit_vertex[i]) return 0;
int u, v;
for (u = daleko1, v = daleko2; u != v; u = parent[u])
if (shagi[u] < shagi[v]) swap(u, v);
rt = u;
if (circles.size() != shagi[daleko1] + shagi[daleko2] - 2 * shagi[rt])
return 0;
st1 = daleko1, end1 = daleko2;
return 1;
}
int main() {
cin >> n;
int st_empty_ped, end_empty_ped;
for (int i = 1; i <= n; i++) {
cin >> start[i];
if (start[i] == 0) st_empty_ped = i;
}
for (int i = 1; i <= n; i++) {
cin >> finish[i];
if (finish[i] == 0) end_empty_ped = i;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
sosedi[u].push_back(v), sosedi[v].push_back(u);
}
shagi[0] = -1;
dfs(end_empty_ped, 0);
for (int u = st_empty_ped; u != end_empty_ped; u = parent[u])
swap(start[u], start[parent[u]]);
long long answer = shagi[st_empty_ped];
if (!find_circle()) {
cout << -1 << endl;
return 0;
}
if (!circles.size()) {
cout << 0 << " " << answer << " " << endl;
return 0;
}
int no_vis_vertex = 0, lenght = circles.size();
for (int i = 0; i < lenght; i++)
if (finish[circles[i]] == start[circles[0]]) no_vis_vertex = i;
memset(visit_vertex, false, maxn);
for (int i = st_empty_ped; i; i = parent[i]) visit_vertex[i] = 1;
if (visit_vertex[circles[0]] || visit_vertex[circles.back()]) {
int po = circles.size();
for (int i = 0; i < circles.size(); i++)
if (!visit_vertex[circles[i]]) {
po = i;
break;
}
answer = answer - po +
min(po + no_vis_vertex * 1ll * (lenght + 1),
abs((lenght - no_vis_vertex) * 1ll * (lenght + 1) - po));
} else
answer = (answer +
min(no_vis_vertex, lenght - no_vis_vertex) * 1ll * (lenght + 1));
for (; !visit_vertex[rt]; rt = parent[rt]) answer += 2;
if (st1 > end1) swap(end1, st1);
cout << st1 << " " << end1 << " " << answer << " " << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0;
return 0;
}
if (b >= a) {
cout << -1;
return 0;
}
int ans = 0;
while (1) {
ans++;
h1 -= 12 * b;
h1 += 12 * a;
if (h1 >= h2) {
cout << ans;
return 0;
}
}
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0;
return 0;
}
if (b >= a) {
cout << -1;
return 0;
}
int ans = 0;
while (1) {
ans++;
h1 -= 12 * b;
h1 += 12 * a;
if (h1 >= h2) {
cout << ans;
return 0;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void sample() {}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
sample();
long long int t = 1, n, m, q, k, x, y, p, w, i, j, count = 1, sum = 0, ans,
mod = 1000000007, b;
while (t--) {
long long int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h2 - h1 <= 8 * a) {
cout << 0 << endl;
} else if (b >= a) {
cout << -1 << endl;
} else {
x = h2 - h1 - 8 * a;
y = 12 * (a - b);
long double r = x / (long double)y;
n = ceil(r);
cout << n << endl;
}
}
}
|
### Prompt
Develop a solution in cpp to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void sample() {}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
sample();
long long int t = 1, n, m, q, k, x, y, p, w, i, j, count = 1, sum = 0, ans,
mod = 1000000007, b;
while (t--) {
long long int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h2 - h1 <= 8 * a) {
cout << 0 << endl;
} else if (b >= a) {
cout << -1 << endl;
} else {
x = h2 - h1 - 8 * a;
y = 12 * (a - b);
long double r = x / (long double)y;
n = ceil(r);
cout << n << endl;
}
}
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
using namespace std;
int main() {
int h1, h2, a, b;
scanf("%d %d", &h1, &h2);
scanf("%d %d", &a, &b);
if (a <= b) {
if (8 * a + h1 >= h2)
return 0 * puts("0");
else
return 0 * puts("-1");
} else {
for (int d = 1;; ++d) {
if (d == 1)
h1 += a * 8;
else
h1 += a * 12;
if (h1 >= h2) {
printf("%d\n", d - 1);
break;
}
h1 -= 12 * b;
}
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
using namespace std;
int main() {
int h1, h2, a, b;
scanf("%d %d", &h1, &h2);
scanf("%d %d", &a, &b);
if (a <= b) {
if (8 * a + h1 >= h2)
return 0 * puts("0");
else
return 0 * puts("-1");
} else {
for (int d = 1;; ++d) {
if (d == 1)
h1 += a * 8;
else
h1 += a * 12;
if (h1 >= h2) {
printf("%d\n", d - 1);
break;
}
h1 -= 12 * b;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
scanf("%d%d%d%d", &h1, &h2, &a, &b);
h1 += 8 * a;
if (h1 >= h2) {
printf("0\n");
return 0;
}
if (a <= b) {
printf("-1\n");
return 0;
}
printf("%d\n", (h2 - h1 + (a - b) * 12 - 1) / ((a - b) * 12));
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
scanf("%d%d%d%d", &h1, &h2, &a, &b);
h1 += 8 * a;
if (h1 >= h2) {
printf("0\n");
return 0;
}
if (a <= b) {
printf("-1\n");
return 0;
}
printf("%d\n", (h2 - h1 + (a - b) * 12 - 1) / ((a - b) * 12));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int N = 0;
int h1, h2, up, down;
int solve() {
int d = h2 - h1 - 8 * up;
if (d <= 0) return 0;
if (down >= up) return -1;
int inc = (up - down) * 12;
return (d + inc - 1) / inc;
}
int main() {
cin >> h1 >> h2 >> up >> down;
cout << solve();
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int N = 0;
int h1, h2, up, down;
int solve() {
int d = h2 - h1 - 8 * up;
if (d <= 0) return 0;
if (down >= up) return -1;
int inc = (up - down) * 12;
return (d + inc - 1) / inc;
}
int main() {
cin >> h1 >> h2 >> up >> down;
cout << solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
bool cmp(long long a, long long b) { return a > b; }
int main() {
int h1, h2, day, night;
cin >> h1 >> h2 >> day >> night;
if (h1 + day * 8 >= h2) {
cout << 0 << endl;
return 0;
}
h1 += day * 8;
h1 -= 12 * night;
if (night - day >= 0) {
cout << -1 << endl;
return 0;
}
int ct = 0;
while (h1 < h2) {
ct++;
h1 += 12 * day;
if (h1 >= h2) {
cout << ct << endl;
return 0;
}
h1 -= 12 * night;
}
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
bool cmp(long long a, long long b) { return a > b; }
int main() {
int h1, h2, day, night;
cin >> h1 >> h2 >> day >> night;
if (h1 + day * 8 >= h2) {
cout << 0 << endl;
return 0;
}
h1 += day * 8;
h1 -= 12 * night;
if (night - day >= 0) {
cout << -1 << endl;
return 0;
}
int ct = 0;
while (h1 < h2) {
ct++;
h1 += 12 * day;
if (h1 >= h2) {
cout << ct << endl;
return 0;
}
h1 -= 12 * night;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
while (cin >> a >> b >> c >> d) {
if (c <= d) {
if (a + 8 * c >= b)
printf("0\n");
else
printf("-1\n");
} else {
for (int i = 0;; i++) {
if (a >= b) {
printf("%d\n", i);
break;
}
a = a + 8 * c;
if (a >= b) {
printf("%d\n", i);
break;
}
a = a - 12 * d + 4 * c;
}
}
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
while (cin >> a >> b >> c >> d) {
if (c <= d) {
if (a + 8 * c >= b)
printf("0\n");
else
printf("-1\n");
} else {
for (int i = 0;; i++) {
if (a >= b) {
printf("%d\n", i);
break;
}
a = a + 8 * c;
if (a >= b) {
printf("%d\n", i);
break;
}
a = a - 12 * d + 4 * c;
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int a, b, x, y, ans, org;
long long t;
int main() {
cin >> a >> b;
cin >> x >> y;
t = b - a;
t -= 8 * x;
while (t > 0) {
t += 12 * y;
t -= 12 * x;
ans++;
if (ans == 1e5 + 100) {
cout << "-1";
return 0;
}
}
cout << ans << endl;
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a, b, x, y, ans, org;
long long t;
int main() {
cin >> a >> b;
cin >> x >> y;
t = b - a;
t -= 8 * x;
while (t > 0) {
t += 12 * y;
t -= 12 * x;
ans++;
if (ans == 1e5 + 100) {
cout << "-1";
return 0;
}
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
int abs(int n) { return n > 0 ? n : -n; }
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
using namespace std;
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int h1, h2;
int a, b;
cin >> h1 >> h2;
cin >> a >> b;
h1 += a * 8;
if (h1 < h2 && a <= b) {
cout << -1 << endl;
return 0;
}
int day = 0;
while (h1 < h2) {
h1 -= b * 12;
h1 += a * 4;
++day;
if (h1 >= h2) break;
h1 += a * 8;
}
cout << day << endl;
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
int abs(int n) { return n > 0 ? n : -n; }
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
using namespace std;
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int h1, h2;
int a, b;
cin >> h1 >> h2;
cin >> a >> b;
h1 += a * 8;
if (h1 < h2 && a <= b) {
cout << -1 << endl;
return 0;
}
int day = 0;
while (h1 < h2) {
h1 -= b * 12;
h1 += a * 4;
++day;
if (h1 >= h2) break;
h1 += a * 8;
}
cout << day << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
void solve() {
long long h1;
cin >> h1;
;
long long h2;
cin >> h2;
;
long long a;
cin >> a;
;
long long b;
cin >> b;
;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0 << endl;
return;
}
if (a <= b) {
cout << -1 << endl;
return;
}
long long d = (a - b) * 12;
long long ans = (h2 - h1 + d - 1) / (d);
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
void solve() {
long long h1;
cin >> h1;
;
long long h2;
cin >> h2;
;
long long a;
cin >> a;
;
long long b;
cin >> b;
;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0 << endl;
return;
}
if (a <= b) {
cout << -1 << endl;
return;
}
long long d = (a - b) * 12;
long long ans = (h2 - h1 + d - 1) / (d);
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
long long lH, lH1, lA, lB;
cin >> lH >> lH1 >> lA >> lB;
if ((lH + 8 * lA) >= lH1)
cout << 0 << endl;
else if (lA > lB) {
int iUp, iDown;
iUp = lH1 - lH - (8 * lA);
iDown = 12 * (lA - lB);
cout << (iUp + iDown - 1) / iDown << endl;
} else
cout << -1 << endl;
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
long long lH, lH1, lA, lB;
cin >> lH >> lH1 >> lA >> lB;
if ((lH + 8 * lA) >= lH1)
cout << 0 << endl;
else if (lA > lB) {
int iUp, iDown;
iUp = lH1 - lH - (8 * lA);
iDown = 12 * (lA - lB);
cout << (iUp + iDown - 1) / iDown << endl;
} else
cout << -1 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e5 + 3;
const long long mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
if (a <= b) {
if (h2 <= h1 + (8 * a)) {
cout << "0";
return 0;
} else
cout << "-1";
} else {
if (h2 <= h1 + (8 * a)) {
cout << "0";
return 0;
}
h1 = h1 + (8 * a) - (12 * b);
long long cnt = 0;
while (h1 < h2) {
cnt++;
h1 = h1 + (12 * a);
if (h1 >= h2) break;
h1 = h1 - (12 * b);
}
cout << cnt;
}
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e5 + 3;
const long long mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
if (a <= b) {
if (h2 <= h1 + (8 * a)) {
cout << "0";
return 0;
} else
cout << "-1";
} else {
if (h2 <= h1 + (8 * a)) {
cout << "0";
return 0;
}
h1 = h1 + (8 * a) - (12 * b);
long long cnt = 0;
while (h1 < h2) {
cnt++;
h1 = h1 + (12 * a);
if (h1 >= h2) break;
h1 = h1 - (12 * b);
}
cout << cnt;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long h1, h2, a, b, c, d, e = 1;
int k = 0;
cin >> h1 >> h2 >> a >> b;
c = h2 - h1;
d = a * 8;
if (d >= c)
cout << 0;
else if ((d < c) && (b >= a))
cout << "-1";
else {
while (1) {
d = d - b * 12;
d = d + a * 12;
if (d >= c) {
cout << e;
break;
}
e++;
}
}
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long h1, h2, a, b, c, d, e = 1;
int k = 0;
cin >> h1 >> h2 >> a >> b;
c = h2 - h1;
d = a * 8;
if (d >= c)
cout << 0;
else if ((d < c) && (b >= a))
cout << "-1";
else {
while (1) {
d = d - b * 12;
d = d + a * 12;
if (d >= c) {
cout << e;
break;
}
e++;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename X>
inline X abs(const X& a) {
return a < 0 ? -a : a;
}
template <typename X>
inline X sqr(const X& a) {
return a * a;
}
int main() {
int h1, h2;
cin >> h1 >> h2;
int a, b;
cin >> a >> b;
if (a <= b && (8 * a + h1) < h2) {
cout << -1 << endl;
return 0;
} else {
int ans = 0;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0 << endl;
return 0;
} else {
while (h1 < h2) {
h1 -= 12 * b;
h1 += 12 * a;
ans++;
}
cout << ans << endl;
}
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename X>
inline X abs(const X& a) {
return a < 0 ? -a : a;
}
template <typename X>
inline X sqr(const X& a) {
return a * a;
}
int main() {
int h1, h2;
cin >> h1 >> h2;
int a, b;
cin >> a >> b;
if (a <= b && (8 * a + h1) < h2) {
cout << -1 << endl;
return 0;
} else {
int ans = 0;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0 << endl;
return 0;
} else {
while (h1 < h2) {
h1 -= 12 * b;
h1 += 12 * a;
ans++;
}
cout << ans << endl;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, day = 0;
cin >> h1 >> h2 >> a >> b;
if (a > b || h1 + a * 8 >= h2) {
while (h1 < h2) {
h1 += a * 8;
if (h1 >= h2) break;
h1 -= b * 12;
h1 += a * 4;
++day;
}
} else
day = -1;
cout << day << endl;
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, day = 0;
cin >> h1 >> h2 >> a >> b;
if (a > b || h1 + a * 8 >= h2) {
while (h1 < h2) {
h1 += a * 8;
if (h1 >= h2) break;
h1 -= b * 12;
h1 += a * 4;
++day;
}
} else
day = -1;
cout << day << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class Iter>
ostream& __o(ostream& os, Iter a, Iter b);
template <class A, class B>
ostream& operator<<(ostream& os, const pair<A, B>& p);
template <class A>
ostream& operator<<(ostream& os, const vector<A>& x) {
return __o(os, x.begin(), x.end());
}
template <class A>
ostream& operator<<(ostream& os, const deque<A>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B>
ostream& operator<<(ostream& os, const set<A, B>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B>
ostream& operator<<(ostream& os, const multiset<A, B>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B, class C>
ostream& operator<<(ostream& os, const map<A, B, C>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B, class C>
ostream& operator<<(ostream& os, const multimap<A, B, C>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B>
ostream& operator<<(ostream& os, const pair<A, B>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class Iter>
ostream& __o(ostream& os, Iter a, Iter b) {
os << "{";
if (a != b) {
os << *a;
while (++a != b) os << ", " << *a;
}
return os << "}";
}
template <class Iter>
ostream& __d(ostream& os, Iter a, Iter b) {
os << "{\n";
for (Iter beg = a; a != b; ++a)
os << " " << (a - beg) << ": " << *a << endl;
return os << "}";
}
template <class... Args>
void __p(Args&&... args) {
int t[] = {(cerr << args, 0)...};
(void)t;
cerr << endl;
}
template <class A>
A abs(A a) {
return (a < A() ? -a : a);
}
template <class A, class B>
void mini(A& a, B&& b) {
if (b < a) a = b;
}
template <class A, class B>
void maxi(A& a, B&& b) {
if (b > a) a = b;
}
inline int ceil2(int x) { return 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, a, b;
cin >> a >> h;
h -= a;
cin >> a >> b;
if (h <= a * 8) return cout << "0\n", 0;
h -= a * 8;
a = (a - b) * 12;
if (a <= 0) return cout << "-1\n", 0;
cout << (h + a - 1) / a << '\n';
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class Iter>
ostream& __o(ostream& os, Iter a, Iter b);
template <class A, class B>
ostream& operator<<(ostream& os, const pair<A, B>& p);
template <class A>
ostream& operator<<(ostream& os, const vector<A>& x) {
return __o(os, x.begin(), x.end());
}
template <class A>
ostream& operator<<(ostream& os, const deque<A>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B>
ostream& operator<<(ostream& os, const set<A, B>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B>
ostream& operator<<(ostream& os, const multiset<A, B>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B, class C>
ostream& operator<<(ostream& os, const map<A, B, C>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B, class C>
ostream& operator<<(ostream& os, const multimap<A, B, C>& x) {
return __o(os, x.begin(), x.end());
}
template <class A, class B>
ostream& operator<<(ostream& os, const pair<A, B>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class Iter>
ostream& __o(ostream& os, Iter a, Iter b) {
os << "{";
if (a != b) {
os << *a;
while (++a != b) os << ", " << *a;
}
return os << "}";
}
template <class Iter>
ostream& __d(ostream& os, Iter a, Iter b) {
os << "{\n";
for (Iter beg = a; a != b; ++a)
os << " " << (a - beg) << ": " << *a << endl;
return os << "}";
}
template <class... Args>
void __p(Args&&... args) {
int t[] = {(cerr << args, 0)...};
(void)t;
cerr << endl;
}
template <class A>
A abs(A a) {
return (a < A() ? -a : a);
}
template <class A, class B>
void mini(A& a, B&& b) {
if (b < a) a = b;
}
template <class A, class B>
void maxi(A& a, B&& b) {
if (b > a) a = b;
}
inline int ceil2(int x) { return 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, a, b;
cin >> a >> h;
h -= a;
cin >> a >> b;
if (h <= a * 8) return cout << "0\n", 0;
h -= a * 8;
a = (a - b) * 12;
if (a <= 0) return cout << "-1\n", 0;
cout << (h + a - 1) / a << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-6;
const int INF = (int)(INT_MAX - 100);
const long long mod = (int)(1e+9 + 7);
const int N = (int)(0);
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
int d = h2 - h1, v = a - b;
if (d <= 0) return cout << 0, 0;
;
if (v <= 0) return cout << -1, 0;
;
cout << ((((d)) / ((12 * v)) - (((d)) < 0 && ((d)) % ((12 * v)) != 0)) +
((d) % (12 * v) > 0));
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-6;
const int INF = (int)(INT_MAX - 100);
const long long mod = (int)(1e+9 + 7);
const int N = (int)(0);
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
int d = h2 - h1, v = a - b;
if (d <= 0) return cout << 0, 0;
;
if (v <= 0) return cout << -1, 0;
;
cout << ((((d)) / ((12 * v)) - (((d)) < 0 && ((d)) % ((12 * v)) != 0)) +
((d) % (12 * v) > 0));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1000 * 1000 * 1000 + 7;
constexpr long long inf = 1e9;
long long h1, h2;
long long a, b;
bool check(long long day) {
if (h1 >= h2) return true;
long long h = h1 + 12ll * day * (a - b);
h += 8ll * a;
return (h >= h2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> h1 >> h2 >> a >> b;
if (!check(0) && a <= b)
cout << -1 << endl;
else {
long long lo = -1;
long long hi = check(0) ? 0 : inf;
while (lo + 1 != hi) {
long long mid = (lo + hi) / 2ll;
if (check(mid))
hi = mid;
else
lo = mid;
}
cout << hi << endl;
}
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1000 * 1000 * 1000 + 7;
constexpr long long inf = 1e9;
long long h1, h2;
long long a, b;
bool check(long long day) {
if (h1 >= h2) return true;
long long h = h1 + 12ll * day * (a - b);
h += 8ll * a;
return (h >= h2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> h1 >> h2 >> a >> b;
if (!check(0) && a <= b)
cout << -1 << endl;
else {
long long lo = -1;
long long hi = check(0) ? 0 : inf;
while (lo + 1 != hi) {
long long mid = (lo + hi) / 2ll;
if (check(mid))
hi = mid;
else
lo = mid;
}
cout << hi << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long days = 0, a, b, meeting = -1, now = 14, h1 = 0, h2 = 0;
cin >> h1 >> h2;
cin >> a >> b;
for (int i = 0; i < (int)1e6; i++) {
if (h1 >= h2) {
meeting = days;
break;
}
if (now >= 10 && now < 22) {
h1 += a;
} else {
h1 -= b;
}
now = (now + 1) % 24;
if (!now) days++;
}
cout << meeting;
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long days = 0, a, b, meeting = -1, now = 14, h1 = 0, h2 = 0;
cin >> h1 >> h2;
cin >> a >> b;
for (int i = 0; i < (int)1e6; i++) {
if (h1 >= h2) {
meeting = days;
break;
}
if (now >= 10 && now < 22) {
h1 += a;
} else {
h1 -= b;
}
now = (now + 1) % 24;
if (!now) days++;
}
cout << meeting;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h1 + a * 8 >= h2)
cout << "0";
else if (a <= b)
cout << "-1";
else {
h1 += a * 12;
h1 -= b * 12;
int d = 1;
while (h1 < h2) {
if (h1 + a * 8 >= h2)
break;
else {
d++;
}
h1 += (a - b) * 12;
}
cout << d;
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h1 + a * 8 >= h2)
cout << "0";
else if (a <= b)
cout << "-1";
else {
h1 += a * 12;
h1 -= b * 12;
int d = 1;
while (h1 < h2) {
if (h1 + a * 8 >= h2)
break;
else {
d++;
}
h1 += (a - b) * 12;
}
cout << d;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = (1LL << 60) - 1;
bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h1 + 8 * a >= h2)
cout << 0 << endl;
else if (a > b) {
long long cal = h2 - h1 - 8 * a;
long long cal2 = 12 * (a - b);
long long ans = (cal + cal2 - 1) / cal2;
cout << ans << endl;
} else
cout << -1 << endl;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = (1LL << 60) - 1;
bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h1 + 8 * a >= h2)
cout << 0 << endl;
else if (a > b) {
long long cal = h2 - h1 - 8 * a;
long long cal2 = 12 * (a - b);
long long ans = (cal + cal2 - 1) / cal2;
cout << ans << endl;
} else
cout << -1 << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, i = 0;
cin >> h1 >> h2 >> a >> b;
if (a <= b) {
if (h2 > h1 + 8 * a)
cout << "-1\n";
else
cout << "0\n";
} else if (a > b) {
if (h2 <= h1 + 8 * a)
cout << "0\n";
else {
h1 += 8 * a;
while (h1 < h2) {
h1 += 12 * (a - b);
i++;
}
cout << i;
}
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, i = 0;
cin >> h1 >> h2 >> a >> b;
if (a <= b) {
if (h2 > h1 + 8 * a)
cout << "-1\n";
else
cout << "0\n";
} else if (a > b) {
if (h2 <= h1 + 8 * a)
cout << "0\n";
else {
h1 += 8 * a;
while (h1 < h2) {
h1 += 12 * (a - b);
i++;
}
cout << i;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void masterstroke() {
long long h1, h2, a, b;
cin >> h1 >> h2;
cin >> a >> b;
if (b >= a) {
if (a * 8 + h1 >= h2) {
cout << 0 << endl;
} else {
cout << -1 << endl;
}
} else {
long long ht = h1 + 8 * a;
long long day = 0;
while (ht < h2) {
ht -= b * 12;
ht += a * 12;
day++;
}
cout << day << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
for (long long i = 0; i < t; i++) masterstroke();
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void masterstroke() {
long long h1, h2, a, b;
cin >> h1 >> h2;
cin >> a >> b;
if (b >= a) {
if (a * 8 + h1 >= h2) {
cout << 0 << endl;
} else {
cout << -1 << endl;
}
} else {
long long ht = h1 + 8 * a;
long long day = 0;
while (ht < h2) {
ht -= b * 12;
ht += a * 12;
day++;
}
cout << day << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
for (long long i = 0; i < t; i++) masterstroke();
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int ans = 0;
bool flag = false;
int now = h1, next;
while (!flag) {
next = now + 8 * a;
if (next >= h2) {
flag = 1;
printf("%d\n", ans);
return 0;
}
ans++;
now = next;
next = now - 12 * b;
now = next;
next = now + 4 * a;
now = next;
if (a <= b) {
puts("-1");
return 0;
}
}
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int ans = 0;
bool flag = false;
int now = h1, next;
while (!flag) {
next = now + 8 * a;
if (next >= h2) {
flag = 1;
printf("%d\n", ans);
return 0;
}
ans++;
now = next;
next = now - 12 * b;
now = next;
next = now + 4 * a;
now = next;
if (a <= b) {
puts("-1");
return 0;
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long inf2 = INT_MAX;
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
};
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
long long d = 0;
long long now = h1;
bool flag = 0;
while (d <= 10000000 && now > (-inf)) {
if (!d) {
now += 8 * a;
if (now >= h2) {
flag = 1;
break;
}
} else {
now += 12 * a;
if (now >= h2) {
flag = 1;
break;
}
}
now -= (12 * b);
d++;
}
if (flag)
cout << d << '\n';
else
cout << -1 << '\n';
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long inf2 = INT_MAX;
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
};
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
long long d = 0;
long long now = h1;
bool flag = 0;
while (d <= 10000000 && now > (-inf)) {
if (!d) {
now += 8 * a;
if (now >= h2) {
flag = 1;
break;
}
} else {
now += 12 * a;
if (now >= h2) {
flag = 1;
break;
}
}
now -= (12 * b);
d++;
}
if (flag)
cout << d << '\n';
else
cout << -1 << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int main() {
ios::sync_with_stdio(false);
int u, v, x, y, cnt;
cin >> u >> v >> x >> y;
if (u + 8 * x >= v)
cout << 0;
else if (x > y) {
cout << (v - u + 4 * x - 12 * y - 1) / (12 * x - 12 * y);
} else
cout << -1;
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int main() {
ios::sync_with_stdio(false);
int u, v, x, y, cnt;
cin >> u >> v >> x >> y;
if (u + 8 * x >= v)
cout << 0;
else if (x > y) {
cout << (v - u + 4 * x - 12 * y - 1) / (12 * x - 12 * y);
} else
cout << -1;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long int a, b, c, d;
cin >> a >> b >> c >> d;
a += (8 * c);
if (d >= c and b > a)
cout << -1;
else {
long long int answer = 0;
while (b > a) {
answer++;
a += (c - d) * 12;
}
cout << answer;
}
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long int a, b, c, d;
cin >> a >> b >> c >> d;
a += (8 * c);
if (d >= c and b > a)
cout << -1;
else {
long long int answer = 0;
while (b > a) {
answer++;
a += (c - d) * 12;
}
cout << answer;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int h = 14;
int d = 0;
int od;
int nd = 0;
while (h1 < h2) {
if (h >= 10 && h < 22)
h1 += a;
else {
h1 -= b;
}
h++;
if (h == 24) {
od = nd;
nd = h1;
d++;
h = 0;
if (d == 3 && od >= nd) {
cout << -1;
return 0;
}
if (d > 25000) {
cout << -1;
return 0;
}
}
}
cout << d;
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int h = 14;
int d = 0;
int od;
int nd = 0;
while (h1 < h2) {
if (h >= 10 && h < 22)
h1 += a;
else {
h1 -= b;
}
h++;
if (h == 24) {
od = nd;
nd = h1;
d++;
h = 0;
if (d == 3 && od >= nd) {
cout << -1;
return 0;
}
if (d > 25000) {
cout << -1;
return 0;
}
}
}
cout << d;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
int h1, h2;
int a, b;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
double d = (a - b) * 12;
if (h1 >= h2) {
cout << 0 << endl;
return 0;
}
if (d <= 0) {
cout << -1 << endl;
return 0;
}
int res = ceil((h2 - h1) / d);
cout << res << endl;
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
int h1, h2;
int a, b;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
double d = (a - b) * 12;
if (h1 >= h2) {
cout << 0 << endl;
return 0;
}
if (d <= 0) {
cout << -1 << endl;
return 0;
}
int res = ceil((h2 - h1) / d);
cout << res << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
int main() {
int h1, h2, a, b, days;
days = 0;
cin >> h1 >> h2 >> a >> b;
h1 += a * 8;
if (h1 >= h2) {
cout << 0;
return 0;
}
h1 -= b * 12;
if (a * 12 <= b * 12 && h1 < h2) {
cout << -1;
return 0;
}
while (h1 < h2) {
days++;
h1 += a * 12;
if (h1 >= h2) {
cout << days;
return 0;
}
h1 -= b * 12;
}
cout << days;
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
int main() {
int h1, h2, a, b, days;
days = 0;
cin >> h1 >> h2 >> a >> b;
h1 += a * 8;
if (h1 >= h2) {
cout << 0;
return 0;
}
h1 -= b * 12;
if (a * 12 <= b * 12 && h1 < h2) {
cout << -1;
return 0;
}
while (h1 < h2) {
days++;
h1 += a * 12;
if (h1 >= h2) {
cout << days;
return 0;
}
h1 -= b * 12;
}
cout << days;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void sor(vector<long long> &x) { sort(x.begin(), x.end()); }
void rev(vector<long long> &x) { reverse(x.begin(), x.end()); }
long long gcd(long long x, long long y) {
if (y == 0) {
return x;
}
return gcd(y, x % y);
}
long long max(long long x, long long y) {
if (x > y) {
return x;
} else {
return y;
}
}
long long min(long long x, long long y) {
if (x > y) {
return y;
} else {
return x;
}
}
void primeFactors(long long n, vector<long long> &vec) {
while (n % 2 == 0) {
vec.push_back(2);
n = n / 2;
}
for (long long i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
vec.push_back(i);
n = n / i;
}
}
if (n > 2) vec.push_back(n);
}
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long bina(vector<long long> a, long long n, long long m) {
long long start = 0, end = n - 1;
long long c = 0;
if (m >= a[n - 1]) {
return n;
}
while (start <= end) {
long long mid = (start + end) / 2;
if (m >= a[mid]) {
c = mid + 1;
start = mid + 1;
} else {
end = mid - 1;
}
}
return c;
}
long long binarySearch(vector<long long> arr, long long l, long long r,
long long x) {
if (r >= l) {
long long mid = l + (r - l) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int subArraylen(long long arr[], long long n, long long K) {
unordered_map<int, int> mp;
mp[arr[0]] = 0;
for (int i = 1; i < n; i++) {
arr[i] = arr[i] + arr[i - 1];
mp[arr[i]] = i;
}
int len = INT_MAX;
for (int i = 0; i < n; i++) {
if (arr[i] < K)
continue;
else {
int x = arr[i] - K;
if (x == 0) len = min(len, i);
if (mp.find(x) == mp.end())
continue;
else {
len = min(len, i - mp[x]);
}
}
}
return len;
}
long long prime(long long n) {
for (long long i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return i;
}
}
return 0;
}
long long nextPowerOf2(long long n) {
long long p = 1;
if (n && !(n & (n - 1))) return n;
while (p < n) p <<= 1;
return p;
}
long long countBits(long long number) { return (long long)log2(number) + 1; }
long long comb(long long c, long long n) {
long long num = 1, d = 1;
for (long long i = c; i > max(n, c - n); i--) {
num *= i;
}
for (long long i = 2; i <= min(n, c - n); i++) {
d *= i;
}
return (num / d);
}
long long countDivisors(long long n) {
long long cnt = 0;
for (long long i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt = cnt + 2;
}
}
return cnt;
}
int getCount(string a, string b) {
if (b.length() % a.length() != 0) return -1;
int count = b.length() / a.length();
string str = "";
for (int i = 0; i < count; i++) {
str = str + a;
}
if (str == b) return count;
return -1;
}
long long fact(long long n) {
long long res = 1;
for (int i = 2; i <= n; i++) res = res * i;
return res;
}
long long nCr(long long n, long long r) {
return fact(n) / (fact(r) * fact(n - r));
}
int countFreq(string &pat, string &txt) {
int M = pat.length();
int N = txt.length();
int res = 0;
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j]) break;
if (j == M) {
res++;
j = 0;
}
}
return res;
}
int highestPowerof2(int n) {
int res = 0;
for (int i = n; i >= 1; i--) {
if ((i & (i - 1)) == 0) {
res = i;
break;
}
}
return res;
}
long long binarySearchCount(vector<long long> arr, int n, int key, int s) {
int left = s, right = n;
int mid;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key) mid++;
break;
} else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key) mid--;
return mid + 1;
}
long long nCrModp(long long n, long long r) {
if (r > n - r) r = n - r;
long long C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long i = 1; i <= n; i++) {
for (long long j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % 1000000007;
}
return C[r];
}
void solve() {
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0;
} else {
if (a <= b) {
cout << -1;
} else {
long long x = 0;
while (h1 < h2) {
h1 += 12 * (a - b);
x++;
}
cout << x;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
long long t = 1;
for (long long tt = 0; tt < t; ++tt) {
solve();
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void sor(vector<long long> &x) { sort(x.begin(), x.end()); }
void rev(vector<long long> &x) { reverse(x.begin(), x.end()); }
long long gcd(long long x, long long y) {
if (y == 0) {
return x;
}
return gcd(y, x % y);
}
long long max(long long x, long long y) {
if (x > y) {
return x;
} else {
return y;
}
}
long long min(long long x, long long y) {
if (x > y) {
return y;
} else {
return x;
}
}
void primeFactors(long long n, vector<long long> &vec) {
while (n % 2 == 0) {
vec.push_back(2);
n = n / 2;
}
for (long long i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
vec.push_back(i);
n = n / i;
}
}
if (n > 2) vec.push_back(n);
}
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long bina(vector<long long> a, long long n, long long m) {
long long start = 0, end = n - 1;
long long c = 0;
if (m >= a[n - 1]) {
return n;
}
while (start <= end) {
long long mid = (start + end) / 2;
if (m >= a[mid]) {
c = mid + 1;
start = mid + 1;
} else {
end = mid - 1;
}
}
return c;
}
long long binarySearch(vector<long long> arr, long long l, long long r,
long long x) {
if (r >= l) {
long long mid = l + (r - l) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int subArraylen(long long arr[], long long n, long long K) {
unordered_map<int, int> mp;
mp[arr[0]] = 0;
for (int i = 1; i < n; i++) {
arr[i] = arr[i] + arr[i - 1];
mp[arr[i]] = i;
}
int len = INT_MAX;
for (int i = 0; i < n; i++) {
if (arr[i] < K)
continue;
else {
int x = arr[i] - K;
if (x == 0) len = min(len, i);
if (mp.find(x) == mp.end())
continue;
else {
len = min(len, i - mp[x]);
}
}
}
return len;
}
long long prime(long long n) {
for (long long i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return i;
}
}
return 0;
}
long long nextPowerOf2(long long n) {
long long p = 1;
if (n && !(n & (n - 1))) return n;
while (p < n) p <<= 1;
return p;
}
long long countBits(long long number) { return (long long)log2(number) + 1; }
long long comb(long long c, long long n) {
long long num = 1, d = 1;
for (long long i = c; i > max(n, c - n); i--) {
num *= i;
}
for (long long i = 2; i <= min(n, c - n); i++) {
d *= i;
}
return (num / d);
}
long long countDivisors(long long n) {
long long cnt = 0;
for (long long i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt = cnt + 2;
}
}
return cnt;
}
int getCount(string a, string b) {
if (b.length() % a.length() != 0) return -1;
int count = b.length() / a.length();
string str = "";
for (int i = 0; i < count; i++) {
str = str + a;
}
if (str == b) return count;
return -1;
}
long long fact(long long n) {
long long res = 1;
for (int i = 2; i <= n; i++) res = res * i;
return res;
}
long long nCr(long long n, long long r) {
return fact(n) / (fact(r) * fact(n - r));
}
int countFreq(string &pat, string &txt) {
int M = pat.length();
int N = txt.length();
int res = 0;
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j]) break;
if (j == M) {
res++;
j = 0;
}
}
return res;
}
int highestPowerof2(int n) {
int res = 0;
for (int i = n; i >= 1; i--) {
if ((i & (i - 1)) == 0) {
res = i;
break;
}
}
return res;
}
long long binarySearchCount(vector<long long> arr, int n, int key, int s) {
int left = s, right = n;
int mid;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key) mid++;
break;
} else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key) mid--;
return mid + 1;
}
long long nCrModp(long long n, long long r) {
if (r > n - r) r = n - r;
long long C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long i = 1; i <= n; i++) {
for (long long j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % 1000000007;
}
return C[r];
}
void solve() {
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0;
} else {
if (a <= b) {
cout << -1;
} else {
long long x = 0;
while (h1 < h2) {
h1 += 12 * (a - b);
x++;
}
cout << x;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
long long t = 1;
for (long long tt = 0; tt < t; ++tt) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, inf = 0x3f3f3f3f;
int main() {
int a, b, h1, h2;
while (cin >> h1 >> h2) {
cin >> a >> b;
int add = a * 12, de = b * 12, first = 8 * a;
if (a <= b) {
if (h1 + first >= h2)
puts("0");
else
puts("-1");
continue;
}
int ans = 0;
h1 += first;
if (h1 >= h2)
puts("0");
else {
add -= de;
ans += (h2 - h1) / add;
if ((h2 - h1) % add) ans++;
printf("%d\n", ans);
}
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, inf = 0x3f3f3f3f;
int main() {
int a, b, h1, h2;
while (cin >> h1 >> h2) {
cin >> a >> b;
int add = a * 12, de = b * 12, first = 8 * a;
if (a <= b) {
if (h1 + first >= h2)
puts("0");
else
puts("-1");
continue;
}
int ans = 0;
h1 += first;
if (h1 >= h2)
puts("0");
else {
add -= de;
ans += (h2 - h1) / add;
if ((h2 - h1) % add) ans++;
printf("%d\n", ans);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
int main() {
int m, n, a, b;
while (~scanf("%d%d%d%d", &n, &m, &a, &b)) {
if (n + 8 * a >= m)
printf("0\n");
else if (n + 8 * a < m && b >= a)
printf("-1\n");
else {
n = n + 8 * a;
int ans = 0;
while (1) {
ans++;
n += 12 * a;
n -= 12 * b;
if (n >= m) {
printf("%d\n", ans);
break;
}
}
}
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
int main() {
int m, n, a, b;
while (~scanf("%d%d%d%d", &n, &m, &a, &b)) {
if (n + 8 * a >= m)
printf("0\n");
else if (n + 8 * a < m && b >= a)
printf("-1\n");
else {
n = n + 8 * a;
int ans = 0;
while (1) {
ans++;
n += 12 * a;
n -= 12 * b;
if (n >= m) {
printf("%d\n", ans);
break;
}
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007LL;
const long long inf = 1e18;
inline long long max(long long a, int32_t b) { return a > b ? a : b; }
inline long long max(int32_t a, long long b) { return a > b ? a : b; }
inline long long min(long long a, int32_t b) { return a < b ? a : b; }
inline long long min(int32_t a, long long b) { return a < b ? a : b; }
long long d4_1[4] = {1, 0, -1, 0};
long long d4_2[4] = {0, 1, 0, -1};
long long d8_1[8] = {1, 1, 1, -1, -1, -1, 0, 0};
long long d8_2[8] = {0, -1, 1, 0, -1, 1, 1, -1};
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return to_string(string(1, c)); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (long long i = 0; i < static_cast<long long>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug() { cout << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {}
void solve() {
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
long long height_after_day0 = h1 + 8 * a;
if (height_after_day0 >= h2) {
cout << 0 << "\n";
return;
}
long long height_day1 = height_after_day0 - 12 * b + 4 * a;
if (height_day1 <= h1) {
cout << "-1\n";
return;
}
long long prev_height = height_after_day0;
for (long long i = 1; i < 2000005; i++) {
long long night_height = prev_height + 12 * a;
long long next_height = night_height - 12 * b;
if (next_height >= h2) {
cout << i << "\n";
return;
}
prev_height = next_height;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(10);
long long t = 1;
for (long long i = 1; i <= t; i++) {
solve();
}
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007LL;
const long long inf = 1e18;
inline long long max(long long a, int32_t b) { return a > b ? a : b; }
inline long long max(int32_t a, long long b) { return a > b ? a : b; }
inline long long min(long long a, int32_t b) { return a < b ? a : b; }
inline long long min(int32_t a, long long b) { return a < b ? a : b; }
long long d4_1[4] = {1, 0, -1, 0};
long long d4_2[4] = {0, 1, 0, -1};
long long d8_1[8] = {1, 1, 1, -1, -1, -1, 0, 0};
long long d8_2[8] = {0, -1, 1, 0, -1, 1, 1, -1};
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return to_string(string(1, c)); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (long long i = 0; i < static_cast<long long>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug() { cout << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {}
void solve() {
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
long long height_after_day0 = h1 + 8 * a;
if (height_after_day0 >= h2) {
cout << 0 << "\n";
return;
}
long long height_day1 = height_after_day0 - 12 * b + 4 * a;
if (height_day1 <= h1) {
cout << "-1\n";
return;
}
long long prev_height = height_after_day0;
for (long long i = 1; i < 2000005; i++) {
long long night_height = prev_height + 12 * a;
long long next_height = night_height - 12 * b;
if (next_height >= h2) {
cout << i << "\n";
return;
}
prev_height = next_height;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(10);
long long t = 1;
for (long long i = 1; i <= t; i++) {
solve();
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, ans = 0, f = 0;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
f = 1;
}
if (!f) {
if (a <= b) {
cout << -1 << endl;
return 0;
}
h1 -= 12 * b;
ans++;
while (1) {
h1 += 12 * a;
if (h1 >= h2) break;
h1 -= 12 * b;
ans++;
}
}
cout << ans << endl;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, ans = 0, f = 0;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
f = 1;
}
if (!f) {
if (a <= b) {
cout << -1 << endl;
return 0;
}
h1 -= 12 * b;
ans++;
while (1) {
h1 += 12 * a;
if (h1 >= h2) break;
h1 -= 12 * b;
ans++;
}
}
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
if (b < a) a = b;
return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 0;
void solve() {
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
h2 -= h1;
h2 -= 8 * a;
a *= 12;
b *= 12;
if (h2 <= 0) {
cout << 0;
return;
}
long long diff = a - b;
if (diff <= 0) {
cout << -1;
return;
}
cout << ((h2 + diff - 1) / diff);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
if (b < a) a = b;
return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 0;
void solve() {
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
h2 -= h1;
h2 -= 8 * a;
a *= 12;
b *= 12;
if (h2 <= 0) {
cout << 0;
return;
}
long long diff = a - b;
if (diff <= 0) {
cout << -1;
return;
}
cout << ((h2 + diff - 1) / diff);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
```
|
#include <bits/stdc++.h>
int main() {
int h1, h2;
int a, b;
scanf("%d%d", &h1, &h2);
scanf("%d%d", &a, &b);
int i = h1;
int day = 1;
int ans = 0;
while (i < h2) {
if (day == 1) {
day = 0;
i = i + a * 8;
if (a <= b && i < h2) {
ans = -1;
break;
}
} else {
i = i + a * 12;
}
if (i >= h2) {
break;
} else {
ans++;
i -= b * 12;
}
}
printf("%d\n", ans);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
int main() {
int h1, h2;
int a, b;
scanf("%d%d", &h1, &h2);
scanf("%d%d", &a, &b);
int i = h1;
int day = 1;
int ans = 0;
while (i < h2) {
if (day == 1) {
day = 0;
i = i + a * 8;
if (a <= b && i < h2) {
ans = -1;
break;
}
} else {
i = i + a * 12;
}
if (i >= h2) {
break;
} else {
ans++;
i -= b * 12;
}
}
printf("%d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
int x = m - n, r = 0, t = 14;
if (b >= a && a * 8 < x) {
cout << -1;
return 0;
}
while (x > 0) {
if (10 <= t && t < 22)
x -= a;
else
x += b;
t++;
if (t == 24) r++, t = 0;
}
cout << r;
}
|
### Prompt
Create a solution in CPP for the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
int x = m - n, r = 0, t = 14;
if (b >= a && a * 8 < x) {
cout << -1;
return 0;
}
while (x > 0) {
if (10 <= t && t < 22)
x -= a;
else
x += b;
t++;
if (t == 24) r++, t = 0;
}
cout << r;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, i, c, d;
cin >> h1 >> h2 >> a >> b;
if (b >= a && h2 - h1 > 8 * a)
cout << "-1" << endl;
else {
for (i = 0;; i++) {
if (i == 0) {
c = 8 * a;
h1 = h1 + c;
if (h1 >= h2) {
cout << i << endl;
break;
}
d = 12 * b;
h1 = h1 - d;
} else {
c = 12 * a;
h1 = h1 + c;
if (h1 >= h2) {
cout << i << endl;
break;
}
d = 12 * b;
h1 = h1 - d;
}
}
}
}
|
### Prompt
Generate a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, i, c, d;
cin >> h1 >> h2 >> a >> b;
if (b >= a && h2 - h1 > 8 * a)
cout << "-1" << endl;
else {
for (i = 0;; i++) {
if (i == 0) {
c = 8 * a;
h1 = h1 + c;
if (h1 >= h2) {
cout << i << endl;
break;
}
d = 12 * b;
h1 = h1 - d;
} else {
c = 12 * a;
h1 = h1 + c;
if (h1 >= h2) {
cout << i << endl;
break;
}
d = 12 * b;
h1 = h1 - d;
}
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, h1, h2, d = -1;
cin >> h1 >> h2 >> a >> b;
if (a <= b && h1 + 8 * a < h2) {
cout << -1;
return 0;
}
h1 -= 4 * a;
while (h1 < h2) {
h1 += 12 * a;
d++;
if (h1 >= h2) {
cout << d;
return 0;
}
h1 -= 12 * b;
}
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, h1, h2, d = -1;
cin >> h1 >> h2 >> a >> b;
if (a <= b && h1 + 8 * a < h2) {
cout << -1;
return 0;
}
h1 -= 4 * a;
while (h1 < h2) {
h1 += 12 * a;
d++;
if (h1 >= h2) {
cout << d;
return 0;
}
h1 -= 12 * b;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e9 + 7;
int cal(int j) {
int sum1 = ((j) * (j - 1)) / 2;
return (sum1);
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int findGCD(int arr[], int n) {
int result = arr[0];
for (int i = 1; i < n; i++) {
result = gcd(arr[i], result);
if (result == 1) {
return 1;
}
}
return result;
}
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++)
if (n % i == 0) return false;
return true;
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
bool isNumber(string s) {
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false) return false;
return true;
}
int maxi(int a, int b, int c) {
int ma = max(a, b);
return (max(ma, c));
}
int ldn(int n) {
if (n % 2 == 0)
return (2);
else {
int i;
for (i = 3; i * i <= n; i++) {
if (n % i == 0) return (i);
}
return (n);
}
}
long long dig(long long n) {
long long sum = 0;
while (n > 0) {
sum++;
n = n / 10;
}
return (sum);
}
bool is_integer(float k) { return std::floor(k) == k; }
bool isy(long double x) {
if (x >= 0) {
long long sr = sqrt(x);
return (sr * sr == x);
}
return false;
}
long long powert(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
set<long long> findDisappearedNumbers(vector<int>& num) {
set<long long> s;
vector<int> nums = num;
for (int i = 0; i < nums.size(); i++) {
int n = abs(nums[i]);
if (nums[n - 1] > 0) nums[n - 1] = -nums[n - 1];
}
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > 0) s.insert(i + 1);
}
return s;
}
void mycode() {
long long n = 0, li, m, k, ri, i, x, j = -1, d = 0, count = 0, sum = 0;
string s;
cin >> n >> m;
long long a, b;
cin >> a >> b;
d = n + a * 8;
if (d < m) {
if (b >= a) {
sum = -1;
} else {
while (d < m) {
sum++;
d -= 12 * b;
d += a * 12;
}
}
}
cout << sum << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t = 1;
while (t--) {
mycode();
}
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e9 + 7;
int cal(int j) {
int sum1 = ((j) * (j - 1)) / 2;
return (sum1);
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int findGCD(int arr[], int n) {
int result = arr[0];
for (int i = 1; i < n; i++) {
result = gcd(arr[i], result);
if (result == 1) {
return 1;
}
}
return result;
}
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++)
if (n % i == 0) return false;
return true;
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
bool isNumber(string s) {
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false) return false;
return true;
}
int maxi(int a, int b, int c) {
int ma = max(a, b);
return (max(ma, c));
}
int ldn(int n) {
if (n % 2 == 0)
return (2);
else {
int i;
for (i = 3; i * i <= n; i++) {
if (n % i == 0) return (i);
}
return (n);
}
}
long long dig(long long n) {
long long sum = 0;
while (n > 0) {
sum++;
n = n / 10;
}
return (sum);
}
bool is_integer(float k) { return std::floor(k) == k; }
bool isy(long double x) {
if (x >= 0) {
long long sr = sqrt(x);
return (sr * sr == x);
}
return false;
}
long long powert(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
set<long long> findDisappearedNumbers(vector<int>& num) {
set<long long> s;
vector<int> nums = num;
for (int i = 0; i < nums.size(); i++) {
int n = abs(nums[i]);
if (nums[n - 1] > 0) nums[n - 1] = -nums[n - 1];
}
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > 0) s.insert(i + 1);
}
return s;
}
void mycode() {
long long n = 0, li, m, k, ri, i, x, j = -1, d = 0, count = 0, sum = 0;
string s;
cin >> n >> m;
long long a, b;
cin >> a >> b;
d = n + a * 8;
if (d < m) {
if (b >= a) {
sum = -1;
} else {
while (d < m) {
sum++;
d -= 12 * b;
d += a * 12;
}
}
}
cout << sum << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t = 1;
while (t--) {
mycode();
}
return 0;
}
```
|
#include <bits/stdc++.h>
int main() {
int h1, h2, a, b;
scanf("%d %d", &h1, &h2);
scanf("%d %d", &a, &b);
if (h2 - h1 <= 8 * a)
printf("%d", 0);
else if (a <= b)
printf("%d", -1);
else {
h1 = h1 + a * 8 - b * 12;
int count = 1;
while (h1 + 12 * a < h2) {
h1 = a * 12 + h1 - b * 12;
count++;
}
printf("%d", count);
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
int main() {
int h1, h2, a, b;
scanf("%d %d", &h1, &h2);
scanf("%d %d", &a, &b);
if (h2 - h1 <= 8 * a)
printf("%d", 0);
else if (a <= b)
printf("%d", -1);
else {
h1 = h1 + a * 8 - b * 12;
int count = 1;
while (h1 + 12 * a < h2) {
h1 = a * 12 + h1 - b * 12;
count++;
}
printf("%d", count);
}
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int N = 1000006;
const int MAX = 1e9;
int h1, h2, a, b, ans;
int main() {
cin >> h1 >> h2;
cin >> a >> b;
if (h1 + 8 * a >= h2) {
cout << 0 << endl;
return 0;
}
if (a <= b) {
cout << -1 << endl;
return 0;
}
while (1) {
h1 = h1 + 8 * a;
if (h1 >= h2) break;
h1 -= b * 12;
h1 += 4 * a;
ans++;
}
cout << ans << endl;
}
|
### Prompt
Generate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int N = 1000006;
const int MAX = 1e9;
int h1, h2, a, b, ans;
int main() {
cin >> h1 >> h2;
cin >> a >> b;
if (h1 + 8 * a >= h2) {
cout << 0 << endl;
return 0;
}
if (a <= b) {
cout << -1 << endl;
return 0;
}
while (1) {
h1 = h1 + 8 * a;
if (h1 >= h2) break;
h1 -= b * 12;
h1 += 4 * a;
ans++;
}
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline void sub(long long &a, long long b) {
a -= b;
if (a < 0) a += 1000000007;
}
inline void add(long long &a, long long b) {
a += b;
if (a >= 1000000007) a -= 1000000007;
}
template <typename T>
inline T const &MAX(T const &a, T const &b) {
return a > b ? a : b;
}
template <typename T>
inline T const &MIN(T const &a, T const &b) {
return a < b ? a : b;
}
inline long long qp(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 1000000007;
a = a * a % 1000000007, b >>= 1;
}
return ans;
}
inline long long qp(long long a, long long b, long long c) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % c;
a = a * a % c, b >>= 1;
}
return ans;
}
using namespace std;
const unsigned long long ba = 233;
const double eps = 1e-8;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 10000 + 10, maxn = 10000000 + 10, inf = 0x3f3f3f3f;
int main() {
int h1, h2, a, b;
scanf("%d%d%d%d", &h1, &h2, &a, &b);
if (h1 + 8 * a >= h2)
puts("0");
else {
if (a <= b)
puts("-1");
else {
h1 += 8 * a - 12 * b;
int ans = 1;
while (h1 + 12 * a < h2) h1 += 12 * (a - b), ans++;
printf("%d\n", ans);
}
}
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline void sub(long long &a, long long b) {
a -= b;
if (a < 0) a += 1000000007;
}
inline void add(long long &a, long long b) {
a += b;
if (a >= 1000000007) a -= 1000000007;
}
template <typename T>
inline T const &MAX(T const &a, T const &b) {
return a > b ? a : b;
}
template <typename T>
inline T const &MIN(T const &a, T const &b) {
return a < b ? a : b;
}
inline long long qp(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 1000000007;
a = a * a % 1000000007, b >>= 1;
}
return ans;
}
inline long long qp(long long a, long long b, long long c) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % c;
a = a * a % c, b >>= 1;
}
return ans;
}
using namespace std;
const unsigned long long ba = 233;
const double eps = 1e-8;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 10000 + 10, maxn = 10000000 + 10, inf = 0x3f3f3f3f;
int main() {
int h1, h2, a, b;
scanf("%d%d%d%d", &h1, &h2, &a, &b);
if (h1 + 8 * a >= h2)
puts("0");
else {
if (a <= b)
puts("-1");
else {
h1 += 8 * a - 12 * b;
int ans = 1;
while (h1 + 12 * a < h2) h1 += 12 * (a - b), ans++;
printf("%d\n", ans);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0 << endl;
return 0;
}
h1 -= 12 * b;
for (long long i = 1; i <= 1e6; i++) {
h1 += 12 * a;
if (h1 >= h2) {
cout << i << endl;
return 0;
}
h1 -= 12 * b;
}
cout << -1 << endl;
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long h1, h2;
cin >> h1 >> h2;
long long a, b;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2) {
cout << 0 << endl;
return 0;
}
h1 -= 12 * b;
for (long long i = 1; i <= 1e6; i++) {
h1 += 12 * a;
if (h1 >= h2) {
cout << i << endl;
return 0;
}
h1 -= 12 * b;
}
cout << -1 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int64_t h1, h2, a, b, i, c, k, l;
scanf("%I64d%I64d", &h1, &h2);
scanf("%I64d%I64d", &a, &b);
c = h1 + 8 * a;
if (c >= h2) {
printf("0");
return 0;
}
if (c < h2 && a <= b) {
printf("-1");
return 0;
}
if (a > b || a <= b) {
k = h2 - (h1 + 8 * a);
i = 12 * (a - b);
l = (i + k) - 1;
l /= i;
printf("%I64d", l);
}
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int64_t h1, h2, a, b, i, c, k, l;
scanf("%I64d%I64d", &h1, &h2);
scanf("%I64d%I64d", &a, &b);
c = h1 + 8 * a;
if (c >= h2) {
printf("0");
return 0;
}
if (c < h2 && a <= b) {
printf("-1");
return 0;
}
if (a > b || a <= b) {
k = h2 - (h1 + 8 * a);
i = 12 * (a - b);
l = (i + k) - 1;
l /= i;
printf("%I64d", l);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int h1, h2;
int a, b;
int ans = 0;
int main() {
scanf("%d%d", &h1, &h2);
scanf("%d%d", &a, &b);
h1 += a * 8;
if (h1 >= h2)
printf("0\n");
else if (h1 < h2 && b >= a)
printf("-1\n");
else if (h1 < h2 && b < a) {
int num1 = (h2 - h1) % ((a - b) * 12);
if (num1 == 0)
printf("%d\n", (h2 - h1) / ((a - b) * 12));
else
printf("%d\n", (h2 - h1) / ((a - b) * 12) + 1);
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int h1, h2;
int a, b;
int ans = 0;
int main() {
scanf("%d%d", &h1, &h2);
scanf("%d%d", &a, &b);
h1 += a * 8;
if (h1 >= h2)
printf("0\n");
else if (h1 < h2 && b >= a)
printf("-1\n");
else if (h1 < h2 && b < a) {
int num1 = (h2 - h1) % ((a - b) * 12);
if (num1 == 0)
printf("%d\n", (h2 - h1) / ((a - b) * 12));
else
printf("%d\n", (h2 - h1) / ((a - b) * 12) + 1);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2;
int a, b;
int sol;
cin >> h1 >> h2 >> a >> b;
h1 += (a * 8);
if (h1 >= h2)
cout << 0;
else {
if (a <= b)
cout << -1;
else {
sol = 0;
while (h1 < h2) {
sol++;
h1 -= (b * 12);
h1 += (a * 12);
}
cout << sol;
}
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2;
int a, b;
int sol;
cin >> h1 >> h2 >> a >> b;
h1 += (a * 8);
if (h1 >= h2)
cout << 0;
else {
if (a <= b)
cout << -1;
else {
sol = 0;
while (h1 < h2) {
sol++;
h1 -= (b * 12);
h1 += (a * 12);
}
cout << sol;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int aday = a * 12, bday = b * 12;
h1 += a * 8;
if (aday <= bday && h1 < h2) {
cout << -1 << "\n";
return 0;
}
if (h1 >= h2) {
cout << 0 << "\n";
return 0;
}
int d = aday - bday;
int res = (h2 - h1 + d - 1) / d;
cout << res << "\n";
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int aday = a * 12, bday = b * 12;
h1 += a * 8;
if (aday <= bday && h1 < h2) {
cout << -1 << "\n";
return 0;
}
if (h1 >= h2) {
cout << 0 << "\n";
return 0;
}
int d = aday - bday;
int res = (h2 - h1 + d - 1) / d;
cout << res << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2;
int a, b;
int cur;
int ret = 0;
cin >> h1 >> h2;
cin >> a >> b;
cur = h1;
if (cur >= h2 || cur + a * 8 >= h2) {
cout << 0;
return 0;
}
cur = cur + a * 8 - b * 12;
++ret;
while (1) {
if (cur + a * 12 >= h2) {
cout << ret;
return 0;
} else {
if (cur + a * 12 - b * 12 <= cur) {
cout << -1;
return 0;
} else {
cur = cur + a * 12 - b * 12;
++ret;
}
}
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2;
int a, b;
int cur;
int ret = 0;
cin >> h1 >> h2;
cin >> a >> b;
cur = h1;
if (cur >= h2 || cur + a * 8 >= h2) {
cout << 0;
return 0;
}
cur = cur + a * 8 - b * 12;
++ret;
while (1) {
if (cur + a * 12 >= h2) {
cout << ret;
return 0;
} else {
if (cur + a * 12 - b * 12 <= cur) {
cout << -1;
return 0;
} else {
cur = cur + a * 12 - b * 12;
++ret;
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, c, d;
cin >> h1 >> h2 >> a >> b;
float count;
h1 += 8 * a;
if (h1 >= h2) {
cout << "0" << endl;
} else if (b >= a) {
cout << "-1" << endl;
} else {
c = h2 - h1;
d = a - b;
count = ceil(float(c) / (float(d) * 12));
cout << count << endl;
}
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, c, d;
cin >> h1 >> h2 >> a >> b;
float count;
h1 += 8 * a;
if (h1 >= h2) {
cout << "0" << endl;
} else if (b >= a) {
cout << "-1" << endl;
} else {
c = h2 - h1;
d = a - b;
count = ceil(float(c) / (float(d) * 12));
cout << count << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 100010;
const double PI = acos(-1);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll h, f, a, b, time, t = 0;
cin >> h >> f >> a >> b;
for (time = 15; time < 10000000; time++) {
if (t == 0) {
h += a;
} else {
h -= b;
}
if (time % 24 == 10 || time % 24 == 22) t ^= 1;
if (h >= f) break;
}
if (time == 10000000) {
cout << -1 << endl;
} else {
time -= 10;
cout << (time) / 24LL << endl;
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 100010;
const double PI = acos(-1);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll h, f, a, b, time, t = 0;
cin >> h >> f >> a >> b;
for (time = 15; time < 10000000; time++) {
if (t == 0) {
h += a;
} else {
h -= b;
}
if (time % 24 == 10 || time % 24 == 22) t ^= 1;
if (h >= f) break;
}
if (time == 10000000) {
cout << -1 << endl;
} else {
time -= 10;
cout << (time) / 24LL << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b;
int main() {
ios_base::sync_with_stdio(0);
cin >> h1 >> h2 >> a >> b;
if (h2 <= h1 + a * 8) {
cout << 0 << endl;
return 0;
}
if (b >= a) {
cout << -1 << endl;
return 0;
}
int d = 1;
h1 += a * 8;
h1 -= b * 12;
while (h2 > h1 + a * 12) {
h1 += (a - b) * 12;
++d;
}
cout << d << endl;
}
|
### Prompt
Generate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b;
int main() {
ios_base::sync_with_stdio(0);
cin >> h1 >> h2 >> a >> b;
if (h2 <= h1 + a * 8) {
cout << 0 << endl;
return 0;
}
if (b >= a) {
cout << -1 << endl;
return 0;
}
int d = 1;
h1 += a * 8;
h1 -= b * 12;
while (h2 > h1 + a * 12) {
h1 += (a - b) * 12;
++d;
}
cout << d << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b, t(14);
int main() {
cin >> h1 >> h2 >> a >> b;
if (h1 + a * (22. - 14.) < h2 && a <= b)
cout << -1;
else if (h1 + a * (22. - 14.) >= h2)
cout << 0;
else {
h1 += a * (22 - 14);
int ans(0);
while (true) {
ans++;
h1 -= (12 * b);
if (h1 + a * 12 < h2) {
h1 += (12 * a);
} else {
cout << ans;
break;
}
}
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b, t(14);
int main() {
cin >> h1 >> h2 >> a >> b;
if (h1 + a * (22. - 14.) < h2 && a <= b)
cout << -1;
else if (h1 + a * (22. - 14.) >= h2)
cout << 0;
else {
h1 += a * (22 - 14);
int ans(0);
while (true) {
ans++;
h1 -= (12 * b);
if (h1 + a * 12 < h2) {
h1 += (12 * a);
} else {
cout << ans;
break;
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, i;
while (scanf("%d %d", &h1, &h2) != EOF) {
scanf("%d %d", &a, &b);
if (a <= b) {
if (h1 + 8 * a >= h2)
printf("0\n");
else if (h1 + 20 * a - 12 * b >= h2)
printf("1\n");
else
printf("-1\n");
continue;
}
h1 += 8 * a;
if (h1 >= h2) {
printf("0\n");
continue;
}
h1 -= 2 * b;
for (i = 1;; i++) {
h1 -= 10 * b;
h1 += 12 * a;
if (h1 >= h2) break;
h1 -= 2 * b;
}
printf("%d\n", i);
}
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b, i;
while (scanf("%d %d", &h1, &h2) != EOF) {
scanf("%d %d", &a, &b);
if (a <= b) {
if (h1 + 8 * a >= h2)
printf("0\n");
else if (h1 + 20 * a - 12 * b >= h2)
printf("1\n");
else
printf("-1\n");
continue;
}
h1 += 8 * a;
if (h1 >= h2) {
printf("0\n");
continue;
}
h1 -= 2 * b;
for (i = 1;; i++) {
h1 -= 10 * b;
h1 += 12 * a;
if (h1 >= h2) break;
h1 -= 2 * b;
}
printf("%d\n", i);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int a, b, h1, h2, t, h;
int main() {
while (~scanf("%d%d%d%d", &h1, &h2, &a, &b)) {
if (8 * a + h1 >= h2) {
printf("0\n");
continue;
}
if (a <= b) {
if (8 * a + h1 - 12 * b + 12 * a >= h2)
printf("1\n");
else
printf("-1\n");
continue;
}
h = 8 * a + h1 - 12 * b;
t = 1;
while (true) {
h += 12 * a;
if (h >= h2) break;
++t;
h -= 12 * b;
}
printf("%d\n", t);
}
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a, b, h1, h2, t, h;
int main() {
while (~scanf("%d%d%d%d", &h1, &h2, &a, &b)) {
if (8 * a + h1 >= h2) {
printf("0\n");
continue;
}
if (a <= b) {
if (8 * a + h1 - 12 * b + 12 * a >= h2)
printf("1\n");
else
printf("-1\n");
continue;
}
h = 8 * a + h1 - 12 * b;
t = 1;
while (true) {
h += 12 * a;
if (h >= h2) break;
++t;
h -= 12 * b;
}
printf("%d\n", t);
}
return 0;
}
```
|
#include <bits/stdc++.h>
typedef long long LL;
const double INF = 1e100;
const int oo = ~0u >> 2;
const double pi = acos(-1.0);
const double EPS = 1e-8;
const int MAXN = 100033;
int h1, h2;
int a, b;
int cur;
int main() {
scanf("%d%d", &h1, &h2);
scanf("%d%d", &a, &b);
if (h1 + 8 * a >= h2)
puts("0");
else if (a > b) {
int num = h2 - h1 - 8 * a, l = 12 * (a - b);
printf("%d\n", (num + l - 1) / l);
} else
puts("-1");
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
typedef long long LL;
const double INF = 1e100;
const int oo = ~0u >> 2;
const double pi = acos(-1.0);
const double EPS = 1e-8;
const int MAXN = 100033;
int h1, h2;
int a, b;
int cur;
int main() {
scanf("%d%d", &h1, &h2);
scanf("%d%d", &a, &b);
if (h1 + 8 * a >= h2)
puts("0");
else if (a > b) {
int num = h2 - h1 - 8 * a, l = 12 * (a - b);
printf("%d\n", (num + l - 1) / l);
} else
puts("-1");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> vis[1500000];
int a[1500000], b[1500000];
int main() {
int h1, h2, cur1, cur2;
int cnt = 0;
scanf("%d%d%d%d", &h1, &h2, &cur1, &cur2);
if (cur1 * 8 >= h2 - h1)
printf("0\n");
else if (cur2 >= cur1 && cur1 * 8 < h2 - h1)
printf("-1\n");
else if (cur2 >= cur1)
printf("-1\n");
else {
int m = h2 - h1;
while (m > 0) {
m -= cur1 * 8;
if (m <= 0) {
break;
}
m += cur2 * 12;
m -= cur1 * 4;
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> vis[1500000];
int a[1500000], b[1500000];
int main() {
int h1, h2, cur1, cur2;
int cnt = 0;
scanf("%d%d%d%d", &h1, &h2, &cur1, &cur2);
if (cur1 * 8 >= h2 - h1)
printf("0\n");
else if (cur2 >= cur1 && cur1 * 8 < h2 - h1)
printf("-1\n");
else if (cur2 >= cur1)
printf("-1\n");
else {
int m = h2 - h1;
while (m > 0) {
m -= cur1 * 8;
if (m <= 0) {
break;
}
m += cur2 * 12;
m -= cur1 * 4;
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e2 + 7;
long long a, b, h1, h2, H, jump, req, total;
string s;
int main() {
cin >> h1 >> h2 >> a >> b;
req = h2 - h1, jump = (a - b) * 12, req -= (a * 8);
if (b >= a or (h2 - h1) <= a * 8)
puts((h2 - h1) <= a * 8 ? "0" : "-1");
else
cout << req / jump + (req % jump != 0) << "\n";
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e2 + 7;
long long a, b, h1, h2, H, jump, req, total;
string s;
int main() {
cin >> h1 >> h2 >> a >> b;
req = h2 - h1, jump = (a - b) * 12, req -= (a * 8);
if (b >= a or (h2 - h1) <= a * 8)
puts((h2 - h1) <= a * 8 ? "0" : "-1");
else
cout << req / jump + (req % jump != 0) << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long m, a, b, n, d, x;
cin >> a >> b >> n >> m;
x = (b - a) - (8 * n);
if (x <= 0)
cout << "0" << endl;
else {
if (n <= m)
cout << "-1" << endl;
else {
double y = (double)x / ((12 * n) - (12 * m));
cout << ceil(y) << endl;
}
}
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long m, a, b, n, d, x;
cin >> a >> b >> n >> m;
x = (b - a) - (8 * n);
if (x <= 0)
cout << "0" << endl;
else {
if (n <= m)
cout << "-1" << endl;
else {
double y = (double)x / ((12 * n) - (12 * m));
cout << ceil(y) << endl;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
int x, y, f, b;
cin >> x >> y;
cin >> f >> b;
if (f * 8 + x >= y) {
cout << "0\n";
return 0;
}
long long cur = f * 8 + x - 12LL * b, ans = 0;
while (1) {
ans++;
if (cur + 12LL * f >= y || ans > 6e7) break;
cur = cur + 12LL * f - b * 12LL;
}
if (ans >= 6e7)
cout << "-1\n";
else
cout << ans << endl;
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
int x, y, f, b;
cin >> x >> y;
cin >> f >> b;
if (f * 8 + x >= y) {
cout << "0\n";
return 0;
}
long long cur = f * 8 + x - 12LL * b, ans = 0;
while (1) {
ans++;
if (cur + 12LL * f >= y || ans > 6e7) break;
cur = cur + 12LL * f - b * 12LL;
}
if (ans >= 6e7)
cout << "-1\n";
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h1 + a * 8 >= h2) {
cout << 0 << '\n';
} else {
cout << ((12 * a - 12 * b > 0)
? (h2 - (h1 + a * 8)) / (12 * a - 12 * b) +
((h2 - (h1 + a * 8)) % (12 * a - 12 * b) > 0)
: -1)
<< '\n';
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
if (h1 + a * 8 >= h2) {
cout << 0 << '\n';
} else {
cout << ((12 * a - 12 * b > 0)
? (h2 - (h1 + a * 8)) / (12 * a - 12 * b) +
((h2 - (h1 + a * 8)) % (12 * a - 12 * b) > 0)
: -1)
<< '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k, h;
cin >> n >> m >> k >> h;
if ((n + (8 * k)) < m && k <= h) {
cout << -1 << endl;
return 0;
}
int i = 0;
while (n < m) {
if (i == 0)
n += 8 * k;
else
n += 12 * k;
if (n >= m) break;
n -= 12 * h;
i++;
}
cout << i << endl;
}
|
### Prompt
Create a solution in CPP for the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k, h;
cin >> n >> m >> k >> h;
if ((n + (8 * k)) < m && k <= h) {
cout << -1 << endl;
return 0;
}
int i = 0;
while (n < m) {
if (i == 0)
n += 8 * k;
else
n += 12 * k;
if (n >= m) break;
n -= 12 * h;
i++;
}
cout << i << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
return (b == 0 ? a : gcd(b, a % b));
}
long long int lcm(int a, int b) { return (a * (b / gcd(a, b))); }
long long int max(long long int a, long long int b, long long int c) {
return max(a, max(b, c));
}
long long int power(long long int x, long long int y) {
long long int ans = 1;
while (y > 0) {
if (y & 1) ans = (ans * x) % 1000000007;
x = (x * x) % 1000000007;
y /= 2;
}
return ans;
}
int main() {
long long int t, n, h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
n = h2 - h1;
if (8 * a >= n)
cout << "0" << endl;
else if (12 * a <= (12 * b))
cout << "-1" << endl;
else if ((12 * b + n) <= 20 * a)
cout << "1" << endl;
else {
long long int day = 1;
n = n - 8 * a + 12 * b - 12 * a;
while (n > 0) {
n += 12 * b;
n -= 12 * a;
day++;
}
cout << day << endl;
}
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
return (b == 0 ? a : gcd(b, a % b));
}
long long int lcm(int a, int b) { return (a * (b / gcd(a, b))); }
long long int max(long long int a, long long int b, long long int c) {
return max(a, max(b, c));
}
long long int power(long long int x, long long int y) {
long long int ans = 1;
while (y > 0) {
if (y & 1) ans = (ans * x) % 1000000007;
x = (x * x) % 1000000007;
y /= 2;
}
return ans;
}
int main() {
long long int t, n, h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
n = h2 - h1;
if (8 * a >= n)
cout << "0" << endl;
else if (12 * a <= (12 * b))
cout << "-1" << endl;
else if ((12 * b + n) <= 20 * a)
cout << "1" << endl;
else {
long long int day = 1;
n = n - 8 * a + 12 * b - 12 * a;
while (n > 0) {
n += 12 * b;
n -= 12 * a;
day++;
}
cout << day << endl;
}
}
```
|
#include <bits/stdc++.h>
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int exp(long long int x, long long int y) {
long long int res = 1;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res;
}
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int h1, h2, a, b, i, x;
cin >> h1 >> h2;
cin >> a >> b;
if (8 * a >= h2 - h1)
cout << 0;
else {
if (b >= a)
cout << -1;
else {
h1 = h1 + 8 * a;
x = 12 * (a - b);
if ((h2 - h1) % x == 0)
cout << (h2 - h1) / x;
else
cout << ((h2 - h1) / x) + 1;
}
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int exp(long long int x, long long int y) {
long long int res = 1;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res;
}
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int h1, h2, a, b, i, x;
cin >> h1 >> h2;
cin >> a >> b;
if (8 * a >= h2 - h1)
cout << 0;
else {
if (b >= a)
cout << -1;
else {
h1 = h1 + 8 * a;
x = 12 * (a - b);
if ((h2 - h1) % x == 0)
cout << (h2 - h1) / x;
else
cout << ((h2 - h1) / x) + 1;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b, t;
int main() {
scanf("%d%d%d%d", &h1, &h2, &a, &b);
t = 0;
if (h1 + a * 8 < h2) {
h1 += a * 8;
h1 -= 12 * b;
++t;
while (1) {
h1 += a * 12;
if (h1 >= h2) break;
h1 -= 12 * b;
++t;
if (a <= b) {
t = -1;
break;
}
}
}
printf("%d", t);
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b, t;
int main() {
scanf("%d%d%d%d", &h1, &h2, &a, &b);
t = 0;
if (h1 + a * 8 < h2) {
h1 += a * 8;
h1 -= 12 * b;
++t;
while (1) {
h1 += a * 12;
if (h1 >= h2) break;
h1 -= 12 * b;
++t;
if (a <= b) {
t = -1;
break;
}
}
}
printf("%d", t);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, h1, h2;
while (cin >> h1 >> h2 >> a >> b) {
if (b >= a) {
if (8 * a + h1 < h2)
cout << -1 << endl;
else
cout << 0 << endl;
} else {
if ((h2 - h1 - 8 * a) <= 0)
cout << 0 << endl;
else {
if ((h2 - h1 - 8 * a) % (12 * (a - b)) == 0)
cout << (h2 - h1 - 8 * a) / (12 * (a - b)) << endl;
else
cout << (h2 - h1 - 8 * a) / (12 * (a - b)) + 1 << endl;
}
}
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, h1, h2;
while (cin >> h1 >> h2 >> a >> b) {
if (b >= a) {
if (8 * a + h1 < h2)
cout << -1 << endl;
else
cout << 0 << endl;
} else {
if ((h2 - h1 - 8 * a) <= 0)
cout << 0 << endl;
else {
if ((h2 - h1 - 8 * a) % (12 * (a - b)) == 0)
cout << (h2 - h1 - 8 * a) / (12 * (a - b)) << endl;
else
cout << (h2 - h1 - 8 * a) / (12 * (a - b)) + 1 << endl;
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
static void redirect(int argc, const char **argv) {
ios::sync_with_stdio(false);
cin.tie(NULL);
if (argc > 1) {
static filebuf f;
f.open(argv[1], ios::in);
cin.rdbuf(&f);
if (!cin) {
cerr << "Failed to open '" << argv[1] << "'" << endl;
exit(1);
}
}
if (argc > 2) {
static filebuf f;
f.open(argv[2], ios::out | ios::trunc);
cout.rdbuf(&f);
if (!cout) {
cerr << "Failed to open '" << argv[2] << "'" << endl;
}
}
cin.exceptions(ios::failbit);
}
static void solve() {
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
if (h1 >= h2)
cout << "0\n";
else if (a <= b)
cout << "-1\n";
else {
long long g = (a - b) * 12;
long long ans = (h2 - h1 + g - 1) / g;
cout << ans << '\n';
}
}
int main(int argc, const char **argv) {
redirect(argc, argv);
solve();
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
static void redirect(int argc, const char **argv) {
ios::sync_with_stdio(false);
cin.tie(NULL);
if (argc > 1) {
static filebuf f;
f.open(argv[1], ios::in);
cin.rdbuf(&f);
if (!cin) {
cerr << "Failed to open '" << argv[1] << "'" << endl;
exit(1);
}
}
if (argc > 2) {
static filebuf f;
f.open(argv[2], ios::out | ios::trunc);
cout.rdbuf(&f);
if (!cout) {
cerr << "Failed to open '" << argv[2] << "'" << endl;
}
}
cin.exceptions(ios::failbit);
}
static void solve() {
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
h1 += 8 * a;
if (h1 >= h2)
cout << "0\n";
else if (a <= b)
cout << "-1\n";
else {
long long g = (a - b) * 12;
long long ans = (h2 - h1 + g - 1) / g;
cout << ans << '\n';
}
}
int main(int argc, const char **argv) {
redirect(argc, argv);
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long LINF = 1e18;
const long double EPS = 1e-9;
const long double PI = 3.141592653589;
const long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
float tbegin = clock();
srand(42);
if ("" != "") {
freopen(
""
".in",
"r", stdin);
freopen(
""
".out",
"w", stdout);
}
int h1, h2;
cin >> h1 >> h2;
int a, b;
cin >> a >> b;
int h = h1;
int cur = 15;
int day = 0;
while (day < 1000000) {
if (11 <= cur && cur <= 22) {
h += a;
} else {
h -= b;
}
if (h >= h2) {
cout << day << "\n";
return 0;
}
if (a <= b && day > 1) {
cout << "-1\n";
return 0;
}
cur++;
if (cur >= 24) {
cur -= 24;
day++;
}
}
cout << "-1\n";
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long LINF = 1e18;
const long double EPS = 1e-9;
const long double PI = 3.141592653589;
const long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
float tbegin = clock();
srand(42);
if ("" != "") {
freopen(
""
".in",
"r", stdin);
freopen(
""
".out",
"w", stdout);
}
int h1, h2;
cin >> h1 >> h2;
int a, b;
cin >> a >> b;
int h = h1;
int cur = 15;
int day = 0;
while (day < 1000000) {
if (11 <= cur && cur <= 22) {
h += a;
} else {
h -= b;
}
if (h >= h2) {
cout << day << "\n";
return 0;
}
if (a <= b && day > 1) {
cout << "-1\n";
return 0;
}
cur++;
if (cur >= 24) {
cur -= 24;
day++;
}
}
cout << "-1\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int h = 14;
int d = 0;
int od;
int nd = 0;
while (h1 < h2) {
if (h >= 10 && h < 22)
h1 += a;
else {
h1 -= b;
}
h++;
if (h == 24) {
od = nd;
nd = h1;
d++;
h = 0;
if (d == 3 && od >= nd) {
cout << -1;
return 0;
}
}
}
cout << d;
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
int h = 14;
int d = 0;
int od;
int nd = 0;
while (h1 < h2) {
if (h >= 10 && h < 22)
h1 += a;
else {
h1 -= b;
}
h++;
if (h == 24) {
od = nd;
nd = h1;
d++;
h = 0;
if (d == 3 && od >= nd) {
cout << -1;
return 0;
}
}
}
cout << d;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
long long d = 1;
long long bh = h1;
h1 += 8 * a;
if (a <= b) {
if (h1 >= h2) {
cout << 0 << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
while (true) {
if (h1 >= h2) {
cout << d - 1 << endl;
return 0;
}
h1 += 12 * a;
h1 -= 12 * b;
d++;
}
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long h1, h2, a, b;
cin >> h1 >> h2 >> a >> b;
long long d = 1;
long long bh = h1;
h1 += 8 * a;
if (a <= b) {
if (h1 >= h2) {
cout << 0 << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
while (true) {
if (h1 >= h2) {
cout << d - 1 << endl;
return 0;
}
h1 += 12 * a;
h1 -= 12 * b;
d++;
}
}
```
|
#include <bits/stdc++.h>
int a, b, c, d, h;
int main() {
scanf("%d %d %d %d", &a, &b, &c, &d);
h = a + 8 * c;
if (h >= b) {
printf("0");
return 0;
}
if (c <= d) {
printf("-1");
return 0;
}
printf("%d",
(b - h) / (12 * (c - d)) + (((b - h) % (12 * (c - d)) == 0) ? 0 : 1));
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
int a, b, c, d, h;
int main() {
scanf("%d %d %d %d", &a, &b, &c, &d);
h = a + 8 * c;
if (h >= b) {
printf("0");
return 0;
}
if (c <= d) {
printf("-1");
return 0;
}
printf("%d",
(b - h) / (12 * (c - d)) + (((b - h) % (12 * (c - d)) == 0) ? 0 : 1));
return 0;
}
```
|
#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
long int h1, h2, a, b, number = 1;
scanf("%ld %ld", &h1, &h2);
scanf("%ld %ld", &a, &b);
int night = 12, day = 8;
if (day * a >= h2 - h1)
printf("0\n");
else if (a <= b)
printf("-1\n");
else {
while (a * day + number * (a - b) * night < h2 - h1) number++;
printf("%ld \n", number);
}
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
long int h1, h2, a, b, number = 1;
scanf("%ld %ld", &h1, &h2);
scanf("%ld %ld", &a, &b);
int night = 12, day = 8;
if (day * a >= h2 - h1)
printf("0\n");
else if (a <= b)
printf("-1\n");
else {
while (a * day + number * (a - b) * night < h2 - h1) number++;
printf("%ld \n", number);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b, k;
int main() {
cin >> h1 >> h2 >> a >> b;
if (h1 + 8 * a < h2 && a <= b) {
cout << "-1";
return 0;
}
h1 -= 4 * a;
for (;; k++) {
h1 += 12 * a;
if (h1 >= h2) {
cout << k;
break;
}
h1 -= 12 * b;
}
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b, k;
int main() {
cin >> h1 >> h2 >> a >> b;
if (h1 + 8 * a < h2 && a <= b) {
cout << "-1";
return 0;
}
h1 -= 4 * a;
for (;; k++) {
h1 += 12 * a;
if (h1 >= h2) {
cout << k;
break;
}
h1 -= 12 * b;
}
return 0;
}
```
|
#include <bits/stdc++.h>
int main() {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
b -= a + 8 * c;
c = (c - d) * 12;
if (b <= 0)
printf("0\n");
else if (c <= 0)
printf("-1\n");
else
printf("%d\n", (b + c - 1) / c);
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
int main() {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
b -= a + 8 * c;
c = (c - d) * 12;
if (b <= 0)
printf("0\n");
else if (c <= 0)
printf("-1\n");
else
printf("%d\n", (b + c - 1) / c);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, s = 0, a, m, b;
cin >> n >> m;
cin >> a >> b;
n += a * 8;
if (n >= m)
cout << 0;
else {
if (b >= a)
cout << -1;
else {
while (n < m) {
s++;
n = n - 12 * b;
n = n + 12 * a;
}
cout << s;
}
}
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, s = 0, a, m, b;
cin >> n >> m;
cin >> a >> b;
n += a * 8;
if (n >= m)
cout << 0;
else {
if (b >= a)
cout << -1;
else {
while (n < m) {
s++;
n = n - 12 * b;
n = n + 12 * a;
}
cout << s;
}
}
}
```
|
#include <bits/stdc++.h>
const int maxn = 1e5 + 7;
int ans = 0;
int main() {
int h1, h2, a, b;
scanf("%d%d%d%d", &h1, &h2, &a, &b);
int dis = h2 - h1;
if (a * 8 >= dis)
puts("0");
else {
ans++;
h1 += a * 8 - b * 12;
h1 += a * 12;
if (h1 > h2)
puts("1");
else if (b >= a)
puts("-1");
else {
h1 -= b * 12;
dis = h2 - h1;
int v = (a - b) * 12;
if (a * 12 >= dis)
puts("2");
else {
dis -= a * 12;
int T = dis / v + (dis % v != 0);
ans = ans + 1 + T;
printf("%d\n", ans);
}
}
}
}
|
### Prompt
Generate a cpp solution to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
const int maxn = 1e5 + 7;
int ans = 0;
int main() {
int h1, h2, a, b;
scanf("%d%d%d%d", &h1, &h2, &a, &b);
int dis = h2 - h1;
if (a * 8 >= dis)
puts("0");
else {
ans++;
h1 += a * 8 - b * 12;
h1 += a * 12;
if (h1 > h2)
puts("1");
else if (b >= a)
puts("-1");
else {
h1 -= b * 12;
dis = h2 - h1;
int v = (a - b) * 12;
if (a * 12 >= dis)
puts("2");
else {
dis -= a * 12;
int T = dis / v + (dis % v != 0);
ans = ans + 1 + T;
printf("%d\n", ans);
}
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int ch = 0, h, h1, p = 0, h2, a, b, i, first = 0, days;
cin >> h1 >> h2 >> a >> b;
h = h2 - h1;
for (i = 1; i <= 8; i++) {
ch += a;
if (ch >= h) {
cout << "0\n";
first = 1;
p = 1;
break;
}
}
if (b >= a && first == 0) {
cout << "-1\n";
first = 1;
p = 1;
}
days = 1;
while (first == 0) {
for (i = 1; i <= 12; i++) ch -= b;
for (i = 1; i <= 12; i++) {
ch += a;
if (ch >= h) first = 1;
}
if (first == 0) days++;
}
if (p == 0) cout << days << "\n";
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int ch = 0, h, h1, p = 0, h2, a, b, i, first = 0, days;
cin >> h1 >> h2 >> a >> b;
h = h2 - h1;
for (i = 1; i <= 8; i++) {
ch += a;
if (ch >= h) {
cout << "0\n";
first = 1;
p = 1;
break;
}
}
if (b >= a && first == 0) {
cout << "-1\n";
first = 1;
p = 1;
}
days = 1;
while (first == 0) {
for (i = 1; i <= 12; i++) ch -= b;
for (i = 1; i <= 12; i++) {
ch += a;
if (ch >= h) first = 1;
}
if (first == 0) days++;
}
if (p == 0) cout << days << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline long long lin() {
long long x;
scanf("%lld", &x);
return x;
}
inline int mul(int a, int b) {
long long x = a;
x *= (long long)b;
if (x >= 1000000007) x %= 1000000007;
return x;
}
inline int add(int a, int b) {
return (a + b) >= 1000000007 ? a + b - 1000000007 : a + b;
}
inline int sub(int a, int b) {
return (a - b) < 0 ? 1000000007 - b + a : a - b;
}
inline long long pow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a;
b = b >> 1;
a = a * a;
}
return res;
}
const int MAX = INT_MAX;
const int MIN = INT_MIN;
int main() {
int x, y, a, b;
cin >> x >> y >> a >> b;
double res = 0;
if ((x + (8 * a)) >= y) {
cout << 0;
return 0;
}
if (a == b) {
cout << -1;
return 0;
}
res = (((double)((y - x) - (8 * a))) / ((double)(12 * (a - b))));
if (res < 0)
cout << -1;
else {
res = ceil(res);
cout << res;
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline long long lin() {
long long x;
scanf("%lld", &x);
return x;
}
inline int mul(int a, int b) {
long long x = a;
x *= (long long)b;
if (x >= 1000000007) x %= 1000000007;
return x;
}
inline int add(int a, int b) {
return (a + b) >= 1000000007 ? a + b - 1000000007 : a + b;
}
inline int sub(int a, int b) {
return (a - b) < 0 ? 1000000007 - b + a : a - b;
}
inline long long pow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a;
b = b >> 1;
a = a * a;
}
return res;
}
const int MAX = INT_MAX;
const int MIN = INT_MIN;
int main() {
int x, y, a, b;
cin >> x >> y >> a >> b;
double res = 0;
if ((x + (8 * a)) >= y) {
cout << 0;
return 0;
}
if (a == b) {
cout << -1;
return 0;
}
res = (((double)((y - x) - (8 * a))) / ((double)(12 * (a - b))));
if (res < 0)
cout << -1;
else {
res = ceil(res);
cout << res;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b;
int main() {
cin >> h1 >> h2;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2)
cout << "0";
else {
if (a <= b)
cout << "-1";
else {
h1 -= 12 * b;
for (int i = 1; i <= 10000; i++) {
h1 += 12 * a;
if (h1 >= h2) {
cout << i;
break;
}
h1 -= 12 * b;
}
}
}
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
Examples
Input
10 30
2 1
Output
1
Input
10 13
1 1
Output
0
Input
10 19
1 2
Output
-1
Input
1 50
5 4
Output
1
Note
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int h1, h2, a, b;
int main() {
cin >> h1 >> h2;
cin >> a >> b;
h1 += 8 * a;
if (h1 >= h2)
cout << "0";
else {
if (a <= b)
cout << "-1";
else {
h1 -= 12 * b;
for (int i = 1; i <= 10000; i++) {
h1 += 12 * a;
if (h1 >= h2) {
cout << i;
break;
}
h1 -= 12 * b;
}
}
}
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.