A Magic Lamp(贪心+链表)
A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2521 Accepted Submission(s): 986
likes traveling. One day she finds a magic lamp, unfortunately the
genie in the lamp is not so kind. Kiki must answer a question, and then
the genie will realize one of her dreams.
The question is: give you
an integer, you are allowed to delete exactly m digits. The left digits
will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
Each
test case will contain an integer you are given (which may at most
contains 1000 digits.) and the integer m (if the integer contains n
digits, m will not bigger then n). The given integer will not contain
leading zero.
If the result contains leading zero, ignore it.
1000001 1
100001 2
12345 2
54321 2
1
0
123
321
#include<cstdio>
#include<cstring>
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
char n[MAXN];
int vis[MAXN];
int main(){
int m,t;
while(~scanf("%s%d",n,&m)){
mem(vis,);
t=strlen(n);
int r=t-;
for(int i=;i<m;i++){
int cnt=;
for(int j=;j<r;j++){
if(vis[j])continue;
int x=j+;
while(vis[x])x++;//
if(n[j]>n[x]){
vis[j]=;cnt=;break;
/*比赛时候这样写的,一直wa仍然感觉没错。。。
if(n[j]<n[j+1]){
cnt=1;
vis[j]=1;
n[j]=n[j+1];
break;
}
*/
}
}
if(!cnt)vis[r--]=;
}int flog=;
for(int i=;i<t;i++){
if(vis[i])continue;
if(flog&&n[i]=='')continue;
flog=;printf("%c",n[i]);
}
if(flog)printf("");
puts("");
}
return ;
}
链表:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
struct Node{
int pre,next,val;
};
Node lis[MAXN];
char s[MAXN];
int main(){
int n,len;
while(~scanf("%s%d",s,&n)){
mem(lis,);
len=strlen(s);
for(int i=;i<=len;i++){
lis[i].pre=i-;
lis[i].val=s[i-]-'';
lis[i].next=i+;
}
lis[].next=;lis[len+].pre=len;
int p,q;
while(n--){
p=;
while(p!=len+){
q=lis[p].next;
if(lis[p].val>lis[q].val){
lis[q].pre=lis[p].pre;
q=lis[p].pre;lis[q].next=lis[p].next;
break;
}
p=lis[p].next;
}
}
p=;
while(lis[p].val==&&p!=len+)p=lis[p].next;
if(p==len+)puts("");
else{
while(lis[p].next!=n+){
printf("%d",lis[p].val);p=lis[p].next;
}
puts("");
}
}
return ;
}
A Magic Lamp(贪心+链表)的更多相关文章
- HDU3183 A Magic Lamp —— 贪心(单调队列优化)/ RMQ / 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题解: 方法一:贪心. 在草稿纸上试多几次可以知道,删除数字中从左到右最后一位递增(可以等于)的 ...
- hdu 3183 A Magic Lamp 贪心
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- A Magic Lamp HDU - 3183 (逆向贪心/RMQ)
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so ...
- hdoj--3183--A Magic Lamp(贪心)
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDOJ 3183 A Magic Lamp
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
- hdu 3183 A Magic Lamp(RMQ)
A Magic Lamp Time Limi ...
- HDU3183A Magic Lamp,和NYOJ最大的数一样
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 3183 A Magic Lamp(RMQ问题, ST算法)
原题目 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- MSSQL SERVER 2008 R2 无法连接到数据库,用户sa登录失败,错误:18456
原因:勾选了强制实施密码策略,但是设置的密码很简单依然可以,比如:123456 这是为什么?原来,这个功能要用到NetValidatePasswordPolicy() API这个函数. (该功能只有在 ...
- python异常处理try,except,else,finally,raise
先看下else的使用: try: ... exception: ... else: ... 只有在try中没有发生任何异常,所有代码完全成功的情况下才会转入else 再看下finally: final ...
- JavaEE Tutorials (11) - 使用Criteria API创建查询
11.1Criteria和Metamodel API概述16811.2使用Metamodel API为实体类建模170 11.2.1使用元模型类17011.3使用Criteria API和Metamo ...
- Book of Evil 树双向DFS
Book of Evil Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area ...
- .NET使用NPOI组件将数据导出Excel
.NPOI官方网站:http://npoi.codeplex.com/ 可以到此网站上去下载最新的NPOI组件版本 2.NPOI在线学习教程(中文版): http://www.cnblogs.com/ ...
- BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐( LIS )
求LIS , 然后用 n 减去即为answer ---------------------------------------------------------------------------- ...
- 我的Python成长之路---第五天---Python基础(17)---2016年1月30日(晴)
常用模块 1.模块介绍 模块,用一砣代码实现了某个功能的代码集合. 模块分为三种 自定义模块 内置标准模块(又称标准库) 开源模块 2.常用模块 time模块 time.time() import t ...
- Android dp和sp的用法汇总
1 > dp 是跟像素密度无关的单位,也就是说在相同尺寸.但不同的分辨率的手机上,用dp标识的东西,显示的大小是一样的. sp是用于标识字体的,它不仅跟屏幕尺寸有关,还跟设置的系统字体大小有关. ...
- Eclipse 取消import自动补全具体的类名
有时候,在代码里写了一个JFrame,然后Eclipse就自动添加了import javax.swing.JFrame; 但有时候希望只要import javax.swing.*;就可以了,不希望具体 ...
- 数据结构——表(list)
#include <iostream> #include <list> using namespace std; 标准类的存储方式为双向循环链表 list类 class lis ...