hdu5489(2015合肥网络赛F题)
生活随笔
收集整理的這篇文章主要介紹了
hdu5489(2015合肥网络赛F题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自:http://blog.csdn.net/lwt36/article/details/48774103
題意:
給出一個數列,在其中刪除連續的L個數字,使得剩余的數字LIS最大,輸出此LIS。
思路:
nlogn的LIS+線段樹。
代碼:
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <set> #include <vector>using namespace std; #define pr(x) cout << #x << " = " << x << " " #define prln(x) cout << #x << " = " << x << endl const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n, L, a[N], all[N]; int f[N], g[N], h[N];int maxv[N << 2]; void update(int o, int v, int l, int r, int rt) {if(l == r) {maxv[rt] = max(maxv[rt], v);return;}int m = l + r >> 1;if(o <= m) update(o, v, l, m, rt << 1);else update(o, v, m + 1, r, rt << 1 | 1);maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]); }int query(int L, int R, int l, int r, int rt) {if(L <= l && r <= R) return maxv[rt];int m = l + r >> 1, ret = -INF;if(L <= m) ret = max(ret, query(L, R, l, m, rt << 1));if(R > m) ret = max(ret, query(L, R, m + 1, r, rt << 1 | 1));return ret; }int main() { #ifdef LOCALfreopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endifios_base::sync_with_stdio(0);int t; scanf("%d", &t);int kase = 0;while(t--) {scanf("%d%d", &n, &L);for(int i = 1; i <= n; ++i) {scanf("%d", a + i);all[i] = a[i];}sort(all + 1, all + 1 + n);int m = unique(all + 1, all + 1 + n) - all - 1;memset(h, 0x3f, sizeof h);for(int i = 1; i <= n; ++i) {int k = lower_bound(h + 1, h + 1 + n, a[i]) - h;f[i] = k;h[k] = a[i];}memset(h, 0x3f, sizeof h);for(int i = n; i; --i) {int k = lower_bound(h + 1, h + 1 + n, -a[i]) - h;g[i] = k;h[k] = -a[i];}int ans = 0;a[n + 1] = INF; g[n + 1] = 0;memset(maxv, 0, sizeof maxv);//delete [i-l,i-1] length:l [0,i-l-1]->maxvfor(int i = L + 1; i <= n + 1; ++i) {int o = lower_bound(all + 1, all + 1 + m, a[i]) - all;ans = max(ans, query(0, o - 1, 0, m, 1) + g[i]);o = lower_bound(all + 1, all + 1 + m, a[i - L]) - all; //插入一個新的if(i <= n) update(o, f[i - L], 0, m, 1);}printf("Case #%d: %d\n", ++kase, ans);}return 0; }總結
以上是生活随笔為你收集整理的hdu5489(2015合肥网络赛F题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu5491(2015合肥网络赛H题)
- 下一篇: poj2449(第k短路)