洛谷P1631 序列合并

序列合并,将a1b1入队,然后从当前节点aibj开始扩展,扩展到ai+1bj和aibj+1,扔到优先队列里,节点可能被重复扩展,设置数字t,存储当点ai已经扩展到哪个节点。交上去之后40分。

#include
#include
#include
#include 
using namespace std; 
const int N=1E5+10;
struct node{
    int x,y,val;
    bool operator <( node b)const{
        return val>b.val;
    }
    node(int i,int j,int k):x(i),y(j),val(k){}
};
priority_queuepq;
int a[N],b[N],t[N],n; 
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);
    a[n+1]=b[n+1]=2e9+10;
    pq.push({1,1,a[1]+b[1]});	
    t[1]=1;
    for(int i=1;i<=n;i++){
        node tmp=pq.top();
        pq.pop();
        printf("%d ",tmp.val);
        if(t[tmp.x]

自己想不明白原因,但应该是扩展或去重写错了,修改成set判重,过了

for(int i=1; i<=n; i++) {
		node tmp=pq.top();
		pq.pop();
		printf("%d ",tmp.val);
		if(tmp.y+1>t[tmp.x]) {
			for(int j=t[tmp.x]+1; j<=n&&j<=tmp.y+1; j++)
				pq.push({tmp.x,j,a[tmp.x]+b[j]});
			t[tmp.x]=tmp.y+1;
		}
		if(tmp.y>t[tmp.x+1]) {
			for(int j=t[tmp.x+1]+1; tmp.x+1<=n&&j<=n&&j<=tmp.y; j++)
				pq.push({tmp.x+1,j,a[tmp.x+1]+b[j]});
			t[tmp.x+1]=tmp.y;
		}
	}

前面的代码错在哪儿呢,查找数据发现扩展的时候有些节点漏了,比如扩展i+1,j时候,可能前面的i+1,j-1还没有扩展呢.修改后,过了

for(int i=1; i<=n; i++) {
		node tmp=pq.top();
		pq.pop();
		printf("%d ",tmp.val);
		if(tmp.y+1>t[tmp.x]) {
			for(int j=t[tmp.x]+1; j<=n&&j<=tmp.y+1; j++)
				pq.push({tmp.x,j,a[tmp.x]+b[j]});
			t[tmp.x]=tmp.y+1;
		}
		if(tmp.y>t[tmp.x+1]) {
			for(int j=t[tmp.x+1]+1; tmp.x+1<=n&&j<=n&&j<=tmp.y; j++)
				pq.push({tmp.x+1,j,a[tmp.x+1]+b[j]});
			t[tmp.x+1]=tmp.y;
		}
	}

 

你可能感兴趣的:(数据结构,u)