11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums
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 of the integers. Your task is to find the k smallest sums among them.
Input
There will be several test cases. The first line of each case contains an integer k (2 ≤ k ≤ 750). Each of
the following k lines contains k positive integers in each array. Each of these integers does not exceed
1,000,000. The input is terminated by end-of-file (EOF).
Output
For each test case, print the k smallest sums, in ascending order.
Sample Input
3
1 8 5
9 2 5
10 7 6
2
1 1
1 2
Sample Output
9 10 12
2 2
题意:
给你k个数组,从每个数组里面取个数字,求和,让找k个最小值;
大神的优先队列,自己用好友的思路写了一遍,没考虑周全,wa了,然后借助大神的思路写了一遍就对了,大神是把复杂问题简单化,把k维转化为二维;
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=800;
typedef long long LL;
int mp[MAXN][MAXN];
struct Node{
int s,p;
Node(int s,int p):s(s),p(p){}
friend bool operator <(Node a,Node b){
return a.s>b.s;
}
};
void merge(int *a,int *b,int *c,int n){
sort(a,a+n);sort(b,b+n);
priority_queue<Node>dl;
for(int i=0;i<n;i++)dl.push(Node(a[i]+b[0],0));
for(int i=0;i<n;i++){
Node d=dl.top();dl.pop();
c[i]=d.s;
dl.push(Node(d.s-b[d.p]+b[d.p+1],d.p+1));
}
}
int main(){
int k;
while(~scanf("%d",&k)){
for(int i=0;i<k;i++)for(int j=0;j<k;j++)scanf("%d",&mp[i][j]);
for(int i=1;i<k;i++){
merge(mp[0],mp[i],mp[0],k);
}
for(int i=0;i<k;i++){
if(i)printf(" ");
printf("%d",mp[0][i]);
}
puts("");
}
return 0;}
刚开始没考虑周全的代码:
4
1 2 5 100
1 2 8 100
1 8 9 100
1 10 15 100
这组数据就没过。。
wa代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=800;
typedef long long LL;
priority_queue<int,vector<int>,greater<int> >dl,q;
int main(){
int k;
while(~scanf("%d",&k)){
while(!dl.empty())dl.pop();
while(!q.empty())q.pop();
int ans=0;
int x,a,b;
for(int i=0;i<k;i++){
for(int j=0;j<k;j++){
scanf("%d",&x);dl.push(x);
}
a=dl.top();
ans+=a;
dl.pop();
b=dl.top();
q.push(b-a);
// printf("%d %d\n",a,b);
while(!dl.empty())dl.pop();
}
for(int i=0;i<k;i++){
if(i)printf(" ");
if(!i)printf("%d",ans);
else printf("%d",ans+q.top()),q.pop();
}
puts("");
}
return 0;
}
11997 - K Smallest Sums(优先队列)的更多相关文章
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- UVa 11997 K Smallest Sums - 优先队列
题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...
- UVA 11997 K Smallest Sums 优先队列 多路合并
vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...
- uva 11997 K Smallest Sums 优先队列处理多路归并问题
题意:K个数组每组K个值,每次从一组中选一个,共K^k种,问前K个小的. 思路:优先队列处理多路归并,每个状态含有K个元素.详见刘汝佳算法指南. #include<iostream> #i ...
- 优先队列 UVA 11997 K Smallest Sums
题目传送门 题意:训练指南P189 分析:完全参考书上的思路,k^k的表弄成有序表: 表1:A1 + B1 <= A1 + B2 <= .... A1 + Bk 表2:A2 + B1 &l ...
- 【UVA 11997 K Smallest Sums】优先级队列
来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...
- 【UVA–11997 K Smallest Sums 】
·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意: 输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...
- UVA 11997 K Smallest Sums (多路归并)
从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...
- uva_11997,K Smallest Sums优先队列
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #inclu ...
随机推荐
- cookieless domain
概述 什么是cookieless domain?虽然名字中带有cookie,其实完全可以不使用cookie.这只是一种将网页中静态的文本,图片等的域名和主域名相区别开的方法. 主域名难免会使用到coo ...
- iOS:ABPeoplePickerNavigationController系统通讯录使用
昨天因项目需求要访问系统通讯录获取电话号码,于是乎从一无所知,开始倒腾,倒腾了一下午,总算了弄好了.写这边博客是为了记录一下,自己下一次弄的时候就别在出错了.同时,有和我一样的菜鸟能够避免走一下弯路. ...
- 浅谈Struts2(三)
一.Struts2收集client的参数 核心思路: <form method="post" action="XXXX"> <input ty ...
- JS学习之事件冒泡
(1)什么是事件起泡 首先你要明白一点,当一个事件发生的时候,该事件总是有一个事件源,即引发这个事件的对象,一个事件不能凭空产生,这就是事件的发生. 当事件发生后,这个事件就要开始传播.为什 ...
- 10247 - Complete Tree Labeling(递推高精度)
Problem B Complete Tree Labeling! Input: standard input Output: standard output Time Limit: 45 secon ...
- 基本event封装:阻止冒泡、默认事件等
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> ...
- A Byte of Python 笔记(6)模块
第8章 模块 用户在程序中定义一次函数而重用代码,如果用户想在其他程序中重用很多函数,可以通过使用模块的方式. 模块就是一个包含了所有用户定义的函数和变量的文件.为了在其他程序中重用模块,模块的文件名 ...
- selenium + python自动化测试环境搭建--亲测
环境准备: 1.下载所学安装包: setuptools https://pypi.python.org/packages/2.7/s/setuptools/ selenium https://pypi ...
- Webx pull service
1.概述 pull service的功能是将对象置入模板中.被pull service放到模板中的对象,不需要应用程序的干预即可直接使用.如果模板没有用到某个对象,则不会产生创建该对象的开销.看起来, ...
- chrome extension overview
目录 什么是扩展............................................................................................ ...