CodeForces - 1015D
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.
Input
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.
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 (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.
Examples
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 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<stack> 8 #include<deque> 9 #include<map> 10 #include<iostream> 11 using namespace std; 12 typedef long long LL; 13 const double pi=acos(-1.0); 14 const double e=exp(1); 15 const int N = 100010; 16 17 #define lson i << 1,l,m 18 #define rson i << 1 | 1,m + 1,r 19 20 int main() 21 { 22 LL n,k,s; 23 LL i,j,x,y,p; 24 scanf("%lld%lld%lld",&n,&k,&s); 25 p=1; 26 if(k>s||(n-1)*k<s) 27 printf("NO\n"); 28 else 29 { 30 printf("YES\n"); 31 x=s/k; 32 y=s%k; 33 for(i=1; i<=k; i++) 34 { 35 LL mid=x; 36 if(y>0) 37 { 38 mid+=1; 39 y--; 40 } 41 if(p+mid>n) 42 p=p-mid; 43 else 44 p=p+mid; 45 printf("%lld ",p); 46 } 47 } 48 return 0; 49 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步