题意:给定n<=100000线段[l,r],然后给这些线段染色(red or blue),求最后平面上任意一个点被蓝色及红色覆盖次数只差的绝对值不大于1

思路:把每条线段拆成2个点[l<<1, r<<1|1]来表示,

那么如果把染red看成1,blue看成-1

那么对于每条线段染色为v(-1 or 1),等价于l<<1点为v,r<<1|1为-v,

则题目等价于求一种合法的方案使得任意点的abs(前缀和S)<=1

此外,由于任一点取值为 -1 or 1,并且过程中abs(S)<=1,所以任意奇数位置(下标从0开始)的前缀和为0

所以转化为图论模型,就是经典的二分图染色模型:

对于每个线段两端连一条边;

对于排序后相邻的(i<<1, i <<1|1)连一条边,

又由于图的特殊性不会出现奇数环。。

因为对于任意的一个点,只有两条边,并且这两条边类型不同,不妨设为A和B。

那么如果出现了环,那么路径上肯定是ABAB交替出现,并且是偶数。。因为出现奇环必有路径上出现重复的AA或者BB,显然这种情况是不会出现的。。

code:

 /*
* Author: Yzcstc
* Created Time: 2014/10/30 19:12:23
* File Name: cf429.cpp
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
using namespace std;
vector<int> e[];
int col[], n;
pair<int, int> p[];
void dfs(int u, int color){
if (col[u] != -) return;
col[u] = color;
for (int i = ; i < (int)e[u].size(); ++i)
dfs(e[u][i], color^);
} void solve(){
for (int i = ; i <= n * ; ++i) e[i].clear();
int l, r;
for (int i = ; i < n; ++i){
scanf("%d%d", &l, &r);
e[i<<].push_back(i<<|), e[i<<|].push_back(i<<);
p[i<<] = make_pair(l<<,i<<), p[i<<|] = make_pair(r<<|, i<<|);
}
sort(p, p + *n);
for (int i = ; i < n; ++i){
int x = p[i<<].second, y = p[i<<|].second;
e[x].push_back(y), e[y].push_back(x);
}
memset(col, -, sizeof(col));
for (int i = ; i < n; ++i){
if (col[i<<] == -) dfs(i<<, );
printf("%d ", col[i<<]);
}
} int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
while (scanf("%d", &n) != EOF){
solve();
}
return ;
}

最新文章

  1. UWP webview 键盘bug,回退页面,键盘会弹一下。
  2. VMware如何实现和主机共享网络上网
  3. C++注意事项
  4. 常见的SQL语句
  5. C#设计模式(13)——代理模式(Proxy Pattern)
  6. 500Internal Server Error
  7. python学习(三):matplotlib学习
  8. mysql学习笔记(sqlalchemy安装及简单使用)
  9. Mybatis源码解析(一)(2015年06月11日)
  10. windows phone (22) 隐藏元素
  11. WebService的简单实现
  12. 【转】揭秘JavaScript中谜一样的this
  13. SSM框架中前端页面(AJAX+Jquery+spring mvc+bootstrap)
  14. Web应用程序设计十个建议
  15. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习3
  16. python字符串前面加u,r,b的含义
  17. gitlab4.0_工程提交
  18. tensorflow学习6
  19. IDC:IDC 清单
  20. Windows Server 2012设置WinDbg Kernel Debugging Local

热门文章

  1. vbs下载文件
  2. C++学习基础八——重载输入和输出操作符
  3. Windows程序设计(第五版)学习:第一章 起步
  4. 关于 this 和 prototype 的理解
  5. 从Unity引擎过度到Unreal4引擎(最终版)
  6. asp.net GDI+绘制矩形渐变
  7. B站运维团队成长的血泪史
  8. PHP--目录处理
  9. UIWebView使用时的问题,包含修改user agent
  10. 在spark中操作mysql数据 ---- spark学习之七