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

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.

Example
Input

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

Output

4

题解
本题使用dfs的递归方法。同时要注意适当使用全局变量。answer数组用于保存符合题意的数字。

#include<cstdio>
#include<list>
using namespace std;
int K,S,n,sum;
void dfs(int i,int S,list<int> answer,int* a)
{
	if(S == 0 && answer.size() == K)
	{   
		sum++;    
		return;
	}
	if( i >= n)
		return;
	if( answer.size()>K || S < 0)
		return;
	dfs(i+1,S,answer,a);       
	answer.push_back(a[i]);      
	dfs(i+1,S-a[i],answer,a);   
	answer.pop_back();     
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&K,&S);
		int a[n];   
		sum = 0;
		for(int i = 0;i < n;i++)
		{
			scanf("%d",&a[i]);
		}
		list<int> answer;   
		dfs(0,S,answer,a);
		printf("%d",sum);
	}
	return 0;
}

B - 区间选点


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

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

Output

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

Examples
Input

2
1 5
4 6

Output

1

Input

3
1 3
2 5
4 6

Output

2

题解
将区间构造为结构体,并按照右边界由小到大,左边界由大到小的顺序排序。然后考虑贪心算法,把每个区间最右边的点取出,删掉该区间,当所有区间至少一个点为结束标志。

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct region
{
	int a;
	int b;
};
bool cmp(region x,region y)
{
	return x.b != y.b ? x.b < y.b : x.a > y.a;
} 
int main()
{
	int N;
	scanf("%d",&N);
	vector<region> r(N);
	for(int i = 0; i < N;i++)
	{
		scanf("%d %d",&r[i].a,&r[i].b);
	}
	sort(r.begin(),r.end(),cmp);
	int ans=0;
	while( !r.empty() )
	{
		region now = r[0];
		r.erase( r.begin() );
		int x = now.b;
		ans++;
		for(int i = 0 ; i < r.size();i++)
		{
			if(r[i].a <= x && r[i].b >= x)
			{
				r.erase( r.begin() + i );
				i--;
			}
		}
	}
	printf("%d\n",ans);
}

/*


Input

2
1 5
4 6

Output

1

Input

3
1 3
2 5
4 6

Output

2

*/

C - 区间覆盖


描述

数轴上有 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

提示

   这道题输入数据很多,请用scanf而不是cin

题解
本题同样先构造结构体表示区间,并按照先左边界后有边界的规则排序。然后找到右边界最大的区间,在进行下一波循环

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct region
{
	int left;
	int right;
	bool operator < (const region &p) const
	{
		return left != p.left ?  left < p.left:right < p.right;
	}
};
int b, e;
int find(region* arr,int size,int _s)
{
	int max = _s;
	int site = -1;
	for (int i = b; i < size; i++)
	{
		if (arr[i].left <= _s && arr[i].right >= max)
		{
			max = arr[i].right;
			site = i;
		}

		if (arr[i].left > _s )
		{
			b = i;
			break;
		}
	}
	return site;
}
int main()
{
	int N, T;
	scanf("%d %d",&N,&T);
	b = 0; 
	e = N - 1;
	region* r = new region[N];
	for(int i = 0; i < N;i++)
	{
		scanf("%d %d",&r[i].left,&r[i].right);
	}
	sort(r, r + N);
	int s = 1;
	int con = 0;
	int number = 0;
	while (1)
	{
		number++;
		con = find( r , N , s);
		if (con == -1)
		{
			printf("-1");
			break;
		}
		s = r[con].right;
		if (s >= T)
		{
			printf("%d",number);
			break;
		}
		s++;
	}

}
/*
3 10
1 7
3 6
6 10

2
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值