F. Clique in the Divisibility Graph DP
http://codeforces.com/contest/566/problem/F
1 second
256 megabytes
standard input
standard output
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Print a single number — the maximum size of a clique in a divisibility graph for set A.
8
3 4 6 8 10 18 21 24
3
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
设DP[i]表示第i个数字结尾的最大合法情况。
那么递推过来的话,要在前i - 1个数字中,是a[i]约数的,才能递推过来dp[i],那么需要把a[i]分解因子。这样要sqrtn复杂度。超时。
可以考虑以第a[i]个数递推去后面,就是去更新[i + 1,以后的数字。
那么只有k * a[i]的才能递推过去。
但是有些会重复,
3 4 24这样,24既可以从3递推过来,也可以从4递推过来。那么娶个max即可。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e6 + ;
int a[maxn];
int add[maxn];
int calc(int val) {
int en = (int)sqrt(val * 1.0);
int res = ;
for (int i = ; i <= en; ++i) {
if (val % i == ) {
res = max(res, add[i]);
res = max(res, add[val / i]);
}
}
return res;
}
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
int ans = ;
for (int i = ; i <= n; ++i) {
add[a[i]] += ;
for (int j = * a[i]; j <= maxn - ; j += a[i]) {
add[j] = max(add[j], add[a[i]]);
}
ans = max(ans, add[a[i]]);
}
// for (int i = 1; i <= n; ++i) {
// printf("%d ", add[a[i]]);
// }
printf("%d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
F. Clique in the Divisibility Graph DP的更多相关文章
- Codeforces.566F.Clique in the Divisibility Graph(DP)
题目链接 \(Description\) 给定集合\(S=\{a_1,a_2,\ldots,a_n\}\),集合中两点之间有边当且仅当\(a_i|a_j\)或\(a_j|a_i\). 求\(S\)最大 ...
- 周赛-Clique in the Divisibility Graph 分类: 比赛 2015-08-02 09:02 23人阅读 评论(3) 收藏
Clique in the Divisibility Graph time limit per test1 second memory limit per test256 megabytes inpu ...
- Codeforces 566F Clique in the Divisibility Graph
http://codeforces.com/problemset/problem/566/F 题目大意: 有n个点,点上有值a[i], 任意两点(i, j)有无向边相连当且仅当 (a[i] mod a ...
- Codeforces 835 F Roads in the Kingdom(树形dp)
F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...
- UVA11324 The Largest Clique[强连通分量 缩点 DP]
UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...
- POJ 1745 Divisibility (线性dp)
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10598 Accepted: 3787 Des ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- AGC 016 F - Games on DAG(状压dp)
题意 给你一个有 \(n\) 个点 \(m\) 条边 DAG 图,点的标号和拓扑序一致. 现在有两个人进行博弈,有两个棋子分别在 \(1, 2\) 号点上,需要不断移动到它指向的点上. 如果当前两个点 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) F - Uniformly Branched Trees 无根树->有根树+dp
F - Uniformly Branched Trees #include<bits/stdc++.h> #define LL long long #define fi first #de ...
随机推荐
- 【POJ 3580】SuperMemo Splay
题意 给定$n$个数,$m$个询问,每次在$[L,R]$区间加上一个数,或者反转一个区间$[L,R]$,或者循环右移区间$[L,R]$共$T$次,或者在第$x$个数后插入一个数$p$,或者删除第$x$ ...
- cookie的保存与提取
爬虫过程中,cookie可以保留用户与服务器之间的交互信息,使服务器与用户相互能够识别.由于HTTP协议是无状态协议,即不能够识别客户端身份,即使客户端多次请求同一个url服务器仍然响应.这种协议导致 ...
- codeforces 569A A. Music(水题)
题目链接: A. Music time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- linux命令学习笔记(51):lsof命令
lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在, 通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控制协 ...
- homebrew cask安装launch rocket【转】
简介 brew cask是一个用命令行管理Mac下应用的工具,它是基于homebrew的一个增强工具. homebrew可以管理Mac下的命令行工具,例如imagemagick, nodejs,如下所 ...
- linux 几个逼格高实用的命令
1.xargs [root@gdpsq1x25 log]# find . -type f -name "*.log" |sort -rn./yum.log./sssd/sssd_s ...
- failed to create rwlayer: lstat /var/lib/docker/overlay2/ no such file or directory
在使用Docker构建微服务镜像时出现的错误.第一天构建好好的,第二天就出现了这样的错误.通过百度这条错误的信息非常少,只在 stackoverflow.com 上找到一条,问题指向了 dockerf ...
- Uncommon Words from Two Sentences
https://leetcode.com/problems/uncommon-words-from-two-sentences We are given two sentences A and B. ...
- 《Java多线程编程核心技术》读后感(三)
synchronized同步语句块 用synchronized声明方法在某些情况下是有弊端的,比如A线程调用同步方法执行以一个长时间的任务,那么B线程则必须等待比较较长的时间.在这样的情况下可以使用s ...
- 7.10实习培训日志-Maven 敏捷编程
总结 今天早上主要学习了Maven和Idea的Docker插件,遇到了一些坑,对于Idea的Docker插件,不能下载,然后我去访问Idea插件官网,发现被墙了,只要开个VPN就好.下午主要是张总经理 ...