BZOJ 3703: 昊昊的壮举之造福社会
2024-08-30 12:07:16
搜索,剪枝
首先可以二分答案迭代加深,假设要买 $p$ 台
那么肯定卖价格最小的 $p$ 台
再来个 $A*$ ,设搜到当前情况时,有 $waste$ 的钱一定要被浪费(其实就是某些学校剩下的钱连最便宜的都买不起)
设最便宜的 $p$ 台电脑总价值为 $sum$ ,所有学校的总钱数为 $S$,那么我们最多浪费 $S-tot$,如果 $waste>S-tot$ 就直接返回
但是显然还是不够
看看数据发现相同价格的电脑和相同初始钱数的学校数量很多
不妨使搜索时保证,对于相同价格的电脑,购买的学校的初始钱数单调不增,显然这样不会影响搜索时的正确性
然后就可以跑过了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=5e5+;
int n,m,mon[N],cst[N],ans;
int S,sum[N],Mx,Las[N];
bool GG;
void dfs(int pos,int waste)
{
if(waste>Mx) return;
if(!pos) { GG=; return; }
int R=Las[cst[pos]];
for(int i=R;i;i--)
{
if(cst[pos]>mon[i]) continue;
mon[i]-=cst[pos]; Las[cst[pos]]=i;
dfs(pos-,waste+ (mon[i]<cst[])*mon[i] );
mon[i]+=cst[pos];
if(GG) return;//记得先还原mon再返回
}
Las[cst[pos]]=R;
}
bool check(int p)
{
if(sum[p]>S||cst[p]>mon[m]) return ;
GG=; Mx=S-sum[p];
for(int i=;i<=n;i++) Las[cst[i]]=m;//每次都要初始化Las
dfs(p,); return GG;
}
int main()
{
m=read();
for(int i=;i<=m;i++) mon[i]=read(),S+=mon[i];
n=read();
for(int i=;i<=n;i++) cst[i]=read();
sort(mon+,mon+m+); sort(cst+,cst+n+);
for(int i=;i<=n;i++) sum[i]=sum[i-]+cst[i];
int L=,R=n;
while(L<=R)
{
int mid=L+R>>;
if(check(mid)) L=mid+,ans=mid;
else R=mid-;
}
printf("%d\n",ans);
return ;
}
最新文章
- 计算机程序的思维逻辑 (31) - 剖析Arrays
- Understanding the Uncertain Geographic Context Problem
- Discuz API的延伸
- hdu Oil Deposits
- 使用Nginx在自己的电脑上实现负载均衡
- CODEVS1380 没有上司的舞会 (树形DP)
- c# 获取 webbrowser 完整 cookie
- Oracle默认的用户名和密码
- JS实现精确加减乘除
- Function对象
- 改ucosii的中断禁止和恢复代码,这是一个荒谬的错误【 mrs msr】
- 合并多段zip文件并解压缩
- Qt Creator+MinGW+boost特殊函数的使用示例
- Timewarp 一种生成当中帧技术,异步时间扭曲(Asynchronous Timewarp)
- Leetcode_27_Remove Element
- 计算机网络相关:应用层协议(一):DNS
- 『素数 Prime判定和线性欧拉筛法 The sieve of Euler』
- Mybatis源码解析 - mapper代理对象的生成,你有想过吗
- Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
- ARCSDE直连Oracle时出现错误Failed to connect to the specified server. Underlying DBMS error[ORA-12154: TNS:could not resolve the connect identifier specified. No extended error]