HDU_3183_A Magic Lamp(贪心)
2015-08-06 00:24
591 查看
A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2274 Accepted Submission(s): 902
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
题意:给你一个数字串,要你删除m个数字后所得的最小数字串是多少。
分析:本题是搜RMQ-ST算法题目搜到的。但是没想到怎么用它来做。然后本题其实可以直接贪心过的。但是首先要想清楚一些问题。
删除m个数字,相当于在里面从左往右取n-m个数字;所得数最小,也就是每次取得数字尽量小。那么,取得的第一个数字一定在区间[0,m]内,为什么呢?因为除了第一个数之外还要取n-m-1个数字,所以区间右边界最大只能是m,每次在区间里找最小的那个数(尽量靠左);依次类推,假设第一个数字取得的下标是index1,那么,第二个数字一定是在[index1+1,m+1]内取得;依次类推下去,右边界每次加1。当选取到了n-m个数字之后,也就找到了答案了~
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183
代码清单:
#include<set> #include<map> #include<cmath> #include<queue> #include<stack> #include<ctime> #include<string> #include<cstdio> #include<cctype> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; const int maxn = 1e3 + 5; int m,l,r,k; char s[maxn]; char ans[maxn]; int main(){ while(scanf("%s %d",s,&m)!=EOF){ l=0; r=m; k=0; m=strlen(s)-m; while(m--){ int index=l; for(int i=l;i<=r;i++){ if(s[index]>s[i]) index=i; //注意是>,而不是>= } ans[k++]=s[index]; l=index+1; r++; } int head=0; while(ans[head]=='0'&&head<k) head++; //去掉前导0 if(head==k) printf("0\n"); else{ for(;head<k;head++) printf("%c",ans[head]); printf("\n"); } }return 0; }
相关文章推荐
- 跨域抓取博客园RSS
- 第二步开始:计算几何 数论
- SDM For Face Alignment流程介绍及Matlab代码实现之测试篇
- 二叉树题目
- Notepad++中常用的插件
- 让应用程序只有一个实例
- Java的访问控制
- EL表达式从request和session中取值
- Leetcode # 94 Binary Tree Inorder Traversal
- C++ inline函数
- selenium+webdriver(python)+webbrowser入门
- SDM For Face Alignment 流程介绍及Matlab代码实现之训练篇
- 笔试题:05_回溯字符串
- 笔试题:04年_字符串替换
- 笔试题:03年_迷宫
- 笔试题:09年_atoi
- hdoj 4123 Bob’s Race 【树的直径 + RMQ】 【二分查询会超时】
- A004-AS开发HelloWorld
- 实习系列1
- SDM For Face Alignment 流程介绍及Matlab代码实现之预处理篇