CodeForces - 1004A-Sonya and Hotels(思维)
Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.
The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has nn hotels, where the ii-th hotel is located in the city with coordinate xixi. Sonya is a smart girl, so she does not open two or more hotels in the same city.
Sonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to dd. The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel.
Because Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original nn hotels to the new one is equal to dd.
Input
The first line contains two integers nn and dd (1≤n≤1001≤n≤100, 1≤d≤1091≤d≤109) — the number of Sonya's hotels and the needed minimum distance from a new hotel to all others.
The second line contains nn different integers in strictly increasing order x1,x2,…,xnx1,x2,…,xn (−109≤xi≤109−109≤xi≤109) — coordinates of Sonya's hotels.
Output
Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to dd.
Examples
Input
4 3
-3 2 9 16
Output
6
Input
5 2
4 8 11 18 19
Output
5
Note
In the first example, there are 66 possible cities where Sonya can build a hotel. These cities have coordinates −6−6, 55, 66, 1212, 1313, and 1919.
In the second example, there are 55 possible cities where Sonya can build a hotel. These cities have coordinates 22, 66, 1313, 1616, and 2121.
题解:
判断是否有交叉,如果有就不加,没有就加差值,但是差值最大只能是2,超过2的当2算
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n,d;
cin>>n>>d;
int a[105];
for(int t=0;t<n;t++)
{
scanf("%d",&a[t]);
}
sort(a,a+n);
long long int sum=2;
for(int t=0;t<n-1;t++)
{
if(a[t]+d<=a[t+1]-d)
{
if((a[t+1]-a[t]-2*d+1)>2)
sum+=2;
else
sum+=a[t+1]-a[t]-2*d+1;
}
}
cout<<sum<<endl;
return 0;
}
CodeForces - 1004A-Sonya and Hotels(思维)的更多相关文章
- [Codeforces Round495A] Sonya and Hotels
[题目链接] https://codeforces.com/contest/1004/problem/A [算法] 直接按题意模拟即可 时间复杂度 :O(NlogN) [代码] #include< ...
- codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- Codeforces 714C. Sonya and Queries Tire树
C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...
- Codeforces 713A. Sonya and Queries
题目链接:http://codeforces.com/problemset/problem/713/A 题意: Sonya 有一个可放置重复元素的集合 multiset, 初始状态为空, 现给予三种类 ...
- CodeForces - 427A (警察和罪犯 思维题)
Police Recruits Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- codeforces 893D Credit Card 贪心 思维
codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...
- CodeForces 1151F Sonya and Informatics
题目链接:http://codeforces.com/problemset/problem/1151/F 题目大意: 给定长度为 n 的 01 序列,可以对该序列操作 k 次,每次操作可以交换序列中任 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
随机推荐
- PyQt5豆瓣镜像下快速安装
直接pip安装,慢到你怀疑人生.豆瓣镜像安装,嗯,不能更爽. pip install PyQt5 -i https://pypi.douban.com/simple 谢谢:https://blog.c ...
- JavaScript--Object类
Object类是所有JavaScript类的基类,提供了一种创建自定义对象的简单方式,不需要程序员再定义构造函数. 主要属性: constructor-对象的构造函数 prototype-获得类的pr ...
- 高并发压力下导致数据库bug
环境信息: linux 6.1 + oracle11.2.0.3 RAC 问题现象: 学校晚上6点选课,人数大概有3000,7点时,数据库报错如下(数据库到6点多还是可以连接的),数据库hu ...
- 分享知识-快乐自己:SpringMvc中的四种数据源及相关配置(整合快速集成开发)
数据库连接: jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://39.105.105.186:3306/SpringMybatis?us ...
- python基础-私有变量和方法
如果要让内部属性不被外部访问,可以把属性的名称前加上两个下划线__,在Python中,实例的变量名如果以__开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问 __priva ...
- codeforces 659C C. Tanya and Toys(水题+map)
题目链接: C. Tanya and Toys time limit per test 1 second memory limit per test 256 megabytes input stand ...
- PS 滤镜— —图像偏移
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- nginx 反向代理配置
转载一篇特别好的nginx配置博文:http://www.cnblogs.com/hunttown/p/5759959.html
- bzoj 3796 Mushroom追妹纸 —— 后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3796 先把三个串拼在一起,KMP 求 s1 , s2 中每个位置和 s3 的匹配情况: 注意 ...
- CentOS虚拟机通过主机网络上网
0 环境简介 环境如下: (1)宿主机为WIN7系统,连接内网,同时通过网关服务器上外网: (2)虚拟机为VMWare12下的CentOS7系统. 宿主机通过WIFI方式上外网的配置方法,参考本人另一 ...