首页 > 上网技巧 > 电脑小技巧 > 贪心算法经典例题

贪心算法经典例题

时间:2019-04-12 10:09 作者:QQ地带 我要评论

贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。
 
贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关。
 
FatMouse’ Trade
FatMouse’ Trade
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33703    Accepted Submission(s): 10981
 
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
 
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.
 
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
 
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
 
Sample Output
13.333
31.500
 
贪心思想
从每傍食物最便宜的开始
 
C++代码
C++
#include<iostream>
#include<algorithm>
using namespace std;
struct bean{
double pound;
double value;
double perv;
bool operator < (const bean &A)const{
return perv > A.perv;
}
}buff[100];
int main(){
int n;
double m;
while(cin>>m>>n){
if(m == -1 && n == -1)break;
for(int i = 0; i < n; i++){
scanf("%lf%lf", &buff[i].pound, &buff[i].value);
buff[i].perv = buff[i].pound/buff[i].value;
}
sort(buff, buff + n);
int id = 0;
double ans = 0;
while(m > 0 && id < n){
if(m > buff[id].value){
m -= buff[id].value;
ans += buff[id].pound;
}else{
ans += m*buff[id].perv;
m = 0;
}
id++;
}
cout<<ans<<endl;
}
return 0;
}
/*
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<algorithm>
 
using namespace std;
 
struct bean{
    double pound;
    double value;
    double perv;
    bool operator < (const bean &A)const{
        return perv > A.perv;
    }
}buff[100];
 
int main(){
    int n;
    double m;
    while(cin>>m>>n){
        if(m == -1 && n == -1)break;
        for(int i = 0; i < n; i++){
            scanf("%lf%lf", &buff[i].pound, &buff[i].value);
            buff[i].perv = buff[i].pound/buff[i].value;
        }
        sort(buff, buff + n);
        int id = 0;
        double ans = 0;
        while(m > 0 && id < n){
            if(m > buff[id].value){
                m -= buff[id].value;
                ans += buff[id].pound;
            }else{
                ans += m*buff[id].perv;
                m = 0;
            }
            id++;
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
*/
今年暑假不AC
“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”
 
确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)
 
Input
 
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。
 
Output
 
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。Sample Input
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0
 
贪心思想
按照结束时间的早晚来,先看结束时间早的
 
C++代码
C++
#include<iostream>
#include<algorithm>
using namespace std;
struct prog{
int start;
int endx;
bool operator < (const prog &A)const{
return endx < A.endx;
}
}buff[10000];
int main(){
int n;
while(cin>>n){
if(n == 0)break;
int ans = 1, time = 0;
for(int i = 0; i < n; i++){
cin>>buff[i].start>>buff[i].endx;
}
sort(buff, buff + n);
time = buff[0].endx;
for(int i = 1; i < n; i++){
if(buff[i].start >= time){
ans++;
time = buff[i].endx;
}
}
cout<<ans<<endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<algorithm>
using namespace std;
 
struct prog{
    int start;
    int endx;
    bool operator < (const prog &A)const{
        return endx < A.endx;
    }
}buff[10000];
 
int main(){
    int n;
    while(cin>>n){
        if(n == 0)break;
        int ans = 1, time = 0;
        for(int i = 0; i < n; i++){
            cin>>buff[i].start>>buff[i].endx;
        }
        sort(buff, buff + n);
        time = buff[0].endx;
        for(int i = 1; i < n; i++){
            if(buff[i].start >= time){
                ans++;
                time = buff[i].endx;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

标签: 贪心算法
顶一下
(0)
0%
踩一下
(0)
0%

Google提供的广告