UVA 11997 - K最小的金额 (DP&转换&&优先队列) 好题
来源:互联网 发布:告白墙源码 编辑:程序博客网 时间:2024/06/06 15:04
根
11997 - K最小的金额
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 test cases. The first line of each case contains an integer k (2 ≤ k ≤ 750). Each ofthe following k lines contains k positive integers in each array. Each of these integers does not exceed1,000,000. The input is terminated by end-of-file (EOF).OutputFor 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
//写了一早上,都没写出来,看了大神的博客,原来大神是用优先队列写的。。。
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>#define N 800#define INF 0x3f3f3f3f using namespace std;int map[N][N];struct zz{int s;int p;zz(int s,int p):s(s),p(p){}friend bool operator<(zz a,zz 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<zz>q;for(int i=0;i<n;i++)q.push(zz(a[i]+b[0],0));for(int i=0;i<n;i++){zz d=q.top();q.pop();c[i]=d.s;q.push(zz(d.s-b[d.p]+b[d.p+1],d.p+1));}}int main(){int n,i,j;while(scanf("%d",&n)!=EOF){for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&map[i][j]);for(i=1;i<n;i++){merge(map[0],map[i],map[0],n);}for(i=0;i<n;i++){if(i)printf(" ");printf("%d",map[0][i]);}printf("\n");}return 0;}
//这个代码一直WA,不知为啥。。。经过队友提醒才明白,这组数据过不了。。。
4
1 2 5 100
1 2 8 100
1 8 9 100
1 10 15 100
#include<stdio.h>#include<string.h>#define N 800#define ll long long#include<algorithm>using namespace std;int a[800][800];int sum[6400];int main(){int n,i,j,k,m;while(scanf("%d",&n)!=EOF){memset(sum,0,sizeof(sum));for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<n;i++)sort(a[i],a[i]+n);//for(i=0;i<n;i++)//{//for(j=0;j<n;j++)//printf("%d ",a[i][j]);//printf("\n");//}int num=0;for(i=0;i<n;i++)num+=a[i][0];//printf("--%d\n",num);k=0;for(i=0;i<n;i++)for(j=1;j<n;j++)sum[k++]=a[i][j]-a[i][0];sort(sum,sum+k);//for(i=0;i<k;i++)//printf("%d--",sum[i]);//printf("\n");printf("%d",num);for(i=0;i<n-1;i++)printf(" %d",num+sum[i]);printf("\n");}return 0;}
- UVA 11997 - K最小的金额 (DP&转换&&优先队列) 好题
- uva 11997 k个最小和(优先队列实现多路归并)题解
- 找最小的k个数(优先队列)
- UVA 11997 K Smallest Sums(优先队列)
- UVA 11997 K Smallest Sums(优先队列)
- UVA 11997 K Smallest Sums(优先队列)
- UVa 11997 K Smallest Sums / 优先队列
- UVA 11997 K Smallest Sums 优先队列
- UVA 11997 K Smallest Sums(优先队列)
- uva 11997 - K Smallest Sums(优先队列)
- UVA 11997 - K Smallest Sums 优先队列
- uva 11997 K Smallest Sums 优先队列
- UVa 11997 K Smallest Sums (优先队列)
- codeforce 754 D k个区间的交的尽可能的大 (优先队列) 贪心好题!!!
- UVA 11997 - K Smallest Sums(优先队列+多路合并)
- UVA - 11997 K Smallest Sums 归并排序+优先队列
- 【优先队列之多路合并】UVA - 11997 K Smallest Sums
- K Smallest Sums(Uva 11997) 多路归并+优先队列
- shell中各种括号的作用()、(())、[]、[[]]、{}
- OpenGL ES 学习教程(四) 买来墙纸,给立方体贴上!
- 内存对齐
- 关于redis、memcache、mongoDB 的对比
- android5.0应用显示安装未完成问题解决方案
- UVA 11997 - K最小的金额 (DP&转换&&优先队列) 好题
- linux管道pipe
- iOS页面间的几种传值方式
- 【PR系列之002-1104959】Google输入法建议词列表PopupWindow遮挡输入词,建议词列表PopupWindow无法上下滑动
- js 等待几秒 每隔几秒 执行
- 多媒体切片与http协议传输
- IOS学习之CocoaPods使用详细说明(相当于Maven搭建环境导入统一管理的jar包)
- Android 弹无虚发之第四弹:你应该掌握的Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
- 框架搭建-SSI搭建步骤详解版本二