当前位置:网站首页>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 .
边栏推荐
- Knowledge sorting of exception handling
- Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
- Have time to look at ognl expressions
- Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
- 单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~
- 读写分离-Mysql的主从复制
- [LeetCode]动态规划解分割数组I[Red Fox]
- 管理系统-ITclub(下)
- Gbase 8A OLAP analysis function cume_ Example of dist
- Go from introduction to actual combat -- channel closing and broadcasting (notes)
猜你喜欢

单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~

∫(0→1) ln(1+x) / (x ² + 1) dx

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

Figure countdownlatch and cyclicbarrier based on AQS queue

清华大学教授:软件测试已经走入一个误区——“非代码不可”
How to design an elegant caching function

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear

真香,自从用了Charles,Fiddler已经被我彻底卸载了

开源技术交流丨一站式全自动化运维管家ChengYing入门介绍

AQS SOS AQS with me
随机推荐
记一次List对象遍历及float类型判断大小
How many ways does selenium upload files? I don't believe you have me all!
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
regular expression
Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
Selenium上传文件有多少种方式?不信你有我全!
Method of reading file contents by Excel
QT base64 encryption and decryption
美团20k软件测试工程师的经验分享
[LeetCode]186. Flip word II in string
VMware virtual machine PE startup
JVM memory structure when creating objects
excel读取文件内容方法
[LeetCode]513. 找树左下角的值
[leetcode] dynamic programming solution split integer i[silver fox]
豆沙绿保护你的双眼
Have time to look at ognl expressions
TreeSet details
win11桌面出现“了解此图片”如何删除
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊