D
题意:
n个人围一圈, 顺时针或者逆时针扔球, r是距离, op是‘0’的话就是顺, ‘1’为逆, ‘?’为不知道是顺还是逆,现在已知n球员人数、m投球次数和x最先投球球员的号码。以及m次投掷的信息, 求最后一次球会在的玩家数量以及递增的玩家编号
有点优化的dfs的感觉, 但dfs会超时
void solved()
{
cin >> n >> m >> x;
set<int>s, t;
s.insert(x);
for(int i = 0; i < m; i ++)
{
int r; char op;
cin >> r >> op;
for(auto x : s)
{
int y = x + r;
if(y > n) y -= n;
int yy = x - r;
if(yy <= 0) yy += n;
if(op == '0')
t.insert(y);
else if(op == '1')
t.insert(yy);
else
t.insert(y), t.insert(yy);
}
s = t;
t.clear();
}
cout << s.size() << endl;
for(auto k : s)
cout << k << " " ;
cout << endl;
}
超时写法
struct ball
{
int r; char op;
}e[N];
void dfs(int st, int d, vector<int>& v)
{
// cout << st << " " << d << endl;
if(d == m + 1)
{
v.push_back(st);
return;
}
int r = e[d].r;
char c = e[d].op;
int y = st + r;
if(y > n) y -= n;
int yy = st - r;
if(yy <= 0) yy += n;
if(c == '0')
dfs(y, d + 1, v);
else if(c == '1')
dfs(yy, d + 1, v);
else
{
dfs(y, d + 1, v);
dfs(yy, d + 1, v);
}
}
void solved()
{
cin >> n >> m >> x;
for(int i = 1; i <= m; i ++)
cin >> e[i].r >> e[i].op;
vector<int>v;
set<int>s;
dfs(x, 1, v);
sort(v.begin(), v.end());
for(auto k : v)
s.insert(k);
cout << s.size() << endl;
for(auto k : s)
cout << k << " ";
cout << endl;
}