1. 程式人生 > >Luogu 2827 [NOIP2016] 蚯蚓

Luogu 2827 [NOIP2016] 蚯蚓

變量 class bool cbe clas || ont src def

原來真的是按題意模擬啊,還以為有高能的算法可以直接算每個$t$的值。

考慮到先切的蚯蚓一定比後切的蚯蚓長,於是可以弄三個隊列分別存放原來的序列和兩個切開後的序列,每次取出三個隊頭的最大值進行擴展。

考慮到每秒鐘除了取出來的隊頭其他的長度都會增加$q$,那麽我們可以寫一個全局變量$tag$標記現在進行了幾輪,然後每一次進隊的時候反向減去這一個$tag$就好了。

時間復雜度$O(nlogn)$。

$stl$的$queue$開了$O2$之後就很快了

Code:

技術分享圖片
#include <cstdio>
#include <cstring>
#include 
<queue> #include <algorithm> using namespace std; typedef double db; const int N = 7e6 + 5; const int inf = 1 << 30; int n, m, q, t, a[N * 3]; db p; queue <int> Q[3]; inline void read(int &X) { X = 0; char ch = 0; int op = 1; for(; ch >
9|| ch < 0; ch = getchar()) if(ch == -) op = -1; for(; ch >= 0 && ch <= 9; ch = getchar()) X = (X << 3) + (X << 1) + ch - 48; X *= op; } bool cmp(const int x, const int y) { return x > y; } inline int bet(int x, int y, int
rx, int ry) { return rx > ry ? x : y; } inline int getQ(int now) { if(Q[now].empty()) return -inf; else return Q[now].front(); } inline int getMax() { int res[3]; for(int i = 0; i < 3; i++) res[i] = getQ(i); return bet(bet(0, 1, res[0], res[1]), 2, res[bet(0, 1, res[0], res[1])], res[2]); } int main() { int u, v; read(n), read(m), read(q), read(u), read(v), read(t); p = (db)u / v; for(int i = 1; i <= n; i++) read(a[i]); sort(a + 1, a + 1 + n, cmp); for(int i = 1; i <= n; i++) Q[0].push(a[i]); int tag = 0; for(int i = 1; i <= m; i++) { int now = getMax(); int z = Q[now].front() + tag; Q[now].pop(); if(i % t == 0) printf("%d ", z); int x = (int)z * p, y = z - x; tag += q; x -= tag, y -= tag; Q[1].push(x), Q[2].push(y); } printf("\n"); int len = 0; for(int i = 0; i < 3; i++) for(;!Q[i].empty(); Q[i].pop()) a[++len] = Q[i].front(); sort(a + 1, a + 1 + len, cmp); /* for(int i = 1; i <= len; i++) printf("%d ", a[i]); printf("\n"); */ int cnt = (n + m) / t; for(int i = 1; i <= cnt; i++) printf("%d ", a[i * t] + tag); printf("\n"); return 0; }
View Code

Luogu 2827 [NOIP2016] 蚯蚓