寒假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;
}
相关推荐
浮生如梦_1 小时前
Halcon基于laws纹理特征的SVM分类
图像处理·人工智能·算法·支持向量机·计算机视觉·分类·视觉检测
励志成为嵌入式工程师3 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
捕鲸叉3 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer3 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
wheeldown4 小时前
【数据结构】选择排序
数据结构·算法·排序算法
观音山保我别报错5 小时前
C语言扫雷小游戏
c语言·开发语言·算法
TangKenny6 小时前
计算网络信号
java·算法·华为
景鹤6 小时前
【算法】递归+深搜:814.二叉树剪枝
算法
iiFrankie6 小时前
SCNU习题 总结与复习
算法
Dola_Pan7 小时前
C++算法和竞赛:哈希算法、动态规划DP算法、贪心算法、博弈算法
c++·算法·哈希算法