Erasing and Winning UVA - 11491

9 篇文章 0 订阅

题目传送门

题意:给你n个数字让你从这n个数字当中删除m个,使得最后得到的数字的大小最大。

思路:贪心即可,从前面仅可能选择最大的数字。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define MAXN 200000
#define MAXE 210
#define INF 10000000
#define MOD 1000000007
#define LL long long
#define PI acos(-1.0)

using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    string str;
    cin >> str;
    string order_str = "\0";
    int pos = 0;
    char max_char = 0;
    for (int i = 0; i + m < n; i = pos + 1) {
      if (m == 0) {
        break;
      }
      max_char = str[i];
      pos = i;
      for (int j = i; j <= i + m && j < n; ++j) {
        if (str[j] > max_char) {
          max_char = str[j];
          pos = j;
        }
      }
      m -= (pos - i);
      order_str += max_char;
    }
    if (m == 0) {
      for (int i = pos + 1; i < n; ++i) {
        order_str += str[i];
      }
    }
    cout << order_str << endl;
  }
  return 0;
}

/*
4 2
3759
6 3
123123
7 4
1000000
0 0
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值