Heap 3214 LIS题解
生活随笔
收集整理的這篇文章主要介紹了
Heap 3214 LIS题解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
依據(jù)問題轉(zhuǎn)換成最長(zhǎng)不降子序列問題。
10^9的輸入數(shù)據(jù)計(jì)算起來還是挺花時(shí)間的。由于這里僅僅能使用O(nlgn)時(shí)間復(fù)雜度了。
只是證明是能夠算出10^9個(gè)數(shù)據(jù)的。
由于時(shí)間限制是5s.
#include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <limits.h> #include <stack> #include <queue> #include <set> #include <map> using namespace std;const int MAX_N = 20; vector<int> arr, a2; int N;inline int lSon(int rt) { return rt<<1|1; } inline int rSon(int rt) { return (rt<<1)+2; }void postOrder(int rt, int &v) {int l = lSon(rt), r = rSon(rt);if (l < N) postOrder(l, v);if (r < N) postOrder(r, ++v);a2.push_back(arr[rt]-v); }int biGetIndex(int low, int up, int v) {while (low <= up){int mid = low + ((up-low)>>1);if (v < a2[mid]) up = mid-1;else low = mid+1;}return low; }int LIS() {int j = 0;for (int i = 1; i < N; i++){if (a2[i] >= a2[j]) a2[++j] = a2[i];else{int id = biGetIndex(0, j, a2[i]);a2[id] = a2[i];}}return j+1; }int main() {int a;scanf("%d", &N);arr.clear(), a2.clear();while (scanf("%d", &a) != EOF){arr.push_back(a);}N = (int) arr.size();int v = 0;postOrder(0, v);int len = LIS();printf("%d\n", N-len);return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/lytwajue/p/6758611.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Heap 3214 LIS题解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SNS大负载系统解决方案研究
- 下一篇: poj2109 Power of Cry