1. 程式人生 > >Erasing and Winning UVA - 11491 貪心

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 }