(经典贪心)— 删数问题

B:删数问题

时间限制: C/C++ 1000ms; Java 2000ms 内存限制: 65535KB

通过次数: 3 总提交次数: 6

问题描述

键盘输入一个正整数N,去掉其中任意S个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的N和S,寻找一种方案使得剩下的数字组成的新数最小。(N不超过240位,N > S)

输入描述

两行,第一行:正整数n,第二行:正整数S。

输出描述

  • n去掉的s个数字后组成的新的正整数m。

样例输入

123006
2

样例输出

1006

来源

2016年中北大学新生赛

提示

补充样例
输入
123006 3
输出
6

提交统计讨论

题解:由于数据小顿时变成了水题,暴力。

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<vector>
#include<bitset>
#include<iostream>
#define ullmax 1844674407370955161
#define llmax 9223372036854775807
#define intmax 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
using namespace std;
int main(){
    string s;
    int k;
    cin>>s>>k;
    while(k){
        int leap=0;
        for(int i=0;i<s.size()-1;i++){
            if(s[i]>s[i+1]){
                leap=1;
                s.erase(i,1);
                break;
            }
        }
        if(!leap)
            s.erase(s.size()-1,1);
        k--;
    }
    int flag=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='0'){
            if(flag){
                printf("0");
            }
        }
        else{
            flag=1;
            printf("%c",s[i]);
        }
    }
    printf("\n");
    return 0;
}

 

1134: 整数去位

时间限制: 1 Sec  内存限制: 128 MB
提交: 962  解决: 175
[提交][状态][讨论版]

题目描述

键盘输入一个高精度的正整数N,去掉其中任意M个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的N和M寻找一种方案使得剩下的数字组成的新数最小。输出组成的新的正整数。
输入数据均不需判错。如果去掉了某几个位后得到的新整数开头为0,保留0。
输入:
505
1
输出:
05

输入

第一行为高精度正整数N(N的长度不超过10^6位)
第二行为M(0<=M<=N的长度)

输出

去掉M位后的最小新数。

样例输入

82386782
3

样例输出

23672

提示

来源

贪心算法★

[提交][状态][讨论版]

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=(tree[rt<<1],tree[rt<<1|1]);
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=2e6+5;
int v[N];
string str;
int main(){
    int m;
    cin>>str;
    scanf("%d",&m);
    int len=str.length();

    int p1=0,p2=1;
    while(p2<len){                  
        while(m&&str[p2]<str[p1]){
            v[p1]=1;
            m--;
            while(p1>=0&&v[p1]) p1--;
            if(p1<0) break;
        }
        p1=p2;
        p2++;
    }
    for(int i=len-1;i>=0;i--){
        if(m&&!v[i]){
            v[i]=1;
            m--;
        }
    }
    for(int i=0;i<len;i++)
        if(!v[i]) printf("%c",str[i]);
    return 0;
}

P1323 删数问题

Luogu

应用 

题库训练比赛记录讨论

    • 632通过
    • 2.6K提交
  • 题目提供者yeszy 管理员
  • 评测方式云端评测
  • 标签高性能
  • 难度普及+/提高
  • 时空限制1000ms / 128MB

 提交  题解   

  • 提示:收藏到任务计划后,可在首页查看。

体验新版界面

最新讨论显示

推荐的相关题目显示

题目描述

描述:

一个集合有如下元素:1是集合元素;若P是集合的元素,则2 * P +1,4*P+5也是集合的元素,

取出此集合中最小的K个元素,按从小到大的顺序组合成一个多位数,现要求从中删除M个数位上的数字,

使得剩下的数字最大,编程输出删除前和删除后的多位数字。

注:不存在所有数被删除的情况

输入输出格式

输入格式:

输入的仅一行,K,M的值,K,M均小于等于30000。

输出格式:

输出为两行,第一行为删除前的数字,第二行为删除后的数字。

输入输出样例

输入样例#1: 复制

5  4

输出样例#1: 复制

137915
95

说明

数据范围:

对于30%的数据,有1 ≤ K ,M≤ 300;

对于100%的数据,有1 ≤ K,M ≤ 30000。

这么水的题目,就不要打表了。

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=(tree[rt<<1],tree[rt<<1|1]);
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e6+5;
struct cmp{
    bool operator()(ll &x,ll &y)const{
        return x>y;
    }
};

priority_queue<ll,vector<ll>,cmp>qq;
string str="";

void fun(ll ans){
    stack<char>ss;
    while(ans){
        ss.push('0'+(ans%10));
        ans/=10;
    }
    while(!ss.empty()){
        str+=ss.top();
        ss.pop();
    }
}
int r[N];
int v[N];
int main(){
    int k,m;
    scanf("%d %d",&k,&m);
    qq.push(1);
    int num=0;
    while(!qq.empty()){
        ll ans=qq.top();
        qq.pop();
        fun(ans);
        num++;
        if(num>=k) break;
        qq.push(2*ans+1);
        qq.push(4*ans+5);
    }
    cout<<str<<endl;

    int len=str.length();
    int p1=0,p2=1;
    while(p2<len){           //  以 p2 为轴向前,在 m 的范围内将小于 str[p2] 的值都删掉
        while(m&&str[p2]>str[p1]){
            v[p1]=1;
            m--;
            while(p1>=0&&v[p1]) p1--;
            if(p1<0) break;
        }
        p1=p2;
        p2++;
    }
    for(int i=len-1;i>=0;i--)
        if(m){
            v[i]=1;
            m--;
        }
    for(int i=0;i<len;i++){
        if(!v[i])
            printf("%c",str[i]);
    }
    printf("\n");
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值