題目鏈接:?[點擊打開鏈接](http://acm.hdu.edu.cn/showproblem.php?pid=5493)
題意:有n個人排隊,每個人都有一個獨一無二的身高,告訴你每個人的身高和他前面或者后面的比他高的人的個數(到底是前是后是未知的)。 要求你還原原來的隊列,并且字典序最小。
思路: 因為要求字典序最小, 我們可以先按照身高從小到大排序,假設當前到了第i高的人, 他前面或者后面有k個人, 那么他前面的所有人都比他矮, 比他高的還有n-i個人,那么假設他前面還有p個空位, 他就是第p+1個空位上的人, 那么怎么求p呢? ?因為要求字典序最小, 所以 p = min(k, n - i - k)。 為什么這樣是對的呢?每個人有兩個可能位置啊, 因為他之前的都比他矮, ?所以他無論在哪個位置都是可以的。 那么為了讓字典序最小, 就選擇一個較小的位置。 ? 當n - i - k < 0 時, 說明沒有多余空格, 那么無解。
為了加速算法, 二分第i個人的位置, 用樹狀數組判斷他前面有多少人以此確定空余的位置數。 ??
復雜度O(n*logn*logn)。
細節參見代碼:
~~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100000 + 5;
int T,n,m,bit[maxn],kase=0,ans[maxn];
struct node {
int v, num;
bool operator < (const node& rhs) const {
return v < rhs.v;
}
}a[maxn];
int sum(int x) {
int ans = 0;
while(x > 0) {
ans += bit[x];
x -= x & -x;
}
return ans;
}
void add(int x, int d) {
while(x <= n) {
bit[x] += d;
x += x & -x;
}
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
memset(bit, 0, sizeof(bit));
for(int i=1;i<=n;i++) {
scanf("%d%d",&a[i].v,&a[i].num);
}
sort(a+1,a+n+1);
bool ok = true;
for(int i=1;i<=n;i++) {
int k = min(a[i].num, n-i-a[i].num);
int l = 1, r = n, mid;
if(n - i - a[i].num < 0) { ok = false; break; }
++k;
while(r > l) {
mid = (r+l)/2;
if(mid - sum(mid) >= k) r = mid;
else l = mid + 1;
}
add(l, 1);
ans[l] = a[i].v;
}
printf("Case #%d:",++kase);
if(ok) {
for(int i=1;i<=n;i++) {
printf(" %d",ans[i]);
}
printf("\n");
}
else printf(" impossible\n");
}
return 0;
}
~~~
- 前言
- 1608 - Non-boring sequences(折半遞歸。。暫且這么叫吧)
- 11491 - Erasing and Winning(貪心)
- 1619 - Feel Good(高效算法-利用數據結構優化-優先隊列)
- hdu-4127 Flood-it!(IDA*算法)
- UESTC 1132 醬神賞花 (用數據結構優化DP)
- HDU 2874 Connections between cities(LCA離線算法)
- Codeforces Round #317 A. Lengthening Sticks(組合+容斥)
- HDU 3085 Nightmare Ⅱ(雙向BFS)
- HDU 5592 ZYB&#39;s Premutation(二分+樹狀數組)
- Codeforces Round #320 (Div. 1) C. Weakness and Poorness(三分)
- HDU 5212 Code(容斥)
- HDU 5596 GTW likes gt(multiset)
- FZU 2159 WuYou(貪心)
- HDU 3450 Counting Sequences(DP + 樹狀數組)
- HDU 5493 Queue(二分+樹狀數組)
- HDU 1166 敵兵布陣(線段樹版)
- HDU 1394 Minimum Inversion Number(樹狀數組||線段樹)
- HDU 2795 Billboard(線段樹)
- POJ 2828 Buy Tickets(樹狀數組)
- 《完全版線段樹》- NotOnlySuccess
- POJ 2886 Who Gets the Most Candies?(樹狀數組+二分)
- HDU 1698 Just a Hook(線段樹區間修改)
- POJ 3468 A Simple Problem with Integers(線段樹|區間加減&amp;&amp;區間求和)
- POJ 2528 Mayor&#39;s posters(線段樹區間修改+離散化)
- HDU 5606 tree(并查集)
- POJ 3734 Blocks(矩陣優化+DP)
- POJ 3233 Matrix Power Series(矩陣優化)
- HDU 5607 graph(矩陣優化+概率DP)
- POJ 2777 Count Color(線段樹區間修改+位運算)
- POJ 1436 Horizontally Visible Segments(線段樹區間修改)
- UVA 1513 - Movie collection(樹狀數組)
- UVA 1232 - SKYLINE(線段樹區間更新)
- 11525 - Permutation(二分+樹狀數組)
- 11402 - Ahoy, Pirates!(線段樹區間更新(標記重疊的處理))
- Educational Codeforces Round 6 E. New Year Tree(DFS序+線段樹)