数字游戏

Problem Description

小艾和小牛在上数学课的时候觉得非常的无聊,于是他们想出了一个新的游戏,小牛写下N位的数字,小艾的任务就是在去除了K位后得到一个最大的数。

Input

第一行输入一个N和一个K( 1 <= K < N <= 500 000 ).
接下来就是N位数字,确保输入数据中没有前导0。

Output

输出去除了K位后的最大数字。

Sample Input

4 2
1924
7 3
1231234
10 4
4177252841

Sample Output

94
3234
775841
 1 #include<stdio.h>
 2 int p[500005];
 3 char sp[500005];
 4 int main() {
 5     int n,k,i,head,cnt;
 6     while(scanf("%d%d",&n,&k)==2) {
 7         head=cnt=0;
 8         getchar();
 9         gets(sp);
10         p[++head]=sp[0]-'0';
11         for(i=1; i<n; i++) {
12             while(p[head]<sp[i]-'0'&&head!=0) {
13                 if(cnt==k) break;
14                 head--;
15                 cnt++;
16             }
17             p[++head]=sp[i]-'0';
18         }
19         for(i=1; i<=head; i++) {
20             printf("%d",p[i]);
21         }
22         puts("");
23     }
24     return 0;
25 }
View Code(单调队列)

 

posted @ 2013-06-03 17:38  1002liu  阅读(143)  评论(0编辑  收藏  举报