https://codeforces.com/contest/1101/problem/C

C++版本一

题解:

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;struct Node
{int l;int r;int id;int f;
}ed[N];
bool cmp(const Node &x,const Node &y)
{if (x.l!=y.l) return x.l<y.l;return x.r<y.r;
}
bool cmp2(const Node &x,const Node &y)
{return x.id<y.id;
}
int main()
{scanf("%d",&t);while (t--){int flag=0;scanf("%d",&n);for (int i=1;i<=n;++i){scanf("%d%d",&ed[i].l,&ed[i].r);ed[i].id=i;}sort(ed+1,ed+n+1,cmp);int a;a=ed[1].r;ed[1].f=1;for (int i=2;i<=n;++i){if (!flag){if (ed[i].l<=a) a=max(a,ed[i].r),ed[i].f=1;else{flag=1;ed[i].f=2;}}else ed[i].f=2;}if (!flag) printf("-1\n");else{sort(ed+1,ed+n+1,cmp2);for (int i=1;i<=n;++i){printf("%d ",ed[i].f);}printf("\n");}}
}

C++版本二

题解:根据题意两组区间不能有交集。所以至少有两块独立的合并后区间。

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;struct node{int l,r,id,f;bool operator <(const node &S)const{if(l==S.l)return r<S.r;return l<S.l;}
}e[N];
int c[N<<1];
char str;
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endif//scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d",&e[i].l,&e[i].r);e[i].id=i;}sort(e+1,e+n+1);int pos=e[1].r;flag=1;c[e[1].id]=1;q=1;for(int i=2;i<=n;i++){if(pos<e[i].l){flag=0;if(q==1)q=2;elseq=1;}c[e[i].id]=q;pos=max(pos,e[i].r);}if(flag){printf("-1\n");continue;}for(int i=1;i<n;i++){printf("%d ",c[i]);}printf("%d\n",c[n]);}//cout << "Hello world!" << endl;return 0;
}

Division and Union相关推荐

  1. 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 ...

  2. 【CF套题】 Educational Codeforces Round 58

    [前言] 组队CF之帮wyl上橙,我和sc打小号上紫. 结果sc成功FST两题,wyl成功skipped. 我的小号幸存了qwq. [题目] 原题地址 A.Minimum Integer 特判一下dd ...

  3. Python type hints 之 Optional,Union

    1,前言 type hint 在pep484加入,我个人觉得这种类似于类型约束的(机制)有点违背了python简单.简洁的初衷,在慢慢向c# java 这种强类型语言看齐的节奏. 不过好在不强制使用, ...

  4. C++ 共用体union 的使用

    共用体是什么 共用体将不同的数据类型组织为一个整体, 需要注意的是,共用体在同一时刻只能存储一个数据成员的值., 共用体变量的地址和它的格式成员的地址都是同一地址 共用体的一般形式 union 共用体 ...

  5. 关于 并查集(union find) 算法基本原理 以及 其 在分布式图场景的应用

    二月的最后一篇水文-想写一些有意思的东西. 文章目录 环检测在图数据结构中的应用 深度/广度优先 检测环 并查集数据结构 (Union-Find) 基本概念 初始化 合并 union 查找祖先 优化1 ...

  6. 联合体union和大小端(big-endian、little-endian)

    1.联合体union的基本特性--和struct的同与不同 union,中文名"联合体.共用体",在某种程度上类似结构体struct的一种数据结构,共用体(union)和结构体(s ...

  7. struct和union的大小问题

    union类型以其中size最大的为其大小 struct类型以其中所有size大小之和为其大小 #include<iostream> using namespace std; int ma ...

  8. 利用c语言结构体和union实现类似c++的public,private的实现

    最近在看strongswan源代码,看到strongswan的代码框架很有意思,用C语言实现类的思想.当我们编写完一个模块,我们需要提供的是H的文件给其他模块使用,我们希望H文件中就只能包含一些公有函 ...

  9. php union all,Union与Union All的区别

    Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...

最新文章

  1. 爱奇艺才被做空又爆裁员,技术研发为裁员重灾区
  2. Intellij IDEA打开就闪退或关闭
  3. 全排列算法的字典序排列
  4. Java JDK代理、CGLIB、AspectJ代理分析比较
  5. 微服务的好处与弊端_在云原生时代,就一定要用微服务吗?
  6. 利用自定命令打开常用软件,小白秒变大神。
  7. centos7删除文件命令_干货 | 玩转云文件存储——利用CFS实现web应用的共享访问...
  8. Eclipse 常用快捷键,实战经典
  9. opencv python教程-OpenCV4 Python 最新中文版官方教程来了(附下载)
  10. MikroTik RouterOS 6.x版本开始支持使用img镜像安装(U盘安装的终极解决方法)
  11. Git下载安装以及基本指令使用
  12. 统考计算机各题型分数,计算机等级考试分值
  13. erp生产管理系统对电子工厂发挥的作用
  14. python计算球体表面积和体积_图解球体表面积和体积正确计算方法及计算公式(原创)...
  15. 实现收藏本站和设为首页功能
  16. 轻松禁止自动更新FLASH插件有绝招
  17. Android使用Activity用作弹出式对话框Dialog
  18. python分割图片数字_python实现图片中文字分割效果
  19. HTML Purifier --非常好用的XSS过滤器
  20. Linux下ps命令

热门文章

  1. linux 上传工具 lr,Linux传输工具lrzsz
  2. java 全局变量 内存不回收_JAVA知识梳理:内存管理与垃圾回收机制
  3. linux的write是线程安全的吗,socket的write/send还是是否是线程安全?
  4. 动作分析 姿态估计_单人或多人的人体姿态骨架估计算法概述
  5. spark任务shell运行_了解Spark 应用的一生
  6. CSS设置文本字体居中
  7. 卡在登陆界面进不去_穿越火线第十三年:需要的不仅仅是新界面,重要还是留住旧玩家...
  8. php 计算字符串相邻最大重复数_php查找字符串出现次数的方法
  9. java arraylist线程安全_ArrayList升级为线程安全的List
  10. python雷达和柱形图_Python Pygal常见数据图(折线图、柱状图、饼图、点图、仪表图和雷达图)详解...