SGU - 296 - Sasha vs. Kate

上题目:

296. Sasha vs. Kate

Time limit per test: 1 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard




During the regular Mars's World Finals Subregional Programming Contest a boy Sasha lost N "Mars" bars of chocolate to a girl Kate. But for two years already Sasha does not hurry to pay his debt. And now Sasha and Kate decided that Sasha will give Kate P chocolate bars, where number P can be obtained from the number N by removing exactly K decimal digits. Sasha generously let Kate to choose digits to be removed. Your task is to find out how many bars Sasha will give Kate. Of course Kate will choose K digits from the number N in such a way that the resulting number P would be maximal.

Input

The first line of the input file contains two integer numbers N and K (1≤ N≤ 101000; 0≤ K≤ 999). Number K is strictly less than the number of digits in N. N will not have any leading zeros.

Output

Output the unknown P.

Example(s)
sample input
sample output
1992 2
99

 

sample input
sample output
1000 2
10

 

  题意是给你一个最多有1001的数字,然后让你剔除其中的某K个数字,求可以得到的最大的数字。

  这一题用贪心思想。

     这一题一开始读错题目,以为是挑K个数字,还以为是连续的,然后就WA了两次。后来终于看清题目了。然后就开始了各种yy,想过的思路有不少,最后列举了几个数字,找到了一点规律。

      规律是这样的:当第i个数字比后一个数字小的话,把它删掉可以比删掉其他数字的结果得到更大的结果,然后如果后面的数字等于或者小于当前的数字的话,说明当前的数字暂时可以不用删掉,然后对i+1操作。

      当然,这个规律还不完善,如果数字为32121,要求删掉2个数字的话,那当然是删掉两个1,可是刚才的思路只删除了一个1,同时如果在外面加一个循环也不能解决问题,如果序列式是32112的话就可以很好地得到正确答案,那应该怎么做呢,暂时的做法是在所有数字的结尾加一个INF,这是根据我的代码的结构(我是用栈来实现的)写出来的解决方案= =,好像有点XX(= =),AC的代码有bug= =现在改了一下。听其他同学的解法好像用了一个for和一个while就搞定了。= =

 

上代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 #include <stack>
 5 #include <algorithm>
 6 #define MAX 300000+10
 7 using namespace std;
 8 
 9 
10 int s[MAX];
11 stack<int> S;
12 
13 int main()
14 {
15     //freopen("data.txt","r",stdin);
16     int i,j,n,k,e,h;
17     char c;
18     k=0;
19     memset(s,-1,sizeof(s));
20     while((c=getchar())!=' ')
21     {
22         s[k]=c-'0';
23         k++;
24     }
25     scanf("%d",&n);
26     h=n;
27     i=0;
28     //S.push(s[i]);
29     //i++;
30     while(h)
31     {
32         while(!S.empty() && s[i]>S.top())
33         {
34                 S.pop();
35                 h--;
36                 if(h==0) break;
37         }
38         if(h==0) break;
39         S.push(s[i]);
40         i++;
41         //if(s[i]==-1) break;
42     }
43     h=S.size();
44     j=0;
45     for(i--;j<h;j++,i--)
46     {
47         s[i]=S.top();
48         S.pop();
49     }
50     //e=0;
51     //for(j=0;j<h;j--) e=e*10+p[j];
52     if(i<k)
53     for(i++;i<k;i++)
54     {
55         printf("%d",s[i]);
56          //e=e*10+s[i];
57     }
58     else printf("0");
59     printf("\n");
60     return 0;
61 }
296

 

posted @ 2013-07-20 23:18  海拉鲁的林克  阅读(337)  评论(0编辑  收藏  举报