【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();
}
【LOJ】#3089. 「BJOI2019」奥术神杖的更多相关文章
- Loj #3089. 「BJOI2019」奥术神杖
Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ...
- LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划
题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...
- LOJ 3089: 洛谷 P5319: 「BJOI2019」奥术神杖
题目传送门:LOJ #3089. 题意简述: 有一个长度为 \(n\) 的母串,其中某些位置已固定,另一些位置可以任意填. 同时给定 \(m\) 个小串,第 \(i\) 个为 \(S_i\),所有位置 ...
- Loj #3093. 「BJOI2019」光线
Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ...
- loj 3090 「BJOI2019」勘破神机 - 数学
题目传送门 传送门 题目大意 设$F_{n}$表示用$1\times 2$的骨牌填$2\times n$的网格的方案数,设$G_{n}$$表示用$1\times 2$的骨牌填$3\times n$的网 ...
- LOJ 3094 「BJOI2019」删数——角标偏移的线段树
题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...
- LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域
题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...
- LOJ 3093 「BJOI2019」光线——数学+思路
题目:https://loj.ac/problem/3093 考虑经过种种反射,最终射下去的光线总和.往下的光线就是这个总和 * a[ i ] . 比如只有两层的话,设射到第二层的光线是 lst ,那 ...
- LOJ 3092 「BJOI2019」排兵布阵 ——DP
题目:https://loj.ac/problem/3092 同一个人的不同城堡之间没有什么联系,只是和<=m.所以对每个城堡的 s 个值排序,做一个 f[ i ][ j ] 表示第 i 个城堡 ...
随机推荐
- deep sort
目录 1. 准备代码与数据 deep_sort开源代码 克隆到本地服务器 git clone https://github.com/nwojke/deep_sort.git 下载MOT16数据集( ...
- 使用fui(Find Unused Imports)扫描工程中不用的类
为了给APP提速,需要定期清理不用的类 fui(Find Unused Imports)是开源项目能很好的分析出不再使用的类,准确率非常高,唯一的问题是它处理不了动态库和静态库里提供的类,也处理不了C ...
- 子类中执行父类的方法(引出super()与mro列表)
1. 我们先想一下在python中如果子类方法中想执行父类的方法,有什么方式?大概有三种: Parent.__init__(self, name) # 通过父类的名字,指定调用父类的方法 super( ...
- springMVC课程笔记(一)springMVC架构原理分析
一.springMVC架构原理分析 1.先搞清楚什么是springMVC: 其实springMVC是spring框架中的一个模块,springMVC和spring无需通过中间整合层整合,SpringM ...
- Java终止线程的三种方式
停止一个线程通常意味着在线程处理任务完成之前停掉正在做的操作,也就是放弃当前的操作. 在 Java 中有以下 3 种方法可以终止正在运行的线程: 使用退出标志,使线程正常退出,也就是当 run() 方 ...
- Leetcode题目98.验证二叉搜索树(递归-中等)
题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是 ...
- Linux设备驱动程序 之 顺序锁
当要保护的资源很小,很简单,会频繁的被访问而且写入访问很少的且必须快速时(即读不允许让写饥饿),就可以使用顺序锁(seqlock):从本质上讲,顺序锁会允许读取者对资源的自由访问,但需要读取者检查是否 ...
- php 中 使用foreach为数组增加键值对
php 中的 foreach 在php中,使用foreach来遍历数组的频率很高,并且其性能要高于 list() 和 each() 结合来遍历数组: 当遍历二位数组的第一层数组,并要给第二位数组增加一 ...
- usage memcache in linux
set和add的区别 set可以重写存在的键值对, 也可以添加新的/ 而add不行, 如果存在已有的键名, 则add不会做更新该键值对, 不做任何事, 就是一次无效操作, 也就是, add可以防止重写 ...
- Java面试题准备(掌握)
1.使用什么方式进行文件上传? 一般使用FormData对象,ajax异步上传. 为什么使用? 1.FormData对象Mozilla上的介绍的,XMLHttpRequest Level 2添加了一个 ...