跳到正文

题解

题解:AT_abc366_f [ABC366F] Maximum Composition

OldDriverTree约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年8月12日
收录
2026年6月4日
更新
2026年11月8日
篇幅
约 2 分钟
获赞
6

版本

  1. v1当前

Solution

我们充分发扬人类智慧:

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

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

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

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

Code

C++
//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;
}
原文
存档评论12 条抓取于