题解:AT_abc453_c [ABC453C] Sneaking Glances
TchongS1010C · · 题解
1.翻译
高桥现在正在数轴上的
2.选择算法
如果你睁大你的眼睛看数据范围……
n\le 20
我们明显可以用 dfs 暴力枚举,因为我们是选择性 dfs ,所以时间复杂度为
3.实现
关于 dfs 的参数,我们用
接下来我们先考虑
dfs(step + 1 , pos - a[step] , cnt + (pos > 0 && pos - a[step] < 0)) ;//我们面临着第step+1次选择,当前的位置为pos-a[step],如果本来在0的右边但是走到了0的左边我们就穿越了一次。
dfs(step + 1 , pos + a[step] , cnt + (pos < 0 && pos + a[step] > 0)) ;//我们面临着第step+1次选择,当前的位置为pos+a[step],如果本来在0的左边但是走到了0的右边我们就穿越了一次。
然后我们来考虑已经选完,即
if(step > n)
{
maxn = max(maxn , cnt) ;
return ;//递归的话一定要退出,不然会MLE+TLE+RE组合拳
}
最后面让我来把代码先给大家。
#include <bits/stdc++.h>
using namespace std ;
int n , a[25] , maxn ;
void dfs(int step , double pos , int cnt)
{
if(step > n)
{
maxn = max(maxn , cnt) ;
return ;
}
dfs(step + 1 , pos - a[step] , cnt + (pos > 0 && pos - a[step] < 0)) ;
dfs(step + 1 , pos + a[step] , cnt + (pos < 0 && pos + a[step] > 0)) ;
}
int main()
{
cin >> n ;
for(int i = 1 ; i <= n ; i ++)
{
cin >> a[i] ;
}
dfs(1 , 0.5 , 0) ;//面临第一次选择,位置在0.5,没穿越过一次
cout << maxn ;
return 0 ;
}
最后面请大家不要抄,这样的模版题应该不至于这么难吧?难度的话建议给橙。
AC记录
谢谢大家!