Walking Between Houses(贪心+思维)
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
- 题目意思:
- 现在有n个房子排成一列,编号为1~n,起初你在第1个房子里,现在你要进行k次移动,每次移动一都可以从一个房子i移动到另外一个其他的房子j
里(i != j),移动的距离为|j - i|。问你进过k次移动后,移动的总和可以刚好是s吗?若可以则输出YES并依次输出每次到达的房子的编号,
否则输出NO。- 解题思路:
每次至少移动一个单位的距离,至多移动n-1个单位的距离,所以要想完成上述要求每次决策前后一定要满足条件:
k <= s && k*(n-1) >= s
假设当前决策为移动x单位的距离,所以要满足下述条件:
( k-1 <= s-x) && (x <= n-1)
得:max(x) = min( (s - k + 1) , (n-1) )
因此我们的决策为:每次移动的大小为 s与k的差值 和 n-1 中的较小值,使得s和k尽快的相等。
- #include<cstdio>
- #include<cstring>
- #include<cstring>
- #define ll long long int
- #include<algorithm>
- using namespace std;
- ll n,s,k;
- int main()
- {
- ll sum,i,pos,len;
- scanf("%lld%lld%lld",&n,&k,&s);
- if(k>s||k*(n-)<s)
- {
- printf("NO\n");
- }
- else
- {
- printf("YES\n");
- pos=;///记录位置
- while(k--)
- {
- len=min(s-k,n-);
- s=s-len;
- if(pos+len<=n)///向前移还是向后移的判断
- {
- pos=pos+len;
- }
- else
- {
- pos=pos-len;
- }
- printf("%d ",pos);
- }
- }
- return ;
- }
Walking Between Houses(贪心+思维)的更多相关文章
- CF D. Walking Between Houses (贪心)
题意: 现在有n个房子排成一列,编号为1~n,起初你在第1个房子里,现在你要进行k次移动,每次移动一都可以从一个房子i移动到另外一个其他的房子j里(i != j),移动的距离为|j - i|.问你进过 ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 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 st ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- T - Posterized(贪心思维)
Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...
- 【CF1015D】Walking Between Houses(构造,贪心)
题意:从1开始走,最多走到n,走k步,总长度为n,不能停留在原地,不能走出1-n,问是否有一组方案,若有则输出 n<=1e9,k<=2e5,s<=1e18 思路:无解的情况分为两种: ...
- Codeforces Round #501 (Div. 3) D. Walking Between Houses (思维,构造)
题意:一共有\(n\)个房子,你需要访问\(k\)次,每次访问的距离是\(|x-y|\),每次都不能停留,问是否能使访问的总距离为\(s\),若能,输出\(YES\)和每次访问的房屋,反正输出\(NO ...
随机推荐
- Mysql-常用数据的基本操作和基本形式
一 .介绍 二 .插入数据INSERT 三 .更新数据UPDATE 四 .删除数据DELETE 五 .查询数据SELECT 六 .权限管理 一. 介绍 MySQL数据操作: DML ========= ...
- K8S学习心得 == 创建容器influxdb的RC和SVC
附上:YAML在线检查:http://nodeca.github.io/js-yaml Part 1: 部署Influxdb的RC文件,步骤如下: step 1: influxdb-rc.yaml文件 ...
- shell习题第6题:监听80端口
[题目要求] 写一个脚本,判断本机的80端口(加入服务为httpd)是否开启,如果开启就什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知相关人员 [核心要点] 检测80端口使用 ...
- linux介绍及基本命令
linux简介 Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux是一套免费使用和自由传播的类Unix操作系统,是一个基 ...
- spark RDD官网RDD编程指南
http://spark.apache.org/docs/latest/rdd-programming-guide.html#using-the-shell Overview(概述) 在较高的层次上, ...
- CDH部署(以5.7.5为例)
博客园首发,转载请注明出处https://www.cnblogs.com/tzxxh/p/9120020.html 一.准备工作(下面的内容括号内写master的表示仅在master节点执行,all代 ...
- C语言数据结构与算法之深度、广度优先搜索
一.深度优先搜索(Depth-First-Search 简称:DFS) 1.1 遍历过程: (1)从图中某个顶点v出发,访问v. (2)找出刚才第一个被顶点访问的邻接点.访问该顶点.以这个顶点为新的顶 ...
- 004---IO模型
io模型 同步.异步.阻塞.非阻塞概念 同步:发出一个功能调用时,在没有得到结果之前,该调用就不会返回,原地等待 异步:相反,不需要等待 阻塞:调用结果返回之前,当前线程会被挂起,如io操作,只有在得 ...
- 20145226夏艺华 《Java程序设计》实验报告三
实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运营和维护上的过程.软件工程包括下列领 ...
- P,NP,NPC的通俗解释
这或许是众多OIer最大的误区之一. 你会经常看到网上出现“这怎么做,这不是NP问题吗”.“这个只有搜了,这已经被证明是NP问题 了”之类的话.你要知道,大多数人此时所说的NP问题其实都是指的N ...