Pagini recente » Cod sursa (job #2983699) | Cod sursa (job #2191823) | Istoria paginii runda/tema_1_lot/clasament | Cod sursa (job #2553486) | Cod sursa (job #480365)
Cod sursa(job #480365)
# include <fstream>
using namespace std;
const char FIN[] = "sdo.in", FOU[] = "sdo.out" ;
const int MAX = 3000004 ;
int A[MAX];
int N, K;
void cit ( int & ) ;
int parti ( int * , int , int ) ;
void sdo ( int * , int , int , int ) ;
void read_data ( void ) {
ifstream f ( FIN ) ;
cit ( N ) , cit ( K ) ;
for ( int i = 1; i <= N; ++i ) {
cit ( A[i] ) ;
}
}
int main ( void ) {
read_data () ;
sdo ( A, 1, N, K ) ;
ofstream g ( FOU ) ;
g << A[K] ;
}
int parti ( int A[], int st, int dr ) {
int i = st - 1, j = dr + 1, piv = A [ st + ( rand () % ( dr - st + 1 ) ) ] ;
while ( true ) {
for ( ++i ; A[i] < piv; ++i ) ;
for ( --j ; A[j] > piv; --j ) ;
if ( i < j ) {
swap ( A[i], A[j] ) ;
} else {
return j ;
}
}
return 0 ;
}
void sdo ( int A[], int st, int dr, int k ) {
if ( st == dr) {
return ;
}
int q = parti ( A, st, dr ) ;
int t = q - st + 1 ;
if ( t >= k ) {
sdo ( A, st, q, k ) ;
} else {
sdo ( A, q + 1, dr, k - t ) ;
}
}
const int hg = 1 << 13 ;
int poz ;
char ch[hg] ;
inline void cit ( int &x )
{
x = 0;
if ( ch[0] == '\0' ) fread (ch, 1, hg, stdin);
else while ( ch[poz] < '0' || ch[poz] > '9' )
if ( ++poz == hg )
fread (ch, 1, hg, stdin), poz = 0;
while ( ch[poz] >= '0' && ch[poz] <= '9' )
{
x = x * 10 + ch[poz] - '0';
if ( ++poz == hg )
fread (ch, 1, hg, stdin), poz = 0;
}
}