序列和的前n小元素

序列和的前n小元素

Description

给出两个长度为n的有序表(非降序表)A和B, 在A和B中各任取一个相加, 可以得到n2个和. 求这些和最小的n个

Input

输入有两行内容,每行中都有n个整数,互相之间以一个空格相隔.

Output

输出只有一行内容,该行有n个从小到大的整数,代表得到的n个和, 互相之间以一个空格分隔.

Sample Input

1 3 5 7
2 4 6 8

Sample Output

3 5 5 7

HINT

对30%的数据,1≤n≤1000; 对100%的数据,1≤n≤100000; 对所有的数据,输入的2n个数在-109至109之间;

Source

//
//  d.cpp
//  noip
//
//  Created by lijian3256 on 2020/1/11.
//  Copyright © 2020 lijian3256. All rights reserved.
//
 
#include <bits/stdc++.h>
using namespace std;
  
const int maxcount = 100002;
  
int heap[maxcount];
int heapsize = 0;
  
  
void put(int n)
{
    heap[heapsize + 1] = n;
    heapsize ++;
    int now = heapsize;
    while(now > 1)
    {
        int parent = now >> 1;
        if(heap[parent] <= heap[now])
        {
            return;
        }
        else
        {
            swap(heap[parent], heap[now]);
            now = parent;
        }
    }
}
  
int get()
{
    if(heapsize < 1)
        {
            cout << "error";
            return -1;
        }
    int r = heap[1];
      
    heap[1] = heap[heapsize];
    heapsize--;
    int now = 1;
      
    while(now*2 <= heapsize)
    {
        int son1 = now * 2;
        int son2 = son1 + 1;
        int smallerson = son1;
        if(son2 <= heapsize)
        {
            if(heap[son1] > heap[son2])
            {
                smallerson = son2;
            }
            else
                smallerson = son1;
        }
          
        if(heap[smallerson] < heap[now])
        {
            swap(heap[smallerson], heap[now]);
            now = smallerson;
        }
        else
        {
            return r;
        }
    }
    return r;
}
  
struct T
{
    int n;
    int i;
    int j;
};
T  heap2[maxcount];
int heapsize2 = 0;
  
void put2(int n, int i, int j)
{
    T node;
    node.n = n;
    node.i = i;
    node.j = j;
    heap2[heapsize2 + 1] = node;
    heapsize2 ++;
    int now = heapsize2;
    while(now > 1)
    {
        int parent = now >> 1;
        if(heap2[parent].n <= heap2[now].n)
        {
            return;
        }
        else
        {
            swap(heap2[parent], heap2[now]);
            now = parent;
        }
    }
}
  
T get2()
{
    T r = heap2[1];
      
    heap2[1] = heap2[heapsize2];
    heapsize2--;
    int now = 1;
      
    while(now*2 <= heapsize2)
    {
        int son1 = now * 2;
        int son2 = son1 + 1;
        int smallerson = son1;
        if(son2 <= heapsize2)
        {
            if(heap2[son1].n > heap2[son2].n)
            {
                smallerson = son2;
            }
            else
                smallerson = son1;
        }
          
        if(heap2[smallerson].n < heap2[now].n)
        {
            swap(heap2[smallerson], heap2[now]);
            now = smallerson;
        }
        else
        {
            return r;
        }
    }
    return r;
}
  
int main()
{
    int a[maxcount];
    int b[maxcount];
      
    char temp;
    int d;
    int n = 0;
    while(true)
    {
        n++;
        scanf("%d", &d);
        put(d);
        scanf("%c", &temp);
        if(temp == ' ')
            continue;
        else
            break;
    }
    for(int i=1;i<=n;i++)
        a[i] = get();
  
    n = 0;
    while(true)
    {
        n++;
        scanf("%d", &d);
        put(d);
        scanf("%c", &temp);
        if(temp == ' ')
            continue;
        else
            break;
    }
    for(int i=1;i<=n;i++)
        b[i] = get();
  
  
  
          
//  cout << endl;
//  for(int i=1;i<=n;i++)
//      cout << a[i] << " ";
//  cout << endl;
//  for(int i=1;i<=n;i++)
//      cout << b[i] << " ";
    int mm[maxcount];
    memset(mm, 0, sizeof(mm));
    put2(a[1] + b[1], 1, 1);
    mm[1] = 1;
    for(int cc=1;cc<=n;cc++)
    {
        T t = get2();
        cout << t.n << " ";
        mm[t.i] = 0;
        if(mm[t.i+1] == 0)
        {
            put2(a[t.i+1] + b[t.j], t.i+1, t.j);
            mm[t.i+1] = t.j;
        }
        if(mm[t.i] == 0)
        {
            put2(a[t.i] + b[t.j+1], t.i, t.j+1);
            mm[t.i] = t.j + 1;
        }
    }
  
    return 0;
}
 
/**************************************************************
    Problem: 1496
    User: LJA001162
    Language: C++
    Result: 正确
    Time:100 ms
    Memory:4152 kb
****************************************************************/
posted @ 2019-12-22 11:17  牛大了的牛大  阅读(481)  评论(0编辑  收藏  举报