程序设计思维与实践 Week3 作业

A 选数问题

1. 题目大意

Given n positive numbers, ZJM can select exactly K of them that sums to S. Now ZJM wonders how many ways to get it!

Input:
The first line, an integer T <= 100, indicates the number of test cases. For each case, there are two lines. The first line, three integers indicate n, K and S. The second line, n integers indicate the positive numbers.

Output:
For each case, an integer indicate the answer in a independent line.

Example:

1
10 3 10
1 2 3 4 5 6 7 8 9 10
4

2. 思路历程

  • 这道题主要考虑深度优先搜索,以及最大程度减少判断次数的可行性剪枝。
  • 用递归进行深度优先搜索,对每个当前处理的数有选和不选两种情况进行深搜。
  • 先考虑最复杂的枚举O(K·2^n) - > 可以优化到组合P(n, K) - > 进一步剪枝跳过选出的数超过K和选出的数的和超过S的情况。

3. 具体实现

  • 函数solve(int i, int S, list& res)为递归主体函数,i记录当前处理的数的下标,S记录目标和,res数组记录已选中的数。
  1. 进入函数时先判断是否达到边界情况
    若已选中K个数且它们的和刚好等于目标和,则该方案合法且返回
    若遍历到n个数之外,或选中的数大于K,或它们的和超过了目标和,则该方案不合法且返回
  2. 再对当前处理的数进行选或不选两种情况的深搜
    若未选中,则直接进入下一个数的递归
    若选中,则将这个数加入选中的数组,并从目标和中减去该数作为新的目标和,进入下一个数的递归
  • 对n个数进行排序可以降低时间复杂度,排序对枚举进行了优化,从某一临界值开始后面的所有数都会直接返回。

4. 代码

#include <iostream>
#include <algorithm>
#include <list>
using namespace std;

int m, n, K, S, cnt;
int a[20];

void solve(int i, int S, list<int>& res)
{
    if (res.size() == K && S == 0)
    {
        cnt++;
        return;
    }
    
    if (i >= n) return;
    if (res.size() > K || S < 0) return;
    
    solve(i + 1, S, res); //不选
    res.push_back(a[i]);
    solve(i + 1, S - a[i], res); //选
    res.pop_back();
}

int main()
{
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> n >> K >> S;
        for (int  j = 0; j < n; j++)
            cin >> a[j];
        sort(a, a + n);
        list<int> res;
        cnt = 0;
        solve(0, S, res);
        cout << cnt <<endl;
    }
    return 0;
}

B 区间选点

1. 题目大意

数轴上有 n 个闭区间 [a_i, b_i]。取尽量少的点,使得每个区间内都至少有一个点(不同区间内含的点可以是同一个)

输入:
第一行1个整数N(N<=100)
第2 ~ N+1行,每行两个整数a,b(a,b<=100)

输出:
一个整数,代表选点的数目

样例:

2
1 5
4 6
1

2. 思路历程

  • 这道题主要考虑贪心算法,每次贪心时都使选中的点可以存在于尽可能多的闭区间内。
  • 先考虑取边界的点可以增多涉及的区间(如两个闭区间只有一个交点,该交点也是边界点,就涉及了两个区间)。
  • 再考虑取边界点中的左边界或右边界(二者本质相同,都要取更“居中”的边界)
  1. 如果整体左到右遍历区间,则取右边界(我使用的方法)
  2. 如果整体从右到左遍历区间,则取左边界
  • 最后确定区间的多条件排序标准,先以右边界升序,再以左边界降序。

3. 具体实现

  • 创建Section类表示区间,并对其进行操作符重载便于排序。
  • Section类数组排序后遍历(整体为区间从左到右的顺序),因此初始选中点min_b为所有区间里最小的右边界sections[0].b,创建向量right存放已选中的边界点。
  1. 遍历已排序的n个区间,选出不包含已选中点的区间,即左边界比当前右边界(选中点)更大的区间
  2. 更新选中点为该区间的右边界,并将该点加入right
  3. 遍历结束后right的长度即为选中点的个数

4. 代码

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Section
{
    int a, b;
    Section() {}
    bool operator < (const Section& section) const
    {
        if (b != section.b) return b < section.b;
        return a > section.a;
    }
};

int main()
{
    int n;
    cin >> n;
    Section *sections = new Section[n];
    for (int i = 0; i < n; i++)
        cin >> sections[i].a >> sections[i].b;
    sort(sections, sections + n);
    
    int min_b = sections[0].b;
    vector<int> right;
    right.push_back(min_b);
    for (int i = 1; i < n; i++)
    {
        if (sections[i].a > min_b)
        {
            min_b = sections[i].b;
            right.push_back(min_b);
        }
    }
    cout << right.size() << endl;
    
    return 0;
}

C 区间覆盖

1. 题目大意

数轴上有 n (1<=n<=25000)个闭区间 [ai, bi],选择尽量少的区间覆盖一条指定线段 [1, t](1<=t<=1,000,000)。
覆盖整点,即(1,2)+(3,4)可以覆盖(1,4)。
不可能办到输出 -1

输入:
第一行:N和T
第二行至N+1行:每一行一个闭区间

输出:
选择的区间的数目,不可能办到输出 -1

样例:

3 10
1 7
3 6
6 10
2

2. 思路历程

  • 这道题也是考虑贪心算法,每次贪心时都使选中的区间对指定线段的覆盖程度最大。
  • 先考虑普通情况下,区间长度大(除去指定线段外的部分),对指定线段的覆盖程度一定大。
  • 再考虑边界情况下,区间最小起点、最大终点是否覆盖指定线段,以及区间之间的间隔是否影响覆盖指定线段。
  • 对所有区间顺序遍历,确保考虑的完整性,因此对区间先以左边界升序排序,再以右边界降序排序。

3. 具体实现

  • 创建Sections类表示区间,在构造函数中直接切掉指定线段以外的部分(避免在求最大区间长度时产生干扰),并对其操作符重载便于排序。
  • Section类数组排序后遍历(整体为区间从左到右的顺序),初始选中区间的左端点为min_a,右端点为max_b。
  1. 处理指定线段左端点的覆盖属于特殊情况,计数值也需特殊讨论,若min_a > 左端点则无法覆盖
  2. 处理指定线段中间的覆盖情况,每次循环要么找到相同左端点时最大的右端点,要么更新左端点,进行剪枝判断。
    若当前区间的左端点仍小于min_a,当前max_b未覆盖到指定线段右端点,则进入下一个区间,找max_b的最大值(排序可以保证该比较的正确性)
    若当前区间的左端点大于min_a,区间数增1,更新min_a为max_b+1,若此时区间左端还大于min_a则不能覆盖,若不大于,则更新max_b为当前区间右端点
    若当前max_b覆盖到了指定线段右端点,即表示找到了使指定线段被覆盖的最小数量的区间

4. 代码

#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;

int n, t;

struct Section
{
    int a, b;
    Section() {}
    Section(int theA, int theB)
    {
        if (theA < 1) a = 1;
        else a = theA;
        if (theB > t) b = t;
        else b = theB;
    }
    bool operator < (const Section& section) const
    {
        if (a != section.a) return a < section.a;
        return b > section.b;
    }
};

int main()
{
    int l, r, cnt = 0;
    scanf("%d%d", &n, &t);
    Section *sections = new Section[n];
    for (int i = 0; i < n; i++)
    {
        scanf("%d%d", &l, &r);
        sections[i] = Section(l, r);
    }
    sort(sections, sections + n);
    
    int min_a = sections[0].a, max_b = sections[0].b;
    if (min_a > 1)
    {
        cout << "-1" << endl;
        return 0;
    }
    cnt++;
    
    for (int i = 1; i < n; i++)
    {
        if (sections[i].a <= min_a)
            max_b = max(max_b, sections[i].b);
        else
        {
            cnt++;
            min_a = max_b + 1;
            if (sections[i].a <= min_a)
                max_b = sections[i].b;
            else
            {
                cout << "-1" << endl;
                return 0;
            }
        }
        if (max_b >= t)
        {
            cout << cnt << endl;
            return 0;
        }
    }
    
    cout << "-1" << endl;
    
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值