Codeforces 940 区间DP单调队列优化
A
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int num[];
int main()
{
int n, d;
cin >> n >> d;
int anser = ;
for (int i = ; i <= n; i++)
{
cin >> num[i];
}
if (n == )
{
cout << << endl;
return ;
}
sort(num + , num + + n);
int now = num[n] - num[];
int l = ;
int r = n;
for (int i = n - ; i >= ; i--)
{
for (int j = ; j <= n - i; j++)
{
if (num[j + i] - num[j] <= d)
{
cout << n - i - << endl;
return ;
}
}
}
cout<<n-<<endl;
return ;
}
B
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int num[];
int main()
{
ll n, k, a, b;
cin >> n >> k >> a >> b;
ll now = n;
ll cost = ;
ll remain, aim;
if (k == )
{
cost += 1LL * a * (now - );
cout << cost << endl;
return ;
}
while (now != )
{
if (now < k)
{
cost += 1LL * a * (now - );
break;
}
remain = now % k;
cost += remain * a;
now -= remain;
aim = now / k;
cost += min(1LL * a * (now - aim), 1LL * b);
now = aim;
}
cout << cost << endl;
return ;
}
C
找字典序最小的比给定字符串S大的字符串T
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int num[];
int nextt[];
int main()
{
int n, k;
string a;
cin >> n >> k >> a;
for (int i = ; i < n; i++)
{
num[a[i] - 'a'] = ;
}
for (int i = ; i <= ; i++)
{
for (int j = i + ; j <= ; j++)
{
if (num[j])
{
nextt[i] = j;
break;
}
}
}
if (k > n)
{
cout << a;
int aim;
for (int i = ; i <= ; i++)
{
if (num[i])
{
aim = i;
break;
}
}
char ch = 'a' + aim;
for (int i = ; i <= k - n; i++)
{
cout << ch;
}
cout << endl;
}
else
{
int aim;
int cur;
for (int i = k - ; i >= ; i--)
{
if (nextt[a[i] - 'a'] != )
{
aim = nextt[a[i] - 'a'];
cur = i;
break;
}
}
for (int i = ; i < cur; i++)
{
cout << a[i];
}
char ch = aim + 'a';
cout << ch;
for (int i = ; i <= ; i++)
{
if (num[i])
{
aim = i;
break;
}
}
ch = aim + 'a';
for (int i = ; i <= k - cur - ; i++)
{
cout << ch;
}
cout << endl;
}
return ;
}
D
在01边界的时候更新答案范围即可
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int num[];
int minnum[];
int maxnum[];
int main()
{
int n;
cin >> n;
for (int i = ; i <= n; i++)
{
scanf("%d", &num[i]);
}
string a;
cin >> a;
for (int i = ; i <= n; i++)
{
int maxn = -;
for (int j = ; j <= ; j++)
{
maxn = max(maxn, num[i - j]);
}
maxnum[i] = maxn;
}
for (int i = ; i <= n; i++)
{
int minn = ;
for (int j = ; j <= ; j++)
{
minn = min(minn, num[i - j]);
}
minnum[i] = minn;
}
int l = -;
int r = ;
for (int i = ; i <= n - ; i++)
{
if (a[i] == '' && a[i - ] == '')
{
l = max(l, maxnum[i + ] + );
}
if (a[i] == '' && a[i - ] == '')
{
r = min(r, minnum[i + ] - );
}
}
cout << l << " " << r << endl;
return ;
}
E
单调队列优化DP
可以证明全局最小值<=局部最小值 所以全部块要不是一块一块的 要不就是C块里面的
因为每一个块有两种选择 所以DP方程为dp[i]=min(dp[i-1]+num[i],dp[i-c]+pre[i]-pre[i-c]-min(num[j]) ) [i-c+1<=j<=i]
dp[i]为到第i位最少需要的代价
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-10;
const double EPS = 1.0e-4;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
const int MAXN = 1e5 + ;
ll num[MAXN];
int q[MAXN];
int head, tail;
ll dp[MAXN];
ll pre[MAXN];
list<int> que;
int main()
{
int n, c;
cin >> n >> c;
for (int i = ; i <= n; i++)
{
scanf("%lld", num + i);
pre[i] = pre[i - ] + num[i];
}
for (int i = ; i <= n; i++)
{
dp[i] = dp[i - ] + num[i];
while (head >= tail && i - c >= q[tail])
{
tail++;
}
while (head >= tail && num[q[head]] > num[i])
{
head--;
}
q[++head] = i;
if (i >= c)
{
dp[i] = min(dp[i], dp[i - c] + pre[i] - pre[i - c] - num[q[tail]]);
}
}
cout << dp[n] << endl;
}
Codeforces 940 区间DP单调队列优化的更多相关文章
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- Codeforces 1077F2 Pictures with Kittens (hard version)(DP+单调队列优化)
题目链接:Pictures with Kittens (hard version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:数据量5000, ...
- Codeforces 445A Boredom(DP+单调队列优化)
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...
- 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces
题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...
- Luogu 2627 修建草坪 (动态规划Dp + 单调队列优化)
题意: 已知一个序列 { a [ i ] } ,求取出从中若干不大于 KK 的区间,求这些区间和的最大值. 细节: 没有细节???感觉没有??? 分析: 听说有两种方法!!! 好吧实际上是等价的只是看 ...
- 1023: [SHOI2008]cactus仙人掌图(DP+单调队列优化)
这道题吗= =首先解决了我多年以来对仙人掌图的疑问,原来这种高大上的东西原来是这个啊= = 然后,看到这种题,首先必须的就是缩点= = 缩点完之后呢,变成在树上找最长路了= =直接树形dp了 那么那些 ...
- P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)
题目链接:传送门 题目: 题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows ( ...
- bzoj 1855 dp + 单调队列优化
思路:很容易写出dp方程,很容易看出能用单调队列优化.. #include<bits/stdc++.h> #define LL long long #define fi first #de ...
- 股票交易(DP+单调队列优化)
题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi, ...
随机推荐
- vue中下载excel的使用,后端链接两种情况,一个是链接,一个是文件流
vue中下载excel使用 一.这是第一种情况,后台链接地址返回的是一个url,这个时候我只要在导出按钮上绑定exportData()这个事件方法就好了 exportData() { this ...
- 后盾网lavarel视频项目---laravel 使用laracasts/flash插件提示信息
后盾网lavarel视频项目---laravel 使用laracasts/flash插件提示信息 一.总结 一句话总结: laracasts/flash插件的效果就是一个弹出的boostrap模块框, ...
- Linux编译部署vsftp服务
部署vsftp服务 Hostname OS IP Software Vsftpsvr RHEL-5.8 10.0.0.58 yum install -y vsftpd yum install –y ...
- 使用ffmpeg来转换media Video
FFMPEG -i 1.wmv -c:v libx264 -strict -2 1_wmv.mp4 ffmpeg -i b.mp4 -codec copy -bsf h264_mp4toannexb ...
- Linux_Rsync远程同步备份服务器
目录 目录 Remote Sync 同步的类型 本地模式 远程模式 RSync列表模式 RSync 服务模式 Setup RSync service How to use the rsync comm ...
- Apache web服务器(LAMP架构)
1.apache介绍 1).世界上使用率最高的网站服务器,最高时可达70%:官方网站:apache.org 2).http 超文本协议 HTML 超文本标记语言 3).URL 统一资源定位符 http ...
- python中的装饰器练习
一:编写装饰器,为多个函数加上认证的功能(用户的账号密码) 要求登录成功一次,后续的函数都无需输入用户名和密码FLAG=False#此时还未登录 全局变量 写这个步骤的意义在于:方便 知道已经登录成功 ...
- Python内置函数compile
英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...
- Java8---函数式编程-示例
// Java8函数式编程示例—(Predicate.Stream.Optional) https://blog.csdn.net/weixin_41950473/article/details/84 ...
- MySQL-快速入门(14)MySQL性能优化
1.MySQL性能优化包括查询速度优化.数据库结构优化.数据库服务器优化等. 优化的切入点:合理的结构设计.表结构.索引.查询语句. 2.show status查询数据库的性能参数 show stat ...