UVa 11491 奖品的价值

https://vjudge.net/problem/UVA-11491

题意:一个n位整数,删除其中的d个数字,输出最大值。

思路:肯定从高位开始分析,代码如下。

 1 #include<string>
 2 #include<iostream>  
 3 using namespace std;
 4 
 5 const int maxn = 100000 + 5;
 6 
 7 int n, d;
 8 char a[maxn];
 9 
10 int main()
11 {
12     //freopen("D:\txt.txt", "r", stdin);
13     while (cin>>n>>d && n&&d)
14     {
15         getchar();
16         int k = 0;
17         for (int i = 0; i < n; i++)
18         {
19             char c = getchar();
20             while (k>0 && k+(n-i)>n-d && a[k] < c)//需要保留n-d个数字,已经填写到第k个,还有n-i个未填写  
21                 k--;                      //若k+(n-i)>n-d,说明已经填写的当中有需要删除的,此时选择删除小于c的数字  
22             if (k < n-d) a[++k] = c;//若k<n-d,说明还没有填写够n-d个数字  
23         }
24         a[++k] = '';
25         cout << a+1 << endl;
26     }
27     return 0;
28 }
原文地址:https://www.cnblogs.com/zyb993963526/p/6354314.html