Title Link: Https://cn.vjudge.net/contest/209473#problem/B
The main idea: for n numbers, give sum[j]-sum[i] (sum for prefix and) symbol (plus or minus 0), to find a set of n number of the feasible solution (n number is between -10--10) "Guaranteed there must be a solution"
Problem Solving Ideas:
First reaction! Differential constraint!
The difference constraint is used to solve the reasonable solution of the inequality group, and the sum[i]-sum[j]>0 is converted to Sum[i]-sum[j]>=-1, which is less than 0. Turn the sum[i]-sum[j]==0 into a sum[i]-sum[j]>=0,sum[j]-sum[i]>=0.
The difference constraint will be described in another topic, the students will be able to build this method of running the shortest possible, not this method of the students recommend the second method to choose the topology of the sorting. "But recommended differential constraints, because the feeling is simpler than the extension line"
Later, and other students to exchange discussions, only to know the solution, or the official solution is a topological sort.
Change the size of the relationship to one-way, such as the 鶸 of the ugly code is the big prefix and draw a side pointing to the small prefix and.
The special point is equal to zero processing, think for half an hour (good weak AH), think of a very ugly method, is to put two equal points of the size of the relationship is completely copied. That is, if SUM[A]==SUM[B], then all connection A is not connected to the side of B, all plus B, all connection B is not connected to a side, all on a, regardless of direction.
The second special point is to control the size of n number, if you choose the difference constraint only need to change the upper limit value to 10 on the line, for the extension row, I think of an ugly method, the largest prefix and assigned to 10*n, down each layer minus 1, because the topic guarantee there must be a solution, so there will be no problem.
Put the following code:
Differential constraint 6msAC code:
1 /*by Lstg*/2 /*2018-01-27 15:32:28*/3 4#include <stdio.h>5 #defineINF 1020000006 7 intmap[ the][ the];8 9 Ten intMain () { One A intt,i,j,n,k; - CharT; -scanf"%d",&T); the while(t--){ - -scanf"%d",&n); - GetChar (); + for(i=0; i<=n+1; i++) - for(j=0; j<=n+1; j + +) + if(I!=J) map[i][j]=inf; A for(i=1; i<=n;i++) at for(j=i;j<=n;j++){ -t=GetChar (); - if(t=='+') map[j][i-1]=-1; - Else if(t=='-') map[i-1][j]=-1; - Else -map[i-1][j]=map[j][i-1]=0; in - } to for(i=0; i<=n;i++) +map[n+1][i]=Ten; -n++; the for(k=0; k<=n;k++) * for(i=0; i<=n;i++) $ for(j=0; j<=n;j++)Panax Notoginseng if(map[i][k]+map[k][j]<Map[i][j]) -map[i][j]=map[i][k]+Map[k][j]; the for(i=1; i<n;i++) +printf"%d", map[n][i]-map[n][i-1]); APutchar (Ten); the } + return 0; -}
Topology Sort 6msAC Code:
1 /*by Lstg*/2 /*2018-03-04 00:11:12*/3 4 5#include <stdio.h>6#include <string.h>7 8 intsum[ the],g[ the][ the],du[ the],stk[ the],n;9 Ten void_getans () { One A inti,top=0. PY - for(i=0; i<=n;i++) - if(!Du[i]) { thestk[++top]=i; -sum[i]=Ten*N; - } - while(top) { +p=stk[top--]; - for(i=0; i<=n;i++) + if(G[p][i]) { Adu[i]--; at if(!Du[i]) { -sum[i]=sum[p]-1; -stk[++top]=i; - } - } - } in } - to intMain () { + - the intt,i,j,k; * Charch[ the]; $ Panax Notoginsengscanf"%d",&T); - while(t--){ the +Memset (Du,0,sizeof(du)); AMemset (G,0,sizeof(g)); thememset (SUM,0,sizeof(sum)); +scanf"%d",&n); - $scanf"%s", ch); $k=0; - for(i=0; i<n;i++) - for(j=i+1; j<=n;j++){ the if(ch[k]=='+'){ -g[j][i]=true;Wuyidu[i]++; the } - if(ch[k]=='-'){ Wug[i][j]=true; -du[j]++; About } $k++; - } -k=0; - for(i=0; i<n;i++) A for(j=i+1; j<=n;j++) + if(ch[k++]=='0') the for(intA=0; a<=n;a++){ - if(!g[i][a]&&G[j][a]) { $g[i][a]=true; thedu[a]++; the } the if(!g[j][a]&&G[i][a]) { theg[j][a]=true; -du[a]++; in } the if(!g[a][i]&&G[a][j]) { theg[a][i]=true; Aboutdu[i]++; the } the if(!g[a][j]&&G[a][i]) { theg[a][j]=true; +du[j]++; - } the }Bayi _getans (); the the for(i=1; i<=n;i++) - -printf"%d", sum[i]-sum[i-1]); thePutchar (Ten); the } the return 0; the}
"Topological sort or differential constraint" Guess UVALive-4255