習題8-4 獎品的價值(Erasing and Winning, UVa11491)

思路:
貪心,由於數的特性,顯然貪心可得正解,每次選取可選的最大值。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)  
#define SFF(a, b) scanf("%d%d", &a, &b)  
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define PB push_back
#define LL long long
#define mod 10007
#define inf 100000007
#define eps 1e-12
using namespace std;
int buf[20];
int read() {
	int x = 0; char ch = getchar(); bool f = 0;
	while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
void write(int x) {
	if (!x) { putchar(48); return; }
	int l = 0; if (x < 0) putchar('-'), x = -x;
	while (x) buf[++l] = x % 10, x = x / 10;
	while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
const int maxn = 1e5 + 5;
char s[maxn];

char get_max(int l, int r, int &nl) {
	char ret = '0';
	FOR(i, l, r + 1) {
		if (s[i] > ret) ret = s[i], nl = i + 1;
	}
	return ret;
}

int main() {
	int n, k;
	while (~SFF(n, k)) {
		if (n == 0 && k == 0) break;
		k = n - k;
		scanf("%s", s);
		string ans;
		int l = 0, r = n - k;
		int nl;
		while (k--) {
			char ch = get_max(l, r, nl);
			ans += ch;
			l = nl;
			r = n - k;
		}
		cout << ans << endl;
	}
	return 0;
}
維護一個單調棧,便於在一次掃描中得到最優解。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)  
#define SFF(a, b) scanf("%d%d", &a, &b)  
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define PB push_back
#define LL long long
#define mod 10007
#define inf 107
#define eps 1e-12
using namespace std;
int buf[20];
int read() {
	int x = 0; char ch = getchar(); bool f = 0;
	while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
void write(int x) {
	if (!x) { putchar(48); return; }
	int l = 0; if (x < 0) putchar('-'), x = -x;
	while (x) buf[++l] = x % 10, x = x / 10;
	while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
const int maxn = 100005;
char s[maxn];

int main() {
	int n, k;
	while (~SFF(n, k)) {
		if (n == 0 && k == 0) break;
		int need = n - k;
		stack<char> sta;
		sta.push('9');
		scanf("%s", s);
		FOR(i, 0, n) {
			if (need - SZ(sta) + 1 == n - i) {
				while (i < n) sta.push(s[i++]);
			} 
			else {
				if (s[i] > sta.top()) {
					while (s[i] > sta.top() && (SZ(sta) - 1 + n - i) > need) //大的數考慮是否數夠多
						sta.pop();
				}
				if(SZ(sta) - 1 < need) sta.push(s[i]);	//小的數考慮是否多了
			}
		}
		vector<char> ans;
		while (SZ(sta) != 1) {
			ans.push_back(sta.top());
			sta.pop();
		}
		for (int i = SZ(ans) - 1; i >= 0; --i)
			putchar(ans[i]);
		putchar('\n');
	}
	return 0;
}


發佈了222 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章