首页 > 代码库 > 贪心/poj 2437 Muddy roads

贪心/poj 2437 Muddy roads

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 struct node 6 { 7     int st,ed; 8 }; 9 node a[10010];10 11 int n,L,ans,pre;12 13 bool cmp(node x,node y)14 {15     return x.st<y.st;16 }17 18 int main()19 {20     while (scanf("%d%d",&n,&L)!=EOF)21     {22         for (int i=1;i<=n;i++) scanf("%d%d",&a[i].st,&a[i].ed);23         sort(a+1,a+n+1,cmp);24         ans=0;25         pre=-9999999;26         for (int i=1;i<=n;i++)27         {28             if (pre<=a[i].st)29             {30                 int len=a[i].ed-a[i].st;31                 int sum=(len+L-1)/L;32                 ans+=sum;33                 pre=a[i].st+sum*L;34             }35             else if (pre>a[i].st)36             {37                 int len=a[i].ed-pre;38                 int sum=(len+L-1)/L;39                 ans+=sum;40                 pre+=sum*L;41             }42         }43         printf("%d\n",ans);44     }45     return 0;46 }

 

贪心/poj 2437 Muddy roads