算法笔记学习三(贪心)

ACM贪心:VJ

问题 E: FatMouse’s Trade

时间限制: 1 Sec 内存限制: 32 MB
提交: 475 解决: 262
[提交][状态][讨论版][命题人:外部导入]
题目描述
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.

输入
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.

输出
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.

样例输入
4 2
4 7
1 3
5 5
4 8
3 8
1 2
2 5
2 4
-1 -1
样例输出
2.286
2.500

本题主旨在交换商品,样例输入给出的第一个输入为每个房间能用多少个i,交换多少个j
那么根据贪心算法的思路:局部最优保证整体最优:
那么我们要做的就是:先选倍率高的,再选倍率低的,自然的,首先定义一个结构体数组,每个结构体包含三个变量
第一个:i
第二个:j
第三个:sprices,即倍率的 double型,i*1.0/j
然后将各组按照sprice进行排序,

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
struct Trade{
    int j;
    int f;
    double sprice;
}trade[1010];
bool cmp(Trade a,Trade b)
{
    return a.sprice>b.sprice;
}
int main()
{
    int m,n;
    while(scanf("%d %d",&m,&n),m!=-1&&n!=-1)
    {
        for(int i = 0;i < n;i++)
        {
            scanf("%d%d",&trade[i].j,&trade[i].f);
            trade[i].sprice = trade[i].j*1.0/trade[i].f;
        }
        sort(trade,trade+n,cmp);
        double sum = 0;
        for(int i = 0 ;i < n;i++)
        {
            if(m>=trade[i].f)
            {
                sum += trade[i].j;
                m -= trade[i].f;
            }
            else
            {
                sum += trade[i].sprice*m;
                break;
            }
        }
        printf("%.3f\n",sum);
    }
}

问题 A: 看电视

时间限制: 1 Sec 内存限制: 32 MB
提交: 1549 解决: 721
[提交][状态][讨论版][命题人:外部导入]
题目描述
暑假到了,小明终于可以开心的看电视了。但是小明喜欢的节目太多了,他希望尽量多的看到完整的节目。
现在他把他喜欢的电视节目的转播时间表给你,你能帮他合理安排吗?
输入
输入包含多组测试数据。每组输入的第一行是一个整数n(n<=100),表示小明喜欢的节目的总数。
接下来n行,每行输入两个整数si和ei(1<=i<=n),表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。
当n=0时,输入结束。
输出
对于每组输入,输出能完整看到的电视节目的个数。
样例输入
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
样例输出
5

思路:同样,局部到最优,将每组输入都进行从早到晚的排序,每当第二个输出的start小于第一个输出的end时,便不符合要求,需要舍弃,继续遍历下一个

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;

const int maxn = 110;
struct TVShows{
	int start;
	int end;
};

bool cmp(struct TVShows a, struct TVShows b){
	if(a.end != b.end)
		return a.end < b.end;
	else
		return a.start > b.start;
}

int main(){
	int n;
	while(scanf("%d", &n) != EOF){
		if(n == 0) break;
		struct TVShows *tvs = (struct TVShows*)malloc(n*sizeof(struct TVShows));
		for(int i=0; i<n; i++)
			scanf("%d %d\n", &tvs[i].start, &tvs[i].end);
		sort(tvs, tvs+n, cmp);
		int count = 1, t = tvs[0].end;
		for(int i=1; i<n; i++){
			if(tvs[i].start >= t){
				count++;
				t = tvs[i].end;
			}
		}
		printf("%d\n", count);
	}
	return 0;
}

问题 D: Repair the Wall

Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon.
When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what made the situation worse was that all of Kitty’s walls were made of wood.
One day, Kitty found that there was a crack in the wall. The shape of the crack is
a rectangle with the size of 1×L (in inch). Luckly Kitty got N blocks and a saw(锯子) from her neighbors.
The shape of the blocks were rectangle too, and the width of all blocks were 1 inch. So, with the help of saw, Kitty could cut down some of the blocks(of course she could use it directly without cutting) and put them in the crack, and the wall may be repaired perfectly, without any gap.
Now, Kitty knew the size of each blocks, and wanted to use as fewer as possible of the blocks to repair the wall, could you help her ?

输入
The problem contains many test cases, please process to the end of file( EOF ).
Each test case contains two lines.
In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which
mentioned above.
In the second line, there are N positive integers. The ith integer Ai(0<Ai<1000000000 ) means that the ith block has the size of 1×Ai (in inch).

输出
For each test case , print an integer which represents the minimal number of blocks are needed.
If Kitty could not repair the wall, just print “impossible” instead.

样例输入
2 2
12 11
14 3
27 11 4
109 5
38 15 6 21 32
5 3
1 1 1

样例输出
1
1
5
impossible

思路:将第二行输入(提供的数量)进行由大到小的排序,累加,知道大于等于给定的临界值为止,定义一个count进行计数,看需要几个,得到局部最优。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
bool cmp(int a,int b)
{
    return a<b;
}
int main()
{

    int L,N;
    while(scanf("%d %d",&L,&N)!=EOF)
    {
        int count = 0;
    int sum = 0;
    bool flag = false;
        int num[N];
        for(int i = 0; i < N; i++)
        {
            scanf("%d",&num[i]);
        }
        sort(num,num+N,cmp);
        //从小到大排列
        while(N--)
        {
        if(sum < L)
            {
                sum += num[N];
                count++;
            }

            if(sum>=L)
            {
                flag = true;
                printf("%d\n",count);
                break;
            }
        }
        if(flag==false)
            printf("impossible");
    }
}

问题 G: 找零钱

时间限制: 1 Sec 内存限制: 128 MB
提交: 523 解决: 361
[提交][状态][讨论版][命题人:外部导入]
题目描述
小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为50 20 10 5 1 五种。请在知道要找多少钱n给小明的情况下,输出纸币数量最少的方案。 1<=n<=99;
输入
有多组数据 1<=n<=99;
输出
对于每种数量不为0的纸币,输出他们的面值数量,再加起来输出
样例输入
25
32
样例输出
20
1+51
20
1+101+12

思路:输出纸币数量最少的方案,显然,算法:
纸币面额由大到小排列,进行叠加判断是否大于等于金额,记录每次不同金额纸币的数量,输出时末尾不能有多余输出

#include <stdio.h>
#include <algorithm>
using namespace std;

int main()
{
    int price;
    while(scanf("%d",&price)!=EOF)
    {
        int a[5] = {50, 20, 10, 5, 1}, m = 0;
        int b[5] = {0};

        while(price>0)
        {
            if(a[m]>price)
            {
                m++;
                continue;
            }

            b[m] = price / a[m];
            price = price % a[m];
            m++;
        }
        m--;
        for (int i = 0; i <5 ;i++)
        {
            if(b[i]==0)
                continue;

            if(i!=m)
                printf("%d*%d+", a[i], b[i]);
            else
                printf("%d*%d", a[i], b[i]);
        }
        printf("\n");
    }
    return 0;
}

军官要给手下的n个士兵发奖章,给定n个整数,可在每个数上多次加1,要求最终结果所有n个数均不相等,求最少要加多少次1。

首先我们设一个数组a来储存所有n个数字,按升序排序。对a数组扫一遍,如果a[i]==a[i+1],则需要改变数的大小。那么我们怎么知道要加多少个一呢?在本题中我设了一个ans标记用来表示比a[i+1]大且不存在a中的最小整数,ans-a[i+1]即为我们要加1的个数。
注意:每次加1之后整个a数组就会发生改变,为此我定义了一个数组b来表示变化后的a数组,以便于求ans。

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<string>
#include<ctype.h>
#include<map>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,i;
    while(scanf("%d",&n)!=EOF)
    {
        int a[60001] = {0},k;
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&k);
            a[k]++;
        }
        int sum = 0,counts = 0,i = 1;
        while(sum != n)
        {
            while(a[i]>1)
            {
                counts++;
                a[i]--;
                a[i+1]++;
            }
            sum += a[i];
            i++;
        }
        printf("%d\n",counts);
    }
    return 0;
}



散列和贪心的结合思想


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值