武大OJ 574. K-th smallest

Description

     Give you a number S of length n,you can choose a position and remove the number on it.After that,you will get a new number. 

 

     More formally,you choose a number x(1<=x<=n),then you will get the number Rx=S1S2…..Sx-1 Sx+1……Sn..The problem is what number x you choose will get k-th smallest Rx of all R.

 

     If there are more than one answer,choose smallest x.

 

Input

First line of each case contains two numbers n and k.(2 ≤ k≤  n ≤ 1 000 000).

Next line contains a number of length n. Each position corresponds to a number of 1-9.

Output

Output x on a single line for each case.

Sample Input

10 5
6228814462
10 4
9282777691

Sample Output

10
5



考虑123456543212345这种数字,就可以找到规律了
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<stdlib.h>
 5 #include<string.h> 
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 int n,k;
10 typedef struct{
11     int num,sum,firstX;
12 }Point;
13 Point p[1000005];
14 
15 int solve(int tot){
16     int i=1;
17     while(i<=tot)
18     {
19         while(i<=tot&&p[i].num>p[i+1].num)
20         {
21             k-=p[i].sum;
22             if(k<=0)
23             {
24                 return p[i].firstX;    
25             }
26             p[i].sum=0;
27             i++;    
28         }
29         i++;    
30     }
31     
32     for(i=tot;i>=1;--i)
33     {
34         k-=p[i].sum;
35         if(k<=0)
36         {
37             return p[i].firstX;    
38         }
39         p[i].sum=0;
40     }
41     return 0;
42 
43 }
44 
45 
46 int main()
47 {
48     p[0].num=0;
49     while(~scanf("%d %d",&n,&k))
50     {
51         char c;scanf("%c",&c);
52         int tot=0;
53         for(int i=1;i<=n;++i)
54         {
55             scanf("%c",&c);
56             if(p[tot].num==c-'0')
57             {
58                 p[tot].sum++;
59             }
60             else 
61             {
62                 tot++;
63                 p[tot].num=c-'0';
64                 p[tot].sum=1;
65                 p[tot].firstX=i;
66             }
67         }
68         
69     //    for(int i=1;i<=tot;++i)
70     //    cout<<p[i].num<<" "<<p[i].sum<<" "<<p[i].firstX<<endl;
71         
72         cout<<solve(tot)<<endl;
73         
74     
75     }
76 
77 }

 



转载于:https://www.cnblogs.com/noip/p/9571193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值