• Codeforces Round #501 (Div. 3) 1015D Walking Between Houses


    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 |xy||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 (2n1092≤n≤109 , 1k21051≤k≤2⋅105 , 1s10181≤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 (1hin1≤hi≤n ) on the second line, where hihi is the house you visit on the ii -th move.

    For each jj from 11 to k1k−1 the following condition should be satisfied: hjhj+1hj≠hj+1 . Also h11h1≠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




    题目大意: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
    ");
    31         return 0;
    32     }
    33     printf("YES
    ");
    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 }
    
    
  • 相关阅读:
    做正确的事,正确的做事
    博客园翻车启示录
    从一次线下读书会获得的收获
    坚持一个好习惯该有多难?
    有道云笔记
    Leetcode 10. 正则表达式匹配
    C++版
    VS 2013 professional版在win10上安装出错的解决方法
    华为校招2016.09机试 第1题: 字符串按指定长度重新分割
    C#版
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572939.html
Copyright © 2020-2023  润新知