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单调队列优化的更多相关文章

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

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

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

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

  3. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. java学习之- 创建线程run和start特点

    标签(空格分隔): run,start 为什么做run方法的覆盖? 1.Thread类用于描述线程,该类就定义一个功能用于存储线程要运行的代码,该存储功能就是run方法: 也就是说Thread种的ru ...

  2. HyperV - glossary

    Root Partition - sometimes called partition. Manages machine-level functions such as device drivers, ...

  3. [论文理解] LFFD: A Light and Fast Face Detector for Edge Devices

    LFFD: A Light and Fast Face Detector for Edge Devices 摘要 从微信推文中得知此人脸识别算法可以在跑2K图片90fps,仔细一看是在RTX2070下 ...

  4. leetcode-mid-math-371. Sum of Two Integers-NO-???

    mycode: 没思路啊...二级制四则运算不熟悉... 参考: 既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法: 1. 用二进制表示两个加数,a=5=0101 ...

  5. MVC、MVP 和 MVVM

    MVC Model–View–Controller 模型:管理应用程序的数据.逻辑和规则 视图:展示数据(可以直接从模型中获取数据) 控制器:接收输入并将其转化成模型和视图的命令 MVP Model– ...

  6. Windows环境下Mysql 5.7读写分离简单记录

    一.目的 本文记录了在Windows环境中,mysql数据库读写分离配置过程. 二.准备: Master机器:Windows 10 虚拟机,IP:192.168.3.32 Slave机器:Window ...

  7. MYSQL5.7源码包编译安装

    Centos下用cmake编译安装MySQL 5.7安装依赖包yum -y install gcc gcc-c++ ncurses ncurses-devel cmake下载相应源码包cd /usr/ ...

  8. Spring 注解概览

    从Java5.0开始,Java开始支持注解.Spring做为Java生态中的领军框架,从2.5版本后也开始支持注解.相比起之前使用xml来配置Spring框架,使用注解提供了更多的控制Spring框架 ...

  9. 【Qt开发】Qt Creator在Windows上的调试器安装与配置

    Qt Creator在Windows上的调试器安装与配置 如果安装Qt时使用的是Visual Studio的预编译版,那么很有可能就会缺少调试器(Debugger),而使用MSVC的Qt对应的原生调试 ...

  10. 【Linux开发】Linux V4L2驱动架构解析与开发导引

    Linux V4L2驱动架构解析与开发导引 Andrew按:众所周知,linux中可以采用灵活的多层次的驱动架构来对接口进行统一与抽象,最低层次的驱动总是直接面向硬件的,而最高层次的驱动在linux中 ...