"Greedy", "two-dimensional partial order", "weight-value block" bzoj1691 [Usaco2007 Dec] Picky foodie

Source: Internet
Author: User

Since the requirements of the topic satisfy the two-dimensional partial order, it is natural for us to think of all the things (grass and cattle) to be read in, sort one-dimensional (delicacy), and then take the current smallest in another dimension (value).

As a result, splay, Mutiset, weight of the block and so on support the query successor.

1#include <cstdio>2#include <cmath>3#include <algorithm>4 using namespacestd;5 intnum,ch[ A],f,c;6InlinevoidRint&x) {7C=0; f=1;8      for(;c<'0'|| C>'9'; C=getchar ())if(c=='-') f=-1;9      for(x=0; c>='0'&&c<='9'; C=getchar ()) (x*=Ten) + = (c'0');Tenx*=F; One } A Long Longans; - structval3{intC,v;BOOL  is;voidRead () {R (c); R (v);}} a[200001]; - structpoint{intV,p;} t[200001]; the BOOL operator< (ConstVal3 &a,ConstVal3 &b) {returnA.V!=B.V? A.v>b.v:a. is>b. is;} - BOOL operator< (ConstPoint &a,ConstPoint &b) {returna.v<B.V;} - intn,m,en,ma[200001],b[200001],sumv[ the],l[ the],r[ the],num[200001],sum=1; - intb[200001]; + voidMakeblock () - { +     intSz=sqrt (en);if(!SZ) sz=1; A      for(; sum*sz<en;++sum) at       { -l[sum]=r[sum-1]+1; r[sum]=sum*sz; -            for(intI=l[sum];i<=r[sum];++i) num[i]=sum; -       } -l[sum]=r[sum-1]+1; r[sum]=en; -      for(intI=l[sum];i<=r[sum];++i) num[i]=sum; in } - voidInsert (Const int&AMP;X) {++b[x]; + +sumv[num[x]];} to voidDelete (Const int&AMP;X) {--b[x];--sumv[num[x]];} + intNext (Const int&x) - { the      for(inti=x;i<=r[num[x]];i++)if(B[i])returni; *      for(inti=num[x]+1; i<=sum;i++)if(Sumv[i]) $        for(intj=l[i];j<=r[i];j++)Panax Notoginseng         if(B[j])returnJ; -     return-1; the } + intMain () A { the R (n); R (m); +      for(intI=1; i<=n;++i) a[i]. Read (); -      for(inti=n+1; i<=m+n;++i) $       { $ A[i]. Read (); -A[i]. is=1; -       } theSort (A +1, a+n+m+1); -      for(intI=1; i<=m+n;++i)Wuyi       { thet[i].v=a[i].c; -t[i].p=i; Wu       } -Sort (t+1, t+1+n+m); Aboutma[b[t[1].p]=++en]=t[1].v; $      for(intI=2; i<=n+m;++i) -       { -         if(t[i].v!=t[i-1].V) + +en; -ma[b[t[i].p]=en]=t[i].v; A } makeblock (); +      for(intI=1; i<=n+m;++i) the       if(A[i]. is) Insert (B[i]); -       Else $         { the           intnex=Next (B[i]); the           if(nex==-1) the             { thePuts"-1"); -               return 0; in             } theans+= (Long Long) Ma[nex]; the Delete (NEX); About         } theprintf"%lld\n", ans); the     return 0; the}

"Greedy", "two-dimensional partial order", "weight-value block" bzoj1691 [Usaco2007 Dec] Picky foodie

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.