2020年蓝桥杯全国校赛 节目安排问题
【问题描述】小明要组织一台晚会,总共准备了 n 个节目。然后晚会的时间有限,他只能最终选择其中的 m 个节目。这 n 个节目是按照小明设想的顺序给定的,顺序不能改变。小明发现,观众对于晚会的喜欢程度与前几个节目的好看程度有非常大的关系,他希望选出的第一个节目尽可能好看,在此前提下希望第二个节目尽可能好看,依次类推。小明给每个节目定义了一个好看值,请你帮助小明选择出 m 个节目,满足他的要求。【输入格式】输入的第一行包含两个整数 n, m ,表示节目的数量和要选择的数量。第二行包含 n 个整数,依次为每个节目的好看值。【输出格式】输出一行包含 m 个整数,为选出的节目的好看值。【样例输入】5 33 1 2 5 4【样例输出】3 5 4【样例说明】选择了第1, 4, 5个节目。【评测用例规模与约定】对于 30% 的评测用例,1 <= n <= 20;对于 60% 的评测用例,1 <= n <= 100;
对于所有评测用例,1 <= n <= 100000,0 <= 节目的好看值 <= 100000。
#include
#include
using namespace std;
#define N 1000
struct edge//创建节点结构体
{
int month;
int x;
int y;
};
queue qu;//创建一个队列
int main()
{
int n, m, k;
cin >> n >> m;
char b[N][N];
for (int i = 1; i < n+1; i++)
{
for (int j = 1; j < m + 1; j++)
{
char ch;
cin >> ch;
if (ch == 'g')
{
edge temp;
temp.month = 0;
temp.x = i;
temp.y = j;
qu.push(temp);//长草的进队
}
}
}
cin >> k;
while (!qu.empty())
{
edge u;
u = qu.front();
b[u.x][u.y] = 'g';//标识已长草的结点
qu.pop();//出队
if (u.month < k)//在规定月数下新增长草的结点
{
if (u.x -1> 0)
{
edge tempt;
tempt.x = u.x - 1;
tempt.y = u.y;
tempt.month = u.month + 1;
qu.push(tempt);
}
if (u.x + 1 <= n)
{
edge tempt;
tempt.x = u.x + 1;
tempt.y = u.y;
tempt.month = u.month + 1;
qu.push(tempt);
}
if (u.y - 1 > 0)
{
edge tempt;
tempt.x = u.x;
tempt.y = u.y - 1;
tempt.month = u.month + 1;
qu.push(tempt);
}
if (u.y + 1 <= m)
{
edge tempt;
tempt.x = u.x;
tempt.y = u.y + 1;
tempt.month = u.month + 1;
qu.push(tempt);
}
}
}
for (int i = 1; i < n + 1; i++)//输出最终的已长草的表格
{
for (int j = 1; j < m + 1; j++)
{
if (b[i][j] != 'g')
cout << '.';
else
cout << b[i][j];
}
cout << endl;
}
return 0;
}