1245 the smallest n and
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description
Description
There are two sequences A and b of length N, each taking a number in A and B can get n^2 and, the n^2 and the smallest n.
Enter a description
Input Description
The first line enters a positive integer n, the second row n integers ai and ai≤10^9, and the third row n integer bi,
and bi≤10^9
Output description
Output Description
Output only one row, containing n integers, from small to large output these n smallest and between adjacent numbers with
separated by spaces.
Sample input
Sample Input
5
1 3 2) 4 5
6 3 4) 1 7
Sample output
Sample Output
2 3 4) 4 5
Data range and Tips
Data Size & Hint
"Data size" for 100% of data, meet 1≤n≤100000.
Category labels
Tags Click here to expandHeap tree structure
Exercises
The most violent method:
We can figure out all the things and then sort them out, and obviously space and time will explode.
On-Line ideas:
(In fact, it is not very clear that the i*j-1 of the first n solution is the optimal solution)
Try to get rid of some of the impossible States.
First, or a A, a, or the same table:
| b\a |
1 |
2 |
... | ..
I |
... | ..
N |
| 1 |
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
| ... |
|
|
|
|
|
|
| I |
|
|
|
|
|
|
| ... |
|
|
|
|
|
|
| N |
|
|
|
|
|
|
observed that for(I,JThis point, the element smaller than it has at leastIXJ−1 of them.
Since we are asking for the first n small, the point satisfying the requirement must satisfy at leastIxJ−1<n is ixj≤N.
So we can reduce the number of points to
⌊N1⌋+⌊N2⌋+...+⌊NI⌋+...+⌊NN⌋=O(N∑i= 1n1i) = o (nlog n)
Complexity of Time:O(nlog2n)
Space complexity:O(nlogn)
AC Code 1:
#include <cstdio>#include<algorithm>using namespacestd;#defineN 100010intn,cnt,a[n],b[n],c[n* -];intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d", A +i); for(intI=1; i<=n;i++) scanf ("%d", B +i); Sort (a+1, a+n+1); Sort (b+1, b+n+1); for(intI=1; i<=n;i++){ for(intj=1; i*j<=n;j++) {c[++cnt]=a[i]+B[j]; }} sort (c+1, c+cnt+1); for(intI=1; i<=n;i++) printf ("%d", C[i]); return 0;}
AC Code 2:
#include <cstdio> #include <algorithm> #include <queue> #include <vector>using namespace std;# Define N 100010int N,cnt,a[n],b[n];p riority_queue<int,vector<int>,greater<int> >que;int Main () { scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%d", a+i); for (int i=1;i<=n;i++) scanf ("%d", b+i); Sort (a+1,a+n+1); Sort (b+1,b+n+1); for (int i=1;i<=n;i++) {for (int j=1;i*j<=n;j++) { que.push (a[i]+b[j]); } } for (int i=1;i<=n;i++) { printf ("%d", que.top ()); Que.pop (); } return 0;}
1245 the smallest N and