欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

算法进阶指南-基础算法-贪心

程序员文章站 2022-05-24 17:33:57
1.Sunscreen题目链接代码:import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;import java.util.HashMap;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int C =...

1.Sunscreen
题目链接
算法进阶指南-基础算法-贪心
代码:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.HashMap;
public class Main {
    public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
         int C = in.nextInt();
         int L = in.nextInt();
         int[] minSPF = new int[C];
         int[] maxSPF = new int[C];
         int[] SPF = new int[L];
         int[] cover = new int[L];
         for(int i=0;i<C;i++){
             minSPF[i]=in.nextInt();
             maxSPF[i]=in.nextInt();
         }
         for(int i=0;i<L;i++){
             SPF[i]=in.nextInt();
             cover[i]=in.nextInt();
         }
         Integer[] c = new Integer[C];
         for(int i=0;i<C;i++)c[i]=i;
         Arrays.sort(c, new Comparator<Integer>() {
             @Override
             public int compare(Integer o1, Integer o2) {
                 return minSPF[o2]-minSPF[o1];
             }
         });
         int res = 0;
         for(int i=0;i<C;i++){
             int ix = c[i];
             int minSPFi = minSPF[ix];
             int maxSPFi = maxSPF[ix];
             int maxIx = 0;
             int maxSPFC = Integer.MIN_VALUE;
             boolean findOne = false;
             for(int j=0;j<L;j++){
                 if(SPF[j]>=minSPFi&&SPF[j]<=maxSPFi){
                     //需要满足条件
                     //System.out.println("im in!");
                     if(SPF[j]>maxSPFC&&cover[j]>0){
                         maxSPFC=SPF[j];
                         maxIx = j;
                         findOne=true;
                         //System.out.println(maxIx);
                     }
                 }
             }
             /*
             贪心策略:由于minSPF按照递减策略排序,那么每一个不低于当前minSPF的防晒霜都不会低于其他后续防晒霜的minSPF,
             对于当前可选的x,y,如果SPF[y]>SFP[x],那么后续奶牛只可能有三种情况“x,y都可”,“x可,y大于maxSPF了”,“x,y都不可”
             选择SPF最大的显然能够满足更多奶牛们的要求
              */
             if(findOne){
                 cover[maxIx]--;//用掉一瓶
                 res++;//满足一头牛
             }
         }
        System.out.println(res);
    }
}


2.Stall Reservations
题目链接
算法进阶指南-基础算法-贪心

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public  class Main
{

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n;
        n=sc.nextInt();
        row x[]=new row[50000+10];
        int room[]=new int[50000+10];

        for(int i=1;i<=n;i++)
        {   x[i]=new row();
            x[i].begin=sc.nextInt();
            x[i].end=sc.nextInt();
            x[i].z=i;
        }

        Arrays.sort(x,1,n+1);
        int ans=1;

        for(int i=1;i<=n;i++)
        {  int biao=0;
           int min=999999;
            for(int j=1;j<=ans;j++)
            {
                if(room[j]<x[i].begin&&room[j]<min)
                {
                    biao=j;
                    min=room[j];
                }
            }
            if(biao==0){
                ans++;
                room[ans]=x[i].end;
                x[i].ans=ans;
            }
            else
            {
                room[biao]=x[i].end;
                x[i].ans=biao;
            }

        }

        Arrays.sort(x, 1, n + 1, new Comparator<row>() {
            @Override
            public int compare(row o1, row o2) {
                return o1.z-o2.z;
            }
        });
        System.out.println(ans);
        for(int i=1;i<=n;i++)
            System.out.println(x[i].ans);
    }



}





 class row implements Comparable<row>{
    int begin,end;
    int ans;
    int z;

    @Override
    public int compareTo(row o) {
        return begin-o.begin;
    }




}

3.Radar Installation
题目链接
算法进阶指南-基础算法-贪心
代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int d = sc.nextInt();
		C[] node = new C[n];
		boolean f = true;
		int x,y;
		for(int i=0;i<n;i++) {
			x = sc.nextInt();
			y = sc.nextInt();
			if(y>d) {
				f = false;
				break;
			}
			double len = Math.sqrt(d*d-y*y);
			node[i] = new C(x-len,x+len);
		}
		if(!f) {
			System.out.println(-1);
		}else {
			Arrays.sort(node);
			int ans = 0;
			double r = -1e10;
			for(int i=0;i<n;i++) {
				if(node[i].l>r+1e-6) {
					ans++;
					r = node[i].r;
				}
			}
			System.out.println(ans);
		}
	}
}	
class C implements Comparable<C>{
	double l,r;
	public C(double l,double r) {
		this.l = l;
		this.r = r;
	}
	public int compareTo(C o) {
		if(this.r>o.r)
			return 1;
		else
			return -1;
	}
}

4.国王的游戏
题目链接
算法进阶指南-基础算法-贪心
代码:


import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// @author:jahup
public class Main {


    public static void main( String args[] ){

        Scanner sc=new Scanner(System.in);
        int n;
        final int N=10000+10;

        n=sc.nextInt();
        human all[]=new human[N];
        for(int i=1;i<=n+1;i++){
            all[i]=new human(sc.nextInt(),sc.nextInt());

        }

        Arrays.sort(all,2,n+2);

        BigInteger all_=new BigInteger("1");
        BigInteger max=new BigInteger("0");
        for(int i=1;i<=n+1;i++){
            BigInteger now_x=new BigInteger(all[i].x.toString());
            BigInteger now_y=new BigInteger(all[i].y.toString());
            max=all_.divide(now_y).compareTo(max)>0?all_.divide(now_y):max;
            all_=all_.multiply(now_x);
        }
        System.out.println(max.toString());
    }
}
class human implements Comparable<human>{
    Integer x,y;

    public human(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int compareTo(human o) {
        return  x*y-o.x*o.y;
    }
}

5.Color a Tree
题目链接
算法进阶指南-基础算法-贪心
代码:

import java.util.Scanner;

public class Main {
	static int n,root;
	static E[] node = new E[1005];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		root = sc.nextInt();
		int res = 0;
		for(int i=1;i<=n;i++) {
			int a = sc.nextInt();
			node[i] = new E(i,a,1,a);
			res += a;    //每个都先是独立的点
		}
		for(int i=1;i<n;i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			node[b].father = a;
		}
		for(int i=1;i<n;i++) {
			int p = find();   //找到均值最大的点
			int f = node[p].father; 
			res += node[p].sum*node[f].size;  //与父节点合并  添加在后面  
			node[p].avg = -1;   //与父节点已经合并  删出该点   
			for(int j=1;j<=n;j++) {   //在把该节点的子节点与新父节点连接
				if(node[j].father == p) {
					node[j].father = f;
				}
			}
			//更新父节点
			node[f].sum += node[p].sum;
			node[f].size += node[p].size;
			node[f].avg = node[f].sum*1.0/node[f].size;
		}
		System.out.println(res);
	}
	public static int find() {
		int t = 0;
		double avg = 0;
		for(int i=1;i<=n;i++) {
			if(i!=node[i].father&&avg<node[i].avg) {
				avg = node[i].avg;
				t = i;
			}
		}
		return t;
	}
}
class E{
	int father,sum,size;
	double avg;
	public E(int father,int sum,int size,double avg) {
		this.father = father;
		this.sum = sum;
		this.size = size;
		this.avg = avg;
	}
}

本文地址:https://blog.csdn.net/jahup/article/details/110236150