寒假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;
}
相关推荐
想跑步的小弱鸡4 小时前
Leetcode hot 100(day 3)
算法·leetcode·职场和发展
xyliiiiiL5 小时前
ZGC初步了解
java·jvm·算法
爱的叹息6 小时前
RedisTemplate 的 6 个可配置序列化器属性对比
算法·哈希算法
独好紫罗兰6 小时前
洛谷题单2-P5713 【深基3.例5】洛谷团队系统-python-流程图重构
开发语言·python·算法
每次的天空7 小时前
Android学习总结之算法篇四(字符串)
android·学习·算法
请来次降维打击!!!7 小时前
优选算法系列(5.位运算)
java·前端·c++·算法
qystca7 小时前
蓝桥云客 刷题统计
算法·模拟
别NULL7 小时前
机试题——统计最少媒体包发送源个数
c++·算法·媒体
weisian1518 小时前
Java常用工具算法-3--加密算法2--非对称加密算法(RSA常用,ECC,DSA)
java·开发语言·算法
程序员黄同学9 小时前
贪心算法,其优缺点是什么?
算法·贪心算法