Codeforces Round #501 (Div. 3) D(贪心,模拟,思维)

链接
D. Walking Between Houses
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n houses in a row. They are numbered from 1 to n in order from left to right. Initially you are in the house 1.

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 |x−y| 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 n, k, s (2≤n≤109, 1≤k≤2⋅105, 1≤s≤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 (1≤hi≤n) on the second line, where hi is the house you visit on the i-th move.

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

Examples
inputCopy
10 2 15
outputCopy
YES
10 4
inputCopy
10 9 45
outputCopy
YES
10 1 10 1 2 1 2 1 6
inputCopy
10 9 81
outputCopy
YES
10 1 10 1 10 1 10 1 10
inputCopy
10 9 82
outputCopy
NO

题意:有一段连续的区间,长度为n,某人位于坐标1,可以在该区间内来回走动,他想刚好在k次移动后共走了s的路程,不可原地移动,如果无法完成,输出NO,否则输出YES,再输出每次移动后的下标。
思路:最开始时,尽量移动最大距离,后面得注意留下路程让他每次移动一格后刚好k次走完s的路程。

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	int n,k,s;
	cin >> n >> k >> s;
	if (k > s || (n-1)*k < s){
		cout << "NO" << endl;
	}else{
		cout << "YES" << endl;
		int l,cur = 1;
		while(k--){
			l = min(n-1,s - (k - 1));
			if (cur - l <= 0) cur += l;
			else cur -= l;
			cout << cur << " ";
			s -= l;
		}
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值