Round #232 (Div. 2)_A

A. On Segment's Own Points
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Our old friend Alexey has finally entered the University of City N — the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm.

The dorm has exactly one straight dryer — a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has nstudents. Dean's office allows i-th student to use the segment (li, ri) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students!

Alexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (n - 1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1.

Input

The first line contains a positive integer n (1 ≤ n ≤ 100). The (i + 1)-th line contains integers li and ri (0 ≤ li < ri ≤ 100) — the endpoints of the corresponding segment for the i-th student.

Output

On a single line print a single number k, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments.

Sample test(s)
input
3
0 5
2 8
1 6
output
1
input
3
0 10
1 5
7 15
output
3
Note

Note that it's not important are clothes drying on the touching segments (e.g. (0, 1) and (1, 2)) considered to be touching or not because you need to find the length of segments.

In the first test sample Alexey may use the only segment (0, 1). In such case his clothes will not touch clothes on the segments (1, 6)and (2, 8). The length of segment (0, 1) is 1.

In the second test sample Alexey may dry his clothes on segments (0, 1) and (5, 7). Overall length of these segments is 3.

这道题的大概意思是给定N个区间,第一个为指定的空间,其余N-1个可能会覆盖第一个区间的部分,然后让你求剩余区间的大小。

开始时我想的是用一个整型数组A[101]初始化为0,被覆盖的部分区间的整数全部赋值为1,然后统计非零的个数来算出剩余空间的大小,却发现3 [0 4][0 2] [3 4]测试无法解释,后来发现将其改为开区间(左闭右开)就可以解决问题。

这样的话[0 4)就可以区间内,a[0],a[1] ,a[3]值为1,剩余空间为一,问题得解。

这题给我们一个启示,可以通过开闭区间的方式将一个点抽象为一个线段区间,比如说[0, 4),0可以代表[0, 1)这个区间,1可以代表[1, 2)这个区间,这样的话就可以“以点代线”,合理的解决问题而无需分类考虑不同的情况。

以下是 AC代码:

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

int main(){
    int n,a,b,i,j;
    bool s[110]={false};
    cin>>n;
    cin>>a>>b;
    for(i=1; i<n; i++){
        int c,d;
        cin>>c>>d;
        for(j=c; j<d; j++){
            s[j]=true;
        }
    }
    int sum=0;
    for(i=a; i<b; i++){
        if(!s[i])   sum++;
    }
    cout<<sum<<endl;
    return 0;
}



为了深入掌握如何使用pyecharts和wordcloud库生成图进行数据分析可视化,推荐您参考《历年考研分数线的数据分析与可视化教程》。这份资源不仅包含了实际的数据集和源码,而且详细指导了如何实现可视化过程,非常适合想要了解并掌握从数据分析到可视化输出的整个流程的学习者。 参考资源链接:[历年考研分数线的数据分析与可视化教程](https://wenku.youkuaiyun.com/doc/43pq9qt1cc) 创建图通常涉及到几个关键步骤,以下是基于Python进行图创建的详细步骤和代码示例: 1. 准备工作:确保已经安装了必要的Python库,包括pyecharts和wordcloud。可以使用pip命令安装:`pip install pyecharts wordcloud`。 2. 加载数据:首先,你需要有一个包含需要可视化的关键的数据集。假设数据以字符串列表的形式给出。 3. 数据处理:为了生成,通常需要对数据进行一些预处理,比如去除停用、标点符号、空格等。 4. 创建对象:使用wordcloud库创建一个对象,并设置合适的参数,如背景颜色、最大数、字体大小范围、停用列表等。 5. 绘制图:调用对象的generate方法,传入处理好的文本数据,然后使用to_image方法将对象转换为图片。 6. 可视化展示:如果需要,可以使用pyecharts库进一步将生成的图进行展示。pyecharts提供了丰富的图表类型和配置项,可以根据需要自定义图表的样式和展示效果。 下面是一个简单的代码示例: ```python from wordcloud import WordCloud from PIL import Image import matplotlib.pyplot as plt from pyecharts.charts import Image from io import BytesIO # 假设这是我们的关键数据集 keywords = ['Python', '数据分析', '可视化', '教育', '项目', '图', '练习'] # 数据预处理(可选) # ...(此处省略了数据预处理代码) # 创建对象 wordcloud = WordCloud( background_color='white', # 背景颜色 max_words=200, # 最大显示的数 mask=None, # 不使用mask font_path='path/to/your/font.ttf' # 指定字体路径 ).generate(' '.join(keywords)) # 将图转换为图片 image = Image() image.add_image(plt.imshow(wordcloud, interpolation='bilinear')) # 将图片转换为可展示的格式 buffer = BytesIO() wordcloud.to_image().save(buffer, format='PNG') buffer.seek(0) # 使用pyecharts展示图 image.render_embed() # 显示图 plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') # 不显示坐标轴 plt.show() ``` 上述代码展示了如何使用wordcloud库生成图,并使用matplotlib和pyecharts库进行展示。通过这样的步骤,你可以将任何数据集中的关键图的形式直观地展现出来。 在掌握了图的基本制作方法后,你可以尝试更多的扩展功能,如自定义形状、添加动态效果等。此外,也可以探索pyecharts的其他图表类型,以满足不同的可视化需求。为了进一步深化对Python数据分析和可视化的理解,建议继续学习《历年考研分数线的数据分析与可视化教程》,该教程会为你提供更多的实践素材和深入的理论知识。 参考资源链接:[历年考研分数线的数据分析与可视化教程](https://wenku.youkuaiyun.com/doc/43pq9qt1cc)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值