《看了受制了》第十天,5道题,合计45道题
2023年9月5日
牛客周赛10 游游的最长稳定数组
题目理解
就是模拟,只要符合要求长度加一,不符合就归1然后重新计数
代码实现
#include<iostream>
#include<cmath>
using namespace std;
const int N = 100000 + 10;
int a[N], n;
int main()
{
    cin >> n;
    
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    int tmp = 1;
    int res = 0;
    for(int i = 1; i < n ; i++)
    {
        if(abs(a[i] - a[i + 1]) > 1)
        {
            res = max(res, tmp);
            tmp = 1;
        }else
            tmp++;
        
    }
    res = max(res, tmp);
    
    cout << res;
    return 0;
}
ACWING5039 构造数组
题目理解
这个题目的说明稍微有一点点绕,是让没出现的最小正整数尽可能大。
然后我们只需要先将我们的a数组排个序,然后如果我们小于就+1等于就不变!然后就顺下来最后就是答案啦。
代码实现
#include<set>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int n, a[N];
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a + 1, a + 1 + n);
    int k = 0;
    for(int i = 1; i <= n; i++)
        if(k < a[i])
            k++;
        else
            k = a[i];
    cout << k + 1;    
    return 0;    
}
ACWING5038 函数
题目理解
说啥做啥,就是个函数。
代码实现
#include<iostream>
using namespace std;
int main()
{
    int x;
    cin >> x;
    cout << 2 * x * x - 2 * x + 1;
    return 0;
}
ACWING5042 病毒
题目理解
这个题就是个多起点BFS,我们只需要搬模板,最后我们只需要进行输出最远距离的格子即可。
代码实现
#include<iostream>
#include<queue>
using namespace std;
const int N = 2010;
int dist[N][N];
int n, m, k;
queue<pair<int, int>> q;
void bfs()
{
    int a[4] = {1, -1, 0, 0}, b[4] = {0, 0, 1, -1};
    auto p = q.front();
    while(!q.empty())
    {
        auto p = q.front();
        for(int i = 0; i < 4; i++)
        {
            int x = p.first + a[i], y = p.second + b[i];
            if(x >= 1 && x <= n && y >= 1 && y <= m && dist[x][y] == 0)
            {
                dist[x][y] = dist[p.first][p.second] + 1;
                q.push({x, y});
            }
        }
        q.pop();
    }
}
int main()
{
    cin >> n >> m;
    cin >> k;
    for(int i = 1; i <= k ;i++)
    {
        int a, b;
        cin >> a >> b;
        q.push({a, b});
        dist[a][b] = 1;
    }
    bfs();
    int x, y, res = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            if(dist[i][j] > res)
            {
                res = dist[i][j];
                x = i, y = j;
            }
        }
    cout << x << " " << y;
    return 0;
}
ACWING5041 函数
题目理解
说啥做啥。
代码实现
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    int n;
    cin >> n;
    if(n % 2)
        cout << 0;
    else
        cout << (1 << n / 2);
    return 0;
}

                
            
        
浙公网安备 33010602011771号