题解:AT_abc366_f [ABC366F] Maximum Composition

· · 题解

Solution

我们充分发扬人类智慧:

将所有一次函数按 A^2+B 从大到小排序。

根据数学直觉,答案中的一次函数在数组中肯定不会太靠后。

所以我们只取前 25 个点跑状压 \text{DP}。

这样速度快的飞起,在 N=200000 时都可以在 1.5s 内卡过。

Code

//when you use vector or deque,pay attention to the size of it.
//by OldDirverTree
#include<bits/stdc++.h>
#include<atcoder/all>
#define P pair<int,int>
#define int long long
#define mid (l+r>>1)
using namespace std;
using namespace atcoder;
const int N=2e5+1;
int n,m,f[1<<25];

int read() {
    int x=0; bool f=true; char c=0;
    while (!isdigit(c) ) f&=(c!='-'),c=getchar();
    while (isdigit(c) ) x=(x<<3)+(x<<1)+(c&15),c=getchar();
    return f?x:-x;
}
struct node
{
    int x,y;
    bool operator <(node o)const {
        return x*x+y>o.x*o.x+o.y;
    }
}a[N];

int solve(int S)
{
    if (~f[S]) return f[S];
    int cnt=__builtin_popcount(S);
    if (cnt==m) return f[S]=1;
    for (int i=0;i<n;i++) if (!(S>>i&1) )
    f[S]=max(f[S],a[i].x*solve(S|(1<<i) )+a[i].y);
    return f[S];
}
main()
{
    n=read(),m=read();
    memset(f,-1,sizeof f);
    for (int i=0;i<n;i++)
    a[i]={read(),read()};
    sort(a,a+n),n=min(n,25ll);
    printf("%lld",solve(0) );
    return 0;
}