Greedy little mind

Greedy strategy is an algorithm of optimal strategy in every decision is taken under the current sense. Greedy algorithm, to meet local optimal solution can be introduced by the global optimal solution. In simple terms, a big problem is to split into a sub-issue, and then find the optimal solution for each sub-problem, combined into a globally optimal solution

Greedy question is always the present, the future does not consider the impact may cause. So when choosing a greedy algorithm, we must consider its validity.

 

To give an incorrect greedy: noip2011 improve group day2 Question 3 sightseeing bus , data year when the exam can 100pts (specific ideas can see explanations) with the greedy, then the goose is actually greed is wrong, by others card out qwq;

Common practice greedy:

In the following topics increase the difficulty of the group, the most common, there are two greedy. One is: "We will follow a certain order to sort XXX, then a certain order (for example, from small to large) process." The other is: "We always take the maximum / small things in XXX, XXX and update", sometimes "XXX largest / small stuff" can be optimized, such as using a priority queue maintenance.

Sorting:

A common input ordering method is a case where an array (generally one or two) comprise several weights, calculated by the method of sorting and then iterates simulation determined optimum value.

Regret Law:

eg: Luo Gu p2949 work schedule

Greedy idea:

      1. assume every job done, after all the work will be sorted by the deadline into the team.

      2. In the judgment item i work to do, if that deadline in line with the conditions, it is not with the team when compared to the smallest elements of remuneration, if the item i work a higher remuneration (sorry), then ans + = a [ i] .pq.top ().

       PS: with a priority queue (small heap root) to maintain the head of the queue elements to a minimum.

That simple question is simple greed, that is difficult is difficult, mainly because of greed proved think it is right.

Here are a few classic greedy algorithm application:

1. Select the disjoint interval problem:

Given n open interval (ai, bi), try to select a plurality of sections, such that the interval twenty-two no common point.

[Thinking] in accordance with the end of time bi from small to large, in order to consider each event, if not repeat, then you can choose this event.

eg: Events schedule

The activities sorted by the end of the period, after ordering sweeping from the first to the last, if not previously elected to the conflict to choose this event

#include<bits/stdc++.h>

using namespace std;

int n;

struct node{
    int st,ed;
}a[1005];

bool cmp(node x,node y){
    return x.ed<y.ed;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
      scanf("%d%d",&a[i].st,&a[i].ed);
      
    sort(a+1,a+n+1,cmp);
    
    int t=a[1].ed;
    int ans=1;
    
    for(int i=2;i<=n;i++){
        if(a[i].st>=t){
            ans++;
            t=a[i].ed;
        }
    }
    
    cout<<ans<<endl;
}

2. The range of choice of site issues:

Given n closed interval [ai, bi], selected from points on the number line as little as possible, such that each section has at least one point (the interval containing different points may be the same);

[Thinking] by the end of the bi from small to large, for the interval, if there is no point, then added a point in the tail (because the tail is the most likely place to be repeated coverage);

eg: planting trees

#include<cstdio>
#include<algorithm>
#include<iostream>

using namespace std;

int n,m,k,ans;
bool p[50100];
struct h{
    int b,e,t;
}a[50100];

bool cmp(h x,h y){
    return x.e<y.e;
}

int main(){
    
    cin>>n>>m;
    
    for(int i=1;i<=m;i++)
      cin>>a[i].b>>a[i].e>>a[i].t;
      
    sort(a+1,a+m+1,cmp);
    
    for(int i=1;i<=m;i++){
        k=0;
        for(int j=a[i].b;j<=a[i].e;j++)
          if(p[j]) k++;
        if(k>=a[i].t) continue;
        
        for(int j=a[i].e;j>=a[i].b;j--)
            if(!p[j]) {
                p[j]=1;
                k++;
                ans++;
                if(k==a[i].t) break;
        }
        
    }
    
    cout<<ans;
}

3. The range of issues covered:

Before we talked qwq? Is the water into the city of segments covered in a bar qwq

eg: Sprinklers

#include<bits/stdc++.h>

using namespace std;

int n,cnt,l,h,x,r;

struct seg{double x,y;}a[20005];

bool cmp(const seg &x,const seg &y){
    return x.x<y.x;
}

void read(){
    cin>>n>>l>>h;
    cnt=0;
    for(int i=1;i<=n;i++){
        cin>>x>>r;
        if(r<=h/2) continue;
        cnt++;
        a[cnt].x=x-sqrt(r*r-h*h/4.0);
        a[cnt].y=x+sqrt(r*r-h*h/4.0);
    }
}

void solve(){
    double t=0;
    int ans=0,bj=1,i=1;
    while(t<l){
        ans++;
        double s=t;
        for(;a[i].x<=s&&i<=cnt;i++)
          if(t<a[i].y) t=a[i].y;
        if(t==s&&s<l) {
            cout<<"0"<<endl;
            bj=0;
            break;
        }
    }
    if(bj) cout<<ans<<endl;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        read();
        sort(a+1,a+cnt+1,cmp);
        solve();
    }
}

 

Guess you like

Origin www.cnblogs.com/zhuier-xquan/p/10983451.html