uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-10;
const int INF=1000000000;
const int maxn=750+10;
int n,a[maxn][maxn];
struct node
{
    int s,b;
    node(int s,int b): s(s),b(b){}
    bool operator < (const node &x) const
    {
        return s>x.s;
    }
};
priority_queue<node>pq;
void he(int *A,int *B,int *C)
{
    while(!pq.empty()) pq.pop();
    for(int i=0; i<n; i++)
    {
        pq.push(node(A[i]+B[0],0));
    }
    for(int i=0; i<n; i++)
    {
        node t=pq.top();
        pq.pop();
        C[i]=t.s;
        pq.push(node(t.s-B[t.b]+B[t.b+1],t.b+1));
    }
}
int main()
{
    //freopen("in1.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d",&n)==1)
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                scanf("%d",&a[i][j]);
            }
            sort(a[i],a[i]+n);
        }
        for(int i=1; i<n; i++)
        {
            he(a[0],a[i],a[0]);
        }
        for(int i=0; i<n-1; i++) printf("%d ",a[0][i]);
        printf("%d\n",a[0][n-1]);
    }
    return 0;
}

uva11997

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-10;
const int INF=1000000000;
const int maxn=1000+10;
int k,a,b,ans[10000+10];
string s;
struct node
{
    int num,t,p;
    node (int num,int t,int p):num(num),t(t),p(p){}
    bool operator <(const node &x)const
    {
        if(t!=x.t)
        {
            return t>x.t;
        }
        else
        {
            return num>x.num;
        }
    }
};
priority_queue<node>pq;
int main()
{
    //freopen("in1.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(cin>>s)
    {
        if(s[0]==‘#‘)
        {
            scanf("%d",&k);
            for(int i=0;i<k;i++)
            {
                node tmp=pq.top();
                pq.pop();
                ans[i]=tmp.num;
                pq.push(node(tmp.num,tmp.t+tmp.p,tmp.p));
            }
        }
        else
        {
            scanf("%d%d",&a,&b);
            pq.push(node(a,b,b));
        }
        for(int i=0;i<k;i++)
        {
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}

uvalive 3135

时间: 2024-10-29 15:50:08

uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)的相关文章

【暑假】[实用数据结构]UVa11997 K Smallest Sums

UVa11997 K Smallest Sums  题目: K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly one element in each array and calculate the sum of the integers. Your task is to find the k smallest sums among them. In

题解——UVA11997 K Smallest Sums

题面 背景 输入 输出 翻译(渣自翻) 给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来 题解 k路归并问题的经典问题 可以转化为二路归并问题求解 考虑A[],B[]两个有序数组 使用堆,记录一些二元组\( (x,y) \),x表示值,y表示对应的b的下标,因为我们是把b合并到a上,所以我们能够根据记录的下标推出后面的值 然后两两合并 所以就很简单 放代码 #include <cstdio> #include <algorithm> #inc

UVALive - 3135 - Argus (优先队列!!)

UVALive - 3135 Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financi

【优先队列之多路归并】UVALive 3135 Argus

UVALive 3135 Argus http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18684 题意 编写一个系统执行一系列Register命令:Register Q_num Period,每个命令执行周期是Period,执行事件Q_num:如果事件同时发生,优先执行Q_num小的. 示例 Sample Input Register 2004 200 Register 2005 300 # 5 Sample Output

11997 - K Smallest Sums(优先队列)

11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick exactly one element in eacharray and calculate the sum of the integers. Your task is to find the k smallest sums among them.InputThere will be several

UVa 11997 K Smallest Sums 优先队列&amp;&amp;打有序表&amp;&amp;归并

UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status You're given k arrays, each array has k integers. There are k^k ways to pick exactly one element in each array and calculate the sum o

【优先队列之多路合并】UVA - 11997 K Smallest Sums

Source : UVA - 11997 K Smallest Sums http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意 有k个整数数组,各包含k个元素,从每个数组中选取一个元素加起来,可以得到k^k个和,求这些和中最小的k个值. 示例 Sample Input 3 1 8 5 9 2 5 10 7 6 2 1 1 1 2 Sample Output 9 10 12 2 2 思路 二路归并构建二维平面表:

【暑假】[实用数据结构]UVAlive 3135 Argus

UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, fin

UVA 11997 The K smallest Sums

给出K*K的矩阵,每一行取一个数,构成K个数的和,总共有 k^k种可能,从中取出前k个最小的. 一开始犯了错,因为只要对每行排序,最小的必定是第一列的和,然后我当时就想着,逐步推进,每次将某行的那个数变成其下一列那个数,当然间距要最小.我这样明显是不对的,这样的话每个数只用了一次,而题目的意思明显是可以重复多次. 然后大白上说的是把他看成两行,即每次处理两行,留下最小的k个 再与下一次读入的那一行继续处理.这个方法相当给力,不过有个难点是,怎么在两行的时候,比较快的得到前k小的,我试过全部算一遍