(贪心)删数问题

【题目】

过键盘输入一个高精度的正整数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.\n",cas,maxx);
        cas++;
    }
    return 0;
}

  

posted @ 2015-01-16 20:17  心田定则  阅读(225)  评论(0编辑  收藏  举报