如果只需要使用数字dp和二进制搜索,那么答案的起点似乎是正确的。但我不明白
upperbound
lowerbound
用于或甚至是必需的。
下面是在生成函数后我将如何做这个问题
F()
binary_search(int start,int end,int k){
int mid=(start+end)/2;
//mid is answer only if F(mid) has exactly k numbers and F(mid-1) has k-1 numbers
if(F(mid)==k&&F(mid-1)==k-1) return mid;
//if F(mid) has less than k numbers we need to check for greater numbers for answer
else if(F(mid)<k) return binary_search(mid+1,end,k);
//finally if F(mid) has more than or equal to k numbers we check smaller numbers for answer
else return binary_search(start,mid-1,k);
}
(琐碎的建议)当你做函数时
F(N)
您应该使其能够计算和存储
F(x)
x
属于范围
[1,N]
.