admin管理员组

文章数量:1027471

【今日三题】排序子序列(模拟) / 消减整数(贪心) / 最长上升子序列(二)(贪心+二分)

排序子序列(模拟)

  • 排序子序列
在这里插入图片描述
代码语言:javascript代码运行次数:0运行复制
#include <iostream>
using namespace std;

const int N = 1e5 + 1;
int n, arr[N], res;

int main() 
{
    cin >> n;
    for (int i = 0; i < n; i++) cin >> arr[i];
    int i = 0;
    while (i < n)
    {
        if (i == n - 1) 
        {
            res++;
            break;
        }
        if (arr[i] > arr[i + 1])
        {
            while (i + 1 < n && arr[i] >= arr[i + 1]) i++;
            res++;
        }
        else if (arr[i] < arr[i + 1])
        {
            while (i + 1 < n && arr[i] <= arr[i + 1]) i++;
            res++;
        }
        else 
        {
            while (i + 1 < n && arr[i] == arr[i + 1]) i++;
        }
        i++;
    }
    cout << res << endl;
    return 0;
}

消减整数(贪心)

  • 消减整数
代码语言:javascript代码运行次数:0运行复制
#include <iostream>
using namespace std;

int t, h;

int main()
{
    cin >> t;
    while (t--)
    {
        int h = 0, cnt = 0, a = 1;
        cin >> h;
        while (h)
        {
            cnt++;
            h -= a;
            if (h % (2 * a) == 0) a *= 2;
        }
        cout << cnt << endl;
    }
    return 0;
}

最长上升子序列(二)(贪心+二分)

  • 最长上升子序列(二)

本题有两种常规做法:动态规划和贪心。dp的时间复杂度是N^2,本题的数据范围会超时,贪心的时间复杂度是N*logN,因此本题只能用贪心。

代码语言:javascript代码运行次数:0运行复制
class Solution {
public:
    int LIS(vector<int>& a) {
        if (a.empty()) return 0;
        vector<int> v;
        v.push_back(a[0]);
        for (int i = 1; i < a.size(); i++)
        {
            if (a[i] > v.back()) v.push_back(a[i]);
            else
            {
                int l = 0, r = v.size() - 1;
                while (l < r)
                {
                    int mid = l + (r - l) / 2;
                    if (a[i] > v[mid]) l = mid + 1;
                    else r = mid;
                }
                v[r] = a[i];
            }
        }
        return v.size();
    }
};

本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-05-03,如有侵权请联系 cloudcommunity@tencent 删除return动态规划排序数据int

【今日三题】排序子序列(模拟) / 消减整数(贪心) / 最长上升子序列(二)(贪心+二分)

排序子序列(模拟)

  • 排序子序列
在这里插入图片描述
代码语言:javascript代码运行次数:0运行复制
#include <iostream>
using namespace std;

const int N = 1e5 + 1;
int n, arr[N], res;

int main() 
{
    cin >> n;
    for (int i = 0; i < n; i++) cin >> arr[i];
    int i = 0;
    while (i < n)
    {
        if (i == n - 1) 
        {
            res++;
            break;
        }
        if (arr[i] > arr[i + 1])
        {
            while (i + 1 < n && arr[i] >= arr[i + 1]) i++;
            res++;
        }
        else if (arr[i] < arr[i + 1])
        {
            while (i + 1 < n && arr[i] <= arr[i + 1]) i++;
            res++;
        }
        else 
        {
            while (i + 1 < n && arr[i] == arr[i + 1]) i++;
        }
        i++;
    }
    cout << res << endl;
    return 0;
}

消减整数(贪心)

  • 消减整数
代码语言:javascript代码运行次数:0运行复制
#include <iostream>
using namespace std;

int t, h;

int main()
{
    cin >> t;
    while (t--)
    {
        int h = 0, cnt = 0, a = 1;
        cin >> h;
        while (h)
        {
            cnt++;
            h -= a;
            if (h % (2 * a) == 0) a *= 2;
        }
        cout << cnt << endl;
    }
    return 0;
}

最长上升子序列(二)(贪心+二分)

  • 最长上升子序列(二)

本题有两种常规做法:动态规划和贪心。dp的时间复杂度是N^2,本题的数据范围会超时,贪心的时间复杂度是N*logN,因此本题只能用贪心。

代码语言:javascript代码运行次数:0运行复制
class Solution {
public:
    int LIS(vector<int>& a) {
        if (a.empty()) return 0;
        vector<int> v;
        v.push_back(a[0]);
        for (int i = 1; i < a.size(); i++)
        {
            if (a[i] > v.back()) v.push_back(a[i]);
            else
            {
                int l = 0, r = v.size() - 1;
                while (l < r)
                {
                    int mid = l + (r - l) / 2;
                    if (a[i] > v[mid]) l = mid + 1;
                    else r = mid;
                }
                v[r] = a[i];
            }
        }
        return v.size();
    }
};

本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-05-03,如有侵权请联系 cloudcommunity@tencent 删除return动态规划排序数据int

本文标签: 今日三题排序子序列(模拟)消减整数(贪心)最长上升子序列(二)(贪心二分)