Codeforces Round #251 (Div. 2) D binary

Source: Internet
Author: User

Codeforces Round #251 (Div. 2) D binary

 

It is a good question. First of all, it is not difficult to draw a few more questions. To meet the question conditions, it is possible that the minimum value of array a is increasing, or the maximum value of array B is decreasing, in the beginning, the priority queue was used directly, and the error was found. Because there were not two situations at a time, either a plus or B minus, it was difficult to draw a few more pictures, we need to find that all elements in a with the value of x are greater than or equal to x, and all elements in B are less than or equal to x, so we only need to find this x, according to a few more draws, it is found that x is one of the elements in a and B. Therefore, if multiple paintings are correct, it is directly searched in the array a and B, then, perform a binary search for the answer, and then obtain the final smallest answer. You need to maintain the prefix of a and the suffix of B.

 

 

#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        #include
         
          #include
          
           #include
           
            #include
            
             #include
             
              #define ll long long#define eps 1e-8const int inf = 0xfffffff;const ll INF = 1ll<<61;using namespace std;//vector
              
                > G;//typedef pair
               
                 P;//vector
                
                  > ::iterator iter;////map
                 
                  mp;//map
                  
                   ::iterator p;int n,m;ll ans;ll aa[100000 + 55],bb[100000 + 55];ll sa[100000 + 55],sb[100000 + 55];vector
                   
                     G;void init() {memset(aa,0,sizeof(aa));memset(bb,0,sizeof(bb));memset(sa,0,sizeof(sa));memset(sb,0,sizeof(sb));G.clear();}bool input() {while(scanf(%d %d,&n,&m) == 2) {for(int i=1;i<=n;i++) {scanf(%I64d,&aa[i]);G.push_back(aa[i]);}for(int i=1;i<=m;i++) {scanf(%I64d,&bb[i]);G.push_back(bb[i]);}return false;}return true;}ll find(ll x) {ll sum = 0;int pos = lower_bound(aa + 1,aa + n + 1,x) - aa;sum += (pos - 1) * x - sa[pos - 1];pos = lower_bound(bb + 1,bb + m + 1,x) - bb;sum += sb[pos] - (m - pos + 1) * x;return sum;}void cal() {sort(aa + 1,aa + n + 1);sort(bb + 1,bb + m + 1);sort(G.begin(),G.end());for(int i=1;i<=n;i++)sa[i] = sa[i - 1] + aa[i];for(int i=m;i>0;i--)sb[i] = sb[i + 1] + bb[i];ans = INF;for(int i=0;i
                    
                     

 

 

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.