算法训练.

news2024/9/24 1:15:03

一.扩散

题解:

计算点之间的距离,然后对图进行处理即可,这个数据规模较小,因此我使用了floyd,还有最小生成树和二份答案加并查集的写法;

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include<set>
#include <string>
#include<map>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (int  i = h; i <= n; i++) 
#define down(i, h, n) for(int  i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int MaxN = 200005;
constexpr int MaxM = 10005;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x7fffffff;
constexpr double value = 1e-10;


int main() {
	int n;
	int x[55], y[55];
	int e[55][55];
	cin >> n;
	up(i, 1, n) {
		cin >> x[i] >> y[i];
	}
	up(i, 1, n) {
		up(j, 1, n) {
			e[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]);
		}
	}
	up(k, 1, n) {
		up(i, 1, n) {
			up(j, 1, n) {
				e[i][j] = min(max(e[i][k], e[k][j]), e[i][j]);
			}
		}
	}
	int ans = 0;
	up(i, 1, n) {
		up(j, 1, n) {
			ans = max(ans, e[i][j]);
		}
	}
	cout << int(ceil(ans * 1.0 / 2));
	return 0;
}

二.三分 函数

题解:

三分模版,三分和二分的原理相同,不同的是,三分对于已知的l和r,会有两个三等分点的值mid;不过这里值得注意的是一些差值,需要误差很小;

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include<set>
#include <string>
#include<map>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (int  i = h; i <= n; i++) 
#define down(i, h, n) for(int  i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int MaxN = 10005;
constexpr int MaxM = 10005;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x7fffffff;
constexpr double value = 1e-10;

node{
	double a,b,c;
}e[MaxN];
int t, n;

double check(double x) {
	double max1 = e[0].a * x * x + e[0].b * x + e[0].c;
	up(i, 1, n - 1) {
		max1 = max(e[i].a * x * x + e[i].b * x + e[i].c, max1);
	}
	return max1;
}

void slove() {

	cin >> n;
	up(i, 0, n - 1) {
		cin >> e[i].a >> e[i].b >> e[i].c;
	}
	double l = 0, r = 1000;
	while (r-l>value) {
		double mid1 = l + (r - l) / 3.0;
		double mid2 = r - (r - l) / 3.0;
		if (check(mid1) < check(mid2)) r = mid2;
		else l = mid1;
	}
	printf("%.4lf\n", check(l));
	//cout << fixed << setprecision(4) << check(l);
}
int main() {
	
	Ios;
	cin >> t;
	while (t--) {
		slove();
	}
}

三.Queue Sort

题意:

你需要用一种特殊的排序方法对一个长为 n 的数组进行操作。每次操作,你将 a1​ 放在数组中最后一个小于等于它的元素后面(没有就不动)。求这种排序方法是否可以使得数组升序排列?

题解:

当 a1​ 为原始数组中最后一个最小的数时,题目中的操作变得无意义。而此时数组是否升序,取决于原始数组最后一个最小值后的元素是否升序;原始数组最后一个最小值后的元素升序时操作数为这个元素的下标减一,否则无解;

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include<set>
#include <string>
#include<map>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (int  i = h; i <= n; i++) 
#define down(i, h, n) for(int  i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int MaxN = 200005;
constexpr int MaxM = 10005;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x7fffffff;
constexpr double value = 1e-10;

int a[MaxN];
void slove() {

	int n;
	cin >> n;
	int mins = 2e9;
	int ans;
	bool flag = true;
	up(i, 1, n) {
		cin >> a[i];
		if (mins > a[i]) {
			mins = a[i];
			ans = i;
		}
	}
	up(i, ans + 1, n - 1) {
		if (a[i] > a[i + 1]) flag = false;
	}
	if (flag)cout << ans - 1 << endl;
	else cout << -1 << endl;
}

int main() {
	
	Ios;
	int t;
	cin >> t;
	while (t--) {
		slove();
	}
}

四.Querying Multiset

题意:

给定一个集合和 Q 次操作,每个操作可能是以下操作之一:

  • 第一个操作给定整数 x,表示将 x 放入集合。

  • 第二个操作给定整数 x,表示将集合的数分别加上 x。

  • 第三个操作将集合最小的数删除。

对于每个第三个操作,输出你删去的数。

题解:

这几个操作主要是操作二,把根里面的每一个元素遍历更新显然不符合时间复杂度要求,考虑如何把操作二变为单点修改;所以我们不难想到,用一个值 ans来表示当前的增加量,这样操作二可以解决;在这种情况下:对于操作一,把 ans 看作后面插入元素所需的减少量,那么插入的数字 x 可以用 q.push(x-ans) 来代替;对于操作三,只需要输出堆里最小元素加上 ans 的值即可;

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include<set>
#include <string>
#include<map>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (int  i = h; i <= n; i++) 
#define down(i, h, n) for(int  i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int MaxN = 200005;
constexpr int MaxM = 10005;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x7fffffff;
constexpr double value = 1e-10;

ll ans;
priority_queue<long, vector <long>, greater<long>>q;

void slove() {

	int n;
	cin >> n;
	if (n == 1) {
		ll x;
		cin >> x;
		q.push(x-ans);
	}
	else if (n == 2) {
		ll x;
		cin >> x;
		ans += x;
	}
	else {
		cout << q.top() + ans << endl;
		q.pop();
	}
}
int main() {

	int t;
	cin >> t;
	while (t--) {
		slove();
	}
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1996511.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

微信小程序流量主收益

小程序流量主已经开通三天了,目前收益1.42,,,,,,,,,,,,,,,不过确实点击量不怎么多 再附上我的流量主小程序 点击量如果上去的话,收益应该还是可观的,有想开流量主的任何问题都可以骚扰我,,对小程序有任何意见也欢迎反馈~ 一起进步,一起学习~

CentOS linux 安装openssl(openssl拒绝服务漏洞【CVE-2022-0778】解决)

一、安装 1.下载相关openssl包 下载地址&#xff1a; https://www.openssl.org/source/ 2.将下载好的压缩包放到 /app/server/nginx 路径下&#xff08;根据自己实际需求定义&#xff09; 3.切换至该路径 cd /app/server/nginx4.压缩包解压 压缩包解压 &#xff1a;tar -…

矩阵算法的介绍和实现

一. 介绍 首先我们要清楚矩阵是什么&#xff1a;矩阵是一个按照长方阵列排列的复数或实数集合 1> 定义 定义&#xff1a;mn矩阵为mn个数排成的m行n列的表格&#xff0c;当mn时&#xff0c;矩阵A称为n阶方阵或者n阶矩阵。零矩阵&#xff1a;矩阵所有元素都为0。同型矩阵&a…

Centos7.6配置阿里云镜像源

1、备份本地镜像源&#xff0c;将/etc/yum.repos.d/下所有文件备份到/etc/yum.repos.d/bak/下 2、下载阿里云镜像 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo 3、清除yum缓存-yum clean all 4、验证镜像源仓库 yum repolist

Redis漏洞复现【vulhub靶场】

步骤一&#xff1a;打开靶场 进入目录:cd /vulhub-master/redis/4-unacc 启动:docker-compose up -d 检查:docker-compose ps 步骤二&#xff1a;打开kali在kali安装redis程序 #安装redis apt-get install redis #redis链接 redis-cli -h 192.168.4.176-p 6379#redis常见命令 (…

智慧合规与合同管理是未来企业竞争力的关键

在法律和市场规则日益完善的当代&#xff0c;企业合规是公司治理的核心。它像是一道紧箍咒&#xff0c;确保企业遵循法律法规&#xff0c;避开违规风险&#xff1b;同时也是一枚护身符&#xff0c;保护企业免受不必要的诉讼和罚款&#xff1b;更加是企业竞争力的体现&#xff0…

使用 Prometheus 和 Grafana 监控 FastAPI 服务

在现代应用开发中&#xff0c;监控和可视化服务的运行状态和性能指标对于保证系统稳定性至关重要。本文将介绍如何使用 Prometheus 和 Grafana 对 FastAPI 服务进行监控和可视化&#xff0c;并展示如何通过 prometheus_fastapi_instrumentator 将 FastAPI 应用与 Prometheus 集…

zabbix监控1

1、概念 自带图形化界面&#xff0c;通过网页就可以监控所有服务器的状态。 事件告警、邮件通知 web界面提供的分布式监控以及网络监控功能的开源的企业级的软件解决方案 zabbix可以提供各种类型的监控模版&#xff0c;保证服务器的正常运行&#xff0c;灵活的通知机制可以…

惠海 H7303 DCDC线性恒流IC PWM调光无电感低压差大电流 9V 12V 24V 30V球泡灯/转向灯方案

H7303是一种带PWM调光功能的线性降压恒流LED驱动器&#xff0c;仅需外接一个电阻就可以构成一个完整的LED恒流驱动电路,调节该外接电阻就可以调节输出电流&#xff0c;输出电流可调范围为16~2000mA。H7303内置过热保护功能&#xff0c;可有效保护芯片&#xff0c;避免因过热而造…

【经验分享】ShardingSphere+Springboot-04:自定义分片算法(COMPLEX/STANDARD)

文章目录 3.4 CLASS_BASED 自定义类分片算法3.4.1 复杂分片自定义算法&#xff08;strategyCOMPLEX &#xff09;3.4.2 STANDARD 标准分片自定义算法## 进阶:star: 自定义算法范围查询优化 3.4 CLASS_BASED 自定义类分片算法 3.4.1 复杂分片自定义算法&#xff08;strategyCOM…

Windows 10 /11 系统上安装Arc浏览器!超详细的教程

Arc浏览器在在发布以后&#xff0c;给人留下了相当不错的第一印象。 简洁的界面设计,巧妙的操作逻辑,使用过后让人爱不释手,体验出众&#xff01;目前官方提供了Windows 11 和 Mac版 官方下载&#xff1a;【链接直达】 如果你是Windows 10系统&#xff0c;由于官方没有直接提…

加固三防笔记本电脑:保护数据安全的首选设备

随着信息技术的飞速发展&#xff0c;笔记本电脑早已成为现代生活中不可或缺的工具。然而&#xff0c;普通的笔记本电脑无法适应一些特殊的环境&#xff0c;在数据安全保护方面也有着一定的风险。加固三防笔记本电脑则是保护数据安全的首选设备。下面将介绍加固三防笔记本电脑的…

掌控150+KOC账号!游戏厂商深掘ChinaJoy余温,热度再飙升!

4天36.7万人次&#xff0c;抖音话题总播放次数达到10.9亿&#xff01; ChinaJoy&#xff08;以下简称CJ&#xff09;官方给出了第21届CJ的参观总人次和线上流量数据&#xff0c;展现了其应有的“顶流”热度。 作为全球数字娱乐领域最具知名度与影响力的年度盛会&#xff0c;202…

springmvc框架 dispacherServelet容器组件调用

服务器启动时&#xff0c;tomcat创建并自动装配所有生成对象&#xff1a;spring容器放在服务器应用全局中&#xff0c;springmvc容器被放在dispacherServlet容器中。注解解析器在dispacherServlet创建时赋予它识别相关注解并作出相应操作的能力。 浏览器发送请求req&#xff0c…

养老院老人健康信息管理系统 /养老院管理系统

摘 要 随着信息技术和网络技术的飞速发展&#xff0c;人类已进入全新信息化时代&#xff0c;传统管理技术已无法高效&#xff0c;便捷地管理信息。为了迎合时代需求&#xff0c;优化管理效率&#xff0c;各种各样的管理系统应运而生&#xff0c;各行各业相继进入信息管理时代&a…

达梦数据库 DMFLDR的使用

达梦的DMFLDR使用 1.背景2.要求3.实验步骤3.1 参数简介3.2 控制文件3.3 数据文件编写3.4 创建表3.5 执行装载程序3.6 查看运行结果3.7 数据导出控制文件编写3.8 数据导出命令3.9 行列分隔符 4.实验结论 1.背景 用户通过使用快速装载工具能够把按照一定格式排序的文本数据以简单…

门店收银系统源码+同城即时零售多商户入驻商城源码

一、我们为什么要开发这个系统&#xff1f; 1. 商户经营现状 “腰尾部”商户&#xff0c;无小程序运营能力&#xff1b;自营私域商城流量渠道单一&#xff1b;无法和线下收银台打通&#xff0c;库存不同步&#xff0c;商品不同步&#xff0c;订单不同步&#xff1b; 2.平台服…

SpringBoot-application.properties为对象赋值

简单对象赋值 第一种方式 首先让该Bean交由Spring管理,然后加上ConfigurationProperties(prefix"前缀") <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId>&l…

PCIe学习笔记(19)

TLP Prefix&#xff08;前缀&#xff09;规则 以下规则适用于任何包含TLP Prefix的TLP: •对于任何TLP, TLP第0字节的Fmt[2:0]字段值为100b表示存在TLP Prefix, Type[4]位表示TLP Prefix的类型。 ◦Type[4]位的值为0b表示存在Local TLP Prefix ◦Type[4]位的值为1b表示存在…

一、Matlab基础

文章目录 一、Matlab界面二、Matlab窗口常用命令三、Matlab的数据类型3.1 数值类型3.2 字符和字符串3.3 逻辑类型3.4 函数句柄3.5 结构类型3.6 细胞数组 四、Matlab的运算符4.1 算术运算符4.2 关系运算符4.3 逻辑运算4.4 运算符优先级 五、Matlab的矩阵5.1 矩阵的建立5.2 矩阵的…