蓝桥杯百校真题大联赛第5期(三)

A组

异或数列

我们只需要考虑第一个最高位出现1为奇数的情况,假设最高位开始都是偶数,那么对手可以把这些数都异或掉,如果这个数只出现一次,那么先手必赢,如果出现奇数次,假设总数偶数,那么对手可以一直不使用,直到最后再用,那么对手必胜,因此,此时总数必须是奇数。同时还需要考虑如果最后结果都是0,那么无论什么顺序都和局

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int sum[32];
int res, n;

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        cin >> n;
        res = 0;
        memset(sum, 0, sizeof sum);
        for (int i = 0; i < n; i ++ )
        {
            int x;
            cin >> x;
            res ^= x;
            
            for (int i = 0; i <= 20; i ++ )
                if (x >> i & 1)
                    sum[i] ++ ;
        }
        
        if (!res)
        {
            cout << 0 << endl;
            continue;
        }
        
        int k = 0;
        for (int i = 20; i; i -- )
            if (sum[i] % 2)
            {
                k = i;
                break;
            }
        
        if (sum[k]==1 || n % 2) cout << 1 << endl;
        else puts("-1");
    }
    return 0;
}

括号序列

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 5010, MOD = 1e9 + 7;
typedef long long LL;

int n;
char s[N];
LL f[N][N];

LL work()
{
    memset(f, 0, sizeof f);
    f[0][0] = 1; // 不选择字符时,左括号比右括号多0, 
    // 这个时候不添加左括号也是一种方案方案数为1
    
    for (int i = 1; i <= n; i ++ )
    {
        if (s[i] == '(')
        {
            for (int j = 1; j <= n; j ++ )
                f[i][j] = f[i - 1][j - 1];
        }
        else
        {
            f[i][0] = (f[i - 1][1] + f[i - 1][0]) % MOD;
            for (int j = 1; j <= n; j ++ )
            {
                f[i][j] = (f[i - 1][j + 1] + f[i][j - 1]) % MOD;
            }
        }
    }
    
    for (int i = 0; i <= n; i ++ )
        if (f[n][i])
            return f[n][i];
    return -1;
}

int main()
{
    scanf("%s", s + 1);
    n = strlen(s + 1);
    
    LL l = work();
    reverse(s + 1, s + n + 1);
    for (int i = 1; i <= n; i ++ )
    {
        if (s[i] == '(') s[i] = ')';
        else s[i] = '(';
    }
    
    LL r = work();
    printf("%lld", (l * r) % MOD);
    return 0;
}

B组

螺旋折线

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

int main()
{
    int x, y;
    cin >> x >> y;
    
    int n;
    if (abs(x) <= y)
    {
        n = y;
        cout << (LL)(2 * n - 1) * 2 * n + x - (-n) << endl;
    }
    else if (abs(y) <= x)
    {
        n = x;
        cout << (LL)(2 * n) * (2 * n) + n - y << endl;
    }
    else if (abs(x) <= abs(y) + 1 && y < 0)
    {
        n = abs(y);
        cout << (LL)(2 * n) * (2 * n + 1) + n - x << endl;
    }
    else
    {
        n = abs(x);
        cout << (LL)(2 * n - 1) * (2 * n - 1) + y - (-n + 1) << endl;
    }
    return 0;
}

日志统计

#include <iostream>
#include <cstring>
#include <algorithm>

#define x first
#define y second

using namespace std;
const int N = 100010;

typedef pair<int, int> PII;
PII logs[N];
int cnt[N];
bool st[N];
int n, d, k;

int main()
{
    scanf("%d%d%d", &n, &d, &k);
    
    for (int i = 0; i < n; i ++ )
    {
        scanf("%d%d", &logs[i].x, &logs[i].y);
    }
    
    sort(logs, logs + n);
    
    for (int i = 0, j = 0; i < n; i ++ )
    {
        int id = logs[i].y;
        cnt[id] ++ ;
        
         while (logs[i].x - logs[j].x >= d)
         {
            cnt[logs[j].y] -- ;
            j ++ ;
         }
         if (cnt[id] >= k) st[id] = true;
    }
    
    for (int i = 0; i < N; i ++ )
        if (st[i])
            printf("%d\n", i);
    return 0;
}

全球变暖

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 1010;
char g[N][N];
bool st[N][N];
int n, sum, cnt;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

void dfs(int x, int y)
{
    st[x][y] = true;
    sum ++ ;
    bool has_land = false;
    for (int i = 0; i < 4; i ++ )
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= n || b < 0 || b >= n) continue;
        if (st[a][b]) continue;
        if (g[a][b] == '.')
        {
            has_land = true;
            continue;
        }
        
        dfs(a, b);
    }
    
    if (has_land) cnt ++ ;
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
    
    int res = 0;
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n; j ++ )
            if (g[i][j] == '#' && !st[i][j])
            {
                sum = 0, cnt = 0;
                dfs(i, j);
                if (sum == cnt) res ++ ;
            }
    cout << res << endl;
    return 0;
}

C组

外卖店的优先级

#include <iostream>
#include <cstring>
#include <algorithm>

#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;

const int N = 100010;

int n, m, t;
PII menu[N];
int last[N], score[N];
bool st[N];

int main()
{
    cin >> n >> m >> t;
    
    for (int i = 0; i < m; i ++ )
    {
        scanf("%d%d", &menu[i].x, &menu[i].y);
    }
    
    sort(menu, menu + m);
    
    for (int i = 0; i < m; i ++ )
    {
        int x = menu[i].x, y = menu[i].y;
        
        if (x - last[y] > 1)
        {
            score[y] -= x - last[y] - 1;
            if (score[y] < 0) score[y] = 0;
            if (score[y] <= 3) st[y] = false;
        }
        
        score[y] += 2;
        if (score[y] > 5) st[y] = true;
        last[y] = x;
    }
    
    int cnt = 0;
    
    for (int i = 1; i <= n; i ++ )
    {
        if (last[i] != t)
        {
            score[i] -= t - last[i];
            if (score[i] <= 3) st[i] = false;
        }
        
        if (st[i])
            cnt ++ ;
    }
    
    cout << cnt << endl;
    
    return 0;
}

人物相关性分析

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int Alice[1000000];
int Bob[1000000];
int k;
bool check(char a) {
    if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z')) return false;
    return true;
}
int main()
{
    // 请在此输入您的代码
    cin >> k;
    string str;
    getchar();
    getline(cin,str);
    int acnt = 0, bcnt = 0;
    ll ans = 0;
    int len = str.size();
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == 'A') {
            if (i == 0 || check(str[i - 1])) {
                if (i + 5 == len || (check(str[i + 5]) && i + 5 < len)) {
                    if (str[i + 1] == 'l' && str[i + 2] == 'i' && str[i + 3] == 'c' && str[i + 4] == 'e') {
                        Alice[acnt++] = i;
                        i += 4;
                    }
                }
            }
        }
        else if (str[i] == 'B') {
            if (i == 0 || check(str[i - 1])) {
                if (i + 3 == len || (check(str[i + 3]) && i + 3 < len)) {
                    if (str[i + 1] == 'o' && str[i + 2] == 'b') {
                        Bob[bcnt++] = i;
                        i += 2;
                    }
                }
            }
        }
    }
    for (int i = 0; i < acnt; i++) {
        int l = 0, r = 0;
        int start = upper_bound(Bob, Bob + bcnt, Alice[i] - k - 4) - Bob;
        int End = upper_bound(Bob, Bob + bcnt, Alice[i] + 4 + k + 1) - Bob;
        if (start < bcnt && Bob[start] <= Alice[i] + 4 + k + 1) {
            ans += End - start;
        }
    }
    cout << ans;
    return 0;
}

最大降雨量

#include <iostream>
using namespace std;
int main()
{
  cout << 49 - 12 << endl;
  return 0;
}

等差数列

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 100010;
int n;
int a[N];

int gcd(int a, int b)  // 欧几里得算法
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    cin >> n;
    
    for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    
    sort(a, a + n);
    
    int d = a[1] - a[0];
    for (int i = 1; i < n - 1; i ++ )
    {
        d = gcd(d, a[i + 1] - a[i]);
    }
    
    if (!d) cout << n << endl;
    else
        printf("%d\n", (a[n - 1] - a[0]) / d + 1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值