传送门

搜索,剪枝

首先可以二分答案迭代加深,假设要买 $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 ;
}

BZOJ 3703: 昊昊的壮举之造福社会的更多相关文章

  1. bzoj 3704 昊昊的机油之GRST 贪心dp,思维

    昊昊的机油之GRST Time Limit: 10 Sec  Memory Limit: 1024 MBSubmit: 80  Solved: 33[Submit][Status][Discuss] ...

  2. UESTC 1256 昊昊爱运动 Map

    昊昊爱运动 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 昊昊喜欢运动 他N天 ...

  3. CDOJ 1259 昊昊爱运动 II 线段树+bitset

    昊昊爱运动 II 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r ...

  4. cdoj 1256 昊昊爱运动 预处理/前缀和

    昊昊爱运动 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1256 Descr ...

  5. CDOJ 1259 昊昊爱运动 II bitset+线段树

    题目链接 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r天参加了 ...

  6. cdojQ - 昊昊爱运动 II

    地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: Q - 昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others) ...

  7. UESTC-1259 昊昊爱运动 II

    昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)     昊昊喜 ...

  8. cdoj 1256 昊昊爱运动 预处理

    昊昊爱运动 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 昊昊喜欢运动 他NN ...

  9. bzoj 3704 昊昊的机油之GRST - 贪心

    题目传送门 传送门 题目大意 给定一个数组$a$和数组$b$,每次操作可以选择$a$的一个子区间将其中的数在模4意义下加1,问把$a$变成$b$的最少操作次数. 首先求$b - a$,再差分,令这个数 ...

随机推荐

  1. sqli-5&6

    第五关 Double Injection - Single Quotes - String (双注入GET单引号字符型注入) 1.发现前几关的方法都不能用了,要么报错(没有其他有关信息.要么什么也不出 ...

  2. UNIX网络编程总结四

    socket: 为了执行网络I/O,一个进程做的第一件事就是调用socket函数. family指明协议族,type指明类型,除非在原始套接口,protocol一般为0,并非所有的family,typ ...

  3. proxyTable-后端代理-跨域请求数据

    config >>> index.js  配置 proxyTable: { '/api': { target:'https://api.jisuapi.com', // 你请求的第三 ...

  4. NodeJS使用puppeteer进行截图

    const puppeteer = require('/home/ordinaryUser_2/automation/NodeJS/node/lib/node_global/lib/node_modu ...

  5. 2017 ICPC HongKong B:Black and White(扫描线+线段树)

    题目描述 Consider a square map with N × N cells. We indicate the coordinate of a cell by (i, j), where 1 ...

  6. sublime text 3 Package Control无法下载插件

    Package Control无法下载插件,发现是因为被墙了. ubuntu18.04 下使用privoxy设置全局参见:https://www.cnblogs.com/linjunfu/p/1101 ...

  7. spring+JdbcTemplate简单使用(一)

    目录 @ 1. 环境配置 maven(项目管理) idea(编译器) jdk1.8(Java环境) MySQL5.6(MySQL数据库) 2. 创建项目 在 idea 中创建普通的 maven 项目 ...

  8. ZOJ 3822 ( 2014牡丹江区域赛D题) (概率dp)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 题意:每天往n*m的棋盘上放一颗棋子,求多少天能将棋盘的每行每列都至少有 ...

  9. LLDB动态调试

  10. save change is not permitted

    https://support.microsoft.com/en-my/help/956176/error-message-when-you-try-to-save-a-table-in-sql-se ...