How many groups(DP)
题意:
定义:设M为数组a的子集(元素可以重复),将M中的元素排序,若排序后的相邻两元素相差不超过2,则M为a中的一个块,块的大小为块中的元素个数
给出长度为n的数组a,1<=n<=200,1<=ai<=200,可以任选最多两个数(可以不选),将它们值+1或-1。问如何修改可以使得数组中最大的块的大小最大。
思路:
想到是否可以DP:第一维是数组中的下标;由于最多能修改两次,要有一维记录当前状态的修改次数;为了能递推,还要一维记录当前状态的修改值是+1还是-1。
因此定义\(dp[i][j][k]\):对于排序后的数组,前i个数总共修改了j次,将第i个数修改位a[i]+k-1(如果没有修改,k=1),前i数最大块的大小。
P.S. T=1e5,n*T=1e7,需要快读
#include <bits/stdc++.h>
using namespace std;
const int maxn=105;
int a[maxn];
int dp[maxn][3][3];//dp[i][j][k]:到第i个数为止总共修改了j次,将a[i]+k-1后,前i个数的最大集合大小
inline int read() {
int ret = 0, f = 1;
char ch = getchar();
while (ch<'0' || ch > '9') {
if (ch == '-')
f = -f;
ch = getchar();
}
while (ch >= '0'&&ch <= '9') {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret *= f;
}
int main(){
int t,n;
t=read();
for(int kase=1;kase<=t;kase++){
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
sort(a+1,a+1+n);
memset(dp,0,sizeof(dp));
int ans=1;
dp[1][0][1]=dp[1][1][0]=dp[1][1][2]=1;
for(int i=2;i<=n;i++){
for(int j=0;j<=2;j++){//到i改动了j次
if(j==0){//到a[i]一次未改
if(a[i]-a[i-1]<=2)
dp[i][0][1]=dp[i-1][0][1]+1;
else dp[i][0][1]=1;
continue;
}
for(int k=0;k<=2;k++){//a[i]
dp[i][j][k]=1;//初始化为1
for(int l=0;l<=2;l++){//a[i-1]
int ai=a[i]+k-1,aj=a[i-1]+l-1;
if(k==1){//a[i]不修改
if(ai-aj<=2)
dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][l]+1);
}
else{
if(ai-aj<=2)
dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][l]+1);
}
}
}
}
for(int j=0;j<=2;j++)
for(int k=0;k<=2;k++)
ans=max(ans,dp[i][j][k]);
}
printf("Case %d: %d\n",kase,ans);
}
}
How many groups(DP)的更多相关文章
- HDU 4293 Groups (线性dp)
OJ题目:click here~~ 题目分析:n个人分为若干组 , 每一个人描写叙述其所在的组前面的人数和后面的人数.求这n个描写叙述中,最多正确的个数. 设dp[ i ] 为前i个人的描写叙述中最多 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- HDU 4293---Groups(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...
- Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees DP
C. Coloring Trees ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...
- [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)
树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...
- android dp
http://www.see-say.com/viewnews-47657.html http://cn.club.vmall.com/thread-970026-1-1.html http://ww ...
- C. Coloring Trees DP
传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...
- codeforces 484D D. Kindergarten(dp)
题目链接: D. Kindergarten time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
随机推荐
- 探究activity-android学习第三天
转载来源:https://www.jianshu.com/p/f8c68b3c2847?utm_campaign=maleskine&utm_content=note&utm_medi ...
- 洛谷 P1886 滑动窗口(单调队列)
题目链接 https://www.luogu.org/problemnew/show/P1886 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始 ...
- [Linux Test Project] [PATCH] Fix an bad variable name erro in runltp script
[PATCH] Fix an bad variable name erro in runltp script (/opt/ltp/runltp) Hi All, I got an error fo ...
- Codeforces Round #574 (Div. 2) A~E Solution
A. Drinks Choosing 有 $n$ 个人,每个人各有一种最喜欢的饮料,但是买饮料的时候只能同一种的两个两个买(两个一对) 学校只打算卖 $\left \lceil \frac{n}{2} ...
- 项目使用Kafka镜像报错处理记录:this server does not host this topic-partition
背景 项目使用docker swarm部署 服务之间使用消息中间件 kafka 通信 Kafka 使用 star 3.7k 的 wurstmeister/kafka:2.12-2.2.1 镜像 Zoo ...
- C# 字符串Trim进阶
private void button1_Click(object sender, EventArgs e) {//去掉字符串头尾指定字符 string MyInfo= "--中华人民共和国 ...
- 【JAVA】 02-Java对象细节
链接: 笔记目录:毕向东Java基础视频教程-笔记 GitHub库:JavaBXD33 目录: <> <> 内容待整理: 面向过程: 代表语言-c:即通过函数体现,并不断调用函 ...
- oooooooooooooooo
安装后打开mysqld配置项加入skip-grant-tables可以无密码登录,登录进去后修改密码修改成功删除skip-grant-tables mysql> select user, plu ...
- JavaScript面向对象编程(1)-- 基础
自从有了Ajax这个概念,JavaScript作为Ajax的利器,其作用一路飙升.JavaScript最基本的使用,以及语法.浏览器对象等等东东在这里就不累赘了.把主要篇幅放在如何实现JavaScri ...
- 给公司个别安装好的系统环境处理-相当half系统初始化脚本shell
#!/bin/bash# Used for other system-environment update! echo -e '\n\033[35m~~请使用root权限运行此脚本~~\033[0m\ ...