POJ2437Muddy roads 贪心

Muddy roads

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 3554

 

Accepted: 1625

Description

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.

Input

* 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. 

Output

* Line 1: The miminum number of planks FJ needs to use.

Sample Input

3 3
1 6
13 17
8 12

Sample Output

5

Hint

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: 

                   111222..333444555....

                   .MMMMM..MMMM.MMMM....

                   012345678901234567890

Source

USACO 2005 U S Open Silver

算法分析:

题意:

n摊不定区间长度的泥坑,有无数个长度为L木板,求最少需要多少木板才能覆盖所有泥坑?

分析:

把泥坑按左区间位置升序排序,
1、前一个木板完全覆盖了当前的泥坑
2、前一个木板完全没接触到当前的木板 则更新端点
3、前一个木板覆盖了一部分 则计算铺完剩下的泥坑需要多少木板
代码实现:
#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
const int INT_INF = 0x3f3f3f3f;  
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN = 1e5 + 10;  
const int MAXT = 10000 + 10;  
const int M=10005;
struct node
{
	int x,y;
};
bool cmp(const node &a,const node &b)
{
	if(a.x!=b.x)
	return a.x<b.x;
	else 
	return a.y<b.y;
}
int main()
{
   int n,m;
   while(scanf("%d%d",&n,&m)!=EOF)
   {
   	node a[M];
   	for(int i=0;i<n;i++)
	{
	    scanf("%d%d",&a[i].x,&a[i].y);
	}
	sort(a,a+n,cmp);
	int p=0;
	int ans=0;
	for(int i=0;i<n;i++)
	{
	  if(p>=a[i].y) continue;
	  if(p<=a[i].x) {p=a[i].x;}    //注意和下面顺序不能错
	  if(p<=a[i].y)
	  {
	  	if((a[i].y-p)%m==0)
	  	{
	  		ans+=(a[i].y-p)/m;
	  		p=a[i].y;
	  	 }
	  	else	
		{
		  ans+=(a[i].y-p)/m+1;
		  p+=((a[i].y-p)/m+1)*m ;
		}
		
	  }
   }
   cout<<ans<<endl;
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/81267215