B. Light It Up 思维题
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 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.
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.
这题主要运用了一个思想 就是加入一个点就相当于把一个点拆成了两个 ,
根据题目的要求贪心 ,将a[i] 拆成两个点的时候 就是 拆成a[i]-1 和 a[i];这个我们就可以获得最优解 ,
ans[i]记录的题目要求值得前缀和
因为改变了a[i] 所以就需要a[i]后的关灯的值
m - a[i] - (ans[n + 1] - ans[i]) 这个为a[i]后关灯的值
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + ;
typedef long long LL;
int a[maxn], n, m, ans[maxn];
int main() {
cin >> n >> m;
a[] = , a[n + ] = m;
for (int i = ; i <= n ; i++) scanf("%d", &a[i]);
int flag = ;
ans[] = ;
for (int i = ; i <= n + ; i++) {
ans[i] = ans[i - ] + flag * (a[i] - a[i - ]);
flag ^= ;
}
int cnt = ans[n + ];
for (int i = ; i <= n + ; i++) {
cnt = max(cnt, ans[i] - + m - a[i] - (ans[n + ] - ans[i]));
}
printf("%d\n", cnt);
return ;
}
B. Light It Up 思维题的更多相关文章
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- cf796c 树形,思维题
一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...
随机推荐
- ansible自动化运维入门
1.ansible的安装 1)使用源码安装Python3.5 安装支持包 yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses- ...
- Vijos 纸牌
题目网址 https://vijos.org/d/Randle/p/5a0011e1d3d8a10a532d6d71 题目描述 在桌面上放着n张纸牌,每张纸牌有两面,每面都写着一个非负整数.你的邪王真 ...
- P3305 [SDOI2013]费用流
题目描述 Alice和Bob在图论课程上学习了最大流和最小费用最大流的相关知识. 最大流问题:给定一张有向图表示运输网络,一个源点S和一个汇点T,每条边都有最大流量. 一个合法的网络流方案必须满足: ...
- 什么鬼,又不知道怎么命名class了
什么鬼,又不知道怎么命名class了 2015/10/25 · CSS · class 分享到:5 原文出处: 结一(@结一w3cplus) 相信写css的人都会遇到下面的问题: 糟糕,怎么命名 ...
- oracle 游标例子
CREATE OR REPLACE PROCEDURE PRC_WAP_ACTIVEUSERS(RETCODE OUT VARCHAR2) /***************************** ...
- 给apk签名
一.签名 把apk和签名文件放在jdk bin目录下,然后在jkd bin目录下执行以下代码: jarsigner -verbose -keystore xxx.keystore -signedjar ...
- Kotlin 1 函数
#2 函数 函数声明和平时我见到的有点不太一样,使用关键字fun来声明.(感觉好欢乐的样子···O(∩_∩)O~~) 下面的示例,简单的声明了一个函数: // 这是函数声明 fun this_is_a ...
- 『JavaScript』封装
封装可以被定义为对对象的内部数据表现形式和实现细节进行隐藏.通过封装可以强制实施信息隐藏. 在JavaScript中,并没有显示的声明私有成员的关键字等.所以要想实现封装/信息隐藏就需要从另外的思路出 ...
- Git 相关工具及教程地址
一.Git GUI 客户端 Git 客户端下载(Windows) TortoiseGit 客户端下载(Windows) Sourcetree 客户端下载(Windows.Mac) Git Extens ...
- Ubuntu下使用Git_6
这回真的是最后一篇了,哈哈,改写提交. 这里这部分在目前的学习阶段还没有用到,所以,这里将不在有实验的部分,在下面的链接中有详细的介绍 这也是我第一讲一个网站的内容完成的学习完成,这这部分,我讲简单的 ...