Greedy class Interval Problems

Interval coverage problems

With N closed interval [Ai, Bi] number line, the selection interval covered as little as possible to specify a segment [S, T].

Read in preprocessing, directly ignored outside of the range, from small to large order according to what the left point, to start looking to satisfy the left point less now covers the right point range, and the right end point of the interval [more tongue twisters do, I What say ...... If you do not find on output no Solution

paste

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define N 1000005
 5 inline int read(){
 6     int s=0,w=1;
 7     char ch=getchar();
 8     while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
 9     while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
10     return s*w;
11 }
12 struct p{
13     int a,b;
14     friend bool operator < (p x,p y)
15     {
16         if(x.a==y.a) return x.b<y.b;
17         return x.a<y.a;
18     }
19 }qy[N];
20 int main()
21 {
22     int n=read(),s=read(),t=read(),cnt=0;
23     while(n--)
24     {
25         int x=read(),y=read();
26         if(x>t||y<s) continue;
27         qy[++cnt].a=x,qy[cnt].b=y;
28     }
29     sort(qy+1,qy+cnt+1);
30     int now=s,i=1,ans=0;
31     while(now<t)
32     {
33         ans++;
34         int kk=now;
35         for(;qy[i].a<=kk&&i<=cnt;i++)
36             now=max(now,qy[i].b);
37         if(now==kk&&kk<t){cout<<"No Solution\n";break;}
38     }
39     if(now>=t) cout<<ans<<endl;
40 }
View Code

 

Similarly, pretreatment of water to cover it issues out of a diameter less than the width, diameter into the ok

 

Interval does not intersect the problem

There are n terms of work, respectively, in each job start time si, ti time ends. Your goal is to participate as much as possible to participate in the work, then work up to how many can participate in it?

Write time is too long, but still able to read

Directly attached

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 #define MAX 100005
 5 struct p{
 6     int yi,er;
 7 }qy[MAX];
 8 bool cmp(p a,p b)
 9 {
10     return a.er<b.er;
11 }
12 int main()
13 {
14     int n;
15     while(cin>>n&&n)
16     {
17         for(int i=0;i<n;i++) cin>>qy[i].yi;
18         for(int i=0;i<n;i++) cin>>qy[i].er;
19         sort(qy,qy+n,cmp);
20         int ans=0,t=0;
21         for(int i=0;i<n;i++)
22             if(t<qy[i].yi)
23             {
24                 t=qy[i].er;
25                 ans++;
26             }
27         cout<<ans<<endl;
28     }
29 }
View Code

 

Interval choice of site issues

Given the x-axis of the n closed interval [ai, bi], select as few points, such that each section has at least one closing point (point contained in the different sections may be the same).

After sorting out the last point of the first interval down to down, this interval contains the point of not watching, ans = 1, then the point is updated to the first occurrence of a range that does not contain the right end point of this point [ that I around ...... .

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define N 1000005
 5 inline int read(){
 6     int s=0,w=1;
 7     char ch=getchar();
 8     while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
 9     while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
10     return s*w;
11 }
12 struct p{
13     int a,b;
14     friend bool operator < (p x,p y)
15     {
16         if(x.b==y.b) return x.a>y.a;
17         return x.b<y.b;
18     }
19 }qy[N];
20 int main()
21 {
22     int t=read();
23     for(int i=0;i<t;i++) qy[i].a=read(),qy[i].b=read();
24     sort(qy,qy+t);
25     int node=qy[0].b,ans=1;
26     for(int i=1;i<t;i++)
27         if(qy[i].a>node) node=qy[i].b,ans++;
28     cout<<ans<<endl;
29 }
View Code

 

Guess you like

Origin www.cnblogs.com/Aaaamber/p/11282946.html
Recommended