結果

問題 No.979 Longest Divisor Sequence
ユーザー okok
提出日時 2020-01-31 22:38:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,649 bytes
コンパイル時間 1,038 ms
コンパイル使用メモリ 97,128 KB
実行使用メモリ 144,892 KB
最終ジャッジ日時 2023-10-17 11:07:50
合計ジャッジ時間 5,787 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 166 ms
142,516 KB
testcase_11 AC 170 ms
142,516 KB
testcase_12 AC 172 ms
142,516 KB
testcase_13 AC 49 ms
5,592 KB
testcase_14 TLE -
testcase_15 AC 931 ms
143,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}


template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;
	
	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}
		
		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}
	
public:
	
	segment_tree(){
		
	}
	
	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}
	
	~segment_tree(){
		
	}
	
	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;
		
		operation = fun;
		
		n = 1;
		
		while(n < n_) n *= 2;
		
		dat.resize(2 * n - 1);
	
		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}
	
	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}
	
};


signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int N, maxA = 0;
	int ans = 0;
	vector<int> A;
	vector<segment_tree<int>> seg;
	
	cin>>N;
	
	A.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>A[i];
		maxA = max(maxA, A[i]);
	}	
	
	seg.resize(maxA+1);
	
	for(int i = 1; i <= maxA; i++){
		// cout<<i<<endl;
		seg[i].init((maxA+i)/i+4,0, [](int first, int second){ return max(first,second);});
	}
	
	for(int i = N-1; i >= 0; i--){
		int maximum = seg[A[i]].query(1, (maxA+A[i])/A[i] + 1);
		// int maximum = 0;
		// for(int j = 0; j < (maxA+A[i])/A[i]; j++){
			// cout<<j<<" "<<seg[A[i]].query(j, j+ 1)<<endl;
		// }
		// cout<<i<<" max = "<<maximum<<endl;
		
		for(int j = 1; j * j <= A[i]; j++){
			if(A[i]%j == 0){
				{
					// int hoge = seg[j].query(A[j]/j-1, A[j]/j);
					
					// hoge = max(hoge, maximum+1);
					
					// seg[j].update(A[i]/j-1, hoge);
					seg[j].update(A[i]/j-1, maximum+1);
				}
				
				{
					int z = A[i]/j;
					// int hoge = seg[z].query(A[j]/z-1, A[j]/z);
					
					// hoge = max(hoge, maximum+1);
					
					// seg[z].update(A[i]/z-1, hoge);
					seg[z].update(A[i]/z-1, maximum+1);
				}
			}
		}
	}
	 
	for(int i = 1; i <= maxA; i++){
		ans = max(ans, seg[i].query(0, (maxA+i)/i + 2));
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0