Greedy algorithm - algorithm notes

how are you

Greedy algorithm: only consider local optimal solution so that the overall result of the method to achieve the optimal solution. That is, the middle of each step of the strategy is the optimal solution, so that the global reach most or superior results.

PAT 1020 moon cake (25 points)

Chinese people eat moon cake is in the Mid-Autumn Festival a traditional food, different regions have many different flavors of moon cake. Now given all kinds of moon cake inventory, total price, and the maximum demand of the market, you can get the maximum benefit calculated how much.

Note: Allow removing a portion of the stock sale. Case examples given is this: If we have three kinds of moon cake, its stocks were 18,15,10 tons, the total price respectively 75,72,45 billion. If the maximum market demand is only 20 million tons, then we should be the maximum revenue strategy to sell all of the 150,000 tons of the two kinds of moon cake, as well as 50,000 tons of the three kinds of moon cake, get 72 + 45/2 = 94.5 (million) .

Input formats:

Each input comprises a test. Each test case is given to a positive integer not more than 1000 N represents the number of types of moon cake, and no more than 500 (in units of thousands of tons) of the positive integer D represents the maximum demand of market. Then given N th row represents a positive number of stocks of each moon cake (in units of thousands of tons); N last line gives positive number indicates a total sales price of each moon cake (in units of billions). Between numbers separated by a space.

Output formats:

For each test, the maximum benefit in the output line, in units of billions and accurately to two decimal places.

Sample input:

3 20
18 15 10
75 72 45

 

Sample output:

94.50

answer:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

struct moon{
    double number,totalprice,singleprice;//库存,总价,单价
};

bool cmp(moon a,moon b){
    return a.singleprice>b.singleprice;
};

int main(){
    int n,d;
    cin>>n>>d;
    vector<moon> v(n);
    for(int i = 0;i<n;i++){
        cin>>v[i].number;
    }
    for(int i = 0;i<n;i++){
        cin>>v[i].totalprice;
        v[i].singleprice = v[i].totalprice/v[i].number;
    }
    sort(v.begin(),v.end(),cmp);
  

    double result=0.0;
    for(int i = 0;i<n;i++){
        if(v[i].number<=d) result =result + v[i].totalprice;
        else{
            result = result + v[i].singleprice*d;
            break;
        } 
        d = d - v[i].number;
    }
    printf("%.2f",result);
    return 0;
}

PAT 1023 set a minimum number (20 minutes)

0-9 each given a number. You can arrange these numbers in any order, but must be fully utilized. The goal is to make the resulting number as small as possible (do not pay attention to the first 0). For example: Given two 0, 1 two, three 5, a 8, we get the minimum number is 10015558.

The minimum number is now given a number, please write program output can be composed of.

Input formats:

Input 10 is given a non-negative integer in a row, we have the sequence represents the number 0, the number of number 1, number 9 ....... Separated by a space between integer. The total number of 10 digits is no more than 50, and has at least one non-zero digit.

Output formats:

The smallest number in a row can be output thereof.

Sample input:

2 2 0 0 0 3 0 0 1 0

 

Sample output:

10015558

answer:

 

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

int main() {
	vector<int> a(10);
	for(int i = 0;i<10;i++){
		cin>>a[i];
	}
	int n;
	for(int i = 1;i<10;i++){
		if(a[i]!=0){
			n = i;
			a[i] = a[i] - 1;
			break;	
		}
	}
	cout<<n;
	for(int i = 0;i<10;i++){
		for(int j = 0;j<a[i];j++){
			cout<<i;
		}
	}
	return 0;
}


//二刷
#include<iostream>
using namespace std;

int main(){
    int count[10];//记录数字0~9的个数
    for(int i = 0;i<10;i++){
        scanf("%d",&count[i]);
    }
    for(int i =1;i<10;i++){
        if(count[i]>0){
            printf("%d",i);
            count[i]--;
            break;
        }
    }
    for(int i =0;i<10;i++){
        for(int j = 0;j<count[i];j++){
            printf("%d",i);
        }
    }
    return 0;
}

 

 

Published 98 original articles · won praise 2 · Views 3699

Guess you like

Origin blog.csdn.net/qq_30377869/article/details/105027732