BNU寒假弱校联盟C.Shorter Musical Notes(upper_bound的使用)
来源:互联网 发布:js 设置disabled 编辑:程序博客网 时间:2024/05/16 08:36
C. Shorter Musical Notes
Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld Java class name: MainSubmit Status PID: 4134
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 B_i (1<= B_i <= 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 B_1, note 2 from time B_1 through just before time B_1 + B_2, 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 T_i (0 <= T_i <= 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
Input
* Line 1: Two space-separated integers: N and Q
* Lines 2..N+1: Line i+1 contains the single integer: B_i
* Lines N+2..N+Q+1: Line N+i+1 contains a single integer: T_i
Output
* Lines 1..Q: Line i of the output contains the result of query i as a single integer.
Sample Input
3 521323401
Sample Output
23311
解题思路:
题目的意思就是先输入n个数字,然后q个询问,问每次询问的数字是1->x个数字中的哪一个了。
比如说,2 1 3,那么实际就是1 1 2 3 3 3 。。。怎么询问,你就怎么查数就好了。
upper_bound(x)是返回大于x的第一个元素。。。
代码:
# include<cstdio># include<iostream># include<algorithm>using namespace std;# define MAX 10000+4int a[MAX];int sum[MAX];int main(void){ int n,q; cin>>n>>q; for ( int i = 1;i <= n;i++ ) { scanf("%d",&a[i]); } for ( int i = 1;i <= n;i++ ) { sum[i]=sum[i-1]+a[i]; } for ( int i = 0;i < q;i++ ) { int temp; scanf("%d",&temp); int p = upper_bound(sum+1,sum+n+1,temp)-sum; printf("%d\n",p); } return 0;}
0 0
- BNU寒假弱校联盟C.Shorter Musical Notes(upper_bound的使用)
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing bnu 52325
- ZOJ 3924 Musical Notes
- map::lower_bound/upper_bound的使用
- map::lower_bound/upper_bound的使用
- upper_bound()和lower_bound()的使用
- 2015弱校联盟(1) - C. Censor
- lower_bound()与upper_bound()函数的使用
- lower_bound与upper_bound还有fill的使用
- stlmap中upper_bound和 lower_bound的使用
- upper_bound()和low_bound函数的使用
- upper_bound 和 lower_bound 的简单使用
- lower_bound upper_bound 【 二分函数的使用】
- 寒假的c语言进阶
- 弱校联盟#con4海豹的队列
- C/C++-STL中lower_bound与upper_bound的用法
- [C++] upper_bound和lower_bound
- C++primer学习:关于upper_bound和lower_bound的探究.
- Yii2清除cookie的问题
- C++ STL 算法:Heap算法
- assets文件夹资源的访问
- 关于eclipse 开发java程序的要点
- 是什么造就了一个优秀的程序员?
- BNU寒假弱校联盟C.Shorter Musical Notes(upper_bound的使用)
- C++ 虚函数表解析
- 单链表翻转
- [LeetCode]LRU Cache
- Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)
- Lucene教程(二)lucene-4.10.3的Demo学习
- C语言基础——其他
- C#中的ListBox应用
- PAT B1032 挖掘机技术哪家强