训练赛1-1:[USACO 2009 Dec B]Shorter Musical Notes

题目描述:FJ is going to teach his cows how to play a song. The song consists of N (1 <= N <= 10,000) notes, and the i-th note lasts for Bi (1 <= Bi <= 120) beats (thus no song is longer than 1,200,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. Thus, to make sure his cows are paying attention, he asks them Q (1 <= Q <= 50,000) questions of the form, "In the interval from time T through just before time T+1, which note should you be playing?" The cows need your help to answer these questions which are supplied as Ti (0 <= Ti <= end_of_song).
Consider this song with three notes of durations 2, 1, and 3 beats:
image
image


题目大意:

FJ将教他的奶牛如何演奏歌曲。这首歌由N(1<=N<=10000)个音符组成,第i个音符持续Bi(1<=Bi<=120)拍(因此没有歌曲超过1200000拍)。奶牛将在时间0开始播放歌曲;因此,他们将在时间0到B1之前弹奏音符1,在时间B1到B1+B2之前弹奏音符2,以此类推。

然而,最近奶牛对这首歌失去了兴趣,因为它们觉得这首歌太长太无聊了。因此,为了确保他的母牛注意到了,他问他们Q(1<=Q<=50000)这样的问题,“从时间T到时间T+1之前,你应该演奏哪个音符?”奶牛需要您的帮助来回答这些问题,这些问题以Ti(0<=Ti<=歌曲结尾)的形式提供。


思路:

°利用a[]来保存每段时间的音符之差,比如a[2]就是时间1-2的差。
°运用前缀和公式,来计算当前时间对应的音符即可。

AC代码:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxN =1e6+10;
int a[maxN],b[maxN];
int main(){
    int n,m,j;
    int k = 0;
    scanf("%d%d",&n,&m);
    memset(a,0,sizeof(a));
    for(int i = 0;i<n;i++){
        scanf("%d",&j);
        k += j;// k == 当前时间
        a[k]=1;//每段时间的音符之差
    }
    b[0]=1;//因为0到b1时间都是1;
    for(int i = 1;i<k;i++) b[i] = b[i-1]+a[i];
    for(int i = 0;i<m;i++){
        scanf("%d",&j);
        printf("%d\n",b[j]);
    }
    return 0;
}
posted @ 2023-05-21 12:02  百香果mark  阅读(8)  评论(0)    收藏  举报