贪心——Playing with numbers ( Gym 101061 E )

  • 题目链接:
    http://codeforces.com/gym/101061/problem/E

  • 分析:
    给出一个长度最大为100000位的数字,求从中剔除掉N个数字后得到到最大数和最小数。

  • 题解:
    非常简单的一道贪心:
    若是找最小数,那么维持原来的数字单调递增,若递减,则把前面的数字删去直到递增,遍历一遍后如果没删够,则从后面删。
    若是找最大数,那么维持原来的数字单调递减,若递增,则把前面的数字删去直到递减,便利一遍后如果每删够,则从后面删。

  • AC代码:
    这里写图片描述

/*************************************************************************
    > File Name: E.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
    > Created Time: 2016年08月15日 星期一 15时43分48秒
 ************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;
#define MaxN 100000
#define MaxM MaxN*10
#define INF 1000000000
#define bug cout<<88888888<<endl;
int T,N;
char S[112345];
char MIN[112345];
char MAX[112345];
void output(char x[],int ll)
{
    for(int i=ll-1;i>=0;i--)
    {
        cout << x[i];
    }
    cout << endl;
}
int main()
{
    scanf("%d", &T);
    while(T--)
    {
        string S;
        int N;
        cin >> S;
        scanf("%d", &N);
        stack<char> Stack;

        int K = N;
        for (int i = 0; i < S.length(); i++)
        {
            while (!Stack.empty() && K > 0 && S[i] < Stack.top())
            {
                Stack.pop();
                K--;
            }
            Stack.push(S[i]);
        }
        while(K--) Stack.pop();
        int cnt = 0;
        while(!Stack.empty())
        {
            MIN[cnt++] = Stack.top();
            Stack.pop();
        }

        K = N;
        for(int i = 0;i<S.length();i++)
        {
            while( !Stack.empty() && K>0 && S[i] > Stack.top() )
            {
                Stack.pop();
                K--;
            }
            Stack.push(S[i]);
        }
        while(K--) Stack.pop();
        cnt = 0;
        while(!Stack.empty())
        {
            MAX[cnt++] = Stack.top();
            Stack.pop();
        }

        output(MIN, S.length()-N);
        output(MAX, S.length()-N);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值