习题8-4 UVA - 11491 Erasing and Winning 奖品的价值(滑动窗口)

大体题意:

给你一个n 位的整数,让你删除其中d个数字,使得剩下的n-d个数字组成的数尽量大!

问最大是多少?

思路:

因为n最大是100000,所以存数字要用char 数组来存,

然后遍历每一位数字s[i];

在建立一个char ans[maxn] 来存取答案!

int cnt表示ans数组第几个数值,那么n - i 为剩下的还没遍历的数的个数,cnt 表示已经加入的数的个数,n-d 为最终目标数的个数

所以上来先判断能不能删除ans 中数,

当cnt + n - i > n -d时 并且 s[i] > ans[cnt]时才删,删的话--cnt即可!

删除完之后 判断能否向ans 数组中加入数字,  也就是cnt < n -d  的话  ans 就加入数字。

最后输出ans数组即可!

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 10;
char s[maxn];
char ans[maxn];
int main(){
	int n,d;
	while(scanf("%d%d",&n,&d) == 2 && (n || d)){
		getchar();
		gets(s);
		int cnt = 0;
		for (int i = 0; s[i]; ++i){
			while(cnt && cnt + n-i > n-d && ans[cnt] < s[i])--cnt;
			if (cnt < n-d)ans[++cnt] = s[i];
		}
		ans[cnt+1] = 0;
		puts(ans+1);
		
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值