HDU 2795 Billboard

Posted zaq19970105

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2795 Billboard相关的知识,希望对你有一定的参考价值。

题目链接:https://vjudge.net/problem/HDU-2795

题目大意:

  有一块长度为 H 宽度为 W 的广告牌,现在有 n 则广告要依次贴上去,第 i 则广告的长度为 1 宽度为 w[i],广告要尽量贴得高,其次尽量靠左,求每则广告位于第几行,贴不上就输出-1。

分析:

  如果广告牌的所有行剩余宽度的最大值大于等于 w[i],那么这个广告肯定能贴在某一行,可以通过二分的方法继续判断以缩小区间,没错,就是最值线段树。

代码如下:

技术图片
  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define pr(x) cout << #x << " = " << x << "  "
 15 #define prln(x) cout << #x << " = " << x << endl
 16  
 17 #define LOWBIT(x) ((x)&(-x))
 18  
 19 #define ALL(x) x.begin(),x.end()
 20 #define INS(x) inserter(x,x.begin())
 21  
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,inf,sizeof(a))
 24 #define msM(a) memset(a,-1,sizeof(a))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30  
 31 template<typename T1, typename T2>
 32 istream &operator>>(istream &in, pair<T1, T2> &p) {
 33     in >> p.first >> p.second;
 34     return in;
 35 }
 36  
 37 template<typename T>
 38 istream &operator>>(istream &in, vector<T> &v) {
 39     for (auto &x: v)
 40         in >> x;
 41     return in;
 42 }
 43  
 44 template<typename T1, typename T2>
 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 46     out << "[" << p.first << ", " << p.second << "]" << "\n";
 47     return out;
 48 }
 49  
 50 typedef long long LL;
 51 typedef unsigned long long uLL;
 52 typedef pair< double, double > PDD;
 53 typedef pair< int, int > PII;
 54 typedef set< int > SI;
 55 typedef vector< int > VI;
 56 typedef map< int, int > MII;
 57 typedef vector< LL > VL;
 58 typedef vector< VL > VVL;
 59 const double EPS = 1e-10;
 60 const int inf = 1e9 + 9;
 61 const LL mod = 1e9 + 7;
 62 const int maxN = 2e5 + 7;
 63 const LL ONE = 1;
 64 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
 65 const LL oddBits = 0x5555555555555555;
 66 
 67 LL H, W, n; 
 68 LL w[maxN];
 69 
 70 #define lson l , mid , rt << 1 
 71 #define rson mid + 1 , r , rt << 1 | 1
 72 
 73 struct SegmentTree{
 74     int st[maxN << 2];
 75     
 76     inline void pushUp(int rt) {
 77         st[rt] = max(st[rt << 1], st[rt << 1 | 1]);
 78     }
 79     
 80     inline void pushDown(int rt) { }
 81     
 82     inline void build(int l, int r, int rt) {
 83         if(l >= r) {
 84             st[rt] = W;
 85             return;
 86         }
 87         int mid = (l + r) >> 1;
 88         build(lson);
 89         build(rson);
 90         pushUp(rt);
 91     }
 92     
 93     // 查到某一行能容纳 x 就更新 
 94     inline LL queryAndUpdate(LL x, int l, int r, int rt) {
 95         if(st[rt] < x) return -1;
 96         if(l >= r) {
 97             st[rt] -= x;
 98             return r;
 99         }
100         LL ret = 0;
101         int mid = (l + r) >> 1;
102         if(st[rt << 1] >= x) ret = queryAndUpdate(x, lson); // 前面的行优先 
103         else if(st[rt << 1 | 1] >= x) ret = queryAndUpdate(x, rson);
104         
105         pushUp(rt);
106         return ret;
107     }
108 };
109 SegmentTree segTr;
110 
111 int main(){
112     INIT();
113     while(cin >> H >> W >> n) {
114         For(i, 1, n) cin >> w[i];
115         if(H >= n) H = n; // 行数大于 n 时,只有前 n 行有用 
116         segTr.build(1, H, 1);
117         For(i, 1, n) cout << segTr.queryAndUpdate(w[i], 1, H, 1) << endl;
118     }
119     return 0;
120 }
View Code

 

以上是关于HDU 2795 Billboard的主要内容,如果未能解决你的问题,请参考以下文章

hdu 2795 Billboard(线段树)

HDU 2795 Billboard(线段树)

hdu 2795 Billboard

HDU 2795 Billboard (线段树)

HDU 2795 Billboard 贴广告(线段树)

HDU 2795 - Billboard(线段树)