Walking Between Houses ( CodeForces 1015D ) ( 贪心 )

本文解析了CodeForces1015D WalkingBetweenHouses题目,介绍了问题背景及要求,通过分析给出了两种解题思路并附上了对应的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎访问https://blog.youkuaiyun.com/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

 

 

Walking Between Houses ( CodeForces 1015D ) 

 

 

Description

There are nn houses in a row. They are numbered from 11 to nnin order from left to right. Initially you are in the house 11.

You have to perform kk moves to other house. In one move you go from your current house to some other house. You can't stay where you are (i.e., in each move the new house differs from the current house). If you go from the house xx to the house yy, the total distance you walked increases by |x−y||x−y| units of distance, where |a||a| is the absolute value of aa. It is possible to visit the same house multiple times (but you can't visit the same house in sequence).

Your goal is to walk exactly ss units of distance in total.

If it is impossible, print "NO". Otherwise print "YES" and any of the ways to do that. Remember that you should do exactly kk moves.

Input

The first line of the input contains three integers nn, kk, ss (2≤n≤1092≤n≤109, 1≤k≤2⋅1051≤k≤2⋅105, 1≤s≤10181≤s≤1018) — the number of houses, the number of moves and the total distance you want to walk.

Output

If you cannot perform kk moves with total walking distance equal to ss, print "NO".

Otherwise print "YES" on the first line and then print exactly kk integers hihi (1≤hi≤n1≤hi≤n) on the second line, where hihi is the house you visit on the ii-th move.

For each jj from 11 to k−1k−1 the following condition should be satisfied: hj≠hj+1hj≠hj+1. Also h1≠1h1≠1 should be satisfied.

Examples

Input

10 2 15

Output

YES
10 4 

Input

10 9 45

Output

YES
10 1 10 1 2 1 2 1 6 

Input

10 9 81

Output

YES
10 1 10 1 10 1 10 1 10 

Input

10 9 82

Output

NO

 

题意:

一排紧挨着的房子,标号1~n,你目前在第1个房子的位置,移动 k 次,要求每次必须移动不能停在原地,并且移动的距离为两点坐标差的绝对值(如从 x 移动至 y 则移动的距离为 |x-y| ,问能否使得移动 k 次的总距离和为s,第一行输出若能则输出YES,不能则输出NO,第二行输出走 k步 后分别的位置,若有多种情况选择任意一种即可。

 

思路:

本题主要用贪心的思想。

1.首先考虑不能的情况,分为两种:

1)k > s ,即步数大于移动的距离,由于每次都不得停留在原地,所以每次移动的距离至少为 1,则 k 步至少也会移动 k 个距离。

2)k * ( n - 1 ) < s ,即 k 步每步都走了最大的距离,若还达不到距离 s ,则此种情况不成立。

2.剩下的就是的情况,这里介绍两种思路:

1)尽可能先走最大的距离,再将剩下的步数拆分。

2)将s步均分,每次都走平均距离。

 

代码:( 思路一 ) 

#include<cstdio>
#include<algorithm>

using namespace std;

int main()
{
    long long int n,k,s,x,y;
    while(~scanf("%lld%lld%lld",&n,&k,&s))
    {
        y=1;
        if((k>s)||(k*(n-1)<s))
            printf("NO\n");
        else
        {
            printf("YES\n");
            while(k>0)
            {
                x=min(n-1,s-(k-1));
                if(y>x)
                    y-=x;
                else
                    y+=x;
                printf("%lld ",y);
                s-=x;
                k--;
            }
        }
    }
    return 0;
}

 

代码:( 思路二 ) 

#include<cstdio>
int main()
{
    long long int n,m,k,res=1;
    scanf("%lld%lld%lld",&n,&m,&k);
    if(k>(n-1)*m||k<m)
        printf("NO\n");
    else
    {
        printf("YES\n");
        int a=k/m,b=k%m;
        for(int i=0;i<m;i++)
            if(i%2==0)
            {
                res+=a;
                if(b)
                    res++,b--;
                printf("%lld ",res);
            }
            else
            {
                res-=a;
                if(b)
                    res--,b--;
                printf("%lld ",res);
            }
    }
    return 0;
}

 

 

 

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值