Codeforces 1321

3 篇文章 0 订阅
1 篇文章 0 订阅

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值