45pts 求助
  • 板块P1852 跳跳棋
  • 楼主_lyx1311_
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/5/31 21:14
  • 上次更新2023/11/4 22:26:56
查看原帖
45pts 求助
490378
_lyx1311_楼主2021/5/31 21:14

不知道为什么, TLE 45pts.

代码:

#include<bits/stdc++.h>
using namespace std;

struct State{
	int x, y, z;
	
	State(int X = 0, int Y = 0, int Z = 0) : x(X), y(Y), z(Z) {}
}st, ed;

inline int depth(State& a){
	int depth = 0;
	
	while(a.y - a.x != a.z - a.y){
		int d1 = a.y - a.x, d2 = a.z - a.y;
		
		if(d1 < d2){
			int t = (d2 - 1) / d1;
			
			a.x += d1 * t;
			a.y += d1 * t;
			
			depth += t;
		}
		else{
			int t = (d1 - 1) / d2;
			
			a.y -= d2 * t;
			a.z -= d2 * t;
			
			depth += t;
		}
	}
	
	return depth;
}

inline bool same(State& a, State& b){
	return (a.x == b.x) && (a.y == b.y) && (a.z == b.z);
}

inline void Run(State& a, int k){
	while(k > 0 && a.y - a.x != a.z - a.y){
		int d1 = a.y - a.x, d2 = a.z - a.y;
		
		if(d1 < d2){
			int t = (d2 - 1) / d1;
			t = min(t, k);
			
			a.x += d1 * t;
			a.y += d1 * t;
			
			k -= t;
		}
		else{
			int t = (d1 - 1) / d2;
			t = min(t, k);
			
			a.y -= d2 * t;
			a.z -= d2 * t;
			
			k -= t;
		}
	}
}

inline void work(State st, State ed){
	State X = st, Y = ed;
	int depth_s = depth(X), depth_e = depth(Y);
	
	if(same(X, Y) == false){
		puts("NO");
		return;
	}
	
	puts("YES");
	
	int T = abs(depth_s - depth_e);
	if(depth_s < depth_e)
		Run(ed, T);
	else
		Run(st, T);
	
	int L = 0, R = min(depth_s, depth_e), ans;
	while(L <= R){
		int mid = (L + R) / 2;
		
		State X0 = st, Y0 = ed;
		Run(X0, mid), Run(Y0, mid);
		
		if(same(X0, Y0))
			ans = mid, R = mid - 1;
		else
			L = mid + 1;
	}
	
	cout << T + 2 * ans << endl;
}

int main(){
	cin >> st.x >> st.y >> st.z;
	cin >> ed.x >> ed.y >> ed.z;
	
	work(st, ed);
	
	return 0;
}
2021/5/31 21:14
加载中...