Algorithm


D. Walking Between Houses
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

You have to perform k 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 x to the house y, the total distance you walked increases by |xy||�−�| units of distance, where |a||�| is the absolute value of a. 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 s 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 k moves.

Input

The first line of the input contains three integers nks (2n1092≤�≤1091k21051≤�≤2⋅1051s10181≤�≤1018) — the number of houses, the number of moves and the total distance you want to walk.

Output

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

Otherwise print "YES" on the first line and then print exactly k integers hiℎ� (1hin1≤ℎ�≤�) on the second line, where hiℎ� is the house you visit on the i-th move.

For each j from 11 to k1�−1 the following condition should be satisfied: hjhj+1ℎ�≠ℎ�+1. Also h11ℎ1≠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

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

long long n, k, s;
vector<int> sol;

int main() {
  scanf("%lld %lld %lld", &n, &k, &s);

  if(s < k || s > k * (n - 1)) {
    puts("NO");
    return 0;
  }

  sol.push_back(1);

  bool ok = true;
  long long cur = 1, nxt[] = {1, n};
  while(s >= abs(cur - nxt[ok])) {
    s -= abs(cur - nxt[ok]);
    cur = nxt[ok];
    ok = !ok;
    sol.push_back(cur);
    --k;
  }

  int step;
  if(s == k) {
    if(!ok) {
      step = -1;
      for(int i = cur - 1; k != 0; --k, i += step) {
        if(i == 0)
          i = 2, step = 1;
        if(i == n + 1)
          i = n - 1, step = -1;
        sol.push_back(i);
      }
    } else {
      step = 1;
      for(int i = cur + 1; k != 0; --k, i += step) {
        if(i == 0)
          i = 2, step = 1;
        if(i == n + 1)
          i = n - 1, step = -1;
        sol.push_back(i);
      }
    }
  } else if(s > k) {
    long long need = s - k + 1;
    s -= need;
    --k;
    if(ok) {
      step = 1;
      sol.push_back(sol.back() + need);
      for(int i = sol.back() + 1; k != 0; --k, i += step) {
        if(i == 0)
          i = 2, step = 1;
        if(i == n + 1)
          i = n - 1, step = -1;
        sol.push_back(i);
      }
    } else {
      step = -1;
      sol.push_back(sol.back() - need);
      for(int i = sol.back() - 1; k != 0; --k, i += step) {
        if(i == 0)
          i = 2, step = 1;
        if(i == n + 1)
          i = n - 1, step = -1;
        sol.push_back(i);
      }
    }
  } else {
    while(s < k) {
      sol.pop_back();
      s += abs(1 - n);
      ++k;
      ok = !ok;
    }
    long long need = s - k + 1;
    s -= need;
    --k;
    if(ok) {
      step = 1;
      sol.push_back(sol.back() + need);
      for(int i = sol.back() + 1; k != 0; --k, i += step) {
        if(i == 0)
          i = 2, step = 1;
        if(i == n + 1)
          i = n - 1, step = -1;
        sol.push_back(i);
      }
    } else {
      step = -1;
      sol.push_back(sol.back() - need);
      for(int i = sol.back() - 1; k != 0; --k, i += step) {
        if(i == 0)
          i = 2, step = 1;
        if(i == n + 1)
          i = n - 1, step = -1;
        sol.push_back(i);
      }
    }
  }

  puts("YES");
  for(int i = 1; i < sol.size(); ++i)
    printf("%d ", sol[i]);
  puts("");

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10 2 15

Output

x
+
cmd
YES
10 4
Advertisements

Demonstration


Codeforces Solution-D. Walking Between Houses-Solution in C, C++, Java, Python,Walking Between Houses,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+