Advanced algorithms - greedy

The following is a summary of Informatics Olympiad AC code on a website through evaluation

· Example:

Subject description:

  N with a set of events E = {1,2, ..., n}, where each activity require the same resource, such as a lecture hall or the like, and only one event can use this resource at the same time. I Each activity has a requirement to use the resource si start time and an end time fi, and si <fi. If the event i is selected, the resources it occupies half-open time interval [si, fi) inside. If the interval [si, fi) with the interval [sj, fj) are disjoint, called activity i and j are compatible activity. That is, when si≥fj or sj≥fi, activities i and j are compatible activities. Select the largest collection of mutually compatible activities thereof.

Code:

 

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdio>
 4 
 5 using namespace std;
 6 
 7 struct node
 8 {
 9     int st;
10     int ed;
11 }a[1005];
12 
13 bool cmp(node x,node y)
14 {
15     return x.ed<y.ed;
16 }
17 
18 int main()
19 {
20     int n;
21     scanf("%d",&n);
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     int t=a[1].ed;
25     int ans=1;
26     for(int i=2;i<=n;++i)
27     {
28         if(a[i].st>=t)
29         {
30             ans++;
31             t=a[i].ed;
32         }
33     }
34     printf("%d",ans);
35     return 0;
36 }

 

· Example Two:

Subject description:

  Now our country to carry out the new rural construction, housing construction in rural areas into a unified planning and construction, the government requires that each household door plant some trees. Roadside entrance area divided into blocks, and numbers into 1..N. Each part is a unit size and a maximum of a tree species. Each resident is assigned a three in front of the house number B, E, T. This means that the number of three residents want a minimum tree species between B and T E. Of course, B≤E, residents must remember not grow more than the number of trees in the specified area of ​​the block area, so T≤E-B + l. Residents who want to plant trees in their respective regions can cross. Your task is to find the minimum number of trees to meet all the requirements, try less government spending.

Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 struct line{int s,e,v;}a[5005];
 8 
 9 int n,m;
10 
11 bool used[30005]={0};
12 
13 bool cmp(const line &x,const line &y){return x.e<y.e;}
14 
15 void init()
16 {
17     int i;
18     scanf("%d%d",&n,&m);
19     for(i=1;i<=m;++i) scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].v);
20     sort(a+1,a+m+1,cmp);
21 }
22 
23 void solve()
24 {
25     int i,j,k,ans=0;
26     for(int i=1;i<=m;++i)
27     {
28         k=0;
29         for(j=a[i].s;j<=a[i].e;++j)    if(used[j]) ++k;
30         if(k>=a[i].v) continue;
31         for(j=a[i].e;j>=a[i].s;--j)
32             if(!used[j])
33             {
34                 used[j]=1;
35                 k++;
36                 ans++;
37                 if(a[i].v==k) break;
38             }
39     }
40     printf("%d",ans);
41 }
42 
43 int main()
44 {
45     init();
46     solve();
47     return 0;
48 }

· Example Three:

Subject description:

Length L meters, width W m n lawn with a watering nozzle. Each nozzle are housed in the lawn on the center line (W from each of the two sides / 2 meters). We know the position of each nozzle (the distance from the centerline of lawn left end), and it can cover the range of watering.

I ask you: If you want piece of lawn watering, requires a minimum number of heads opened?

Code:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 #define N 300005
 6  
 7 using namespace std;
 8  
 9 const double Eps = 1e-5 ;
10  
11 inline int wread(){
12     char c=getchar ();int flag=1,wans=0;
13     while (c<'0'||c>'9'){if (c=='-') flag=-1;c=getchar ();}
14     while (c>='0'&&c<='9'){wans=wans*10+c-'0';c=getchar ();}
15     return wans*=flag;
16 }
17  
18 int T;
19 int n,L,W;
20 struct node {double lx,rx;int pos,r;}a[N];
21 bool e666 (node x,node y){return x.lx<y.lx;}
22  
23 int main (){
24     T=wread();
25     while (T--){
26         memset (a,0,sizeof a);
27         n=wread();L=wread();W=wread();
28         for (int i=1;i<=n;++i){
29             a[i].pos=wread(),a[i].r=wread();
30             if (a[i].r*a[i].r-(W*1.0/2.0)*(W*1.0/2.0 ) < 0 ) A [I] .POS = - . 1 , A [I] .lx = - . 1 , A [I] .rx = - . 1 ;
 31 is              // a circle (for example, not cover completely the upper end like lawn sandwiching the lawn circles in Example 1) it is certainly not selected from 
32              the else {
 33 is                  a [I] .lx = max ( 0.000000 , ( Double ) a [I] .POS-sqrt (( Double ) a [I] .r * ( Double ) A [I] .r- (* W is 1.0 / 2.0 ) * (* W is 1.0 / 2.0 )));
 34 is                  A [I] .rx = A [I] + sqrt .POS (( Double ) A [I] .r * ( Double ) A [I] .r- (* W is 1.0 /2.0 ) * (W is * 1.0 / 2.0 ));                    
 35              }
 36          }
 37 [   
38 is          Sort (A + . 1 , A + n-+ . 1 , E666);    
 39          
40          int NX = . 1 ; 
 41 is          Double S = 0 ; // start point 
42 is          BOOL F. = to true ; // determines whether or not cover a full one section 
43 is          int ANS = 0 ; // record 
44 is          
45          the while (NX <= n-) {
 46 is             ++ ANS ;
 47              Double Re = S;
 48              the while (A [NX] .lx <&& NX = Re <= n-) { // template 
49                  IF (A [NX] .rx> = Re) S = max (S, A [NX] .rx);
 50                  NX ++ ;
 51 is              }
 52 is              IF (S> = L) { BREAK ;}
 53 is              IF (Re == S) = {F. to false ; BREAK ;} // updated it, drawing ____ i.e. there must be a current interval between the section and the next by a distance not meet the requirements of full coverage, exit 
54 is          }
 55          IF the puts ((F.!) " 0 " );
56         else    printf("%d\n",ans);
57     }
58     return 0;
59 }

· Example 4:

Subject description:

  N a factory received orders for products, which n products are processed in two workshops A, B, and must be processed at the plant after the A to B the processing plant.

A product i in A, B of two vehicles processing time was Ai, Bi. How to arrange this processing order products in order to make the shortest total processing time.

Processing time is mentioned here means: start from the first product processing to the final product are all in the A, B two finished processing workshops of time.

Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 int ans[1005],n,k,i,j,t,a[1005];
 7 int b[1005],m[1005],s[1005];
 8 
 9 void read()
10 {
11     int i;
12     scanf("%d",&n);
13     for(i=1;i<=n;++i) scanf("%d",&a[i]);
14     for(i=1;i<=n;++i) scanf("%d",&b[i]);
15 }
16 
17 void solve()
18 {
19     for(i=1;i<=n;++i){m[i]=min(a[i],b[i]);s[i]=i;}
20     for(i=1;i<=n-1;++i)
21         for(j=i+1;j<=n;++j)
22             if(m[i]>m[j]){swap(m[i],m[j]);swap(s[i],s[j]);}
23     k=0;t=n+1;
24     for(i=1;i<=n;++i)
25         if(m[i]==a[s[i]]){k++;ans[k]=s[i];}
26         else{t--;ans[t]=s[i];}
27     k=0;t=0;
28     for(i=1;i<=n;++i)
29     {
30         k+=a[ans[i]];
31         if(t<k) t=k;
32         t+=b[ans[i]];
33     }
34     printf("%d\n",t);
35     for(i=1;i<=n;++i) printf("%d ",ans[i]);
36     printf("\n");
37 }
38 
39 int main()
40 {
41     read();
42     solve();
43     return 0;
44 }

 

Guess you like

Origin www.cnblogs.com/juruohqk/p/10991626.html