[Offer收割]编程练习赛30

提取用户名

题解

简单的模拟题,大家懂的

代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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 vec;
typedef vector 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;
}

你可能感兴趣的:(hihocoder)