Codeforces Round #668 (Div. 2)
A. Permutation Forgery
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 1e6 + 10 ;
const int N= 205 ;
using namespace std;
void solve ( ) {
int p[ 105 ] ;
int n;
cin>> n;
for ( int i = 1 ; i <= n; ++ i) cin>> p[ i] ;
for ( int j = n; j >= 1 ; -- j) cout<< p[ j] << " " ;
cout<< endl;
}
signed main ( ) {
int _ = 1 ;
cin >> _;
while ( _-- ) solve ( ) ;
return 0 ;
}
B. Array Cancellation
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 1e5 + 10 ;
const int N= 205 ;
using namespace std;
int p[ maxn] ;
void solve ( ) {
int n, flag= 0 , z= 0 , f= 0 ;
cin>> n;
for ( int i = 1 ; i <= n; ++ i) {
cin>> p[ i] ;
if ( p[ i] > 0 ) flag= 1 ;
if ( ! flag) f- = p[ i] ;
if ( flag) {
z+ = p[ i] ;
if ( z< 0 ) f- = z, z= 0 ;
}
} int t= ceil ( ( f+ z) * 1.0 / 2 ) ;
cout<< t<< endl;
}
signed main ( ) {
int _ = 1 ;
cin >> _;
while ( _-- ) solve ( ) ;
return 0 ;
}
C. Balanced Bitstring
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void solve ( ) {
int n, k, flag= 1 ;
string s;
cin>> n>> k>> s; char t[ k] ;
for ( int j = 0 ; j < k; ++ j) t[ j] = s[ j] ;
for ( int i = 0 ; i < s. size ( ) ; ++ i) {
if ( s[ i] != '?' ) {
if ( s[ i] != t[ i% k] ) {
if ( t[ i% k] != '?' ) { flag= 0 ; break ; }
else t[ i% k] = s[ i] ;
}
}
}
int a= 0 , b= 0 , c= 0 ;
for ( int i= 0 ; i< k; i++ ) {
if ( t[ i] == '1' ) a++ ;
if ( t[ i] == '0' ) b++ ;
if ( t[ i] == '?' ) c++ ;
}
if ( abs ( a- b) > c) flag= 0 ;
cout<< ( flag? "YES" : "NO" ) << endl;
}
signed main ( ) {
int _ = 1 ;
cin >> _;
while ( _-- ) solve ( ) ;
return 0 ;
}
#include <bits/stdc++.h>
using namespace std;
int cnt[ 300003 ] ;
int main ( ) {
int T;
cin>> T;
while ( T-- ) {
int n, k, ret0= 0 , ret1= 0 ;
string s;
cin>> n>> k>> s;
for ( int i= 0 ; i< n; i++ ) cnt[ i] = 0 ;
for ( int i= 0 ; i< n; i++ )
if ( s[ i] != '?' ) cnt[ i% k] | = ( 1 << ( s[ i] - '0' ) ) ;
bool ok= 1 ;
for ( int i= 0 ; i< k; i++ ) {
if ( cnt[ i] == 1 ) ret0++ ;
if ( cnt[ i] == 2 ) ret1++ ;
if ( cnt[ i] == 3 ) ok= 0 ;
}
if ( ret0> k/ 2 || ret1> k/ 2 ) ok= 0 ;
cout<< ( ok? "YES" : "NO" ) << endl;
}
}
D. Tree Tag
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 0x3f3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 1e5 + 10 ;
using namespace std;
vector< int > to[ maxn] ;
int r= 0 , depth[ maxn] ;
int n, a, b, da, db;
int dfs ( int x, int f) {
int len= 0 ;
for ( int y: to[ x] ) {
if ( y!= f) {
depth[ y] = depth[ x] + 1 ;
int ans= 1 + dfs ( y, x) ;
r= max ( ans+ len, r) ;
len= max ( len, ans) ;
}
}
return len;
}
void solve ( ) {
cin>> n>> a>> b>> da>> db;
for ( int i = 1 ; i <= n; ++ i) to[ i] . clear ( ) ;
for ( int i = 0 ; i < n- 1 ; ++ i) {
int u, v;
cin>> u>> v;
to[ u] . push_back ( v) ;
to[ v] . push_back ( u) ;
}
r= 0 ;
depth[ a] = 0 ;
dfs ( a, - 1 ) ;
if ( 2 * da>= min ( r, db) || depth[ b] <= da) cout<< "Alice" << endl;
else cout<< "Bob" << endl;
}
signed main ( ) {
int _ = 1 ;
cin >> _;
while ( _-- ) {
solve ( ) ;
}
return 0 ;
}