Find the answer

Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0

Problem Description

Given a sequence of n integers called W and an integer m. For each i (1 <= i <= n), you can choose some elements W**k (1 <= k < i), and change them to zero to make ∑i**j=1W**j<=m. So what's the minimum number of chosen elements to meet the requirements above?.

Input

The first line contains an integer Q --- the number of test cases.

For each test case:

The first line contains two integers n and m --- n represents the number of elemens in sequence W and m is as described above.

The second line contains n integers, which means the sequence W.

1 <= Q <= 15

1 <= n <= 2*105

1 <= m <= 109

For each i, 1 <= W**i <= m

Output

For each test case, you should output n integers in one line: i-th integer means the minimum number of chosen elements W**k (1 <= k < i), and change them to zero to make ∑i**j=1W**j<=m.

Sample Input

2
7 15
1 2 3 4 5 6 7
5 100
80 40 40 40 60

Sample Output

0 0 0 0 0 2 3
0 1 1 2 3

题意

自己读题,几句话很难说清楚

转化一下就是将最少的数变成0,并且自己不能选,使\(\sum_{j=1}^{i} \leq m\),输出最小的次数。

题解

贪心一下,取最大的几个。

离散+权值线段树就成。

代码

#include<bits/stdc++.h>
#define int long long
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define MAXN 200010
struct sgt
{
int val,p;
int l,r;
} f[MAXN<<2]; int wh[MAXN];
int a[MAXN];
pair <int,int> pt[MAXN];
int n;
int m; void build(int x,int l,int r)
{
f[x].l = l;
f[x].r = r;
f[x].val = f[x].p = 0;
if (l == r) return;
build(x<<1,l,(l+r)>>1);
build(x<<1|1,((l+r)>>1)+1,r);
} void add(int x,int pos,int val)
{
f[x].val += val;
f[x].p ++;
if (f[x].l == pos && f[x].r == pos) return;
if (pos > f[x<<1].r) add(x<<1|1,pos,val);
else add(x<<1,pos,val);
} int query(int x,int val)
{
if (f[x].l == f[x].r)
if (f[x].val == val) return f[x].p;
else return 0;
if (f[x<<1].val >= val) return query(x<<1,val);
return f[x<<1].p + query(x<<1|1,val - f[x<<1].val);
} signed main()
{
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
memset(f,0,sizeof(f));
build(1,1,n);
for (int i=1; i<=n; i++)
scanf("%d",a+i),pt[i].first = a[i], pt[i].second = i;
sort(pt+1,pt+n+1);
for (int i=1; i<=n; i++)
wh[pt[i].second] = i;
int tot = 0;
for (int i=1; i<=n; i++)
{
tot += a[i];
if (tot <= m) printf("0 ");
if (tot > m) printf("%d ",i-query(1,m-a[i])-1);
add(1,wh[i],a[i]);
}
puts("");
}
}

最新文章

  1. tomcat linux下的部署安装
  2. Greenplum查询计划分析
  3. CentOS 6忘记密码解决办法,root和普通用户均可
  4. Git - 使用指南
  5. HDU3177 贪心
  6. VMware虚拟机固定IP后克隆出现无法访问网卡问题
  7. 两次fopen不同的文件返回相同的FILE* 地址
  8. JAVA正则忽略大小写
  9. HTML5之选择上传图片文件
  10. Java获取.properties配置文件某一项value根据key值
  11. JAVA的字符编码及问题
  12. iOS下的界面布局利器-MyLayout布局框架
  13. Effective Java 第三版——18. 组合优于继承
  14. MySQL--pt-osc工具学习
  15. 初识gauge自动化测试框架(二)
  16. python对Excel表格操作
  17. 关于window.localtion的用法几点总结
  18. Linux:文件系统层次结构标准(Filesystem Hierarchy Standard)
  19. aerospike数据库配置
  20. 微信小程序silk格式转码成mp3格式

热门文章

  1. 快速生成500W测试数据库
  2. Mac OS X 11中的/usr/bin 的“Operation not permitted”
  3. springboot笔记-thymeleaf
  4. 洛谷 P1417 烹调方案 题解
  5. ubuntu 设置sudo 免密码
  6. 微信内置浏览器不支持 onclick 如何解决?(原因是因为内面中的内容或者标签大部分是动态生成的)
  7. js获取两个经纬度之间的角度(0度-360度)
  8. BFC的概念、BFC触发方式、BFC作用介绍
  9. DSP学习资料:基于6U VPX的 XC7VX690T+C6678的双FMC接口雷达通信处理板
  10. Jmeter线程组间传递参数