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 ...
随机推荐
- SQLSERVER 使用 ROLLUP 汇总数据,实现分组统计,总计(合计),小计
版权声明:本文为博主原创文章,未经博主允许不得转载.本人观点或有不当之处,请在评论中及时指正,我会在第一时间内修改. https://blog.csdn.net/aiming66/article/de ...
- shell习题第7题:备份数据库
[题目要求] 设计一个shell脚本用来备份数据库,首先在本地服务器上保存一份数据,然后再远程拷贝一份,本地保存一周的数据,远程保存一个月 假设我们知道mysql root账号的密码,要备份的库为da ...
- Redis 持久化深入--机制、可靠性及比较
本文是对 antirez 博客中 Redis persistence demystified 的翻译和总结.主要从Redis的持久化机制,提供何种程度的可靠性以及与其他数据库的比较三个方面进行讨论. ...
- Delphi写的DLL,OCX中多线程一个同步问题
Delphi写的DLL,OCX中如果使用了TThread.Synchronze(Proc),可能导致线程死锁,原因是无法唤醒EXE中主线程, Synchronze并不会进入EXE主线程消息队列. 下面 ...
- usb驱动之打印usb设备信息(二)
以下是打印鼠标左右键及其他输入的源代码,详细说明见https://www.cnblogs.com/zhu-g5may/p/9309381.html /*参考/drivers/hid/usbhid/us ...
- python 银行系统
目前代码只写到这 主要部分已经实现 功能部分展现 首先我们需要五个类 用户类 : 成员属性 name id 以及 card 卡类: 成员属性 卡号 密码 余额 锁 界面类: 管理员界 ...
- Altium Designer (AD) 中规则的部分讲解
当创建好PCB时,选择 Design - Rules 即可进行规则的设置,也可以直接利用快捷键D-R(多利用快捷键,可以有效的提高设计效率,) 这个是规则的总界面,熟练以后可以直接从这里进行修改,很便 ...
- C语言中的强制类型转换
先直接放程序吧,后面还有总结. -------------------------------------------start------------------------------------ ...
- postgres 输出数据集的自定义函数
定义一个可输出数据集自定义函数有多种方法 1,先定义结构,再使用结构输出结果 CREATE TYPE compfoo AS (f1 int, f2 text); CREATE FUNCTION get ...
- 在.net core中使用Thrift
Thrift应用比较广泛,这里不介绍Thrift的基本概念和使用.Thrift对.net支持的很好,但自从.net core诞生引来,我曾多次关注Thrift的官方网站,看看对.net core是否提 ...