BZOJ4995-[Usaco2017 Feb]Why Did the Cow Cross the Road

topic

Incrementing the segment considered in accordance with the case of a right end point of the same order of arrangement in accordance with the left end point

Every greedy will give it the nearest chicken this segment left point

Solution to a problem with the Internet have something like a pointer, so I can not understand, so specifically wrote a code base.

Time complexity bit ugly.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=20005;
 4 int n,m,t[N],ans,b[N],nxt[N],pre[N];
 5 struct node
 6 {
 7     int l,r;
 8 }a[N];
 9 int cmp(node x,node y)
10 {
11     if(x.r==y.r)return x.l<y.l;
12     return x.r<y.r;
13 }
14 int main()
15 {
16     scanf("%d%d",&n,&m);
17     for(int i=1;i<=n;i++)scanf("%d",&t[i]),nxt[i]=i+1,pre[i]=i-1;
18     for(int i=1;i<=m;i++)scanf("%d%d",&a[i].l,&a[i].r);
19     sort(t+1,t+n+1);
20     sort(a+1,a+m+1,cmp);
21     memset(b,0,sizeof(b));
22     for(int i=1;i<=m;i++)
23     {
24         int l=1,r=n;
25         while(l<r)
26         {
27             int mid=(l+r)>>1;
28             if(t[mid]>=a[i].l)r=mid;else l=mid+1;
29         }
30         if(!b[l]&&t[l]>=a[i].l&&t[l]<=a[i].r)b[l]=1;else
31         {
32             while(b[l]&&l<=n&&t[l]<=a[i].r)l++;
33             if(t[l]>a[i].r||l>n||t[l]<a[i].l)continue;
34             b[l]=1;
35         }
36     }
37     int ans=0;
38     for(int i=1;i<=n;i++)if(b[i])ans++;
39     printf("%d\n",ans);
40     return 0;
41 }
View Code

Guess you like

Origin www.cnblogs.com/Jack-Oran/p/11234103.html