Week3 assignment

Question selection question A

Title description

Given [Math Processing Error] positive numbers, ZJM can select exactly [Math Processing Error] of them that sums to [Math Processing Error]. Now ZJM wonders how many ways to get it!
input:
The first line, an integer [Math Processing Error], indicates the number of test cases. For each case, there are two lines. The first line, three integers indicate [Math Processing Error], [Math Processing Error] and [Math Processing Error]. The second line, [Math Processing Error] integers indicate the positive numbers.
output:
For each case, an integer indicate the answer in a independent line.

Ideas

Here, all the numbers are stored in an array. This array performs the dfs recursive operation. When the recursion is k times, it is judged whether it is correct. Before k times, it is greater than S and the recursion should be ended early to save time

Code
#include<iostream>
using namespace std;
int n,K,S,ans;
int a[100];


void dfs(int k,int sum,int end)
{
	if(sum>S)return;//提前退出 
	if(k==K)
	{
		if(sum==S) ans++;
	}
	else 
	{
		for(int i=end;i<n;i++)
			dfs(k+1,sum+a[i],i+1);
	}
}

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

to sum up

I am guilty of this assignment because of playfulness and not paying attention to the notices in the group. I did n’t find something wrong until the end of this assignment, o (╥﹏╥) o.

Choosing a point in question B

Title description

There are n closed intervals [a_i, b_i] on the number line. Take as few points as possible so that there is at least one point in each interval (the points contained in different intervals can be the same)
Input:
1 integer N in the first line (N <= 100)
lines 2 ~ N + 1 , Two integers a, b (a, b <= 100)
output per line :
an integer representing the number of points selected

Ideas

The meaning of this question is very clear. Here I use this greedy criterion, take the smallest right node in the remaining interval, and compare its left node with the reference point that is the right node of the previously selected interval. How can it be smaller than the right node? One point can be shared, otherwise the reference point is updated to the right node of the new interval. Here, all the intervals are sorted non-decreasingly with the right node, and the elements are sequentially taken from them.

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

struct mm
{
	int l,r;
	mm(){};
	mm(int a,int b):l(a),r(b){};
	bool operator<(const mm&p)
	{
		return r<p.r;
	}
};
mm shu[101];
int n;
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>shu[i].l>>shu[i].r;
	}
	sort(shu,shu+n);
	int count=0,nowp=-1;
	for(int i=0;i<n;i++)
		if(shu[i].l>nowp)
		{
			nowp=shu[i].r;
			count++;
		}
	cout<<count<<endl;
	return 0;
}
to sum up

Through this question, Konjac can figure out the trick of overloading the less-than sign and the less-than sign in the cmp function. As shown in the following cmp function, a and b can be regarded as the first two items in the array, a <b It means the relationship in the array after sorting. In this case, it is an incremental relationship. When the overload is less than the sign, the called structure or class can be regarded as the former element. The internal representation of the function is the same as the cmp function. Here is also the incremental relationship. Chicken understanding

	bool operator<(const mm&p)
	{
		return r<p.r;
	}
	
	bool cmp(const int &a,const int &b)
	{
    	return a<b;
	}

C question interval coverage

Title description

There are n (1 <= n <= 25000) closed intervals [ai, bi] on the number line. Choose as few intervals as possible to cover a specified line segment [1, t] (1 <= t <= 1,000,000).
Cover the whole point, ie (1,2) + (3,4) can cover (1,4).
input: The
first line: N and T indicate the given interval number and the right end value of the interval to be covered. The
second line to the N + 1 line: each line is a closed interval.
output:
the number of selected intervals, it is impossible to output -1

Ideas

This time we use the left end point in non-decreasing order to arrange. For each left end value of the record, we must find an element from the array whose left end point is less than or equal to the left end value of the current record, and the right end value is as large as possible Update to cover the entire interval with as few intervals as possible.

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

struct mm
{
	int l,r;
	bool operator<(const mm&p)
	{
		return l<p.l;
	}
};

mm shu[25002];
int n,T,S=1;
int main()
{
	scanf("%d%d",&n,&T); 
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&shu[i].l,&shu[i].r);
	}
	sort(shu,shu+n);
    int n_x=1,n_y=0,count=0;
    int i=0;
    while(n_x<=T&&i<n)
    {
		if(shu[i].l>n_x) break;
			bool flag = 0;
    	for(;i<n&&shu[i].l<=n_x;i++)
    	{
    		if(shu[i].r>n_y)
    		{
				n_y=shu[i].r;//更新最右值
				flag=1;
			}
		}
		if(flag)
		{
	      count++;
		  n_x=n_y+1;
		} 
    }
   	if(n_x>T) printf("%d\n",count);
	else printf("-1\n"); 
	return 0;
}

to sum up

At the beginning, the need for flag was not taken into account. Count ++ was performed for each right endpoint value update, which resulted in a great result. A flag was set to indicate that the right value was updated, which solved the problem of adding one more times.

Published 20 original articles · Like 3 · Visits 457

Guess you like

Origin blog.csdn.net/qq_44893580/article/details/104978056