首页 > 其他分享 >ABC245E Wrapping Chocolate [线段树二分]

ABC245E Wrapping Chocolate [线段树二分]

时间:2023-08-09 18:26:29浏览次数:53  
标签:二分 return int 线段 Wrapping Chocolate 物品 盒子 ABC245E

也许更好的阅读体验

\(\mathcal{Description}\)
\(n\)个物品有长和宽,\(m\)个盒子也有长和宽,一个盒子最多可以装一个物品,问\(n\)个物品能否都放进盒子,物品和盒子不能旋转
\(\mathcal{Solution}\)
先离散化长和宽,将物品和盒子按照长从大到小排序
考虑到当前物品时将所有长大于等于当前物品的盒子全部放进一个权值线段树,权值线段树维护长大于等于当前物品的并且宽为\(x\)的盒子有多少个,则在线段树上二分出宽刚好大于x的位置,将对应数量减\(1\)即可

我是傻子,用multiset不需要离散化还自带二分和删除,就当是线段树二分练手吧
\(\mathcal{Code}\)

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 10;
const int maxt = 2e6 + 10;
struct pt {
    int x, y;
}a[maxn], b[maxn];
int n, m, tot, rt;
int t[maxn << 1];
int val[maxt], ls[maxt], rs[maxt];
void insert (int &k, int l, int r, int x)
{
    if (!k) k = ++tot;
    if (l == r) return ++val[k], void();
    int md = (l + r) >> 1;
    if (x <= md)    insert(ls[k], l, md, x);
    else    insert(rs[k], md + 1, r, x);
    val[k] = val[ls[k]] + val[rs[k]];
}
bool delet (int &k, int l, int r, int x)
{
    if (!k || !val[k]) return false;
    if (l == r) return --val[k], true;
    int md = (l + r) >> 1;
    if (md >= x) {
        if (!delet(ls[k], l, md, x))    return delet(rs[k], md + 1, r, x);
        return true;
    }
    return delet(rs[k], md + 1, r, x);
}
bool cmp (pt x, pt y) 
{
    return x.x > y.x;
}
bool check ()
{
    sort(a + 1, a + n + 1, cmp);
    sort(b + 1, b + m + 1, cmp);
    int i = 1, j = 1;
    while (i <= n) {
        while (j <= m && b[j].x >= a[i].x)  insert(rt, 1, n + m, b[j].y), ++j;
        if (!delet(rt, 1, n + m, a[i].y))   return false;
        ++i;
    }
    return true;
}
void lsh ()
{
    int cnt = 0;
    for (int i = 1; i <= n; ++i)    t[++cnt] = a[i].x;
    for (int i = 1; i <= m; ++i)    t[++cnt] = b[i].x;
    sort(t + 1, t + cnt + 1);
    cnt = unique(t + 1, t + cnt + 1) - t - 1;
    for (int i = 1; i <= n; ++i)    a[i].x = lower_bound(t + 1, t + cnt + 1, a[i].x) - t;
    for (int i = 1; i <= m; ++i)    b[i].x = lower_bound(t + 1, t + cnt + 1, b[i].x) - t;
    cnt = 0;
    for (int i = 1; i <= n; ++i)    t[++cnt] = a[i].y;
    for (int i = 1; i <= m; ++i)    t[++cnt] = b[i].y;
    sort(t + 1, t + cnt + 1);
    cnt = unique(t + 1, t + cnt + 1) - t - 1;
    for (int i = 1; i <= n; ++i)    a[i].y = lower_bound(t + 1, t + cnt + 1, a[i].y) - t;
    for (int i = 1; i <= m; ++i)    b[i].y = lower_bound(t + 1, t + cnt + 1, b[i].y) - t;
}
int main ()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)    scanf("%d", &a[i].x);
    for (int i = 1; i <= n; ++i)    scanf("%d", &a[i].y);
    for (int i = 1; i <= m; ++i)    scanf("%d", &b[i].x);
    for (int i = 1; i <= m; ++i)    scanf("%d", &b[i].y);
    lsh();
    printf("%s\n", check() ? "Yes" : "No");
    return 0;
}

如有哪里讲得不是很明白或是有错误,欢迎指正
如您喜欢的话不妨点个赞收藏一下吧

标签:二分,return,int,线段,Wrapping,Chocolate,物品,盒子,ABC245E
From: https://www.cnblogs.com/Morning-Glory/p/17617680.html

相关文章

  • CF598E Chocolate Bar
    CF598EChocolateBar      一道简单的DP,虽然用搜索写的。我们用f(i,j,z)表示把X×Y的巧克力分成总大小为Z的小块所需最小代价。每次掰开的方式有两种,横着掰和竖着掰,故有两种转移。  #include<bits/stdc++.h>usingnamespacestd;intn,m,k,T;constintinf=......
  • (UVA)Big Chocolate
    BigChocolateMohammadhasrecentlyvisited Switzerland .Asheloveshisfriendsverymuch,hedecidedtobuysomechocolateforthem,butasthisfinechocolateisveryexpensive(YouknowMohammadisalittleBITstingy!),hecouldonlyaffordbuyingone......
  • CF1477F Nezzar and Chocolate Bars 题解
    题意:有一根长为\(1\)的巧克力,已经被切了\(m-1\)刀被分成\(m\)分,接下来每次在整根长度为\(1\)的巧克力上均匀随机一个点切一刀,求每一小段巧克力长度均小于一个给定值\(K\)需要的期望次数。引理:Irwin-Hall分布:对于\(n\)个在\([0,1]\)内均匀分布的实数随机变量,它们......
  • 写书写到一半,强迫症发作跑去给HotChocolate修bug
    前言这是写作《C#与.NET6开发从入门到实践》时的小故事,作为本书正式上市的宣传,在此分享给大家。正文.NET目前有两个比较成熟的GraphQL框架,其中一个是HotChocolate,在使......
  • [ABC245E] Wrapping Chocolate题解
    听说没人写,那就来一发。这种偏序问题大概率是要排个序的。将盒子和巧克力视为一个东西,\(c\)视为\(a\),\(d\)视为\(b\),放在一起以\(a\)为第一关键字,\(b\)为第二关键......
  • CF1801F - Another n-dimensional chocolate bar
    \[枉过路,经行处,寒水凄凉漱黄土\]\[相逢途,舟难住,雾凇封树冰刺骨\]\[虽未闻望地如何,亦誓要从渃河渡\]首先,考虑可能的\(b\)序列,对于\(b_i\),设\(p=\prod_{j\neqi}b_j\)......
  • E - Dividing Chocolate --- detecting next end point by double expand & half fold
    E-DividingChocolatehttps://atcoder.jp/contests/abc159/tasks/abc159_e 思路对于不大于k位置的查找,前一篇给出了基于前缀和的从左向右逐步查找方法https://ww......
  • E - Dividing Chocolate
    E-DividingChocolatehttps://atcoder.jp/contests/abc159/tasks/abc159_e 思路https://www.cnblogs.com/ycx-akioi/p/AtCoder-abc159.htmlCodehttps://atcoder.......
  • Installing Chocolatey
    InstallingChocolateyChocolateyinstallsinseconds...NOTE: NeedtoinstallaparticularversionofChocolatey?Proxy?Installtoadifferentlocation?Advance......
  • chocolatey安装和使用
    1.什么是ChocolateyChocolatey是一种软件管理解决方案,不同于您在Windows上体验过的任何解决方案。可以这样想-您使用一个小PowerShell创建一个软件部署包,然后您......