[USACO09DEC]音符Music Notes (二分、STL)

https://www.luogu.org/problem/P2969

 

题目描述

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. 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:
Beat:   0    1    2    3    4    5    6    ...
        |----|----|----|----|----|----|--- ...
        1111111111     :              :
                  22222:              :
                       333333333333333:

Here is a set of five queries along with the resulting answer:
   Query    Note
     2        2
     3        3
     4        3
     0        1
     1        1

约翰准备教他的奶牛们弹一首歌.这首歌由N(1<=n<= 50000)个音阶组成,第i个音阶要敲击Bi<=10000次.奶牛从第0时刻开始弹,因此他从0时刻到Bi-1时刻都是敲第1个音阶, 然后他从B1时刻到B1+B2-1时刻敲第2个音阶,从B1+B2到B1+B2+B3-1时刻敲第3个音阶……现在有q(i<q<50000)个问题:在时间段区间t,T+1内,奶牛敲的是哪个音阶?

输入描述:

* Line 1: Two space-separated integers: N and Q
* Lines 2..N+1: Line i+1 contains the single integer: Bi
* Lines N+2..N+Q+1: Line N+i+1 contains a single integer: Ti

输出描述:

* Lines 1..Q: Line i of the output contains the result of query i as a single integer.

示例1

输入

3 5
2
1
3
2
3
4
0
1

输出

2
3
3
1
1

 

题解:

二分查找

STL的upper_bound函数,找到第一个大于待查元素的值,用法见代码。类似的还有lower_bound,找到的是第一个不小于待查元素的值。

 

下面介绍一下STL中的lower_bound()和upper_bound():(原文链接:https://blog.csdn.net/qq_40160605/article/details/80150252)

lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。

在从小到大的排序数组中,

lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

在从大到小的排序数组中,重载lower_bound()和upper_bound()

lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。


STL写法:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 #include <math.h>
10 const int INF=0x3f3f3f3f;
11 typedef long long LL;
12 const int mod=1e9+7;
13 const double PI=acos(-1);
14 const int maxn=50010;
15 using namespace std;
16 
17 int n,q;
18 int B[maxn];
19 int ans[maxn];
20 
21 int main()
22 {
23     scanf("%d %d",&n,&q);
24     for(int i=1;i<=n;i++)
25     {
26         scanf("%d",&B[i]);
27         ans[i]=ans[i-1]+B[i];
28     }
29     
30     for(int i=0;i<q;i++)
31     {
32         int x;
33         scanf("%d",&x);
34         printf("%d\n",upper_bound(ans+1,ans+1+n,x)-ans);
35     }
36     return 0;
37 }

 

二分写法:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <map>
11 #include <math.h>
12 const int INF=0x3f3f3f3f;
13 typedef long long LL;
14 const int mod=1e9+7;
15 const double PI=acos(-1);
16 const int maxn=100010;
17 using namespace std;
18 //ios::sync_with_stdio(false);
19 //    cin.tie(NULL);
20 
21 int n,q;
22 int l,r,mid;
23 int B[50010];
24 int num[50010];
25 
26 int main()
27 {
28     scanf("%d %d",&n,&q);
29     for(int i=1;i<=n;i++)
30     {
31         scanf("%d",&B[i]);
32         num[i]=num[i-1]+B[i];
33     }
34     for(int i=1;i<=q;i++)
35     {
36         int x;
37         scanf("%d",&x);
38         int l,r;
39         l=1;
40         r=n;
41         while(l<=r)
42         {
43             int mid=(l+r)/2;
44             if(num[mid]<=x)
45                 l=mid+1;
46             else r=mid-1;
47         }
48         printf("%d\n",l);
49     }
50 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

       
上一篇:ROLLBACK - 退出当前事务


下一篇:目录:Matrix Differential Calculus with Applications in Statistics and Econometrics,3rd_[Magnus2019]