Light It Up CF1000B 思维
1 second
256 megabytes
standard input
standard output
Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 00 and turn power off at moment MM. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.
The lamp allows only good programs. Good program can be represented as a non-empty array aa, where 0<a1<a2<⋯<a|a|<M0<a1<a2<⋯<a|a|<M. All aiai must be integers. Of course, preinstalled program is a good program.
The lamp follows program aa in next manner: at moment 00 turns power and light on. Then at moment aiai the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment 11 and then do nothing, the total time when the lamp is lit will be 11. Finally, at moment MM the lamp is turning its power off regardless of its state.
Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program aa, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of aa, or even at the begining or at the end of aa.
Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from xx till moment yy, then its lit for y−xy−x units of time. Segments of time when the lamp is lit are summed up.
First line contains two space separated integers nn and MM (1≤n≤1051≤n≤105, 2≤M≤1092≤M≤109) — the length of program aa and the moment when power turns off.
Second line contains nn space separated integers a1,a2,…,ana1,a2,…,an (0<a1<a2<⋯<an<M0<a1<a2<⋯<an<M) — initially installed program aa.
Print the only integer — maximum possible total time when the lamp is lit.
3 10
4 6 7
8
2 12
1 10
9
2 7
3 4
6
In the first example, one of possible optimal solutions is to insert value x=3x=3 before a1a1, so program will be [3,4,6,7][3,4,6,7] and time of lamp being lit equals (3−0)+(6−4)+(10−7)=8(3−0)+(6−4)+(10−7)=8. Other possible solution is to insert x=5x=5 in appropriate place.
In the second example, there is only one optimal solution: to insert x=2x=2 between a1a1 and a2a2. Program will become [1,2,10][1,2,10], and answer will be (1−0)+(10−2)=9(1−0)+(10−2)=9.
In the third example, optimal answer is to leave program untouched, so answer will be (3−0)+(7−4)=6(3−0)+(7−4)=6.
题意:在0-m间插入数字使得相邻两数相差值的和最大
分析:要使插入的数能够让差值和最大则插入的位置肯定是每个数减一的位置,接下来在插入的时候求个前缀和,后缀和就行了
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn], n, m;
int main(){
std::ios::sync_with_stdio(false);
while( cin >> n >> m ) {
ll ans = , on = , off = ;
for( ll i = ; i <= n; i ++ ) {
cin >> a[i];
}
a[] = , a[n+] = m;
for( ll i = n; i >= ; i -- ) {
if( i& ) {
off += a[i+] - a[i] - ;
} else {
on += a[i+] - a[i] - ;
}
if( a[i+] != a[i] + ) {
ans = max( ans, off - on );
}
if( i& ) {
off ++;
} else {
on ++;
}
}
cout << ans + on << endl;
}
return ;
}
Light It Up CF1000B 思维的更多相关文章
- 思维+模拟--POJ 1013 Counterfeit Dollar
Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver d ...
- Codeforces | CF1000B 【Light It Up】
蒟蒻第二篇题解... 比赛的时候写这道题MLE了qwq..根据CF的赛制我也没敢再交第二次.. 简单讲一下思路好了(假装是dalao)..根据题意要加一个或者不加新的点..如果加一个新的点意味着从这个 ...
- B. Light It Up 思维题
Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedu ...
- CodeForces 1000B Light It Up(贪心、思维)
https://codeforces.com/problemset/problem/1000/B 题意: 一个模拟思维题.就是有一盏灯,0时刻开着.n次操作,你可以在其中加入一次操作(或者不加),操作 ...
- Gym 101775C - Traffic Light - [思维题]
题目链接:http://codeforces.com/gym/101775/problem/C 题意: 给出 $N$ 个红绿灯,又给出 $N+1$ 个距离 $S_i = S_0,S_1, \cdots ...
- hdu 3698 Let the light guide us(线段树优化&简单DP)
Let the light guide us Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/O ...
- (纪录片)光的故事 BBC Light Fantastic (2004)
简介: 导演: Jeremy Turner主演: Simon Schaffer / Dimitri Andreas ... Al Hazen / Edmund Dehn ... Priest/Old ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- Photoshop、Illustrator思维导图笔记
半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.
随机推荐
- kubernetes集群升级的正确姿势
kubernetes社区非常活跃,每季度都会发布一个release.但是线上集群业务可用性要求较高,场景复杂,任何微小的变更都需要非常小心,此时跟随社区版本进行升级略显吃力.但是为了能够使用到最新的一 ...
- jQuery插件之路(一)——试着给jQuery的一个Carousel插件添加新的功能
前几日在网上看到了一个关于Carousel插件的教学视频,于是也顺便跟着学习着做了一下.但是在做完之后发现,在别的网站上面看到类似的效果要比现在做的这个要多一个功能,也就是在底下会有一些按钮,当鼠标放 ...
- BFS DFS模板
转载于https://blog.csdn.net/alalalalalqp/article/details/9155419 BFS模板: #include<cstdio> #include ...
- Spring入门编程问题集锦Top10
我写的一篇文章,希望对spring初学者有所帮助: 1.如何学习Spring? 你可以通过下列途径学习spring: ①. spring下载包中doc目录下的MVC-step-by-step和samp ...
- css公共样式 | 标签元素初始化
PC参考样式1: @charset "utf-8"; html{background:#fff;overflow:auto;} body{min-width:1200px;font ...
- Spring Boot + Security + JWT 实现Token验证+多Provider——登录系统
首先呢就是需求: 1.账号.密码进行第一次登录,获得token,之后的每次请求都在请求头里加上这个token就不用带账号.密码或是session了. 2.用户有两种类型,具体表现在数据库中存用户信息时 ...
- RE最全面的正则表达式----字符验证
二.校验字符的表达式汉字:^[一-彪]{0,}$英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$长度为3-20的所有字符:^.{3,20}$由26个英文字母组成的字 ...
- 同“窗”的较量:部署在 Windows 上的 .NET Core 版博客站点发布上线
为了验证 docker swarm 在高并发下的性能问题,周一我们发布了使用 docker-compose 部署的 .net core 版博客站点(博文链接),但由于有1行代码请求后端 web api ...
- linux100day(day3)--常用文本处理命令和vim文本编辑器
今天,来介绍几个常用文本处理命令和vim文本编辑器 day3--常用文本处理命令和vim文本编辑器 col,用于过滤控制字符,-b过滤掉所有控制字符,这个命令并不常用,但可以使用man 命令名| co ...
- intellij idea与github整合管理代码
各位看官大家好,博主每每在公司学习新知识写代码时都需要通过U盘带回家来继续每天的学习,觉得这样实在麻烦,于是今天就整合了一下github来完成代码的管理. 开始之前我们需要准备三样东西:1.intel ...