[Offer收割]编程练习赛30

提取用户名

题解

简单的模拟题,大家懂的

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <vector>
#include <map>
#include <unordered_map>

#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int MAXN = 8e2 + 5;
const int INF = 0x3f3f3f3f;
char S[MAXN];

int main(){
    while(gets(S)){
        int len = strlen(S);
        bool t = 0, b_out = false, b_write = false;
        for(int i = 0;i < len;i ++){
            if(!b_out && S[i] == '@') b_out = true;
            else{
                if(b_out && isalpha(S[i])){
                    if(t && !b_write) printf(" ");
                    b_write = true;
                    printf("%c", S[i]);
                    t = true;
                }
                else{
                    if(b_out) i --;
                    b_write = false;
                    b_out = false;
                }
            }
        }
        printf("\n");
    }
    return 0;
}

股票价格II

题解

为了让自己卖出去的利润最多,可以从后面往前面保存从 i 开始到最后的最大值,然后一一处理就可以了

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <vector>
#include <map>
#include <unordered_map>

#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
int A[MAXN], N;
int Mx[MAXN];
int main() {
    while(~scanf("%d", &N)) {
        for(int i = 0; i < N; i ++) {
            scanf("%d", &A[i]);
        }
        memset(Mx, 0, sizeof(Mx));
        for(int i = N - 1; i >= 0; i --) {
            Mx[i] = max(Mx[i + 1], A[i]);
        }
        int sum = 0;
        int t = 0;
        for(int i = 0;i < N;i ++){
            if(A[i] <= Mx[i + 1]){
                sum += Mx[i + 1] - A[i];
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}

小Hi的生成树计数

题解

推出公式an=3an1an2,所以用矩阵快速幂就可以了,列出变换矩阵

310100010

然后直接求就可以了,由于中间有减,所以可能为负所以最终答案要加上mod再取模

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <vector>
#include <map>
#include <unordered_map>

#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

typedef long long LL;
typedef vector<LL> vec;
typedef vector<vec> mat;
LL n;
mat mul(mat &A, mat &B){
    mat C(A.size(), vec(B[0].size()));
    for(int i = 0;i < A.size();i ++){
        for(int k = 0;k < B.size();k ++){
            for(int j = 0;j < B[0].size();j ++){
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
            }
        }
    }
    return C;
}

mat pow(mat A, LL n){
    mat B(A.size(), vec(A.size()));
    for(int i = 0;i < A.size();i ++) B[i][i] = 1;
    while(n){
        if(n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}


int main() {
    //FIN;
    //FOUT;
    while(~scanf("%lld", &n)) {
        mat C(3, vec(3));
        C[0][0] = 3, C[0][1] = 1, C[0][2] = 0;
        C[1][0] = -1, C[1][1] = 0, C[1][2] = 1;
        C[2][0] = 0, C[2][1] = 0, C[2][2] = 0;

        mat A(3, vec(3));
        A[0][0] = 3, A[0][1] = 1, A[0][2] = 1;
        A[1][0] = 0, A[1][1] = 0, A[1][2] = 0;
        A[2][0] = 0, A[2][1] = 0, A[2][2] = 0;
        mat B = pow(C, n);
        A = mul(A, B);
        printf("%lld\n",(A[0][2] + mod) % mod);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值