Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 数学、A题第三次被系统hack了 (┬_┬)、集合交集
2016-09-16 02:31
369 查看
A. Meeting of Old Friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya!
Sonya is an owl and she sleeps during the day and stay awake from minute l1 to
minute r1 inclusive.
Also, during the minute k she prinks and is unavailable for Filya.
Filya works a lot and he plans to visit Sonya from minute l2 to
minute r2 inclusive.
Calculate the number of minutes they will be able to spend together.
Input
The only line of the input contains integers l1, r1, l2, r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018, l1 ≤ r1, l2 ≤ r2),
providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks.
Output
Print one integer — the number of minutes Sonya and Filya will be able to spend together.
Examples
input
output
input
output
Note
In the first sample, they will be together during minutes 9 and 10.
In the second sample, they will be together from minute 50 to minute 74 and
from minute 76 to minute 100.
Source
Codeforces Round #371 (Div. 2)
My Solution
数学、A题第三次被系统hack了 (┬_┬)
虽然比赛期间 趁机 hack 了一份C++的A题代码 和 两份 python的A题代码, 他们都是简单错误, 都是 l > r 出现负数了
笔者自己是分类讨论的, 所以不会出现 l > r 从而出现负数的情况, 但分类讨论中 有个地方 应该是 if(d < a) ans = 0; 写成了 if(d <= a), (┬_┬) 小于写成了小于等于
以后一定要仔细考虑 == 什么时候取到啊, 这里一个小忽视, 直接 从 371 名 稳涨分的名次调到了 846名 ⊙﹏⊙‖∣ 可惜了
分类讨论的情况比较多, 这样的小地方确实不容易检查的到, 没办法, 此外这个 A 题被我复杂化了, 不用这样麻烦分类讨论的,
这个就是 2个集合(区间)求交集, 然后 判断 k 是否在那个交集里。
1、分类讨论版本
2、求交集版本,推荐版本
Thank you!
------from ProLights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya!
Sonya is an owl and she sleeps during the day and stay awake from minute l1 to
minute r1 inclusive.
Also, during the minute k she prinks and is unavailable for Filya.
Filya works a lot and he plans to visit Sonya from minute l2 to
minute r2 inclusive.
Calculate the number of minutes they will be able to spend together.
Input
The only line of the input contains integers l1, r1, l2, r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018, l1 ≤ r1, l2 ≤ r2),
providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks.
Output
Print one integer — the number of minutes Sonya and Filya will be able to spend together.
Examples
input
1 10 9 20 1
output
2
input
1 100 50 200 75
output
50
Note
In the first sample, they will be together during minutes 9 and 10.
In the second sample, they will be together from minute 50 to minute 74 and
from minute 76 to minute 100.
Source
Codeforces Round #371 (Div. 2)
My Solution
数学、A题第三次被系统hack了 (┬_┬)
虽然比赛期间 趁机 hack 了一份C++的A题代码 和 两份 python的A题代码, 他们都是简单错误, 都是 l > r 出现负数了
笔者自己是分类讨论的, 所以不会出现 l > r 从而出现负数的情况, 但分类讨论中 有个地方 应该是 if(d < a) ans = 0; 写成了 if(d <= a), (┬_┬) 小于写成了小于等于
以后一定要仔细考虑 == 什么时候取到啊, 这里一个小忽视, 直接 从 371 名 稳涨分的名次调到了 846名 ⊙﹏⊙‖∣ 可惜了
分类讨论的情况比较多, 这样的小地方确实不容易检查的到, 没办法, 此外这个 A 题被我复杂化了, 不用这样麻烦分类讨论的,
这个就是 2个集合(区间)求交集, 然后 判断 k 是否在那个交集里。
1、分类讨论版本
#include <iostream> #include <cstdio> #include <vector> using namespace std; typedef long long LL; const int maxn = 1e6 + 8; //2 4 1 2 5 int main() { #ifdef LOCAL freopen("a.txt", "r", stdin); //freopen("a.out", "w", stdout); int T = 6; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); LL a, b, c, d, k, ans = 0; cin >> a >> b >> c >> d >> k; if(a == b){ if(a >= c && a <= d){ if(a != k) ans = 1; else ans = 0; } else ans = 0; } else if(c == d){ if(c >= a && c <= b){ if(k != c) ans = 1; else ans = 0; } else ans = 0; } else if(c <= a){ if(d >= b){ ans = b - a + 1; if(k >= a && k <= b) ans--; } else{ //WA29 if(d <= a) ans = 0; if(d < a) ans = 0; else{ ans = d - a + 1; if(k >= a && k <= d) ans--; } } } else if(c <= b){ if(d >= b){ ans = b - c + 1; if(k >= c && k <= b) ans--; } else{ ans = d - c + 1; if(k >= c && k <= d) ans--; } } else{ ans = 0; } cout << ans << endl; #ifdef LOCAL cout << endl; } #endif // LOCAL return 0; }
2、求交集版本,推荐版本
#include <iostream> #include <cstdio> #include <vector> using namespace std; typedef long long LL; const int maxn = 1e6 + 8; //2 4 1 2 5 int main() { #ifdef LOCAL freopen("a.txt", "r", stdin); //freopen("a.out", "w", stdout); int T = 7; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); LL a, b, c, d, k, ans = 0; cin >> a >> b >> c >> d >> k; LL l = max(a, c); LL r = min(b, d); if(l > r) ans = 0; else{ ans = r - l + 1; if(k >= l && k <= r) ans--; } cout << ans << endl; #ifdef LOCAL cout << endl; } #endif // LOCAL return 0; }
Thank you!
------from ProLights
相关文章推荐
- 匈牙利 算法&模板
- Day17对话框
- form表单与fiddler抓包
- 【c++】指针参数是如何传递内存的
- 20160916随笔
- HDU 5446 lucas定理 + 中国剩余定理
- 调整数组顺序使奇数位于偶数前面
- oracle实例诡异down的真实原因
- Altium designer窗口杂乱?给你两支招
- Lucene学习笔记1--lucene开篇hello lucene
- hdu 5442 F - Favorite Donut 后缀数组 / 字符串の最小表示法+kmp
- picasso无法更新控件上显示的拍照后的图片
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序
- 51Nod-1405-树的距离之和
- js 字符连接对象
- 自己表现得呵呵哒
- Check if tree b is part of tree a JAVA 实现
- 百度图片、百度文库, 维基百科英文版
- light oj 1017 简单区间dp...新学会的东西
- Node.js JSON模块