https://www.zybuluo.com/ysner/note/1312307 Problem surface
Poke me.
Analytical
Feel more routine.
Of course, the premise is to note that each currency does not affect.
That's easy.
Set \ (f[i][j][k]\) represents the value of the i\ ,\ (a\) has \ (i\) yuan,\ (b\) has \ (j\ ) yuan. Obviously the amount of money fixed,\ (c\) of the sum can be calculated by the passing.
So the starting state and the final state are known.
Then enumerate how many of the money they have left after they have traded, and transfer directly.
If set \ (x,y,z\) represents the amount of change in a,b,c\ , then the actual number of exchanges is \ ((\delta x+\delta y+\delta z) \div 2\). (This hand is counted out).
If you want to \ (bzoj\) , you need to enumerate the currency value backwards. Because the larger the value of the currency, the more effectively transferred to the back of the state of the less, it is easy to be \ (continue\) off. The
complexity should be \ (O (6*1000^2*30^2=5.4*10^9) \) , but it is difficult to run full.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #define RE register#define il inlineusing namespace Std;int x1,x2,x3,f[2][1005][1005], Num[5][10],snum[10],ss[5],sum,val[10]={0,1,5,10,20,50,100};il int gi () {re int x=0,t=1; Re char Ch=getchar (); while (ch!= '-' && (ch< ' 0 ' | | Ch> ' 9 ')) Ch=getchar (); if (ch== '-') T=-1,ch=getchar (); while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar (); return x*t;} int main () {X1=gi (); X2=gi (); X3=gi (); for (re int i=1;i<=3;++i) for (re int j=6;j>=1;--j) {num[i][j]=gi (); SNUM[J]+=NUM[I][J]; SS[I]+=VAL[J]*NUM[I][J]; } Sum=ss[1]+ss[2]+ss[3]; memset (f[0],63,sizeof (f[0)); f[0][ss[1]][ss[2]]=0; for (re int i=1;i<=6;++i) {RE int now=i&1,pre=now^1; memset (f[now],63,sizeof (F[now)); for (re int j=0;j<=sum;++j) for (re int k=0;k<=sum-j;++k) {if (F[PRE][J][K]>1E9) continue; for (re int N=0;n<=snum[i];++n) for (re int m=0;m<=snum[i]-n;++m) {re int a=j+ (N-num[1][i]) *val[i],b=k+ (m-num[ 2][i]) *val[i]; if (a>=0&&b>=0&&a<=1000&&sum-a-b>=0) {re int w=abs (N-num[1][i]) +abs (m-num[ 2][i]) +abs (num[1][i]+num[2][i]-n-m) >>1; F[now][a][b]=min (F[NOW][A][B],F[PRE][J][K]+W); }}}} re int las1=ss[1]-x1+x3,las2=ss[2]+x1-x2; if (las1<0| | las2<0| | sum-las1-las2<0| | F[0][LAS1][LAS2]>1E9) puts ("impossible"); else printf ("%d\n", F[0][las1][las2]); return 0;}
[SHOI2008] Recurring debt