1. 程式人生 > >hdu 6119 小小粉絲度度熊 (區間處理+尺取)

hdu 6119 小小粉絲度度熊 (區間處理+尺取)

比賽 scanf bits 處理 的確 spa ios con printf

http://acm.hdu.edu.cn/showproblem.php?pid=6119

解題思路:給出的出發時間和結束時間對有重合的部分進行處理,然後用尺取法找出最後的結果。比賽的時候的確想到了用尺取的想法完成題目,但是代碼能力不行沒有想出來。

AC代碼:

 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 const int maxn=100005;
 5 struct node
 6 {
 7     int l,r;
 8 }p[maxn];
 9 int sum[maxn];
10 bool cmp(node a,node b) 11 { 12 return a.l<b.l; 13 } 14 int main() 15 { 16 int n,m; 17 while(~scanf("%d%d",&n,&m)) 18 { 19 for(int i=0;i<n;i++) 20 scanf("%d%d",&p[i].l,&p[i].r); 21 sort(p,p+n,cmp); 22 int len=0; 23 int nowl=p[0
].l,nowr=p[0].r; 24 for(int i=1;i<n;i++) 25 { 26 if(p[i].l<=nowr) 27 nowr=max(nowr,p[i].r); 28 else 29 { 30 p[len].l=nowl; 31 p[len++].r=nowr; 32 nowl=p[i].l; 33 nowr=p[i].r;
34 } 35 } 36 p[len].l=nowl; 37 p[len++].r=nowr; 38 for(int i=0;i<len-1;i++) 39 sum[i]=max(0,p[i+1].l-p[i].r-1); 40 int l=0,r=0; 41 int now=0,ans=0; 42 while(r<len) 43 { 44 ans=max(ans,p[r].r-p[l].l+1+m-now); 45 now+=sum[r++]; 46 while(now>m) 47 now-=sum[l++]; 48 } 49 printf("%d\n",ans); 50 } 51 return 0; 52 }

hdu 6119 小小粉絲度度熊 (區間處理+尺取)