P1631 and p1631
Description
There are two sequences A and B whose lengths are all N. In A and B, we can get N ^ 2 and N ^ 2 and the N ^ 2 and the N smallest N.
Input/Output Format
Input Format:
The first line is a positive integer N;
The second line contains N integers: Ai <= Ai + 1 and Ai <= 10 ^ 9;
The third line contains N integer Bi, meeting the requirements of Bi <= Bi + 1 and Bi <= 10 ^ 9.
[Data scale]
1 <= N <= 50%;
For 100% of the data, 1 <=n <= 100000.
Output Format:
The output contains only one row and N integers. The smallest sum of N values is output from small to large. Adjacent numbers are separated by spaces.
Input and Output sample input sample #1:
32 6 61 4 8
Output sample #1:
3 6 7
Note that
Priority_queue is slower than heap
Make_heap is slower than pop_heap
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 const int MAXN=1000001; 9 priority_queue<int>q;10 void read(int & n)11 {12 char c='+';int x=0;int flag=0;13 while(c<'0'||c>'9')14 {15 c=getchar();16 if(c=='-')flag=1;17 }18 19 while(c>='0'&&c<='9')20 x=x*10+c-48,c=getchar();21 if(flag==1)n=-x;22 else n=x;23 }24 int n,tot;25 int a[MAXN],b[MAXN],c[MAXN];26 int main()27 {28 read(n);29 for(int i=1;i<=n;i++)30 read(a[i]);31 for(int i=1;i<=n;i++)32 read(b[i]);33 for(int i=1;i<=n;i++)34 c[i]=a[1]+b[i];35 make_heap(c+1,c+n+1);36 for(int i=2;i<=n;i++)37 {38 for(int j=1;j<=n;j++)39 {40 if(a[i]+b[j]<c[1])41 {42 //q.push(a[i]+b[j]);43 c[1]=a[i]+b[j];44 pop_heap(c+1,c+n+1);45 }46 else break;47 }48 }49 //priority_queue<int,vector<int>,greater<int> >p;50 make_heap(c+1,c+n+1,greater<int>());51 int k=n;52 for(int i=1;i<=n;i++)53 {54 printf("%d ",c[1]);55 pop_heap(c+1,c+1+k,greater<int>());56 k--;57 }58 return 0;59 }