【解题报告】Codeforces Round #372 (Div. 2)

题目链接


A.Crazy Computer(Codeforces 716A)

思路

设置一个计数变量。遍历数组,当 bca 时计数变量自增 1 ,否则就令其重新为 1 。最后计数变量就是答案。

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int n, c, ans, a[maxn];

int main() {
    scanf("%d%d", &n, &c);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    ans = 1;
    for(int i = 2; i <= n; i++) {
        if(a[i] - a[i-1] <= c) {
            ans++;
        }
        else {
            ans = 1;
        }
    }
    printf("%d\n", ans);
    return 0;
}

B.Complete the Word(Codeforces 716B)

思路

枚举每个长度为 26 的区间,若区间满足条件就记录区间 [l,r] 并生成题目要求的字符串,否则输出 1 。生成字符串的方法是,统计 [l,r] 区间中缺少哪个字符,将其装进队列。然后遍历区间,遇到问号就把队列里的字符往上填。区间外的问号随便填即可。

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 5e4 + 10;
bool vis[300];
char s[maxn];
int n, type, head, tail, cnt[300];
queue <int> q;

// 生成字符串的函数
// 其中[l, r]为能修改成目标子串的区间
void solve(int l, int r) {
    for(int i = l; i <= r; i++) {
        vis[s[i]] = true;
    }
    for(int i = 'A'; i <= 'Z'; i++) {
        if(vis[i] == false) {
            q.push(i);
        }
    }
    for(int i = l; i <= r; i++) {
        if(s[i] == '?') {
            if(!q.empty()) {
                s[i] = q.front();
                q.pop();
            }
            else {
                s[i] = 'B';
            }
        }
    }
    for(int i = 0; i < l; i++) {
        if(s[i] == '?') {
            s[i] = 'A';
        }
    }
    for(int i = r + 1; i < n; i++) {
        if(s[i] == '?') {
            s[i] = 'C';
        }
    }
    printf("%s\n", s);
}

int main() {
    scanf("%s", s);
    n = strlen(s);
    // 特判字符串长度较短的情况
    if(n < 26) {
        puts("-1");
        return 0;
    }
    // 先统计[0, 25]这个区间
    for(int i = 0; i < 26; i++) {
        if(cnt[s[i]] == 0 && s[i] != '?') {
            type++;
        }
        cnt[s[i]]++;
    }
    if(type + cnt['?'] >= 26) {
        solve(0, 25);
        return 0;
    }
    head = 0;
    tail = 25;
    // 枚举每个长度为26的区间
    while(true) {
        if(--cnt[s[head]] == 0 && s[head] != '?') {
            type--;
        }
        head++;
        if(++tail >= n) {
            break;
        }
        if(++cnt[s[tail]] == 1 && s[tail] != '?') {
            type++;
        }
        if(type + cnt['?'] >= 26) {
            solve(head, tail);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

C.Plus and Square Root(Codeforces 716C)

思路

理解题意后便能梳理出两个关系:

ai+ansi×i=a2i+1

aimodi=0

因为第 i 个答案为 ansi ,因此把第一个式子变形

ansi=a2i+1aii

这个式子说明只要能满足 aimodi=0 a2i+1modi=0 的数列 ai 都能用来构造答案数列。我们可以令

ai=i×(i1)

于是有

ansi=(i+1)2ii+1

这样构造出来的答案序列,除了 ans1 不能满足条件外均能满足条件。所以要特判。

代码

#include <bits/stdc++.h>
using namespace std;

int n;

int main() {
    scanf("%d", &n);
    puts("2");
    for(long long i = 2; i <= n; i++) {
        printf("%I64d\n", (i + 1) * (i + 1) * i - i + 1);
    }
    return 0;
}

(其它题目略)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值