zoukankan      html  css  js  c++  java
  • codeforces 1015D

    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的路径上,要求你走k次,要求一共走的距离为s
    题解:如果可以走的话就走min(n - 1,s - (k-i))的距离,然后循环走就行
    代码如下:
    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define fuck(x) cout<<"["<<x<<"]";
    #define FIN freopen("input.txt","r",stdin);
    #define FOUT freopen("output.txt","w+",stdout);
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int maxn = 1e5+5;
    int ans[maxn];
    
    int main(){
    #ifndef ONLINE_JUDGE
      FIN
    #endif
      long long n, k, s;
      cin >> n >> k >> s;
      if (s>(n-1)*k){
        cout << "NO" << endl;
        return 0;
      }
      if (s < k) {
        cout << "NO" << endl;
        return 0;
      }
      cout << "YES" << endl;
      long long fsts = 1;
      for (int i=1;i<=k;i++) {
        long long temp = min(n - 1,s - (k-i));
        s -= temp;
        if (fsts == 1) fsts+=temp;
        else fsts-=temp;
        cout << fsts << ' ';
      }
      puts("");
      return 0;
    
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    C# 单例模式
    [C# 线程处理系列]专题四:线程同步
    C#实现远程开机(局域网测试通过)
    C#实现类只实例化一次(被多个类访问调用)
    c#委托与事件、消息、WndProc用法(转)
    UML 类图常用表示方法.
    C# Socket 接受数据不全的处理
    执行数据库操作失败: 连接未关闭。 连接的当前状态为打开
    hdu 2018 母牛的故事 动态规划入门题
    Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9416561.html
Copyright © 2011-2022 走看看