三月份阿里实习招聘的时候面试了一道堆排序的算法题,奈何太菜,做不出来被刷了,现在重新拿起这道题,同时也练习一下c++中优先级队列的使用。
/************************************************************************/
/* 给定两个升序数组A和B,定义一个集合中的元素为A中的元素和B中的元素相加,*/
/* 求集合中最小的K个数 */
/* 思路:利用堆排序,堆中存储A+B的元素和,并且存储对应的A数组下标和B数组下标*/
/* 每次弹出一个最小的元素,根据该元素的两个下标(i,j),分别将A[i]+B[j+1]和A[i+1]+B[j]两个元素入堆*/
/* 可以保证入堆的两个元素是剩余结果中较小的两个值,算法复杂度可以为KlogK*/
/* 测试数据 A:1 3 4 5 7 8 10 */
/* B : 2 3 5 5 6 7 9 11 */
/* 求最小的K=6个数 */
/************************************************************************/
#include<iostream>
#include<queue>
using namespace std;
struct Elem
{
int x, y, val;
bool operator <(const Elem& elem)const
{
return val > elem.val;
}
};
int main()
{
int A[] = { 1, 3, 4, 5, 7, 8, 10,11 };
int B[] = { 2, 3, 5, 5, 6, 7, 9, 11 };
int len1=11,len2 = 11;
int K = 10;
priority_queue<Elem>que;
que.push(Elem{ 0, 0, A[0] + B[0] });
int preVal=-1;
while (!que.empty()&& K)
{
Elem temp = que.top();
que.pop();
int x = temp.x, y = temp.y, val = temp.val;
if (x < len1 - 1)
que.push(Elem{ x + 1, y, A[x + 1] + B[y] });
if (y < len2 - 1)
que.push(Elem{ x, y+1, A[x] + B[y+1] });
if (val != preVal)
{
preVal = val;
K--;
cout << val << " ";
}
}
cout << endl;
return 0;
}
另外最近看到google APAC test 2017的一道题也可以用堆排序来做,记录如下。题目为:
Problem
There’s an island in the sea. The island can be described as a matrix with R rows and C columns, with H[i][j] indicating the height of each unit cell. Following is an example of a 3*3 island:
3 5 5
5 4 5
5 5 5
Sometimes, a heavy rain falls evenly on every cell of this island. You can assume that an arbitrarily large amount of water falls. After such a heavy rain, some areas of the island (formed of one or more unit cells joined along edges) might collect water. This can only happen if, wherever a cell in that area shares an edge (not just a corner) with a cell outside of that area, the cell outside of that area has a larger height. (The surrounding sea counts as an infinite grid of cells with height 0.) Otherwise, water will always flow away into one or more of the neighboring areas (for our purposes, it doesn’t matter which) and eventually out to sea. You may assume that the height of the sea never changes. We will use W[i][j] to denote the heights of the island’s cells after a heavy rain. Here are the heights of the example island after a heavy rain. The cell with initial height 4 only borders cells with higher initial heights, so water will collect in it, raising its height to 5. After that, there are no more areas surrounded by higher cells, so no more water will collect. Again, note that water cannot flow directly between cells that intersect only at their corners; water must flow along shared edges.
Following is the height of the example island after rain:
3 5 5
5 5 5
5 5 5
Given the matrix of the island, can you calculate the total increased height sum(W[i][j]-H[i][j]) after a heavy rain?
Input
The first line of the input gives the number of test cases, T. T test cases follow.
The first line of each test case contains two numbers R and C indicating the number of rows and columns of cells on the island. Then, there are R lines of C positive integers each. The j-th value on the i-th of these lines gives H[i][j]: the height of the cell in the i-th row and the j-th column.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the total increased height.
Limits
1 ≤ T ≤ 100.
1 ≤ H[i][j] ≤ 1000.
Small dataset
1 ≤ R ≤ 10.
1 ≤ C ≤ 10.
Large dataset
1 ≤ R ≤ 50.
1 ≤ C ≤ 50.
Sample
Input
Output
3
3 3
3 5 5
5 4 5
5 5 5
4 4
5 5 5 1
5 1 1 5
5 1 5 5
5 2 5 8
4 3
2 2 2
2 1 2
2 1 2
2 1 2
Case #1: 1
Case #2: 3
Case #3: 0
Case 1 is explained in the statement.
In case 2, the island looks like this after the rain:
5 5 5 1
5 2 2 5
5 2 5 5
5 2 5 8
Case 3 remains unchanged after the rain.
代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <math.h>
using namespace std;
int a[50][50];
int flag[50][50];
int C[4] = { 1, -1, 0, 0 };
int D[4] = { 0, 0, 1, -1 };
struct Island
{
int val;
int x;
int y;
};
bool operator < (const Island& lhs, const Island& rhs)
{
return lhs.val > rhs.val;
}
int main()
{
int T;
while (cin >> T)
{
int row, col,k;
for (k = 0; k < T; k++)
{
memset(a, 0, sizeof(a));
memset(flag, 0, sizeof(flag));
cin >> row >> col;
priority_queue<Island>que;
int sum = 0;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
{
cin >> a[i][j];
if (i == 0 || i == row - 1 || j == 0 || j == col - 1)
{
que.push(Island{a[i][j],i,j});
flag[i][j] = 1;
}
}
while (!que.empty())
{
Island temp = que.top();
que.pop();
for (int i = 0; i < 4; i++)
{
int xx = temp.x + C[i];
int yy = temp.y + D[i];
if (xx > 0 && xx < row - 1 && yy>0 && yy < col - 1 && !flag[xx][yy])
{
if (a[xx][yy] < temp.val)
{
sum += (temp.val - a[xx][yy]);
a[xx][yy] = temp.val;
}
flag[xx][yy] = 1;
que.push(Island{ a[xx][yy], xx, yy });
}
}
}
cout << "Case #" << k + 1 << ": " << sum << endl;
}
}
return 0;
}