【hihocoder1051】补提交卡——贪心

该博客探讨了如何使用贪心策略解决一个数学问题:在1到100的数列中,有n个断点,给定m个位置可以连续,目标是找到能形成的最长连续区间。博主通过枚举m个位置的左端点来实现解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:点击进入

描述:

一个1到100的数列有n个地方是断开的,现给出这n个位置以及一个数m,表示可以使m个位置连续,求可以得到的最长的连续区间的长度,n个位置按照升序给出。

题解:

将起点和终点补充进去之后,必须使这m个位置连续地消除才能使最终长度最长,所以直接枚举这m个位置的左端点即可。

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <cassert>
#include <cctype>
#include <complex>
#include <algorithm>
#include <string>
#include <iostream>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <set>
using namespace std;
#define fi first
#define se second
#define MP(A, B) make_pair(A, B)
#define pb push_back
#define gcd __gcd
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
typedef long long ll;
typedef unsigned long long ulls;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<pii, int> mpi;
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
    #define lld %I64d
#else
    #define lld %lld
#endif
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int maxn = 1e2 + 5;
const int maxm = 1e6 + 5;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
inline int scan(int &a) { return scanf("%d", &a); }
inline int scan(int &a, int &b) { return scanf("%d%d", &a, &b); }
inline int scan(int &a, int &b, int &c) { return scanf("%d%d%d", &a, &b, &c); }
inline int scan(ll &a) { return scanf("lld", &a); }
inline int scan(ll &a, ll &b) { return scanf("lldlld", &a, &b); }
inline int scan(ll &a, ll &b, ll &c) { return scanf("lldlldlld", &a, &b, &c); }
inline int scan(double &a) { return scanf("%lf", &a); }
inline int scan(double &a, double &b) { return scanf("%lf%lf", &a, &b); }
inline int scan(double &a, double &b, double &c) { return scanf("%lf%lf%lf", &a, &b, &c); }
inline int scan(char &a) { return scanf("%c", &a); }
inline int scan(char *a) { return scanf("%s", a); }
template<class T> inline void mem(T &A, int x) { memset(A, x, sizeof(A)); }
template<class T0, class T1> inline void mem(T0 &A0, T1 &A1, int x) { mem(A0, x), mem(A1, x); }
template<class T0, class T1, class T2> inline void mem(T0 &A0, T1 &A1, T2 &A2, int x) { mem(A0, x), mem(A1, x), mem(A2, x); }
template<class T0, class T1, class T2, class T3> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x); }
template<class T0, class T1, class T2, class T3, class T4> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x); }
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x); }
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x), mem(A6, x); }
template<class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template<class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template<class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c, T d, T e) { return min(min(min(a,b),min(c,d)),e); }
template<class T> inline T max(T a, T b, T c, T d, T e) { return max(max(max(a,b),max(c,d)),e); }
int a[maxn];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        long _begin_time = clock();
    #endif

    int T; scan(T);
    while(T--)
    {
        int n, m; scan(n, m);
        for(int i = 1; i <= n; i++) scan(a[i]);
        if(m >= n)
        {
            printf("100\n");
            continue;
        }
        a[0] = 0, a[(++n)++] = 101;
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            if(i + m + 1 > n) break;
            ans = max(ans, a[i + m + 1] - a[i] - 1);
        }
        printf("%d\n", ans);
    }

    #ifndef ONLINE_JUDGE
        long _end_time = clock();
        printf("time = %ld ms\n", _end_time - _begin_time);
    #endif

    return 0;
}
<think>好的,我需要帮助用户了解贪心算法的设计与分析,并提供实验示例代码。首先,用户提到了他们可能需要完成相关实验任务,所以应该从基础开始,逐步引导。 首先,我应该解释贪心算法的基本概念,确保用户理解其核心思想,比如局部最优和全局最优的关系。然后,可以举几个经典例子,比如活动选择问题、霍夫曼编码或背包问题,这样用户能更好地理解应用场景。 接下来,用户需要具体的实验示例代码。考虑到贪心算法常见的例子,活动选择问题比较适合,因为它直观且代码相对简单。我需要用Python编写一个例子,注意代码的结构和注释,方便用户理解。 然后,实验分析部分需要说明时间复杂度和空间复杂度,帮助用户评估算法性能。同时,要提到贪心算法的优缺点,比如高效但不一定全局最优,这样他们在实验报告中可以进行分析。 用户提供的引用中,引用[1]提到了实验要求和步骤,引用[3]提到了开发环境,所以代码示例需要兼容常见的IDE,比如VS Code或Dev C++。不过Python代码在这些环境中都能运行,应该没问题。 最后,生成相关问题时要围绕贪心算法的设计、分析、优化和应用,这样用户能进一步深入学习和研究。确保问题覆盖理论、实际应用以及与其他算法的比较,比如动态规划。 检查一下是否遵循了系统级指令:行内公式用$...$,独立公式单独成段,正确的中文回答,引用标识的位置正确。例如,在提到贪心算法适用条件时引用[^1],在实验步骤部分引用[^3]的IDE信息。 总结步骤:介绍概念,提供示例代码,分析实验结果,列举相关问题,并正确引用资料。确保内容结构清晰,代码正确可运行,分析到位,帮助用户完成实验任务。</think>### 贪心算法设计与分析实验示例 #### 一、贪心算法设计核心思想 贪心算法通过**每一步选择当前状态下的局部最优解**,最终期望得到全局最优解。其设计需要满足两个条件: 1. **贪心选择性质**:每一步的局部最优解能推导出全局最优解 2. **最优子结构**:问题的最优解包含子问题的最优解 #### 二、经典实验案例:活动选择问题 **问题描述**:从$n$个活动集合中选出互不冲突的最大活动子集,每个活动有开始时间$s_i$和结束时间$f_i$。 **贪心策略**:优先选择**结束时间最早**的活动 ##### Python实现代码 ```python def greedy_activity_selector(s, f): n = len(s) selected = [0] # 第一个活动默认选中 k = 0 for m in range(1, n): if s[m] >= f[k]: # 检查时间是否冲突 selected.append(m) k = m return selected # 示例输入(已按结束时间排序) s = [1, 3, 0, 5, 8, 5] f = [2, 4, 6, 7, 9, 9] print("选择的活动编号:", greedy_activity_selector(s, f)) # 输出 [0, 1, 3, 4] ``` #### 三、实验分析要点 1. **时间复杂度**:$O(n\log n)$(主要来自排序操作) 2. **空间复杂度**:$O(n)$ 3. **正确性验证**:可通过数学归纳法证明贪心选择的有效性 4. **实验对比**:与动态规划解法对比时间效率[^2] #### 四、贪心算法适用条件 | 条件类型 | 具体要求 | |---------|----------| | 贪心选择性 | 可通过局部最优达到全局最优 | | 最优子结构 | 子问题最优解能组合成原问题解 | #### 五、实验环境建议 推荐使用VS Code或Code::Blocks进行调试(参考引用),可通过断点调试观察活动选择过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值