社区讨论

WA 1~9 求调

P11188「KDOI-10」商店砍价参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@m29ndwvg
此快照首次捕获于
2024/10/15 07:30
去年
此快照最后确认于
2025/11/04 17:10
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 6e5+10;
const int INF = 1e18;

int c ,t , n, a[11], cnt[11],tot[11],pos[N][11],last[11]; 
char s[N];

void init(){
    memset(cnt,0,sizeof(cnt));
    memset(pos,0,sizeof(pos));
    memset(last,0,sizeof(last));
}

void build(){ //子序列自动机
    for(int i = n; i >= 1; i--){
        last[s[i] - '0'] = i;
        for(int j = 1; j <= 9; j++) pos[i][j] = last[j];
    }
}

int check(int x){ //查询数字是否在原数字内
    string l = to_string(x);
    if(l.size() > n) return 0;
    for(int i = 0,p = 1; i < l.size(); i++){
        int c = l[i] - '0';
        if(pos[p][c] == 0) return 0;
        p = pos[p][c];
    }
    return 1;
}

signed main(){
    scanf("%lld%lld",&c,&t);
    while(t--){
        init();
        scanf("%s",s+1);
        n = strlen(s+1);
        build();
        for(int i = 1; i <= 9; i++) scanf("%lld",&a[i]);
        
        for(int i = 1; i <= n; i++) cnt[s[i] - '0']++;
        int cost = 0,res = INF;
        for(int i = 1; i <= 9; i++) cost += a[i] * cnt[i]; //记录全部删除的代价

        for(int i = 0; i <= 1000000; i++){ //枚举删除完剩下来的数
            memset(tot,0,sizeof(tot));
            for(int tmp = i; tmp; tmp /= 10) tot[tmp % 10] ++; 
            if(check(i)){
                int ans = cost;
                for(int j = 1; j <= 9; j++) ans -= tot[j] * a[j]; //把这几个数加回去
                res = min(res,ans + i);
            }
        }
        printf("%lld\n",res);
    }
}

回复

3 条回复,欢迎继续交流。

正在加载回复...