基础题,重点是使用最小的成本完成任务,即对每一发导弹,如果有能拦截它的导弹拦截系统,选择其中最大拦截高度最低的系统;如果没有,建立新的导弹拦截系统。
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 100000;
int high[MAXN]; //所有导弹拦截系统的最大拦截高度
int main()
{
int N;
while (cin >> N)
{
memset(high, 0, sizeof(high));
int num = 0; //导弹拦截系统的数量
int temp; //导弹的高度
/*对每一发导弹,如果有能拦截它的导弹拦截系统,选择其中
最大拦截高度最低的系统;如果没有,建立新的导弹拦截系统*/
for (int i = 0; i < N; i++)
{
cin >> temp;
int j;
int index = num; //选择的拦截系统的编号
for (j = 0; j < num; j++) //查找有无能拦截该导弹的系统
{
if (high[j] >= temp)
{
index = j;
break;
}
}
for (j = index + 1; j < num; j++) //查找有无更优的系统
{
if (high[j] >= temp && high[j] < high[index])
index = j;
}
if (index == num) //建立新的导弹拦截系统
{
high[index] = temp;
++num;
}
else
high[index] = temp;
}
cout << num << endl;
}
return 0;
}
继续加油。