JZOJ5006. A
来源:互联网 发布:滨州大数据产业园 编辑:程序博客网 时间:2024/06/05 16:07
题目大意
给出一个长度为
现在有
问最多能同时满足多少个条件。
Data Constraint
题解
如果我们能将这些区域的关系建成一棵树的话,就很好求答案了。
考虑扫描线,从下往上扫描,每次遇到一个挡板,就合并板两侧的区域,作为一个新区域,这个可以并查集维护。然后将条件挂到对应区域的点上即可。
时间复杂度:
SRC
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespace std ;#define N 5000000 + 10struct Note { int w , h , type ;} C[N] ;struct Stype { int Num0 , Num1 ;} P[N] ;int Node[2*N] , Next[2*N] , Head[N] , tot ;int f[N] , g[N] , fa[N] ;int Case , n , m , Cnt ;bool cmp( Note a , Note b ) { return a.h < b.h || ( a.h == b.h && a.type < b.type ) || ( a.h == b.h && a.type == b.type && a.w < b.w ) ; }int Read() { int ret = 0 ; char ch = getchar() ; while ( ch < '0' || ch > '9' ) ch = getchar() ; while ( ch >= '0' && ch <= '9' ) { ret = ret * 10 + ch - '0' ; ch = getchar() ; } return ret ;}void link( int u , int v ) { Node[++tot] = v ; Next[tot] = Head[u] ; Head[u] = tot ;}int Get( int x ) { return fa[x] == x ? x : fa[x] = Get( fa[x] ) ; }int NewNode() { ++ Cnt ; fa[Cnt] = Cnt ; P[Cnt].Num0 = P[Cnt].Num1 = 0 ; return Cnt ;}void Union( int x , int y ) { int fy = Get(y) ; if ( fy == x ) return ; link( x , fy ) ; fa[fy] = x ;}void DFS( int x ) { f[x] = P[x].Num1 ; g[x] = P[x].Num0 ; for (int p = Head[x] ; p ; p = Next[p] ) { DFS( Node[p] ) ; f[x] += f[Node[p]] ; g[x] += max( f[Node[p]] , g[Node[p]] ) ; }}int main() { Case = Read() ; while ( Case -- ) { tot = Cnt = 0 ; memset( P , 0 , sizeof(P) ) ; memset( Head , 0 , sizeof(Head) ) ; n = Read() , m = Read() ; for (int i = 1 ; i <= n ; i ++ ) fa[i] = i ; for (int i = 1 ; i < n ; i ++ ) { C[m+i].w = i ; C[m+i].h = Read() ; C[m+i].h ++ ; C[m+i].type = -1 ; } for (int i = 1 ; i <= m ; i ++ ) { C[i].w = Read() ; C[i].h = Read() ; C[i].type = Read() ; C[i].h ++ ; } sort( C + 1 , C + n + m , cmp ) ; Cnt = n ; C[n+m].h = -1 ; for (int i = 1 ; i <= n + m - 1 ; i ++ ) { for (int j = i ; j <= n + m ; j ++ ) { if ( C[j].h != C[i].h ) { i = j - 1 ; break ; } if ( C[j].type == -1 ) { int now = NewNode() ; Union( now , C[j].w ) ; Union( now , C[j].w + 1 ) ; } else { int now = NewNode() ; Union( now , C[j].w ) ; if ( C[j].type == 0 ) P[now].Num0 ++ ; else P[now].Num1 ++ ; } } } int Root = Get(1) ; DFS( Root ) ; printf( "%d\n" , max( f[Root] , g[Root] ) ) ; } return 0 ;}
以上.
1 0
- JZOJ5006. A
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- A
- A*
- a
- A
- oracle数据库创建表空间,创建用户,给用户授权
- js 跨域问题解决方案
- CI1.4 字符串空格替换
- Android绕过usb主机permision确认对话框framework修改方案
- [Leetcode] Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
- JZOJ5006. A
- Spring 注解
- 射频识别技术漫谈(11)——Mifare系列卡的共性
- dragger2的入门基础
- 扫雷实用帖:HBase读延迟的12种优化套
- JS内置对象
- 《中山大学内部选拔(一)》 游戏
- nginx for windows再编译版
- 排序算法:快速排序