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("");
}
}

2019 Multi-University Training Contest 3 T7 Find the answer的更多相关文章

  1. 2019 Nowcoder Multi-University Training Contest 4 E Explorer

    线段树分治. 把size看成时间,相当于时间 $l$ 加入这条边,时间 $r+1$ 删除这条边. 注意把左右端点的关系. #include <bits/stdc++.h> ; int X[ ...

  2. 2019 Nowcoder Multi-University Training Contest 1 H-XOR

    由于每个元素贡献是线性的,那么等价于求每个元素出现在多少个异或和为$0$的子集内.因为是任意元素可以去异或,那么自然想到线性基.先对整个集合A求一遍线性基,设为$R$,假设$R$中元素个数为$r$,那 ...

  3. 2019 Multi-University Training Contest 8

    2019 Multi-University Training Contest 8 C. Acesrc and Good Numbers 题意 \(f(d,n)\) 表示 1 到 n 中,d 出现的次数 ...

  4. 2019 Multi-University Training Contest 7

    2019 Multi-University Training Contest 7 A. A + B = C 题意 给出 \(a,b,c\) 解方程 \(a10^x+b10^y=c10^z\). tri ...

  5. 2019 Multi-University Training Contest 1

    2019 Multi-University Training Contest 1 A. Blank upsolved by F0_0H 题意 给序列染色,使得 \([l_i,r_i]\) 区间内恰出现 ...

  6. 2019 Multi-University Training Contest 2

    2019 Multi-University Training Contest 2 A. Another Chess Problem B. Beauty Of Unimodal Sequence 题意 ...

  7. 2019 Multi-University Training Contest 5

    2019 Multi-University Training Contest 5 A. fraction upsolved 题意 输入 \(x,p\),输出最小的 \(b\) 使得 \(bx\%p&l ...

  8. HDU校赛 | 2019 Multi-University Training Contest 6

    2019 Multi-University Training Contest 6 http://acm.hdu.edu.cn/contests/contest_show.php?cid=853 100 ...

  9. HDU校赛 | 2019 Multi-University Training Contest 5

    2019 Multi-University Training Contest 5 http://acm.hdu.edu.cn/contests/contest_show.php?cid=852 100 ...

随机推荐

  1. 20191209 Linux就该这么学(5)

    5. 用户身份与文件权限 5.1 用户的身份和能力 Linux 系统的管理员之所以是 root,并不是因为它的名字叫 root,而是因为该用户的身份号码即 UID( User IDentificati ...

  2. python 三元运算、列表推倒式、字典推倒式、生成器生成式

    1.三元运算 name=input('姓名>>: ') res='SB' if name == 'alex' else 'NB' print(res) 2.列表推倒式 #1.示例 egg_ ...

  3. Spring JdbcTemplate 和 NamedParameterJdbcTemplate 使用

    1.简单介绍 DAO层 的一般使用常见的是MyBatis 和 Hibernate,但是Hibernate是重量级的,而且学习成本较高,Mybatis 需要编写大量配置文件及接口文件,对于简单的项目应用 ...

  4. 02: kubernetes安装

    参考官网:http://docs.kubernetes.org.cn/ 1.1 集群部署 1.集群结构 192.168.56.11 linux-node1 linux-node1.example.co ...

  5. Linux命令基础#1

    系统基础 三大部件:CPU 内存 IO 1.CPU :运算器 控制器 存储器 2.内存:CPU的数据只能从内存读取,且内存数据有易失性(页面) 3.IO:控制总线 数据总线(一个IO) OS原理: O ...

  6. 分位数回归及其Python源码

    分位数回归及其Python源码 天朗气清,惠风和畅.赋闲在家,正宜读书.前人文章,不得其解.代码开源,无人注释.你们不来,我行我上.废话少说,直入主题.o( ̄︶ ̄)o 我们要探测自变量 与因变量 的关 ...

  7. sql server 函数详解(1)字符串函数

    ASCII()函数 CHAR()函数 LEFT()函数 RIGHT()函数 LTRIM()函数 RTRIM()函数 STR()函数 字符串逆序的函数REVERSE() 计算字符串的长度函数LEN(st ...

  8. IDEA的快捷方式

    一,IDEA的快捷方式1,F8单步执行 2,F9运行调试 3,CTRL +鼠标左键=进入查看定义 4,CTRL+alt +鼠标左键=查看实现 5,Shift+F6重命名 6,alt +intsert= ...

  9. homebrew学习(三)之homebrew命令

    安装homebrew: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...

  10. scp 从另一台linux服务器拷贝文件或文件目录

    格式:scp [参数] [原路径] [目标路径] download 使用方法:scp -r root@127.0.0.1:/opt/soft/test /opt/soft/ scp -r 用户名@IP ...