CF#274 (Div. 2) C.(贪心+排序)
2014-10-21 00:13
375 查看
C. Exams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
题目链接:http://codeforces.com/contest/479/problem/C
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly
n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.
According to the schedule, a student can take the exam for the
i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the
i-th subject allowed him to take an exam before the schedule time on day
bi (bi < ai). Thus, Valera can take an exam for the
i-th subject either on day
ai, or on day
bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number
ai.
Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he
takes exams so that all the records in his record book go in the order of non-decreasing date.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.
Each of the next n lines contains two positive space-separated integers
ai and
bi (1 ≤ bi < ai ≤ 109)
— the date of the exam in the schedule and the early date of passing the
i-th exam, correspondingly.
Output
Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.
Sample test(s)
Input
Output
Input
Output
Note
In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes
an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.
In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
解题思路:
题意是某人有n科考试,每科考试给a和b两个值。a代表老师规定的考试日期,b代表在第b天他也可以去考试。也就是说他只能选择在第a天或第b天去考试。求最后一科
考试日期的最小值。老师会给他记录考试日期,所有课按规定的a记录,要求最后的到的记录是升序排列。
把a按升序排列,a相同的,b小的放前面。然后res初始为1,如果当前的b大于等于res,那么更新res;否则,用当前的a更新res。就是每次花尽可能小的天数来更新res,这样求出的res就是最小的结束日期。
完整代码:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
题目链接:http://codeforces.com/contest/479/problem/C
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly
n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.
According to the schedule, a student can take the exam for the
i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the
i-th subject allowed him to take an exam before the schedule time on day
bi (bi < ai). Thus, Valera can take an exam for the
i-th subject either on day
ai, or on day
bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number
ai.
Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he
takes exams so that all the records in his record book go in the order of non-decreasing date.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.
Each of the next n lines contains two positive space-separated integers
ai and
bi (1 ≤ bi < ai ≤ 109)
— the date of the exam in the schedule and the early date of passing the
i-th exam, correspondingly.
Output
Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.
Sample test(s)
Input
3 5 2 3 1 4 2
Output
2
Input
3
6 1
5 24 3
Output
6
Note
In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes
an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.
In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
解题思路:
题意是某人有n科考试,每科考试给a和b两个值。a代表老师规定的考试日期,b代表在第b天他也可以去考试。也就是说他只能选择在第a天或第b天去考试。求最后一科
考试日期的最小值。老师会给他记录考试日期,所有课按规定的a记录,要求最后的到的记录是升序排列。
把a按升序排列,a相同的,b小的放前面。然后res初始为1,如果当前的b大于等于res,那么更新res;否则,用当前的a更新res。就是每次花尽可能小的天数来更新res,这样求出的res就是最小的结束日期。
完整代码:
#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; struct node { int a ; int b; }q[10001]; bool cmp(node x , node y) { if(x.a == y.a) return x.b < y.b; return x.a < y.a; } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int n ; while(~scanf("%d",&n)) { for(int i = 0 ; i < n ; i ++ ) { scanf("%d%d",&q[i].a,&q[i].b); } sort(q , q + n , cmp); int res = 1; for(int i = 0 ; i < n ; i ++) { if(q[i].b >= res) res = q[i].b; else res = q[i].a; } printf("%d\n",res); } }
相关文章推荐
- Codeforces Round #169 (Div. 2)C. Little Girl and Maximum Sum(贪心/排序)
- Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
- 巧妙地利用STL map set pair 贪心+排序 Codeforces Round #331 (Div. 2)C. Wilbur and Points
- 【Codeforces Round 332 (Div 2)C】【贪心】Day at the Beach 最多区间数划分使得区间排序构成全局排序
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序
- Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)
- Codeforces #274 (Div. 2) C. Exams(贪心:结构体排序)
- Codeforces Round #402 (Div. 2) C. Dishonest Sellers 贪心、排序
- 【Codeforces Round 335 (Div 2) C】【贪心 脑洞 有趣排序】Sorting Railway Cars 全排列排序可以提前后提后的最小操作数
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心
- 【Codeforces Round 329 (Div 2) B】【映射 排序 贪心】Anton and Lines 查看所有直线pair在[x1,x2]范围内是否有交点
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序, 贪心, 暴力(居然可以n方暴力)
- CodeForces #431 Div. 2 849C From Y to Y 贪心 技巧题
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心
- Codeforces Round #390 (Div. 2) - A Lesha and array splitting (贪心)
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
- Codeforces Round #389 (Div. 2) C. Santa Claus and Robot 贪心+最短路
- Codeforces Round #227 (Div. 2) / 387C George and Number (贪心)
- 【Wunder Fund Round 2016 (Div 1 + Div 2 combined)B】【暴力 贪心】Guess the Permutation 全排列a[i][j]=min(p[i],p
- Codeforces Round #424 (Div. 2) D. Office Keys(贪心 二分 or DP)