SDNUOJ 1098 观妹挑菊(贪心)

观妹挑菊题目链接

1098.观妹挑菊

Time Limit: 1000 MS    Memory Limit: 32768 KB
Total Submission(s): 70    Accepted Submission(s): 32

Description

话说上次观妹种了不少菊花之后,在采摘的时候遇到了不少麻烦
每个菊花的花瓣数目是不一样的,观妹要在n朵菊花中,剔除掉p朵菊花
保证原有顺序的情况下,使得菊花花瓣数组成的序列最大
现在请你来帮观妹来完成这个任务

Input

多个测试样例,每个样例包含两个数字n和p
1<=n<=10^100,每一位表示一个菊花
1<=p<100

Output

分别输出余下的菊花瓣数按原次序组成的最大的数

Sample Input

785452362215 5
92081346718538 10
1008908 5

Sample Output

8562215
9888
98

Hint

例如,有5朵菊花
花瓣数依次为54321
要剔除3朵菊花
那就变成了54
这样组成的序列是最大的

Source

SDNU ACM-ICPC 2012 Training Weekly Contest(Freshman/12-16)

也不知道观妹是哪位女装大佬hhh

这道题一看就是贪心,策略不同,然而我写了50行1000多b的代码,鹏哥的200b?。。。好奇心趋势我托人要来了他的代码。。

其实思路相似,都是从字符串当前位置往后开始寻找是否存在可以替代当前位置的数,如果没有就跳过,但是鹏哥是直接把这种情况简化了,直接把i的位置保留下来,删去i的位置处的数,我是最后一块删,而且他用了字符串,而我,用的是字符数组,好麻烦且臃肿的说。。。难受,多练。

我的是这样的:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
char a[105];
bool vis[105];
int main()
{
    int p;
    while(scanf("%s%d",a,&p) != EOF)
    {
        memset(vis, 0, sizeof(vis));
        int tot = p;
        int pp = p;
        int n = strlen(a);
        for(int i = 0; i < n; i ++)
        {
            int maxy = (int)(a[i] - '0');
            for(int j = i + 1; j <= i + p; j ++)
            {
                int k = (int)(a[j] - '0');
                if(maxy < k)
                {
                    tot --;
                    vis[i] = 1;
                    break;
                }
            }
            p = tot;
            if(tot == 0) break;
        }
        int len = n - pp;
        for(int i = 0; i < n; i ++){
            if(! vis[i])
            {
                printf("%c",a[i]);
                len --;
            }
            if(len == 0) break;
        }
        printf("\n");
    }
    return 0;
}

鹏哥的是这样的:

猜你喜欢

转载自blog.csdn.net/qq_41444888/article/details/81233882
今日推荐