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. mysql数据库表反向生成modes类

    一,如果你是windows,打开cmd,切换到desktop目录下 二,需要连接你的数据库,并且执行命令:sqlacodegen  --outfile models.py mysql+pymysql: ...

  2. IoC有什么好处

    IoC(Inversion of Control):控制反转. DI(Dependency Injection):依赖注入. 控制反转是目的,依赖注入是实现控制反转的手段. 控制反转是一种面向对象的思 ...

  3. ICML 2019 分析

    ICML 2019 分析 Word Embeddings Understanding the Origins of Bias in Word Embeddings Popular word embed ...

  4. WPF C# 创建缩略图

    不太精确的方法: public bool ThumbnailCallback() { return false; } private void CreateThumb(int toWidth) { S ...

  5. JavaScript-W3School-Browser 对象:Window open() 方法

    ylbtech-JavaScript-Runoob-Browser 对象:Window open() 方法 1.返回顶部 1. Window open() 方法  Window 对象 定义和用法 op ...

  6. JS获取select被选中的option的值

    一:JavaScript原生的方法 1:拿到select对象: var myselect=document.getElementById(“test”); 2:拿到选中项的索引:var index=m ...

  7. 性能测试工具之WebBench

    一.简介 WebBench是一款在Linux下使用非常简单的压力测试工具.它的原理是:WebBench首先fork出多个子进程,每个子进程都循环做web访问测试.子进程把访问的结果通过pipe告诉父进 ...

  8. Jackson常用注解及用法

    最近写项目,用到Jackson的一些注解,总结一下,帮助自己记忆. 1.@JsonIgnore 和 @JsonIgnoreProperties 两个注解可以对照比较后选择使用: @JsonIgnore ...

  9. 应用安全 - 无文件式攻击 - 工具型攻击 - PowerShell - 汇总

    PowerShell 使用 | 命令 win+r ->powershell #启动Powershell窗口 get-host #查看版本 Get-Host | Select-Object Ver ...

  10. Java——ArrayList源码解析

    以下针对JDK 1.8版本中的ArrayList进行分析. 概述     ArrayList基于List接口实现的大小可变的数组.其实现了所有可选的List操作,并且元素允许为任意类型,包括null元 ...