zoukankan      html  css  js  c++  java
  • URAL 1998 The old Padawan 二分

    The old Padawan

    题目连接:

    http://acm.timus.ru/problem.aspx?space=1&num=1998

    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 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 ≤ 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 9, ti < ti+1).

    Output

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

    Sample Input

    5 1 4
    1
    2
    3
    4
    5
    4

    Sample Output

    8

    Hint

    题意

    这个人每秒钟会捡起来一个物品

    这个人会在某些时间发呆,如果发呆的话,他会一直丢下物品,直到丢完或者丢下价值和超过k的物品

    丢下去的顺序是捡起来顺序的倒叙

    问你最少多少秒,可以把所有东西都捡起来

    题解:

    对于发呆这个东西,直接二分就好了,二分一直丢到哪儿

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const long long inf = 1LL<<50;
    const int maxn = 1e5+7;
    long long sum[maxn],t[maxn],a[maxn],k;
    int n,m;
    int main()
    {
        scanf("%d%d%lld",&n,&m,&k);
    	for(int i=1;i<=n;i++)
    		scanf("%lld",&a[i]);
    	for(int i=n;i>=1;i--)
    		sum[i]=sum[i+1]+a[i];
    	for(int i=1;i<=m;i++)
    		scanf("%lld",&t[i]);
    	t[m+1] = inf;
    	sum[0] = inf;
    	int now = 0;
    	long long ans = 0;
    	int j = 1;
    	while(1)
    	{
    		if(j==m+1)
    		{
    			ans = ans + n - now;
    			break;
    		}
    		if(t[j]>ans+n-now)
    		{
    			ans = ans + n - now;
    			break;
    		}
    		now = now + (t[j]-t[j-1]) - 1;
    		int l = 0,r = now,Ans = 1;
    		while(l<=r)
    		{
    			int mid = (l+r)/2;
    			if(sum[mid]-sum[now+1]>k)Ans=mid,l=mid+1;
    			else r=mid-1;
    		}
    		now = Ans - 1;
    		now = max(now,0);
    		ans = t[j];
    		j++;
    	}
    	cout<<ans<<endl;
    }
  • 相关阅读:
    jsp设置footer底部内容
    dashboard项目心得:
    深度和广度优先算法
    一个action读取另一个action里的session
    算法笔记-0302
    JAVA基础---面向对象
    Flutter 读写本地文件
    Dart 处理json,built_value库
    Flutter 页面入栈和出栈
    web项目如何使用Material Icons
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5370323.html
Copyright © 2011-2022 走看看