当前位置:网站首页>Acwing week 57 longest continuous subsequence - (binary or tree array)
Acwing week 57 longest continuous subsequence - (binary or tree array)
2022-06-27 22:02:00 【Lovely and beautiful girl】
The longest continuous subsequence
The question :
Just give you an array , Ask you the longest continuous subsequence , Make the sum of this interval > Interval length *100.
reflection :
At that time, seeing this question was a glance question , Subtract all 100, Then seek >0 Subinterval of . For the longest , Just use a tree array . It is the same as the question I have done before : Xiao Yang buys fruit .
Then this question card nlongn How to do it , All the time . Then I thought it was not two points , Does the binary answer satisfy monotonicity , I think of the old one , When the answer is No , Not necessarily the answer on the right is no , So it feels wrong . Actually, this question , For when the length is 5 When there is a plan to meet , So the length is 6 You can do it when you are . That is to say 5 When it doesn't work ,6 Definitely not . Because this view is not just the kind of fixed continuity , The answer in two is at least >=mid.
Code :
Two points :
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 1e6+10,M = 2010;
int T,n,m,k;
int va[N];
int sum[N];
int minn[N];
bool check(int mid)
{
for(int i=mid;i<=n;i++)
{
if(sum[i]>minn[i-mid]) return true;
}
return false;
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++) cin>>va[i];
for(int i=1;i<=n;i++)
{
va[i] -= 100;
sum[i] = sum[i-1]+va[i];
}
for(int i=1;i<=n;i++) minn[i] = inf;
for(int i=1;i<=n;i++) minn[i] = min(minn[i-1],sum[i]);
int l = 0,r = n;
while(l<r)
{
int mid = (l+r+1)>>1;
if(check(mid)) l = mid;
else r = mid-1;
}
cout<<l;
return 0;
}
Tree array timeout :
#include<iostream>
#include<cstring>
#include<string>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
//#pragma GCC optimize(2)
#define eps 1e-9
#define fi first
#define se second
#define pb push_back
#define db double
#define ll long long
#define PII pair<int,int >
#define cin(x) scanf("%d",&x)
#define cout(x) printf("%d ",x)
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int ll
using namespace std;
const int N = 5e6,M = 2000;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const double pai = acos(-1);
int T,n,m,k;
int va[N];
int sum[N];
int tr[N],R = 2e6+5;
vector<int > v;
int get(int x)
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
int bit(int x)
{
return x&(-x);
}
void update(int x,int value)
{
while(x<=R)
{
tr[x] = min(tr[x],value);
x += bit(x);
}
}
int query(int x)
{
int minn = inf;
while(x)
{
minn = min(minn,tr[x]);
x -= bit(x);
}
return minn;
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=R;i++) tr[i] = inf;
for(int i=1;i<=n;i++)
{
cin>>va[i];
va[i] -= 100;
}
for(int i=1;i<=n;i++)
{
sum[i] = sum[i-1]+va[i];
v.pb(sum[i]);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
int maxn = 0;
for(int i=1;i<=n;i++)
{
int now = get(sum[i]);
if(sum[i]>0) maxn = max(maxn,i);
else maxn = max(maxn,i-query(now-1));
update(now,i);
}
cout<<maxn;
return 0;
}
summary :
Think a lot , Don't take it for granted .
边栏推荐
- ∫(0→1) ln(1+x) / (x² + 1) dx
- 深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
- xpath
- Go 访问GBase 8a 数据库的一个方法
- qt 大文件生成md5校验码
- GBase 8a数据库用户密码安全相关参数汇总
- [leetcode] 508. Élément de sous - arbre le plus fréquent et
- GBase 8a V8版本节点替换期间通过并发数控制资源使用减少对系统影响的方法
- It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
- 管理系统-ITclub(中)
猜你喜欢
![[LeetCode]动态规划解分割数组I[Red Fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[LeetCode]动态规划解分割数组I[Red Fox]

Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function

Read write separation master-slave replication of MySQL

STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app

Special training of guessing game

年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数

Go from introduction to practice -- definition and implementation of behavior (notes)

Burp suite遇到的常见问题
![[leetcode] dynamic programming solution partition array ii[arctic fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[leetcode] dynamic programming solution partition array ii[arctic fox]

深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
随机推荐
Go from introduction to practice - error mechanism (note)
Experience sharing of meituan 20K Software Test Engineers
Software defect management - a must for testers
[LeetCode]动态规划解分割数组I[Red Fox]
Simulink method for exporting FMU model files
Dynamic refresh mapper
oracle迁移mysql唯一索引大小写不区分别怕
Have time to look at ognl expressions
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
Interview question 3 of software test commonly used by large factories (with answers)
Oracle migration MySQL unique index case insensitive don't be afraid
Summary of gbase 8A database user password security related parameters
I think I should start writing my own blog.
分享|智慧环保-生态文明信息化解决方案(附PDF)
Go from introduction to practice -- definition and implementation of behavior (notes)
[LeetCode]161. Edit distance of 1
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
[LeetCode]515. Find the maximum value in each tree row
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
[LeetCode]动态规划解拆分整数I[Silver Fox]