当前位置: 首页> 财经> 产业 > C++笔试强训day29

C++笔试强训day29

时间:2025/7/9 1:08:50来源:https://blog.csdn.net/cy18779588218/article/details/139072637 浏览次数:1次

目录

1.排序子序列

2.消减整数

3.最长上升子序列(二)


1.排序子序列

链接icon-default.png?t=N7T8https://www.nowcoder.com/questionTerminal/2d3f6ddd82da445d804c95db22dcc471

利用指针模拟即可,注意过渡掉开头的恒定段即可。

#include <iostream>
using namespace std;
const int N = 1e5 + 10;int a[N];
int n;
int main() {cin >> n;for (int i = 1; i <= n; ++i) cin >> a[i];int i = 1;int ret = 0;while (i <= n){if (i == n){ret++;break;}if (a[i] < a[i + 1]){ret++;while (i + 1 <= n && a[i] <= a[i + 1]){i++;}}else if (a[i] > a[i + 1]){ret++;while (i + 1 <= n && a[i] >= a[i + 1]){i++;}}else{while (i + 1 <= n && a[i] == a[i + 1]){i++;}}++i;}cout << ret << endl;return 0;
}

2.消减整数

链接icon-default.png?t=N7T8https://ac.nowcoder.com/acm/problem/219038

数学规律加上一点贪心(本质就是不能一味的贪心,因为题意要求恰好减为0

#include <iostream>
using namespace std;int t, h;
int solve()
{if(h == 1)return 1;int ret = 0;int a = 1;while(h){h -= a;ret++;if(h % (2 * a) == 0)a *= 2;}return ret;
}int main()
{cin >> t;while(t--){cin >> h;cout << solve() << endl;}      return 0;
}

3.最长上升子序列(二)

链接icon-default.png?t=N7T8https://www.nowcoder.com/practice/4af96fa010c44638a7e112abf65f7237?tpId=196&tqId=39285&ru=/exam/oj

class Solution
{int dp[100010] = { 0 }; // dp[i] 表⽰:⻓度为 i 的最⼩末尾int pos = 0;
public:int LIS(vector<int>& a){for (auto x : a){// 查找 x 应该放在哪个位置if (pos == 0 || x > dp[pos]){dp[++pos] = x;}else{// ⼆分查找插⼊位置int l = 1, r = pos;while (l < r){int mid = (l + r) / 2;if (dp[mid] >= x) r = mid;else l = mid + 1;}dp[l] = x;}}return pos;}
};

关键字:C++笔试强训day29

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: