陈老师的多校联合 I题 二分

来源:互联网 发布:js绑定事件的方法 兼容 编辑:程序博客网 时间:2024/05/18 03:25

http://vjudge.net/contest/view.action?cid=51327#problem/I

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 wi (in kilograms) in the order Luke is going to raise them (1 ≤ wi ≤ 10 4). Next m lines contain moments ti, when Luke gets distracted by some events (1 ≤ ti ≤ 10 9ti < ti+1).

Output

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

Sample Input

inputoutput
5 1 4123454
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.
题目大意:

                  地上有一定顺序的石子,每个石子的重量已知,现一人在地上捡石子,一秒捡起来一个,但是在第b[i],秒要从新丢下至少重量大于k的石子,重新捡。问最少需要多长时间才能把所有的石子权不捡起来。

解题思路:

                   本来想着模拟枚举一下就能过,写完才发现数据竟然是10^5,  于是又换了中二分写法==

#include <stdio.h>#include <string.h>#include <iostream>using namespace std;int n,m,k;int s[100005],a[100005],b[100005];int erfen(int x){    int l=0,r=n;    int mid,ss=0;;    while(l<=r)    {        mid=(l+r)/2;        if(s[mid]<x)        {             ss=mid;             l=mid+1;        }        else            r=mid-1;    }    return ss;}int main(){    while(~scanf("%d%d%d",&n,&m,&k))    {        memset(a,0,sizeof(a));        memset(s,0,sizeof(s));        memset(b,0,sizeof(b));        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            s[i]=s[i-1]+a[i];        }        for(int i=1;i<=m;i++)            scanf("%d",&b[i]);        b[++m]=2e9;        int sum=0,p=0,temp;        for(int i=1;i<=m;i++)        {            if(n-p<b[i]-sum)            {                sum+=n-p;                break;            }            else            {                temp=p+b[i]-sum-1;                sum+=b[i]-sum;                p=erfen(s[temp]-k);            }        }        printf("%d\n",sum);    }    return 0;}

附上超时的代码:(不能白写)

#include <string.h>#include <stdio.h>#include <iostream>using namespace std;int n,m,k,a[100005];int queue[100005],stack[100005];int  main(){    while(~scanf("%d%d%d",&n,&m,&k))    {        for(int i=0;i<n;i++)            scanf("%d",&queue[i]);        for(int i=0;i<m;i++)            scanf("%d",&a[i]);        int x=0;        int y=0;        int t=0;        int xx=0;        int count=a[0];        while(x!=n)        {            int flag=1;            if(t+1!=count)            {               flag=0;               stack[y++]=queue[x++],t++;            }            else            {               //printf("\n\n");                t++;                int w=0;                while(w<=k)                {                   queue[--x]=stack[--y];                   w+=stack[y];                   if(y==0)                      break;                }                if(x<0)                    x=0,y=0;                if(++xx<m)                    count=a[xx];                else                    count=-99999;            }            /*if(flag==0)                printf("(%d,%d)\n",queue[x-1],stack[y-1]);            else                printf("(%d,%d)\n",queue[x],stack[y]);*/        }        printf("%d\n",t);    }    return 0;}/*5 2 4123452 4*/


0 0
原创粉丝点击