(Winter Camp) Mooo Moo (full backpack)

Source: Internet
Author: User

Mooo Moo

Time limit: 1 Sec memory limit: up to MB
Submissions: 5 Resolution: 4
Submitted State [Discussion Version]

Title DescriptionFarmer John had completely forgotten how many cows he owns!  He is too embarrassed to go to his fields to count the cows, since he doesn ' t want the cows to realize his mental lapse. Instead, he decides to count hisCows secretly by planting microphones in the fields in which his cows tend to gather, figuring that he can determine the Number of cows from the total volume of the mooing he hears.

FJ ' s n fields (1 <= n <=) is all arranged in a line along a long straight road. Each field might contain several types of cows; FJ owns cows that come from B different breeds (1 <= B <=), and a cow of breed I moos at a volume of V (i) (1 <  = V (i) <= 100). Moreover, there is a strong wind blowing down the road, which carries the sound of mooing in one direction from left to RI Ght:if the volume of mooing in some field was X, then under the next field this would contribute X-1 to the total mooing volum E (and X-2 in the field after, etc.). Otherwise stated, the mooing volume in a field was the sum of the contribution due to cows in that field, plus X-1, where X mooing volume in the preceding field.

Given the volume of mooing that FJ records in each field, please compute the minimum possible number of cows FJ might own.

The volume FJ records in any field are at most 100,000.

Input* Line 1:the integers N and B.
* Lines 2..1+b:line i+1 contains the integer V (i).
* Lines 2+b. 1+b+n:line 1+b+i contains the total volume of any mooing in field I.Output* Line 1:the minimum number of cows owned by FJ, or-1 if there are no configuration of cows consistent with the Inpu T.

Sample input
5 257017162019
Sample output
4
Tips

FJ owns 5, with mooing volumes 0,17,16,20,19. There is breeds of cows; The first Moos at a volume of 5, and the other at a volume of 7.There is 2 cows of breed #1 and 1 cow of breed #2 in Fiel D 2, and there is
Another cow of breed #1 in field 4.

"Analysis" by test instructions know, each area of the sound size in addition to itself only by the previous region, minus the impact of the previous area, only the sound of their own production, the minimum number of cattle, is a complete backpack.

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)#definePB Push_backtypedefLong Longll;using namespacestd;Const intN =1e3;Const intM =24005;intn,m,k,maxn=0, ans=0;intmx=-1;intv[n],a[n],b[n],dp[n*N];intMain () {scanf ("%d%d",&n,&k);  for(intI=1; i<=k;i++) scanf ("%d",&V[i]);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]);  for(intI=1; i<=n;i++) {B[i]=a[i]-max (a[i-1]-1,0); MX=Max (mx,b[i]); }     for(intI=1; i<=mx;i++) dp[i]=inf;  for(intI=1; i<=k;i++){         for(intj=v[i];j<=mx;j++){            if(Dp[j-v[i]]!=inf) Dp[j]=min (dp[j],dp[j-v[i]]+1); }    }     for(intI=1; i<=n;i++){        if(dp[b[i]]==inf) {printf ("-1\n"); return 0; } ans+=Dp[b[i]]; } printf ("%d\n", ans); return 0;} 

(Winter Camp) Mooo Moo (full backpack)

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.