贪心策略
- 问题分解成为多个子问题
- 子问题求局部最优解
- 局部最优解组合成原问题的解
简单贪心
例题7.2 FatMouse’s Trade
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1000;
struct JavaBean{
double weight;
double cost;
};
JavaBean arr[MAXN];
bool Compare(JavaBean x,JavaBean y){
return x.weight/x.cost>y.weight/y.cost;
}
int main(){
int n,m;
while(scanf("%d%d",&m,&n)!=EOF){
if(n==-1&&m==-1){
break;
}
for(int i=0;i<n;++i){
scanf("%lf%lf",&arr[i].weight,&arr[i].cost);
}
sort(arr,arr+n,Compare);
double answer=0;
for(int i=0;i<n;++i){
if(m>=arr[i].cost){
m-=arr[i].cost;
answer+=arr[i].weight;
}else{
answer+=arr[i].weight*(m/arr[i].cost);
m=0;
break;
}
}
printf("%.3f\n",answer);
}
return 0;
}
例题7.3 Senior’s Gun
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=100000+10;
long long gun[MAXN];
long long monster[MAXN];
bool Compare(long long x,long long y){
return x>y;
}
int main(){
int caseNumber;
scanf("%d",&caseNumber);
while(caseNumber--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%lld",&gun[i]);
}
for(int j=0;j<m;++j){
scanf("%lld",&monster[j]);
}
sort(gun,gun+n,Compare);
sort(monster,monster+m);
long long answer=0;
for(int i=0;i<n;i++){
if(i>=m||gun[i]<=monster[i]){ //疑问:枪和monster相等为什么不行
break;
}
answer+=gun[i]-monster[i];
}
printf("%lld\n",answer);
}
return 0;
}
ECNU 1045 箱子打包
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10;
int length[MAXN];
int main(){
int n,l;
scanf("%d%d",&n,&l);
for(int i=0;i<n;++i){
scanf("%d",&length[i]);
}
sort(length,length+n);
int left=0;
int right=n-1;
int answer=0;
while(left<=right){
if(length[left]+length[right]<=l){
left++;
right--;
}else{
right--;
}
answer++;
}
printf("%d\n",answer);
return 0;
}
POJ 2456 Aggressive cows
Aggressive cows
Description
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don’t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
Input
-
Line 1: Two space-separated integers: N and C
-
Lines 2…N+1: Line i+1 contains an integer stall location, xi
Output -
Line 1: One integer: the largest minimum distance
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
最大的最小间距:二分策略
思路:利用二分和贪心结合的思想,通过二分来猜测到一个值,然后进行判断,如果答案可行,则猜的距离可以再大些,如果答案不可行,则猜的距离就要再小些。在判断答案是否可行时,可以用cnt来记录匹配到槽的奶牛数,先将第一头奶牛放在第一个槽,然后依次枚举每个槽,当枚举的槽与前一个放奶牛的槽的距离>=猜的答案时,cnt++,最后如果cnt>=牛的数量,说明这个距离可行,如果cnt<牛的数量,说明这个距离太大了。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10;
int arr[MAXN];
bool Judge(int n,int m,int distance){ //判定可行性
int current=arr[0];
int number=1; //牛的数量
for(int i=1;i<n;++i){
if(arr[i]-current>=distance){
number++;
current=arr[i]; //更新现在的位置
}
if(number>=m){
return true;
}
}
return false;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
sort(arr,arr+n);
int left=1; //间距的最小值
int right=arr[n-1]-arr[0]; //间距的最大值
while(left<=right){
int middle=left+(right-left)/2;
if(Judge(n,m,middle)){
left=middle+1; //让距离增大一点
}else{
right=middle-1; //让距离变小一点
}
}
printf("%d\n",right); //求最小的最大
}
return 0;
}
注意:mid = (left + right) / 2 容易溢出!因为left+right很容易超过int范围!而mid = left + (right - left) / 2 不容易溢出,所以建议以后写二分时要用mid = left + (right - left) / 2
Drying
Description
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
Input
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
Output
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #1
3
2 3 9
5
sample input #2
3
2 3 6
5
Sample Output
sample output #1
3
sample output #2
2
思路:假设X1是吹风耗费的时间, 那么 mid - X1 就是自然烘干的时间。
那么要烘干一件水分多的衣服要花费的时间就是 k * X1 + (mid - X1) >= a[i]
其中a[i]就是水分多的衣服的水量。移项后得出 X1 >= (a[i] - mid)/(k-1)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=1e5+10;
int water[MAXN];
bool Judge(int n,int k,int time){
int sum=0;
for(int i=0;i<n;i++){
if(water[i]>time){
sum+=ceil((water[i]-time)*1.0/(k-1)); // 注意理解k-1,向上取整,且取整函数用于浮点数所以乘1.0
}
if(sum>time){
return false;
}
}
return true;
}
int main(){
int n;//衣服的件数n
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&water[i]); //每件衣服含水量
}
int k;
scanf("%d",&k);
sort(water,water+n);
if(k==1){
printf("%d\n",water[n-1]);
}else{
int left=1;
int right=water[n-1];
while(left<=right){
int middle=left+(right-left)/2;
if(Judge(n,k,middle)){
right=middle-1;
}else{
left=middle+1;
}
}
printf("%d\n",left);
}
return 0;
}```