CodeForces D. Walking Between Houses
Posted zlrrrr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces D. Walking Between Houses相关的知识,希望对你有一定的参考价值。
http://codeforces.com/contest/1015/problem/D
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
代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll N, K, S; int main() { cin >>N >> K >> S; if((N - 1) * K < S || S < K) { printf("NO "); return 0; } printf("YES "); int st = 1; while(K --) { int dis = min(S - K, N - 1); S -= dis; if(st + dis <= N) st += dis; else st -= dis; printf("%d ", st); } printf(" "); return 0; }
以上是关于CodeForces D. Walking Between Houses的主要内容,如果未能解决你的问题,请参考以下文章
Diagonal Walking v.2 CodeForces - 1036B (思维,贪心)
CodeForces - 1015 D.Walking Between Houses
Codeforces 1154D - Walking Robot - [贪心]
codeforces 979C Kuro and Walking Route