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-;
const int INF=;
const int maxn=+;
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=; i<n; i++)
{
pq.push(node(A[i]+B[],));
}
for(int i=; i<n; i++)
{
node t=pq.top();
pq.pop();
C[i]=t.s;
pq.push(node(t.s-B[t.b]+B[t.b+],t.b+));
}
}
int main()
{
//freopen("in1.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)==)
{
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
scanf("%d",&a[i][j]);
}
sort(a[i],a[i]+n);
}
for(int i=; i<n; i++)
{
he(a[],a[i],a[]);
}
for(int i=; i<n-; i++) printf("%d ",a[][i]);
printf("%d\n",a[][n-]);
}
return ;
}
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-;
const int INF=;
const int maxn=+;
int k,a,b,ans[+];
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[]=='#')
{
scanf("%d",&k);
for(int i=;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=;i<k;i++)
{
printf("%d\n",ans[i]);
}
}
return ;
}
uvalive 3135
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. Ther ...
- UVA-11997 K Smallest Sums
UVA - 11997 K Smallest Sums Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- UVA11997 K Smallest Sums
思路 经典的k路归并问题 问题先转换为2路的有序表归并 先让A[1~k]都和B[1]相加,然后加入堆中,取出堆顶(A[x]+B[y])之后,再放入A[x]+B[y+1] 代码 #include < ...
- 题解——UVA11997 K Smallest Sums
题面 背景 输入 输出 翻译(渣自翻) 给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来 题解 k路归并问题的经典问题 可以转化为二路归并问题求解 考虑A[], ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- D - K Smallest Sums(多路归并+贪心)
Problem K K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pic ...
- 373. Find K Pairs with Smallest Sums (java,优先队列)
题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...
随机推荐
- pandas(零)数据结构
pandas的两个主要的数据结构: Series series是一种类似于一维数组的对象,它由一组数据(NumPy数组类型的数据)和一组与之相关的数据标签(索引)组成. from pandas imp ...
- 第一次java小考心得体会
本周四Java第一次上课,测试了一个ATM程序,从两点半开始到五点半结束. 整整三个小时,结果怎么说呢,大概可以用惨不忍睹来形容吧. 因为之前有一个测试样卷,以为考试内容不会变的我,只是把学生信息管理 ...
- HDF及HDF-EOS数据格式简介
HDF-EOS数据格式介绍 HDF(Hierarchy Data Format )数据格式是美国伊利诺伊大学国家超级计算应用中心(NCSA ,National Central for Super co ...
- python多线程编程(3): 使用互斥锁同步线程
问题的提出 上一节的例子中,每个线程互相独立,相互之间没有任何关系.现在假设这样一个例子:有一个全局的计数num,每个线程获取这个全局的计数,根据num进行一些处理,然后将num加1.很容易写出这样的 ...
- Linux服务器iops性能测试-fio
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...
- HTML系列(1)简介
开始整理html的知识. (1)HTML HTML 是用来描述网页的一种语言. 1.HTML指的是超文本标记语言: HyperText Markup Language 2.HTML不是 ...
- 【leetcode刷题笔记】Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 嵌入式C函数优化
0. 引言 这是一个简单函数的优化,但却体现了代码易读性和效率的综合考虑. 如果问我如何写出优秀的代码,答曰:再写一版. 1. 版本1 从环形buffer中取出数据,然后放到一个结构体中.buffer ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:创建UIHelper通用自定义分页和选择开关与PagesHelper和IsSelect简单用法
@helper放入地方 分页效果 选择开关编辑调用 <dl> <dd class="dc1">是否主管:</dd> <dd> @UI ...
- java基础之常量与变量
概要:通过这段时间的工作,发现自己的基础还是很薄弱的,so,you know 常量 一种特殊的变量,程序运行过程中不能改变的值 语法格式:final 数据类型 常量名称 = 常量值 例子:fina i ...