Codeforces 455A Boredom 取数字的dp

题目链接:点击打开链接

给定一个n长的序列

删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失,即无法获得这2个数的分数

问最高得分。

先统计每个数出现的次数,然后dp一下,对于每个数只有取或不取2种状态。

#include <algorithm>
#include <cctype>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <climits>
#include <vector>
#include<iostream>
#include <queue>
using namespace std;
#define ll long long
ll hehe;
#define N 100005
ll num[N], n;
ll dp[N][2];
void work(){
	for(ll i = 1; i < N; i++){
		dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
		if(num[i]) {
			if(i-2>=0)
			dp[i][1] = max(dp[i-2][0], dp[i-2][1])+num[i]*i;
			dp[i][1] = max(dp[i][1], dp[i-1][0] + num[i]*i);
		}
	}
	cout<<max(dp[N-1][0], dp[N-1][1])<<endl;
}
int main(){
	ll i, j, u;
	while(cin>>n){
		memset(num, 0, sizeof num);
		memset(dp, 0, sizeof dp);
		for(i = 1; i <= n; i++)
		{
			scanf("%I64d", &u);
			num[u] ++;
		}
		work();
	}
    return 0;
}  

Codeforces 455A Boredom 取数字的dp

时间: 2024-08-28 14:21:49

Codeforces 455A Boredom 取数字的dp的相关文章

Codeforces 455A Boredom (dp)

很裸的dp 状态转移方程 dp[i]=max(dp[i-1],dp[i-2]+dp[i]*i) #include<bits/stdc++.h> using namespace std; long long dp[100020]; int main() { int n,a; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a); dp[a]++; } for(int i=2;i&

Codeforces 455A - Boredom

Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it. Given a sequence a consisting of n integers. The player can make several steps. In a single s

codeforces 514E Darth Vader and Tree (dp+快速幂)

codeforces 514E Darth Vader and Tree (dp+快速幂) 题意: 有一棵树,每个节点有n个儿子,给出父亲到每个儿子的距离di,问离祖先距离不超过x的子孙有多少个(子孙包括祖先)对1e9+7取模. 限制: 1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100 思路: 因为di <= 100,所以可以用快速幂来处理这道题, 大概过程为:先用dp算出前100的答案,剩下的用快速幂来处理.

CodeForces 540D Bad Luck Island 概率dp

CodeForces 540D 应该是简单概率dp,由于写得少显得十分蠢萌 求期望逆推,求概率正推,大概是这么个意思,贴一发留恋 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define db double const int maxn=108; db dp[maxn][maxn][maxn]; int main() { int i,j,n,m,k,p; whi

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who

CodeForces 21D Traveling Graph 状压dp+欧拉回路

题目链接:点击打开链接 题意: 给定n个点m条边的无向图 求从1点开始经过每条边至少一次最后回到1点的最小路程 显然就是找一条路径可重复的欧拉回路 思路: 首先对于欧拉回路的结论是:所有点的度数都为偶数 因为所有边至少经过一次,那么可以把题意转换成加最少多少条边使得图满足以上结论 而加的边目的是为了把奇度数转成偶度数,先floyd一下得到任意点间加边的最小花费 dp[i]表示状态i下度数都为偶数的最小花费. 状压dp,把i状态下,所有未选择的点中挑2个奇度数的转移即可. #include <cs

BZOJ 1566 管道取珠(DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1566 题意: 思路:假设得到同一个序列S 的方案有ai=3,x,y,z,那么ai^2就是(x,x)(x,y)(x,z)(y,x)(y,y)(y,z)(z,x)(z,y)(z,z),因此我 们可以将原来的一套装置看做两套.f[i][j][p][q]表示从第一套装置的上面取i个下面取j个.从第二套装置的上面取p个下面取q个,且两套装置 得到的两个序列相同的方案数,则答案就是f[n][m][

Codeforces 67C Sequence of Balls 编辑距离 dp

题目链接:点击打开链接 有一个交换操作比较特殊,所以记录每个点距离自己最近的那个字符的位置 然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉 把第二行要交换的2个字符 之间的字符都插入第一行的2个字符之间 然后再进行交换. #include <cstdio> #include <cstring> #include<iostream> using namespace std; #define inf 10000000 #define N 4005 #define

Codeforces Round #261 (Div. 2) E (DP)

E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are