Codeforces 1110D Jongmah [DP]
我…我我把这…这这题切了???
说实话这题的确不难,只是我看到有大佬没做出来有点慌……
突然发现这题是我在洛谷的第500个AC呢。那就更要写篇题解纪念一下了。
思路
容易想到一个贪心:把有三个的都取完,然后随便搞后面的。
这显然是错的……
但你进一步思考发现:对于三元组\(\{x-2,x-1,x\}\),它最多取\(2\)次,否则就可以变成多个\(\{x,x,x\}\)后再重新搞。
那么就容易想到DP了。
设\(dp_{i,j,k}\)表示考虑到第\(i\)位,钦定第\(i\)位已经用掉了\(j\)个,第\(i-1\)位已经用掉了\(k\)个,能得到的三元组个数。
转移时枚举选几个\(\{i-2,i-1,i\}\) ,得到转移方程:
\]
最后\(ans=dp_{m,0,0}\)。
注意DP时一个位置会被钦定两次,所以\(0\leq j,k\leq 4\)。
代码
#include<bits/stdc++.h>
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define sz 1010101
typedef long long ll;
template<typename T>
inline void read(T& t)
{
t=0;char f=0,ch=getchar();
double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.')
{
ch=getchar();
while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();
}
t=(f?-t:t);
}
template<typename T,typename... Args>
inline void read(T& t,Args&... args){read(t); read(args...);}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.txt","r",stdin);
#endif
}
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
int n,m;
int cnt[sz];
int dp[sz][5][5];
int main()
{
file();
int x;
read(n,m);
rep(i,1,n) read(x),++cnt[x];
memset(dp,~0x3f,sizeof(dp));
dp[0][0][0]=0;
rep(i,0,min(4,cnt[1])) dp[1][i][0]=(cnt[1]-i)/3;
rep(i,2,m)
{
rep(j,0,4)
{
if (j>cnt[i]) break;
rep(k,0,4)
{
if (k>cnt[i-1]) break;
rep(l,0,2)
{
if (j+l>cnt[i]||k+l>cnt[i-1]||l>cnt[i-2]||k+l>4) break;
dp[i][j][k]=max(dp[i][j][k],dp[i-1][k+l][l]+(cnt[i]-l-j)/3+l);
}
}
}
}
cout<<dp[m][0][0];
}
Codeforces 1110D Jongmah [DP]的更多相关文章
- Codeforces 1110D Jongmah (DP)
题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 解析:首先我们容易发现,我们 ...
- Codeforces 1110D. Jongmah 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1110D.html 题意 给定 n 个数,每一个数都是在 [1,m] 里的整数. 从中取出形如 {x,x,x ...
- 【Codeforces 1110D】Jongmah
Codeforces 1110 D 题意:给\(n\)个麻将,每个麻将上有一个\(1..m\)的整数\(a_i\). 现在要将这些麻将们分成一个一个三元组,有两种情况: \([i-1,i,i+1]\) ...
- 【Codeforces 1110D】Jongmah FST分析
Codeforces 1110 D FST分析 dotorya.FizzyDavid.MofK.gamegame.matthew99.chokudai.eddy1021.DBradac.Happy_N ...
- Jongmah CodeForces - 1110D
传送门 题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 题解:三个一模一样的 ...
- codeforces 682D(DP)
题目链接:http://codeforces.com/contest/682/problem/D 思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列 ...
- codeforces 666A (DP)
题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...
随机推荐
- Mark Text - 下一代所见即所得的Markdown编辑器
Mark Text 所输及所见,摒弃了众多 markdown 编辑器左边写作右边预览的写作方式,巧妙的将编辑和预览融为一体.snabbdom 作为 Mark Text 的渲染引擎,保证了极速渲染编辑页 ...
- Burpsuite之Burp Collaborator模块介绍
Burp Collaborator.是从Burp suite v1.6.15版本添加的新功能,它几乎是一种全新的渗透测试方法.Burp Collaborator.会渐渐支持blind XSS,SSRF ...
- 小试XML实体注入攻击
基础知识 XML(Extensible Markup Language)被设计用来传输和存储数据.关于它的语法,本文不准备写太多,只简单介绍一下. XML基本知识 1 2 3 4 5 <?xml ...
- 【codeforces 870F】Paths
Description You are given a positive integer n. Let's build a graph on vertices 1, 2, ..., n in such ...
- win7, ubuntu双系统,重装win7后,修复引导
参考: http://blog.csdn.net/abcsunl/article/details/72875983 http://blog.csdn.net/davied9/article/detai ...
- 非关系型数据库mongodb的语法模式
from pymongo import MongoClient #连接 conn = MongoClient() #进入数据库 db = conn.edianzu #连接mydb数据库,没有则自动创建 ...
- 【blog】SpringBoot普通类中如何获取其他bean例如Service、Dao
自己写工具类 工具类 import org.springframework.beans.BeansException; import org.springframework.context.Appli ...
- python 07
1.文件操作: f=open(...) 是由操作系统打开文件,那么如果我们没有为open指定编码,那么打开文件的默认编码很明显是操作系统说了算了 操作系统会用自己的默认编码去打开文件,在windows ...
- 基于netty的一款http server
cicada 基本功能 1.基于注解.注解扫描 2.ioc 对象管理 3.intercepter 拦截器 ref https://github.com/TogetherOS/cicada
- 一张图片资源要占用多大内存xhdpi xxhdpi
一张图片资源要占用多大内存,可以用下面的计算公式计算 4 * withPixel*(targetDensity /sourcedensity) * heightPixel*(targetDensity ...