CF2040B Paint a Strip 题解

· · 题解

思路

我们已知尽量使用操作 2 给序列赋值。因为想使用操作 2 就必须左右两个端点为 1,且 x \times 2 + 2 是能够满足要求的最远的距离,所以当序列前 x 个数都已赋为 1 时,我们先对 a_{x \times 2 + 2} 使用操作 1,再以 a_x 为左端点,a_{x \times 2 + 2} 为右端点使用操作 2 时,能够使操作 1 尽量少。

代码

#include<bits/stdc++.h>
using namespace std;
int t;//多测
void solve(int t){
    while(t--){
        int n;
        scanf("%d", &n);
        int ans = 1;
        for(int x = 1; x < n; x *= 2, x += 2) ans++;//核心代码
        printf("%d\n", ans);//记得换行
    }
}
int main(){
    scanf("%d", &t);
    solve(t);
    return 0;
}