寒假2 蓝桥杯-二分

cpp 复制代码
#include<iostream>
using namespace std;
#include<algorithm>
int n;
int arr[100010];

int f_left(int q)
{
	int a = -1;
	int left = 1, right = n;
	int mid;
	while (left < right)
	{
		mid = (left + right) / 2;
		if (arr[mid] == q)
		{
			right = mid - 1;
			//cout << "1";
		}
		else if (arr[mid] < q)
		{
			left = mid+1;
			//cout << "2";
		}
		else
		{
			right = mid - 1;
			//cout << "3";
		}
	}
	return left;
}

int f_right(int q)
{
	int a = -1;
	int left = 1, right = n;
	int mid;
	while (left < right)
	{
		mid = (left + right) / 2;
		if (arr[mid] == q)
		{
			left = mid + 1;
			//cout << "4";
		}
		else if (arr[mid] > q)
		{
			
			right = mid - 1;
			//cout << "5";
		}
		else
		{
			left = mid + 1;
			//cout << "6";
		}
	}
	return right;
}
int main()
{
	int q;
	cin >> n >> q;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
	}
	int left = f_left(q);
	int right = f_right(q);
	if (left ==-1&&right==-1)
	{
		cout << "-1 -1" << endl;
	}
	else
	{
		cout << left << " " << right << endl;
		cout << right - left  << endl;
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int n;
int arr[60];
void f(int q)
{
	int left = 1;
	int right = n;
	int mid;
	int ans;
	while (left < right)
	{
		mid = (left + right) / 2;
		if (arr[mid] < q)
		{
			if (arr[mid + 1] > q)
			{
				cout << mid+1 << endl;
				return;
			}
			left = mid + 1;
		}
		else
		{
			if (arr[mid - 1] < q)
			{
				cout << mid << endl;
				return;
			}
			right = mid - 1;
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
	}
	int q;
	cin >> q;
	if (q < arr[1])
	{
		cout << 1 << endl;
	}
	else if (q > arr[n])
	{
		cout << n + 1 << endl;
	}
	else
	{
		f(q);
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<algorithm>
int n, k;
int arr[100010];
int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
	}
	int left = 1;
	int right = 100000000;
	int mid;
	int ans = 0;
	while (left <= right)
	{
		int res = 0;
		mid = (left + right) / 2;
		for (int i = 1;i <= n;i++)
		{
			res += (arr[i] / mid);
			if (res > k)break;
		}
		if (res == k)
		{
			if (mid > ans)ans = mid;
			left = mid + 1;
		}
		else if (res > k)
		{
			if (mid > ans)ans = mid;
			left = mid + 1;
		}
		else
		{
			
			right = mid - 1;
		}
	}
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int arr[27];
int main()
{
	int n;
	cin >> n;
	char ch;
	for (int i = 0;i < n;i++)
	{
		cin >> ch;
		if (int(ch) >= 65 && int(ch) <= 90)
		{
			arr[int(ch) - 64] = 1;
		}
		else
		{
			arr[int(ch) - 96] = 1;
		}
	}
	for (int i = 1;i <= 26;i++)
	{
		if (arr[i] != 1)
		{
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}
相关推荐
yuanManGan1 小时前
数据结构漫游记:静态链表的实现(CPP)
数据结构·链表
火星机器人life2 小时前
基于ceres优化的3d激光雷达开源算法
算法·3d
虽千万人 吾往矣2 小时前
golang LeetCode 热题 100(动态规划)-更新中
算法·leetcode·动态规划
arnold663 小时前
华为OD E卷(100分)34-转盘寿司
算法·华为od
ZZTC3 小时前
Floyd算法及其扩展应用
算法
lshzdq4 小时前
【机器人】机械臂轨迹和转矩控制对比
人工智能·算法·机器人
2401_858286115 小时前
115.【C语言】数据结构之排序(希尔排序)
c语言·开发语言·数据结构·算法·排序算法
猫猫的小茶馆5 小时前
【数据结构】数据结构整体大纲
linux·数据结构·算法·ubuntu·嵌入式软件
u0107735145 小时前
【字符串】-Lc5-最长回文子串(中心扩展法)
java·算法
帅逼码农5 小时前
K-均值聚类算法
算法·均值算法·聚类