Checkers HDU - 3830
Little X, Little Y and Little Z are playing checkers when Little Y is annoyed. So he wants to make the chessboard much bigger. Although Little Z insists the original version, Little X stands by Little Y. After they enlarge the chessboard, the chessboard turns to an infinite line.
The chessboard is like the Number Axes now, with each integer point able to hold a checker. At initial status there are three checkers on three different integer points , and through the game there always are three checkers. Every time, they can choose a checker A to jump across a pivot checker B to a new position(but the distance between old A and B equals to new A and B, and there should be no other checkers except B in the range [old A, new A]).
After playing for a while, they wonder whether an given status a,b,c can be transferred to x,y,z. obeying the rules. Since the checkers are considered the same, it is unnecessary for a must jump to x.
The chessboard is like the Number Axes now, with each integer point able to hold a checker. At initial status there are three checkers on three different integer points , and through the game there always are three checkers. Every time, they can choose a checker A to jump across a pivot checker B to a new position(but the distance between old A and B equals to new A and B, and there should be no other checkers except B in the range [old A, new A]).
After playing for a while, they wonder whether an given status a,b,c can be transferred to x,y,z. obeying the rules. Since the checkers are considered the same, it is unnecessary for a must jump to x.
InputThe first line is a,b,c.
The second line is x,y,z.
They are all integers in range (-10^9, 10^9) and the two status are valid.
OutputThe first line is YES or NO, showing whether the transfer can be achieved.
If it is YES, then output the least steps in the second line.
Sample Input
1 2 3 0 3 5
Sample Output
YES 2
Hint
The middle checker jumps to position 0, and the status is 0 1 3 Then , the middle one jumps to 5.
题意:有三个点,一个点能跳到与之相邻的点的对称位置当且仅当中间没有第三个点存在。
题解:详解!!!
1 // ConsoleApplication1.cpp: 定义控制台应用程序的入口点。 2 // 3 4 //#include "stdafx.h" 5 #include<cstdio> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std; 10 11 #define ll __int64 12 13 struct State { ll x, y, z, step; }; 14 15 State S, T; 16 17 inline bool cmp_state(State a, State b) { 18 if (a.x == b.x && a.y == b.y && a.z == b.z) return true; 19 return false; 20 } 21 22 inline ll Abs(ll x) { 23 return x > 0LL ? x : -x; 24 } 25 26 inline void Sort(State& a) { 27 if (a.y < a.x) swap(a.x, a.y); 28 if (a.z < a.x) swap(a.x, a.z); 29 if (a.z < a.y) swap(a.y, a.z); 30 } 31 32 State Root(State& a) { 33 State tp = a; 34 tp.step = 0; 35 ll step = 0; 36 while (Abs(tp.x - tp.y) != Abs(tp.y - tp.z)) { 37 ll len1 = Abs(tp.x - tp.y); 38 ll len2 = Abs(tp.y - tp.z); 39 if (len2 > len1) { 40 ll c = (len2 - 1) / len1; 41 step += c; 42 tp.y += c*len1; 43 tp.x += c*len1; 44 } 45 else { 46 ll c = (len1 - 1) / len2; 47 step += c; 48 tp.y -= c*len2; 49 tp.z -= c*len2; 50 } 51 } 52 a.step = step; 53 return tp; 54 } 55 56 void update(State& a, ll delta) { 57 ll count = 0; 58 while (count < delta) { 59 ll len1 = Abs(a.x - a.y); 60 ll len2 = Abs(a.y - a.z); 61 ll k = Abs(count - delta); 62 if (len1 < len2) { 63 ll c = (len2 - 1) / len1; 64 ll Min = min(k, c); 65 a.x += Min*len1; 66 a.y += Min*len1; 67 count += Min; 68 if (Min == k) break; 69 } 70 else { 71 ll c = (len1 - 1) / len2; 72 ll Min = min(k, c); 73 a.y -= Min*len2; 74 a.z -= Min*len2; 75 count += Min; 76 if (Min == k) break; 77 } 78 } 79 a.step -= delta; 80 } 81 82 ll Solve() { 83 State tS, tT; 84 ll low = 0, high = S.step; 85 while (low <= high) { 86 ll mid = (low + high) >> 1; 87 ll delta = S.step - mid; 88 tS = S; tT = T; 89 update(tS, delta); 90 update(tT, delta); 91 if (!cmp_state(tS, tT)) 92 high = mid - 1; 93 else 94 low = mid + 1; 95 } 96 return 2 * (S.step - high); 97 } 98 99 int main() 100 { 101 while (scanf_s("%I64d%I64d%I64d", &S.x, &S.y, &S.z) != EOF) { 102 scanf_s("%I64d%I64d%I64d", &T.x, &T.y, &T.z); 103 S.step = T.step = 0; 104 Sort(S); Sort(T); 105 State RS = Root(S); 106 State RT = Root(T); 107 if (!cmp_state(RS, RT)) { 108 printf_s("NO\n"); 109 continue; 110 } 111 ll temp = Abs(S.step - T.step); 112 if (S.step > T.step) 113 update(S, S.step - T.step); 114 else 115 update(T, T.step - S.step); 116 ll ans = Solve(); 117 printf_s("YES\n"); 118 printf_s("%I64d\n", ans + temp); 119 } 120 return 0; 121 }