上题目:

296. Sasha vs. Kate

Time limit per test: 1 second(s)
Memory limit: 65536
kilobytes
input: standard
output: standard

During the regular Mars's World Finals Subregional Programming Contest a boy Sasha lost N
"Mars" bars of chocolate to a girl Kate. But for two years already
Sasha does not hurry to pay his debt. And now Sasha and Kate decided
that Sasha will give Kate P chocolate bars, where number P can be obtained from the number N by removing exactly K
decimal digits. Sasha generously let Kate to choose digits to be
removed. Your task is to find out how many bars Sasha will give Kate. Of
course Kate will choose K digits from the number N in such a way that the resulting number P would be maximal.

Input

The first line of the input file contains two integer numbers N and K (1≤ N≤ 101000; 0≤ K≤ 999). Number K is strictly less than the number of digits in N. N will not have any leading zeros.

Output

Output the unknown P.

Example(s)
sample input
sample output
1992 2
99
sample input
sample output
1000 2
10

  题意是给你一个最多有1001的数字,然后让你剔除其中的某K个数字,求可以得到的最大的数字。

  这一题用贪心思想。

这一题一开始读错题目,以为是挑K个数字,还以为是连续的,然后就WA了两次。后来终于看清题目了。然后就开始了各种yy,想过的思路有不少,最后列举了几个数字,找到了一点规律。

规律是这样的:当第i个数字比后一个数字小的话,把它删掉可以比删掉其他数字的结果得到更大的结果,然后如果后面的数字等于或者小于当前的数字的话,说明当前的数字暂时可以不用删掉,然后对i+1操作。

当然,这个规律还不完善,如果数字为32121,要求删掉2个数字的话,那当然是删掉两个1,可是刚才的思路只删除了一个1,同时如果在外面加一个循环也不能解决问题,如果序列式是32112的话就可以很好地得到正确答案,那应该怎么做呢,暂时的做法是在所有数字的结尾加一个INF,这是根据我的代码的结构(我是用栈来实现的)写出来的解决方案= =,好像有点XX(= =),AC的代码有bug= =现在改了一下。听其他同学的解法好像用了一个for和一个while就搞定了。= =

上代码:

 #include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#define MAX 300000+10
using namespace std; int s[MAX];
stack<int> S; int main()
{
//freopen("data.txt","r",stdin);
int i,j,n,k,e,h;
char c;
k=;
memset(s,-,sizeof(s));
while((c=getchar())!=' ')
{
s[k]=c-'';
k++;
}
scanf("%d",&n);
h=n;
i=;
//S.push(s[i]);
//i++;
while(h)
{
while(!S.empty() && s[i]>S.top())
{
S.pop();
h--;
if(h==) break;
}
if(h==) break;
S.push(s[i]);
i++;
//if(s[i]==-1) break;
}
h=S.size();
j=;
for(i--;j<h;j++,i--)
{
s[i]=S.top();
S.pop();
}
//e=0;
//for(j=0;j<h;j--) e=e*10+p[j];
if(i<k)
for(i++;i<k;i++)
{
printf("%d",s[i]);
//e=e*10+s[i];
}
else printf("");
printf("\n");
return ;
}

296

SGU - 296 - Sasha vs. Kate的更多相关文章

  1. SGU 296.Sasha vs. Kate(贪心)

    题意: 给出长度为n(<=1000)的一个数.输出删掉k个数字后的最大值. Solution: 简单贪心. s[i]代表数字s的第i位. 从前往后第一个满足s[i]>s[i-1]的位置,最 ...

  2. 今日SGU 5.22

    SGU 296 题意:给你一个最多1000位的数,让你删除k位使得剩下的数最大 收获:贪心 #include<bits/stdc++.h> #define de(x) cout<&l ...

  3. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  4. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  5. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  6. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

  7. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

  8. Ubuntu 汉化及kate汉化和使用自带终端的解决方式

    汉化方法:1,打开屏幕上方的“system”(系统),里面有个“Administration”(系统管理),选择“Snaptic Package Manager”(软件包管理器),打开,输入密码后进入 ...

  9. SGU 422 Fast Typing(概率DP)

    题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...

随机推荐

  1. MySQL基础教程-绝对推荐

    https://wenku.baidu.com/view/1acfe579ee06eff9aef80752.html?from=search MySQL--公司培训PPT Mysql体系结构以及与Or ...

  2. ubuntu使用ssh连接远程电脑的方法

    目前,大多数linux distributions都预先安装了ssh的客户端,即可以连接别人的电脑.但也有例外的情况,所以,下面先把ssh的客户端与服务端的安装一并讲了吧. ssh客户端及服务端的安装 ...

  3. https://www.threatminer.org/domain.php?q=blackschickens.xyz ——域名的信誉查询站点 还可以查IP

    https://www.threatminer.org/domain.php?q=blackschickens.xyz https://www.threatminer.org/host.php?q=6 ...

  4. vue 中数据没有同步渲染的解决方法

    今天在做一个页面,遇到一个数据渲染不同步的问题,如下: 代码如下:原理:点击时,对应的banklist 的选项选项变为 true 选中状态 html: <div class="PayO ...

  5. Hadoop一主一从部署(2)

    Hadoop部署一主一从(2) 1.关闭防火墙和Linux守护进程 执行命令: iptables -F setenforce 0 2.对Hadoop集群进行初始化,在namenode(主机)上执行命令 ...

  6. RPC与REST

    RPC与REST (摘自网络,个人理解)

  7. TCP/IP,必知必会的

    文章目录 前言 TCP/IP模型 数据链路层 网络层 ping Traceroute TCP/UDP DNS TCP连接的建立与终止 TCP流量控制 TCP拥塞控制 0 前言 本文整理了一些TCP/I ...

  8. php判断方法及区别

    php判断方法 ‘is_类型名称’    php判断方法 $x="1"; echo gettype(is_string($x)); isset    是否存在 empty   是否 ...

  9. CNN结构:用于检测的CNN结构进化-结合式方法

    原文链接:何恺明团队提出 Focal Loss,目标检测精度高达39.1AP,打破现有记录     呀 加入Facebook的何凯明继续优化检测CNN网络,arXiv 上发现了何恺明所在 FAIR 团 ...

  10. Linux命令小记

    以下说法都是基于普通用户的角度,如果是root,可能会有不同. (1)rm -r或-R选项:递归删除目录及其内容(子目录.文件) rm默认无法删除目录,如果删除空目录,可以使用-d选项.如果目录非空, ...