Codeforces Round #501 (Div. 3) 1015D Walking Between Houses
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 kk moves.
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.
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.
10 2 15
YES
10 4
10 9 45
YES
10 1 10 1 2 1 2 1 6
10 9 81
YES
10 1 10 1 10 1 10 1 10
10 9 82
NO
题目大意:1~n个房子,起点为1,然后规定刚好k步,走完s的距离,(从x到y,距离为|x-y|).
思路:以为是个深搜。但是感觉写不了。。。看了官方题解。(蠢了)。左右走无法判断。
官方给了 cur + x 和 cur - x 来左右走。怎么保证刚好k步s距离呢。 s-(k-1)
如果s-(k-1) > (n-1) 走最大的距离,然后k -= 1, s -= l; (l为他俩的最小值)
当k等于1,剩最后s',就保证刚好走完s。k != 1, 总还会剩一点。比较巧妙
不能刚好k步走完s,当且仅当 k > s or k*(n-1) < s
能继续走,当且仅当 k-1 <= s-x x <= n-1 也就是min(n-1, s-(k-1));
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14 using namespace std; 15 typedef long long ll; 16 const int inf = 0x3f3f3f3f; 17 18 ll step(ll cur, ll x) { 19 if(cur - x > 0)//能左走就先左走 20 return cur - x; 21 else//不行就右走 22 return cur + x; 23 } 24 25 int main() { 26 //freopen("in.txt", "r", stdin); 27 ll n, k, s, cur = 1; 28 scanf("%lld%lld%lld", &n, &k, &s); 29 if(k > s || k*(n-1) < s) {//只有这两种情况NO 30 printf("NO\n"); 31 return 0; 32 } 33 printf("YES\n"); 34 while(k > 0) {//先走最大的,直到最后不足最大的,然后一点点走。 35 ll l = n-1 < s-(k-1) ? n-1 : s-(k-1);// s-(k-1)能保证k步刚好s距离 36 cur = step(cur, l);//左走还是右走 37 printf("%lld ", cur); 38 s -= l; 39 k -= 1; 40 } 41 }