uva 11491 Erasing and Winning 奖品的价值(贪心)
2017-08-14 19:42
477 查看
Juliano is a fan of the TV show Erasing and Winning, where participants are selected in a draw andreceive 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 eraseexactly D digits from the number in the board; the number formed by the remaining digits is the valueof 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, giventhe number the presenter wrote in the board, and the number of digits Juliano must erase, determinesthe 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 wrotein the board and the number
of digits that must be erased. The next line contains the number thepresenter 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, containingthe highest prize Juliano can win.
Sample Input
4 2
3759
6 3
123123
7 4
1000000
0 0
Sample Output
79
323
100
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char a[100010];
int main()
{
//freopen("outD.txt","w",stdout);
int n,d;
while(cin>>n>>d&&n+d)
{
memset(a,0,sizeof(a));
int k=0;
char c;
for(int i=0;i<n;i++)
{
cin>>c;
while(k>0&&c>a[k]&&k>i-d)k--;
if(k+d<n) a[++k]=c;
}
a[++k]='\0';
cout<<(a+1)<<endl;
}
return 0;
}
In the show, the presenter writes a number of N digits in a board. The participant must then eraseexactly D digits from the number in the board; the number formed by the remaining digits is the valueof 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, giventhe number the presenter wrote in the board, and the number of digits Juliano must erase, determinesthe 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 wrotein the board and the number
of digits that must be erased. The next line contains the number thepresenter 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, containingthe highest prize Juliano can win.
Sample Input
4 2
3759
6 3
123123
7 4
1000000
0 0
Sample Output
79
323
100
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char a[100010];
int main()
{
//freopen("outD.txt","w",stdout);
int n,d;
while(cin>>n>>d&&n+d)
{
memset(a,0,sizeof(a));
int k=0;
char c;
for(int i=0;i<n;i++)
{
cin>>c;
while(k>0&&c>a[k]&&k>i-d)k--;
if(k+d<n) a[++k]=c;
}
a[++k]='\0';
cout<<(a+1)<<endl;
}
return 0;
}
相关文章推荐
- UVA 11491 Erasing and Winning 奖品的价值 (贪心)
- 习题8-4 UVA - 11491 Erasing and Winning 奖品的价值(滑动窗口)
- UVa 11491 Erasing and Winning (贪心,单调队列或暴力)
- UVA - 11491 Erasing and Winning 贪心
- (贪心+栈)UVa 11491 Erasing and Winning
- UVA 11491 Erasing and Winning(贪心)
- UVa 11491 - Erasing and Winning(贪心)
- Uva 11491 Erasing and Winning(贪心)
- uva 11491 - Erasing and Winning
- UVA11491:Erasing and Winning(奖品的价值)
- 【思路、优化】UVa 11491 - Erasing and Winning
- UVA 11491 Erasing and Winning
- Uva - 11491 - Erasing and Winning
- Uva - 11491 - Erasing and Winning
- [UVa 11491] 奖品的价值(Erasing and Winning)
- Erasing and Winning UVA - 11491
- uva 11491 Erasing and Winning
- UVa11491- Erasing andWinning(从一题看多解)
- 11491 - Erasing and Winning(贪心)
- 奖品的价值 Erasing and Winning