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