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 ...
随机推荐
- c#将List转换成DataTable(采用Emit)
前段时间通过网上查找,使用emit将Datatable,DataReader转换成List<T>了.这是从数据库到展示. 但是最近整理Hikari(我写的数据库连接池),发现c#里面数据库 ...
- LNMP web服务的安装
第1章 安装Nginx 环境: 系统:CentOS6.5 软件:nginx-1.6.3 mysql-5.5.49 php-5.5.32 1.1 Nginx官网 http://nginx. ...
- PHP程序员的技术成长规划 第一阶段:基础阶段
第一阶段:基础阶段(基础PHP程序员) 重点:把LNMP搞熟练(核心是安装配置基本操作)目标:能够完成基本的LNMP系统安装,简单配置维护:能够用PHP源码做基本的简单系统的PHP开发:能够在PHP中 ...
- kbmMW功能 - kbmMWProcess单元(转帖)
此贴为转发红鱼儿的文章,原贴地址: https://www.cnblogs.com/kinglandsoft/p/kbmmw-features-5-kbmmwprocess-unit.html 在新的 ...
- Delphi 调试连接 任意Android手机/平板/盒子
Delphi有时候无法连接调试一些手机,解决方案: 1.安装Google USB Driver 2.通过设备管理器查看手机或平板USB的VID,PID 3.修改你的电脑上的android_winusb ...
- hadoop生态搭建(3节点)
软件:CentOS-7 VMware12 SSHSecureShellClient shell工具:Xshell 规划 vm网络配置 01.基础配置 02.ssh配置 03.zookeep ...
- 笔记本电脑、VM虚拟机、开发板三者网线连接互ping
笔者在做NFS挂接练习时,发现网上的资料大部分是笔记本电脑(以下简称PC)和虚拟机PING.PC和开发板PING,这样的方式不是我想要的.笔者需要使用无线网卡上网,使用有线网卡进行三者互PING.在开 ...
- 树莓派 raspberry系统 VNC View 连接 Cannot currently show the desktop 错误解决
https://www.raspberrypi.org/forums/viewtopic.php?t=216737 我是因为空间不够
- spark 例子count(distinct 字段)
spark 例子count(distinct 字段) 例子描述: 有个网站访问日志,有4个字段:(用户id,用户名,访问次数,访问网站) 需要统计: 1.用户的访问总次数去重 2.用户一共访问了多少种 ...
- 考研编程练习----Kruskal
#include <stdio.h> #include <stdlib.h> #define MAX 100 /* 定义边(x,y),权为w */ typedef st ...