HDU 3183 贪心

A Magic Lamp

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

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

Source

HDU 2009-11 Programming Contest

题意:给你一个长度最多为1000的数 删去m位 使得最后的数最小 存在前导零

题解:贪心 处理 从左到右遍历 找到第一个a[i]>a[i+1] 并删除第i个  vector 处理

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<queue>
 4 #include<stack>
 5 #include<map>
 6 #include<cstring>
 7 #include<cstdio>
 8 #include<vector>
 9 #define ll __int64
10 using namespace std;
11 char a[1005];
12 vector<int> ve;
13 int flag;
14 int n;
15 int main()
16 {
17  memset(a,0,sizeof(a));
18   while(cin>>a>>n)
19 {
20     int len;
21     len=strlen(a);
22     if(len<=n)
23     cout<<"0"<<endl;
24     else
25     {
26         ve.clear();
27       for(int i=0;i<len;i++)
28           ve.push_back(a[i]-‘0‘);
29         while(n--)
30         {
31             int flag=1;
32             for(int i=0;i<ve.size()-1;i++)
33             {
34                 if(ve[i]>ve[i+1])
35                 {
36                     flag=0;
37                     ve.erase(ve.begin()+i);
38                     break;
39                 }
40             }
41             if(flag)
42             {
43                 ve.erase(ve.end()-1);
44             }
45         }
46         flag=1;
47         for(int i=0;i<ve.size();i++)
48         {
49             if(ve[i]==0&&flag)
50               continue;
51             else
52             {
53                 flag=0;
54                 cout<<ve[i];
55             }
56         }
57         if(flag)
58         cout<<"0";
59         cout<<endl;
60     }
61     memset(a,0,sizeof(a));
62 }
63     return 0;
64 } 
时间: 2024-08-27 13:52:32

HDU 3183 贪心的相关文章

hdu 3183 A Magic Lamp(RMQ)

A Magic Lamp                                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Kiki likes traveling. One day she finds a magic lamp, u

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

hdu 3183 A Magic Lamp(RMQ)

题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个,使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数,每次取的时候保证后面能取的个数足够,并且取的数最小,查询最小的操作用RMQ优化. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 10005; int N, M, d[m

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu

hdu 1050(贪心算法)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19278    Accepted Submission(s): 6582 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a