[] P1631 sequences into luogu

Topic Link www

Look closely we will find the following rules

Fixing A [i], and every n are ordered:

A[1] + B[1], A[1] + B[2], …, A[1] + B[n]

A[2] + B[1], A[2] + B[2], …, A[2] + B[n]

A[n] + B[1], A[n] + B[2], …, A[n] + B[n]

analysis

That every time we need only consider the first of each line has not been included in the answer

We have established a small heap root, for the first time put the first number of each line added to the heap

The number of rows added to the heap and this number is located

Every time we export top of the heap, and found that the number of top of the heap where the line will be a number of top of the heap after heap to join (you can use an array of records each flag line takes into account the first few numbers)

In order to make the code more concise

We can use stl in prority_queue, convert it to a small heap root.

A pair type for storing a value for each element of the stack, and the row

code

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
long long a[100001],b[100001];
priority_queue<pair<long long,int> > qwq;//第一个存这个数的值 第二个存它在第几行
int read(){
    int a = 0,f = 0;char p = getchar();
    while(!isdigit(p)){f|=p=='-0';p = getchar();}
    while(isdigit(p)){a = (a<<3)+(a<<1)+(p^48);p = getchar();}
    return f?-a:a;
}
int flag[100001];//记录每一行到了第几个
int main(){
    int n;
    n = read();
    for(int i = 1;i <= n; i++)
        a[i] = read();
    for(int i = 1;i <= n;i++)b[i] = read();
    for(int i = 1;i <= n;i ++){
        qwq.push(make_pair(-a[i]-b[1],i));
        flag[i] = 1;
    }
    while(n--){
        cout<<-qwq.top().first<<" ";
        int x = qwq.top().second;//在第x行
        qwq.pop();
        flag[x]++;
        long long sum = a[x]+b[flag[x]];
        qwq.push(make_pair(-sum,x));
    }
    return 0;

}

Guess you like

Origin www.cnblogs.com/huixinxinw/p/12242330.html