The old Padawan(二分)

Description

Yoda: Use the Force. Yes. Now, the stone. Feel it. Concentrate!
Luke Skywalker is having exhausting practice at a God-forsaken planet Dagoba. One of his main difficulties is navigating cumbersome objects using the Power. Luke’s task is to hold several stones in the air simultaneously. It takes complete concentration and attentiveness but the fellow keeps getting distracted.
Luke chose a certain order of stones and he lifts them, one by one, strictly following the order. Each second Luke raises a stone in the air. However, if he gets distracted during this second, he cannot lift the stone. Moreover, he drops some stones he had picked before. The stones fall in the order that is reverse to the order they were raised. They fall until the total weight of the fallen stones exceeds  kkilograms or there are no more stones to fall down.
The task is considered complete at the moment when Luke gets all of the stones in the air. Luke is good at divination and he can foresee all moments he will get distracted at. Now he wants to understand how much time he is going to need to complete the exercise and move on.

Input

The first line contains three integers:  n is the total number of stones,  m is the number of moments when Luke gets distracted and  k (1 ≤  n, m ≤ 10  5, 1 ≤  k ≤ 10  9). Next  n lines contain the stones’ weights  w i (in kilograms) in the order Luke is going to raise them (1 ≤  w i ≤ 10  4). Next  m lines contain moments  t i, when Luke gets distracted by some events (1 ≤  t i ≤ 10  9t i <  t i+1).

Output

Print a single integer — the number of seconds young Skywalker needs to complete the exercise.

Sample Input

input output
5 1 4
1
2
3
4
5
4
8

Hint

In the first three seconds Luke raises stones that weight 1, 2 and 3 kilograms. On the fourth second he gets distracted and drops stones that weight 2 and 3 kilograms. During the next four seconds he raises all the four stones off the ground and finishes the task.
总结:
比赛的时候因为前面的一个模拟题没有做出来,然后心里比较烦躁,接下来又去做这道题脑子完全是一篇混乱,以后做题的时候一定要冷静,心态一定要平和,把思路完全理清了再开始敲代码,题目的具体分析见注释;
代码如下:
#include <iostream>
#include <cstdio>
using namespace std;

const int N =100004;

int sum[N],t[N];

int binary_search(int l,int r,int x){
    int mid;
    while(l<r){
        mid=(l+r)>>1;
        if(sum[mid]<x)
            l=mid+1;
        else r=mid;
    }
    return l;
}
int main(){
    int n,m,k;
    while(cin>>n>>m>>k){
        int tmp;
        sum[0]=0;
        for(int i=1;i<=n;i++){
            cin>>tmp;
            sum[i]=sum[i-1]+tmp;
        }
        for(int i=1;i<=m;i++)
            cin>>t[i];
        t[0]=0;
        int ans=0,o=0;
        for(int i=1;i<=m;i++){
            o+=t[i]-t[i-1]-1;//当前举起来的石头+这个时间段理论上可以举起的石头
            if(n<=o){//如果一共没有那么多石头
                ans+=n-(o-(t[i]-t[i-1]-1));
                break;
            }
            ans=t[i];//当前所话费的时间
            o=binary_search(1,o+1,sum[o]-k)-1;//放下>=K(kg)的石头后剩下的已经举起的石头
        }
        if(n>o)//没有愤怒点后,但是还没有把石头全部举起来
            ans+=n-o;
        cout<<ans<<endl;
    }
}
/******
6 2 5
1 2 3 4 5 6
3 5
*****/


你可能感兴趣的:(The old Padawan(二分))