搞了一下午+半晚上。其实不是很难。
提答题重要的是要发现数据的特殊性质,然后根据不同数据写出不同的算法获得其对应的分数。
首先前两个测试点我们发现可以直接暴搜通过,事实上对于每个数据都暴搜加上一定的次数限制,都可以获得两分的好成绩。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 int n,m,a[105],ans=-0x7fffffff; 8 int fin[1000500],st[1000500]; 9 struct num{ 10 int x1,x2; 11 num(){x1=x2=0;} 12 num(int x,char o){ 13 if(o=='v')x1=x,x2=0; 14 else x1=0,x2=x; 15 } 16 }; 17 struct data{ 18 char o,f; 19 num x,y; 20 int s1,s2; 21 }d[100500]; 22 char getc(){ 23 char ch=getchar(); 24 while(ch!='v'&&ch!='c'&&ch!='i'&&ch!='s'&&ch!='+'&&ch!='-') 25 ch=getchar(); 26 return ch; 27 } 28 void dfs(int x,int step){ 29 if(x>n||x<1){ 30 if(a[1]>ans){ 31 for(int i=1;i<step;i++) 32 fin[i]=st[i]; 33 ans=a[1]; 34 } 35 return ; 36 } 37 if(d[x].o=='v'){ 38 int now=d[x].y.x1?a[d[x].y.x1]:d[x].y.x2; 39 if(d[x].f=='+')a[d[x].x.x1]+=now; 40 else a[d[x].x.x1]-=now; 41 dfs(x+1,step); 42 if(d[x].f=='+')a[d[x].x.x1]-=now; 43 else a[d[x].x.x1]+=now; 44 } 45 if(d[x].o=='i'){ 46 int x1=d[x].x.x1?a[d[x].x.x1]:d[x].x.x2; 47 int x2=d[x].y.x1?a[d[x].y.x1]:d[x].y.x2; 48 if(x1<x2)dfs(d[x].s1,step); 49 else dfs(d[x].s2,step); 50 } 51 if(d[x].o=='s'){ 52 st[step]=1,dfs(d[x].s1,step+1); 53 st[step]=2,dfs(d[x].s2,step+1); 54 } 55 } 56 int read(){ 57 int a=0,f=1;char ch=getchar(); 58 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 59 while(ch>='0'&&ch<='9'){a=a*10+ch-'0';ch=getchar();} 60 return a*f; 61 } 62 int main(){ 63 freopen("train1.in","r",stdin); 64 freopen("train1.in","r",stdin); 65 scanf("%d%d",&n,&m); 66 for(int i=1;i<=n;i++){ 67 d[i].o=getc(); 68 if(d[i].o=='v'){ 69 d[i].x=num(read(),'v'); 70 d[i].f=getc(); 71 d[i].y=num(read(),getc()); 72 } 73 else if(d[i].o=='s'){ 74 d[i].s1=read(); 75 d[i].s2=read(); 76 } 77 else if(d[i].o=='i'){ 78 d[i].x=num(read(),getc()); 79 d[i].y=num(read(),getc()); 80 d[i].s1=read(); 81 d[i].s2=read(); 82 } 83 } 84 dfs(1,1); 85 for(int i=1;fin[i];i++) 86 printf("%d\n",fin[i]); 87 return 0; 88 }