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. 第四周总结&实验报告二

    第四周总结&实验报告二 课程总结 这周我们学习了string类,以及很多string类的很多操作方法,同时string也是一个对象,在用到它时我们首字母需要大写,这周我们还加深了对函数构造的理 ...

  2. 使用HeapAnalyzer分析内存泄漏

    从IBM网站下载ha433包,释放,执行ha433.jar文件 https://www.ibm.com/developerworks/mydeveloperworks/groups/service/h ...

  3. Jquery Ajax调用asmx出错问题

    1.//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释.      [System.Web.Script.Services.ScriptService] 这个 ...

  4. Openstack 实现技术分解 (1) 开发环境 — Devstack 部署案例详解

    目录 目录 前言 系统环境 Devstack 下载源码 配置文件 local.conf & localrc 简易的环境脚本 openrc 部署 Devstack 自动化部署流程 部署案例 单节 ...

  5. CentOS7_装机软件推荐

    目录 目录 前言 CentOS7中文支持 Install Yum Fastest Mirror Plugin Install Flash Player Install NTFS-3G Install ...

  6. 阶段3 1.Mybatis_11.Mybatis的缓存_3 mybatis一对一实现延迟加载

    不用骨架创建项目 复制一对多的代码src下的代码到我们刚才创建的项目里面 把依赖信息复制过来 这里原来实现的功能是立即加载的功能.sql语句是一次性查询的两个表关联的查询. 调整代码 删除Accoun ...

  7. 中国MOOC_零基础学Java语言_第4周 循环控制_2念整数

    2 念整数(5分) 题目内容: 你的程序要读入一个整数,范围是[-100000,100000].然后,用汉语拼音将这个整数的每一位输出出来. 如输入1234,则输出: yi er san si 注意, ...

  8. C 语言的函数 - 内存分析

    函数基本概念 Linux 中,函数在内存的代码段(code 区),地址比较靠前. 函数定义 C 语言中,函数有三个要素:入参.返回值.函数名,缺一不可.函数使用前必须先声明,或者在使用之前定义. 函数 ...

  9. Centos6.5安装配置svn服务器

    一. yum安装svn服务器 yum -y install subversion 二. 检测安装结果 svnserve --version //显示安装结果,表示安装成功了 三. 创建代码仓库目录 m ...

  10. House_Of_Spirit ctf oreo程序分析

    oreo程序下载 提取码:t4xx 程序分析 int __cdecl main() { leave_add = 0; leave_del = 0; leave_buf = (char *)&u ...