B
思维题
题解:对于某个满足题意的序列来说 其元素的值与其序号的差值x相等
如样例1中的 a2 = 7, a4 = 9, a5 = 10; x值均为5
注意若通过数组储存 下标(即x)会出现负值 所以需要将输入均加上一个较大的值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
int n;
int b[maxn], a[maxn];
ll sm[1000010];
int vis[maxn];
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%d",&b[i]);
a[i] = b[i] + 200000;
}
ll ans = 0;
for(int i = 1; i <= n; i++){
sm[a[i]-i] += b[i];
}
for(int i = 1; i < 1000010; i++)
ans = max(ans, sm[i]);
printf("%lld\n",ans);
//system("pause");
return 0;
}
C
暴力枚举
题解:只需要从最大字母开始枚举即可,时间复杂度为O(26n)
#include<bits/stdc++.h>
using namespace std;
/*
从最大字母开始暴力枚举
将删除位置的值改为“#”
*/
typedef long long ll;
const int maxn = 2e5+10;
int n;
char s[105];
int vis[maxn];
int main(){
scanf("%d",&n);
scanf("%s",s);
//cout <<s <<endl;
int ans = 0;
for(int i = 25; i > 0; i--){
for(int j = 0; j < n; j++){
if(s[j] == 'a'+i){//找到最大字母
//cout << s[j] <<endl;
int l = j-1, r = j+1;
//注意!!若相邻为最大元素 则遍历时继续循环 因为若相同序列的最左或最右满足条件 则该序列全部都符合条件
while((s[l]=='#'||s[l]=='a'+i)&&l>=0) l--;
while((s[r]=='#'||s[r]=='a'+i)&&r<n) r++;
//cout << s[l] << ' ' << s[r] << endl;
if(s[l]=='a'+i-1||s[r]=='a'+i-1){//相邻元素
ans++;
s[j] = '#';
}
}
}
}
printf("%d\n",ans);
//system("pause");
return 0;
}