贪心——Cottage Village

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 1n,t103 0 ≤ ∣ x i ∣ ≤ 1 0 3 0\le|x_i|\le10^3 0xi103 1 ≤ a i ≤ 1 0 3 1\le a_i\le10^3 1ai103


译自残阳如血

题目描述

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;
}
  • 37
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值