Ural 1998 The old Padawan(二分)
2014-07-31 18:14
489 查看
点击打开题目链接
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.
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).
raises all the four stones off the ground and finishes the task.
题意:有一个人要举起m块石头,但是在举石头的过程中容易走神,在他走神的1s内,要向前掉至少Kkg的石头,求其至少多少步才能把石头都举起来。
二分判断现在举起的石头还有多少。然后一步一步来。
代码:
#include <cstdio>
#include <cstring>
#include<iostream>
#include<queue>
#include <algorithm>
#define N 100005
using namespace std;
int n,m,k;
int w
,t
,s
;
int binary(int x)
{
int l=0,r=n,mid,ss=0;
while(l<=r)
{
mid=(l+r)/2;
if(s[mid]<x)
{ l=mid+1;ss=mid;}
else
r=mid-1;
}
return ss;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(s,0,sizeof(s));
memset(w,0,sizeof(s));
memset(t,0,sizeof(s));
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
s[i]=s[i-1]+w[i];
}
for(int i=1;i<=m;i++)
scanf("%d",&t[i]);
t[++m]=2e9;
int sum=0,tmp=0,p=0;
for(int i=1;i<=m;i++)
{
if(n-p<t[i]-sum)
{
sum+=n-p;break;
}
else
{
tmp=t[i]-sum+p;
sum+=t[i]-sum;
p=binary(s[tmp-1]-k);
}
}
printf("%d\n",sum);
}
return 0;
}
1998. The old Padawan
Time limit: 0.5 secondMemory 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
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 heraises all the four stones off the ground and finishes the task.
题意:有一个人要举起m块石头,但是在举石头的过程中容易走神,在他走神的1s内,要向前掉至少Kkg的石头,求其至少多少步才能把石头都举起来。
二分判断现在举起的石头还有多少。然后一步一步来。
代码:
#include <cstdio>
#include <cstring>
#include<iostream>
#include<queue>
#include <algorithm>
#define N 100005
using namespace std;
int n,m,k;
int w
,t
,s
;
int binary(int x)
{
int l=0,r=n,mid,ss=0;
while(l<=r)
{
mid=(l+r)/2;
if(s[mid]<x)
{ l=mid+1;ss=mid;}
else
r=mid-1;
}
return ss;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(s,0,sizeof(s));
memset(w,0,sizeof(s));
memset(t,0,sizeof(s));
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
s[i]=s[i-1]+w[i];
}
for(int i=1;i<=m;i++)
scanf("%d",&t[i]);
t[++m]=2e9;
int sum=0,tmp=0,p=0;
for(int i=1;i<=m;i++)
{
if(n-p<t[i]-sum)
{
sum+=n-p;break;
}
else
{
tmp=t[i]-sum+p;
sum+=t[i]-sum;
p=binary(s[tmp-1]-k);
}
}
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- ural 1998 The old Padawan (模拟+二分)
- URAL 1998 The old Padawan 二分
- URAL-1998 The old Padawan 二分
- (校赛)URAL 1998 The old Padawan
- URAL 1948 H - The Robot on the Line 二分 + 数学
- ural 1998 The old Padawan
- URAL 1721 Two Sides of the Same Coin(二分匹配)
- ACM ICPC 2013-2014 I. The old Padawan(二分+简单模拟)
- URAL 1998 The old Padawan
- The old Padawan(二分)
- ural 1998 The old Padawan
- URAL 1721. Two Sides of the Same Coin 二分匹配 构图
- URAL 1949 The Best Picture in the Galaxy 二分匹配 求字典序最小解
- Ural 1998 The old Padawa
- URAL 1980 Road to Investor(二分+最短路)
- 【二分】Petrozavodsk Winter Training Camp 2017 Day 1: Jagiellonian U Contest, Monday, January 30, 2017 Problem A. The Catcher in the Rye
- hdu The Frog's Games(二分枚举)
- URAL 1994 The Emperor's plan 概率dp
- URAL 1920 Titan Ruins: the Infinite Power of Magic
- URAL 2018 The Debut Album (DP)