当前位置:网站首页>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 .
边栏推荐
- [leetcode] dynamic programming solution split integer i[silver fox]
- 神奇的POI读取excel模板文件报错
- This set of steps for performance testing using JMeter includes two salary increases and one promotion
- Simulink导出FMU模型文件方法
- Summary of Web testing and app testing by bat testing experts
- [sword offer ii] sword finger offer II 029 Sorted circular linked list
- regular expression
- Go 访问GBase 8a 数据库的一个方法
- 年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数
- Go from introduction to actual combat - only any task is required to complete (notes)
猜你喜欢

Summary of Web testing and app testing by bat testing experts

Special training of guessing game

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

我想我要开始写我自己的博客了。
![\W and [a-za-z0-9_], \Are D and [0-9] equivalent?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?
![\w和[A-Za-z0-9_],\d和[0-9]等价吗?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\w和[A-Za-z0-9_],\d和[0-9]等价吗?

管理系統-ITclub(下)

Go from introduction to practice - error mechanism (note)

I think I should start writing my own blog.

Luogu p5706 redistributing fertilizer and house water
随机推荐
Null pointer exception
Gbase 8A OLAP analysis function cume_ Example of dist
vmware虚拟机PE启动
∫(0→1) ln(1+x) / (x² + 1) dx
Installing Oracle11g under Linux
[LeetCode]515. Find the maximum value in each tree row
Magic POI error in reading excel template file
Simulink method for exporting FMU model files
Go from introduction to actual combat - task cancellation (note)
[leetcode] dynamic programming solution partition array ii[arctic fox]
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
Go from introduction to practice -- shared memory concurrency mechanism (notes)
Simulink导出FMU模型文件方法
Have time to look at ognl expressions
Express e stack - small items in array
[LeetCode]30. 串联所有单词的子串
Quick excel export according to customized excel Title Template
TreeSet details
[LeetCode]100. Same tree
软件缺陷管理——测试人员必会