codeforces D. Walking Between Houses(思维+贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwwlps/article/details/81700028

题意:给出n,k,s,初始位置为1,在x坐标轴上可到达的最远位置为n,问恰好走k步能否使得所走距离的和刚好等于s

D. Walking Between Houses

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn houses in a row. They are numbered from 11 to nn in 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 kkmoves.

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

Copy

10 2 15

output

Copy

YES
10 4 

input

Copy

10 9 45

output

Copy

YES
10 1 10 1 2 1 2 1 6 

input

Copy

10 9 81

output

Copy

YES
10 1 10 1 10 1 10 1 10 

input

Copy

10 9 82

output

Copy

NO

题解:一开始我的想法就是,寻求一个关系式(n-1)*x+(k-x)m  =s 来下手,后来找不到什么方法,但可以确定是个思维题

分析: 
每次至少移动一个单位的距离,至多移动n-1个单位的距离,所以要想完成上述要求每次决策前后一定要满足条件: 
k <= s && k*(n-1) >= s

假设当前决策为移动x单位的距离,还有k步可以走,还需要走s距离,所以要满足下述条件: 
(k-1 <= s-x) && (x <= n-1)

得:max(x) = min( (s - k + 1) , (n-1) ) 
因此我们的决策为:每次移动的大小为 s与k的差值 和 n-1 中的较小值,即每次可以移动的x最大值,使得s和所走距离

尽快的相等。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include <vector>
#include<queue>
#include <stack>
#include <map>
#define maxn 100005
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
LL n,k,s;
struct node
{

};
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>k>>s;
    LL t1=0;
    t1=k*(n-1);
    if(s>t1||k>s)
    {
       cout << "NO\n";
    }
    else
    {
       cout << "YES\n";
       LL x,last;
       LL cnt=0;
       last=1;
       while(s>0)
       {
          x=min(s-k+1,n-1);
          if(cnt%2==0)
          {
             cout << last+x << ' ';
             last=last+x;
             cnt++;
          }
          else
          {
             cout << last-x << ' ';
             last=last-x;
             cnt++;
          }
          k--;
          s-=x;
       }
       cout << endl;
       //(n-1)*x+k-x  =s
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/wwwlps/article/details/81700028