武大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 }