Cottage Village
题面翻译
题目描述
给定一个坐标轴,轴上有 n n n 座房子,每座房子的位置是 x i x_i xi,边长是 a i a_i ai。有个人想要贴着其中一座房子建造一座他自己的边长为 t t t 的房子,请输出共有多少种建造方法。
输入格式
第一行输入两个正整数 n , t n,t n,t。
接下来 n n n 行,每行输入两个以空格分隔的整数 x i , a i x_i,a_i xi,ai。
输出格式
输出共 1 1 1 行,输出边长为 t t t 的房子有多少种建造方法。
数据范围
1 ≤ n , t ≤ 1 0 3 1\le n,t\le10^3 1≤n,t≤103, 0 ≤ ∣ x i ∣ ≤ 1 0 3 0\le|x_i|\le10^3 0≤∣xi∣≤103, 1 ≤ a i ≤ 1 0 3 1\le a_i\le10^3 1≤ai≤103。
译自残阳如血。
题目描述
A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» n n n square houses with the centres on the O x Ox Ox -axis. The houses’ sides are parallel to the coordinate axes. It’s known that no two houses overlap, but they can touch each other.
The architect bureau, where Peter works, was commissioned to build a new house in «Flatville». The customer wants his future house to be on the O x Ox Ox -axis, to be square in shape, have a side $ t $ , and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the $ Ox $ -axis and it shouldn’t overlap any of the houses in the village.
Peter was given a list of all the houses in «Flatville». Would you help him find the amount of possible positions of the new house?
输入格式
The first line of the input data contains numbers $ n $ and $ t $ ( $ 1<=n,t<=1000 $ ). Then there follow $ n $ lines, each of them contains two space-separated integer numbers: $ x_{i} $ $ a_{i} $ , where $ x_{i} $ — $ x $ -coordinate of the centre of the $ i $ -th house, and $ a_{i} $ — length of its side ( $ -1000<=x_{i}<=1000 $ , $ 1<=a_{i}<=1000 $ ).
输出格式
Output the amount of possible positions of the new house.
样例 #1
样例输入 #1
2 2
0 4
6 2
样例输出 #1
4
样例 #2
样例输入 #2
2 2
0 4
5 2
样例输出 #2
3
样例 #3
样例输入 #3
2 3
0 4
5 2
样例输出 #3
2
提示
It is possible for the $ x $ -coordinate of the new house to have non-integer value.
思路
这道题的意思是说题目给定的坐标(这个坐标是在边长为
a
i
a_i
ai房子的中间),因此我们得用double类型来存储,而且我们还知道那个人要盖的房子可以在最左边和最右边,所以我们代码的ans
初始值为2。然后如果每个空隙恰好等于
t
t
t的话,我们就ans++,如果大于
t
t
t,我们就ans+=2。
代码
//个人感觉是因为题目只是说某个房子的位置在中心(真服了,那翻译,错的离谱)
#include<iostream>
#include<algorithm>
#include<cstring>
#define x first
#define y second
using namespace std;
const int N = 1e3+10;
typedef pair<double,double>PII;
PII w[N];
int n,t;
int main(){
cin>>n>>t;
for(int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
w[i].x=a-b/2.0;
w[i].y=a+b/2.0;
}
sort(w+1,w+1+n);
int ans=2;//最左与最右,初值直接赋 2
for(int i=2;i<=n;i++){
if(w[i].x-w[i-1].y>t)ans+=2;
if(w[i].x-w[i-1].y==t)ans++;
}
cout<<ans;
return 0;
}