A - Combination Lock 开锁最小的步骤数
char a[1010];
char c[1010];
int main()
{
int n;
while ( ~scanf("%d", &n) )
{
scanf("%s%s", a, c);
int ans = 0;
for( int i = 0; i < n; ++i )
{
ans += abs( a[i] - c[i] ) > 5 ? 10 - abs( a[i] - c[i] ) : abs( a[i] - c[i] );
//printf("ans: %d\n", ans);
}
printf("%d\n", ans);
}
return 0;
}
B - School Marks 给出n门成绩中的k门,问在每门分数不超过p,中位数不低于y,总分不超过x的情况下,剩下n-k门的成绩可能是多少。
可以确定有一种情况是无论如何都不符合的,即 小于中位数的成绩已经有n/2门以上了,可以直接判掉,剩下的情况下,因为总分不能超过x,即总分要尽量小,那么选中位数那个最低分数y或1来当剩下k门分数总是没错的。所以只需要尽量使成绩为1的尽可能多(<=n/2),剩下的都是y,最后判断是不是总分超过x即可
const int N = 1010;
int a[N];
vector <int> v;
int main()
{
int n, p, k, x, y;
while( ~scanf("%d%d%d%d%d", &n, &k, &p, &x, &y) ) {
int sum = 0, small = 0;
v.clear();
for( int i = 1; i <= k; ++i ) {
scanf("%d", &a[i]);
sum += a[i];
small += ( a[i] < y );
}
if( small >= (n+1) / 2 ) {
puts("-1");
continue;
}
int tmp = 0;
while ( small + tmp < n / 2 && k + tmp < n ) {
v.push_back( 1 );
++sum;
++tmp;
}
while( k + tmp < n ) {
v.push_back( y );
sum += y;
++tmp;
}
if( sum > x )
puts("-1");
else
{
for( int i = 0; i < v.size(); ++i )
printf("%d ", v[i]);
puts("");
}
}
return 0;
}
求起点走到终点的时候终点为’X‘是否可能,并输出“YES”或“NO”,其中每次踩到(坚固的冰)’.‘上面时,坚固的并会变成易破碎的“X”。
简单的bfs
const int N = 510;
char a[N][N];
int n, m;
int sx, sy, ex, ey;
int dir[4][2] = {
1, 0, -1, 0, 0, 1, 0, -1
};
queue < pair< int, int > > q;
bool bfs( )
{
while( !q.empty() ) q.pop();
q.push( make_pair( sx, sy ) );
while( !q.empty() )
{
auto now = q.front();
q.pop();
for( int i = 0; i < 4; ++i )
{
int xx = now.first + dir[i][0];
int yy = now.second + dir[i][1];
if( xx <= 0 || xx > n || yy <= 0 || yy > m )
continue;
if( xx == ex && yy == ey )
{
if( a[xx][yy] == 'X' )
return 1;
else
{
a[xx][yy] = 'X';
q.push( make_pair( xx, yy ) );
}
}
else
{
if( a[xx][yy] == '.' )
{
a[xx][yy] = 'X';
q.push( make_pair( xx, yy ) );
}
else
continue;
}
}
}
return 0;
}
int main()
{
while( ~scanf("%d%d", &n, &m) ) {
memset( a, 'X', sizeof( a ) );
for( int i = 1; i <= n; ++i ) {
scanf("%s", a[i]+1);
}
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
if( bfs( ) )
cout << "YES" << endl;
else
cout << "NO" <<endl;
}
return 0;
}