1. 程式人生 > 其它 >NOIP 模擬 $85\; \rm 盡梨了$

NOIP 模擬 $85\; \rm 盡梨了$

題解

題解 \(by\;zj\varphi\)

每次多選一個商場,所用時間在 \(a_i\) 不為 \(0\) 的情況下至少乘二。

所以答案不會很大,\(dp\) 陣列只需開 \(log_2n\) 即可。

退一下式子,假設在當前已經過去了 \(t\) 時間,該前往下一個商場了,那麼有:

\[a_j\times (a_i\times (t+1)+b_i+(t+1)+1)+b_j+a_i\times (t+1)+b_i+(t+1)+1\le a_i\times (a_j\times (t+1)+b_j+(t+1)+1)+b_i+a_j\times (t+1)+b_j+(t+1)+1 \]

解得 \(\frac{b_i+1}{a_i}\le \frac{b_j+1}{a_j}\)

那麼先按 \(\frac{b_i+1}{a_i}\le \frac{b_j+1}{a_j}\) 從下到大排序,然後再 \(dp\),方程為 \(f_{i,j}=\min(f_{i,j},f_{i-1,j-1}+time_i)\)

\(time_i\) 表示第 \(i\) 個商場所用的時間。

剩下的 \(a_i\)\(0\) 的放到最後列舉新增進答案。

Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    #define debug1(x) std::cerr << #x"=" << x << ' '
    #define debug2(x) std::cerr << #x"=" << x << std::endl
    #define Debug(x) assert(x)
    struct nanfeng_stream{
        template<typename T>inline nanfeng_stream &operator>>(T &x) {
            bool f=false;x=0;char ch=gc();
            while(!isdigit(ch)) f|=ch=='-',ch=gc();
            while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
            return x=f?-x:x,*this;
        }
    }cin;
}
using IO::cin;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    using ll=long long;
    static const int N=2e5+7;
    int f[50],n,T,ans,pos=1e9,sum;
    struct Seg{int a,b;}s[N];
    inline int main() {
        FI=freopen("eriri.in","r",stdin);
        FO=freopen("eriri.out","w",stdout);
        cin >> n >> T;
        for (ri i(1);i<=n;pd(i)) cin >> s[i].a >> s[i].b;
        std::sort(s+1,s+n+1,[](const Seg &s1,const Seg &s2) {return 1ll*(s1.b+1)*s2.a<1ll*s1.a*(s2.b+1);});
        memset(f,0x3f,sizeof(f));
        f[0]=0;
        for (ri i(1);i<=n;pd(i)) {
            if (!s[i].a) {pos=i;break;}
            for (ri j(cmin(29,i-1));~j;bq(j)) {
                ll t=1ll*(s[i].a+1)*(f[j]+1)+s[i].b;
                if (t>T) continue;
                f[j+1]=cmin(f[j+1],(int)t);
            }
        }
        for (ri i(cmin(pos-1,30));i;bq(i)) if (f[i]<=T) {ans=i;break;}
        if (pos<=n) {
            std::sort(s+pos,s+n+1,[](const Seg &s1,const Seg &s2) {return s1.b<s2.b;});
            for (ri i(pos);i<=n;pd(i)) {
                sum+=s[i].b+1;
                if (sum>T) break;
                for (ri j(cmin(pos-1,30));~j;bq(j)) 
                    if (sum+f[j]<=T) {ans=cmax(ans,j+i-pos+1);break;}
            }
        }
        printf("%d\n",ans);
        return 0;
    }
}
int main() {return nanfeng::main();}