算法笔记复现

区间贪心算法

//优先左最大
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=110;//const后面带int
struct inteval{
   
	double x;
	double y;
}s[maxn];
bool cmp(inteval a,inteval b){
   
	if(a.x!=b.x){
   
		return a.x>b.x;//先从左端点从大到小 
	}
	else{
   
		return a.y<b.y;//再从右端点从小到大 
	}	
}
int main(){
   
	int n;
	while(~scanf("%d",&n)){
   
		for(int i=0;i<n;i++){
   
			scanf("%lf %lf",&s[i].x,&s[i].y);//double输入	
		}
		sort(s,s+n,cmp);//检查引用algorithm
		int lx=s[0].x;
		int count=1;
		for(int i=1;i<n;i++){
   //从1开始
			if(s[i].y<lx){
   
				lx=s[i].x;
				count++;
			}
		}
		printf("%d\n",count);//不要忘了输出,输出不要忘了换行 
	} 
	return 0;
}
//优先右最小 
#include <cstdio>
#include <algorithm>
using namespace std;//不能漏 
struct inteval{
   
	double x;
	double y;
}s[110];
bool cmp(inteval a,inteval b){
   
	if(a.y!=b.y){
   
		return a.y<b.y; 
	} 
	else{
   
		return a.x>b.x;
	}
} 
int main(){
   
	int n=0;
	while(scanf("%d",&n)!=EOF){
   
		for(int i=0;i<n;i++){
   
			scanf("%lf %lf",&s[i].x,&s[i].y); //double输入
		}
		sort(s,s+n,cmp);
		int ly=s[0].y;
		int count=1;
		for(int i=1;i<n;i++){
   //从1开始
			if(s[i].x>ly){
   
				ly=s[i].y;
				count++;
			}
		}
		printf("%d\n",count);
	}
	return 0;
}

二分算法

#include <cstdio>
#include <algorithm>
using namespace std;
int binarySearch(int a[],int left,int right,int x){
   
	//a严格递增,非递归,二分查等于 
	//序列中是否存在满足某条件的元素 
	//二分区间[left,right],传入初值[0,n-1]; 
	int mid;
	while(left<=right){
   //这里是为了判断区间是否存在 
		mid=(left+right)/2;//mid=left+(right-left)/2;
		if(x<a[mid]){
   //严格递减:x>mid
			right=mid-1;//right!=mid
		}
		else if(x>a[mid]){
   //严格递减:x<mid
			left=mid-1;//left!=mid
		}
		else if(x==a[mid]){
   
			return mid;
		}
	}
	return -1;
} 
int h_equal(int a[],int left,int right,int x){
   //大等于的第一个数
//上界为n,即所有数都小于x返回n 
//二分区间[left,right],传入初值[0,n]; 
	while(left<right){
   
	//思路:假设存在,它该在的位置。因此是为了让循环一直执行 
		int mid=left+(right-left)/2;
		if(a[mid]>=x){
   
			right=mid;
		}
		else{
   
			left=mid+1;
		}
	}
	return left;//当right=left时停止,因此也可以return right,返回的都是夹出来的位置; 
}
int higher(int a[],int left,int right,int x){
   //大于x的第一个数 
//二分区间 [left,right],传入初值[0,n]; 
	while(left<right){
   
		int mid=left+(right-left)/2;
		if(a[mid]>x){
   
			right=mid;
		}
		else{
   
			left=mid+1;
		}
	}
	return right;//当right=left时停止,因此也可以return right,返回的都是夹出来的位置; 
} 
#define condition (1); 
int conditionSolve(int left,int right){
   //寻找某序列中第一个满足某条件的位置
//以“从左到右先不满足,然后满足”为例 
//二分区间[left,right],初值覆盖解的所有可能值
	while(left<right){
   //对闭区间夹结果来说,left==right则找到唯一位置 
	//左开右闭:while(left+1<right) 
		int mid=left+(right-left)/2;
		if(condition){
   
			right=mid;//条件成立往左找 
		}
		else{
   
			left=mid+1;//条件不成立往右找 
			//左开右闭:left=mid; 
		}
	}
	return left;//返回夹出来的位置 
	//左开右闭:return left+1; 
	//求最后一个满足条件condition的,等于求第一个满足条件!condition的 
} 
int main(){
   
	int n=0;
	int a[20];
	scanf("%d",&n);
	for(int i=0;i<20;i++){
   
		scanf("%d",&a[i]); 
	}
	sort(a,a+n);
	printf("%d\n",binarySearch(a,0,20,n));
	return 0; 
}  

木棒切割问题

#include <cstdio>
int solve(int k,int a[],int n){
   
	int l=0,x=k;
	while(l<a[0]){
   
		if(x>=k){
   
			l++;
			x=0;
			for(int i=0;i<n;i++){
   	
				x+=a
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值