Bus Stop

题目描述

In a rural village in Thailand, there is a long, straight, road with houses scattered along it.  
(We can picture the road as a long line segment, with an eastern endpoint and a western endpoint.)  The Thai government is planning to set up bus stops on this road in such a way that every house is within ten kilometers of one of the stops.  What is the minimum number of stops the government need to set up to satisfy the requirement? 

输入

The first line contains a single integer m representing the number of the test cases.  Eachtest case consists of the following two lines:   
The first line contains a single integer n representing the number of houses on the road, where 0 ≤ n ≤ 2,000,000. 
The second line contains n integers h1  h2  … hn  representing the locations of the houses in kilometers as measured from the start of the road, where 0 ≤ hi  ≤ 90,000,000.  That is, the first house is h1  kilometers away from the start of the road, the second house is h2  kilometers 
away from the start of the road, and so on.  You may assume that hi ’s are sorted in ascending order, e.g., h1  ≤ h2  ≤ h3  ≤ … ≤ hn . 

输出

For each test case, print out in a single line the minimum number of bus stops that satisfy the requirement. 

样例输入

复制样例数据

2
5
1 2 3 200 210
4
10 30 80 200

样例输出

2
3
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
int a[2000010],b[2000010];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        fill(b,b+2000010,21);//为了让最后一个要建的站加到sum里
        int sum=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            if(i>=1){
                b[i-1]=a[i]-a[i-1];//记录每两个家之间的差值
            }
        }
        int flag=0;
        for(int i=0;i<n;i++){
            flag+=b[i];//用flag累加每个差值
            if(flag>20){//一旦flag>20,就说明从flag=0的家开始到当前的家的距离超过了20,则要sum++
                sum++;
                flag=0;//更新flag为0,继续以当前家为起点往后寻找20公里以内的家
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值