LOJ#3089. 「BJOI2019」奥术神杖

看见乘积就取log,开根号就是除法,很容易发现这就是一道01分数规划。。

然后建出AC自动机直接dp就行,判断条件要设成>0,因为起点的值是1,取完ln后是0

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 2005
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
} int N,M,cur;
int nxt[MAXN][10],pre[MAXN],Ncnt;
char T[MAXN],s[MAXN],ans[MAXN];
db val[MAXN];
vector<db> ed[MAXN];
db dp[2][MAXN];
int from[MAXN][MAXN];
char c[MAXN][MAXN];
void Insert(db v) {
int l = strlen(s + 1);int p = 1;
for(int i = 1 ; i <= l ; ++i) {
if(!nxt[p][s[i] - '0']) nxt[p][s[i] - '0'] = ++Ncnt;
p = nxt[p][s[i] - '0'];
}
ed[p].pb(v);
}
queue<int> Q;
void build_ACAM() {
for(int i = 0 ; i <= 9 ; ++i) nxt[0][i] = 1;
pre[1] = 0;
Q.push(1);
while(!Q.empty()) {
int u = Q.front();Q.pop();
ed[u].insert(ed[u].end(),ed[pre[u]].begin(),ed[pre[u]].end());
for(int i = 0 ; i <= 9 ; ++i) {
int v = nxt[u][i];
if(v) {
pre[v] = nxt[pre[u]][i];
Q.push(v);
}
else nxt[u][i] = nxt[pre[u]][i];
}
}
} bool Calc(db mid) {
memset(val,0,sizeof(val));
for(int i = 1 ; i <= Ncnt ; ++i) {
for(auto v : ed[i]) val[i] += v - mid;
}
for(int i = 1 ; i <= Ncnt ; ++i) dp[0][i] = dp[1][i] = -1e9;
dp[0][1] = 0;cur = 0;
from[0][1] = 0;
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= Ncnt ; ++j) dp[cur ^ 1][j] = -1e9;
for(int j = 1 ; j <= Ncnt ; ++j) {
if(dp[cur][j] <= -1e9) continue;
if(T[i] == '.') {
for(int h = 0 ; h <= 9 ; ++h) {
if(dp[cur ^ 1][nxt[j][h]] < dp[cur][j] + val[nxt[j][h]]) {
dp[cur ^ 1][nxt[j][h]] = dp[cur][j] + val[nxt[j][h]];
from[i][nxt[j][h]] = j;
c[i][nxt[j][h]] = h + '0';
}
}
}
else {
int h = T[i] - '0';
if(dp[cur ^ 1][nxt[j][h]] < dp[cur][j] + val[nxt[j][h]]) {
dp[cur ^ 1][nxt[j][h]] = dp[cur][j] + val[nxt[j][h]];
from[i][nxt[j][h]] = j;
c[i][nxt[j][h]] = h + '0';
}
}
}
cur ^= 1;
}
for(int i = 1 ; i <= Ncnt ; ++i) {
if(dp[cur][i] > 0) return true;
}
return false;
}
void Solve() {
read(N);read(M);
scanf("%s",T + 1);
Ncnt = 1;
int v;
for(int i = 1 ; i <= M ; ++i) {
scanf("%s",s + 1);read(v);
Insert(log(v));
}
build_ACAM();
int cnt = 50;
db l = 0,r = 21;
while(cnt--) {
db mid = (l + r) / 2; if(Calc(mid)) l = mid;
else r = mid;
}
Calc(l);
int st = 0;
for(int i = 1 ; i <= Ncnt ; ++i) {
if(dp[cur][i] > 0) st = i;
}
for(int i = N ; i >= 1 ; --i) {
int pre = from[i][st];
ans[i] = c[i][st];
st = pre;
}
for(int i = 1 ; i <= N ; ++i) {
putchar(ans[i]);
}
enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

【LOJ】#3089. 「BJOI2019」奥术神杖的更多相关文章

  1. Loj #3089. 「BJOI2019」奥术神杖

    Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ...

  2. LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划

    题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...

  3. LOJ 3089: 洛谷 P5319: 「BJOI2019」奥术神杖

    题目传送门:LOJ #3089. 题意简述: 有一个长度为 \(n\) 的母串,其中某些位置已固定,另一些位置可以任意填. 同时给定 \(m\) 个小串,第 \(i\) 个为 \(S_i\),所有位置 ...

  4. Loj #3093. 「BJOI2019」光线

    Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ...

  5. loj 3090 「BJOI2019」勘破神机 - 数学

    题目传送门 传送门 题目大意 设$F_{n}$表示用$1\times 2$的骨牌填$2\times n$的网格的方案数,设$G_{n}$$表示用$1\times 2$的骨牌填$3\times n$的网 ...

  6. LOJ 3094 「BJOI2019」删数——角标偏移的线段树

    题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...

  7. LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域

    题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...

  8. LOJ 3093 「BJOI2019」光线——数学+思路

    题目:https://loj.ac/problem/3093 考虑经过种种反射,最终射下去的光线总和.往下的光线就是这个总和 * a[ i ] . 比如只有两层的话,设射到第二层的光线是 lst ,那 ...

  9. LOJ 3092 「BJOI2019」排兵布阵 ——DP

    题目:https://loj.ac/problem/3092 同一个人的不同城堡之间没有什么联系,只是和<=m.所以对每个城堡的 s 个值排序,做一个 f[ i ][ j ] 表示第 i 个城堡 ...

随机推荐

  1. POJ - 3376 Finding Palindromes manacher+字典树

    题意 给n个字符串,两两拼接,问拼接后的\(n\times n\)个字符串中有多少个回文串. 分析 将所有正串插入字典树中,马拉车跑出所有串哪些前缀和后缀为回文串,记录位置,用反串去字典树中查询,两字 ...

  2. c 判断一个字符是否为空格

    #include <stdio.h> #include <wctype.h> int main () { wchar_t c; ; wchar_t str[] = L" ...

  3. antd-mobile的DatePicker分钟精度半小时

    项目要求,在时间选择上需要精确到分钟,且分钟只能半小时,既0分钟或者是30分钟. 前期引用的时间控件是antd-mobile的DatePicker组件,具体用法可参考:https://mobile.a ...

  4. Q窗口操作函数(窗口最大化,全屏,隐藏最大化最小化按钮)

    //Qt主窗口没有最小化,最大化按钮且最大化显示  int main(int argc, char *argv[]) { QApplication a(argc, argv); TestQtForWi ...

  5. cucumber+testng

    执行顺序 beforeSuite in RunnerForInt beforeSuite in RunnerForString beforeTest in RunnerForInt beforeTes ...

  6. HmacSha1加密-java

    package com.test; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache ...

  7. 小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数

    笔记 8.断路器监控仪表参数讲解和模拟熔断     简介:讲解 断路器监控仪表盘参数和模拟熔断 1.sse  server-send-event推送到前端 资料:https://github.com/ ...

  8. Java中传入一个时间范围,取出该时间范围内所有日期的集合

    直接上代码: import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; impor ...

  9. tomcat中的server.xml元素详解

    附:Tomcat加载顺序 加载类和资源的顺序为: 1./Web-INF/classes 2./Web-INF/lib/*.jar 3.Bootstrap 4.System 5.$CATALINA_HO ...

  10. SPRINGMVC 视图介绍

    SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请 ...