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

Problem Description
Kiki
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?
 
Input
There are several test cases.
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.
 
Output
For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it.
 
Sample Input
178543 4
1000001 1
100001 2
12345 2
54321 2
 
Sample Output
13
1
0
123
321
 题解:给一个数,去掉一部分后得到的数最小,真是写醉了。。。各种wa,最后都想到了负数,思路没错,就是贪心,找到n[i]>n[j]就减去i这个数,最后过了,现在仍然感觉自己思路每错,换种写法就会ac。。。
代码:
 #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(贪心+链表)的更多相关文章

  1. HDU3183 A Magic Lamp —— 贪心(单调队列优化)/ RMQ / 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题解: 方法一:贪心. 在草稿纸上试多几次可以知道,删除数字中从左到右最后一位递增(可以等于)的 ...

  2. hdu 3183 A Magic Lamp 贪心

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  3. 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 ...

  4. hdoj--3183--A Magic Lamp(贪心)

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. HDOJ 3183 A Magic Lamp

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

  7. hdu 3183 A Magic Lamp(RMQ)

    A Magic Lamp                                                                               Time Limi ...

  8. HDU3183A Magic Lamp,和NYOJ最大的数一样

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. HDU 3183 A Magic Lamp(RMQ问题, ST算法)

    原题目 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. css 定义hr的几种样式

    <style type="text/css"> <!-- .hr0{ height:1px;border:none;border-top:1px dashed # ...

  2. 使用Boost.Asio编写通信程序

    摘要:本文通过形像而活泼的语言简单地介绍了Boost::asio库的使用,作为asio的一个入门介绍是非常合适的,可以给人一种新鲜的感觉,同时也能让体验到asio的主要内容. Boost.Asio是一 ...

  3. UVAlive 2326 Moving Tables(贪心 + 区间问题)

    The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...

  4. mysql 假设存在id则设数据自添加1 ,不存在则加入。java月份计算比較

    </pre><pre name="code" class="sql">INSERT INTO invite_rejectlog_num ...

  5. 【剑指offer】第一个仅仅出现一次的字符

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/27106997 题目描写叙述: 在一个字符串(1<=字符串长度<=10000,所 ...

  6. 《TCP/IP详细说明》读书笔记(17章)-TCP传输控制协定

    1.TCP的服务 在一个TCP连接中.仅有双方进行彼此通信. TCP通过下列方式来提供可靠性: 1)应用数据被切割成TCP觉得最适合发送的数据块. 这和UDP全然不同,应用程序产生的数据报长度保持不变 ...

  7. 工具篇-TraceView

    --- layout: post title: 工具篇-TraceView  description: 让我们远离卡顿和黑屏 2015-10-09 category: blog --- ## 让我们远 ...

  8. 【转】Configuring VM Acceleration on Linux

    Configuring VM Acceleration on Linux Linux-based systems support virtual machine acceleration throug ...

  9. 分页:T-SQL存储过程和EF存储过程的使用

    首先准备好分页的T-SQL语句: create proc usp_activityFenYe @pageIndex int, @pageSize int, @pageCount int output ...

  10. HTML解决div里面img的缝隙问题

    图片IMG与容器下边界之间有空隙怎么办?这里介绍3中简单的解决方法. 第一,给图片img标签display:block. img{display:block} 第二,定义容器里的字体大小为0. div ...