当前位置: 首页 > news >正文

[NOIP 1999 提高组] 导弹拦截

题目链接:

线性DP代码(O(n^2)时间复杂度):
 

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+10;

int n = 0;
int arr[N];
int dp[N];
vector<int> q;

signed main(){
    int x;
    while(cin >> x) arr[++n] = x;
    
    //找到最大不上升序列
    for(int i = 1; i <= n; i++){
        dp[i] = 1;
        for(int j = 1; j < i; j++){
            if(arr[j] >= arr[i]){
                dp[i] = max(dp[i], dp[j]+1);
            }
        }
    }
    
    int ans = 0;
    for(int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    
    //找到需要几套系统
    q.push_back(50010);
    
    for(int i = 1; i<= n; i++){
        bool check = false;
        for(int j = 0; j < q.size();j++){
            if(q[j] >= arr[i]){
                q[j] = arr[i];
                check = true;
                break;
            }
        }
        if(!check) q.push_back(arr[i]);
    }
    cout << ans << endl << q.size() << endl;
    return 0;
}

贪心+二分思路优化:

①题目要求我们求出最长不上升子序列,当序列每个数字都选择当前最大的时候(符合不上升规则),那么得出的序列就是最长的不上升子序列。

②我们用 g[i] 数组来记录长度为i的子序列结尾最后一个数字是谁。g[]数组记录的是最长不上升子序列长度,不记录最长不上升子序列的具体数字。枚举数组 arr[] ,当 arr[i] <= g[i]的时候,将arr[i] 添加到g[i+1]中;当 arr[i] > g[i],要在 g[] 数组中找到最后一个大于 arr[i] 的数字,更新 g[r] 的最大值为 arr[i]。

③这道题是求的是降序的最长子序列,容易搞错二分条件。要明白 g[] 数组的含义。

贪心+二分代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+10;

int arr[N]; //存数据
//len为g[]的长度, g[i]存的是长度为i序列,最后一个字母是谁?
//g[]数组不存最长不上升子序列,记录的是长度
int len = 1, g[N], p;

signed main(){
    //读入数据
    int k;
    while(cin >> k) arr[++p] = k;
    
    //对g初始化
    g[1] = arr[1];
    for(int i = 2; i <= p; i++){
        if(g[len] >= arr[i]){
            g[++len] = arr[i];
        }
        else{
            //在有序数组g[]中查找最后一个大于arr[i]的数字 返回r
            int l = 0, r = len+1;
            while(l+1 < r){
                int mid = (l+r) / 2;
                if(g[mid] >= arr[i]){
                    l = mid;
                }
                else r = mid;
            }
            g[r] = arr[i];
        }
    }
    
    //找到需要几套系统
    vector<int> q; q.push_back(arr[1]);
    for(int i = 2; i <= p; i++){
        int check = false;
        for(int j = 0; j < (int)q.size(); j++){
            if(q[j]  >= arr[i]){
                q[j] = arr[i];
                check = true;
                break;
            }
        }
        if(!check){
            q.push_back(arr[i]);
        }
    }
    
    cout << len << endl << q.size() << endl;
    return 0;
}

http://www.dtcms.com/a/107264.html

相关文章:

  • C++ STL简单的几个容器
  • I²C总线高级特性与故障处理分析
  • 【leetcode100】每日温度
  • OpenCV 从入门到精通(day_04)
  • 面向对象
  • python实现简单fast-cgi服务,对接到nginx
  • 蓝桥云客 刷题统计
  • 持续集成与Jenkins安装使用教程
  • 分布式锁方案-Redisson
  • Linux命令-tar
  • 使用 MapReduce 进行高效数据清洗:从理论到实践
  • Linux内核中ARP协议的实现与dev_addr字段的作用
  • LabVIEW 调用 Python 函数
  • SAP-ABAP:ABAP `LEAVE LIST-PROCESSING` 深度解析
  • 天梯赛 L2-023 图着色问题
  • ai prompt工程师认证
  • AT_abc306_b [ABC306B] Base 2
  • 【工具变量】全国分省低空经济高质量发展数据(2012-2023年)
  • Word 插入无页眉页码的空白页(即插入奇数页)
  • WebSocket connection failed 解决
  • 基于机器学习的三国时期诸葛亮北伐失败因素量化分析
  • 数学知识集锦
  • Ubuntu24.04-中文输入法的切换
  • 批量将文本文件转换为 Word/PDF/Excel/图片等其它格式
  • python-leetcode 64.在排序数组中查找元素的第一个和最后一个位置
  • c语言大小端判断
  • 【JavaScript】十三、事件监听与事件类型
  • 95. 费解的开关
  • 密码学基础——古典密码学
  • 云端革命:数字文明的重构与新生