A.Crazy Computer(Codeforces 716A)
思路
设置一个计数变量。遍历数组,当
b−c≤a
时计数变量自增
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=a2i+1−aii
这个式子说明只要能满足 aimodi=0 和 a2i+1modi=0 的数列 ai 都能用来构造答案数列。我们可以令
ai=i×(i−1)
于是有
ansi=(i+1)2i−i+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;
}
(其它题目略)