UVa 714-copying Books Two-point answer

Source: Internet
Author: User

Title Link: 714-copying Books

Thinking of solving problems

See the code for the specific processing method

/************************************************************** Problem:User:youmi language:c++ result:a ccepted time:memory:****************************************************************///#pragma COMMENT (linker, "/stack:1024000000,1024000000")//#include <bits/stdc++.h>#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<stack>#include<Set>#include<sstream>#include<cmath>#include<queue>#include<string>#include<vector>#defineZeros (a) memset (A,0,sizeof (a))#defineOnes (a) memset (A,-1,sizeof (a))#defineSC (a) scanf ("%d", &a)#defineSC2 (A, b) scanf ("%d%d", &a,&b)#defineSC3 (a,b,c) scanf ("%d%d%d", &a,&b,&c)#defineSCS (a) scanf ("%s", a)#defineSclld (a) scanf ("%i64d", &a)#definePT (a) printf ("%d\n", a)#definePtlld (a) printf ("%i64d\n", a)#defineRep0 (i,n) for (int i=0;i<n;i++)#defineREP1 (i,n) for (int i=1;i<=n;i++)#defineRep_1 (i,n) for (int i=n;i>=1;i--)#defineRep_0 (i,n) for (int i=n-1;i>=0;i--)#defineMax (a) (a) > (b)? (a):(B)#defineMin (a) (a) < (b)? (a):(B)#defineLson (step<<1)#defineRson (lson+1)#defineESP 1e-6#defineOO 0x3fffffff#defineTEST cout<< "*************************" <<endlusing namespaceStd;typedefLong Longll;intn,k;Const intmaxn= -+Ten; ll A[maxn];map<int,int>m;//is used to mark 1-n which locations should have "/" symbols.intMain () {//freopen ("In.txt","R", stdin); intt_t; scanf ("%d",&t_t);  for(intKase=1; kase<=t_t;kase++) {SC2 (n,k); ll L=0, r=0;            REP1 (i,n) {sclld (a[i]); L=Max (l,a[i]);//The lower bound is definitely the maximum value of R+=a[i];//Upper limit is naturally the sum of all}inttot=0; LL cur=R;        ll temp;  while(l<r)//each method has its chunk and the maximum value, such as the white Paper said 7,9. So we're looking for the maximum chunk and the smallest value in the block condition {//such as the case on the white paper, 7<9, so we found 7 tot=0; Temp=0; //printf ("L->%i64d cur->%i64d r->%i64d\n", l,cur,r);             for(inti=n;i>=1; i--)//From the end of the statistics (actually here from the beginning can also, because we are looking for the smallest sub-conditions), each time the greedy to make the back of the larger, then the front of the natural smaller {//printf ("tot->%d temp->%i64d \ n", tot,temp);temp+=A[i]; if(temp>cur) {                    ++tot; I++///Because temp plus a[i] is more than cur, so the right side of the next piece should be a[i], so here i++ and the i--offset in the for loop//printf ("%d%d%i64d\n", tot,i-1,temp-a[i-1]);temp=0; if(tot==k)//divided k times, then a total of k+1 block, that is, this case cur too small {l=cur+1; Cur= (l+r) >>1;  Break; }                }            }            if(tot<k)/////cur block The result is not enough, for example, maybe only a piece, so this situation cur too big {R=cur; Cur= (l+r) >>1; After the}//while loop, we find a minimum chunk and maximum value that satisfies the block condition, and the current cur value is the value, and then the greedy starts from the back to tot=0; Temp=0;            M.clear ();  for(inti=n;i>=1; i--) {Temp+=A[i]; if(temp>cur) {M[i++]=1; ++tot; Temp=0; }} k--; intId=1;  while(tot<k)//If the number of blocks by cur is less than cur, such as 100,100,100,100,100, this case, the sub-4 block is cur the minimum satisfying condition should be 200, press 200 from the rear, get 100/100 100/100 100, less than {// Then we in 100/100 100/100 100 casually inserted a plate is also satisfied with the condition, in order to make the front as little as possible, so I should put the board in front, so starting from 1 to searchif(!M[id]) {M[id]=1; Tot++; } ID++; }        //Ptlld (r);Rep1 (i,n) {printf ("%lld%c", A[i],i==n?'\ n':' '); if(M[i]) {printf ("/ "); }        }    }    return 0;}

UVa 714-copying Books Two-point answer

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.