ICPC2018 Nakhon Pathom-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? 

我们可以把这条路描绘成一条长线段,有一个东部端点和一个西部端点。泰国政府正计划在这条路上建设一些公交车站,以一种特别的方式:每个房子都在其中一个公交车站的10km之内(Caution:这是半径10km,或者说以某点为中心的10个单位长度的邻域),政府至少需要建设多少公交车站才能满足需求?

输入

The first line contains a single integer m representing the number of the test cases.  Eachtest case consists of the following two lines:   

第一行包含一个整数m,表示测试样例的个数。每一个测试样例都由下列两行组成:
The first line contains a single integer n representing the number of houses on the road, where 0 ≤ n ≤ 2,000,000.

第一行包含一个正整数n,表示这条路上的房子数 
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 . 

第二行包含n个正整数h1...hn,表示这座房子从起点开始算的距离(单位为Km),这也就是说,第一座房子离路头h1 km远,第二栋房子离路头h2 km远,以此类推。你可以假设h1的数据已经按升序排列。

输出

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

对于每个测试样例,输出一行:满足需求的最小公交车站数

Sample input

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

Sample output

2
3

这道题的最大难点是想明白建设车站的所谓10km是半径10Km,而不是某两个地方的距离是10km。

想明白这件事之后,我们可以直接用差分数组记下各个点之间的距离,以此相加,若超出最大允许直径20km,则另设新站。

所以有这样的思路:

  1. 在输入时构建差分数组,其中b[i]指的是a[i]-a[i-1],即i房与i-1房的间距,因为第一座房子不存在这样的距离(与端点的距离没用),在此之后记得把b[1]赋值为0 
  2. 用一个循环对1-n进行遍历并设定起点为a[1],若某个点超出以a[1]为边界点的直径20km,则cnt++,更新起点,继续循环。

代码:

 

#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf("%lld",&a)
#define din(a) scanf("%d",&a)
#define printlnlld(a) printf("%lld\n",a)
#define printlnd(a) printf("%d\n",a)
#define printlld(a) printf("%lld",a)
#define printd(a) printf("%d",a)
#define reset(a,b) memset(a,b,sizeof(a))
const int INF=0x3f3f3f3f;
using namespace std;
const double PI=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod=1000000007;
const int tool_const=1999112620000907;
const int tool_const2=33;
///Schlacht von Stalingrad
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
ll a[3000000],b[3000000];
int DETERMINATION()
{
    ll t;
    lldin(t);
    while(t--)
    {
        ll n;
        lldin(n);
        for(int i=1; i<=n; i++)
        {
            lldin(a[i]);
            b[i]=a[i]-a[i-1];
        }
        b[1]=0;//第一座房子与左端点距离无效
        if(n==0)//没有房子自然也就没有站点
            printf("0\n");
        else
        {
            ll cnt=1,current=0;//由于存在房子时必然至少有一个站点,所以初始化为1
            for(int i=1; i<=n; i++)
            {
               current+=b[i];
               if(current>20)
               {
                   cnt++;
                   current=0;
               }
            }
            printlnlld(cnt);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值