Round #232 (Div. 2)_A
来源:互联网 发布:windows phone官方 编辑:程序博客网 时间:2024/05/22 05:06
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.
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.
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.
30 52 81 6
1
30 101 57 15
3
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;}
- Round #232 (Div. 2)_A
- Codeforces Round #305 (Div. 2)_A
- Codeforces Round #306 (Div. 2)_A
- Codeforces Round #328 (Div. 2)_A. PawnChess
- Codeforces Round #331 (Div. 2) _A. Wilbur and Swimming Pool
- Codeforces Round #254_A
- CF_Educational Codeforces Round 28_A
- Codeforces Round #232 (Div. 2)
- Codeforces Round #232 (Div. 2)
- Round #232 (Div. 2)_B
- Codeforces Round #232 (Div. 2)
- Codeforces Round #232 (Div. 2)渣渣也写。
- Codeforces Round #232 (Div. 2) A
- 【索引】CodeForces Round #232 (Div. 2)
- Codeforces Round #232 (Div. 2) B
- Round #146(Div 2)
- Round #145(Div 2)
- Round #338 (Div. 2)
- html中<a>标签的使用
- 第一周上机实践项目——胖子伤不起
- chrome调试js
- 【stm32+uC/OS-II】ucosii移植简单详细步骤
- java ip to int
- Round #232 (Div. 2)_A
- 判断是否是通过手机访问
- spring3.1缓存详解
- PHP代码优化 成员变量获取速度对比
- 6.交换排序——冒泡排序
- 工厂方法模式
- C++编译器与链接器工作原理
- jquery获取不到图片的高度 需要在window.onload=function后才能获取到
- 人际交往十大黄金法则