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