A1041 Be Unique
# include <iostream>
# include <vector>
using namespace std; ;
int main ( ) {
vector< int > nums;
int n, num[ 10001 ] { } ;
cin>> n;
for ( int i= 0 ; i< n; i++ ) {
int t;
cin>> t;
num[ t] += 1 ;
nums. push_back ( t) ;
}
bool flag = true ;
for ( int i: nums) {
if ( num[ i] == 1 ) {
cout<< i;
flag = false ;
break ;
}
}
if ( flag)
cout<< "None" ;
}
A1042 Shuffling Machine
# include <iostream>
using namespace std;
int main ( ) {
int k, s[ 55 ] { } , t[ 55 ] { } ;
cin>> k;
for ( int i= 1 ; i< 55 ; i++ ) {
cin>> s[ i] ;
t[ i] = i;
}
for ( int i= 0 ; i< k; i++ ) {
int sh[ 55 ] { } ;
for ( int j= 1 ; j< 55 ; j++ ) {
sh[ s[ j] ] = t[ j] ;
}
for ( int j= 1 ; j< 55 ; j++ )
t[ j] = sh[ j] ;
}
for ( int i= 1 ; i< 55 ; i++ ) {
int m = t[ i] / 13 , c = t[ i] % 13 ;
if ( c == 0 ) {
m -= 1 ;
c = 13 ;
}
switch ( m) {
case 0 : cout<< "S" << c; break ;
case 1 : cout<< "H" << c; break ;
case 2 : cout<< "C" << c; break ;
case 3 : cout<< "D" << c; break ;
case 4 : cout<< "J" << c;
}
if ( i < 54 ) cout<< " " ;
}
}
A1043 Is It a Binary Search Tree
# include <iostream>
# include <vector>
using namespace std;
typedef struct Node {
int val;
Node * left, * right;
} * Bitree;
void insertNode ( Bitree & head, Node* key) {
if ( ! head) {
head = key;
return ;
}
if ( key-> val < head-> val)
insertNode ( head-> left, key) ;
else
insertNode ( head-> right, key) ;
}
vector< int > pre1, pre2, ans, res;
void preorder ( Bitree head) {
if ( ! head) return ;
pre1. push_back ( head-> val) ;
preorder ( head-> left) ;
preorder ( head-> right) ;
}
void premir ( Bitree head) {
if ( ! head) return ;
pre2. push_back ( head-> val) ;
premir ( head-> right) ;
premir ( head-> left) ;
}
void postorder ( Bitree head) {
if ( ! head) return ;
postorder ( head-> left) ;
postorder ( head-> right) ;
res. push_back ( head-> val) ;
}
void postmir ( Bitree head) {
if ( ! head) return ;
postmir ( head-> right) ;
postmir ( head-> left) ;
res. push_back ( head-> val) ;
}
int main ( ) {
int n;
cin>> n;
Bitree head = NULL ;
for ( int i= 0 ; i< n; i++ ) {
Node* node = new Node;
cin>> node-> val;
node-> left = NULL ;
node-> right = NULL ;
insertNode ( head, node) ;
ans. push_back ( node-> val) ;
}
preorder ( head) ;
premir ( head) ;
if ( pre1 == ans)
postorder ( head) ;
else if ( pre2 == ans)
postmir ( head) ;
if ( ! res. size ( ) ) cout<< "NO" ;
else {
cout<< "YES" << endl;
for ( int i= 0 ; i< res. size ( ) ; i++ ) {
cout<< res[ i] ;
if ( i < res. size ( ) - 1 )
cout<< " " ;
}
}
}
A1044 Shopping in Mars
# include <iostream>
# include <vector>
# include <climits>
using namespace std;
int main ( ) {
int n, amount;
cin>> n>> amount;
vector< pair< int , int >> res;
int num[ n+ 1 ] , min = INT_MAX, i= 1 , j= 1 , total = 0 ;
for ( int t= 1 ; t<= n; t++ )
scanf ( "%d" , & num[ t] ) ;
while ( j<= n) {
total += num[ j++ ] ;
if ( total >= amount) {
while ( i< j) {
if ( total- num[ i] >= amount) {
total -= num[ i++ ] ;
} else {
if ( min > total) {
min = total;
res. clear ( ) ;
res. push_back ( make_pair ( i, j- 1 ) ) ;
} else if ( min == total)
res. push_back ( make_pair ( i, j- 1 ) ) ;
break ;
}
}
}
}
for ( int i= 0 ; i< res. size ( ) ; i++ ) {
cout<< res[ i] . first<< "-" << res[ i] . second<< endl;
}
}
A1045 Favorite Color Stripe
# include <iostream>
# include <vector>
using namespace std;
int main ( ) {
int n, m, l;
cin>> n>> m;
int cur = 1 , fav[ 201 ] { } , strip[ 10001 ] ;
for ( int i= 0 ; i< m; i++ ) {
int t;
cin>> t;
fav[ t] = cur++ ;
}
cin>> l;
cur = 0 ;
for ( int i= 0 ; i< l; i++ ) {
int t;
cin>> t;
if ( fav[ t] ) strip[ cur++ ] = fav[ t] ;
}
int ans = 0 , dp[ cur] ;
for ( int i= 0 ; i< cur; i++ ) {
dp[ i] = 1 ;
for ( int j= 0 ; j< i; j++ ) {
if ( strip[ j] <= strip[ i] )
dp[ i] = max ( dp[ i] , dp[ j] + 1 ) ;
}
ans = max ( ans, dp[ i] ) ;
}
cout<< ans;
}
# include <iostream>
using namespace std;
int main ( ) {
int n, m, l;
cin>> n>> m;
int cur = 1 , fav[ m+ 1 ] { } ;
for ( int i= 1 ; i<= m; i++ )
cin>> fav[ i] ;
cin>> l;
int strip[ l+ 1 ] { } ;
for ( int i= 1 ; i<= l; i++ )
cin>> strip[ i] ;
int dp[ m+ 1 ] [ l+ 1 ] { } ;
for ( int i= 1 ; i<= m; i++ ) {
for ( int j= 1 ; j<= l; j++ ) {
dp[ i] [ j] = max ( dp[ i- 1 ] [ j] , dp[ i] [ j- 1 ] ) ;
if ( fav[ i] == strip[ j] ) dp[ i] [ j] ++ ;
}
}
cout<< dp[ m] [ l] ;
}
A1046 Shortest Distance
# include <iostream>
using namespace std;
int main ( ) {
int n, m;
cin>> n;
int dis[ n+ 1 ] { } , total = 0 ;
for ( int i= 1 ; i<= n; i++ ) {
scanf ( "%d" , & dis[ i] ) ;
total += dis[ i] ;
dis[ i] += dis[ i- 1 ] ;
}
cin>> m;
for ( int i= 0 ; i< m; i++ ) {
int a, b, sd;
cin>> a>> b;
sd = dis[ max ( a, b) - 1 ] - dis[ min ( a, b) - 1 ] ;
cout<< min ( sd, total- sd) << endl;
}
}
A1047 Student List for Course
# include <iostream>
# include <vector>
# include <algorithm>
# include <cstring>
using namespace std;
int cmp ( char * a, char * b) {
return strcmp ( a, b) < 0 ;
}
int main ( ) {
int n, k;
cin>> n>> k;
char str[ 40000 ] [ 5 ] ;
vector< char * > course[ 2501 ] ;
for ( int i= 0 ; i< n; i++ ) {
scanf ( "%s" , str[ i] ) ;
int c;
scanf ( "%d" , & c) ;
for ( int j= 0 ; j< c; j++ ) {
int cid;
scanf ( "%d" , & cid) ;
course[ cid] . push_back ( str[ i] ) ;
}
}
for ( int i= 1 ; i<= k; i++ ) {
printf ( "%d %d\n" , i, course[ i] . size ( ) ) ;
sort ( course[ i] . begin ( ) , course[ i] . end ( ) , cmp) ;
for ( int j= 0 ; j< course[ i] . size ( ) ; j++ )
printf ( "%s\n" , course[ i] [ j] ) ;
}
}
A1048 Find Coins
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
int main ( ) {
int n, m;
cin>> n>> m;
int coin[ n] ;
for ( int i= 0 ; i< n; i++ )
scanf ( "%d" , & coin[ i] ) ;
sort ( coin, coin+ n) ;
for ( int i= 0 ; i< n; i++ ) {
if ( coin[ i] < m) {
int left = i+ 1 , right = n- 1 ;
while ( left <= right) {
int mid = left + ( right - left) / 2 ;
if ( coin[ i] + coin[ mid] == m) {
cout<< coin[ i] << " " << coin[ mid] ;
return 0 ;
} else if ( coin[ i] + coin[ mid] < m)
left = mid+ 1 ;
else
right = mid- 1 ;
}
} else
break ;
}
cout<< "No Solution" ;
}
A1049 Counting Ones
# include <iostream>
# include <string>
# define ll long long
using namespace std;
int main ( ) {
ll n, total = 0 ;
cin>> n;
string num = to_string ( n) ;
for ( int i= 0 ; i< num. size ( ) ; i++ ) {
string th = num. substr ( 0 , i) , tl = num. substr ( i+ 1 ) ;
if ( th == "" ) th = "0" ;
if ( tl == "" ) tl = "0" ;
ll tn = 1 ;
for ( int j= i+ 1 ; j< num. size ( ) ; j++ )
tn *= 10 ;
if ( num[ i] == '1' )
total += stoll ( th) * tn + stoll ( tl) + 1 ;
else if ( num[ i] < '1' )
total += stoll ( th) * tn;
else
total += ( stoll ( th) + 1 ) * tn;
}
cout<< total;
}
# include <iostream>
# include <string>
using namespace std;
int main ( ) {
int n, i = 1 , total = 0 , index, low, high;
cin>> n;
while ( n/ i) {
index = n/ i% 10 ;
high = n/ i/ 10 ;
low = n% i;
if ( index == 1 ) total += high* i + low+ 1 ;
else if ( index < 1 ) total += high* i;
else total += ( high+ 1 ) * i;
i *= 10 ;
}
cout<< total;
}
A1050 String Subtraction
# include <iostream>
using namespace std;
int main ( ) {
string s1, s2;
getline ( cin, s1) ;
getline ( cin, s2) ;
for ( int i= 0 ; i< s1. size ( ) ; i++ ) {
for ( int j= 0 ; j< s2. size ( ) ; j++ ) {
if ( s1[ i] == s2[ j] ) {
s1[ i] = '0' ;
break ;
}
}
}
for ( int i= 0 ; i< s1. size ( ) ; i++ ) {
if ( s1[ i] != '0' )
cout<< s1[ i] ;
}
}