贪心策略,从左往右,在保证最左边那点能覆盖同时尽可能往右覆盖,貌似很多题都可以这样贪。

View Code
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 struct data
 6 {
 7     int s,e;
 8     bool operator<(const data &ne)const
 9     {
10         if(s!=ne.s)
11             return s<ne.s;
12         else
13             return e<ne.e;
14     }
15 } po[10005];
16 int main()
17 {
18     int n,len;
19     while(scanf("%d%d",&n,&len)!=EOF)
20     {
21         for(int i=0; i<n; i++)
22             scanf("%d%d",&po[i].s,&po[i].e);
23         if(n==0)
24         {
25             printf("0\n");
26             continue;
27         }
28         sort(po,po+n);
29         int cnt=0,lx=1<<31;
30         for(int i=0; i<n; i++)
31         {
32             if(lx+len<po[i].e)
33             {
34                 if(lx+len<=po[i].s)
35                     lx=po[i].s;
36                 else
37                     lx+=len;
38                 int tp=(po[i].e-lx)/len+((po[i].e-lx)%len!=0);
39                 cnt+=tp;
40                 lx+=tp*len-len;
41             }
42         }
43         printf("%d\n",cnt);
44     }
45     return 0;
46 }