zoukankan      html  css  js  c++  java
  • 【JZOJ6294】动态数点【ST表】【二分答案】

    题目大意:

    题目链接:https://jzoj.net/senior/#main/show/6294
    给出序列aa,求最长的子序列alara_lsim a_r,使得lrlsim r中有一个位置kk能被所有lrlsim r中的数整除。


    思路:

    题外话:和 JZOJ3895 重题了啊。那道题还是之前B组的。
    显然题目就是要我们求出最长的区间[l,r][l,r]使得gcd(alar)=min(alar)gcd(a_lsim a_r)=min(a_lsim a_r)
    直接ST表求出区间gcdgcd和最小值,然后二分答案,转换成一个判定性问题。这个显然是满足单调性的。
    时间复杂度O(nlog2n)O(nlog^2 n)
    **出题人a范围写错,用long long就T了,int就过了color{white} exttt{**出题人a范围写错,用long long就T了,int就过了}


    代码:

    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("inline")
    #include <queue>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int N=500010,LG=20;
    int Min[N][LG+1],Gcd[N][LG+1];
    int n,l,r,mid;
    queue<int> q;
    
    bool check(int len)
    {
    	int k=log2(len);
    	bool ok=0;
    	for (int i=1;i<=n-len+1;i++)
    	{
    		int minn=min(Min[i][k],Min[i+len-(1<<k)][k]);
    		int GCD=__gcd(Gcd[i][k],Gcd[i+len-(1<<k)][k]);
    		if (minn==GCD)
    		{
    			if (!ok)
    			{
    				ok=1;
    				while (q.size()) q.pop();
    			}
    			q.push(i);
    		}
    	}
    	return ok;
    }
    
    int main()
    {
    	freopen("point.in","r",stdin);
    	freopen("point.out","w",stdout);
    	scanf("%d",&n);
    	memset(Min,0x3f3f3f3f,sizeof(Min));
    	for (int i=1;i<=n;i++)
    	{
    		scanf("%d",&Min[i][0]);
    		Gcd[i][0]=Min[i][0];
    	}
    	for (int j=1;j<=LG;j++)
    		for (int i=1;i+(1<<(j-1))<=n;i++)
    		{
    			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
    			Gcd[i][j]=__gcd(Gcd[i][j-1],Gcd[i+(1<<(j-1))][j-1]);
    		}
    	l=1; r=n;
    	while (l<=r)
    	{
    		mid=(l+r)>>1;
    		if (check(mid)) l=mid+1;
    			else r=mid-1;
    	}
    	printf("%d %d
    ",q.size(),l-2);
    	while (q.size()) 
    	{
    		printf("%d ",q.front());
    		q.pop();
    	}
    	return 0;
    }
    
  • 相关阅读:
    Http record java
    Java String constructed from byte array has bad length
    Schema
    Scale-up(纵向扩展) vs Scale-out(横向扩展)
    数据同步
    JDBC and Oracle conn.commit and conn.setAutocommit not working properly
    DGIM
    Github Blog 搭建手册
    软件探索(一)
    经典书单 —— 人文社科
  • 原文地址:https://www.cnblogs.com/hello-tomorrow/p/11998059.html
Copyright © 2011-2022 走看看