贪心研究(1) -------- 区间贪心的基本思路poj1083

来源:互联网 发布:qt5开发及实例 源码 编辑:程序博客网 时间:2024/05/22 15:33

#include <stdio.h>#include <string>#include <string.h>#include <queue>#include <stack>#include <map>#include <iostream>#include <stdlib.h>#include <math.h>#include <algorithm>#define mod 10e9+7;#define inf 0x3f3f3f3f;#define mem(x , y)  memset(x , y , sizeof(x))const int MAX = 100000;using namespace std;struct node{    int s,e ;    bool operator < (const node & A)const{        return s < A.s  ;    }}data[MAX] ;int vis[MAX] ;int main(){    int T;    scanf("%d",&T);    while(T--){        int n ;        int ans = 0 ;        mem(vis , 0) ;        mem(data, 0) ;        scanf("%d",&n) ;        for(int i=0;i<n;i++)        {            int a,  b ;            scanf("%d%d",&a,&b) ;            if(a & 1) a = (a - 1)/2 ;            else a = a / 2 - 1;            if(b & 1) b = (b - 1)/2 ;            else b = b / 2 - 1 ;            for(int j=min(a,b) ; j <= max(a , b) ; j++){                vis[j] ++ ;                ans = max(vis[j] , ans) ;            }        }        printf("%d\n",ans*10) ;    }}

对于贪心 , 主要是抓住问题的关键点 ;

poj1083-----------若干个区间 , 求不相交区间的组数 ;

对于区间问题,主要是

1.区间排序 ------ 优化问题 ,贪心的一种思路

2.树状树组-------优化覆盖个数的计数问题

3.拆分单位长度,离散化(可以使用数据结构优化) ;

对于此题 , 如果一个单位区间被n条线段相交 , 那么至少要走n次  。由此得出下限 。

下面证明每一次选玩max的计数一定减一 ,

假设两个相邻最近的max片段a,b。什么情况下会出现选完之后最大值还是max 。 那就是对于选定一个a的区间 ,包含b的区间都不会被选到 , 也就是都和a的区间有交点 。 那么那个交点的counter就等于max+1了,故不可能 。 也就是每一个max区间段每一次选择不相交区间都可以找到一个区间包含它且符合要求 。所以最少max*10的时间

0 0