Eugeny and Play List
题目描述:
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
1 2
2 8
1 16
样例输出 1
1
1
样例输入 2
4 9
1 2
2 1
1 1
2 2
1 2 3 4 5 6 7 8 9
样例输出 2
1
1
2
2
3
4
4
4
4
超时的代码
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int>>v;
int main(){
int n,m,c,t;
scanf("%d%d",&n,&m);
v.clear();
int sum=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&c,&t);
sum+=(c*t);
v.push_back({i,sum});
}
// for(auto i:v){
// cout<<i.first<<"-"<<i.second<<endl;
// }
int x=v.front().second;
int b;
for(int i=0;i<m;i++){
cin>>b;
while(x<b){
v.erase(v.begin());
x=v.front().second;
}
printf("%d\n",v.front().first);
}
return 0;
}
AC 代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,c,t;
scanf("%d%d",&n,&m);
int sum=0;
queue<pair<int,int>>v;
for(int i=1;i<=n;i++){
scanf("%d%d",&c,&t);
sum+=(c*t);//时间累加
//i:第i首歌
//sum:第i首歌曲结束的时间
v.push({i,sum});
}
int x=v.front().second;
int val;
for(int i=0;i<m;i++){
cin>>val;
while(x<val){
//如果时间比所询问的要小,弹出队首元素
//x的值更新
v.pop();
x=v.front().second;
}
printf("%d\n",v.front().first);
}
return 0;
}