第一周总结
加油站跑路问题
int n, l, p;
struct c {
int a, b;
} ab[ 10010 ] ;
priority_queue< int > q;
bool cmp ( c a, c b)
{
return a. a< b. a;
}
int main ( )
{
cin>> n;
for ( int i= 1 ; i<= n; i++ )
{
cin>> ab[ i] . a>> ab[ i] . b;
}
cin>> l>> p;
int kc= p;
for ( int i= 1 ; i<= n; i++ )
{
ab[ i] . a= l- ab[ i] . a;
}
sort ( ab+ 1 , ab+ 1 + n, cmp) ;
ab[ n+ 1 ] . a= l;
ab[ n+ 1 ] . b= 0 ;
int ans= 0 , hf= 0 ;
for ( int i= 1 ; i<= n+ 1 ; i++ )
{
int d= ab[ i] . a- hf;
while ( d> kc)
{
if ( q. empty ( ) )
{
cout<< "-1" << endl;
return 0 ;
}
ans++ ;
kc+= q. top ( ) ;
q. pop ( ) ;
}
kc-= d;
hf= ab[ i] . a;
q. push ( ab[ i] . b) ;
}
cout<< ans<< endl;
return 0 ;
}
每个单词独立翻转(字符串空格问题)
int main ( )
{
int n;
string s, a;
cin>> n;
getchar ( ) ;
while ( n-- )
{
stack< int > t;
getline ( cin, s) ;
for ( int i= 0 ; i< s. size ( ) ; i++ )
{
if ( s[ i] != ' ' && s[ i] != '\0' )
{
t. push ( s[ i] ) ;
}
else
{
while ( ! t. empty ( ) )
{
a= t. top ( ) ;
cout<< a;
t. pop ( ) ;
}
cout<< " " ;
}
}
while ( ! t. empty ( ) )
{
a= t. top ( ) ;
cout<< a;
t. pop ( ) ;
}
cout<< endl;
}
return 0 ;
}
一组数据的【最长不重复长度】查询
const int N = 1000000 + 5 ;
using namespace std;
int visit[ N] ;
int main ( )
{
int n;
int ans= 0 ;
cin>> n;
queue< int > q;
for ( int i= 0 ; i< n; i++ )
{
int x;
cin>> x;
if ( visit[ x] == 0 )
{
q. push ( x) ;
visit[ x] = 1 ;
}
else
{
ans= max ( ans, ( int ) q. size ( ) ) ;
while ( visit[ x] == 1 )
{
int temp= q. front ( ) ;
q. pop ( ) ;
visit[ temp] = 0 ;
}
q. push ( x) ;
visit[ x] = 1 ;
}
}
ans= max ( ans, ( int ) q. size ( ) ) ;
cout<< ans<< endl;
return 0 ;
}
均分一组数,求【最小步数】
const int N = 110 ;
int a[ N] ;
int main ( )
{
int n;
cin>> n;
int s= 0 ;
for ( int i = 0 ; i < n; i ++ )
{
cin>> a[ i] ;
s+= a[ i] ;
}
s/= n;
int ans= 0 ;
for ( int i = 0 ; i < n; i ++ )
{
if ( a[ i] != s)
{
ans++ ;
a[ i+ 1 ] -= s- a[ i] ;
a[ i] += s- a[ i] ;
}
}
cout<< ans<< endl;
return 0 ;
}
拆分素数
const int N = 100010 ;
int prime[ N] , cnt = 0 ;
bool st[ N] ;
void get_prime ( int x)
{
for ( int i = 2 ; i <= x; i++ )
{
if ( ! st[ i] )
prime[ cnt++ ] = i;
for ( int j = 0 ; prime[ j] <= x / i; j++ )
{
st[ prime[ j] * i] = true ;
if ( i % prime[ j] == 0 )
break ;
}
}
}
int main ( )
{
ios:: sync_with_stdio ( false ) ;
cout. tie ( 0 ) ;
cin. tie ( 0 ) ;
get_prime ( N) ;
int n;
while ( cin >> n)
{
if ( n == 0 )
break ;
int l = 0 , r = 0 , ans = 0 , sum = 0 ;
while ( true )
{
while ( r < cnt && sum < n )
sum += prime[ r++ ] ;
if ( sum < n)
break ;
if ( sum == n )
ans ++ ;
sum -= prime[ l++ ] ;
}
cout << ans << endl;
}
return 0 ;
}
遍历图BFS
char a[ 23 ] [ 23 ] ;
int m, n, ans= 1 ;
int dx[ ] = { 1 , - 1 , 0 , 0 } ;
int dy[ ] = { 0 , 0 , - 1 , 1 } ;
void bfs ( int x, int y)
{
queue< pair< int , int > > q;
q. push ( make_pair ( x, y) ) ;
a[ x] [ y] = '#' ;
while ( ! q. empty ( ) )
{
pair< int , int > no= q. front ( ) ;
q. pop ( ) ;
int x= no. first, y= no. second;
for ( int i= 0 ; i< 4 ; i++ )
{
int tx, ty;
tx= x+ dx[ i] ;
ty= y+ dy[ i] ;
if ( tx>= 0 && tx< n && ty>= 0 && ty< m && a[ tx] [ ty] == '.' )
{
a[ tx] [ ty] = '#' ;
q. push ( make_pair ( tx, ty) ) ;
ans++ ;
}
}
}
cout<< ans<< endl;
}
int main ( )
{
int x, y;
cin>> m>> n;
for ( int i= 0 ; i< n; i++ )
for ( int j= 0 ; j< m; j++ )
{
cin>> a[ i] [ j] ;
if ( a[ i] [ j] == '@' )
x= i, y= j;
}
bfs ( x, y) ;
return 0 ;
}
遍历图DFS
char a[ 21 ] [ 21 ] = { '#' } ;
int dx[ 4 ] = { - 1 , 0 , 1 , 0 } ;
int dy[ 4 ] = { 0 , 1 , 0 , - 1 } ;
int m, n, x, y, ans= 1 ;
void dfs ( int x, int y)
{
a[ x] [ y] = '#' ;
for ( int i= 0 ; i< 4 ; i++ )
{
int tx, ty;
tx= x+ dx[ i] ;
ty= y+ dy[ i] ;
if ( a[ tx] [ ty] == '.' && tx>= 0 && ty>= 0 && tx< n && ty< m)
{
ans++ ;
dfs ( tx, ty) ;
}
}
}
int main ( )
{
cin>> m>> n;
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< m; j++ )
{
cin>> a[ i] [ j] ;
if ( a[ i] [ j] == '@' )
{
x= i, y= j;
}
}
}
dfs ( x, y) ;
cout<< ans;
return 0 ;
}
同时二分两个答案
int main ( )
{
long long n, a, b, mid, l, r;
cin>> n;
int flag= 0 ;
long long t= sqrt ( ( double ) n* 2 ) ;
for ( long long i= 1 ; i< t; i++ )
{
l= i;
r= t;
a= l* ( l+ 1 ) / 2 ;
mid= l+ r >> 1 ;
while ( l<= r)
{
b= mid* ( mid+ 1 ) / 2 ;
if ( a+ b< n)
{
l= mid+ 1 ;
mid= l+ r >> 1 ;
}
else if ( a+ b> n)
{
r= mid- 1 ;
mid= l+ r >> 1 ;
}
else
{
flag= 1 ;
break ;
}
}
if ( flag== 1 )
break ;
}
if ( flag== 1 )
cout<< "YES" << endl;
else
cout<< "NO" << endl;
return 0 ;
}
浮点单答案二分
int n, y;
double mid;
double anss ( )
{
double t= 0 ;
for ( int i= 1 ; i<= n; i++ )
t+= ( y/ ( pow ( mid+ 1 , i) ) ) ;
return t;
}
int main ( )
{
int x;
double l= 0 , r= 11 ;
double ans= 0x3f3f3f ;
cin>> x>> y>> n;
while ( r- l> 1e-6 )
{
mid= ( l+ r) / 2 ;
ans= anss ( ) ;
if ( ans> x) l= mid;
else r= mid;
}
printf ( "%.1f" , l* 100 ) ;
return 0 ;
}