zoukankan      html  css  js  c++  java
  • (贪心)删数问题

    【题目】

    过键盘输入一个高精度的正整数n(n的有效位数≤240),去掉其中任意s个数字后,剩下的数字按

    原左右次序将组成一个新的正整数。编程对给定的n 和s,寻找一种方案,使得剩下的数字组成的新数最小。

    输入:n

    s

    输出:最后剩下的最小数

    【样例输入】

    178543

    S=4

    【样例输出】

    13

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    using namespace std;
    char s[100010];
    int k,len;
    void work()
    {
       int temp=k;
       while(temp--)
       {
          int i=0;
          while(i<len-1&&s[i]<=s[i+1]) i++;
          for(int j=i+1;j<len;j++)
                s[j-1]=s[j];
       }
    }
    int main()
    {
          cin>>s>>k;
          len=strlen(s);
          work();
          for(int i=0;i<len-k;i++)
                printf("%c",s[i]);
    }
    

      

    hdu 1692

    Destroy the Well of Life

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 930    Accepted Submission(s): 377


    Problem Description
    In the game of DotA (Defense of the Ancient), there are two opposite legions called The Sentinel and The Scourage. 
    Now The Scourage has made a big success and The Sentinel is at stake!
    So The Sentinel must destroy the Well of Life of The Scourage.
    The chief of The Sentinel, Prophet, asks EarthShaker to do this arduous task.

    There are N Wells of Life of The Scourage (The Wells of Life are numbered from 1 to N), and EarthShaker’s task is to destroy the Nth Well of Life.
    The following information is known about each Well of Life:
    Wi – the weight of water on i-th Well of Life before it is destroyed.
    Li – if the weight of water on i-th Well of Life is more than Li, the i-th Well of Life will be destroyed and the water of it will pours to the (i + 1)-th Well of Life.
    Pi – EarthShaker has a skill called Echo-Slam, the i-th Well of Life will be immediately destroyed when he uses Echo-Slam to it and the water of it will pours to the (i + 1)-th Well of Life. For the i-th Well of Life, the energy that EarthShaker need to use Echo-Slam to destroy it is Pi. 

    Can you tell EarthShaker the minimum amount of energy needed to destroy the Nth Well of Life? 

     
    Input
    The input consists of several test cases. There is a single number on the first line, the number of cases. There are no more than 10 cases.
    Each case contains a natural number N on the first line, 1<=N<=100,000.
    Following N lines contains three numbers Wi, Li, Pi (Wi<=Li, 0<=Wi, Li, Pi <=20,000), representing the information of the i-th Well of Life.
     
    Output
    For each case, print a number in a line representing the least amount of energy EarthShaker needed to use, so as to destroy the Nth Well of Life. Use the format in the sample.
     
    Sample Input
    1 3 1000 1000 1 0 1000 2 2 10 100
     
    Sample Output
    Case 1: Need to use 3 mana points.
     
    Source
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<string>
    using namespace std;
    #define maxn 100010
    int tt,n;
    int w[maxn],p[maxn],l[maxn],maxx;
    int main()
    {
        int sum,carry,cas;
        cas=1;
        scanf("%d",&tt);
        while(tt--)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%d%d%d",&w[i],&l[i],&p[i]);
            maxx=p[n];
            for(int i=1;i<n;i++)
            {
                sum=0,carry=0;
                for(int j=i;j<=n;j++)
                {
                    carry+=w[j];
                    if(carry<=l[j])
                        sum+=p[j];
                    if(sum>maxx)
                        break;
                }
                if(sum<maxx)
                    maxx=sum;
            }
            printf("Case %d: Need to use %d mana points.
    ",cas,maxx);
            cas++;
        }
        return 0;
    }
    

      

  • 相关阅读:
    layui2.5 修改layuicms
    linux ubuntu安装node npm cnpm nvm nrm yarn vue-cli vue-router
    浅析微信支付:(余额提现)企业付款到微信用户零钱或银行卡账户
    pdo类的使用
    生成url的二维码图片
    点击提交按钮后 禁用提交按钮3秒后 再启用
    Maven项目Update Project后JRE System Library自动变回1.5解决办法
    一步步搭建 Spring Boot maven 框架的工程
    AspectJ报错:error at ::0 can't find referenced pointcut XXX
    SpringBoot 文件上传实践
  • 原文地址:https://www.cnblogs.com/a972290869/p/4229705.html
Copyright © 2011-2022 走看看