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) ...
随机推荐
- css 定义hr的几种样式
<style type="text/css"> <!-- .hr0{ height:1px;border:none;border-top:1px dashed # ...
- 使用Boost.Asio编写通信程序
摘要:本文通过形像而活泼的语言简单地介绍了Boost::asio库的使用,作为asio的一个入门介绍是非常合适的,可以给人一种新鲜的感觉,同时也能让体验到asio的主要内容. Boost.Asio是一 ...
- UVAlive 2326 Moving Tables(贪心 + 区间问题)
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...
- mysql 假设存在id则设数据自添加1 ,不存在则加入。java月份计算比較
</pre><pre name="code" class="sql">INSERT INTO invite_rejectlog_num ...
- 【剑指offer】第一个仅仅出现一次的字符
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27106997 题目描写叙述: 在一个字符串(1<=字符串长度<=10000,所 ...
- 《TCP/IP详细说明》读书笔记(17章)-TCP传输控制协定
1.TCP的服务 在一个TCP连接中.仅有双方进行彼此通信. TCP通过下列方式来提供可靠性: 1)应用数据被切割成TCP觉得最适合发送的数据块. 这和UDP全然不同,应用程序产生的数据报长度保持不变 ...
- 工具篇-TraceView
--- layout: post title: 工具篇-TraceView description: 让我们远离卡顿和黑屏 2015-10-09 category: blog --- ## 让我们远 ...
- 【转】Configuring VM Acceleration on Linux
Configuring VM Acceleration on Linux Linux-based systems support virtual machine acceleration throug ...
- 分页:T-SQL存储过程和EF存储过程的使用
首先准备好分页的T-SQL语句: create proc usp_activityFenYe @pageIndex int, @pageSize int, @pageCount int output ...
- HTML解决div里面img的缝隙问题
图片IMG与容器下边界之间有空隙怎么办?这里介绍3中简单的解决方法. 第一,给图片img标签display:block. img{display:block} 第二,定义容器里的字体大小为0. div ...