zoukankan      html  css  js  c++  java
  • 贪心算法(各种贪心题目)

    感觉很多贪心的题目只要想到怎么贪心就很快能解决,但是没有想到的话代码量就会很大,而且很容易出错,所有贪心还是要多做题目,掌握各种贪心的题目

    题目链接:https://vjudge.net/contest/231313#problem/D

    A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.
    Input
    The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.
    Output
    The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null'' line of the input file.
    Sample Input
    0 0 4 0 0 1 
    7 5 1 0 0 0 
    0 0 0 0 0 0 
    Sample Output
    2 
    1 

    题目大意:有1*1,2*2···6*6尺寸的箱子,每种物品的高度确定,只用6*6的箱子,输入每种尺寸的物品有多少个,问你最少要用多少箱子,当六个0时推出循环
    个人思路:6*6,5*5,4*4的物品每一个都需要一个箱子来放,所以先考虑,4个3*3物品占满一个箱子,考虑要多少个箱子,然后考虑已经用了的箱子能放多少个2*2的,不够的话再用箱子
    最后考虑总共用了多少个箱子,把方块数求出来,减去用掉的方块数,就算当前的箱子能放的1*1的物品数量,不够再补箱子
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<cmath>
    #include<math.h>
    #include<algorithm>
    #include<set>
    typedef long long ll;
    using namespace std;
    const ll mod=1e9+7;
    #define INF 0x3f3f3f
    int main()
    {
        ll a[10];
        ll num[4]={0,5,3,1};
        while(1)
        {
            ll ans=0;
            ll tmp=0;
            for(int i=1;i<=6;i++)
            {
                scanf("%I64d",&a[i]);
                tmp+=a[i];
            }
            if(tmp==0)
                return 0;
            ans+=a[6]+a[5]+a[4]+(a[3]+3)/4;//a6,a5,a4每个物品需要一个箱子,4个a3需要一个箱子
            ll a2=a[4]*5+num[a[3]%4];//a6,a5,a4,a3有多少个空位子可以放2*2的物品
            if(a[2]>a2)
            {
                ans+=(a[2]-a2+8)/9;
            }
            int a1=ans*36-a[6]*36-a[5]*25-a[4]*16-a[3]*9-a[2]*4;//总共用的箱子还剩多少个能放a1
            if(a[1]>a1)
            {
                ans+=(a[1]-a1+35)/36;
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
    

      

    题目链接:https://vjudge.net/contest/231317#status/1751151850/D/0/

    Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.

    Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.

    Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches.

    The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.

    Output

    Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.

    Examples
    Input
    5
    rbbrr
    Output
    1
    Input
    5
    bbbbb
    Output
    2
    Input
    3
    rbr
    Output
    0
    Note

    In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.

    In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.

    In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.

    题目大意:输入一个长度为n的字符串,你可以交换两个字符或者改变任意一个字符,问你最少操作次数,使得br交替出现

    个人思路:其实就是有四种情况:

    1、当第一个字符为b时,你可以考虑要不要将它改变为r。  这里就是两种情况,改变或者不改变

    2、当第一个字符为r时,你可以考虑要不要讲它改变为b。 这里也是两种情况,改变或者不改变

    为什么是这样呢,因为任意一个字符串,只有第一个确定了,后面的也都确定了

    看代码

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<cmath>
    #include<math.h>
    #include<algorithm>
    #include<set>
    #include<queue>
    typedef long long ll;
    using namespace std;
    const ll mod=1e9+7;
    #define INF 0x3f3f3f
    char a[100050];
    char b[100050];
    int n;
    int solve(int p,int q,int sum1,int sum2)
    {
        while(q!=n)
            {
                if(b[q]==b[p]&&b[p]=='r')
                {
                    b[q]='b';
                    sum1++;
                    p++;
                    q++;
                }
                else if(b[q]==b[p]&&b[p]=='b')
                {
                    b[q]='r';
                    sum2++;
                    p++;
                    q++;
                }
                else
                {
                    p++;
                    q++;
                }
            }
            return max(sum1,sum2);//注意这里是什么意思,sum1代表r需要改变的次数,sum2代表b需要改变的次数,所以可以
    //交换的次数就是min(sum1,sum2),而剩下的就必须是改变的,大的减去小的加上min(sum1,sum2)就等价于max(sum1,sum2) }
    int main() { int ans1,ans2;//sum1是b的转移次数,sum2是r的转移次数 cin>>n; cin>>a; strcpy(b,a); //cout<<b; if(b[0]=='r') { ans1=solve(0,1,0,0); strcpy(b,a); b[0]='b'; ans2=solve(0,1,1,0); } else if(b[0]=='b') { ans1=solve(0,1,0,0); strcpy(b,a); b[0]='r'; ans2=solve(0,1,0,1); } // cout<<sum1<<sum2<<sum3<<sum4<<endl; cout<<min(ans1,ans2); return 0; }

    题目链接:https://vjudge.net/contest/231316#problem/D

    Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.

    In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.

    What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?

    Input

    The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.

    Output

    Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.

    Examples
    Input
    6 3
    Output
    4
    Input
    8 5
    Output
    3
    Input
    22 4
    Output
    6
    Note

    In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .

    In the second sample test, Memory can do .

    In the third sample test, Memory can do:

    .

    题目大意:输入x,y,x代表刚开始的等边三角形的边长,y代表最终的等边三角形的边长,问你从x变为y,最少要变多少次,注意,变的过程中也要是三角形

    个人思路:刚开始在找规律,应该说找到一个错误的规律,所以wa了,然后没什么想法了,百度了一下,看到个逆序来求,然后瞬间把它秒了,就是逆序来做

    看代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stdio.h>
    #include<string.h>
    #include<cmath>
    #include<math.h>
    #include<algorithm>
    #include<set>
    #include<queue>
    typedef long long ll;
    using namespace std;
    const ll mod=1e9+7;
    const int maxn=1e5+10;
    const ll maxa=1e10;
    #define INF 0x3f3f3f3f3f3f
    int a[5];
    int x,y,ans=0;
    void solve()
    {
        sort(a,a+3);
        int t=a[1]+a[2]-1;
        if(t<=x)
        {
            a[0]=t;
            ans++;
        }
        else
        {
            a[0]=x;
            ans++;
        }
    }
    int main()
    {
        cin>>x>>y;
        for(int i=0;i<3;i++)
        a[i]=y;
        while(a[0]!=x||a[1]!=x||a[2]!=x)
        {
            solve();
        }
        cout<<ans<<endl;
        return 0;
    }
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    Django部分面试题目
    网编部分
    面试题
    mysql安装
    并发编程
    集合以及深浅拷贝和和小数据池--个人一些经验总结
    稍微比较全的那种字典
    个人声明
    python
    python-pdf文件(持续更新
  • 原文地址:https://www.cnblogs.com/caijiaming/p/9304259.html
Copyright © 2011-2022 走看看