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;
}

  

最新文章

  1. Python之路,Day5 - Python基础5
  2. webpack --- 详解
  3. Eclipse的自动排版设置(format)
  4. s3c2440 移值u-boot-2016.03 第5篇 支持dm9000 识别
  5. jsp页面揣出现Invalid action class configuration that references an unknown class解决方案
  6. 怎么学习C++?
  7. Cinemagraph
  8. js优化提升访问速度
  9. .NET4.0下网站应用法度用UrlRewriter.dll重写无后缀路径 (在IIS7.5中的设备办法)
  10. asp动态生成google的sitemap地图的代码
  11. Linux相关指令
  12. 解决Cygwin中vim的backspace不能正常使用(转)
  13. (原创)speex与wav格式音频文件的互相转换(二)
  14. shell编程的一些例子4
  15. 组合索引leaf 数据存储
  16. 纠错《COM技术内幕》之ProgID
  17. html5中将图片的绝对路径转换成文件对象
  18. 在ConcurrentModificationException异常上的联想
  19. Mac下通过VMware Fusion安装centos虚拟机操作记录
  20. [No0000168]Excle只允许用户输入纯文本,禁止用户修改单元格样式、格式等

热门文章

  1. nbtstat 查询IP地址对应的计算机名称
  2. 如何:打开 IIS 管理器
  3. BootStrap 智能表单系列 十一 级联下拉的支持
  4. iOS 技能集结号
  5. ajax 上传图片 并预览
  6. Cocos2d-x基础篇C++
  7. Snap.svg中transform旋转值的“r+数组”表现形式
  8. iter, yield与enumerate的实现
  9. Word2007中如何插入参考文献
  10. Lotus Sametime 服务器的安装和配置