CodeForces 22D Segments 排序水问题
主题链接:点击打开链接
升序右键点。采取正确的点
删边暴力
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include <set>
#include <math.h>
using namespace std;
#define inf 10000000
#define ll int
#define N 2105
#define L(x) (x<<1)
#define R(x) (x<<1|1)
inline ll Mid(ll a,ll b){return (a+b)>>1;}
ll n;
struct node{
ll x,y;
}a[N];
bool cmp(node a,node b){
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
vector<ll>ans;
ll h[N];
int main(){
ll i, j;
while(~scanf("%d",&n)){
for(i=1;i<=n;i++){
cin>>a[i].x>>a[i].y;
if(a[i].x>a[i].y)swap(a[i].x,a[i].y);
}
memset(h, 0, sizeof h);
sort(a+1,a+n+1,cmp);
ans.clear();
for(i = 1; i <=n;i++)if(!h[i]){
ans.push_back(a[i].y);
for(j=i;j<=n;j++)
{
if(a[j].x<=a[i].y && a[i].y<=a[j].y)
h[j] = 1;
}
}
cout<<ans.size()<<endl;
for(i=0;i<ans.size();i++)
printf("%d%c",ans[i],i==((ll)ans.size()-1)?'\n':' ');
}
return 0;
}
/*
3
40 -83
52 -80
-21 -4 */
/*
struct node{
ll l, r, id;
ll maxx,val,lmaxx;
}tree[N];
void push_down(ll id){
if(tree[id].l==tree[id].r)return ;
if(tree[id].val){
tree[L(id)].val += tree[id].val;
tree[R(id)].val += tree[id].val;
tree[L(id)].lmaxx += tree[id].val;
tree[L(id)].maxx += tree[id].val;
tree[R(id)].lmaxx += tree[id].val;
tree[R(id)].maxx += tree[id].val;
tree[id].val = 0;
}
}
void build(ll l, ll r, ll id){
tree[id].l = l, tree[id].r = r;
tree[id].maxx = tree[id].val = 0;
tree[id].lmaxx = 0;
if(l==r)return ;
ll mid = Mid(l,r);
build(l,mid,L(id)); build(mid+1,r,R(id));
}
void updata(ll l, ll r, ll val, ll id){
push_down(id);
if(l == tree[id].l && tree[id].r == r){
tree[id].val += val;
tree[id].maxx += val;
tree[id].lmaxx += val; return;
}
ll mid = Mid(tree[id].l, tree[id].r);
if(mid<l)updata(l,r,val,R(id));
else if(r<=mid)
updata(l,r,val,L(id));
else {
updata(l,mid,val,L(id));
updata(mid+1,r,val,R(id));
}
tree[id].maxx = max(tree[L(id)].maxx, tree[R(id)].maxx);
tree[id].lmaxx = tree[L(id)].lmaxx;
}
struct hehe{
ll val, x;
hehe(ll a=0,ll b=0):val(a),x(b){}
bool operator<(const hehe&a)const{
if(a.val==val)return a.x<x;
return a.val>val;
}
};
hehe query(ll l, ll r, ll id){
push_down(id);
if(l == tree[id].l && tree[id].r == r && tree[id].lmaxx==tree[id].maxx)
return hehe(tree[id].maxx, tree[id].l);
ll mid = Mid(tree[id].l, tree[id].r);
if(mid<l)return query(l,r,R(id));
else if(r<=mid)return query(l,r,L(id));
else return max(query(l,mid,L(id)),query(mid+1,r,R(id)));
}
set<ll>myset;
set<ll>::iterator p;
map<ll,ll>mp;
ll pos[N];
vector<ll>ans;
int use[N];
int main(){
ll i, j, u, v;
while(cin>>n){
myset.clear();
mp.clear();
ans.clear();
for(i=1;i<=n;i++){
cin>>x[i]>>y[i];
if(x[i]>y[i])swap(x[i],y[i]);
myset.insert(x[i]);
myset.insert(y[i]);
use[i] = true;
}
ll siz = 1;
for(p=myset.begin(); p!=myset.end(); p++, siz++) {
pos[siz] = *p;
mp.insert(pair<ll,ll>(*p,siz));
}
for(i=1;i<=n;i++) {
x[i] = mp.find(x[i])->second;
y[i] = mp.find(y[i])->second;
}
build(1,siz-1,1);
for(i=1;i<=n;i++)
updata(x[i],y[i],1,1);
while(tree[1].maxx){
hehe now = query(1,siz-1,1);
ans.push_back(pos[now.x]);
for(i=1;i<=n;i++)
if(x[i]<=now.x&&now.x<=y[i] && use[i])
updata(x[i],y[i],-1,1), use[i] = false;
}
cout<<ans.size()<<endl;
for(i=0;i<ans.size();i++)
printf("%d%c",ans[i],i==((ll)ans.size()-1)?'\n':' ');
}
return 0;
}
/**/
版权声明:本文博客原创文章,博客,未经同意,不得转载。
CodeForces 22D Segments 排序水问题的更多相关文章
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
- Codeforces Gym 100531G Grave 水题
Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...
- Division and Union CodeForces - 1101C (排序后处理)
There are nn segments [li,ri][li,ri] for 1≤i≤n1≤i≤n. You should divide all segments into two non-emp ...
- PAT甲题题解-1012. The Best Rank (25)-排序水题
排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...
- PAT甲题题解-1062. Talent and Virtue (25)-排序水题
水题,分组排序即可. #include <iostream> #include <cstdio> #include <algorithm> #include < ...
- hdu1106 排序水题
Problem Description 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整 ...
- CodeForces 705A(训练水题)
题目链接:http://codeforces.com/problemset/problem/705/A 从第三个输出中可看出规律, I hate that I love that I hate it ...
- CodeForces Gym 100685C Cinderella (水题)
题意:给定 n 个杯子,里面有不同体积的水,然后问你要把所有的杯子的水的体积都一样,至少要倒少多少个杯子. 析:既然最后都一样,那么先求平均数然后再数一下,哪个杯子的开始的体积就大于平均数,这是一定要 ...
- CodeForces 534B Covered Path (水题)
题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...
随机推荐
- VSTO学习笔记(三) 开发Office 2010 64位COM加载项
原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...
- A Game of Thrones(13) - Tyrion
The north went on forever. Tyrion Lannister knew the maps as well as anyone, but a fortnight on the ...
- linux zombie process相关学习
1. zombie process是什么? zombie process是那些在系统中已经死掉的process, 通过ps -A | grep defunct可以查看系统中有多少zombie proc ...
- SimpleDateFormat 的线程安全问题与解决方式
SimpleDateFormat 的线程安全问题 SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的详细类. 它同意格式化 (date -> text).语法分析 (te ...
- 阿里巴巴2015研究project普通笔试题,与答案
欢迎您对这篇文章的其他建议.我可以留言在以下平台. 个人博客网站:www.anycodex.com/blog/ Csdn博客网站:http://my.csdn.net/?ref=toolbar 微博: ...
- 基于CefGlue的桌面应用开发
原文地址:http://johnnyfee.github.io/csharp/2013/12/21/cef-glue/ 前言 如果你想使用WEB技术来开发桌面客户端,并且是想使用的语言也是C#时,那请 ...
- CRL 版本2.1.0.0下载
此次更新完善了部份功能,详情见UpdateLog,重新整理了开发文档,更美档,更详细 百度盘下载 下载地址: 百度盘下载
- iOS_UIButton 简单操作
UIButton 风格 typedef NS_ENUM(NSInteger, UIButtonType) { UIButtonTypeCustom = 0, // no button type UIB ...
- 一些周期性GC的理由为何
1.供tomcat:防止内存泄漏监听器 JreMemoryLeakPreventionListener在上班,每隔一小时默认触发一次System.gc Class clazz = Class.forN ...
- MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录
分组排序查询最大记录 //对 "catagory" 不等于 null 的数据进行分组查询,且查询结果倒序 db.getCollection('userAccount').aggre ...