北邮校赛 I. Beautiful Array(DP)
I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync
题目描述
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)的更多相关文章
- 北邮校赛 H. Black-white Tree (猜的)
H. Black-white Tree 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...
- 北邮校赛 F. Gabriel's Pocket Money(树状数组)
F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...
- Codeforces 1155 D Beautiful Array DP,最大子段和
题意 给出一个长度为\(n\)的数列和数字\(x\),经过最多一次操作将数列的一个子段的每个元素变为\(a[i]*x\),使该数列的最大子段和最大 分析 将这个数列分为3段考虑,第一段和第三段是未修改 ...
- [BNUZOJ1261][ACM][2016北理校赛]方块消除(栈,字符串)
玩过方块消除游戏吗?现在规定当有两个或两个以上相邻且颜色相同的方块在一起的时候,它们就会产生消除反应.当存在多个消除反应同时产生时,最下的反应先执行.现在只给你其中一列,求最后剩下的方块结果. 输入要 ...
- Little Sub and Piggybank (杭师大第十二届校赛G题) DP
题目传送门 题意:每天能往存钱罐加任意实数的钱,每天不能多于起那一天放的钱数.如果某一天的钱数恰好等于那天的特价商品,则可以买,求最后的最大快乐值. 思路:先来一段来自出题人的题解: 显然的贪心:如果 ...
- Nowcoder 北师校赛 B 外挂使用拒绝 ( k次前缀和、矩阵快速幂打表找规律、组合数 )
题目链接 题意 : 中文题.点链接 分析 : 有道题是问你不断求前缀和后的结果 Click here 这道题问的是逆过程 分析方法雷同.可参考 Click here ----------------- ...
- D. Beautiful Array DP
https://codeforces.com/contest/1155/problem/D 这个题目还是不会写,挺难的,最后还是lj大佬教我的. 这个题目就是要分成三段来考虑, 第一段就是不进行乘,就 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- [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 ...
随机推荐
- 【CodeForces】582 C. Superior Periodic Subarrays
[题目]C. Superior Periodic Subarrays [题意]给定循环节长度为n的无限循环数列,定义(l,s)表示起点为l的长度为s的子串,(l,s)合法要求将子串从该起点开始以s为循 ...
- PHP is_null,empty以及isset,unset的区别
1.empty 判断一个变量是否为“空”.null.false.00.0.’0′.』.为以上值的变量在检测時都将返回true. 2.isset 判断一个变量是否已经设置.0.00.’0′.』.’ ‘. ...
- MSSQL数据库 "无法删除数据库 "***",因为该数据库当前正在使用" 解决方案
--1 创建数据库 create database AAA --2 使用数据库 use AAA --3 在AAA数据库下创建table create table BBB ( bId ,) primar ...
- python string 对齐文本的几个方法
用rjust().ljust()和center()方法对齐文本
- problems when installed mysql in linux ubuntu
reference:http://www.jb51.net/article/87160.htm?pc 1.ERROR 2002 (HY000): Can't connect to local MySQ ...
- caffe Python API 之InnerProduct
net.fc3 = caffe.layers.InnerProduct(net.pool1, num_output=1024, weight_filler=dict(type='xavier'), b ...
- KettleDB连接jdbc连接池参数介绍
http://sheng8407-sina-com.iteye.com/blog/1163245 http://blog.csdn.net/dingxingmei/article/details/41 ...
- vim的各种tips
centos系统,修改vim的配置文件 /etc/vimrc 添加如下内容: 1) 打开 vimrc ,添加以下语句来使得语法高亮显示: syntax on 2) 如果此时语法还是没有高亮显示,那么在 ...
- mysql 安装和配置
mysql 安装: 在命令行输入 sudo apt-get install mysql-server 安装过程中会跳出来一个窗口,输入数据库root用户的密码(必须输入密码) 安装完成后 通过 my ...
- tornado 模版
tornado 模版语法 取消转义 : 取消项目转义 :autoescape = None 取消模版转义:{% autoescape None %} 取消行转义 :{% raw bd %} 强制转 ...