二分与贪心算法题

目录

一、看几个完整节目

二、求阶乘有指定0个数的最小的那个数

三、求函数最小值问题


一、看几个完整节目

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample

InputOutputcopy
 
12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0

1、题目理解:这是是按每个节目的结束时间进行排序,因为无论是按照开始时间还是时长进行排序都不可行。主要就是找到最多看成几部完整的节目。所以这个题用到了二分算法。我是第一次接触到二分算法,它主要可以寻找三种类型的值,然后范围调整,一点点逼近那个要找的值。这个题还用到结构体来存放开始时间与结束时间,我用s代表开始时间,e代表结束时间。将结束时间按照从小到大进行排序,设最开始的结束时间为0,每次看完一部剧,就更新结束时间,如果下一部的开始时间大于这个结束时间,那么这部剧就可以看,则剧数,即cont可以加一。

#include <iostream>
#include<algorithm>
using namespace std;
struct mytime
{
	int s;
	int e;
}movie[105];
bool cmp(mytime m1,mytime m2)
{
	return m1.e<m2.e;
}
int main()
{
	int n;
	while(cin>>n,n){
	for(int i=1;i<=n;i++)
		{
			cin>>movie[i].s>>movie[i].e;
		}
		sort(movie,movie+n,cmp);
		int endtime=0;
		int count=0;
		for(int i=1;i<=n;i++)
		{
			if(movie[i].s>=endtime)
			{
				endtime=movie[i].e;
				count++;
			}
		}
		cout<<count<<endl;
	}
		
   return 0;
}

二、求阶乘有指定0个数的最小的那个数

You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1 * 2 * ... * N. For example, 5! = 120, 120 contains one zero on the trail.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.

Output

For each case, print the case number and N. If no solution is found then print impossible.

Sample

InputcopyOutputcopy
3
1
2
5
Case 1: 5
Case 2: 10
Case 3: impossible

2、题目理解:我一开始真的看不懂这个题到底要干嘛,后来略微理解了一点点,就是他会输入q,就是一个数的阶乘之后会有几个0的问题,我们的目的就是找到这个最小值,同时,我们发现一个问题,如果有一个5,那么就会有一个0,因为有2*5等于10嘛,但是用2的话,不太行,所以可以根据5的个数,来判断几个0,所以这个题,就很容易写出判断几个5的num函数。再根据二分法找到这个最小的数字。

#include <iostream>
#include<algorithm>
using namespace std;
int num(int n)
{
	int ans=0;
	while(n){
		ans+=n/5;
		n=n/5;
	}
	return ans;
}
int main()
{
	int mid,i=1;
	int t,q;
	long long l,r;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&q);
		l=0;
		r=100000000000000;
		long long m=0;
		while(l<=r)
		{
			mid=(l+r)/2;
			if(num(mid)==q)
			{
				r=mid-1;
				m=mid;
			}
			else
			{
				if(num(mid)>q)
				{
					r=mid-1;
				}
				else l=mid+1;
			}
		}
			if(m==0) printf("Case %d: impossible\n",i);
			   else printf("Case %d: %lld\n",i,m);
				i++;
		}
			
				
		
   return 0;
}

三、求函数最小值问题

Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.

Input

The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)

Output

Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

Sample

InputcopyOutputcopy
 
2 100 200
 
-74.4291 -178.8534

3、题目理解:一些大佬们都说这是一道数学题。说实话,我一开始依然没看懂这道题,听学长讲过才明白这道题,这个主要是使用导数来求得,因为导数为0的点取到最小值。所以我就用二分法,来一点点逼近那个导数趋近于0的点,求那个y,但是我再在线编译器上运行结果与样例结果一直不一样,然后我就找别人帮我调试了,结果过了。

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
double n;
double fx(double x)
{
	return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}
void fx1(double x)
{
	double mx=6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-n*x;
	printf("%.4lf\n",mx);
}
double erfen(double x,double y)
{
	double mid,mx;
	while(fabs(fx(x)-n)>1e-6)
	{
		mid=(y+x)/2.0;
		mx=fx(mid);
		if(fabs(mx-n)<1e-6)
		{
			return mid;
		}
		else if(mx<n)
		{
			x=mid;
		}
		else
		{
			y=mid;
		}
	}
	return -1;
}
int main()
{
	int m;
	scanf("%d",&m);
	while(m--)
	{
		scanf("%lf",&n);
		double mx=erfen(0,100);
		fx1(mx);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值