当前位置:网站首页>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 .
边栏推荐
- The create database of gbase 8A takes a long time to query and is suspected to be stuck
- Array assignment
- 管理系統-ITclub(下)
- Figure countdownlatch and cyclicbarrier based on AQS queue
- [leetcode] dynamic programming solution partition array i[red fox]
- [LeetCode]161. 相隔为 1 的编辑距离
- [sword offer ii] sword finger offer II 029 Sorted circular linked list
- Hash table - sum of arrays
- Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
- Interval DP of Changyou dynamic programming
猜你喜欢
[LeetCode]动态规划解分割数组I[Red Fox]
【Redis】零基础十分钟学会Redis
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
[leetcode] dynamic programming solution split integer i[silver fox]
百万年薪独家专访,开发人员不修复bug怎么办?
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
How to delete "know this picture" on win11 desktop
Simulink method for exporting FMU model files
Experience sharing of meituan 20K Software Test Engineers
登录凭证(cookie+session和Token令牌)
随机推荐
STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
清华大学教授:软件测试已经走入一个误区——“非代码不可”
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
GBase 8a OLAP函数group by grouping sets的使用样例
xpath
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
GBase 8a OLAP分析函数 cume_dist的使用样例
Xiao Wang's interview training task
∫(0→1) ln(1+x) / (x ² + 1) dx
Luogu p5706 redistributing fertilizer and house water
Method of reading file contents by Excel
VMware virtual machine PE startup
Bean paste green protects your eyes
[LeetCode]572. A subtree of another tree
TreeSet details
How to delete "know this picture" on win11 desktop
Example of using gbase 8A OLAP function group by grouping sets
Knowledge sorting of exception handling