博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 46 (Rated for Div. 2)
阅读量:5058 次
发布时间:2019-06-12

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

链接http://codeforces.com/contest/1000

B. Light It Up
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 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 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.

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.

B. Light It Up
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 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 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.

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,b],则插入的值为a+1或b-1,取决于插入的位置是开还是关。

#include
#include
#include
#include
#include
#include
#include
#define DEBUG(x) cout<<#x<<" = "<
<
=0 ;i-- ){ OnLitTimeToEnd[i]=OffLitTimeToEnd[i+1]+prog[i+1]-prog[i];///踩了一个坑 OnLitTimeToEnd[i]=OffLitTimeToEnd[i-1]+prog[i+1]-prog[i]; OffLitTimeToEnd[i]=OnLitTimeToEnd[i+1]; } int ans=litTime[n+1]; for(int i=1;i<=n+1 ;i++ ){ int t; if(i%2){ ///关 t=prog[i]-1-prog[i-1]+litTime[i-1]+OnLitTimeToEnd[i]; } else { ///开 t=prog[i]-(prog[i-1]+1)+litTime[i-1]+OffLitTimeToEnd[i]; } ans=max(ans,t); } printf("%d\n",ans);}
View Code

 

C. Covered Points Count
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given nn segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

Your task is the following: for every k[1..n]k∈[1..n], calculate the number of points with integer coordinates such that the number of segments that cover these points equals kk. A segment with endpoints lili and riri covers point xx if and only if lixrili≤x≤ri.

Input

The first line of the input contains one integer nn (1n21051≤n≤2⋅105) — the number of segments.

The next nn lines contain segments. The ii-th line contains a pair of integers li,rili,ri (0liri10180≤li≤ri≤1018) — the endpoints of the ii-th segment.

Output

Print nn space separated integers cnt1,cnt2,,cntncnt1,cnt2,…,cntn, where cnticnti is equal to the number of points such that the number of segments that cover these points equals to ii.

Examples
input
Copy
3 0 3 1 3 3 8
output
Copy
6 2 1
input
Copy
3 1 3 2 4 5 7
output
Copy
5 2 0
Note

The picture describing the first example:

Points with coordinates [0,4,5,6,7,8][0,4,5,6,7,8] are covered by one segment, points [1,2][1,2] are covered by two segments and point [3][3] is covered by three segments.

The picture describing the second example:

Points [1,4,5,6,7][1,4,5,6,7] are covered by one segment, points [2,3][2,3] are covered by two segments and there are no points covered by three segments.

按照读入一个区间,统计区间里的点覆盖数目的做法肯定超时。但我想了很久也没有想出一个可行的办法。在网上搜了一下,除了惊叹于大神的能力,对自己平时只顾ac,没有深入理解算法技巧的刷题套路感到羞愧。其实这题用到的思路在运用线段树求面积的题目中出现过,核心思想就是保存当前的覆盖点的数目。具体的做法将所有的点按照坐标的大小从小到大排序,然后处理每一个点。看来不光要做题,更重要的是要体会总结算法这么做的原因和意义,才能够举一反三

#include
#include
#include
#include
#include
#include
#include
#define DEBUG(x) cout<<#x<<" = "<
<
p.delta; }};int N;Point points[MAXN];ll ans[MAXN];int main(){// freopen("in.txt","r",stdin); scanf("%d",&N); for(int i=0;i<2*N ;i++ ){ scanf("%lld",&points[i].pos); if(i%2==0)points[i].delta=1; else { points[i].delta=-1; ///需要包括右端点 points[i].pos++; } } sort(points,points+2*N); int curCov=0; for(int i=0;i<2*N-1 ;i++ ){ curCov+=points[i].delta; ans[curCov]+=points[i+1].pos-points[i].pos; } for(int i=1;i<=N ;i++ ){ printf("%lld",ans[i]); if(i==N)printf("\n"); else printf(" "); }}
View Code

 

转载于:https://www.cnblogs.com/MalcolmMeng/p/9290360.html

你可能感兴趣的文章
利用堆实现堆排序&amp;优先队列
查看>>
Mono源码学习笔记:Console类(四)
查看>>
Android学习路线(十二)Activity生命周期——启动一个Activity
查看>>
《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》
查看>>
CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
查看>>
windows下编译FreeSwitch
查看>>
git .gitignore 文件不起作用
查看>>
Alan Turing的纪录片观后感
查看>>
c#自定义控件中的事件处理
查看>>
App.config自定义节点读取
查看>>
unity3d根据手机串号和二维码做正版验证
查看>>
二十六、Android WebView缓存
查看>>
django Models 常用的字段和参数
查看>>
linux -- 嵌入式linux下wifi无线网卡驱动
查看>>
SVN使用教程总结
查看>>
SQL中varchar和nvarchar有什么区别?
查看>>
OpenCV矩阵运算总结
查看>>
Java Build Practice 4:Extend and Invoke Ant API
查看>>
[转] Transformer图解
查看>>
FreeBSD方式安装 MAC OSX
查看>>