uva 11491/Erasing and Winning

来源:互联网 发布:cs漫画软件 编辑:程序博客网 时间:2024/05/21 12:21
Juliano is a fan of the TV show Erasing and Winning, where participants are selected in a draw and
receive money for taking part in the show.
In the show, the presenter writes a number of N digits in a board. The participant must then erase
exactly D digits from the number in the board; the number formed by the remaining digits is the value
of the money prize for the participant.
Juliano was at last selected to take part in the show, and asked you to write a program that, given
the number the presenter wrote in the board, and the number of digits Juliano must erase, determines
the highest value of the prize he can win.
Input
The input contains several test cases. The first line of a test case contains two integers N and D
(1 ≤ D < N ≤ 105
) indicating respectively the number of digits of the number the presenter wrote
in the board and the number of digits that must be erased. The next line contains the number the
presenter wrote; the number does not start with a zero.
The end of input is indicated by a line containing only two zeros, separated by a space.
Output
For each test case in the input your program must produce one single line in the output, containing
the highest prize Juliano can win.
Sample Input
4 2
3759
6 3
123123
7 4
1000000
0 0
Sample Output
79
323

100

题意:在长度为n的整数中选择性删掉m个数字使最后剩下的数最大。

思路:可以想成是从中选n-m个数字使他们组合成最大的数--长度为l=n-m,从最左边的开始选,第一个可选的范围是0~n-l,在其中选最大的,第二个范围是第一个所选的位置i的下一个到i+1~~n-(l-1),从中选最大的,。。。。。后面一样。

代码:

<span style="font-family:Arial;">#include <iostream>#include<string.h>#include<stdio.h>using namespace std;int n,m,a[100005];char s[100005];int main(){    int i,j,q,p,l,ma,v;    while(scanf("%d %d",&n,&m)&&(n+m))    {        cin.get();        gets(s);        l=n-m;        q=0,p=n-l;        for(i=1,j=1;i<=n-m;i++)        {            ma=0;            //cout<<q<<" "<<p<<endl;            for(int r=q;r<=p;r++)            {                int d=s[r]-'0';                if(d>ma) ma=d,v=r;            }            a[j++]=ma;            l--;            q=v+1,p=n-l;        }        for(i=1;i<j;i++)            cout<<a[i];        cout<<endl;    }    return 0;}</span>


0 0
原创粉丝点击