Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah
Time Limit: 3000 mSec
Problem Description
Input
Output
Print one integer: the maximum number of triples you can form.
Sample Input
10 6
2 3 3 3 4 4 4 5 5 6
Sample Output
3
题解:动态规划,对这种状态定义不熟悉,主要还是没有发现最优方案所具有的性质,其实个人感觉dp往往是在一定的观察的基础上进行的,发现最优结果所必须具有的一些特征,然后利用这种特征减少状态总数,使得dp能够进行。这道题的一个性质就是我们可以让最优结果中对每个i,不会出现三个及三个以上(i, i+1, i+2)型的三元组,因为这样的三元组可以被3个i,3个i+1,3个i+2所替代,而不产生任何影响,这样一来我们就可以对每个i,枚举它所组成的三元组的个数,具体状态定义:
dp[i][j][k],考虑前i种数,(i-1, i, i+1)有j个,(i, i+1, i+2),有k个的最多三元组数
不记录(i-2, i-1, i)的个数是因为在状态转移时用不到,用dp[i][j][k]更行dp[i+1][k][t]即可。转移方程很简单,详见代码。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0);
const int SIZE = + ;
const LL MOD = ; int n, m;
int a[maxn];
int dp[maxn][][]; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n >> m;
int x;
for (int i = ; i < n; i++)
{
cin >> x;
a[x]++;
}
memset(dp, -INF, sizeof(dp));
dp[][][] = ;
for (int i = ; i <= m; i++)
{
for (int j = ; j < ; j++)
{
for (int k = ; k < ; k++)
{
int lim = a[i + ] - j - k;
if (lim < )
continue;
for (int t = ; t < && t <= lim; t++)
{
dp[i + ][k][t] = max(dp[i + ][k][t], dp[i][j][k] + t + (lim - t) / );
}
}
}
}
cout << dp[m + ][][] << endl;
return ;
}
Codeforces Global Round 1 - D. Jongmah(动态规划)的更多相关文章
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- 【Codeforces Round 1110】Codeforces Global Round 1
Codeforces Round 1110 这场比赛只做了\(A\).\(B\).\(C\),排名\(905\),不好. 主要的问题在\(D\)题上,有\(505\)人做出,但我没做出来. 考虑的时候 ...
- Codeforces Global Round 11【ABCD】
比赛链接:https://codeforces.com/contest/1427 A. Avoiding Zero 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正 ...
随机推荐
- 菜鸟入门【ASP.NET Core】7:WebHost的配置、 IHostEnvironment和 IApplicationLifetime介绍、dotnet watch run 和attach到进程调试
WebHost的配置 我们用vs2017新建一个空网站HelloCore 可以使用ConfigureAppConfiguration对配置进行更改,比如说添加jsonfile和commandline配 ...
- .NET JSON 转换 Error ” Self referencing loop detected for type“
在进行实体转换为Json格式报错如下图: Self referencing loop detected for property 'md_agent' with type 'System.Data.E ...
- java基础之XML
目录 java基础之XML 1. XML解析概述 2. DOM4J介绍 2.1 常用包 2.2 内置元素 2.2 Element类 2.3 Attribute类 2.4 常用操作 3. 代码演示 3. ...
- 【18】观察者模式(Observer Pattern)
一.引言 在现实生活中,处处可见观察者模式.例如,微信中的订阅号,订阅博客和QQ微博中关注好友,这些都属于观察者模式的应用.在这一章将分享我对观察者模式的理解,废话不多说了,直接进入今天的主题. 二. ...
- returnFunc.js
var cyn = function(){ console.log("one") return function(){ console.log("two") } ...
- js之模态对话框
目标效果:点击页面按钮,显示模态对话框,在模态对话框里点击取消关闭模式对话框. 效果如下 实现代码如下: <!DOCTYPE html> <html lang="en&qu ...
- PHP7.27: connect mysql 5.7 using new mysqli
<!doctype html> <html> <head> <meta name="viewport" content="wid ...
- 2017-11-20 中文代码示例之Vuejs入门教程(一)问题后续
"中文编程"知乎专栏原文 第一个issue: Error compiling template if using unicode naming as v-for alias · I ...
- [VUE ERROR] Invalid prop: type check failed for prop "list". Expected Array, got Undefined
错误原因: 子组件 props -> list 要求接收的数据类型是 Array, 然而实际接收到的是 Undefined. 子组件代码: props: { list: { type: Arra ...
- python第六十天-----RabbitMQ
RabbitMQ消息队列:默认为消息轮循模式,按client端启动是顺序接收 server端 import pika connection = pika.BlockingConnection(pika ...