当前位置:网站首页>Niuke network: longest continuous subarray with positive product
Niuke network: longest continuous subarray with positive product
2022-06-30 16:39:00 【lsgoose】
1. Non ring


Pay attention to the examination !!
Here is the length of the longest continuous product to be a positive number , We maintain two lengths , One pos One neg,pos Represents the product of the longest positive number up to the current number ,neg Represents the product of the longest negative number up to the current number , We do the following for each number :
1. If the current number is positive , Then the length of the longest positive sequence can be increased by one , If there is the longest negative sequence , Plus one
2. If the current number is 0, Then the longest positive and negative sequences of all sequences ending with this number are 0, Therefore, all are updated to 0
3. If the current number is negative , You need to exchange pos and neg
The specific code is as follows :
#include<bits/stdc++.h>
using namespace std;
int dp(vector<int> &nums){
int res=0;
int pos=nums[0]>0 ? 1: 0;
int neg=nums[0]<0 ? 1: 0;
for(int i=1;i<nums.size();++i){
if(nums[i]>0){
pos=pos+1;
neg=neg>0 ? neg+1 : 0;
}else if(nums[i]==0){
pos=neg=0;
}else{
int tmp=neg>0 ? neg+1:0;
neg=pos+1;
pos=tmp;
}
res=max(res,pos);
}
return res;
}
int main(){
int n;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;++i){
cin>>nums[i];
}
cout<<dp(nums)<<endl;
return 0;
}2. Ring array


The idea here refers to the answers in Niuke .
First , We all know how to find the maximum sum of acyclic arrays , It's just linear dp, The maximum length of each update that ends with the current number , So what if it's cyclic ? Each time we use a new starting point to do linear dp We can work out .
The specific code is as follows :
#include<bits/stdc++.h>// Timeout code
using namespace std;
int solve(int n, vector<int> &v, vector<int> &dp);
int process2(int start, vector<int>&v);
int main(){
int n;
cin>>n;
vector<int> v(n);
for (int i = 0; i < n; ++i){
scanf("%d",&v[i]);
}
vector<int> dp(n, INT_MIN);
cout<<solve(n,v, dp)<<endl;
return 0;
}
int solve(int n, vector<int> &v, vector<int> &dp){
int ans = process2(0, v);
for(int i = 1; i < n; ++i){
ans = max(ans,process2(i,v));
}
return ans;
}
int process2(int start, vector<int>&v){
int ans = v[start];
for(int i = 1; i < v.size(); ++i){
ans = max(ans+v[(start + i)%v.size()],v[(start+i)%v.size()]);
}
return ans;
}
The time complexity is zero O(n^2), Will timeout .
- Switch views , According to the general idea of maximum continuous sum , That is to say dp Can get the largest continuous subarray and .
- Empathy , If we find the sum of the smallest continuous subarray , You will find a feature .
- Hypothetical use sum Record the sum of the entire array , Then the largest continuous subarray and dpmax And minimum continuous subarray dpmin And must be connected , Because suppose you don't want to connect , The middle paragraph is either larger than 0, Then it should belong to the largest subarray continuous sum , Otherwise it should belong to the smallest continuous subarray and .
- So we finally get sum,dpmax,dpmin It can be judged later . hypothesis dpmax+dpmin<sum This means that the sum of the endpoints on both sides of the array is positive , So this part should be left to (dpmax=sum−dpmin), Otherwise go straight back to dpmax that will do , Of course, there is a special case ( Initializing dpmax The first value of the array is given v[0], If it is negative , There must be dpmax+dpmin<sum Therefore, this place should be judged specially , If dpmax<0 Just go back )
The idea is as follows :
1. With ring : First, find out the minimum value of continuous subarray in the case of acyclic res2, Then use an array and sum Subtract the minimum res2 That is, the maximum value of the continuous subarray in the ring case
2. Acyclic case : Maximum subarray and
3. The largest circular subarray and = max( Maximum subarray and , The sum of the arrays - Minimal array and ), To exclude all negative numbers = Maximum subarray and
Link to the original text :https://blog.csdn.net/qq_35915636/article/details/123974337
#include<bits/stdc++.h>
using namespace std;
int dp(vector<int> &nums){
int sum, dpmx, dpmn, mx, mn;
dpmx=dpmn=mx=mn=sum=nums[0];
for(int i=1;i<nums.size();++i){
mx=max(mx+nums[i],nums[i]);
mn=min(mn+nums[i],nums[i]);
dpmx=max(dpmx, mx);
dpmn=min(dpmn, mn);
sum+=nums[i];
}
if(dpmx<0) return dpmx;
if(dpmx+dpmn<sum) dpmx=sum-dpmn;
return dpmx;
}
int main(){
int n;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;++i){
cin>>nums[i];
}
cout<<dp(nums)<<endl;
return 0;
}
边栏推荐
- 从第三次技术革命看企业应用三大开发趋势
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- [machine learning] K-means clustering analysis
- register_chrdev和cdev_init cdev_add用法区别
- Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
- [cve-2019-0193] - Apache Solr dataimport remote command execution analysis
- 抖快B为啥做不好综艺
- After 15 years of working on 21 types of hardware, where is Google?
- 什么是XR扩展现实,XR云串流平台有哪些
- 微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
猜你喜欢

Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)

Open source STM32 USB-CAN project
![[machine learning] K-means clustering analysis](/img/5f/3199fbd4ff2129d3e4ea518812c9e9.png)
[machine learning] K-means clustering analysis

搬运两个负载均衡的笔记,日后省的找

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计

What is XR extended reality and what are the XR cloud streaming platforms

I implement "stack" with C I

The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning

'&lt;', Hexadecimal value 0x3c, is an invalid problem solving

What is the difference between real-time rendering and pre rendering
随机推荐
《网络是怎么样连接的》读书笔记 - 汇总篇
思源笔记:能否提供页面内折叠所有标题的快捷键?
Installing jupyter notebook under Anaconda
[附下载]渗透测试神器Nessus安装及使用
Half year inventory of new consumption in 2022: the industry is cold, but these nine tracks still attract gold
CVPR 2022丨特斯联AI提出:基于图采样深度度量学习的可泛化行人重识别
Policy Center > Device and Network Abuse
CloudXR如何推动XR的未来发展
大学生研究生毕业找工作,该选择哪个方向?
Mysql代理中间件Atlas安装和配置
microblaze 串口学习·2
CVPR 2022 - Tesla AI proposed: generalized pedestrian re recognition based on graph sampling depth metric learning
[machine learning] K-means clustering analysis
In depth analysis of the core code of the gadgetinspector
Dart: string replace related methods to solve replacement characters
招标公告:深圳市财政局数据库异地灾备项目
如何得到股票开户的优惠活动?在线开户安全么?
'&lt;', Hexadecimal value 0x3c, is an invalid problem solving
Interpretation of gaussdb's innovative features: partial result cache accelerates operators by caching intermediate results
Li Zexiang, a legendary Chinese professor, is making unicorns in batches