"BZOJ1229" "Usaco" 4.Toys sadstory three points + greedy

Source: Internet
Author: User

Sad story: Our own OJ data seems a bit of a problem. Standard Range WA 5%


Exercises

Copy to Google TranslateTranslation Results

First, the proof of VFK God Ben to prove that the problem is three points.

{

I come to tell you the truth of the world = =

Because this problem can be the minimum cost maximum flow

Every time the shortest circuit length does not drop

So it's a single peak.

The shortest-circuit length is the difference value.

So the first-order guide does not fall.

Is it simple, rude?


< Span style= "font-family: Microsoft Jacob Black, ' MS Sans Serif ', Sans-serif; font-size:13px "> You have to testify that the function is single-peak.

Of course, the slope of the card

}

Three points after the initial purchase of the number of toys, and then is greedy.

First of all, I would like to say that this greedy real action rule. Although it is really greedy.


First of all, a false greed.

is from go to after sweep, priority with fast wash, meet back time node of toy demand.

You can then discover

A B 2 3

2 3 A B

Express Laundromat 1 days

Slow Laundromat for 3 days,

you can get out of the truck. It's easy ... Well, this greed is wrong anyway.

This error code is attached:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 60# Define M 101000#define inf 0x3f3f3f3fusing namespace std;int need[m];int n,d1,d2,c1,c2,m;int l1,l2;int nd[m],rnd[m];void init (int mid) {int i,j,k;for (i=1;i<=n;i++) nd[i]=rnd[i]=need[i];for (i=1;i<=n;i++) {if (Mid<=nd[i]) {nd[i]-= Mid;return;} else mid-=nd[i],nd[i]=0;} return;} A long long check (int mid) {Long Long ret=0;int i,j,k;init (mid); L1=1+d1,l2=1+d2;for (i=1;i<n;i++) {while (l2<=n) {if ( RND[I]&LT;=ND[L2]) {nd[l2]-=rnd[i];ret+=rnd[i]*c2;break;} else {ret+=nd[l2]*c2;rnd[i]-=nd[l2];nd[l2++]=0;}} if (Rnd[i]) while (l1<=n) {if (Rnd[i]<=nd[l1]) {nd[l1]-=rnd[i];ret+=rnd[i]*c1;rnd[i]=0;break;} else {ret+=nd[l1]*c1;rnd[i]-=nd[l1];nd[l1++]=0;}}} for (i=1;i<=n;i++) ret+=nd[i]* (C1-C2); return ret+mid*m;} int main () {freopen ("test.in", "R", stdin); int i,j,k;int l=0,r=1,mid,midmid,temp=0;scanf ("%d%d%d%d%d%d",&n,& D1,&AMP;D2,&AMP;C1,&AMP;C2,&AMP;M); for (i=1;i<=n;i++) scanf ("%d", &need[i]), r+=need[i];if (D1&GT;D2) Swap (D1,D2), swap (C1,C2), if (C1&LT;C2) c2=c1;for (i=1;i<d1;i++) temp+=need[i];for (i= d1;i<=n;i++) {Temp+=need[i];temp-=need[i-d1];l=max (l,temp);} Long Long Ans=inf;while (1) {if (r-l<=2) {for (i=l;i<r;i++) ans=min (Ans,check (i)); mid=l+ (r-l)/3,midmid=l+2* (r-l)/3;long long Reta=check (mid); Long long Retb=check (MIDMID); if (RETA&LT;RETB) R=midmid; else L=mid;} Cout<<ans<<endl;return 0;}

Then there is the right thought (the thought of the standard process):

Also very simple,

is to maintain a few queues of God horse.

Priority to use a quick laundromat, and then sweep the number of fast laundry times and time

There are some laundries that can choose the slow, and then the current choice of fast laundry instead.


This makes the answer change.

Finally, you can return a function value for the current number of toys.

The last three points will be a single peak.

A Usaco is attached to the standard.

#include <stdio.h> #include <stdlib.h> #include <algorithm>using namespace std; #define MAX (100005) # Define INF (1000000000) int t[max];int Queue[max], num[max];int sn,sm,so,en,em,eo;int d,n1,n2,c1,c2,tc;inline void add_ New (int x,int q) {queue[en]=x;num[en++]=q;} int f (int t) {sn=sm=so=en=em=eo=0;int r = (TC-C2) *t;add_new ( -200000,t); for (int. d=0;d<d;d++) {while (sn!=en && D-QUEUE[SN] >= N1) {NUM[EM]=NUM[SN]; queue[em++] = queue[sn++];} while (Sm!=em && D-QUEUE[SM] >= N2) {NUM[EO]=NUM[SM]; queue[eo++] = queue[sm++];} int i = t[d];while (i > 0) {if (So!=eo) {if (num[eo-1] > i) {r + = C2*i;num[eo-1]-=i;break;} else {r + = c2*num[eo-1];i-= num[eo-1];eo--;}} else if (Sm!=em) {if (num[em-1] > i) {r + = C1*i;num[em-1]-=i;break;} else {r + = c1*num[em-1];i-= num[em-1];em--;}} else return INF;} Add_new (D,t[d]);} return r;} int ternary_search (int s,int e) {while (1) {if (e-s <= 5) {int m = f (s), for (int i=s+1;i<e;i++) m = min (m,f (i)); return m;} int x = s+ (e-s)/3, y = s+2* (E-S)/3;int a = f (x), if (a!= INF && a <= f (y)) E=y;else s=x;}} int main () {scanf ("%d%d%d%d%d%d", &AMP;D,&AMP;N1,&AMP;N2,&AMP;C1,&AMP;C2,&AMP;TC), if (N1 > N2) {N1 ^= N2; N2 ^= N1; N1 ^= N2; C1 ^= C2; C2 ^= C1; C1 ^= C2;} if (C1 < C2) {C2 = C1;} int tsum = 0;for (int i=0;i<d;i++) {scanf ("%d", &t[i]); Tsum + = T[i];} printf ("%d\n", Ternary_search (0,tsum+1)); return 0;}

With an original,

#include <stdio.h> #include <stdlib.h> #include <algorithm>using namespace std; #define MAX (100005) # Define INF (1000000000) int t[max];int Queue[max], num[max];int sn,sm,so,en,em,eo;/* These variables control the queue and  Point to the start * and end of new, middle, and old toys, respectively.  * New toys is ones washed less than N1 days ago, old toys is * washed at least N2 days ago, and middle toys is all the Rest. * We essentially keep three queues in a single array, but the manner * in which we add/remove elements guarantees that we Never has to * worry about memory overlap.  */int d,n1,n2,c1,c2,tc;inline void add_new (int x,int q) {queue[en]=x; Num[en++]=q;} void flush_new (int x) {while (sn!=en && X-QUEUE[SN] >= N1) {NUM[EM]=NUM[SN]; queue[em++]= queue[sn++];}} void Flush_mid (int x) {while (Sm!=em && X-QUEUE[SM] >= N2) {NUM[EO]=NUM[SM]; queue[eo++]= queue[sm++];}} int f (int t) {//find The minimum cost given so we use T toys sn=sm=so=en=em=eo=0;  int r = (TC-C2) *t; /* * In the following algorithm, we pay even to wash the initial toys, so * We had to subtract this out of our Initi Al cost estimate for * purchasing the toys. This is valid as long as we have all of the toys * We purchase (which are why we start the ternary search at Tsum+1 *   instead of 5000001).  */Add_new ( -200000,t); for (int. d=0;d<d;d++) {flush_new (d); Flush_mid (d);//move any toys whose status changes//from being new T    o Middle or middle to old to the appropriate queue int i = t[d];       while (i > 0) {//we deal with the toys in batches to make//the runtime of this function O (N) instead of O (sum ti) if (So!=eo) {//if There is any old toys if (num[eo-1] > i) {//if This batch have more toys than we need, WEC          An stop here R + = C2*i;          Num[eo-1]-=i;        Break          } else {//otherwise, use any toys in this batch R + = C2*num[eo-1];    I-= num[eo-1];      eo--;          }} else if (sm!=em) {//else If there is any middle toys if (num[em-1] > i) {r + = C1*i;          Num[em-1]-=i;        Break          } else {r + = C1*num[em-1];          I-= num[em-1];        em--; }} else return INF; If there is no available toys,//we can ' t find a solution with this many toys} add_new (D,t[d]); Put the toys we used today back into the queue of toys} return R; int ternary_search (int s,int e) {while (1) {if (e-s <= 2) {//when e-s is small enough, our ternary search/      /can get stuck, just handle the end manually int m = f (s);      for (int i=s+1;i<e;i++) m = min (m,f (i));    return m; } int x = s+ (e-s)/3, y = s+2* (e-s)/3; Sample values 1/3 and 2/3//of the through the remaining interval int a = f (x);    f (x) =-1 if x is too few toys to has enough toys each day if (a!= INF && a <= f (y)) e=y;  else s=x; }}iNT Main () {FILE *fin = fopen ("toy.in", "R");  FILE *fout = fopen ("Toy.out", "w");  FSCANF (FIN, "%d%d%d%d%d%d", &AMP;D,&AMP;N1,&AMP;N2,&AMP;C1,&AMP;C2,&AMP;TC); if (N1 > N2) {//set N2 to be greater than N1 N1 ^= N2; N2 ^= N1;    N1 ^= N2; C1 ^= C2; C2 ^= C1;  C1 ^= C2; } if (C1 < C2) {//if faster is cheaper C2 = C1;//then set it cost to that of the slower,//since we  Could always just use the slower instead} int tsum = 0;  for (int i=0;i<d;i++) {fscanf (Fin, "%d", &t[i]); Tsum + = T[i];}  fprintf (Fout, "%d\n", Ternary_search (0,tsum+1)); Fclose (Fin);  Fclose (Fout); return 0;}


The last is the URL

Http://cerberus.delosent.com:794/TESTDATA/NOV08.toy.htm


"BZOJ1229" "Usaco" 4.Toys sadstory three points + greedy

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.