今天练习赛又碰见了这道水题,因为前几天刚做过所以想一A过掉,但是被卡了一个多小时,改了一个多小时才过,超不爽!以下是题目与及代码,坑点我会指出。
Description
Input
Output
Sample Input
1
2 3
1 2 3
2 2 3
Sample Output
3 3 4
/*这道题特别水,除了使用队列以外都是使用暴力*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int a[2007], b[2007];
priority_queue<int, vector<int>, less<int> >p;
void F( int m)
{
for( int i = 0, num = 0; i < m; i ++)
for( int j = 0; j < m; j ++)
{
if(num < m) // 先把前M个塞到最大优先出队的队里;
{
p.push(a[i] + b[j]);
num ++;
}
else if( a[i] + b[j] < p.top()) // 遇到和比队列P里最大值还要小的放进去,还要把原来的P里的最大值踢出来
{
p.pop();
p.push(a[i] + b[j]);
}
}
while(m --) // 从后往前放入a中
{
a[m] = p.top();
p.pop();
}
}
int main()
{
int t, n, m;
scanf("%d", &t);
while(t --)
{
scanf("%d %d", &n, &m); // 这里我的N与M对调了,纯属个人习惯, 感觉这样比较顺眼
for( int i = 0; i < m; i ++)
scanf("%d", &a[i]);
sort( a, a + m); // 这个就是坑点,具体什么原因我也不明白,因为我是有优先队列做的,就算不排序也应该,没问题,但是没有这步我就是死活不能过
while(-- n)
{
for( int i = 0; i < m; i ++)
scanf("%d", &b[i]);
F( m);
}
for( int i = 0; i < m; i ++)
{
if(i) printf(" ");
printf("%d", a[i]);
}
printf("\n");
}
return 0;
}