Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainstorms and now contains (1 <= N <= 10,000) mud pools.
Farmer John has a collection of wooden planks of length L that he can use to bridge these mud pools. He can overlap planks and the ends do not need to be anchored on the ground. However, he must cover each pool completely.
Given the mud pools, help FJ figure out the minimum number of planks he needs in order to completely cover all the mud pools.
Farmer John has a collection of wooden planks of length L that he can use to bridge these mud pools. He can overlap planks and the ends do not need to be anchored on the ground. However, he must cover each pool completely.
Given the mud pools, help FJ figure out the minimum number of planks he needs in order to completely cover all the mud pools.
* Line 1: Two space-separated integers: N and L
* Lines 2..N+1: Line i+1 contains two space-separated integers: s_i and e_i (0 <= s_i < e_i <= 1,000,000,000) that specify the start and end points of a mud pool along the road. The mud pools will not overlap. These numbers specify points, so a mud pool from 35 to 39 can be covered by a single board of length 4. Mud pools at (3,6) and (6,9) are not considered to overlap.
* Lines 2..N+1: Line i+1 contains two space-separated integers: s_i and e_i (0 <= s_i < e_i <= 1,000,000,000) that specify the start and end points of a mud pool along the road. The mud pools will not overlap. These numbers specify points, so a mud pool from 35 to 39 can be covered by a single board of length 4. Mud pools at (3,6) and (6,9) are not considered to overlap.
* Line 1: The miminum number of planks FJ needs to use.
3 3 1 6 13 17 8 12
5
INPUT DETAILS:
FJ needs to use planks of length 3 to cover 3 mud pools. The mud pools cover regions 1 to 6, 8 to 12, and 13 to 17.
OUTPUT DETAILS:
FJ can cover the mud pools with five planks of length 3 in the following way:
FJ needs to use planks of length 3 to cover 3 mud pools. The mud pools cover regions 1 to 6, 8 to 12, and 13 to 17.
OUTPUT DETAILS:
FJ can cover the mud pools with five planks of length 3 in the following way:
111222..333444555.... .MMMMM..MMMM.MMMM.... 012345678901234567890
这个题挺有意思的,没有用到什么算法,就算是单纯的找找规律,不算麻烦。题意是:一条路(坐标轴)上给你n个坑,给坑的起点s_i和 终点e_i,然后给L长的木板,可无缝起来的那种,问走过所有的坑最少用多少木板搭路。
也幸亏有着下面的一个图解,对着比划一下,规律就出来了,不过说也不好形容,在代码里面体现吧。
代码如下:
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct Node
{
int s,e,len;
}node[10010];
int cmp(const Node &a,const Node &b)
{
if(a.s<b.s)
return 1;
return 0;
}
int main()
{
int N,L;
while(~scanf("%d%d",&N,&L) && N && L)
{
for(int i=1; i<=N; i++)
{
scanf("%d%d",&node[i].s,&node[i].e);
//cin>>node[i].s>>node[i].e;
node[i].len=node[i].e-node[i].s;
}
sort(node+1,node+N+1,cmp);
int flag=0;
long long sum=0;
int cnt=0;
int temp1=0,temp2=0;
for(int i=1; i<=N; i++)
{
cnt=(node[i].len-flag+L-1)/L;
sum+=cnt;
temp1=node[i].s+L*cnt+flag;
temp2=temp1-node[i+1].s;
if(temp1>node[i+1].s)
{
flag=temp2;
}
else if(temp1<node[i+1].s && temp2>L/2)
{
flag=temp2;
}
else
{
flag=0;
}
}
printf("%I64d\n",sum);
//cout<<sum<<endl;
}
return 0;
}