![NC24866 [USACO 2009 Dec S]Music Notes](https://img2022.cnblogs.com/blog/2521724/202207/2521724-20220728102331461-1958678954.png)
FJ is going to teach his cows how to play a song. The song consists of N (1 <= N <= 50,000) notes, and the i-th note lasts for Bi (1 <= Bi <= 10,000) beats (thus no song is longer than 500,000,000 beats). The cows will begin playing the song at time 0; thus, they will play note 1 from time 0 through just before time B1, note 2 from time B1 through just before time B1 + B2, etc.
However, recently the cows have lost interest in the song, as they feel that it is too long and boring...
题目
1.题目大意
- 奶牛唱歌,n 个音符依次唱,每个音符持续一段时间,给一个时间,问在唱什么音
2.题目分析
- 二分加前缀和,需要注意初始化
b[0]=-1
,因为时间是从0
开始计时
- 比如第一个音符持续三秒,那么第二秒末是第一个音,第三秒时则是第二个音
3.题目代码
#include <bits/stdc++.h>
using namespace std;
int b[500000008];
int main() {
int n, q, p;
cin >> n >> q;
b[0] = -1;
for(int i=1;i<=n;i++)
{
cin >> b[i];
b[i] += b[i-1];
}
while(q--)
{
cin >> p;
int ans;
int l = 1;
int r = n;
int mid;
while(l<=r)
{
mid = (l+r)/2;
if(b[mid]>=p)
{
r = mid - 1;
ans = mid;
}
else
l = mid + 1;
}
cout << ans << endl;
}
}