zoukankan      html  css  js  c++  java
  • BZOJ 2957 楼房重建 (分块)

    题解:分块,然后暴力维护每一块上升序列,注意是不是最长上升序列,二分查找第二块中大于第一块的最后一个上升序列中的数。

       注意:每一块的大小不要用√n会T掉的,把块的大小设为500-600都可以(T了一页了)。或者你用线段树去写。

      

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <vector>
    #include <cstring>
    #include <iomanip>
    #include <set>
    #include<ctime>
    //#include<unordered_map>
    //CLOCKS_PER_SEC
    #define se second
    #define fi first
    #define ll long long
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define Pii pair<int,int>
    #define Pli pair<ll,int>
    #define ull unsigned long long
    #define pb push_back
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    const int N=1e5+10;
    const ull base=163;
    const int INF=0x3f3f3f3f;
    using namespace std;
    
    int h[N];
    struct node {
        int top,sta[600];
    }p[600];
    bool cmp(int x,int y){
        if(!y)return h[x]>0;
        return 1LL*y*h[x]>1LL*x*h[y];
    }
    int main(){
        int n,m;
        scanf("%d%d",&n,&m);
        int sz=550;
        for(int i=1;i<=m;i++){
            int x;scanf("%d",&x);
            scanf("%d",&h[x]);
            int l=x/sz*sz;
            int r=min(n+1,(x/sz+1)*sz);
            p[x/sz].top=0;
            for(int i=l;i<r;i++){
                if(cmp(i,p[x/sz].sta[p[x/sz].top])){
                    p[x/sz].sta[++p[x/sz].top]=i;
                }
            }
            int last=0,ans=0;
            for(int i=0;i<=(n)/sz;i++){
                int l=1,r=p[i].top;
                int op=0;
                while (l<=r) {
                    int mid=(l+r)>>1;
                    if(cmp(p[i].sta[mid],last)){
                        r=mid-1;
                        op=mid;
                    }
                    else{
                        l=mid+1;
                    }
                }
                if(op){
                    last=p[i].sta[p[i].top];
                    ans+=p[i].top-op+1;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    无感无刷直流电机驱动方式
    STM32F4 GPIO 呼吸灯【库函数操作】
    STM32F4之ADC1【库函数操作】
    STM32 SPI初始化和使用
    ASP.NET可以包含的内容
    STM32 RCC实验 MCO脚输出时钟波形
    STM32F4之USART【库函数操作】
    STM32 MDK工程中使用printf
    STM32F4 外部中断EXTI【库函数操作】
    STM32F4 ADC采集数据的DMA数据传输【库函数操作】
  • 原文地址:https://www.cnblogs.com/Mrleon/p/9051784.html
Copyright © 2011-2022 走看看