zoukankan      html  css  js  c++  java
  • 集合删数

    集合删数

    总时间限制: 20000ms单个测试点时间限制: 2000ms内存限制: 128000kB

    描述
    一个集合有如下元素:1是集合元素;若P是集合的元素,则2 * P +14*P+5也是集合的元素,取出此集合中最小的K个元素,按从小到大的顺序组合成一个多位数,现要求从中删除M个数位上的数字,使得剩下的数字最大,编程输出删除前和删除后的多位数字。
    输入
    输入的仅一行,K,M的值
    输出
    输出为两行,第一行为删除前的数字,第二行为删除后的数字。
    样例输入
    5 4
    样例输出
    137915
    95
     
    分析:
    	首先对于计算前K各元素,可以定义两个队列,每一次取两个队列的队首元素进行比较,并把较小的那个元素从队列中弹出,计算出2*p+1和4*p+5分别放入两个队列中,实际实现时可以使用数组模拟.
    	然后对于删除数字取得最大值的问题.
    	首先明确一个性质,两个数字进行比较,在位数相同时,最高位大的所构成的数字一定比另一个要大;
    	所以,基于这种性质的一个贪心法就出来了.
    	只要总是保证最高位数字最大,就可以得出最大的数字..
    	同理,在保证最高位最大之后,也应保证次高位最大,这样,就解决了这个问题了.
    	代码如下
    </pre><pre class="cpp" name="code">#include<iostream>
    #include<string>
    #include<cstdio>
    #include<algorithm>
    #include<sstream>
    using namespace std;
    typedef long long ll;
    ll num[30010]={0,1};
    int main(){
        int n, m, front, cnt, end;
        ll a, b;
        scanf("%d%d",&n,&m);
        int i = 1, j = 1;
        for (int k = 2; k <= n; k++){
            a = num[i] * 2 + 1;
            b = num[j] * 4 + 5;
            if (a > b) j++,num[k] = b;
            else if (a == b) i++,j++,num[k] = a;
            else i++,num[k] = a;
        }
        string s = "", ans = "";
        for(i = 1; i <= n; i++){
            stringstream ss;string str;
            ss << num[i];ss >> str;
            s += str;
        }
        cout<<s<<endl;
        string::iterator it = s.begin();s.insert(it, '9');
        n = s.size();front = cnt = 0;end = 1;
        while (end <= n && cnt != m){
            if (s[end] <= s[front]) s[++front] = s[end++];
            else front--,cnt++;
        }
        while (end <= n) s[++front] = s[end++];
        for (i = 1; i < n - m; i++) printf("%c",s[i]);
        fclose(stdin);fclose(stdout);
    	return 0;
    }


    人就像命运下的蝼蚁,谁也无法操控自己的人生.
  • 相关阅读:
    openresty
    ATS 相关
    pandas
    flask
    ansible
    zipline
    bcolz
    数据分析 --- concat
    Go --- 基础使用
    Go --- 基础介绍
  • 原文地址:https://www.cnblogs.com/Skyminer/p/6435564.html
Copyright © 2011-2022 走看看