多校训练

I - The old Padawan
Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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   9,   t 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 <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;

int c[100005],w[100005],s[100005],t[100005],tmp,N,n,m,k,i,cnt,pos,ans;

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(i=1;i<=m;i++)
            scanf("%d",&t[i]);
        s[n]=w[n];
        tmp=n-1;
        cnt=1;
        while(s[n]<=k&&tmp>0)
        {
            s[n]+=w[tmp];
            tmp--;
            cnt++;
        }
        c[n]=cnt;
        N=n-1;
        while(N)
        {
            s[N]=s[N+1]-w[N+1];
            cnt--;
            while(s[N]<=k&&tmp>0)
            {
                s[N]+=w[tmp];
                tmp--;
                cnt++;
            }
            c[N]=cnt;
            if(tmp<=0)
            {
                for(i=N;i>0;i--)
                    c[i]=i;
                break;
            }
            N--;
        }
        //处理每一个分散注意力的秒
        sort(t+1,t+m+1);
        t[0]=0;
        ans=0;
        pos=0;
        for(i=1;i<=m;i++)
        {
            tmp=(t[i]-t[i-1]);
            pos+=tmp;
            if(pos>n){//时间超出了全部拿完的时间
                pos-=tmp;
                break;
            }
            ans+=tmp;
            pos=pos-1-c[pos-1];

        }
        ans+=(n-pos);
        printf("%d\n",ans);
    }
    return 0;
}

方法二:二分

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MM 100000005
#define NN 100005

int n, m, k;
int a[NN], b[NN], sum[MM];

int binary_search(int x)
{
    int left = 0, right = n, res = 0;
    while(left<=right)
    {
        int mid=(left+right)/2;
        if(sum[mid]<x)
        {
            res = mid;
            left = mid+1;
        }
        else
            right = mid-1;
    }
    return res;
}

int main()
{
    while(scanf("%d%d%d", &n, &m, &k) != EOF)
    {
        sum[0] = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        for(int i = 0; i < m; i++)
            scanf("%d", b+i);
        b[m]=1234567890;
        m++;
        int now=0, ans=0, ok;
        for(int i = 0; i < m; i++)
        {
            if(n-now<b[i]-ans)//剩的小于下次分心时间,直接全拿了
            {
                ans+=n-now;
                break;
            }
            else
            {
                ok=b[i]-ans+now;//现在有的石头
                ans=b[i];
                now=binary_search(sum[ok-1]-k);//ok-1是因为这一分钟不能拿石头,二分查找当前能拥有的数量
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值