Codeforces Round #667 (Div. 3)
A. Yet Another Two Integers Problem
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 1e4 + 10 ;
using namespace std;
void solve ( ) {
int a, b;
cin>> a>> b;
int t= ceil ( abs ( b- a) * 1.0 / 10 ) ;
cout<< t<< endl;
}
signed main ( ) {
int _ = 1 ;
cin>> _;
while ( _-- ) {
solve ( ) ;
}
return 0 ;
}
B. Minimum Product
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 1e4 + 10 ;
using namespace std;
void solve ( ) {
int a, b, x, y, n;
cin>> a>> b>> x>> y>> n;
cout<< min ( max ( y, b- n+ min ( n, a- x) ) * max ( x, a- n) , max ( x, a- n+ min ( n, b- y) ) * max ( y, b- n) ) << endl;
}
signed main ( ) {
int _ = 1 ;
cin>> _;
while ( _-- ) {
solve ( ) ;
}
return 0 ;
}
C. Yet Another Array Restoration
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 1e4 + 10 ;
using namespace std;
void solve ( ) {
int n, x, y;
cin>> n>> x>> y;
for ( int i= 1 ; i<= 50 ; ++ i)
if ( ( y - x) % i == 0 && ( y - x) / i < n) {
int sum= y+ ( n- ceil ( y* 1.0 / i) ) * i;
if ( y/ i>= n) sum= y;
for ( int j= sum ; n-- ; j - = i) cout << j << " " ;
cout<< endl;
return ;
}
}
signed main ( ) {
int _ = 1 ;
cin>> _;
while ( _-- ) {
solve ( ) ;
}
return 0 ;
}
D. Decrease the Sum of Digits
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 1e4 + 10 ;
using namespace std;
int sum ( int n) {
int a= 0 ;
while ( n) a+ = n% 10 , n/ = 10 ;
return a;
}
void solve ( ) {
int n, s, num= 0 , b= 10 ;
cin>> n>> s, num= n;
while ( sum ( n) > s) n+ = b- n% b, b* = 10 ;
cout<< n- num<< endl;
}
signed main ( ) {
int _ = 1 ;
cin>> _;
while ( _-- ) solve ( ) ;
return 0 ;
}
E. Two Platforms
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 2e5 + 10 ;
using namespace std;
int a[ maxn] , x[ maxn] , l[ maxn] ;
void solve ( ) {
int n, k, y, ans= 0 ;
memset ( a, 0 , sizeof ( a) ) ;
cin >> n >> k;
for ( int i = 1 ; i <= n; ++ i) cin >> x[ i] ;
sort ( x + 1 , x + n + 1 ) ;
for ( int j = 1 ; j <= n; ++ j) cin >> y;
for ( int i = n; i>= 1 ; i-- ) {
int nx= x[ i] + k;
int ii= upper_bound ( x+ 1 , x+ n+ 1 , nx) - x;
a[ i] = max ( a[ i+ 1 ] , ii- i) ;
}
for ( int i = 1 ; i <= n; ++ i) {
int nx= x[ i] + k;
int ii= upper_bound ( x+ 1 , x+ n+ 1 , nx) - x;
ans= max ( ans, a[ ii] + ii- i) ;
}
cout << ans << endl;
}
signed main ( ) {
int _ = 1 ;
cin >> _;
while ( _-- ) solve ( ) ;
return 0 ;
}
F. Subsequences of Length Two
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int maxn = 2e5 + 10 ;
const int N= 205 ;
using namespace std;
int dp[ N] [ N] [ N] ;
void solve ( ) {
int n, kk;
char a[ N] , b[ N] ; int max_= 0 ;
cin>> n>> kk>> a>> b;
if ( b[ 0 ] == b[ 1 ] ) {
char c= b[ 1 ] ;
int cnt= count ( a, a+ n, c) ;
cnt= min ( cnt+ kk, n) ;
max_= cnt* ( cnt- 1 ) / 2 ;
} else {
memset ( dp, - 1000000 , sizeof ( dp) ) ;
dp[ 0 ] [ 0 ] [ 0 ] = 0 ;
for ( int i = 1 ; i <= n; ++ i) {
for ( int j = 0 ; j <= n; ++ j) {
for ( int k = 0 ; k <= kk; ++ k) {
int & res = dp[ i] [ j] [ k] ;
if ( a[ i- 1 ] == b[ 0 ] && j) res = max ( res, dp[ i - 1 ] [ j- 1 ] [ k] ) ;
else if ( a[ i- 1 ] == b[ 1 ] ) res= max ( res, dp[ i- 1 ] [ j] [ k] + j) ;
else res = dp[ i - 1 ] [ j] [ k] ;
if ( k) {
if ( j&& a[ i- 1 ] != b[ 0 ] ) res = max ( res, dp[ i - 1 ] [ j - 1 ] [ k - 1 ] ) ;
if ( a[ i- 1 ] != b[ 1 ] ) res = max ( res, dp[ i- 1 ] [ j] [ k- 1 ] + j) ;
}
max_= max ( max_, res) ;
}
}
}
}
cout<< max_<< endl;
}
signed main ( ) {
int _ = 1 ;
while ( _-- ) solve ( ) ;
return 0 ;
}