陈老师的多校联合 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 9, ti < ti+1).
Output
Print a single integer — the number of seconds young Skywalker needs to complete the exercise.
Sample Input
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
- 陈老师的多校联合 I题 二分
- UVALive 5691 二分图匹配||陈老师的多校联合20140818 J题
- 陈老师的多校联合 20140809 C题
- 陈老师的多校联合 20140808 E题
- 陈老师的多校联合 20140809
- 陈老师的多校联合20180808 map映射字符串
- 陈老师的多校联合20140815 ||uvalive 5881 区间处理
- 陈老师的多校联合20140816||spoj10237 bfs
- 陈老师的多校联合20140818||Uvalive 5683 模拟
- 陈老师的多校联合 D题 字符串处理起来挺麻烦
- 陈老师的多校联合2010811c题||09年武汉站||hdu3232
- 陈老师的多校联合2009年武汉站D题 ||hdu 3033
- 陈老师的多校联合20140816A题||spoj 10228 动态规划
- 陈老师的多校联合20140811||HDU 3236 ||2009年武汉站G题 01背包问题
- 陈老师的多校联合20140816||SPOJ AMR11B 三角形的有向面积
- 暑期第一次多校联合赛 I题 数学题
- 陈老师的艺术人生
- 2013 多校联合 I I-number(hdu 4608)
- H-2023-求平均成绩
- Spring配置数据源
- 主函数创建5个学生的数组,写一个排序函数,让学生按姓名从小到大排序,主函数输出排序后的结果
- 集合(Collections-替换反转)
- 数据结构笔记之线性表的静态链表
- 陈老师的多校联合 I题 二分
- hdu2101 A + B Problem Too
- linux下如何共享文件夹共享
- 集合(Collections-reverseOrder)
- 公司某项目前端技术积累
- 如何附加数据库
- Nginx学习——Nginx进程间的通信
- 遇到bower Arguments to path.join must be strings
- hdu 2555 人人都能参加第30届校田径运动会了