CF - 451B. Sort the Array 排序+模拟

本文介绍了一个编程挑战,任务是判断是否可以通过反转数组中的一段子区间来实现数组的递增排序。文章提供了解决问题的思路和AC代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题目描述:

B. Sort the Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.

Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a.

The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).

Output

Print "yes" or "no" (without quotes), depending on the answer.

If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.

Examples
input
3
3 2 1
output
yes
1 3
input
4
2 1 3 4
output
yes
1 2
input
4
3 1 2 4
output
no
input
2
1 2
output
yes
1 1
Note

Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.

Sample 3. No segment can be reversed such that the array will be sorted.

Definitions

A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].

If you have an array a of size n and you reverse its segment [l, r], the array will become:

a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].


2.题意概述:

给你一段数,要你判断是否能够通过逆序一段字区间使得数严格不递减

3.解题思路:

1、从左到右遍历,第一次出现比前一个数小的下标;
2、然后从右往左遍历,第一次出现比后一个数小的下标;
3、将这段区间逆序,然后判断整个序列是否单调递增

4.AC代码:

#include <stdio.h>
#include <algorithm>
#define maxn 100100
using namespace std;
int a[maxn];
int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		int flag = 0, x = 0, y = 0;
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		for (int i = 0; i < n - 1; i++)
			if (a[i] > a[i + 1])
			{
				x = i;
				break;
			}
		for (int i = n - 1; i > 0; i--)
			if (a[i] < a[i - 1])
			{
				y = i;
				break;
			}
		reverse(a + x, a + y + 1);
		for (int i = 0; i < n - 1; i++)
		{
			if (a[i] > a[i + 1])
			{
				flag = 1;
				break;
			}
		}
		if (flag)
			puts("no");
		else
		{
			printf("yes\n%d %d\n", x + 1, y + 1);
		}


	}
	return 0;


}
## Dymola中计算Fan2ndOrder ## 选取额定工况为1600转 import copy import pandas as pd import numpy as np from scipy.optimize import curve_fit as cf from matplotlib import pyplot as plt ## 将不同转速下的流量-功率曲线和流量-静压曲线拟合成三次型函数 ## 系统的静压曲线使用指数型关系式代替 ALPHA = 0.2 BETA = 0.5 class FanCurve: def __init__(self): self.curve_power = { 1280: [np.float64(-1.0939824906995037e-09), np.float64(2.333022185311005e-05), np.float64(-0.06796629659302983), np.float64(2055.3002100486133)], 1440: [np.float64(-1.136696332147217e-09), np.float64(2.7660598448342913e-05), np.float64(-0.09613228325870021), np.float64(2922.59538328202)], 1500: [np.float64(-1.7603856529124879e-09), np.float64(4.4241229797522305e-05), np.float64(-0.14983705834350444), np.float64(5160.7745241685625)], 1600: [np.float64(-1.1701720186561717e-09), np.float64(3.0605089172086764e-05), np.float64(-0.0989935007927685), np.float64(3869.5082357198503)] } self.curve_dp = { 1280: [np.float64(-3.267975749307142e-10), np.float64(5.2912277771354885e-06), np.float64(-0.03403187464621037), np.float64(741.9041721827268)], 1440: [np.float64(-2.422194407553435e-10), np.float64(3.7698924924279502e-06), np.float64(-0.02559742162953265), np.float64(931.5430549263344)], 1500: [np.float64(-2.3878745827961e-10), np.float64(3.921270379695461e-06), np.float64(-0.02640953385995719), np.float64(1002.1261618905066)], 1600: [np.float64(-2.2348965671136672e-10), np.float64(3.886724444389961e-06), np.float64(-0.027754898177200903), np.float64(1147.392301537067)] } self.speed = [1280, 1440, 1500, 1600] # 有顺序的 不要改 self.alpha = ALPHA self.beta = BETA self.q_min = 0 self.q_max = 24000 def pressure_q(self, q): return self.alpha * q ** self.beta @staticmethod def curve_function(q, a, b, c, d): return a * q **3 + b * q **2 + c * q + d @staticmethod def mk_function(a, b, c, d): # 此函数适用于不插值的计算 def curve_function(q): return a * q ** 3 + b * q ** 2 + c * q + d return curve_function @staticmethod def mk_inter_function(percent, a, b, c, d, e, f, g, h): ## 如果使用插值,就用这个方法构造函数 ## abcd是左侧值的参数,efgh是右侧的 def inter_curve_function(q): l_value = a * q ** 3 + b * q ** 2 + c * q + d r_value = e * q ** 3 + f * q ** 2 + g * q + h return (1 - percent) * l_value + percent * r_value return inter_curve_function @staticmethod def mk_outside_function(coefficient, a, b, c, d): def curve_function(q): return (a * q ** 3 + b * q ** 2 + c * q + d) * coefficient return curve_function def get_functions(self, n): """ 使用转速和系统的阻力特性计算体积流量 转速在性能曲线范围内的采用插值处理 转速在性能曲线外的采用风机相似定律 :param n: :return: """ if 1280 <= n <= 1600: ## 插值得出风机的静压,电功率 if n in self.speed: ## 给定的转速在风机性能曲线内,不需要插值 power_params = self.curve_power[n] power_function = self.mk_function(*power_params) dp_params = self.curve_dp[n] dp_function = self.mk_function(*dp_params) else: ## 给定的转速不在风机性能曲线上,则需要进行插值 new_speed = copy.deepcopy(self.speed) new_speed.append(n) new_speed.sort() idx = 0 for i in range(len(new_speed)): if n == new_speed[i]: idx = i break l_speed = new_speed[idx - 1] r_speed = new_speed[idx + 1] l_power_params = self.curve_power[l_speed] r_power_params = self.curve_power[r_speed] l_dp_params = self.curve_dp[l_speed] r_dp_params = self.curve_dp[r_speed] percent = (n - l_speed) / (r_speed - l_speed) power_function = self.mk_inter_function(percent, *l_power_params, *r_power_params) dp_function = self.mk_inter_function(percent, *l_dp_params, *r_dp_params) else: ## 使用风机相似定律求出风机电功率、静压 if n < 1280: volume_coefficient = n / 1280 else: volume_coefficient = n / 1600 pressure_coefficient = volume_coefficient ** 2 power_coefficient = volume_coefficient ** 3 dp_params = self.curve_dp[1280] power_params = self.curve_power[1280] dp_function = self.mk_outside_function(pressure_coefficient, *dp_params) power_function = self.mk_outside_function(power_coefficient, *power_params) return {"dp_function": dp_function, "power_function": power_function} def solve(self, f, g, q_init=10000, convergence = 1e-06, max_iter = 10000): ## 暂时不对无解情况进行规避 count = 0 def difference(q): return f(q) - g(q) l, r = self.q_min, self.q_max if difference(self.q_min) == 0: return self.q_min if difference(self.q_max) == 0: return self.q_max while True: if difference(l) * difference(r) > 0: raise ValueError("使用二分法时,似乎存在多解,无法求得确切值") if 0 < difference(q_init) <= convergence: break if difference(q_init) * difference(l) > 0: ## 跟左边的值同号 l = q_init q_init = (l + r) / 2 else: r = q_init q_init = (l + r) / 2 count += 1 if count > max_iter: raise TimeoutError("迭代次数已经超过了最大迭代次数") return q_init def run(self, n): functions = self.get_functions(n) q = self.solve(functions["dp_function"], self.pressure_q) return q if __name__ == '__main__': # ## 读取数据 # dataframe_dp = pd.read_excel(r"E:\projects\air_conditioner\fan\v_dp_1600.xlsx") # # # dataframe_power = pd.read_excel(r"E:\projects\air_conditioner\fan\power_1600.xlsx") # # ## 拟合 # # def f_power(x, a, b, c, d): # return a * x **3 + b * x **2 + c * x + d # # # for file_name in ["power_1280", "power_1440", "power_1500", "power_1600"]: # file_path = fr"E:\projects\air_conditioner\fan\{file_name}.xlsx" # dataframe_power = pd.read_excel(file_path) # # x_data = np.array(dataframe_power['x']) # y_data = np.array(dataframe_power['y']) # # a, b, c, d = cf(f_power, x_data, y_data)[0] # xx = [a, b, c, d] # print(file_name, f"x={xx}") n_set = 1400 solution = FanCurve() q = solution.run(n_set) p = solution.pressure_q(q) functions = solution.get_functions(n_set) print("性能曲线压力:", functions["dp_function"](q)) print(q) print("阻力压力:", p) 请帮我看看这段代码中,是如何取不同转速下性能曲线函数中的几个参数值的
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值