zoukankan      html  css  js  c++  java
  • bzoj3718 树状数组

    https://www.lydsy.com/JudgeOnline/problem.php?id=3718

    有时候,要透过题面看到本质

    题意 你的老板命令你将停车场里的车移动成他想要的样子。
    停车场是一个长条矩形,宽度为w。我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系。停车场很长,我们可以认为它一直向右边伸展到无穷远处。
    车都是边平行于坐标轴的矩形,大小可能不同。你可以将车任意地平移(但不能旋转),只要他们不超出停车场的边界,且不能互相碰撞,但紧挨着是允许的(即任意时刻任两辆车的重叠面积为0)。
    你知道目前各辆车的摆放位置,以及老板心中所想的位置。你需要判断是否可以办到老板的任务。

    乍一看觉得毫无地方可以下手,仔细一想会贪心的想到去一个个排列,但也觉得不太好做,但是事实上一辆汽车的移动我们可以用三个条件来描述,他的起始点s,他的终点e,他的宽度w,如果有两辆车满足 s1 < s2 && e1 > e2 && w1 + w2 > W的时候,就是否定情况,否则位肯定情况。因为在这种情况下的两辆车必定需要并排交错。

    这就成了一个三维问题,考虑用排序+树状数组维护前缀最大值解决即可,注意加一个离散化。

    #include <map>
    #include <set>
    #include <ctime>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    #define For(i, x, y) for(int i=x;i<=y;i++)  
    #define _For(i, x, y) for(int i=x;i>=y;i--)
    #define Mem(f, x) memset(f,x,sizeof(f))  
    #define Sca(x) scanf("%d", &x)
    #define Sca2(x,y) scanf("%d%d",&x,&y)
    #define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define Scl(x) scanf("%lld",&x);  
    #define Pri(x) printf("%d
    ", x)
    #define Prl(x) printf("%lld
    ",x);  
    #define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
    #define LL long long
    #define ULL unsigned long long  
    #define mp make_pair
    #define PII pair<int,int>
    #define PIL pair<int,long long>
    #define PLL pair<long long,long long>
    #define pb push_back
    #define fi first
    #define se second 
    typedef vector<int> VI;
    const double eps = 1e-9;
    const int maxn = 5e4 + 10;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9 + 7; 
    int N,M,K,W;
    int tree[maxn];
    struct Point{
        int x1,y1,x2,y2;
        int id,w;
    }s[maxn],e[maxn];
    struct Car{
        int s,e,w;
    }car[maxn];
    int Hash[maxn];
    int lowbit(int t){
        return t & -t;
    }
    void add(int x,int t){
        for(;x <= N ; x+= lowbit(x)) tree[x] = max(tree[x],t);
    }
    int getmax(int t){
        int s = 0;
        for(;t > 0;t -= lowbit(t)) s = max(s,tree[t]);
        return s;
    }
    bool cmp(Car a,Car b){
        return a.e > b.e;
    }
    int main()
    {
        int T; Sca(T);
        while(T--){
            Sca2(N,W); Mem(tree,0);
            For(i,1,N){
                int x1,y1,x2,y2;
                Sca2(x1,y1); Sca2(x2,y2);
                car[i].s = Hash[i] = min(x1,x2);
                car[i].w = abs(y2 - y1);
            }
            For(i,1,N){
                int x1,y1,x2,y2;
                Sca2(x1,y1); Sca2(x2,y2);
                car[i].e = min(x1,x2);
            }
            sort(Hash + 1,Hash + 1 + N);
            int cnt = unique(Hash + 1,Hash + 1 + N) - Hash - 1;
            For(i,1,N) car[i].s = lower_bound(Hash + 1,Hash + 1 + cnt,car[i].s) - Hash;
            sort(car + 1,car + 1 + N,cmp);
            bool flag = 1;
            For(i,1,N){
                int j = i;
                for(;car[i].e == car[j].e && j <= N; j++); j--;
                for(int k = i ; k <= j ; k ++) if(getmax(car[k].s - 1) + car[k].w > W) flag = 0;
                for(int k = i; k <= j ; k ++) add(car[k].s,car[k].w);
                i = j;
            }
            if(flag) puts("TAK");
            else puts("NIE");
        }
        #ifdef VSCode
        system("pause");
        #endif
        return 0;
    }
  • 相关阅读:
    十五天精通WCF——第六天 你必须要了解的3种通信模式
    十五天精通WCF——第五天 你需要了解的三个小技巧
    十五天精通WCF——第四天 你一定要明白的通信单元Message
    十五天精通WCF——第三天 client如何知道server提供的功能清单
    十五天精通WCF——第二天 告别烦恼的config配置
    十五天精通WCF——第一天 三种Binding让你KO80%的业务
    SimpleAdapter & BaseAdapter
    android:layout_gravity和android:gravity的区别
    Restrict & Cascade
    正确理解Mysql的列索引和多列索引
  • 原文地址:https://www.cnblogs.com/Hugh-Locke/p/9769085.html
Copyright © 2011-2022 走看看