Codeforces1059C. Sequence Transformation题解

C. Sequence Transformation

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Let's call the following process a transformation of a sequence of length n .

If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of n integers: the greatest common divisors of all the elements in the sequence before each deletion.

You are given an integer sequence 1,2,...n. Find the lexicographically maximum result of its transformation.

A sequence a[1],a[2],...,a[n] is lexicographically larger than a sequence b[1],b[2],...,b[n], if there is an index i such that a[j]=b[j] for all j < i , and a[i] > b[i].

http://codeforces.com/contest/1059/problem/C

【题解】

首先有一个显而易见的结论:对于任意排列,b序列中的前n/2个数总是1。
原因是相邻的两个数互质。
现在我们来考虑一个长度大于3的排列,怎样才能将b序列的字典序最大。
根据字典序的定义,我们需要最大化每一次进入队列的gcd。
即,当前最大就是整体最好的选择。
那么我们对于任意两个相邻的数,都有一个选择:删除其中的奇数或删除其中的偶数。
我们应该全部删除奇数或者全部删除偶数,不然就会有两个数相邻,从而出现gcd=1使得答案不优。
如果我们删除偶数,我们就会发现有ai=1使得这个序列的gcd=1而使得答案不优。
所以我们对于每个序列,都要删除奇数,使得gcd*=2。
观察操作后的序列
    a{2,4,...,(n&1)?(n-1):(n)}
我们可以将它看成
    a{2*1,2*2,2*3,...,2*(n/2)}
这样我们就把问题转化成了一个长度为n/2的子问题。
对于1<=n<=3的序列或长度小于等于3的子问题,题目的样例就是我们想要的结果,特判即可。
复杂度实际上是O(n)的。
代码并不难写。

【AC代码】

/*
    Coded by Apojacsleam
*/
#include<cstdio>
int n,t;
int main()
{
    scanf("%d",&n);t=1;
    while(n>3)
    {
        for(register int i=1;i<=((n+1)>>1);i++) printf("%d ",t);
        n>>=1;t<<=1;
    }
    if(n==3)  printf("%d %d %d",t,t,t*3);
    else if(n==2) printf("%d %d",t,t*2);
    else printf("%d",t);
    return 0; 
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值