码迷,mamicode.com
首页 > 其他好文 > 详细

HDU3183 贪心/RMQ-st表

时间:2018-09-08 15:31:08      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:要求   code   har   允许   res   min   strlen   limit   ike   

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6244    Accepted Submission(s): 2539


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
 
这个题题意很简单,就是给你一个长度为n的数,要求从中去掉m位,剩余位顺序不变,求能得到的最小的数
其中允许得到有前导零的数
用贪心做的话就很简单,通过观察我们可以得到一个结论
最终答案的第一位一定在下标为0~m之间 如果不是这样的话我们就无法凑出n-m位的数了
同理,第二位一定在上一位的下标+1到m+1之间
代码也很简单:
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     ios::sync_with_stdio(false);
 9     int m;
10     char str[1050];
11     while (cin >> str >> m)
12     {
13         int len = strlen(str);
14         if (len <= m)
15         {
16             cout << "0" << endl;
17             continue;
18         }
19 
20         int flag = 0;
21         int n = len;
22         int l = 0, r = m;
23         for (int i = 0; i < n - m; i++)
24         {
25             char min = str[l];
26             int pos = l;
27             for (int j = l; j <= r; j++)
28             {
29                 if (str[j]< min)
30                 {
31                     min = str[j];
32                     pos = j;
33                 }
34             }
35             l = pos + 1;
36             r++;
37             if (!flag && min == 0)
38                 continue;
39             cout << min;
40             flag++;
41         }
42 
43         if (!flag)
44             cout << 0;
45         cout << endl;
46     }
47 
48     return 0;
49 }

 

用st表的话也差不多是一个思路,我们先用st表处理得到各个区间最小的数的下标,然后同样按照贪心去求0~m,pos+1~m+1,pos+1~m+2.......

 1 #include <iostream>
 2 #include <cmath>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 string s;
 8 int n;
 9 int st[1050][20];
10 char ans[1050];
11 
12 int Min(int a, int b)
13 {
14     return s[a] <= s[b] ? a : b;
15 }
16 
17 void init()
18 {
19     for (int i = 0; i < s.size(); i++)
20         st[i][0] = i;
21 
22     for (int j = 1; (1 << j) < s.size(); j++)
23         for (int i = 0; i + (1 << j) - 1 < s.size(); i++)
24         {
25             st[i][j] = Min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
26             //cout << i << ends << j << endl;
27         }
28 }
29 
30 int search(int l, int r)
31 {
32     int k = (int)(log((double)(r - l + 1)) / log(2.0));
33     return Min(st[l][k], st[r - (1 << k) + 1][k]);
34 }
35 
36 int main()
37 {
38     ios::sync_with_stdio(false);
39     while (cin >> s >> n)
40     {
41         init();
42         int len = s.size() - n;
43         int pos = 0;
44         int flag = 0;
45         while (len--)
46         {
47             pos = search(pos,n + flag);
48             ans[flag++] = s[pos];
49             //cout << s[pos] << endl;
50             pos++;
51         }
52 
53         flag = 0;
54 
55         for (int i = 0; i < s.size() - n; i++)
56         {
57             if (!flag && ans[i] == 0)
58                 continue;
59             flag++;
60             cout << ans[i];
61         }
62         if (!flag)
63             cout << 0;
64         cout << endl;
65     }
66 
67     return 0;
68 }

 

HDU3183 贪心/RMQ-st表

标签:要求   code   har   允许   res   min   strlen   limit   ike   

原文地址:https://www.cnblogs.com/qq965921539/p/9609015.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!