Only feels like nothing could be better when I'm with you
连蒙带猜把这个题过了。
一共写了
状态不是很好所以花了两天半时间才通过,确实也还是太菜了。个人感觉这个题其实并没有特别难,也就和乱西星差不多吧。
不想给代码了,确实写的也不优雅,从第
不过,数据模拟器代码我会放在下面。
我们来逐一拆解这
本文令
1
发现只有
因此我们可以写一个爆搜来枚举每一个选择跳转选什么。
2
发现有了
于是我们可以继续用爆搜来做。
3
这回有
我们可以发现这种结构:
i v 3 c 0 122 124
v 1 - v 3
i c 0 c 1 125 0
v 1 + v 3
v 3 - v 3
那么意思是,如果
也就是,每个部分的最后所有其他变量都会被清零,这意味着多个部分之间互不影响。
因此分段爆搜即可。
4
开始略难了。可以发现这个点全都是
那这是一个
5
这回不是每次跳过一个物品了,有可能跳更多了,那怎么办?
可以跑一下依赖关系,发现存在一棵有向树和该依赖关系是等价的。建树的话可以利用栈。遇到一个事件就把它放栈里,等出了它跳过的区间就弹栈。每次把栈顶作为父亲即可。当然也可以把每个点的区间处理出来,对于每个点暴力扫它覆盖的区间,处理出祖先和后代的关系,然后暴力建树(但是这样会慢一些)。
因此我们可以直接把背包搬到树上做。要注意一下输出顺序。
这个点比较恶心的是,它不再是简单的若干个事件一循环了,要把事件的位置搞清楚,这样才能处理出来。
6
其实做法和
7、8
我去,这怎么和第
原来它是把第
9、10
观察发现又是把背包改成树形背包,那么用第
对于后四个点比较好的实现是在跑出价值的同时把这一块内的最佳选择跑出来。然后输出的时候把这个最佳选择的串拼进去就好了。
其实感觉第
数据模拟器代码
删去了调试信息。询问处理其实就是这个代码(
#include<bits/stdc++.h>
using namespace std;
int var[20];
struct Event{
int op;//事件类型
//1 普通事件(+) 2 普通事件(-) 3 选择跳转事件 4 条件跳转时间
int n1, n2;//参考量 1 2
int p1, p2;//参考量 1 2 的类型,1 常量 2 变量
int t1, t2;//跳转到的事件
}a[100005];
int n, m;
string cp(string &s, int l, int r){//实现一个切片功能,但是是闭区间
return s.substr(l, r - l + 1);
}
int sti(string s){
int res = 0;
for (auto c : s){
if (c == '-')
continue;
res = res * 10 + (c - '0');
}
if (s[0] == '-')
res = -res;
return res;
}
bool isd(char ch){
return ch >= '0' && ch <= '9' || ch == '-';
}
bool isl(char ch){
return ch >= 'a' && ch <= 'z';
}
void GetEvent(int p, string s){
if (s.find("+") != -1){
a[p].op = 1;
int i = 0, j = 0;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].n1 = sti(cp(s, i, j - 1));
i = s.find("+");
while (!isl(s[i]))
i++;
if (s[i] == 'c')
a[p].p2 = 1;
else
a[p].p2 = 2;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].n2 = sti(cp(s, i, j - 1));
}else if (s.find("-") != -1){
a[p].op = 2;
int i = 0, j = 0;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].n1 = sti(cp(s, i, j - 1));
i = s.find("-");
while (!isl(s[i]))
i++;
if (s[i] == 'c')
a[p].p2 = 1;
else
a[p].p2 = 2;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].n2 = sti(cp(s, i, j - 1));
}else if (s[0] == 's'){
a[p].op = 3;
int i = 0, j = 0;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].t1 = sti(cp(s, i, j - 1));
i = j;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].t2 = sti(cp(s, i, j - 1));
}else{
a[p].op = 4;
int i = 1, j = 0;
while (!isl(s[i]))
i++;
if (s[i] == 'c')
a[p].p1 = 1;
else
a[p].p1 = 2;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].n1 = sti(cp(s, i, j - 1));
while (!isl(s[i]))
i++;
if (s[i] == 'c')
a[p].p2 = 1;
else
a[p].p2 = 2;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].n2 = sti(cp(s, i, j - 1));
i = j;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].t1 = sti(cp(s, i, j - 1));
i = j;
while (!isd(s[i]))
i++;
j = i;
while (isd(s[j]))
j++;
a[p].t2 = sti(cp(s, i, j - 1));
}
}
int gvl(int op, int id){
if (op == 1)
return id;
else
return var[id];
}
string opt;
int main(){
cin >> opt;
// freopen("train9.in", "r", stdin);
// freopen("train9.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
cin.ignore();
for (int i = 1; i <= n; i++){
string s;
getline(cin, s);
GetEvent(i, s);
}
int pos = 0;
for (int p = 1; p <= n; p++){
if (a[p].op == 1){
int awa = gvl(a[p].p2, a[p].n2);
var[a[p].n1] += awa;
}else if (a[p].op == 2){
int awa = gvl(a[p].p2, a[p].n2);
var[a[p].n1] -= awa;
}else if (a[p].op == 3){
if (opt[pos] == '1')
p = a[p].t1 - 1;
else
p = a[p].t2 - 1;
pos++;
}else if (a[p].op == 4){
if (gvl(a[p].p1, a[p].n1) < gvl(a[p].p2, a[p].n2))
p = a[p].t1 - 1;
else
p = a[p].t2 - 1;
}
}
cout << var[1] << " " << pos + 1;
return 0;
}