CodeForces 479C Exams 贪心
题目:
1 second
256 megabytes
standard input
standard output
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly nexams. 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.
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.
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.
3
5 2
3 1
4 2
2
3
6 1
5 2
4 3
6
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.
题解:
先按a的值排序,排序完后,考虑最后一个考试要不要换,如果换了能贪心出来非降序则换,否则就不换。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std; const int maxn=+; int n; struct Node{
int x,y;
bool operator < (const Node& tmp) const {
return x>tmp.x||(x==tmp.x)&&y>tmp.y;
}
}nds[maxn]; int main() {
// freopen("data_in.txt","r",stdin);
while (scanf("%d",&n)==&&n) {
for(int i=;i<n;i++){
scanf("%d%d",&nds[i].x,&nds[i].y);
}
sort(nds,nds+n);
int su=;
swap(nds[].x,nds[].y);
for(int i=;i<n;i++){
if(nds[i].y>nds[i-].x){
su=;
break;
}
if(nds[i].x>nds[i-].x){
swap(nds[i].x,nds[i].y);
}
}
int ans;
if(su) ans=nds[].x;
else ans=nds[].y;
printf("%d\n",ans);
}
return ;
}
CodeForces 479C Exams 贪心的更多相关文章
- codeforces 479C Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...
- CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...
- Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心
C. Vanya and Exams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...
- Codeforces Round #274 (Div. 1) A. Exams 贪心
A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...
- codeforces 480A A. Exams(贪心)
题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟
http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams (贪心,模拟)
题意:你有\(n\)天的时间,这段时间中你有\(m\)长考试,\(s\)表示宣布考试的日期,\(d\)表示考试的时间,\(c\)表示需要准备时间,如果你不能准备好所有考试,输出\(-1\),否则输出你 ...
- Codeforces Round #274 (Div. 2) C. Exams (贪心)
题意:给\(n\)场考试的时间,每场考试可以提前考,但是记录的是原来的考试时间,问你如何安排考试,使得考试的记录时间递增,并且最后一场考试的时间最早. 题解:因为要满足记录的考试时间递增,所以我们用结 ...
- codeforces 724D(贪心)
题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...
随机推荐
- mysql如何批量删除数据表
-- 注意这里的`是英文输入法状态下,主键盘数字1的左边的键.drop table `user`,`c_class`;
- Spark RDD理解
目录 ----RDD简介 ----RDD操作类别 ----RDD分区 ----宽依赖和窄依赖作用 ----RDD分区划分器 ----RDD到调度 返回顶部 RDD简介 RDD是弹性分布式数据集(Res ...
- CentOS6编译安装gcc高版本
编译安装gcc高版本 因CentOS中gcc版本仅有4.4,故编译安装gcc高版本. 安装依赖库(如果你已安装过gcc低版本,可跳过这步) yum install glibc-static libst ...
- ImageMagick命令执行学习笔记(常见于图片预览处)
实验版本: ImageMagick版本:6.9.2 push graphic-context viewbox 0 0 640 480 fill 'url(https://"|whoami&q ...
- Vue2.5入门-1
vue如何引用和使用,实例和挂在点的介绍 <!DOCTYPE html> <html> <head> <title>vue 入门</title&g ...
- Go语言基础-序言
2018年6月,第一次接触go语言,在之后通过多本书籍渐渐了解go语言之后,开启了自己go语言全栈工程师的道路.特此记录,希望能给后学的朋友提供一个方向. 语言是一门寻寻渐进的课程,结合自己这两个月的 ...
- intel-FPGA的片内存储器问题
FPGA的片内有很多的存储器资源,可以配置成单端口的ROM.RAM和双端口的ROM.RAM,以及移位寄存器和FIFO等.在学习过程中,笔者遇到过几个小问题,总结如下: 片内是不是有ROM或者RAM? ...
- 最近公共祖先 lca (施工ing)
声明 咳咳,进入重难点的图论算法之一(敲黑板): 题目: 洛谷 P3379 先放标程,施工ing,以后补坑!!!(实在太难,一个模板这么长 [ 不过好像还是没有 AC自动机 长哎 ],注释都打半天,思 ...
- 20+ Docs and Guides for Front-end Developers (No. 5)
It’s that time again to choose the tool or technology that we want to brush up on. If you feel like ...
- 20155301 2016-2017-2 《Java程序设计》第3周学习总结
20155301 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 1.在clothes类中定义了两个变量,很像C语言中自定义变量,clothes属于非公开类. ...