URAL1998-The old Padawan

1998. 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 ≤  nm ≤ 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 output
5 1 4
1
2
3
4
5
4
8

Notes

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)


题意:有n块石头,某人按顺序取石头,在m个时间时,他会按顺序掉石头,直到掉的石头重量大于k,问多少时间后他能拿完所有石头

解题思路:每次掉石头时,二分掉几块石头时石头重量大于k


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

const int INF=0x3f3f3f3f;
#define LL long long

int n,m;
LL k;
LL sum[100050],a[100050];
LL t[100050];

bool check(int x,int sum1)
{
    if(sum[sum1]-sum[sum1-x]>k) return 1;
    else return 0;
}

int main()
{
    while(~scanf("%d%d%lld",&n,&m,&k))
    {
        sum[0]=0;
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
        for(int i=1;i<=m;i++) scanf("%lld",&t[i]);
        int p=0;
        LL ans=0;
        t[0]=0;
        for(int i=1;i<=m;i++)
        {
            LL kk=t[i]-t[i-1]-1;
            ans+=kk+1;
            p+=kk;
            if(p>=n)
            {
                ans=ans-kk-1;
                p-=kk;
                break;
            }
            if(sum[p]<=k) p=0;
            else
            {
                int l=1,r=p,ans1;
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    if(check(mid,p)) {ans1=mid,r=mid-1;}
                    else l=mid+1;
                }
                p-=ans1;
            }
        }
        ans+=n-p;
        printf("%lld\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值