团体程序设计天梯赛-L2题解
L2-002 链表去重 (25分)
# include <bits/stdc++.h>
using namespace std;
struct node
{
int key;
int next;
} add[ 100005 ] ;
int f[ 100005 ] ;
int a1[ 100005 ] , a2[ 100005 ] ;
int main ( )
{
int l, n;
cin >> l >> n;
for ( int i = 0 ; i < n; i++ )
{
int addr;
cin >> addr;
cin >> add[ addr] . key >> add[ addr] . next;
}
int k1 = 0 , k2 = 0 ;
for ( int i = l; i != - 1 ; i = add[ i] . next)
{
if ( f[ abs ( add[ i] . key) ] == 0 )
{
f[ abs ( add[ i] . key) ] = 1 ;
a1[ k1++ ] = i;
}
else
{
a2[ k2++ ] = i;
}
}
if ( k1)
{
for ( int i = 0 ; i < k1 - 1 ; i++ )
{
printf ( "%05d %d %05d\n" , a1[ i] , add[ a1[ i] ] . key, a1[ i + 1 ] ) ;
}
printf ( "%05d %d -1\n" , a1[ k1 - 1 ] , add[ a1[ k1 - 1 ] ] . key) ;
}
if ( k2)
{
for ( int i = 0 ; i < k2 - 1 ; i++ )
{
printf ( "%05d %d %05d\n" , a2[ i] , add[ a2[ i] ] . key, a2[ i + 1 ] ) ;
}
printf ( "%05d %d -1" , a2[ k2 - 1 ] , add[ a2[ k2 - 1 ] ] . key) ;
}
return 0 ;
}
L2-003 月饼 (25分)
# include <bits/stdc++.h>
using namespace std;
struct node
{
double num, value, ave;
} stu[ 1010 ] ;
bool cmp ( node a, node b)
{
return a. ave > b. ave;
}
int main ( )
{
int n;
double d;
cin >> n >> d;
for ( int i = 0 ; i < n; i++ )
cin >> stu[ i] . num;
for ( int i = 0 ; i < n; i++ )
cin >> stu[ i] . value;
for ( int i = 0 ; i < n; i++ )
{
stu[ i] . ave = stu[ i] . value / stu[ i] . num;
}
sort ( stu, stu + n, cmp) ;
int i = 0 ;
double ans = 0 ;
while ( d >= 0 && i < n)
{
if ( d > stu[ i] . num)
{
d -= stu[ i] . num;
ans += stu[ i] . value;
i++ ;
}
else
{
ans += d * stu[ i] . ave;
break ;
}
}
cout << fixed << setprecision ( 2 ) << ans;
return 0 ;
}
L2-004 这是二叉搜索树吗? (25分)
# include <iostream>
# include <stdlib.h>
using namespace std;
typedef struct node * BinTree;
int a[ 20000 ] , b[ 20000 ] , c[ 20000 ] ;
struct node
{
int data;
BinTree l;
BinTree r;
} ;
BinTree Create ( BinTree BT, int x)
{
if ( ! BT)
{
BT= ( BinTree) malloc ( sizeof ( struct node ) ) ;
BT-> data= x;
BT-> l= BT-> r= NULL ;
}
else
{
if ( x< BT-> data)
BT-> l= Create ( BT-> l, x) ;
else
BT-> r= Create ( BT-> r, x) ;
}
return BT;
}
int t= 0 ;
void Pre1 ( BinTree BT)
{
if ( BT)
{
a[ t++ ] = BT-> data;
Pre1 ( BT-> l) ;
Pre1 ( BT-> r) ;
}
}
int t1= 0 ;
void Pre2 ( BinTree BT)
{
if ( BT)
{
b[ t1++ ] = BT-> data;
Pre2 ( BT-> r) ;
Pre2 ( BT-> l) ;
}
}
int f= 0 ;
void Post1 ( BinTree BT)
{
if ( BT)
{
Post1 ( BT-> l) ;
Post1 ( BT-> r) ;
if ( f== 0 )
{
cout<< BT-> data;
f++ ;
}
else
cout<< " " << BT-> data;
}
}
void Post2 ( BinTree BT)
{
if ( BT)
{
Post2 ( BT-> r) ;
Post2 ( BT-> l) ;
if ( f== 0 )
{
cout<< BT-> data;
f++ ;
}
else
cout<< " " << BT-> data;
}
}
int main ( )
{
BinTree BT= NULL ;
int N;
cin>> N;
for ( int i= 0 ; i< N; i++ )
{
cin>> c[ i] ;
BT= Create ( BT, c[ i] ) ;
}
Pre1 ( BT) ;
Pre2 ( BT) ;
int f1= 0 , f2= 0 ;
for ( int i= 0 ; i< N; i++ )
{
if ( a[ i] != c[ i] )
{
f1= 1 ;
break ;
}
}
for ( int i= 0 ; i< N; i++ )
{
if ( b[ i] != c[ i] )
{
f2= 1 ;
break ;
}
}
if ( f1&& f2)
{
cout<< "NO" << endl;
}
else
{
cout<< "YES" << endl;
if ( ! f1) Post1 ( BT) ;
else Post2 ( BT) ;
}
}
L2-005 集合相似度 (25分)
# include <bits/stdc++.h>
using namespace std;
int main ( )
{
set< int > st[ 500 ] ;
int n;
cin>> n;
for ( int i= 1 ; i<= n; i++ )
{
int m;
cin>> m;
while ( m-- )
{
int x;
cin>> x;
st[ i] . insert ( x) ;
}
}
int k;
cin>> k;
while ( k-- )
{
int a, b;
cin>> a>> b;
double nc= 0 , nt;
for ( auto e: st[ b] )
{
if ( st[ a] . find ( e) != st[ a] . end ( ) )
nc++ ;
}
nt= st[ a] . size ( ) + st[ b] . size ( ) - nc;
cout<< fixed<< setprecision ( 2 ) << nc/ nt* 100 << "%" << endl;
}
return 0 ;
}
L2-006 树的遍历 (25分)
# include <iostream>
# include <queue>
using namespace std;
struct node
{
struct node * l, * r;
int data;
} ;
struct node * change ( int * post, int * in, int n)
{
int i;
if ( n <= 0 )
return NULL ;
struct node * bt = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
bt-> data = post[ n - 1 ] ;
for ( i = 0 ; in[ i] != post[ n - 1 ] ; i++ ) ;
bt-> l = change ( post, in, i) ;
bt-> r = change ( post + i, in + 1 + i, n - i - 1 ) ;
return bt;
}
void bfs ( struct node * bt)
{
queue< struct node * > q;
q. push ( bt) ;
int f = 0 ;
while ( ! q. empty ( ) )
{
struct node * t = q. front ( ) ;
q. pop ( ) ;
if ( f) cout << " " ;
cout << t-> data, f++ ;
if ( t-> l)
q. push ( t-> l) ;
if ( t-> r)
q. push ( t-> r) ;
}
}
int main ( )
{
struct node * bt;
int n;
cin >> n;
int a[ 110 ] , b[ 110 ] ;
for ( int i = 0 ; i < n; i++ )
cin >> a[ i] ;
for ( int i = 0 ; i < n; i++ )
cin >> b[ i] ;
bt = change ( a, b, n) ;
bfs ( bt) ;
}
L2-007 家庭房产 (25分)
# include <bits/stdc++.h>
using namespace std;
vector< int > p ( 10000 ) ;
set< int > st;
int sizep[ 10010 ] ;
struct stu1
{
int no;
double num;
double size;
} ans[ 10101 ] ;
struct stu2
{
int no;
double num;
double size;
} stu[ 10101 ] ;
bool cmp ( stu1 a, stu1 b)
{
if ( a. size == b. size)
return a. no < b. no;
return a. size > b. size;
}
int find ( int x)
{
return p[ x] == x ? x : p[ x] = find ( p[ x] ) ;
}
void un ( int x, int y)
{
x = find ( x) , y = find ( y) ;
if ( x != y)
{
if ( x > y)
p[ x] = y, sizep[ y] += sizep[ x] ;
else
p[ y] = x, sizep[ x] += sizep[ y] ;
}
}
int main ( )
{
int n;
cin >> n;
iota ( p. begin ( ) , p. end ( ) , 0 ) ;
for ( int i= 0 ; i< 10000 ; i++ ) sizep[ i] = 1 ;
for ( int i = 0 ; i < n; i++ )
{
int b, c;
cin >> stu[ i] . no >> b >> c;
if ( b != - 1 )
un ( stu[ i] . no, b) ;
if ( c != - 1 )
un ( stu[ i] . no, c) ;
int k;
cin >> k;
while ( k-- )
{
int x;
cin >> x;
un ( stu[ i] . no, x) ;
}
cin >> stu[ i] . num >> stu[ i] . size;
}
for ( int i = 0 ; i < n; i++ )
{
int id = find ( stu[ i] . no) ;
st. insert ( id) ;
ans[ id] . no = id;
ans[ id] . num += stu[ i] . num/ sizep[ id] ;
ans[ id] . size += stu[ i] . size/ sizep[ id] ;
}
cout << st. s