D. Maximum Sum on Even Positions-----------------------思维(类最大子段和)

在这里插入图片描述
在这里插入图片描述
题意:

给定n个数,只能选择一个子段然后翻转,问偶数下标最大的和是多少?

解析:

这道题和最大子段和相似,但是多了一个翻转一段子段。
而且这道题我们选择子段的长度必须是偶数,(因为如果长度是奇数的话,偶数下标对应还是原来的数).

我们先把原数组的偶数下标之和求出来。
然后我们用类似求最大子段和的方法来求出每个偶下标和奇下标翻转对答案带来的贡献

首先如果是偶下标 ,那么我们要和奇下标交换,说明奇下标的数比偶下标的数要大所以带来的贡献就是a[i-1]-a[i]+sum (之前的最大贡献)

如果是奇下标 ,那么我们要和偶下标交换,说明奇下标的数比偶下标的数要大所以带来的贡献就是a[i]-a[i-1]+sum (之前的最大贡献)

最终答案就是:原数组偶数下标之和+奇偶下标交换带来的贡献

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
ll a[N];
int t,n;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		ll ans=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			if(i%2==0) ans+=a[i];
		} 
		ll even=0,old=0,maxv=0;
		for(int i=0;i<n;i+=2)
		{
			old=max(0ll,a[i-1]-a[i]+old);
			maxv=max(maxv,old);
		}
		for(int i=1;i<n;i+=2)
		{
			even=max(0ll,a[i]-a[i-1]+even);
			maxv=max(even,maxv);
		}
		cout<<ans+maxv<<endl;
		
		
	}
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's an implementation of super-direction delay and sum beamforming in Python: ```python import numpy as np def super_direction_delay_and_sum_beamforming(signal, mic_positions, sound_speed, theta, phi, sampling_freq): """ Implementation of super-direction delay-and-sum beamforming for a given signal and microphone positions :param signal: numpy array of shape (num_samples, num_mics) representing the microphone signals :param mic_positions: numpy array of shape (num_mics, 3) representing the 3D positions of the microphones :param sound_speed: speed of sound :param theta: azimuth angle in radians :param phi: elevation angle in radians :param sampling_freq: sampling frequency of the microphone signals :return: numpy array of shape (num_samples,) representing the beamformed signal """ num_mics = mic_positions.shape[0] num_samples = signal.shape[0] # Calculate time delays based on the given azimuth and elevation angles tau = np.zeros(num_mics) for i in range(num_mics): x = mic_positions[i][0] y = mic_positions[i][1] z = mic_positions[i][2] r = np.sqrt(x ** 2 + y ** 2 + z ** 2) tau[i] = r / sound_speed * (np.cos(theta) * np.cos(phi) * x + np.sin(theta) * np.cos(phi) * y - np.sin(phi) * z) # Create a time delay matrix of shape (num_samples, num_mics) t = np.arange(num_samples) / sampling_freq t_mat = np.tile(t, (num_mics, 1)).T tau_mat = np.tile(tau, (num_samples, 1)) delay_mat = np.exp(-2j * np.pi * tau_mat * t_mat) # Apply delay-and-sum beamforming to the microphone signals beamformed_signal = np.sum(delay_mat * signal, axis=1) / num_mics return beamformed_signal ``` You can call this function by passing in the microphone signals, microphone positions, speed of sound, azimuth and elevation angles, and sampling frequency. It will return the beamformed signal. Note that this implementation assumes a uniform linear array of microphones, but can be easily adapted for other array geometries.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值