题目链接:http://codeforces.com/problemset/problem/455/A

题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可以获得分值x,求出能获得的最大分值为多少。

解题思路:从小到大排序,去重一下, 用cnt[i]记录一下数字i出现次数。那么可以得到状态转移方程:dp[i]=max(dp[i],dp[j]+cnt[i]*a[i])(j<i&&a[i]-a[j]>1),再用单调队列优化一下就行了O(∩_∩)O~。

代码:

 #include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define LC(a) (a<<1)
#define RC(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FOR(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=5e6+; LL dp[N],cnt[N],q[N]; int main(){
FAST_IO;
vector<int>v;
int n,lim=;
cin>>n;
for(int i=;i<=n;i++){
int x;
cin>>x;
v.push_back(x);
cnt[x]++;
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
LL ans=,head=,tail=,cur=;
q[tail++]=;
for(int i=;i<v.size();i++){
while(head<tail-&&q[head]<=q[head+]){
head++;
}
dp[i]=q[head]+cnt[v[i]]*v[i];
ans=max(dp[i],ans);
while(cur<=i&&v[cur]<v[i+]-){
LL t=dp[cur];
while(head<tail&&q[tail-]<=t){
tail--;
}
q[tail++]=t;
cur++;
}
}
cout<<ans<<endl;
return ;
}

Codeforces 445A Boredom(DP+单调队列优化)的更多相关文章

  1. Codeforces 940 区间DP单调队列优化

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  2. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  3. Codeforces 1077F2 Pictures with Kittens (hard version)(DP+单调队列优化)

    题目链接:Pictures with Kittens (hard version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:数据量5000, ...

  4. 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces

    题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...

  5. 1023: [SHOI2008]cactus仙人掌图(DP+单调队列优化)

    这道题吗= =首先解决了我多年以来对仙人掌图的疑问,原来这种高大上的东西原来是这个啊= = 然后,看到这种题,首先必须的就是缩点= = 缩点完之后呢,变成在树上找最长路了= =直接树形dp了 那么那些 ...

  6. P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)

    题目链接:传送门 题目: 题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows ( ...

  7. bzoj 1855 dp + 单调队列优化

    思路:很容易写出dp方程,很容易看出能用单调队列优化.. #include<bits/stdc++.h> #define LL long long #define fi first #de ...

  8. 股票交易(DP+单调队列优化)

    题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi, ...

  9. Luogu 2627 修建草坪 (动态规划Dp + 单调队列优化)

    题意: 已知一个序列 { a [ i ] } ,求取出从中若干不大于 KK 的区间,求这些区间和的最大值. 细节: 没有细节???感觉没有??? 分析: 听说有两种方法!!! 好吧实际上是等价的只是看 ...

随机推荐

  1. BZOJ2595:[Wc2008]游览计划——题解(插头dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2595 Description Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行 ...

  2. LibreOJ #539. 「LibreOJ NOIP Round #1」旅游路线(倍增+二分)

    哎一开始看错题了啊T T...最近状态一直不对...最近很多傻逼题都不会写了T T 考虑距离较大肯定不能塞进状态...钱数<=n^2能够承受, 油量再塞就不行了...显然可以预处理出点i到j走c ...

  3. 音视频处理之FFmpeg程序的介绍与使用20180302

    一.FFMPEG程序介绍与使用 主要介绍一下ffmpeg工程包含的三个exe的使用方法. 1. FFMPEG程序介绍 1.1.下载 ffmpeg的官方网站是:http://ffmpeg.org/ 下载 ...

  4. 弄清楚CSS的匹配原理让你写出高效的CSS

    用了这么多年的CSS,现在才明白CSS的真正匹配原理,不知道你是否也跟我一样?看1个简单的CSS: DIV#divBox p span.red{color:red;} 按习惯我们对这个CSS 的理解是 ...

  5. 使用uiautomator时遇到问题的处理方法

    本帖持续更新中… 一.使用adb devices无法连接到模拟器 这种情况可能是因为服务挂了之类的原因,重启一下服务 adb kill-server //关闭adb服务 adb start-serve ...

  6. python shell

    os.system(cmd) 命令执行结果 0或者1 output = os.popen(cmd) print output.read() 通过 os.popen() 返回的是 file read 的 ...

  7. SpringBoot(五) :spring data jpa 的使用

    原文出处: 纯洁的微笑 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法 ...

  8. Java常量池详解之Integer缓存

    一个Java question,求输出结果   public class IntegerTest { public static void main(String[] args) { objPoolT ...

  9. 简单实现VUE的双向数据绑定

    <!DOCTYPE html> <html> <head> <title>vue-双向数据绑定的简单实现</title> </head ...

  10. 爬取知乎话题async使用协程

    import requests import json import time from pyquery import PyQuery import pandas as pd from collect ...