I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync

时间限制 1000 ms 内存限制 65536 KB

题目描述

We call an array "beautiful array of level L", when all its adjacent number pairs have common factor greater than or equal to L. Obviously, a "beautiful array of level L" is also a "beautiful array of level L−1", and so on. Now you are required to remove some elements from a given array of length n, in order to make it become "beautiful array of level L". Try to figure out the maximum length of the new array.

输入格式

The first line contains only one integer T(1≤T≤5), which indicates the number of test cases.
For each test case:

  • The first line contains two integers n,L(1≤n≤50000,2≤L≤1000000);
  • The second line contains n integers a1,a2,...,an(2≤ai≤1000000).

输出格式

For each test case, output a number in a single line, indicating the maximum number of elements retained in the array.

输入样例

1
5 6
7 16 9 24 6

输出样例

3
【题意】给你一个数组,然你删除尽量少的数,使得剩下的数,相邻的两个数的GCD>=K,数的相对位置不变,问你剩下的数最多有几个。
【分析】我们可以从左往右遍历,对于每个数,我们更新一下它的所有因子最近出现的位置。然后对于当前数,我们找到它的所有因子
然后查找这个因子最近出现的位置,然后从那个地方DP过来,然后更新这个因子的位置。
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define met(a,b) memset(a,b,sizeof a)
#define inf 10000000
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 1e6+;
const double eps = 1e-;
int n,sum[N],m,cnt,k;
int lazy[N],a[N];
int p[N],dp[N];
void solve(int x){
for(int i=;i*i<=a[x];i++){
if(a[x]%i==){
if(i>=m&&p[i]!=)dp[x]=max(dp[x],dp[p[i]]+);
if(a[x]/i>=m&&p[a[x]/i]!=)dp[x]=max(dp[x],dp[p[a[x]/i]]+);
p[i]=x;
p[a[x]/i]=x;
}
}
if(p[a[x]]!=&&a[x]>=m)dp[x]=max(dp[x],dp[p[a[x]]]+);
p[a[x]]=x;
}
int main() {
int T,x,y,xx,yy;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
met(p,);
bool ok=false;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>=m)ok=true;
dp[i]=;
}
for(int i=;i<=n;i++){
if(a[i]<m)continue;
else dp[i]=;
solve(i);
}
int ans=;
for(int i=;i<=n;i++){
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}

北邮校赛 I. Beautiful Array(DP)的更多相关文章

  1. 北邮校赛 H. Black-white Tree (猜的)

    H. Black-white Tree 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...

  2. 北邮校赛 F. Gabriel's Pocket Money(树状数组)

    F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...

  3. Codeforces 1155 D Beautiful Array DP,最大子段和

    题意 给出一个长度为\(n\)的数列和数字\(x\),经过最多一次操作将数列的一个子段的每个元素变为\(a[i]*x\),使该数列的最大子段和最大 分析 将这个数列分为3段考虑,第一段和第三段是未修改 ...

  4. [BNUZOJ1261][ACM][2016北理校赛]方块消除(栈,字符串)

    玩过方块消除游戏吗?现在规定当有两个或两个以上相邻且颜色相同的方块在一起的时候,它们就会产生消除反应.当存在多个消除反应同时产生时,最下的反应先执行.现在只给你其中一列,求最后剩下的方块结果. 输入要 ...

  5. Little Sub and Piggybank (杭师大第十二届校赛G题) DP

    题目传送门 题意:每天能往存钱罐加任意实数的钱,每天不能多于起那一天放的钱数.如果某一天的钱数恰好等于那天的特价商品,则可以买,求最后的最大快乐值. 思路:先来一段来自出题人的题解: 显然的贪心:如果 ...

  6. Nowcoder 北师校赛 B 外挂使用拒绝 ( k次前缀和、矩阵快速幂打表找规律、组合数 )

    题目链接 题意 : 中文题.点链接 分析 : 有道题是问你不断求前缀和后的结果 Click here 这道题问的是逆过程 分析方法雷同.可参考 Click here ----------------- ...

  7. D. Beautiful Array DP

    https://codeforces.com/contest/1155/problem/D 这个题目还是不会写,挺难的,最后还是lj大佬教我的. 这个题目就是要分成三段来考虑, 第一段就是不进行乘,就 ...

  8. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  9. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

随机推荐

  1. [SDOI2011] 染色(Luogu 2486)

    题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例#1: 6 5 2 2 1 2 1 1 1 2 1 3 2 4 2 5 2 6 Q 3 5 C ...

  2. Tensorflow 2.0.0-alpha 安装 Linux系统

    1.TensorFlow2.0的安装测试 Linux Tensorflow Dev Summit 正式宣布 Tensorflow 2.0 进入 Alpha 阶段. 基于 Anaconda 创建环境一个 ...

  3. grunt、Browsersync服务及weinre远程调试

    一.grunt server服务 前端开发时,经常需要把静态文件映射成web服务,传统的做法是丢到apache,但太重太不友好了.开发angular的时候,官方的chrome插件对file:///的支 ...

  4. Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总

    In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try ...

  5. 【R语言学习】时间序列

    时序分析会用到的函数 函数 程序包 用途 ts() stats 生成时序对象 plot() graphics 画出时间序列的折线图 start() stats 返回时间序列的开始时间 end() st ...

  6. XCopy复制文件夹命令及参数详解以及xcopy拷贝目录并排除特定文件

    XCOPY是COPY的扩展,可以把指定的目录连文件和目录结构一并拷贝,但不能拷贝系统文件:使用时源盘符.源目标路径名.源文件名至少指定一个:选用/S时对源目录下及其子目录下的所有文件进行COPY.除非 ...

  7. 【bzoj4552】排序

    二分一个值,然后线段树上模拟. #include<bits/stdc++.h> #define lson (o<<1) #define rson (o<<1|1) ...

  8. VM虚拟机,Linux系统安装tools过程遇到 what is the location of the “ifconfig” program

    安装步骤: 复制到/mnt 解压文件 tar -zxvf VMwareTools-10.1.6-5214329.tar.gz 进入减压文件夹后安装 ./vmware-install.pl ... 一直 ...

  9. 使用Storm实现实时大数据分析(转)

    原文链接:http://blog.csdn.net/hguisu/article/details/8454368 简单和明了,Storm让大数据分析变得轻松加愉快. 当今世界,公司的日常运营经常会生成 ...

  10. POJ 1456 Supermarket(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...