【CF1415C】Bouncing Ball

链接


传送门

code

先dp, 再枚举。时间复杂度O(n)

#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>

using namespace std;
inline int read(){
    
    
    int x = 0, op = 1; char ch = getchar();
    while (!isdigit(ch)){
    
    
        if (ch == '-') op = -1; ch = getchar();}
    while (isdigit(ch)){
    
    
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * op;
}
inline void write(int x){
    
    
    if (x < 0) putchar('-'), x = - x;
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
}

const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[N], dp[N];
void solve(){
    
    
    int n = read(), p = read(), k = read();
    memset(a, 0, sizeof a);
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; ++i) {
    
    
        scanf("%1d", a + i);
    }
    int x = read(), y = read();
    for (int i = n; i >= p; --i) {
    
    
        if (i + k > n){
    
    
            dp[i] = (1 - a[i])*x;
        }else{
    
    
            dp[i] = dp[i + k] + (1 - a[i])*x;
        }
    }
    int res = INF;
    for (int i = p; i <= n; ++i) {
    
    
        res = min(res, dp[i] + (i - p)*y);
    }
    write(res), putchar('\n');
}

int main(){
    
    
    int cases = read();
    while (cases--){
    
    
        solve();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_50070650/article/details/112908315