由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
CS版 - 请教一道算法题
相关主题
some question about ucon问一个run bash-script command several times的问题
shortest path algorithm(dijkstra)的变形【求教】Text Indexer for Large Volume of ASCII files【先谢】
[转载] How to implement the "Contour" command标识符真的不能带空格么?
MPI问题求助。Help! (转载)Part 2 的解答
java 里可以插入linux command吗? (转载)[合集] 好吧, 我来个流氓揭发 :p
在linux 和 Unix 上做 C/C++ 有差别吗?linear programming里面的dual problem一般怎么求啊?
what's the dos equivalent of unix "tail" command?This Woman is really cute
how to shut down pbs server or kill all my jobs (转载)如何找到两点之间所有的路径?
相关话题的讨论汇总
话题: command话题: values话题: after话题: commands话题: 11
进入CS版参与讨论
1 (共1页)
w**s
发帖数: 5
1
请教一道算法题 看起来毫无头绪啊
You are given two integers L and R. Their values are initially set to L = 0
and R = 1.
You can change their values by performing the following commands:
the command 'L' changes the value of L to 2 * L - R;
the command 'R' changes the value of R to 2 * R - L.
You are given an integer N. The goal is to find the minimum number of
commands after which L = N or R = N.
For example, consider N = -11: the shortest sequence of commands required to
set either L or R to -11 is ['L', 'L', 'R', 'L']. After each command, the
values of L and R are as follows:
initial values: L = 0, R = 1;
command 'L': L = -1, R = 1;
command 'L': L = -3, R = 1;
command 'R': L = -3, R = 5;
command 'L': L = -11, R = 5.
After the fourth command we get L = N.
expected worst-case time complexity is O(log(N));
expected worst-case space complexity is O(1).
时间复杂度要求O(log(N)) 这个是在提示什么算法啊
谢谢
I******c
发帖数: 163
2
想了好一阵子,开始以为是动态规划,后来发现时间复杂度不对。
观察了一下题目的例子,有以下结果:
1. 如果第一步走L,那么以后无论走L还是走R,L,R都是奇数。 如果第一步走R,以后
L,R都是偶数
2. L, R 其中是一个正数,另一个负数,而且都是各自向坐标轴的两端增长
3. 增长的幅度是 2^0,2^1,2^2,2^3...
所以,以N=-11为例,因为-11是奇数,所以第一步走L,这个时候L,R分别是-1,1.
-1和-11相差10. 10的二进制是101,也就是从-1到-11,需要的增长幅度是2和8,用
L, R表示,就是LRL,加上第一步是L,所以总的步骤就是LLRL.
w**s
发帖数: 5
3
请教一道算法题 看起来毫无头绪啊
You are given two integers L and R. Their values are initially set to L = 0
and R = 1.
You can change their values by performing the following commands:
the command 'L' changes the value of L to 2 * L - R;
the command 'R' changes the value of R to 2 * R - L.
You are given an integer N. The goal is to find the minimum number of
commands after which L = N or R = N.
For example, consider N = -11: the shortest sequence of commands required to
set either L or R to -11 is ['L', 'L', 'R', 'L']. After each command, the
values of L and R are as follows:
initial values: L = 0, R = 1;
command 'L': L = -1, R = 1;
command 'L': L = -3, R = 1;
command 'R': L = -3, R = 5;
command 'L': L = -11, R = 5.
After the fourth command we get L = N.
expected worst-case time complexity is O(log(N));
expected worst-case space complexity is O(1).
时间复杂度要求O(log(N)) 这个是在提示什么算法啊
谢谢
I******c
发帖数: 163
4
想了好一阵子,开始以为是动态规划,后来发现时间复杂度不对。
观察了一下题目的例子,有以下结果:
1. 如果第一步走L,那么以后无论走L还是走R,L,R都是奇数。 如果第一步走R,以后
L,R都是偶数
2. L, R 其中是一个正数,另一个负数,而且都是各自向坐标轴的两端增长
3. 增长的幅度是 2^0,2^1,2^2,2^3...
所以,以N=-11为例,因为-11是奇数,所以第一步走L,这个时候L,R分别是-1,1.
-1和-11相差10. 10的二进制是101,也就是从-1到-11,需要的增长幅度是2和8,用
L, R表示,就是LRL,加上第一步是L,所以总的步骤就是LLRL.
1 (共1页)
进入CS版参与讨论
相关主题
如何找到两点之间所有的路径?java 里可以插入linux command吗? (转载)
请教一np问题在linux 和 Unix 上做 C/C++ 有差别吗?
求一篇文章 (in Lecture Notes in Maths) (转载)what's the dos equivalent of unix "tail" command?
问问Boost library, 尤其是Boost Graph Library (BGL)how to shut down pbs server or kill all my jobs (转载)
some question about ucon问一个run bash-script command several times的问题
shortest path algorithm(dijkstra)的变形【求教】Text Indexer for Large Volume of ASCII files【先谢】
[转载] How to implement the "Contour" command标识符真的不能带空格么?
MPI问题求助。Help! (转载)Part 2 的解答
相关话题的讨论汇总
话题: command话题: values话题: after话题: commands话题: 11