The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, …, ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
1 ≤ n ≤ 300
The input limitations for getting 50 points are:
1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40
题目描述:
给定我们n个背包,要将其中的东西往后面扔,越往后越好,但是扔的条件是只能扔到i+2^k的位置,k的大小可以随意变化,但是i + 2^k要小于或等于n,他要我们输出n-1行,分别表示将前i个背包全都扔掉,即让前i个数都为0,该扔几次,记住,一次只能扔一,即例2中前两个扔完要扔3次,
因此防止TL,直接用位运算来找到在规定范围内他的最大2^k,并且直接扔包
不多说,直接上代码:
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
long long n,a,ans,A[100010];
int main () {
cin >> n;
for(int i = 1;i < n;i++) {
cin >> a;
A[i] += a;
ans += A[i];//将现在的包和前面人过来的包一起扔到后面
cout << ans << "\n";
int k = n-i;
while(k&k-1) //找到在范围内2^n的最大取值
k &= k-1;
A[i+k] += A[i];//将包扔给范围内他所能扔的最远位置
}
return 0;
}
这是之前用暴力的方式来查每个位置上的包所要扔到的位置,这种做法可以过一部分例子,但是在时间要求很高的情况下几句很容易出问题了
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 1e5+10;
int A[maxn],B[17],C[maxn];
int main () {
int n;
B[0] = 1;
for(int i = 1;i <= 14;i++)
B[i] = B[i-1]*2;
cin >> n;
for(int i = 1;i < n;i++) {
cin >> A[i];
for(int j = 0;j <= 14;j++) {
if(i+B[j] > n) {
C[i] = B[j-1]+i;
break;
}
if(i+B[j] == n) {
C[i] = n;
break;
}
}
}
int ans = 0;
for(int i = 1;i <= n-1;i++) {
ans += A[i];
A[C[i]] += A[i];
cout << ans << "\n";
}
return 0;
}