Run ID:28585

提交时间:2022-06-24 14:27:24

#include<iostream> using namespace std; int n; int h[1005];//记录导弹高度 int s[1005];//记录每个系统当前可拦截最高高度 int main() { while (cin >> h[n]) n++; int cnt = 1;//记录系统数 s[cnt] = h[0];//初始化,把第一个导弹高度录入第一个系统 for (int i = 1; i < n; i++)//遍历导弹 { int p = 0;//初始化!!! for (int j = 1; j <= cnt; j++)//遍历当前已有系统 { if (s[j] >= h[i])//如果第j个系统最大高度大于当前导弹 { if (p == 0) p = j;//若p==0,即第一次从已有系统中找到可用系统,则记录当前系统编号 else if (s[p] > s[j])//若存在有比已记录系统更优选择,则记录更优者。注意!!!重点,贪心思想!!! p = j; } } if (!p)//若p==0,即不存在可拦截系统 { s[++cnt] = h[i];//新建一个,且更新其可拦截最大高度 } else s[p] = h[i];//更新可拦截最大高度 } cout << cnt;//输出系统数 return 0; }