NEERC 2013, Eastern subregional contest

I. The old Padawan

Time limit: 0.5 second
Memory limit: 64 MB
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 k kilograms 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 ≤ 105, 1 ≤ k ≤ 109). Next n lines contain the stones’ weights wi (in kilograms) in the order Luke is going to raise them (1 ≤ wi ≤ 104). Next m lines contain moments ti, when Luke gets distracted by some events (1 ≤ ti ≤ 109, ti < ti+1).

Output

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

Sample

inputoutput
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.
Problem Author: Denis Dublennykh (prepared by Egor Shchelkonogov)
 
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
using namespace std;
typedef long long LL ;
const int Max_N=100008 ;
int stone[Max_N] ;
int T[Max_N] ,N ,M ,k ;
int Sum_st[Max_N] ;
int find_id(int id){
   int Left ,Right ,Mid ,ans;
   if(Sum_st[1]-Sum_st[id+1]<=k)
      return 0 ;
   Left=1 ;
   Right=id ;
   while(Left<=Right){
       Mid=(Left+Right)>>1 ;
       if(Sum_st[Mid]-Sum_st[id+1]>k){
           ans=Mid ;
           Left=Mid+1 ;
       }
       else
           Right=Mid-1 ;
   }
   return ans-1 ;
}
int gao(){
    int stone_j=0 ,sum ,ans=0 ;
    T[0]=0 ;
    for(int i=1;i<=M;i++){
           /* for(int t=1;t<=T[i]-T[i-1]-1;t++){
                stone_j++ ;
                ans++ ;
                if(stone_j==N)
                    return ans ;
            }*/
            stone_j+=(T[i]-T[i-1]-1) ;
            ans+=(T[i]-T[i-1]-1) ;
            if(stone_j>=N)
                return ans-(stone_j-N) ;
            stone_j=find_id(stone_j) ;
            ans++ ;
    }
    if(stone_j<N)
        return ans+N-stone_j ;
}
int main(){
    while(scanf("%d%d%d",&N,&M,&k)!=EOF){
        for(int i=1;i<=N;i++)
            scanf("%d",&stone[i]) ;
        Sum_st[N+1]=0 ;
        for(int i=N;i>=1;i--)
            Sum_st[i]=Sum_st[i+1]+stone[i];
        for(int i=1;i<=M;i++)
            scanf("%d",&T[i]) ;
        printf("%d
",gao()) ;
    }
    return 0 ;
}
原文地址:https://www.cnblogs.com/liyangtianmen/p/3460382.html