【习题 8-4 UVA - 11491】Erasing and Winning

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

考虑删掉第i位。 则第i+1位就会取代第i位。 则肯定第i+1位比第i位大的话,才比较好。 则从小到大贪心删,找到第一个a[i+1]>a[i]的i. 然后每次删掉这样的i就可以了。

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
  	8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;

int n,d,l[N+10],r[N+10];
char s[N+10];

void sc(int x){
    int L = l[x],R = r[x];
    r[L] = R;
    l[R] = L;
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    while (cin >> n >> d && n && d){
        cin >> (s+1);
        for (int i = 0;i <= n;i++)
            l[i] = i-1,r[i] = i+1;
        int now = r[0];
        while (1){
            if (d==0)break;
            int nex = r[now];
            while (nex <= n && s[nex]<=s[now]){
                now = r[now];
                nex  = r[nex];
            }
            sc(now);
            now = l[now];
            if (now==0) now = r[now];
            d--;
        }
        for (int i = r[0];i!=n+1;i=r[i])
            cout << s[i];
        cout << endl;
    }
	return 0;
}
posted @ 2018-01-07 14:42  AWCXV  阅读(145)  评论(0编辑  收藏  举报