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

A Magic Lamp(贪心)

时间:2015-11-14 23:16:06      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

A Magic Lamp(贪心)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4965196.html

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