The smallest n and Codevs no.1245

Source: Internet
Author: User

2016-05-31 18:52:15

Topic Links: Smallest N and Codevs no.1245

Main topic:

Give two equal-length numbers, sum each number to find the smallest n group

Solution:

Heap optimization of the great violence

Directly enumerates all possible updates in the maximum heap, deleting the maximum combination

Places to be aware of:

1. If any one addend is greater than or equal to the top element of the heap, break

2. If the sum of the two is greater than or equal to the top element of the heap, break

1 //the smallest n and (Codevs no.1245)2 //Heap (priority queue)3#include <stdio.h>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intmaxn=100010;8Priority_queue <int>Q;9 intN;Ten intX[MAXN]; One intY[MAXN]; A intANS[MAXN]; - intMain () - { thescanf"%d",&N); -      for(intI=1; i<=n;i++) -     { -scanf"%d",&x[i]); +     } -      for(intj=1; j<=n;j++) +     { Ascanf"%d",&y[j]); at     } -Sort (x+1, x+n+1); -Sort (y+1, y+n+1); -      for(intI=1; i<=n;i++) Q.push (x[i]+y[i]); -      for(intI=1; i<=n;i++) -     { in         if(X[i]>=q.top ()) Break; -          for(intj=1; j<=n;j++) to         { +             if(I==J)Continue; -             if(Y[j]+x[i]>=q.top ()) Break; theQ.push (x[i]+y[j]); * Q.pop (); $         }Panax Notoginseng     } -      for(intI=1; i<=n;i++) the     { +ans[i]=q.top (); A Q.pop (); the     } +      for(inti=n;i>=1; i--) -     { $printf"%d", Ans[i]); $     } -     return 0; -}

The smallest n and Codevs no.1245

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.