find the upper bound of j. Suppose Mr. Kitayuta always performs the "l?+?1" jump (l: the length of the previous jump). Then, he will reach the end of the islands before he performs a jump of length d?+?246, because
d?+?(d?+?1)?+?(d?+?2)?+?...?+?(d?+?245)?≥?1?+?2?+?...?+?245?=?245?(245?+?1)?/?2?=?30135?>?30000. Thus, he will never be able to perform a jump of length d?+?246 or longer. Next, let us consider the lower bound of j in a similar way. If d?≤?246, then obviously he will not be able to perform a jump of length d?-?246 or shorter, because the length of a jump must be positive. Suppose Mr. Kitayuta always performs the "l?-?1" jump, where d?≥?247. Then, again he will reach the end of the islands before he performs a jump of length d?-?246, because
d?+?(d?-?1)?+?(d?-?2)?+?...?+?(d?-?245)?≥?245?+?244?+?...?+?1?=?245?(245?+?1)?/?2?=?30135?>?30000. Thus, he will never be able to perform a jump of length d?-?246 or shorter.
Therefore, we have obtained a working solution: similar to the O(m2) one, but we will only consider the value of j between d?-?245 andd?+?245. The time and memory complexity of this solution will be O(m1.5), since the value "245" is slightly larger than
.<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ClRoaXMgc29sdXRpb24gY2FuIGJlIGltcGxlbWVudGVkIGJ5LCBmb3IgZXhhbXBsZSwgdXNpbmcgYSA="normal" two dimensional array with a offset like this: dp[i][j - offset]. The time limit is set tight in order to fail most of naive solutions with search using std::map or something, so using hash maps (unordered_map) will be risky although the complexity will be the same as the described solution.
[End]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include