Erasing and Winning UVA - 11491 贪心

题目:题目链接

思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <deque>
11 
12 #define FRER() freopen("in.txt", "r", stdin)
13 #define FREW() freopen("out.txt", "w", stdout)
14 
15 #define INF 0x3f3f3f3f
16 
17 using namespace std;
18 
19 const int maxn = 1e5 + 5;
20 
21 char s[maxn], ans[maxn];
22 
23 int main()
24 {
25     //FRER();
26     //FREW();
27     ios::sync_with_stdio(0);
28     cin.tie(0);
29 
30     int n, d, m, idx;
31     while(cin >> n >> d && (n || d)) {
32         cin >> s;
33         m = n - d;
34         idx = 0;
35         ans[idx] = s[0];
36         for(int i = 1; i < n; ++i) {
37             while(d && idx >= 0 && ans[idx] < s[i]) {
38                 --idx;
39                 --d;
40             }
41             ans[++idx] = s[i];
42         }
43         ans[m] = '\0';
44         cout << ans << endl;
45     }
46     return 0;
47 }

猜你喜欢

转载自www.cnblogs.com/fan-jiaming/p/9894012.html
今日推荐