hdu 5493 Queue 线段树
来源:互联网 发布:淘宝优惠券app哪个好 编辑:程序博客网 时间:2024/05/16 12:27
题意:n个人排队,给出每个人的身高和,每个人只记得在他前面或后面比他高的人的数量,求一组字典序最小的排队方式。
首先将所有人按照身高从小到大排序,假设数组前面的人都已经合理的排好,那么对于当前的人来说,他的要求就是在他的前面或者后面能够有足够的空间安排比他高的人。于是设线段树当前节点记录该节点的子区间里面还有多少空余的空位,每次查询的时候去找到 当前的人前面(后面)比他高的人的数量 +1 这个位置,然后更新即可。而剩下的空位放的肯定是比他高的人,那么这种贪心策略就是对的。
如 *****++*****(*表示空格,+表示已被占了),第三个人记得高的的人的个数为4,那么他就可以在 ****0++***** 或者 *****++0**** ,即 0 这个位置。按照字典序最小取前者最优。假如碰到一种情况使得剩下的空位为标准,它的前面后面都没有足够的空位来满足“比他高的人的数量”,说明这种就是impossible
#include <bits/stdc++.h>#include <map>#include <set>#include <queue>#include <stack>#include <cmath>#include <time.h>#include <vector>#include <cstdio>#include <string>#include <iomanip>///cout << fixed << setprecision(13) << (double) x << endl;#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1#define ls rt << 1#define rs rt << 1 | 1#define pi acos(-1.0)#define eps 1e-8#define Mp(a, b) make_pair(a, b)#define asd puts("asdasdasdasdasdf");typedef long long ll;typedef pair <int, int> pl;//typedef __int64 LL;const int inf = 0x3f3f3f3f;const int N = 100010;struct node{ int l, r; int x;}tr[N<<2];struct pp{ int h, num;}a[N];int ans[N];int n;bool cmp( pp q, pp w ){ return q.h < w.h;}void pushup( int rt ){ tr[rt].x = tr[ls].x + tr[rs].x;}void build( int l, int r, int rt ){ tr[rt].l = l; tr[rt].r = r; if( l == r ) { tr[rt].x = 1; return; } int mid = ( l + r ) >> 1; build( lson ); build( rson ); pushup( rt );}int query1( int rt, int val ){ if( tr[rt].l == tr[rt].r ) return tr[rt].l; if( tr[ls].x >= val ) return query1( ls, val ); else return query1( rs, val-tr[ls].x );}int query2( int rt, int val ){ if( tr[rt].l == tr[rt].r ) return tr[rt].l; if( tr[rs].x >= val ) return query2( rs, val ); else return query2( ls, val-tr[rs].x );}void update( int rt, int pos ){ if( tr[rt].l == tr[rt].r ) { tr[rt].x = 0; return; } int mid = ( tr[rt].l + tr[rt].r ) >> 1; if( pos <= mid ) update( ls, pos ); else update( rs, pos ); pushup( rt );}int main(){ int tot; scanf("%d", &tot); for( int ca = 1; ca <= tot; ++ca ) { scanf("%d", &n); for( int i = 1; i <= n; ++i ) { scanf("%d%d", &a[i].h, &a[i].num); } sort( a+1, a+1+n, cmp ); build( 1, n, 1 ); bool OK = 1; for( int i = 1; i <= n; ++i ) { int pos1 = inf, pos2 = inf; if( tr[1].x >= a[i].num+1 ) { pos1 = query1( 1, a[i].num+1 ); pos2 = query2( 1, a[i].num+1 ); } if( pos1 == inf && pos2 == inf ) { OK = 0; break; } if( pos1 <= pos2 ) { ans[pos1] = a[i].h; update( 1, pos1 ); } else { ans[pos2] = a[i].h; update( 1, pos2 ); } } printf("Case #%d: ", ca); if( OK ) { for( int i = 1; i <= n; ++i ) { printf("%d%c", ans[i], i == n ? '\n' : ' '); } } else { printf("impossible\n"); } } return 0;}
0 0
- hdu 5493 Queue 线段树
- hdu 5493 Queue 线段树
- HDU 5493 Queue(线段树)
- HDU 5493 Queue 线段树
- hdu 5493 Queue 线段树
- hdu 5493 Queue 贪心+线段树
- hdu 5493 Queue(线段树)
- hdu 5493 Queue(线段树)
- hdu 5493 Queue(逆序对,线段树)
- hdu 5493 Queue 线段树加二分
- HDU-5493---Queue (线段树)
- HDU 5493 Queue (线段树) 2015合肥网络赛
- HDU 5493 Queue(线段树啊 单点更新)
- HDU 5493 Queue (线段树)2015合肥赛区网络赛
- HDU 5493 Queue (线段树逐个插入)
- HDU 5493 Queue(线段树+离线处理)
- hdu 4441 Queue Sequence (splay + 线段树)
- 【线段树】HDU 5493 Queue (2015 ACM/ICPC Asia Regional Hefei Online)
- 在C语言中,double、long、unsigned、int、char类型数据所占字节数
- 【bzoj3483】【SGU505】【Prefixes and suffixes】【字符串hash】
- 个人学习笔记---inode节点详解
- VC绘图/游戏简易教程--5:数学知识在绘图中的运用
- leetCode 99: Recover Binary Search Tree
- hdu 5493 Queue 线段树
- 如何学好C语言
- 三个水杯 21 (bfs 转化) 好题
- OCP-V13-006
- C++ string 类常用函数
- C语言宏定义使用技巧
- 从面向过程到面向对象
- HTML使用canvas载入图片
- OCP-V12-007