hdu 3183 贪心

题意:给一个数字,删掉其中的若干位,使得最后的数字最小

就是每次删除数的时候都是删掉第一个比右边数大的数

利用双向链表模拟

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=1005;
 9 int n,m,t;
10 char s[maxn];
11 struct Node
12 {
13     int v;
14     int from,to;
15     void input(char val,int i)
16     {
17         v=val-'0';
18         from=i-1;
19         to=i+1;
20     }
21 }node[maxn];
22 void del(int t)
23 {
24     int temp=node[t].from;
25     node[temp].to=node[t].to;
26     node[node[t].to].from=temp;
27 }
28 int main()
29 {
30     int i,j,k;
31     #ifndef ONLINE_JUDGE
32     freopen("1.in","r",stdin);
33     #endif
34     while(scanf("%s%d",&s,&m)!=EOF)
35     {
36         int n=strlen(s);
37         for(i=1;i<=n;i++)
38         {
39             node[i].input(s[i-1],i);
40         }
41         node[0].to=1;
42         node[n+1].from=n;
43         int t=0;
44         while(m--)
45         {
46             while(node[t].to!=n+1&&node[node[t].to].v>=node[t].v) t=node[t].to;
47             del(t);
48             t=node[t].from;
49         }
50         t=node[0].to;
51         while(t!=n+1 && node[t].v==0)t=node[t].to;
52         if(t==n+1)
53         {
54             printf("0\n");
55             continue;
56         }
57         while(t!=n+1)
58         {
59             printf("%d",node[t].v);
60             t=node[t].to;
61         }
62         printf("\n");
63     }
64 }

 

posted @ 2015-03-24 22:55  miao_a_miao  阅读(118)  评论(0编辑  收藏  举报