当前位置:网站首页>Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]

Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]

2022-07-07 21:00:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

Topic link :Codeforces Round #275 (Div. 2) C – Diverse Permutation

The question : A string of 1~n. Find the number of absolute values of the difference between two adjacent terms in a sequence ( The number of different absolute values ) by k individual . Find sequence 、

Ideas :1~k+1. Construction sequence front segment , Then directly output the remaining number . The previous structure can be based on , The absolute value of the difference between the two terms is 1~k structure .

AC Code :

#include <stdio.h>
#include <string.h>
int ans[200010];
bool vis[100010];
int n,mark;
int iabs(int a)
{
    if(a<0) return -a;
    return a;
}
int main()
{
    int i,cnt,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int x,y;
        memset(vis,0,sizeof vis);
        ans[0]=1;
        x=1,y=k+1;
        cnt=k;
        for(i=1; i<=k; i++,cnt--)
        {
            int temp=ans[i-1]+cnt;
            if(temp>k+1)
                temp=ans[i-1]-cnt;
            else if(vis[temp])
                temp=ans[i-1]-cnt;
            ans[i]=temp;
            vis[temp]=true;
        }
        for(i=k+1; i<n; i++)
            ans[i]=i+1;
        for(i=0; i<n-1; i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[i]);
    }
    return 0;
}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116287.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072056574982.html