「程序设计思维与实践」Week3 作业:A - 选数问题、B - 区间选点、C - 区间覆盖。dfs枚举+贪心

A - 选数问题

题目描述

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.

Sample Input
1
10 3 10
1 2 3 4 5 6 7 8 9 10
Sample Output
4
Hint

坐标(x, y)表示第x行第y列,行、列的编号从0开始,且以左上角为原点。
另外注意,输出中分隔坐标的逗号后面应当有一个空格。

题解
  • 考虑到数据范围为16,100组考虑dfs枚举选数情况,并加少许剪枝。
  • dfs思路:从第一个数开始枚举,记录三个变量:当前枚举到第几个、当前已经选了几个数、当前和,如果选数个数和当前和满足条件那么计数器加一,否则继续从这个数后面的数字开始枚举。
  • 剪枝:如果当前和已经大于题目要求的和,不再继续搜素。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int K, S, n, ans = 0;
int nums[20];

bool vis[2333];
void dfs(int x, int cnt, int sum) {
	if (sum > S || cnt > K) return;
	if (sum == S && cnt == K) {
		ans ++;
		return;
	}
	for (int i = x+1; i <= n; ++i) {
		dfs(i,cnt+1,sum+nums[i]);
	}
}

int main() {
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> K >> S;
		memset(nums, 0, sizeof(nums));
		memset(vis, 0, sizeof(vis));
		for (int i = 1; i <= n; ++i) {
			cin >> nums[i];
		}
		ans = 0;
		dfs(0,0,0);
		cout << ans << endl;
	}

	return 0;
}

B - 区间选点

题目描述

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

input

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

Output

一个整数,代表选点的数目

Sample Input1
2
1 5
4 6
Sample Output1
1
Sample Input2
3
1 3
2 5
4 6
Sample Output2
2
题解
  • 贪心策略:先按照右端点从小到大排序,对于每个区间,如果没有选过点,那么就选区间最右端的点。
  • 合理解释:对于一个区间,最右侧的点覆盖的其他区间个数一定大于等于其左侧的点覆盖的其他区间个数。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct P {
	int a;
	int b;
}p[233];

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> p[i].a >> p[i].b;
	}
	sort(p+1,p+n+1,[](P x, P y){return x.b < y.b;});
	int ans = 0, now = 0;
	for (int i = 1; i <= n; ++i) {
		if(now < p[i].a) {
			ans ++;
			now = p[i].b;
		}
	}
	cout << ans << endl;
	return 0;
}

C - 区间覆盖

题目描述

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

input

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

Output

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

Sample Input1
3 10
1 7
3 6
6 10
Sample Output1
2
Hint
 这道题输入数据很多,请用scanf而不是cin
题解
  • 贪心策略:对于如何选下一个区间:进行按照左端点排序,从当前点右侧合法点中选取区间右端点最大的,这样能够保证选这一个区间的价值最大
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 25005;
struct P {
	int a;
	int b;
}p[MAXN];

bool cmp(const P &x, const P &y) {
	if(x.a != y.a) return x.a < y.a;
	return x.b > y.b;
}

int main() {
	int n, t;
	cin >> n >> t;
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", &p[i].a, &p[i].b);
	}
	sort(p+1,p+n+1,cmp);
	// puts("sorted:");
	// for (int i = 1; i <= n; ++i) {
	// 	cout << p[i].a << "," << p[i].b << endl;
	// }
	// puts("sorted end");
	int anscnt = 0; // ans
	int now = 0, i = 1; // now_cnt ,,, now,,,id
	while(now < t) { // another interval is needed
		// cout << now << ",,," << endl;
		int maxx = -1, maxid = -1;
		for (; i <= n; i++) {
			// cout << ">>" << p[i].a << endl;
			if(p[i].a > now+1) { //this interval begin cnnt connect
				break;
			}
			// then new.a <= now+1, so find the biggest one
			if(p[i].b > maxx) {
				maxx = p[i].b;
				maxid = i;
			}
		}
		if(maxid == -1) {
			puts("-1");
			exit(0);
		}
		now = maxx;
		anscnt++;
	}
	cout << anscnt << endl;


	return 0;
}


/*
5 10
2 7
3 4
5 6
3 6
6 10

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值