1、模拟
1.1、DNA序列修正
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
using namespace std;
int main ( ) {
IOS;
int N; cin>> N;
string s, t;
cin>> s>> t;
for ( int i= 0 ; i< N; i++ ) {
switch ( s[ i] ) {
case 'A' :
s[ i] = 'T' ;
break ;
case 'T' :
s[ i] = 'A' ;
break ;
case 'C' :
s[ i] = 'G' ;
break ;
case 'G' :
s[ i] = 'C' ;
break ;
}
}
int ans= 0 ;
for ( int i= 0 ; i< N; i++ ) {
if ( s[ i] == t[ i] ) continue ;
for ( int j= i+ 1 ; j< N; j++ ) {
if ( t[ j] == s[ i] && t[ i] == s[ j] ) {
swap ( t[ i] , t[ j] ) ;
break ;
}
}
ans++ ;
}
cout<< ans<< '\n' ;
return 0 ;
}
1.2、无尽的石头
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
using namespace std;
const int N = 1000000 ;
vector< int > a ( N+ 1 , - 1 ) ;
int sm ( int x) {
int s= 0 ;
while ( x) {
s+= ( x% 10 ) ;
x/= 10 ;
}
return s;
}
int main ( ) {
IOS;
int t; cin>> t;
a[ 1 ] = 0 ;
for ( int i= 1 ; i<= N; i++ ) {
if ( a[ i] == - 1 ) continue ;
int x= i+ sm ( i) ;
if ( x> N) break ;
a[ x] = a[ i] + 1 ;
}
while ( t-- ) {
int n; cin>> n;
cout<< a[ n] << "\n" ;
}
return 0 ;
}
2、递归
2.1、带备忘录的斐波那契数列
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
using namespace std;
using ll = long long ;
const int N= 1e5 + 9 ;
const ll p= 1e9 + 7 ;
ll dp[ N] ;
ll fib ( int n) {
if ( dp[ n] ) return dp[ n] ;
if ( n<= 2 ) return 1 ;
return dp[ n] = ( fib ( n- 1 ) + fib ( n- 2 ) ) % p;
}
int main ( ) {
IOS;
int n; cin>> n;
for ( int i= 600 ; i<= n; i++ ) cout<< fib ( i) << "\n" ;
return 0 ;
}
2.2、数的计算
# include <iostream>
using namespace std;
int a[ 10001 ] ;
int dfs ( int dep) {
int res= 1 ;
for ( int i= 1 ; i<= a[ dep- 1 ] / 2 ; i++ ) {
a[ dep] = i;
res+= dfs ( dep+ 1 ) ;
}
return res;
}
int main ( )
{
int n; cin>> n;
a[ 1 ] = n;
cout<< dfs ( 2 ) << "\n" ;
return 0 ;
}
3、进制转换
3.1、进制转换模板
# include <bits/stdc++.h>
using namespace std;
char ch[ ] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' } ;
using ll = long long ;
const int N= 32 ;
int a[ N] ;
int main ( ) {
int T; cin>> T;
while ( T-- ) {
int n, m; cin>> n>> m;
string s; cin>> s;
int len= s. size ( ) ;
for ( int i= 0 ; i< len; i++ ) {
if ( s[ i] >= '0' && s[ i] <= '9' ) {
a[ i] = s[ i] - '0' ;
} else {
a[ i] = s[ i] - 'A' + 10 ;
}
}
ll x= 0 ;
for ( int i= 0 ; i< len; i++ ) {
x= x* n+ a[ i] ;
}
string ans;
while ( x) {
ans+= ch[ x% m] ;
x/= m;
}
reverse ( ans. begin ( ) , ans. end ( ) ) ;
cout<< ans<< "\n" ;
}
return 0 ;
}
3.2、Alice和Bob的爱恨情仇
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
using namespace std;
int main ( ) {
IOS;
int n, k; cin>> n>> k;
int ans= 0 ;
for ( int i= 1 ; i<= n; i++ ) {
int num; cin>> num;
ans+= num;
}
if ( ans% 2 == 0 ) cout<< "Bob" << "\n" ;
else cout<< "Alice" << "\n" ;
return 0 ;
}
4、前缀和
4.1、前缀和模板
# include <bits/stdc++.h>
using namespace std;
const int N = 10001 ;
int prefix[ N] , a[ N] ;
int main ( ) {
int n; cin>> n;
for ( int i= 1 ; i<= n; i++ ) {
cin>> a[ i] ;
prefix[ i] = prefix[ i- 1 ] + a[ i] ;
}
int l, r;
cin>> l>> r;
int sum; sum= prefix[ r] - prefix[ l- 1 ] ;
cout<< sum<< "\n" ;
return 0 ;
}
4.2、区间次方和
# include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 9 ;
const long long P = 1e9 + 7 ;
long long prefix[ 6 ] [ N] , a[ 6 ] [ N] ;
int main ( ) {
int n, m; cin>> n>> m;
for ( int i= 1 ; i<= n; i++ ) {
cin>> a[ 1 ] [ i] ;
}
for ( int i= 2 ; i<= 5 ; i++ ) {
for ( int j= 1 ; j<= n; j++ ) {
a[ i] [ j] = a[ i- 1 ] [ j] * a[ 1 ] [ j] ;
}
}
for ( int i= 1 ; i<= 5 ; i++ ) {
for ( int j= 1 ; j<= n; j++ ) {
prefix[ i] [ j] = prefix[ i] [ j- 1 ] + a[ i] [ j] ;
}
}
while ( m-- ) {
int l, r, k; cin>> l>> r>> k;
cout<< ( prefix[ k] [ r] - prefix[ k] [ l- 1 ] ) % P<< "\n" ;
}
return 0 ;
}
4.3、小郑的蓝桥平衡串
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
using namespace std;
const int N = 1001 ;
char s[ N] ;
int prefix[ N] ;
int main ( ) {
IOS;
cin>> s+ 1 ;
int n= strlen ( s+ 1 ) ;
for ( int i= 1 ; i<= n; i++ ) {
prefix[ i] = prefix[ i- 1 ] + ( s[ i] == 'L' ? 1 : - 1 ) ;
}
int ans= 0 ;
for ( int i= 1 ; i<= n; i++ ) {
for ( int j= i; j<= n; j++ ) {
if ( ( prefix[ j] - prefix[ i- 1 ] ) == 0 ) {
ans= max ( ans, j- i+ 1 ) ;
}
}
}
cout<< ans<< "\n" ;
return 0 ;
}
4.4、大石头的搬运工
# include <bits/stdc++.h>
using namespace std;
using Pair= pair< int , int > ;
using ll= long long ;
int main ( ) {
int n; cin >> n;
vector< Pair> a ( n) ;
ll sw = 0 ;
for ( auto & [ p, w] : a) {
cin>> w>> p, sw+= w;
}
sort ( a. begin ( ) , a. end ( ) ) ;
ll nw= 0 ;
int x= 0 ;
for ( const auto & [ p, w] : a) {
if ( nw* 2 < sw&& sw<= 2 * ( nw+ w) ) {
x= p; break ;
}
nw+= w;
}
ll ans = 0 ;
for ( const auto & [ p, w] : a) {
ans+= ( ll) w* abs ( p- x) ;
}
cout << ans<< '\n' ;
return 0 ;
}
4.5、最大数组和
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
using namespace std;
using ll = long long ;
int main ( ) {
IOS;
int t; cin>> t;
while ( t-- ) {
int n, k; cin>> n>> k;
vector< ll> prefix ( n+ 1 , 0 ) , a ( n, 0 ) ;
for ( auto & v: a) cin>> v;
sort ( a. begin ( ) , a. end ( ) ) ;
for ( int i= 1 ; i<= n; i++ ) {
prefix[ i] = prefix[ i- 1 ] + a[ i- 1 ] ;
}
ll ans= 0 ;
for ( int i= 0 ; i<= k; i++ ) {
ans= max ( ans, prefix[ n- ( k- i) ] - prefix[ 2 * i] ) ;
}
cout<< ans<< "\n" ;
}
return 0 ;
}
4.6、四元组问题**
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n; cin>> n;
if ( n< 4 ) {
cout<< "No" << "\n" ; return 0 ;
}
int k= INT_MIN, m= INT_MAX;
vector< int > v ( n+ 1 ) , rmin ( n+ 1 , m) ;
for ( int i= 1 ; i<= n; i++ ) cin>> v[ i] ;
for ( int i= n- 1 ; i>= 1 ; i-- ) {
rmin[ i] = min ( rmin[ i+ 1 ] , v[ i+ 1 ] ) ;
}
stack< int > s;
for ( int i= 1 ; i<= n; i++ ) {
if ( v[ i] < k&& v[ i] > rmin[ i] ) {
cout<< "YES" ; return 0 ;
}
while ( ! s. empty ( ) && s. top ( ) < v[ i] ) {
k= max ( k, s. top ( ) ) ;
s. pop ( ) ;
}
s. push ( v[ i] ) ;
}
cout<< "NO" ;
return 0 ;
}
5、差分
5.1、区间更新(一维差分)
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
using namespace std;
const int N = 1e5 + 5 ;
int a[ N] , diff[ N] ;
int main ( ) {
IOS;
int n, m;
while ( cin>> n>> m) {
for ( int i= 1 ; i<= n; i++ ) {
cin>> a[ i] ;
diff[ i] = a[ i] - a[ i- 1 ] ;
}
while ( m-- ) {
int x, y, z; cin>> x>> y>> z;
diff[ x] += z;
diff[ y+ 1 ] -= z;
}
for ( int i= 1 ; i<= n; i++ ) a[ i] = a[ i- 1 ] + diff[ i] ;
for ( int i= 1 ; i<= n; i++ ) cout<< a[ i] << " " ;
cout<< "\n" ;
}
return 0 ;
}
5.2、肖恩的投球游戏加强版
# include <bits/stdc++.h>
using namespace std;
const int N = 2001 ;
long long a[ N] [ N] , d[ N] [ N] ;
void solve ( const int & Case) {
int n, m, q; cin>> n>> m>> q;
for ( int i= 1 ; i<= n; i++ )
for ( int j= 1 ; j<= m; j++ ) {
cin>> a[ i] [ j] ;
d[ i] [ j] = a[ i] [ j] - a[ i] [ j- 1 ] - a[ i- 1 ] [ j] + a[ i- 1 ] [ j- 1 ] ;
}
int x1, y1, x2, y2, c;
while ( q-- ) {
cin>> x1>> y1>> x2>> y2>> c;
d[ x1] [ y1] += c;
d[ x2+ 1 ] [ y1] -= c;
d[ x1] [ y2+ 1 ] -= c;
d[ x2+ 1 ] [ y2+ 1 ] += c;
}
for ( int i= 1 ; i<= n; i++ )
for ( int j= 1 ; j<= m; j++ ) {
a[ i] [ j] = a[ i- 1 ] [ j] + a[ i] [ j- 1 ] - a[ i- 1 ] [ j- 1 ] + d[ i] [ j] ;
}
for ( int i= 1 ; i<= n; i++ )
for ( int j= 1 ; j<= m; j++ )
cout<< a[ i] [ j] << " \n" [ j== m] ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
5.4、泡澡
# include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5 ;
using ll = long long ;
ll a[ N] ;
void solve ( const int & Case) {
int n, w; cin>> n>> w;
while ( n-- ) {
int l, r, p; cin>> l>> r>> p;
a[ l] += p;
a[ r] -= p;
}
for ( int i= 1 ; i< N; i++ ) a[ i] = a[ i] + a[ i- 1 ] ;
for ( int i= 0 ; i< N; i++ ) {
if ( a[ i] > w) {
cout<< "No\n" ; return ;
}
}
cout<< "Yes\n" ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
6、离散化
6.1、离散化举例
# include <bits/stdc++.h>
using namespace std;
vector< int > L;
int getidx ( int x) {
return lower_bound ( L. begin ( ) , L. end ( ) , x) - L. begin ( ) ;
}
const int N = 1e5 + 9 ;
int a[ N] ;
int main ( ) {
int n; cin>> n;
for ( int i= 1 ; i<= n; i++ ) cin>> a[ i] ;
for ( int i= 1 ; i<= n; i++ ) L. push_back ( a[ i] ) ;
sort ( L. begin ( ) , L. end ( ) ) ;
L. erase ( unique ( L. begin ( ) , L. end ( ) ) , L. end ( ) ) ;
cout<< "离散化数组为:" ;
for ( const auto & i: L) cout<< i<< " " ;
int v; cin>> v;
cout<< getidx ( v) << "\n" ;
return 0 ;
}
7、贪心
7.1、谈判(优先队列)
# include <bits/stdc++.h>
using namespace std;
using ll = long long ;
priority_queue< ll, vector< ll> , greater< ll>> pq;
void solve ( const int & Case) {
int n; cin>> n;
for ( int i= 0 ; i< n; i++ ) {
int t; cin>> t;
pq. push ( t) ;
}
ll ans= 0 ;
while ( pq. size ( ) > 1 ) {
ll x= pq. top ( ) ; pq. pop ( ) ;
ll y= pq. top ( ) ; pq. pop ( ) ;
ans+= x+ y;
pq. push ( x+ y) ;
}
cout<< ans<< "\n" ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
7.2、纪念品分组(双指针)
# include <bits/stdc++.h>
using namespace std;
const int N = 300001 ;
int a[ N] ;
void solve ( const int & Case) {
int w, n; cin>> w>> n;
for ( int i= 0 ; i< n; i++ ) cin>> a[ i] ;
sort ( a, a+ n) ;
int l= 0 , r= n- 1 ;
int ans= 0 ;
while ( l<= r) {
ans++ ;
if ( l== r) break ;
if ( a[ l] + a[ r] <= w) {
l++ ; r-- ;
} else {
r-- ;
}
}
cout<< ans<< "\n" ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
7.3、分糖果
# include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9 ;
char s[ N] ;
void solve ( const int & Case) {
int n, x; cin>> n>> x;
cin>> s+ 1 ;
sort ( s+ 1 , s+ 1 + n) ;
if ( s[ 1 ] == s[ n] ) {
for ( int i= 1 ; i<= n/ x+ ( n% x? 1 : 0 ) ; i++ ) cout<< s[ i] ;
} else if ( s[ 1 ] == s[ x] ) {
for ( int i= x; i<= n; i++ ) cout<< s[ i] ;
} else {
cout<< s[ x] ;
}
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
7.4、珠宝的最大交替和
# include <bits/stdc++.h>
using namespace std;
using ll = long long ;
const int N = 1e6 + 5 ;
ll a[ N] ;
void solve ( const int & Case) {
int n; cin>> n;
for ( int i= 0 ; i< n; i++ ) {
int num; cin>> num;
a[ i] = abs ( num) ;
}
ll sum= 0 ;
for ( int i= 0 ; i< n; i++ ) {
sum+= ( ll) pow ( - 1 , i) * a[ i] ;
}
ll mx= INT_MIN, mn= INT_MAX;
for ( int i= 0 ; i< n; i+= 2 ) {
if ( a[ i] < mn) mn= a[ i] ;
}
for ( int i= 1 ; i< n; i+= 2 ) {
if ( a[ i] > mx) mx= a[ i] ;
}
cout<< max ( sum, sum+ 2 * ( mx- mn) ) << "\n" ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
7.5、小蓝的礼物
# include <bits/stdc++.h>
using namespace std;
using ll = long long ;
const int N = 1e6 + 5 ;
ll a[ N] ;
void solve ( const int & Case) {
int n, k; cin>> n>> k;
for ( int i= 0 ; i< n; i++ ) cin>> a[ i] ;
sort ( a, a+ n) ;
ll sum= 0 ;
for ( int i= 0 ; i< n; i++ ) {
if ( sum+ ( a[ i] + 1 ) / 2 > k) {
cout<< i<< "\n" ;
return ;
}
sum+= a[ i] ;
}
cout<< n<< "\n" ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
7.6、四个瓷瓶的神秘游戏**
# include <bits/stdc++.h>
using namespace std;
using ll = long long ;
ll num = 3 ;
ll func ( const ll & a, const ll & b, const ll & c) {
ll A= a+ 2 * b;
ll B= 0 ;
ll C= c- b;
ll t= C/ 3 ;
if ( A== 0 ) return 0 ;
C%= num;
if ( C== 0 || C== 1 ) {
return A+ 3 * t;
} else return A+ 3 * t+ 1 ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
vector< ll> a ( 4 ) ;
for ( auto & x: a) cin>> x;
sort ( a. begin ( ) , a. end ( ) ) ;
cout<< max ( { func ( a[ 0 ] , a[ 1 ] , a[ 2 ] ) , func ( a[ 1 ] , a[ 0 ] , a[ 2 ] ) , func ( a[ 2 ] , a[ 0 ] , a[ 1 ] ) , func ( a[ 3 ] , a[ 0 ] , a[ 1 ] ) } ) << "\n" ;
return 0 ;
}
7.7、鸡哥的购物挑战
# include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1 ;
long long a[ N] ;
void solve ( const int & Case) {
int n; cin>> n;
for ( int i= 0 ; i< n; i++ ) cin>> a[ i] ;
sort ( a, a+ n, greater< int > ( ) ) ;
long long ans= 0 ;
for ( int i= 0 ; i< n; i+= 2 ) {
if ( i== n- 1 ) continue ;
if ( a[ i] + a[ i+ 1 ] > 0 ) ans+= a[ i] + a[ i+ 1 ] ;
else break ;
}
cout<< ans<< "\n" ;
}
int main ( ) {
ios:: sync_with_stdio ( false) ;
cin. tie ( nullptr) ;
cout. tie ( nullptr) ;
int T= 1 ;
for ( int Case= 1 ; Case<= T; Case++ ) solve ( Case) ;
return 0 ;
}
7.8、冒险者公会
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( false) ; cin. tie ( nullptr) ; cout. tie ( nullptr) ;
using namespace std;
using ll = long long ;
int main ( ) {
IOS;
int m, n; cin>> m>> n;
vector< int > a ( m) ;
for ( auto & x: a) cin>> x;
vector< vector< int >> b ( n) ;
int mxk= 0 ;
for ( auto & x: b) {
int k; cin>> k;
x. resize ( k) ;
for ( auto & j: x) cin>> j;
sort ( x. begin ( ) , x. end ( ) ) ;
mxk= max ( mxk, k) ;
}
sort ( a. begin ( ) , a. end ( ) ) ;
int j= n- 1 ;
vector< int > c ( m) ;
ll ans= 0 ;
for ( int T = 0 ; T < mxk; T++ ) {
int mx = 0 ;
for ( int i = 0 ; i < n; i++ ) {
if ( ! b[ i] . empty ( ) ) mx= max ( mx, b[ i] . back ( ) ) ;
}
while ( j > 0 && a[ j - 1 ] >= mx) j-- ;
if ( a[ j] < mx) {
cout << "-1\n" ; return 0 ;
}
for ( int i = 0 ; i < n; i++ ) {
if ( ! b[ i] . empty ( ) ) b[ i] . pop_back ( ) ;
}
c[ j] ++ ;
}
for ( int i = 0 ; i < m; i++ ) {
if ( c[ i] > 1 ) {
if ( i == m - 1 ) {
cout << "-1\n" ; return 0 ;
}
c[ i + 1 ] += c[ i] - 1 , c[ i] = 1 ;
}
if ( c[ i] == 1 ) ans += a[ i] ;
}
cout<< ans<< '\n' ;
return 0 ;
}
7.9、明日方舟大作战
# include <bits/stdc++.h>
# define IOS ios:: sync_with_stdio ( false) ; cin. tie ( nullptr) ; cout. tie ( nullptr) ;
using namespace std;
using ll = long long ;
using Pair = pair< int , int > ;
int main ( ) {
IOS;
int n, m, B; cin>> n>> m>> B;
vector< Pair> a ( n) ;
for ( auto & [ x, y] : a) cin>> x>> y;
int mx = 0 ;
for ( int i = 0 ; i < m; i++ ) {
int x; cin >> x;
mx = max ( mx, x) ;
}
vector< int > f ( B + 1 , INT_MIN) ;
f[ 0 ] = 0 ;
for ( auto & [ x, y] : a) {
if ( y > B) continue ;
auto g = f;
vector< int > ( B + 1 , INT_MIN) . swap ( f) ;
for ( int i = 0 ; i < y; i++ ) {
f[ i] = g[ i] ;
}
for ( int i = y; i <= B; i++ ) {
f[ i] = max ( g[ i] , g[ i - y] + x) ;
}
}
int Mx = * max_element ( f. begin ( ) , f. end ( ) ) ;
if ( Mx == 0 ) cout << "-1\n" ;
else cout << ( mx + Mx - 1 ) / Mx << '\n' ;
return 0 ;
}