SGU - 296 - Sasha vs. Kate
上题目:
296. Sasha vs. Kate
Memory limit: 65536
kilobytes
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.
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 the unknown P.
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的更多相关文章
- SGU 296.Sasha vs. Kate(贪心)
题意: 给出长度为n(<=1000)的一个数.输出删掉k个数字后的最大值. Solution: 简单贪心. s[i]代表数字s的第i位. 从前往后第一个满足s[i]>s[i-1]的位置,最 ...
- 今日SGU 5.22
SGU 296 题意:给你一个最多1000位的数,让你删除k位使得剩下的数最大 收获:贪心 #include<bits/stdc++.h> #define de(x) cout<&l ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
- 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 ...
- Ubuntu 汉化及kate汉化和使用自带终端的解决方式
汉化方法:1,打开屏幕上方的“system”(系统),里面有个“Administration”(系统管理),选择“Snaptic Package Manager”(软件包管理器),打开,输入密码后进入 ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
随机推荐
- jquery-layer弹出框
样式1: 代码: 前台jsp: $("#add_table").bind("click",function(){ layer.open({ type: 2, t ...
- Android+Jquery Mobile学习系列(9)-总结和代码分享
经过一个多月的边学习边练手,学会了Android基于Web开发的毛皮,其实开发过程中用Android原生API不是很多,更多的是HTML/Javascript/Css. 个人觉得基于WebView的J ...
- Struts2 中 result type=”json” 的参数解释
转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...
- django自带url模板标签的使用
django模板中url标签和view中的reverse(博客地址)功能相同,都是通过制定处理视图来返回一个url. 使用方法: {% url userEdit 12 %} 或者 {% url use ...
- 自己动手丰衣足食,为Zepto添加Slide动画效果
一.缘由 公司的移动端项目,采用zepto为主要框架,但是zepto毕竟是精简版的jquery,体积小了,功能自然没有这么强大,特别是动画和选择器这两块,需要我们自己去拓展. 在项目开发过程中,很多页 ...
- python中使用pip安装报错:Fatal error in launcher... 解决方法
python安装了2和3版本在 cmd 中用pip报的错误为:Fatal error in launcher:Unable to create process using 这是因为你安装了python ...
- go之for循环
一.基于计数器的迭代 格式 for 初始化语句; 条件语句; 修饰语句{} 实例 package main import "fmt" func main(){ for i:=0;i ...
- python 5:str(某一变量)(将其他数字解释为字符串)
age = messege = "Your's age is " + str(age) #将其他数字更改为字符串 print(messege) 运行结果应该是: Your's ag ...
- System.Net.Mail 详细讲解
http://blog.csdn.net/liyanwwww/article/details/5507498
- C#:设置webBrowser框架与系统相对应的IE内核版本
通常情况下,我们直接调用C#的webBrowser控件,默认的浏览器内核是IE7. 那么如何修改控件调用的默认浏览器版本呢? /// <summary> /// 修改注册表信息来兼容当前 ...