hdu 3183 A Magic Lamp(ST表)
2016-04-10 20:10
351 查看
[align=left]Problem Description[/align]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? [align=left]Input[/align]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. [align=left]Output[/align]For each case, output the minimum result you can get in one line. If the result contains leading zero, ignore it. [align=left]Sample Input[/align]178543 4 1000001 1 100001 2 12345 2 54321 2
[align=left]Sample Output[/align]
13 1 0 123 321
solution:
从n个数中删除m个,使得剩下的数最大,那么我们可以知道你选择n-m个数的第一个数一定在1~m这期间,每次在上次最小和当前位置中间选择一个最小的数作为答案,用ST表查询区间最小值
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const double eps = 1e-6; int minx[1050][22], num[1050], ans[1050]; char a[1050]; int mins(int a, int b) { return num[a] <= num[b] ? a : b; } int qmin(int l, int r) { int k = r - l + 1; k = log2((double)k) + eps; return mins(minx[l][k], minx[r - (1 << k) + 1][k]); } void init(int n) { for (int i = 0; i <n; i++) minx[i][0] = i; for (int j = 1; j < 20; j++) { int k = 1 << (j - 1); for (int i = 0; i + (k << 1) - 1 <= n; i++) minx[i][j] = mins(minx[i][j - 1], minx[i + k][j - 1]); } } int main() { int m; while (scanf("%s%d", a, &m) != EOF) { int len = strlen(a); for (int i = 0; i < len; i++) num[i] = a[i] - '0'; init(len); int pos = 0, len1 = 0; for (int i = m; i <len; i++) { pos = qmin(pos, i); ans[len1++] = num[pos++]; } int i = -1; while (ans[++i] == 0 && i<len1); if (i == len1)printf("0\n"); else { for (int j = i; j < len1; j++) printf("%d", ans[j]); printf("\n"); } } return 0; }
相关文章推荐
- 20145319 第六周学习总结
- 课题的跨专业组队
- Tomcat作为Servlet容器的基本功能
- 枚举小结
- POJ 1465 Multiple
- 类似LCS,构成目标单词(POJ2192)
- 数据结构KMP算法中next函数的求解思想及其解释
- 20145317彭垚 《Java程序设计》第6周学习总结
- oracle小数点前零丢失的问题
- Swift 入门基础-2
- innodb recovery
- 【Android基础】网络图片查看器
- 简单的秒表定时器
- W3C标准与规范
- 排序问题-希尔排序
- Linux命令整合之alias
- Unity 设置Android SDK path
- SSH开发环境整合搭建
- 关于U盘启动操作系统《30天自制操作系统》
- 在编写工程注册页面中的体会和遇到的问题