这个代码似乎常数很小(?)
查看原帖
这个代码似乎常数很小(?)
357440
NullNone楼主2023/11/10 12:17

写的比题解区的代码恶心多了,结果效率还行?

#include <iostream>
using namespace std;
const int MAXN = 1e6 + 5;
int n, s, que[MAXN], qs, qe = -1, t[MAXN], qf;
unsigned dp[MAXN], res;
inline void read(int &var){
    var = 0;
    char reg;
    do{ reg = getchar(); }while(reg < '0' || reg > '9');
    do{
        var = (var << 1) + (var << 3) + (reg ^ 48);
        reg = getchar();
    }while('0' <= reg && reg <= '9');
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    read(n); read(s);
    t[0] = -1;
    res = 3e9 + 1e6;
    for(int i = 1; i <= n; ++i){
        read(t[i]);
        t[i] = max(t[i], t[i - 1] + 1);
        while(qs <= qe){
            qf = que[qe];
            if(dp[qf - 1] + ((i - qf) << 1) > dp[i - 1])
                --qe;
            else
                break;
        }
        que[++qe] = i;
        res += t[i] - t[i - 1] + 1;
        while(qs <= qe){
            qf = que[qs];
            if(dp[qf - 1] + i - qf <= t[i]){
                ++qs;
                res = min(res, unsigned(t[i] + i - qf));
            }else
                break;
        }
        qf = que[qs];
        dp[i] = res;
        if(qs <= qe){
            qf = que[qs];
            dp[i] = min(dp[i], dp[qf - 1] + ((i - qf) << 1));
        }
        dp[i] += s << 1;
    }
    cout << dp[n] << endl;
    return 0;
}
2023/11/10 12:17
加载中...