题解:P12464 『FCRT / 1 - 1』Seats

· · 题解

题解

分析题意

如样例1:

第一步,使 $\min(|x-1|)$ 最大,取 $x=6$,$S=\{1,6\}$; 第二步,使 $\min(|x-1|,|x-6|)$ 最大,实数 $x$ 取 $3.5$,整数 $x$ 即取 $3$ 或 $4$,题目要求**多个满足条件的 $x$,选择其中最小的一个**,故取 $x=3$,$S=\{1,3,6\}$; 最后,无论取 $x=2,4,5$,$\min(|x-1|,|x-3|,|x-6|)$ 均为 $1$。 即最终集合 $S=\{1,3,6\}$。 --- ## 思路 注意到对于第一次,必取 $N(N>=3)$,而随后的每一次,取区间中点向下取整的值即可,以 $N=12$ 为例: **1** 2 3 4 5 6 7 8 9 10 11 **12**(先取首尾); **1** 2 3 4 5 **6** 7 8 9 10 11 **12**($[1,12]$ 中点 $6.5$ 向下取整); **1** 2 **3** 4 5 **6** 7 8 **9** 10 11 **12**(新区间 $[1,6]$ 与 $[6,12]$)。 --- 若设 $f(x)$ 为 $N=x$ 时 $S$ 的大小, 不难发现,$f(12)=f(6)+f(7)-1$,即 $[1,6]$ 与 $[6,12]$ 的和减去重叠的 $6$,即有 $f(x)=f(\lfloor\frac{x+1}{2}\rfloor)+f(\lfloor\frac{x}{2}\rfloor+1)-1$。 结合 $f(1)=f(2)=1,f(3)=f(4)=2$,可写出递推/递归代码。 ## 递推代码: ``` #include<bits/stdc++.h> using namespace std; const int N=1e6+10; int f[N]={0,1,1,2,2}; int main() { int n; scanf("%d",&n); for(int i=5;i<=n;++i) f[i]=f[(i+1)/2]+f[i/2+1]-1; // for(int i=1;i<=n;++i) // printf("%d %d\n",i,f[i]); printf("%d",f[n]); return 0; } ``` 时空复杂度均为 $O(N)$,可得 $40pts$。 --- ## 观察并优化 用以上程序观察,当 $x:5 \to 49$,$f(x)=

3 3 3(3')
4(1')
5 5 5 5 5(5')
6 7 8(3')
9 9 9 9 9 9 9 9 9(9')
10 11 12 13 14 15 16(7')
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17(17')。

注意到,
当 x∈[2^{n+1}+1,2^{n+1}+2^n+1](或 [2 \cdot \ 2^n+1,3 \cdot \ 2^n+1])时,f(x)=2^n+1;
而当 x∈[3 \cdot \ 2^n+1,2^{n+2}] 时,f(x)=x-2^{n+1};
对 x∈[1,4] 进行特判,则有

AC code:

#include<bits/stdc++.h>
using namespace std;
long long p[64]={1};//p[n]表示2^n
int a[5]={0,1,1,2,2};
int main()
{
    for(int i=1;i<=62;++i)
        p[i]=p[i-1]*2;
    p[63]=LONG_LONG_MAX;//2^63用LONG_LONG_MAX代替
    long long n;
    scanf("%lld",&n);
    if(n<=4)cout<<a[n];
    else
    {
        int i;
        for(i=1;i<=61;++i)
            if(n<=p[i+2])
                break;//遍历确定2^n中的n
        if(n<=3*p[i]+1)printf("%lld",p[i]+1);
        else printf("%lld",n-p[i+1]);
    }
    return 0;
}

时空复杂度均为 O(logN)。