week3- jobs

A - number of election issues

Meaning of the questions:

Given n positive number, to choose the K and these numbers is S, a total of how many election law.


Ideas:

This question is the first to use DFS depth-first search, that is, from the starting point to explore the back, once found that the choice does not meet the requirements, you choose another point back again, again and again to explore, to obtain the number selected for the K and S are all .
This means that the first number from the start, traversing all the numbers. Each number has checked and unchecked both cases, the need to use recursion to solve. Recursive when pruning, it is possible to greatly reduce the complexity of the algorithm. When it is determined that the time and S is the number of +1 the. Until the number of complete traversal of all the options or selected> K when it will be pruned.

Code:

#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;
#define _for(i,a,b) for(int i=a;i<b;i++)
int T, n, K, S;
int m[16];
list<int> res;
int step=0;

void solve(int i, int K,int sum)
{
	if (res.size() == K && sum == 0) 
	{
		step++;
		return;
	}
	if (i >= n) return;
	if (res.size() > K || sum < 0) return;
	solve(i + 1, K, sum); //选中,放入链表中
	res.push_back(m[i]);
	solve(i + 1, K, sum - m[i]); //不选
	res.pop_back(); //不选的话,将该数从链表里弹出删除

}
int main()
{
	cin >> T;
	_for(i, 0, T)
	{
		cin >> n >> K >> S;
		_for(j, 0, n)
		{
			cin >> m[j];
		}
		solve(0, K, S);
		cout << step << endl;
		step = 0;
	}
	return 0;
}

B - Interval setpoint

Meaning of the questions:

With n closed interval [a_i, b_i] number line. Take as few points, such that have at least one point (point may be different sections containing the same) in each interval

Input:
The first line of an integer N (N <= 100)
of 2 ~ N + 1 lines, each two integers a, b (a, b < = 100)

: Output
number an integer representing the selected point,

Ideas:

First need to find greedy indicators should be seen gradually to determine the right point value, we will be able to accomplish the objectives we need. It should be in accordance with all sections of the right point b from small to large, from ordering a good first point of maximum range right right point and began to judge, if the left point after the first interval of the right end of a point less than the range, then it is necessary to take a multi-point, the right end of the right section of the cover the maximum point, a case of performing judgment section. If greater than or equal, it means that the right end point of the section, the right end point and then recycled to the left of the current point of the next section are compared, until completion of all comparison interval. The number of statistics to the value of what we want.
Intervals around the structure can be represented.
Because they do not start a rigorous consideration, is determined such that the first point is less than a right section wa frequently left end point of the interval is equal to one, some small details still need attention.

Code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define _for(i, a, b) for(int i=a;i<b;i++)


struct section
{
	int a;
	int b;
	bool operator < (const section& s)const
	{
		if(b!= s.b)
			return b <s.b ;
		else
			return a > s.a;
	}
}s[1000];

bool cmp(const int &s1 ,const int &s2)
{
	return s1>s2 ;
}
int main()
{
	int N;
	cin >> N;
	int end=-1;
	int count = 0;
	_for(i, 0, N)
	{
		cin >> s[i].a >> s[i].b;
	}
	sort(s, s + N);
	_for(i, 0, N)
	{	
		if (end< s[i].a)
		{
			end=s[i].b;
			count++;
		}
	}
	cout << count << endl;
	return 0;
}

C - interval coverage

Meaning of the questions:

There are a number line n (1 <= n <= 25000) a closed interval [ai, bi], selected to cover a range of as little as specified segment [1, t] (1 < = t <= 1,000,000).
Covering the entire point, i.e., (1,2) + (3,4) may cover (1,4).
The impossible Output -1

Input:
First line: N T and the
second row N + 1 to row: each line a closed interval.
Output:
the number of selected sections, the impossible outputs -1.


Ideas:

This question is madness on her face, looked a little problem just do it, failed to consider (1,2) (3,4) can be covered (1,4) case, so that frequent wa. Although tried many samples, but still wrong, just turn to the students showed me out. Reading questions must be careful careful careful.
By using a structure represented interval.
First is the need to find a suitable index greedy, we can see range from 1-t, if all the starting point of the interval is not an impossible to do so. That is, a closed interval left point from small to large, and then traverse all sections, select the starting point for the longest intervals of 1, which is set to +1 point right starting point, and then repeat the process and will be able to get the desired result.


Code:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std; 
#define _for(i,a,b) for(int i=a;i<b;i++)

struct section
{
	int a;
	int b;
	bool operator <(const section &s1) 
	{
		if(a!=s1.a) return a<s1.a;
		else return b>s1.b; 
	}
	
}s[25001];

int main()
{
	int N, T,a,b;
	int count=1; //计数所用	
	scanf("%d %d", &N, &T);
	_for(i, 0, N)
	{
		scanf("%d %d", &a, &b);
		if(a<1) s[i].a=1;  //防止输入错误情况(考虑多了) 
		else s[i].a=a;
		if(b>T) s[i].b=T;
		else s[i].b=b;
	}	
	sort(s, s+ N);
	int maxb=s[0].b;
	int start=1;  //起点设置为1 
	if(s[0].a!=1)  //第一个区间起点不是1,则无解 
	{
		cout<<-1<<endl;
		return 0;
	}
	 
	_for(i,0,N)
	{
		if(s[i].a<=start)  //左端点小于等于起始点 
			maxb=max(maxb,s[i].b); //选择最大的右端点 
		else
		{
			count++; //否则那就需要多取一个区间 
			start=maxb+1;   //将起点设置为最大右端点+1 
			if(s[i].a<=start)   
				maxb=s[i].b;
			else
			{
				cout<<-1<<endl;
				return 0;
			}
			
		}
		if(maxb==T) //到达T 
		{
			cout<<count<<endl;
			return 0;
		}
		
	}
	cout<<-1<<endl;
	return 0;
}
Released seven original articles · won praise 0 · Views 124

Guess you like

Origin blog.csdn.net/weixin_44465341/article/details/104980551