题意:

定义:设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)的更多相关文章

  1. HDU 4293 Groups (线性dp)

    OJ题目:click here~~ 题目分析:n个人分为若干组 , 每一个人描写叙述其所在的组前面的人数和后面的人数.求这n个描写叙述中,最多正确的个数. 设dp[ i ] 为前i个人的描写叙述中最多 ...

  2. 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 ...

  3. HDU 4293---Groups(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...

  4. 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 ...

  5. 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 ...

  6. [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...

  7. android dp

    http://www.see-say.com/viewnews-47657.html http://cn.club.vmall.com/thread-970026-1-1.html http://ww ...

  8. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

  9. codeforces 484D D. Kindergarten(dp)

    题目链接: D. Kindergarten time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

随机推荐

  1. 2019Flutter面试题最新整理大全(含答案)

    一.前言2019年行将结束,也该规划一下自己的职业生涯了:是选择继续从事Android(Android的话已经火了几年了,现在算是进入寒冬了,需要考虑清楚)?还是学习新的跨平台开发Flutter技术? ...

  2. dp(最长升序列:二分查找时间优化nlogn)

    We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...

  3. hdu6341 Problem J. Let Sudoku Rotate (dfs)

    题目传送门 题意: 给你16个16宫格的数独,里面是0~F,你可以逆时针旋转里面的每个16宫格 问你它是从标准数独逆时针旋转多少次得到? 思路: 可以知道每个16宫已经是标准的了,接下来只要考虑每行. ...

  4. 解决python中转化成json的方法不能序列化datetime类型数据(转)

    Python自带的json.dumps方法序列化数据时候如果格式化的数据中有datetime类型数据时候会提示错误TypeError: datetime.datetime(2012, 12, 12, ...

  5. ubuntu 安装php xdebug

    windows 安装xdebug https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html 一.下载 下载与PHP版兼容的Xdeb ...

  6. Linux 给用户 赋某个文件夹操作的权限

    https://my.oschina.net/cqyj/blog/1796047 在root用户登录的情况,赋予opt目录给liuhai这个用户权限 示例代码: # 将目录/opt 及其下面的所有文件 ...

  7. ubuntu 自带截图工具快捷键盘

    PrtSc – 获取整个屏幕的截图并保存到 Pictures 目录. Shift + PrtSc – 获取屏幕的某个区域截图并保存到 Pictures 目录. Alt + PrtSc –获取当前窗口的 ...

  8. BZOJ2695 保护古迹

    非常带劲之计算几何 写的头晕= = 就是平面图转对偶图然后最小割 由于p非常小我们枚举所有保护状态然后割一下 建图真的烦 就是把区域划分出来看一下每一个古迹点是否被小区域包含[好像也可以写点定位] 然 ...

  9. 用pycharm运行pytest

    安装pytest 1. 在pycharm中建项目,建文件,文件名字要以test_开头 2.在文件中插入pytest模块 import pytest #引用pytest模块 3.定义test函数,以及断 ...

  10. Python中使用"subplot"在一张画布上显示多张图

    subplot(arg1, arg2, arg3) arg1: 在垂直方向同时画几张图 arg2: 在水平方向同时画几张图 arg3: 当前命令修改的是第几张图 t = np.arange(0,5,0 ...