UESTC 764 lost Christmas--rmq/line tree

Source: Internet
Author: User

Test instructions: N kinds of goods, each item to different people have different value, there are three candidates, the first for ordinary students, the second is set, the third is to pray, set and pray can choose the same, and will get extra points, set and pray for the general students are unable to choose, ask three how to choose to make the highest total score.

Solution: First set and save to an array sum, and then you can enumerate the ordinary students choose which, and then on the left and right of sum to find a maximum value, update maxi, and then consider the different sets of the case, that is, the set of the array to take the maximum value, and on both sides of the prayer array to take a maximum value, Add, if the maximum value of the set and the maximum value is a mark, we in the previous sum maximum has been updated maxi, so no bonus is certainly smaller than the sum, so directly find the maximum value of two array is OK.

The maximum value of the interval can be RMQ or segment tree.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;#defineN 10017intdsum[n][ -],dji[n][ -],dqi[n][ -];intsum[n],ji[n],qi[n],pu[n],log[n+7000];voidRmq_init (intm) {    inti,j;  for(i=1; i<=m;i++) {dsum[i][0] =Sum[i]; dji[i][0]  =Ji[i]; dqi[i][0]  =Qi[i]; }     for(j=1;(1&LT;&LT;J) <=m;j++)    {         for(i=1; i+ (1&LT;&LT;J)-1<=m;i++) {Dsum[i][j]= Max (dsum[i][j-1],dsum[i+ (1<< (J-1))][j-1]); DJI[I][J]= Max (dji[i][j-1],dji[i+ (1<< (J-1))][j-1]); DQI[I][J]= Max (dqi[i][j-1],dqi[i+ (1<< (J-1))][j-1]); }    }}voidGetLog (intN) {     for(intI=0; i<=n;i++) Log[i]= (int) (Log (Double) i)/log (2.0));}intRMQ (int(*d) [ -],intLintR) {    if(R < L)return 0; intK = log[r-l+1]; returnMax (d[l][k],d[r-(1&LT;&LT;K) +1][k]);}intMain () {intT,i,j,n; scanf ("%d",&t); GetLog (15000);  while(t--) {scanf ("%d",&N);  for(i=1; i<=n;i++) scanf ("%d",&Pu[i]);  for(i=1; i<=n;i++) scanf ("%d",&Ji[i]);  for(i=1; i<=n;i++) scanf ("%d",&Qi[i]);  for(i=1; i<=n;i++) scanf ("%d", &sum[i]), sum[i] + = ji[i]+Qi[i];        Rmq_init (n); intMaxi =0;  for(i=1; i<=n;i++)        {            intNormal =Pu[i]; intSumleft = RMQ (DSum,1, I-1); intSumright = RMQ (dsum,i+1, N); Maxi= Max (maxi,normal+Max (sumleft,sumright)); intMaxji = max (RMQ (DJI,1, I-1), RMQ (dji,i+1, N)); intMaxqi = Max (RMQ (Dqi,1, I-1), RMQ (dqi,i+1, N)); Maxi= Max (maxi,normal+maxji+Maxqi); } cout<<Maxi<<Endl; }    return 0;}
View Code

UESTC 764 lost Christmas--rmq/line tree

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.