【LOJ】#3089. 「BJOI2019」奥术神杖

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();
}
上一篇:Java虚拟机(JVM)你只要看这一篇就够了!


下一篇:loj 3090 「BJOI2019」勘破神机 - 数学