博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
B. Light It Up 思维题
阅读量:4548 次
发布时间:2019-06-08

本文共 3661 字,大约阅读时间需要 12 分钟。

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 yxy−x units of time. Segments of time when the lamp is lit are summed up.

Input

First line contains two space separated integers nn and MM (1n1051≤n≤105, 2M1092≤M≤109) — the length of program aaand 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.

Output

Print the only integer — maximum possible total time when the lamp is lit.

Examples
input
Copy
3 10 4 6 7
output
Copy
8
input
Copy
2 12 1 10
output
Copy
9
input
Copy
2 7 3 4
output
Copy
6
Note

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 (30)+(64)+(107)=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 (10)+(102)=9(1−0)+(10−2)=9.

In the third example, optimal answer is to leave program untouched, so answer will be (30)+(74)=6(3−0)+(7−4)=6.

 

 

 

这题主要运用了一个思想 就是加入一个点就相当于把一个点拆成了两个 ,

根据题目的要求贪心 ,将a[i] 拆成两个点的时候 就是 拆成a[i]-1 和 a[i];这个我们就可以获得最优解 ,

ans[i]记录的题目要求值得前缀和

因为改变了a[i] 所以就需要a[i]后的关灯的值 

  m - a[i] - (ans[n + 1] - ans[i]) 这个为a[i]后关灯的值

 

1 #include 
2 using namespace std; 3 const int maxn = 4e5 + 10; 4 typedef long long LL; 5 int a[maxn], n, m, ans[maxn]; 6 int main() { 7 cin >> n >> m; 8 a[0] = 0, a[n + 1] = m; 9 for (int i = 1 ; i <= n ; i++) scanf("%d", &a[i]);10 int flag = 1;11 ans[0] = 0;12 for (int i = 1 ; i <= n + 1 ; i++) {13 ans[i] = ans[i - 1] + flag * (a[i] - a[i - 1]);14 flag ^= 1;15 }16 int cnt = ans[n + 1];17 for (int i = 1 ; i <= n + 1 ; i++) {18 cnt = max(cnt, ans[i] - 1 + m - a[i] - (ans[n + 1] - ans[i]));19 }20 printf("%d\n", cnt);21 return 0;22 }

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9271350.html

你可能感兴趣的文章
【CF316G3】Good Substrings 后缀自动机
查看>>
【BZOJ2938】[Poi2000]病毒 AC自动机+DFS
查看>>
【BZOJ4750】密码安全 单调栈
查看>>
Java之atomic包的原理及分析
查看>>
Chrome自定义滚动条
查看>>
poj3311(状态压缩dp)
查看>>
《大数据日知录》读书笔记-ch2数据复制与一致性
查看>>
个人冲刺01
查看>>
Ubuntu16.04源的问题
查看>>
mysql基础5(mysql命令集----表操作)
查看>>
DevExpress:下拉框绑定数据源 (ComboBoxEdit,LookUpEdit)
查看>>
视觉里程计06 Qt界面显示摄像头
查看>>
基于unity3d IFC的虚拟仿真系统
查看>>
linux 2.6 驱动笔记(一)
查看>>
SpringMVC与MyBatis整合方法
查看>>
获取当前系统运行目录
查看>>
多个tomcat实例运行的配置
查看>>
一种基于 Numpy 的 TF-IDF 实现报告
查看>>
wpf窗口阴影
查看>>
linux内核分析第四周-使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用...
查看>>