HDU Computer College College Student Program Design Contest (1003) The collector's puzzle

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.