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 ...
随机推荐
- input propertyChange
結合 HTML5 標準事件 oninput 和 IE 專屬事件 onpropertychange 事件來監聽輸入框值變化. oninput 是 HTML5 的標準事件,對於檢測 textarea, i ...
- Kattis - sortofsorting 【排序】
题意 给出一系列字符串,然后要排序 排序规则 只按前两位按字典序来排序,如果前两位完全一样,则按输入的顺序来排 思路 要用 冒泡排序 不能用STL里面的 SORT 因为它不稳定 AC代码 #inclu ...
- 基于 GitHub 搭建/创建自己博客 DIY
此博客主要实现通过github创建个人定制的博客的功能,主要参考如下两篇文章,再次感谢. 创建GitHub技术博客全攻略 “授人以渔”的教你搭建个人独立博客 [说明]:使用本文的正确方式是参考上述两篇 ...
- keepalived nginx 主备配置
keepalived nginx 主备配置(多主多备同理) 1.Nginx服务安装 nginx 不区分主备,在两台服务上安装两个即可. 安装参考:https://www.cnblogs.com/zw ...
- python中完善decorator
@decorator可以动态实现函数功能的增加,但是,经过@decorator“改造”后的函数,和原函数相比,除了功能多一点外,有没有其它不同的地方? 在没有decorator的情况下,打印函数名: ...
- 为多个文件夹下的C源代码编写Makefile文件
上一篇文章写了如何为在同一个文件夹下的C源代码,本篇文章为多个文件夹下的C源代码编写Makefile文件. 建立两个文件夹,分别为abs与src.其最终目录结构如下: 1 $ ls * 2 jun.c ...
- 关于v4l2的一点变更
先打个连接 http://linuxtv.org/downloads/presentations/media_ws_2013/v4l2-multi-format.pdf 2013年linux 多媒体构 ...
- 山东省第六届ACM省赛 H---Square Number 【思考】
题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, ...
- centos 源码安装php5.5
系统环境: CentOS 6.5 / 7.0 x86_64 Fedora 20 x86_64下载 PHP 源码包 # wget http://cn2.php.net/distributions/php ...
- Myeclipse 快捷键使用
MyEclipse快捷键大全-------------------------------------MyEclipse 快捷键1(CTRL)----------------------------- ...