牛客-伤害计算-排列计算

伤害计算题目:

给你一个由“+”号分割的字符串,每一段可能是纯整数,那么直接相加即可,也可能是由一个整数a加“d”加一个整数b组成,表示投掷a次有b面的筛子,结果加上它的期望

思路:

直接记录每个“+”号位置,然后将字符串分割出来,按题意模拟即可,期望公式就是(b+1)b/2a/b;

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const int maxn=2e5+10;
 
 
 
vector <int> ve;
 
double sol(string s){
    int flag=-1;
    for(int i=0;i<s.length();i++){
        if(s[i]=='d') flag=i;
    }
    if(flag==-1) return stod(s);
    string s1(s.begin(),s.begin()+flag);
    string s2(s.begin()+1+flag,s.end());
    double d1=stod(s1),d2=stod(s2);
    return (d2+1)/2*d1;
}
 
int a[maxn];
int main(){
    string s;
    cin >> s;
    int cnt=0;
    for(int i=0;i<s.length();i++){
        if(s[i]=='+') a[++cnt]=i;
    }
    double ans=0;
    if(cnt==0){
        ans+=sol(s);
    }
    else{
    string s2(s.begin(),s.begin()+a[1]);
    string s3(s.begin()+a[cnt]+1,s.end());
    ans+=sol(s2);
    ans+=sol(s3);
    for(int i=1;i<cnt;i++){
        string temp(s.begin()+a[i]+1,s.begin()+a[i+1]);
        //cout <<  temp << endl;
        ans+=sol(temp);
    }
    }
    int temp=int(ans);
    if(ans-temp==0) cout << temp << endl;
    else printf("%.1lf\n",ans);
    return 0;
}

排列计算题目:

给你一个n,以及m次查询,每次查询给出 l,r,输出m次查询的最大区间和,你自己用1-n来排列数组

思路

用差分数组来记录每一个位置查询次数,然后查询次数越多的位置排列的数越大

#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int d[N];
int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++){
        int l, r;
        scanf("%d%d", &l, &r);
        d[l] ++ ,d[r + 1]--;
    }
    for(int i = 1; i <= n; i++){
        d[i] += d[i - 1];
    }
    sort(d + 1, d + n + 1);
    ll ans = 0;
    for(int i = 1; i <= n; i++)
        ans += d[i] * i;
    printf("%lld\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值