A Magic Lamp(贪心)

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。。。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #define mem(x,y) memset(x,y,sizeof(x))
 4 const int MAXN=5050;
 5 char n[MAXN];
 6 int vis[MAXN];
 7 int main(){
 8     int m,t;
 9     while(~scanf("%s%d",n,&m)){
10         mem(vis,0);
11         t=strlen(n);
12         int r=t-1;
13         for(int i=0;i<m;i++){
14             int cnt=0;
15             for(int j=0;j<r;j++){
16                 if(vis[j])continue;
17                 int x=j+1;
18                 while(vis[x])x++;//
19                 if(n[j]>n[x]){
20                     vis[j]=1;cnt=1;break;
21                     /*比赛时候这样写的,一直wa仍然感觉没错。。。
22                     if(n[j]<n[j+1]){
23                     cnt=1;
24                     vis[j]=1;
25                     n[j]=n[j+1];
26                     break;
27                 }
28                     */
29                 }
30             }
31             if(!cnt)vis[r--]=1;
32         }int flog=1;
33         for(int i=0;i<t;i++){
34             if(vis[i])continue;
35             if(flog&&n[i]==‘0‘)continue;
36             flog=0;printf("%c",n[i]);
37         }
38         if(flog)printf("0");
39         puts("");
40     }
41     return 0;
42 }
时间: 2024-08-07 12:32:49

A Magic Lamp(贪心)的相关文章

HDU 3183 A Magic Lamp(贪心+RMQ)

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 allowe

HDU_3183_A Magic Lamp(贪心)

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 geni

hdu 3183 A Magic Lamp (贪心)

///给你一数字,删除其中的m位使其最小 ///贪心:前面的数要小于后面的数 # include <stdio.h> # include <algorithm> # include <iostream> # include <string.h> # include <math.h> using namespace std; char str[1010]; int a[1010]; int c[1010]; int main() { int k,m

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

原题目 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3964    Accepted Submission(s): 1605 Problem Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the

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 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 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, unfortunately the genie in the lamp is not so kind. Kiki must answer a question

hdu A Magic Lamp

http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2556    Accepted Submission(s): 999 Problem Description Kiki likes traveling. One day

E. A Magic Lamp

E. A Magic Lamp Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name: Main Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki