Eugeny and Play List
来源:互联网 发布:淘宝如何延长退货时间 编辑:程序博客网 时间:2024/05/21 07:47
Eugeny loves listening to music. He has n songs in his play list. We know that song number i has the duration of ti minutes. Eugeny listens to each song, perhaps more than once. He listens to song number i ci times. Eugeny's play list is organized as follows: first song number 1 plays c1 times, then song number 2 plays c2 times, ..., in the end the song number n plays cn times.
Eugeny took a piece of paper and wrote out m moments of time when he liked a song. Now for each such moment he wants to know the number of the song that played at that moment. The moment x means that Eugeny wants to know which song was playing during the x-th minute of his listening to the play list.
Help Eugeny and calculate the required numbers of songs.
The first line contains two integers n, m (1 ≤ n, m ≤ 105). The next n lines contain pairs of integers. The i-th line contains integers ci, ti(1 ≤ ci, ti ≤ 109) — the description of the play list. It is guaranteed that the play list's total duration doesn't exceed 109 .
The next line contains m positive integers v1, v2, ..., vm, that describe the moments Eugeny has written out. It is guaranteed that there isn't such moment of time vi, when the music doesn't play any longer. It is guaranteed that vi < vi + 1 (i < m).
The moment of time vi means that Eugeny wants to know which song was playing during the vi-th munite from the start of listening to the playlist.
Print m integers — the i-th number must equal the number of the song that was playing during the vi-th minute after Eugeny started listening to the play list.
1 22 81 16
11
4 91 22 11 12 21 2 3 4 5 6 7 8 9
112234444
思路:因为CD播放的时间宽度达10^9,所以无法数组直接保存下来,但是总的时间宽度也同时只为10^9,而且CD总共只有10^5个,那么只要记载下相应的时间点,也就是最多只有10^5+1个,如果直接查询的话,O(N)的算法,但是有m=10^5组询问数组,TLE,所以二分查询即可,log(n)秒杀,因为是单调有序的上升时刻.
AC Program:
#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>typedef long long ll;#defineclr(a)memset((a),0,sizeof (a))#definerep(i,a,b)for(int i=(a);i<(int)(b);i++)#defineper(i,a,b)for(int i=((a)-1);i>=(int)(b);i--)#defineinf0x7ffffff#defineeps1e-6using namespace std;int d[100005];int flag;void binary_search(int q,int a,int b){//jump out while a=b-1 if(flag)return; if(d[a]>q || d[b]<q)return; if(a==b-1){ cout<<b<<endl; flag=1; return; } binary_search(q,a,(a+b)/2);//binary_search(q,a,b/2); binary_search(q,(a+b)/2,b); }int main(){ //保持n+1段,在n和n+1段则是属于n的标号的cd int n,m; cin>>n>>m; d[0]=1; int kg=1; int c,t; int sum=0; for(int i=0;i<n;i++){ cin>>c>>t; sum+=c*t; d[kg++]=sum; } int q; while(m--){ cin>>q; flag=0; binary_search(q,0,kg-1); } //system("pause"); return 0;}
- Eugeny and Play List
- B. Eugeny and Play List
- B. Eugeny and Play List
- B. Eugeny and Play List
- codeforces 302B. Eugeny and Play List
- codeforces 302 B. Eugeny and Play List
- codeforces 302B - Eugeny and Play List
- codeforces 302B. Eugeny and Play List
- CodeForces 302B Eugeny and Play List(遍历)
- A. Eugeny and Array
- Eugeny and Array
- CF:Eugeny and Array
- A. Eugeny and Array
- A.Eugeny and Array
- BNU Eugeny and Array
- M3U , PLSv2 and ASX Play List Specification
- M3U , PLSv2 and ASX Play List Specification
- Codeforce 302A Eugeny and Array
- 如何恢复新买电脑时的速度
- JQuery实例(三)- 动态修改Table中的内容
- 关于 Android Studio 在windows 8 下无法打开的问题
- ASP.NET BackgroundImageLayout属性值
- Geo Location 地理位置信息小结
- Eugeny and Play List
- UDP基础(一)
- 前端业者的双面人生-coder&designer
- 黑马程序员——生产者消费者问题之线程间通信
- Mac OS X使用技巧
- 修改Eclipse一行的最大长度
- SQL Server 查询语句的执行顺序及 Order By 深层解读
- rtsp protocol (7)
- 基础提供程序在 Open 上失败