avatar
fireworks99
keep hungry keep foolish

Codeforces B.Eugeny and Play List

Description

n首歌曲,给出每首歌曲的总时长以及播放次数,给出一个时刻,输出这一时刻再放哪首歌曲(前开后闭)

http://codeforces.com/problemset/problem/302/B

二分(要简洁,防止死循环)

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int a[100005];
int sum[100005];

int main()
{
    int n, m, c, t;
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d%d", &c, &t);
        a[i] = c * t;
        sum[i] = sum[i - 1] + a[i];
    }
    for(int i = 0; i < m; ++i)
    {
        scanf("%d", &t);
        int l = 0, r = n, mid;
        while(r - l > 1)
        {
            mid = (l + r + 1) >> 1;
            if(sum[mid] >= t)
                r = mid;
            else
                l = mid;
        }
        cout << r << '\n';
    }
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy