【BZOJ 2802】 2802: [Poi2012]Warehouse Store (贪心)
2802: [Poi2012]Warehouse Store
Description
有一家专卖一种商品的店,考虑连续的n天。
第i天上午会进货Ai件商品,中午的时候会有顾客需要购买Bi件商品,可以选择满足顾客的要求,或是无视掉他。
如果要满足顾客的需求,就必须要有足够的库存。问最多能够满足多少个顾客的需求。Input
第一行一个正整数n (n<=250,000)。
第二行n个整数A1,A2,...An (0<=Ai<=10^9)。
第三行n个整数B1,B2,...Bn (0<=Bi<=10^9)。Output
第一行一个正整数k,表示最多能满足k个顾客的需求。
第二行k个依次递增的正整数X1,X2,...,Xk,表示在第X1,X2,...,Xk天分别满足顾客的需求。Sample Input
6
2 2 1 2 1 0
1 2 2 3 4 4Sample Output
3
1 2 4HINT
Source
【分析】
可爱的贪心题。
【并没有秒,并且表示想了很久,并且表示样例调了挺久】
【幸好1A】【好吧学不会大颓果的来者不拒思想ORZ】
先说我的方法:
每次选最小的bi取,然后库存减掉,树状数组维护前缀和。
减库存的时候从当前位置往前减,减到0为止,不过这样会很慢,我用个链表维护,如果是0就直接跳过去了。
【实测不用链表真心TLE。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
#define Maxn 250010
#define LL long long int a[Maxn],b[Maxn],c[Maxn];
LL d[Maxn];
int lt[Maxn],nt[Maxn]; bool cmp(int x,int y)
{
if(b[x]==b[y]) return x>y;
return b[x]<b[y];
} int n; void add(int x,int y)
{
for(int i=x;i<=n;i+=i&(-i))
d[i]+=y;
} LL query(int x)
{
LL ans=;
for(int i=x;i>=;i-=i&(-i))
ans+=d[i];
return ans;
} int op[Maxn]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++) scanf("%d",&b[i]);
for(int i=;i<=n;i++) c[i]=i;
memset(d,,sizeof(d));
for(int i=;i<=n;i++) add(i,a[i]);
for(int i=;i<=n;i++) nt[i]=i+;
for(int i=;i<=n;i++) lt[i]=i-;
for(int i=;i<=n;i++) if(a[i]==)
{
lt[nt[i]]=lt[i];
nt[lt[i]]=nt[i];
}
sort(c+,c++n,cmp);
op[]=;
for(int i=;i<=n;i++)
{
if(query(c[i])>=b[c[i]])
{
op[++op[]]=c[i];
int nw=c[i];
while(nw&&a[nw]<=b[c[i]])
{
add(nw,-a[nw]);
b[c[i]]-=a[nw];
a[nw]=;
lt[nt[nw]]=lt[nw];
nt[lt[nw]]=nt[nw];
nw=lt[nw];
nw--;
}
if(b[c[i]])
{
a[nw]-=b[c[i]];
add(nw,-b[c[i]]);
}
}
}
sort(op+,op++op[]);
printf("%d\n",op[]);
for(int i=;i<=op[];i++) printf("%d ",op[i]);
printf("\n");
return ;
}
方法二:【来者不拒】
能选的先选,选了的bi用一个最大堆维护,如果不行就把最大的bi弄出来让现在的满足。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 250010
#define LL long long struct node
{
int x,id;
friend bool operator < (node x,node y)
{
return x.x<y.x;
}
}; priority_queue<node > q; int a[Maxn],b[Maxn];
bool mark[Maxn]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++) scanf("%d",&b[i]);
LL now=;
int cnt=;
while(!q.empty()) q.pop();
memset(mark,,sizeof(mark));
for(int i=;i<=n;i++)
{
now+=a[i];
if(now>=b[i])
{
mark[i]=;
cnt++;
q.push((node){b[i],i});
now-=b[i];
}
else if(!q.empty()&&q.top().x>b[i])
{
now+=q.top().x-b[i];
mark[q.top().id]=;
mark[i]=;
q.pop();
q.push((node){b[i],i});
}
}
printf("%d\n",cnt);
for(int i=;i<=n;i++) if(mark[i])
printf("%d ",i);printf("\n");
return ;
}
2017-01-21 10:47:36
【BZOJ 2802】 2802: [Poi2012]Warehouse Store (贪心)的更多相关文章
- bzoj2802 [Poi2012]Warehouse Store 贪心+堆
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2802 题解 我一开始想到了一个比较麻烦的做法. 把每一天按照 \(b_i\) 从小到大排序,\ ...
- 【bzoj2802】[Poi2012]Warehouse Store 贪心+堆
题目描述 有一家专卖一种商品的店,考虑连续的n天.第i天上午会进货Ai件商品,中午的时候会有顾客需要购买Bi件商品,可以选择满足顾客的要求,或是无视掉他.如果要满足顾客的需求,就必须要有足够的库存.问 ...
- bzoj 2802 [Poi2012]Warehouse Store STL
[Poi2012]Warehouse Store Time Limit: 10 Sec Memory Limit: 64 MBSec Special JudgeSubmit: 621 Solve ...
- BZOJ2802: [Poi2012]Warehouse Store
2802: [Poi2012]Warehouse Store Time Limit: 10 Sec Memory Limit: 64 MBSec Special JudgeSubmit: 121 ...
- [bzoj2802][Poi2012]Warehouse Store_贪心_堆
Warehouse Store bzoj-2802 Poi-2012 题目大意:一家商店的连续n天内,每一天会进货$a_i$个,有且只有一个客人回来买$b_i$个,问至多满足多少人. 注释:$1\le ...
- 【BZOJ】2802: [Poi2012]Warehouse Store(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=2802 自己yy了一下... 每一次如果够那么就买. 如果不够,考虑之前买过的,如果之前买过的比当前花 ...
- BZOJ2802 [Poi2012]Warehouse Store 【贪心】
题目链接 BZOJ2802 题解 这样的问题通常逆序贪心 每个\(A[i]\)只能用来满足后面的\(B[i]\) 就用当前\(A[i]\)不断提供给最小的\(B[i]\)即可 用一个堆维护 #incl ...
- BZOJ2802——[Poi2012]Warehouse Store
1.题目巨短,自己看看吧 2.分析:这道题,想了半天dp还是想不到,最后看题解发现是个贪心的思想,我们维护一个堆,如果这个人不能加入就把他和堆上最大的进行比较,然后搞搞就行了 #include < ...
- BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心
BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心 Description 有一家专卖一种商品的店,考虑连续的n天. 第i天上午会进货Ai件商品,中午的时候会有顾客需要购买 ...
随机推荐
- bzoj 2956: 模积和 ——数论
Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...
- 正则表达式实现将html文本转换为纯文本格式(将html字符串转换为纯文本方法)
Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase); string strOutput = regex. ...
- 全面了解Nginx主要应用场景(数漫江湖)
前言 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流 N ...
- CursorFileManager对cursor文件的读写
public class CursorFileManager implements CursorManager{public void write(String key, LongCursor cur ...
- 9.1docker容器 跨主机连接
open vswitch 实现跨主机容器连接 准备条件 将本地的网卡 与新建的网桥建立连接 配置 docker 启动项 weave实现跨主机容器连接 null
- 动态替换Linux核心函数的原理和实现
转载:https://www.ibm.com/developerworks/cn/linux/l-knldebug/ 动态替换Linux核心函数的原理和实现 在调试Linux核心模块时,有时需要能够实 ...
- SourceTree 过期,注册导入许可证
参考这里:SourceTree过期,需要注册导入 SourceTree License 许可证 很详细 补充: 如果在 SourceTree 软件里注册失败,可以在网页注册. 如果其他邮箱不支持,可以 ...
- java===java基础学习(5)---文件读取,写入操作
文件的写入读取有很多方法,今天学到的是Scanner和PrintWriter 文件读取 Scanner in = new Scanner(Paths.get("file.txt") ...
- linux===Ubuntu修改设备名称
step1 sudo vim /etc/hostname 修改你需要的,保存退出 step2 sudo vim /etc/hosts 修改你需要的,保存退出 step3 reboot
- mysql 5.6在gtid复制模式下复制错误,如何跳过??
mysql 5.6在gtid复制模式下复制错误,如何跳过?? http://www.xuchanggang.cn/archives/918.html