Test instructions
Have n jewels m box Jewel value a box value b
Each jewel is placed in a box and costs ABS (A-B)
The box can put jewels in infinity.
Ask for the minimum cost
Water problem
Pre-treatment of each value of jewelry placed in the box O (n)
Find the closest left box from left to right L find the closest right box from right to left R
Take min
#include <cstdio>#include<cstring>#include<string>#include<iostream>#include<algorithm>#include<map>using namespacestd;Const intn=20005;intn,k,m;intL[n],r[n];intv[Ten*N],s[n];intMain () {inti,j; intT; while(SCANF ("%d%d", &n,&m)! =EOF) {memset (L,0,sizeof(l)); Memset (R,0,sizeof(R)); memset (V,0,sizeof(v)); memset (s),0,sizeof(s)); for(i=1; i<=n;i++) {scanf ("%d",&V[i]); } while(m--) {scanf ("%d",&i); S[i]=1; } intnow=-10000; for(i=1; i<=10000; i++) { if(S[i]) now=i; L[i]=i-Now ; } Now=30000; for(i=10000; i>=1; i--) { if(S[i]) now=i; R[i]=now-i; } Long Longans=0; for(i=1; i<=n;i++) {ans+=min (R[v[i]],l[v[i]]); } cout<<ans<<Endl; } return 0;}
HDU Computer College College Student Program Design Contest (1003) The collector's puzzle