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\n", x)
#define Prl(x) printf("%lld\n",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;
}

 

转载于:https://www.cnblogs.com/Hugh-Locke/p/9769085.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值